//! The columns a person adds to a shipped table, and the shipped ones he //! hides (architect STEP 2a, piece 3): the definition write that makes them, //! the values kept beside the engine's record, and what a delete, the bin and //! a restore do to those values. //! //! Every row is synthetic and every value invented //! ([nothing-real-ever-goes-in-cotton-s-public-tree]). mod support; use cotton_core::definitions::builtin_table_definitions; use rusqlite::types::Value as SqlValue; use serde_json::{json, Value}; use support::harness::Harness; use support::mcp::{call_tool, call_tool_as_owner, call_tool_raw, mint_token}; /// `table`'s shipped columns named by key, `hide` hidden, then `added`. fn columns(table: &str, hide: &[&str], added: Value) -> Value { let shipped = builtin_table_definitions().iter().cloned().find(|d| d.id == table).expect("a built-in"); let mut list: Vec = shipped .columns .iter() .map(|column| match hide.contains(&column.key.as_str()) { true => json!({ "key": column.key, "hidden": true }), false => json!({ "key": column.key }), }) .collect(); list.extend(added.as_array().cloned().unwrap_or_default()); Value::Array(list) } /// The value a record's row holds under an added column, `None` when the /// row is not there. fn own_value(h: &Harness, table: &str, column: &str, id: &str) -> Option { h.one(&format!("SELECT {column} FROM {table} WHERE id = ?"), &[SqlValue::Text(id.to_string())]).map(|row| row[column].clone()) } /// RULE: A person adds a column to People and its value is the record's like any column's — written by update_record, read back by every read of the table, a real column of the engine's own table (cotton_people.own_met_at), never a shipped key. #[test] fn an_added_column_holds_its_value_beside_the_engines_record() { let mut h = Harness::new(); let secret = mint_token(&mut h, &["read", "write"], "Claude"); call_tool(&h, &secret, "update_definition", json!({ "table": "person", "columns": columns("person", &[], json!([{ "key": "own_met_at", "name": "Met at", "type": "text" }])), })); let made = call_tool(&h, &secret, "create_record", json!({ "table": "person", "data": { "full_name": "Example Ferngrove", "own_met_at": "the choir" }, })); let id = made["record"]["id"].as_str().expect("an id").to_string(); assert_eq!(made["record"]["data"]["own_met_at"], "the choir"); let read = call_tool(&h, &secret, "get_record_by_table", json!({ "table": "person", "id": id })); assert_eq!(read["data"]["own_met_at"], "the choir"); let changed = call_tool(&h, &secret, "update_record", json!({ "table": "person", "id": id, "patch": { "own_met_at": "a wedding" } })); assert_eq!(changed["record"]["data"]["own_met_at"], "a wedding"); let listed = call_tool(&h, &secret, "list_records", json!({ "table": "person" })); let row = listed["records"].as_array().unwrap().iter().find(|r| r["id"] == id.as_str()).cloned().unwrap(); assert_eq!(row["data"]["own_met_at"], "a wedding"); assert_eq!(own_value(&h, "cotton_people", "own_met_at", &id), Some(json!("a wedding")), "a column of the engine's own row"); // Undo puts the added value back through the same split. let undone = call_tool(&h, &secret, "undo", json!({ "change_id": changed["change"]["id"] })); assert_eq!(undone["record"]["data"]["own_met_at"], "the choir"); } /// RULE: A shipped column cannot be removed or retyped by update_definition — its engine reads it — and an added column outside the own_ namespace is refused; the table's columns stay the file's. #[test] fn a_shipped_column_cannot_be_removed_or_retyped() { let mut h = Harness::new(); let secret = mint_token(&mut h, &["read", "write"], "Claude"); let mut removed = columns("person", &[], json!([])); removed.as_array_mut().unwrap().retain(|column| column["key"] != "city"); let mut retyped = columns("person", &[], json!([])); for column in retyped.as_array_mut().unwrap().iter_mut().filter(|column| column["key"] == "city") { column["type"] = json!("number"); } for (columns, says) in [ (removed, "never removed"), (retyped, "never removed or retyped"), (columns("person", &[], json!([{ "key": "nickname", "type": "text" }])), "own_"), ] { let refused = call_tool_raw(&h, &secret, "update_definition", json!({ "table": "person", "columns": columns })); assert!(refused.is_error && refused.text.contains(says), "{}", refused.text); } let drawn = call_tool(&h, &secret, "update_definition", json!({ "table": "person" })); let shipped = builtin_table_definitions().iter().cloned().find(|d| d.id == "person").unwrap(); assert_eq!(drawn["definition"]["columns"], serde_json::to_value(&shipped.columns).unwrap()); } /// RULE: A hidden shipped column keeps its data and its engine: the page no longer draws it, while its value is still read and the engine still writes it to its own store. #[test] fn a_hidden_shipped_column_keeps_its_data_and_its_engine() { let mut h = Harness::new(); let secret = mint_token(&mut h, &["read", "write"], "Claude"); let made = call_tool(&h, &secret, "create_record", json!({ "table": "person", "data": { "full_name": "Example Hillcrest", "city": "Example Town" } })); let id = made["record"]["id"].as_str().unwrap().to_string(); call_tool(&h, &secret, "update_definition", json!({ "table": "person", "columns": columns("person", &["city"], json!([])) })); let drawn = call_tool(&h, &secret, "update_definition", json!({ "table": "person" })); let city = drawn["definition"]["columns"].as_array().unwrap().iter().find(|c| c["key"] == "city").cloned().unwrap(); assert_eq!(city["hidden"], true); assert_eq!(call_tool(&h, &secret, "get_record_by_table", json!({ "table": "person", "id": id }))["data"]["city"], "Example Town"); call_tool(&h, &secret, "update_record", json!({ "table": "person", "id": id, "patch": { "city": "Other Town" } })); let stored = h.one("SELECT city FROM cotton_people WHERE id = ?", &[SqlValue::Text(id.clone())]).unwrap(); assert_eq!(stored["city"], "Other Town", "the engine still writes it"); } /// RULE: An added column's values follow their record: a note in the bin keeps them and its restore draws them again; a note erased out of the bin takes them with it — nothing is left behind silently. #[test] fn added_values_follow_their_record_through_the_bin() { let mut h = Harness::new(); let secret = mint_token(&mut h, &["read", "write"], "Claude"); call_tool(&h, &secret, "update_definition", json!({ "table": "note", "columns": columns("note", &[], json!([{ "key": "own_mood", "type": "choice", "choices": ["calm", "busy"] }])), })); let made = call_tool(&h, &secret, "create_record", json!({ "table": "note", "data": { "title": "Example walk", "own_mood": "calm" } })); let id = made["record"]["id"].as_str().unwrap().to_string(); call_tool(&h, &secret, "delete_record", json!({ "table": "note", "id": id })); assert_eq!(own_value(&h, "cotton_notes", "own_mood", &id), Some(json!("calm")), "the binned note keeps its values"); call_tool(&h, &secret, "restore_from_bin", json!({ "kind": "note", "id": id })); assert_eq!(call_tool(&h, &secret, "get_record_by_table", json!({ "table": "note", "id": id }))["data"]["own_mood"], "calm"); call_tool(&h, &secret, "delete_record", json!({ "table": "note", "id": id })); call_tool_as_owner(&h, "device:phone", "empty_bin", json!({})).json(); assert_eq!(own_value(&h, "cotton_notes", "own_mood", &id), None, "an erased note's values go with its row"); } /// RULE: A shipped record its engine deletes outright (a retract) has its added values cleared with it — no value is left behind for a record that is not there — and the Undo that makes it again at the same id writes them back. #[test] fn a_retracted_records_added_values_are_cleared_and_come_back_with_its_undo() { let mut h = Harness::new(); let secret = mint_token(&mut h, &["read", "write"], "Claude"); call_tool(&h, &secret, "update_definition", json!({ "table": "person_handle", "columns": columns("person_handle", &[], json!([{ "key": "own_label", "type": "text" }])), })); let person = call_tool(&h, &secret, "create_record", json!({ "table": "person", "data": { "full_name": "Example Brookside" } })); let person = person["record"]["id"].as_str().unwrap().to_string(); let made = call_tool(&h, &secret, "create_record", json!({ "table": "person_handle", "data": { "person_id": person, "kind": "email", "value": "brookside@example.com", "own_label": "work", } })); let id = made["record"]["id"].as_str().unwrap().to_string(); let deleted = call_tool_as_owner(&h, "device:phone", "delete_record", json!({ "table": "person_handle", "id": id })).json(); assert_eq!(h.count("SELECT count(*) AS c FROM cotton_person_handles WHERE id = ?", &[SqlValue::Text(id.clone())]), 0, "retracted, its values with it"); call_tool_as_owner(&h, "device:phone", "undo", json!({ "change_id": deleted["change"]["id"] })).json(); let back = call_tool(&h, &secret, "get_record_by_table", json!({ "table": "person_handle", "id": id })); assert_eq!(back["data"]["own_label"], "work"); }