//! Books and Cinemas' Films given to a synthetic database the way a person //! adds a kit tool: their own tables made as an imported tool's are, Books' //! sample rows created on asking, and every chip read. mod support; use base64::{engine::general_purpose::STANDARD, Engine}; use serde_json::{json, Value}; use support::{ mcp::{call_tool, mint_token}, Harness, }; /// The kit package `id` imported with its sample rows; answers the receipt. fn import_with_samples(h: &Harness, token: &str, id: &str) -> Value { let bytes = cotton_core::kit::archive(id).expect("a kit tool").expect("the kit packs"); call_tool(h, token, "import_tool", json!({"bytes": STANDARD.encode(bytes), "with_samples": true})) } /// The titles a chip reads, in its order. fn chip(h: &Harness, token: &str, table: &str, view: &str) -> Vec { let read = call_tool(h, token, "list_records", json!({"table": table, "view": view})); read["records"] .as_array() .unwrap_or_else(|| panic!("{view} reads a list: {read}")) .iter() .map(|record| record["data"]["title"].as_str().unwrap_or_default().to_string()) .collect() } /// RULE: Books and Films (`cinemas`) are added like any kit tool — their tables, being /// their own, are made when the tool arrives — and each chip reads. #[test] fn books_and_films_are_added_and_their_chips_read() { let mut h = Harness::bare(); let token = mint_token(&mut h, &["read", "write"], "Synthetic reader"); for id in ["books", "cinemas"] { let added = call_tool(&h, &token, "add_tool", json!({"tool_id": id})); assert_eq!(added["added"], true, "{id}: {added}"); } // Every view each tool ships, read from its package, never listed here. for id in ["books", "cinemas"] { let tool = cotton_core::kit::package(id).expect(id).expect("decodes").package.tool; assert!(!tool.views.is_empty(), "{id} ships views"); for view in &tool.views { let table = view.read.get("table").and_then(|table| table.as_str()).expect("a view reads a table"); let name = format!("{id}/{}", view.id); assert!(chip(&h, &token, table, &name).is_empty(), "{name}: add_tool brings no sample rows"); } } } /// RULE: Books imported with its sample rows reads them on its chips — each /// narrowed by status and in the chip's order. #[test] fn books_samples_read_on_their_chips() { let mut h = Harness::bare(); let token = mint_token(&mut h, &["read", "write"], "Synthetic reader"); let receipt = import_with_samples(&h, &token, "books"); assert_eq!(receipt["sample_records"], 5, "{receipt}"); assert_eq!(chip(&h, &token, "book", "books/reading"), ["The Lantern Orchard"]); assert_eq!(chip(&h, &token, "book", "books/want_to_read"), ["The Cartographer's Second Wife"]); assert_eq!(chip(&h, &token, "book", "books/finished"), ["Salt Roads of Kemmerin", "A Small Grammar of Rain"], "the last finished first"); assert_eq!(chip(&h, &token, "book", "books/all").len(), 5); } /// RULE: Cinemas' Films brings no sample rows; a series made in it takes /// seasons and a season episodes, each linking back to its whole as the /// cards' openings read them, and the chips narrow by kind and status. #[test] fn films_chips_read_and_a_series_holds_seasons_and_episodes() { let mut h = Harness::bare(); let token = mint_token(&mut h, &["read", "write"], "Synthetic reader"); let added = call_tool(&h, &token, "add_tool", json!({"tool_id": "cinemas"})); assert_eq!(added["added"], true, "{added}"); for (title, kind, status) in [("Paper Comets", "film", "want_to_watch"), ("The Quiet Harbour", "film", "watched"), ("Lowmarsh", "series", "watching")] { let made = call_tool(&h, &token, "create_record", json!({"table": "film", "data": {"title": title, "kind": kind, "status": status}})); assert!(made["record"]["id"].is_string(), "{title}: {made}"); } assert_eq!(chip(&h, &token, "film", "cinemas/films").len(), 2); assert_eq!(chip(&h, &token, "film", "cinemas/series"), ["Lowmarsh"]); assert_eq!(chip(&h, &token, "film", "cinemas/watched"), ["The Quiet Harbour"]); assert_eq!(chip(&h, &token, "film", "cinemas/a_to_z"), ["Lowmarsh", "Paper Comets", "The Quiet Harbour"], "A to Z is every film and series by title"); assert_eq!(chip(&h, &token, "film", "cinemas/all").len(), 3, "All is every film and series"); let films = call_tool(&h, &token, "list_records", json!({"table": "film", "where": {"title": "Lowmarsh"}})); let series = films["records"][0]["id"].as_str().unwrap_or_else(|| panic!("Lowmarsh: {films}")).to_string(); let season = |number: u32, title: &str| { let made = call_tool(&h, &token, "create_record", json!({"table": "film_season", "data": {"film": series, "number": number, "title": title, "year": "2021"}})); made["record"]["id"].as_str().unwrap_or_else(|| panic!("a season: {made}")).to_string() }; let (second, first) = (season(2, "The Dry Year"), season(1, "The Reed Year")); for (number, title) in [(2, "Cut Bank"), (1, "Low Water")] { let made = call_tool(&h, &token, "create_record", json!({"table": "film_episode", "data": {"season": first, "series": series, "number": number, "title": title, "minutes": 47, "aired": "2021-03-14"}})); assert!(made["record"]["id"].is_string(), "an episode: {made}"); } let seasons = call_tool(&h, &token, "list_records", json!({"table": "film_season", "where": {"film": series}})); assert_eq!(seasons["records"].as_array().map(Vec::len), Some(2), "{seasons}"); let episodes = call_tool(&h, &token, "list_records", json!({"table": "film_episode", "where": {"season": first}})); let titles: Vec<&str> = episodes["records"].as_array().unwrap().iter().filter_map(|record| record["data"]["title"].as_str()).collect(); assert_eq!(titles, ["Low Water", "Cut Bank"], "a season's episodes in number order"); let none = call_tool(&h, &token, "list_records", json!({"table": "film_episode", "where": {"season": second}})); assert_eq!(none["records"], json!([])); // An episode reads its season's number and its series' title through the // season (computed lookups), so its lobby card says "S1 E1" under "Lowmarsh". let pilot = &episodes["records"][0]; assert_eq!(pilot["data"]["season_number"], json!(1), "{pilot}"); assert_eq!(pilot["data"]["series_title"], json!("Lowmarsh"), "{pilot}"); let cinemas = cotton_core::kit::package("cinemas").expect("a kit tool").expect("the kit decodes").package; let definition = cinemas.tables.iter().find(|table| table.id == "film_episode").unwrap(); let card = cotton_core::records::card::resolve( definition.card.as_ref().unwrap(), definition, pilot["id"].as_str().unwrap(), pilot["data"].as_object().unwrap(), "Asia/Kolkata", chrono::Utc::now(), cotton_core::records::page::Words::shipped(), &cotton_core::rules::shell_lines::LineSet::shipped("cards").unwrap(), cotton_core::records::card::Around::default(), ) .expect("the episode's card resolves"); assert_eq!(card.fields["subtitle"], json!("S1 E1")); assert_eq!(card.fields["source"], json!("Lowmarsh")); }