//! The database's themes are records an assistant writes, shaped as boards//! (`boards_are_records.rs`): the `theme` table answers one record per theme//! the database can hold — paper and plain as shipped, and `database`, its//! own — and `update_record` of a definition writes the synced row//! `cotton_theme_definitions` holds, held by the server to exactly what this//! build draws. The newest theme the database wrote is the one it wears;//! a delete puts the default back. Every value is invented.mod support;use cotton_core::definitions::theme::ThemeDefinition;use cotton_tools::presentation::{theme, Drawn};use serde_json::{json, Value};use support::harness::Harness;use support::mcp::{call_tool_raw, mint_token, ToolAnswer};fn assistant() -> (Harness, String) {letmut h = Harness::new();let secret = mint_token(&mut h, &["read", "write"], "Example assistant"); (h, secret)}fn refused(answer: ToolAnswer, why: &str) -> String { assert!(answer.is_error, "{why}: the write was taken: {}", answer.text); answer.text}/// The theme the database wears now, as the phone's `definitions.theme` reads it.fn worn(h: &Harness) -> (Value, Drawn) {let resolved = h.with(|db| theme(db, &h.workspace_id)).expect("the theme reads"); (resolved.definition, resolved.drawn)}fn record(h: &Harness, secret: &str, id: &str) -> Value { call_tool_raw(h, secret, "get_record_by_table", json!({ "table": "theme", "id": id })).json()}fn plain_with_a_red_cap() -> Value { json!({ "id": "plain", "name": "Plain", "family": "plain", "cap": { "colour": "red" } })}/// RULE: The `theme` table lists paper, plain and the database's own theme; creation writes the shell's paper and plain at their defaults and the database's own theme with its cap colour, which it wears.#[test]fn every_theme_the_database_can_hold_is_a_record() {let (h, secret) = assistant();let listed = call_tool_raw(&h, &secret, "list_records", json!({ "table": "theme" })).json();let ids: Vec<&str> = listed["records"].as_array().expect("records").iter().filter_map(|r| r["id"].as_str()).collect(); assert_eq!(ids, ["paper", "plain", "database"]);let paper = record(&h, &secret, "paper"); assert_eq!(paper["data"]["drawn"], "default"); assert_eq!(paper["data"]["worn"], false);let own = record(&h, &secret, "database"); assert_eq!(own["data"]["worn"], true);let definition: Value = serde_json::from_str(own["data"]["definition"].as_str().unwrap()).unwrap(); assert_eq!(definition["id"], "database"); assert!(definition["cap"]["colour"].is_string(), "creation gave the database its cap: {definition}");}/// RULE: MCP theme reads and the app share one resolution, while reset keeps the stored sparse override in history so edits remain reversible.#[test]fn a_theme_written_is_worn_and_a_delete_puts_the_default_back() {let (h, secret) = assistant();let created = worn(&h);let own_rows = h.count("SELECT count(*) AS c FROM cotton_theme_definitions WHERE builtin = 0", &[]);// A write after creation is the newest row. h.clock.advance(chrono::Duration::minutes(1));letmut theme = plain_with_a_red_cap(); theme["acts"] = json!({ "destroys": { "stock": "tan" } });let written = call_tool_raw( &h, &secret,"update_record", json!({ "table": "theme", "id": "plain", "patch": { "definition": theme.to_string() } }), ); assert!(!written.is_error, "{}", written.text); assert_eq!(worn(&h), (ThemeDefinition::strict(&theme).unwrap().resolved(), Drawn::Row));let row = h .one("SELECT _hlc, builtin FROM cotton_theme_definitions WHERE id = 'plain'", &[]) .expect("the plain row"); assert!(row["_hlc"].as_str().is_some_and(|hlc| !hlc.is_empty()), "the row syncs: {row:?}"); assert_eq!(row["builtin"], json!(0));let read = record(&h, &secret, "plain"); assert_eq!(read["data"]["drawn"], "yours"); assert_eq!(read["data"]["worn"], true);let read_back: Value = serde_json::from_str(read["data"]["definition"].as_str().unwrap()).unwrap(); assert_eq!(read_back, ThemeDefinition::strict(&theme).unwrap().resolved()); assert_eq!(record(&h, &secret, "paper")["data"]["worn"], false);let deleted = call_tool_raw(&h, &secret, "delete_record", json!({ "table": "theme", "id": "plain" })); assert!(!deleted.is_error, "{}", deleted.text);let before: Value = serde_json::from_str(deleted.json()["change"]["before"]["definition"].as_str().expect("what it replaced")).unwrap(); assert_eq!(before, ThemeDefinition::strict(&theme).unwrap().resolved(), "history keeps the resolved theme it replaced"); assert_eq!(worn(&h), created, "the database wears what creation gave it again"); assert_eq!(record(&h, &secret, "plain")["data"]["drawn"], "default"); assert_eq!(h.count("SELECT count(*) AS c FROM cotton_theme_definitions WHERE builtin = 0", &[]), own_rows); refused( call_tool_raw(&h, &secret, "delete_record", json!({ "table": "theme", "id": "plain" })),"a delete of a theme already at its default", );}/// RULE: A malformed theme — not JSON, a family this build does not ship, an unknown key, a colour that is not one, another theme's id, a column Cotton sets — is refused by the server and nothing is saved; no theme is created and an unknown theme id is refused.#[test]fn a_malformed_or_unknown_theme_is_refused_and_never_saved() {let (h, secret) = assistant();let created = worn(&h);let own_rows = h.count("SELECT count(*) AS c FROM cotton_theme_definitions WHERE builtin = 0", &[]);letmut unknown_key = plain_with_a_red_cap(); unknown_key["sparkle"] = json!(true);letmut not_a_colour = plain_with_a_red_cap(); not_a_colour["cap"]["colour"] = json!("loud");letmut other_id = plain_with_a_red_cap(); other_id["id"] = json!("paper");let cases: [(&str, Value); 6] = [ ("not JSON", json!({ "definition": "{\"id\": \"plain\"," })), ("an unshipped family", json!({ "definition": json!({ "id": "plain", "family": "neon" }).to_string() })), ("an unknown key", json!({ "definition": unknown_key.to_string() })), ("a colour that is not one", json!({ "definition": not_a_colour.to_string() })), ("another theme's id", json!({ "definition": other_id.to_string() })), ("a column Cotton sets", json!({ "worn": true })), ];for (why, patch) in cases { refused( call_tool_raw(&h, &secret, "update_record", json!({ "table": "theme", "id": "plain", "patch": patch })), why, ); assert_eq!(worn(&h), created, "{why}: nothing was saved"); }let stray = json!({ "id": "neon", "family": "plain" }); refused( call_tool_raw(&h, &secret, "create_record", json!({ "table": "theme", "data": { "name": "Neon", "definition": stray.to_string() } })),"a create", ); refused( call_tool_raw(&h, &secret, "update_record", json!({ "table": "theme", "id": "neon", "patch": { "definition": stray.to_string() } })),"an update of an unknown theme", ); assert_eq!(h.count("SELECT count(*) AS c FROM cotton_theme_definitions WHERE builtin = 0", &[]), own_rows);}