//! Books and Films: two kit packages whose tables are their own, not built//! in — data tables as an imported tool's are — read as a deck draws them.use cotton_core::definitions::package::{encode_unsigned_package, ToolPackage};use cotton_core::definitions::TableDefinition;use cotton_core::records::card::{resolve, Around, ResolvedCard};use cotton_core::records::page::Words;use cotton_core::rules::shell_lines::LineSet;use serde_json::{json, Value};fn package(id: &str) -> ToolPackage { cotton_core::kit::package(id).expect("a kit tool").expect("the kit decodes").package}fn table<'a>(package: &'a ToolPackage, id: &str) -> &'a TableDefinition { package.tables.iter().find(|table| table.id == id).unwrap_or_else(|| panic!("{} carries {id}", package.tool.id))}fn views(package: &ToolPackage) -> Vec<(&str, &str, bool)> { package.tool.views.iter().map(|view| (view.id.as_str(), view.label.as_str(), view.is_default)).collect()}/// A record's card as the deck is handed it, read on a fixed day.fn card(table: &TableDefinition, data: &Value) -> ResolvedCard {let now = chrono::DateTime::parse_from_rfc3339("2026-10-23T10:00:00+05:30").unwrap().with_timezone(&chrono::Utc);let cards = LineSet::shipped("cards").expect("the kit ships the cards set");let face = table.card.as_ref().unwrap_or_else(|| panic!("{} carries a card", table.id)); resolve(face, table, "r-1", data.as_object().unwrap(), "Asia/Kolkata", now, Words::shipped(), &cards, Around::default()) .expect("the card resolves")}/// RULE: Books and Films are kit packages whose tables are their own — none/// is built in, so none has compiled storage and each is created as an/// imported tool's table is — with their chips in the canvas's order and/// synthetic sample rows a create accepts.#[test]fn books_and_films_are_packages_of_their_own_tables() {let books = package("books");let films = package("cinemas");// vault_key: each keeps the vault's rows for its sealed files (R6, aa957bc31). assert_eq!(books.tool.tables, ["book", "highlight", "vault_key"]); assert_eq!(films.tool.tables, ["film", "film_season", "film_episode", "watch", "vault_key"]);for table in books.tables.iter().chain(&films.tables) { assert!(!cotton_core::definitions::is_builtin_table(&table.id), "{} is a package's own table", table.id); assert!(!table.builtin && table.engine.is_none(), "{} names no engine", table.id); } assert_eq!( views(&books), [("reading", "Reading", true), ("want_to_read", "Want to read", false), ("finished", "Finished", false), ("stopped", "Stopped", false), ("highlights", "Highlights", false), ("on_phone", "On this phone", false), ("all", "All", false)] );// Cotton Cinemas' fan, in its order (owner, 6 Nov 2026): it opens on// Films; All (the latest added first) and A to Z are every film and// series; the lists after A to Z, On this phone last. No Continue// watching: a film left part-way shows its strip on its poster. No// Watchlist: a list to watch is one of the person's own lists. No// samples: the first open is the doors. assert_eq!( views(&films), [("films", "Films", true), ("series", "Series", false), ("watched", "Watched", false), ("all", "All", false), ("a_to_z", "A to Z", false), ("on_phone", "On this phone", false)] ); assert_eq!(books.samples["book"].len(), 5); assert!(films.samples.is_empty(), "Cinemas ships no sample films");// Each carries its own picture, the one the Library kit drew it with.for package in [&books, &films] {letSome(cotton_core::definitions::ToolPicture::Photo(picture)) = &package.tool.picture else { panic!("{} has a picture", package.tool.id) };let name = format!("{}.png", picture.photo.trim_start_matches("photo:")); assert!(package.pictures.contains_key(&name), "{} carries {name}", package.tool.id); }}/// RULE: Books and Films are kit tools, so the Library kit names them as it/// names Todos — never a second, older Books carried inline that `add_tool`/// would install in the kit's place.#[test]fn the_library_kit_names_books_and_films_as_kit_tools() {use cotton_core::toolkits::KitTool;let library = cotton_core::toolkits::shipped().into_iter().find(|kit| kit.id == "library").expect("the Library kit ships");for id in ["books", "cinemas", "music"] { assert!(library.tools.iter().any(|tool| matches!(tool, KitTool::Kit(key) if key == id)), "Library names {id}"); }for package in cotton_core::toolkits::shipped_kit_tools() { assert!(!cotton_core::kit::ids().contains(&package.tool.id.as_str()), "a shipped kit carries kit tool {} inline", package.tool.id); }}/// The cards set's offer of a date on an undated record, which the media/// faces read as no date.fn add_date() -> Value { json!(LineSet::shipped("cards").unwrap().say("date.add", &[]).unwrap())}/// RULE: a book's card fills the places the standing hardback draws: its/// cover, title and author, how far through it the reader is (`used`, a/// share of a hundred, no `limit`), its pages as a number (`pages`), the day/// it was finished (`date`), the day it was last read (`last_read`), its/// rating in stars (`count`) and its review.#[test]fn a_book_reads_as_its_card() {let books = package("books");let book = table(&books, "book");let reading = card(book, &books.samples["book"][0]); assert_eq!(reading.face, "book.hardback"); assert!(book.card.as_ref().unwrap().offers.is_empty(), "a book wears the hardback alone"); assert_eq!(reading.fields["pages"], json!(312), "the pages, a number the face reckons the reader's page with"); assert!(reading.fields.get("last_read").is_none(), "a book never opened was never read"); assert_eq!(reading.fields["title"], json!("The Lantern Orchard")); assert_eq!(reading.fields["subtitle"], json!("Irena Valcourt")); assert!(reading.fields.get("used").is_none(), "a book the reader has not opened says no share"); assert!(reading.fields.get("limit").is_none(), "the share is of a hundred");letmut opened = books.samples["book"][0].clone(); opened["progress"] = json!(40); opened["position_at"] = json!("2026-10-22T21:30:00Z");let opened = card(book, &opened); assert_eq!(opened.fields["used"], json!(40)); assert_eq!(opened.fields["last_read"], json!("23 Oct"), "the day last read, in the reader's zone, as a day and month"); assert_eq!(reading.fields["date"], add_date(), "a book not finished has no day yet"); assert!(reading.fields.get("count").is_none(), "an unrated book says no rating");let read = card(book, &books.samples["book"][1]); assert_eq!(read.fields["date"], json!("Mon 21 Sep")); assert_eq!(read.fields["count"], json!("4")); assert_eq!(read.fields["body"], json!("Slow first third, then it would not let go."));let half = card(book, &books.samples["book"][2]); assert_eq!(half.fields["count"], json!("4.5"), "a half star is said");}/// RULE: the media faces label their day in the cards set's words, under/// `label.face.<face>.date` — a book's is the day it was Finished, a film's/// and an episode's the day it was Watched — never the plain "Date".#[test]fn the_media_faces_label_their_day() {let cards = LineSet::shipped("cards").unwrap();for (face, said) in [("book", "Finished"), ("film", "Watched"), ("episode", "Watched")] { assert_eq!(cards.say(&format!("label.face.{face}.date"), &[]).as_deref(), Some(said), "{face}"); }}/// RULE: a film's and an episode's cards say their places; a series opens/// into its seasons by number and a season into its episodes, and a film/// that is not a series is no whole.#[test]fn a_film_a_season_and_an_episode_read_as_their_cards() {let films = package("cinemas");let (film, season, episode) = (table(&films, "film"), table(&films, "film_season"), table(&films, "film_episode"));let harbour = json!({"title": "The Quiet Harbour", "year": "2019", "kind": "film", "status": "watched", "watched_on": "2026-09-12", "rating": 4, "review": "The long shot on the pier.", "minutes": 112, "position": 1800});let watched = card(film, &harbour); assert_eq!(watched.face, "film.poster"); assert_eq!(film.card.as_ref().unwrap().offers, ["film.poster"], "a film offers the one-sheet"); assert_eq!(watched.fields["title"], json!("The Quiet Harbour")); assert_eq!(watched.fields["year"], json!("2019")); assert_eq!(watched.fields["state"], json!("Watched")); assert_eq!(watched.fields["date"], json!("Sat 12 Sep")); assert_eq!(watched.fields["count"], json!("4")); assert_eq!(watched.fields["facts"], json!([{"label": "Kind", "said": "Film"}])); assert_eq!(watched.fields["body"], json!("The long shot on the pier."));let listed = card(film, &json!({"title": "Ember and Ash", "kind": "film", "status": "want_to_watch"})); assert_eq!(listed.fields["state"], json!("Want to watch"), "the status choice's label"); assert_eq!(listed.fields["date"], add_date(), "a film wanted, not yet watched, has no day watched yet");let opens = film.card.as_ref().and_then(|card| card.opens.as_ref()).expect("a series opens"); assert_eq!((opens.table.as_str(), opens.by.as_str(), opens.order.as_slice()), ("film_season", "film", &["number".to_string()][..]));let when = opens.when.as_ref().expect("only a series is a whole"); assert_eq!((when.column.as_str(), when.values.as_slice()), ("kind", &["series".to_string()][..]));let opens = season.card.as_ref().and_then(|card| card.opens.as_ref()).expect("a season opens"); assert_eq!((opens.table.as_str(), opens.by.as_str(), opens.order.as_slice()), ("film_episode", "season", &["number".to_string()][..])); assert!(opens.when.is_none(), "every season opens into its episodes");// film_season wears its own season card (ad6a7b5fbc).let first = card(season, &json!({"film": "film:1", "number": 1, "title": "The Reed Year", "year": "2021", "episodes": 3})); assert_eq!(first.face, "season.card"); assert_eq!(first.fields["title"], json!("The Reed Year")); assert_eq!(first.fields["number"], json!("1")); assert_eq!(first.fields["year"], json!("2021")); assert_eq!(first.fields["subtitle"], json!("3 episodes")); assert_eq!(first.fields["limit"], json!(3));// season_number and series_title are lookups through the season, as a// read answers them (the tools' test reads them off a real file).let pilot = card(episode, &json!({"season": "season:1", "number": 3, "title": "Low Water", "minutes": 47, "aired": "2021-03-14", "status": "watched", "watched_on": "2026-10-18", "rating": 4, "season_number": 2, "series_title": "Lowmarsh"})); assert_eq!(pilot.face, "episode.lobby"); assert_eq!(pilot.fields["title"], json!("Low Water")); assert_eq!(pilot.fields["subtitle"], json!("S2 E3"), "the lobby card's mark"); assert_eq!(pilot.fields["source"], json!("Lowmarsh")); assert_eq!(pilot.fields["state"], json!("Watched")); assert_eq!(pilot.fields["date"], json!("Sun 18 Oct"));}/// RULE: a package whose whole opens into parts it does not carry, by a link/// that is not back to it, or only while a value its column cannot hold, is/// refused when it is read — never dealt as nothing.#[test]fn films_with_a_bad_opening_are_refused() {let refused = |change: &dynFn(&mutToolPackage)| {letmut films = package("cinemas"); change(&mut films); encode_unsigned_package(&films).expect_err("a bad opening is refused").to_string() };let opens = |films: &mutToolPackage, table: &str| {let table = films.tables.iter_mut().find(|held| held.id == table).unwrap(); table.card.as_mut().unwrap().opens.as_mut().unwrap().clone() };let set = |films: &mutToolPackage, table: &str, opens: cotton_core::definitions::card::CardOpens| { films.tables.iter_mut().find(|held| held.id == table).unwrap().card.as_mut().unwrap().opens = Some(opens); };let why = refused(&|films| {letmut wrong = opens(films, "film"); wrong.by = "series".into(); set(films, "film", wrong); }); assert!(why.contains("film opens into film_season"), "a by column that is not the whole's link: {why}");let why = refused(&|films| {letmut wrong = opens(films, "film_season"); wrong.by = "series".into(); set(films, "film_season", wrong); }); assert!(why.contains("film_season opens into film_episode"), "series links to the film, not the season: {why}");let why = refused(&|films| { films.tables.retain(|table| table.id != "film_episode"); films.tool.tables.retain(|id| id != "film_episode"); }); assert!(why.contains("film_season opens into film_episode"), "the parts must come with the whole: {why}");let why = refused(&|films| {letmut wrong = opens(films, "film"); wrong.when.as_mut().unwrap().values = vec!["miniseries".into()]; set(films, "film", wrong); }); assert!(why.contains("film opens only while"), "a value the kind cannot hold: {why}");let why = refused(&|films| {letmut wrong = opens(films, "film"); wrong.order = vec!["episode_count".into()]; set(films, "film", wrong); }); assert!(why.contains("film opens into film_season"), "an order column the parts have not: {why}");}