use super::*; use crate::definitions::{builtin_table_definitions, leaf_edit_paths, with_edits}; use serde_json::json; fn shipped(id: &str) -> TableDefinition { builtin_table_definitions().iter().cloned().find(|definition| definition.id == id).expect("a built-in") } fn acts(definition: &TableDefinition) -> Vec { definition.links.iter().flatten().map(|link| link.label.clone()).collect() } /// RULE: A stack sets its face, or none; where the groups are kept is the shipped file's — only the photo list's albums — and a stack can never point at another table's records. #[test] fn a_stack_keeps_where_the_file_keeps_its_groups() { let people = shipped("person"); let slips = json!({"stack": "slips"}); // A face edit is accepted; the build may retain its own device handoffs // beside that face, so the entire drawn object is not an input snapshot. assert!(on_shipped(&people, "groups", &slips, Against::Write).is_ok()); assert_eq!(on_shipped(&people, "groups", &Value::Null, Against::Write).unwrap(), Value::Null); let elsewhere = json!({"stack": "slips", "kept_in": {"table": "album", "items": "album_item"}}); assert!(on_shipped(&people, "groups", &elsewhere, Against::Write).is_err()); let photos = shipped("media"); let kept = serde_json::to_value(&photos.groups).unwrap(); assert!(kept["kept_in"].is_object(), "the photo list keeps its groups in albums"); let mut covers = kept.clone(); covers["stack"] = json!("covers"); assert_eq!(on_shipped(&photos, "groups", &covers, Against::Write).unwrap(), covers); let mut loose = kept.clone(); loose.as_object_mut().unwrap().remove("kept_in"); assert!(on_shipped(&photos, "groups", &loose, Against::Write).is_err(), "the albums stay where they are"); assert!(on_shipped(&people, "groups", &json!({"stack": "towers"}), Against::Write).is_err()); } /// RULE: A person's edit of one definition field pins only that field, so a later shipped sibling reaches the database. #[test] fn a_leaf_edit_does_not_pin_its_section_siblings() { let note = shipped("note"); let mut row = serde_json::to_value(¬e).unwrap(); row["slip"]["subtitle"] = json!("title"); let mut newer = note.clone(); newer.slip.trailing = Some("published_at".into()); newer.validate().expect("the newer shipped slip draws"); let (drawn, kept) = with_edits(&newer, &row, &["slip.subtitle".into()]); assert_eq!(serde_json::to_value(&drawn.slip).unwrap()["subtitle"], json!("title")); assert_eq!(serde_json::to_value(&drawn.slip).unwrap()["trailing"], json!("published_at")); assert_eq!(kept, vec!["slip.subtitle"]); assert_eq!(leaf_edit_paths(¬e, &row, &["slip".into()]), vec!["slip.subtitle"]); } /// RULE: An edited picture keeps the person's selected form across a shipped photo-to-asset change, because a leaf edit must not vanish when the shipped JSON type changes. #[test] fn a_picture_leaf_survives_a_shipped_form_change() { let person = shipped("person"); let mut row = serde_json::to_value(&person).unwrap(); row["picture"] = json!({"photo":"photo:synthetic"}); let mut newer = person.clone(); newer.picture = Some(crate::definitions::ToolPicture::Asset("new-shipped-picture".into())); let (drawn, kept) = with_edits(&newer, &row, &["picture.photo".into()]); assert_eq!(serde_json::to_value(drawn.picture).unwrap(), row["picture"]); assert_eq!(kept, vec!["picture.photo"]); } fn columns_of(definition: &TableDefinition) -> Value { serde_json::to_value(&definition.columns).unwrap() } /// RULE: No shipped file keys a column in the owner's namespace or hides one: `own_` keys and `hidden` are a person's alone, so an added column can never collide with a shipped or a future shipped key — but for a column a kit tool contributes to the table (Birthdays' wish on a person), which the one definition the object serves holds at its contributed key, hidden, since the object adds no contributed column of its own. #[test] fn no_shipped_column_is_in_the_owners_namespace() { let contributed: Vec<(String, String)> = crate::kit::ids() .into_iter() .flat_map(|id| { let tool = crate::kit::package(id).expect("a kit tool").expect("the kit decodes").package.tool; tool.contributes.into_iter().map(move |contribution| (contribution.table, contributed_key(id, &contribution.column.key))) }) .collect(); // A column a retired tool contributed stays on its retired table, so the // owner's values in it are kept: Screenplays' status went with its tool // when Cotton Story took the screenplay tables' place (17 Nov 2026). let left_by_a_retired_tool = [("screenplay", "own_screenplays__status")]; for definition in builtin_table_definitions() { for column in &definition.columns { // Likewise every column a retired tool contributed to a table that stays: Pages' on the page table, // which Web pages keeps (22 Nov 2026). A live table only gains columns. let of_a_retired_tool = crate::retired::GONE_TOOLS.iter().any(|tool| column.key.starts_with(&contributed_key(tool, ""))); if left_by_a_retired_tool.contains(&(definition.id.as_str(), column.key.as_str())) || of_a_retired_tool { assert_eq!(column.hidden, Some(true), "{}.{} stays hidden", definition.id, column.key); continue; } if contributed.iter().any(|(table, key)| *table == definition.id && *key == column.key) { assert_eq!(column.hidden, Some(true), "{}.{} is contributed, so hidden", definition.id, column.key); continue; } assert!(!is_own_column(&column.key), "{}.{} is keyed in the owner's namespace", definition.id, column.key); assert!(column.hidden.is_none(), "{}.{} ships hidden", definition.id, column.key); } assert!(!definition.id.starts_with(OWN_VALUES_TABLE_PREFIX)); } } /// RULE: On a shipped table a person adds columns of his own and hides shipped ones; a shipped column is never removed or retyped — its engine reads it — and an added column is keyed own_…, of a type that holds its own value, saying nothing beyond its name, type and choices. #[test] fn a_person_adds_and_hides_columns_and_never_removes_or_retypes_one() { let people = shipped("person"); let mut wanted = columns_of(&people); let city = wanted.as_array().unwrap().iter().position(|column| column["key"] == "city").unwrap(); wanted[city]["hidden"] = json!(true); wanted.as_array_mut().unwrap().push(json!({"key": "own_met_at", "name": "Met at", "type": "text"})); wanted.as_array_mut().unwrap().push(json!({"key": "own_closeness", "type": "choice", "choices": ["near", "far"]})); let kept = on_shipped(&people, "columns", &wanted, Against::Write).unwrap(); assert_eq!(kept, wanted); let mut drawn = people.clone(); drawn.columns = serde_json::from_value(kept).unwrap(); drawn.validate().expect("the table with his columns draws"); let city_column = drawn.column("city").unwrap(); assert!(!city_column.shown_on(&serde_json::Map::new()), "a hidden column is not drawn"); let refused = |columns: Value| on_shipped(&people, "columns", &columns, Against::Write).unwrap_err(); let mut removed = columns_of(&people); removed.as_array_mut().unwrap().remove(city); assert!(refused(removed).contains("never removed")); let mut retyped = columns_of(&people); retyped[city]["type"] = json!("number"); assert!(refused(retyped).contains("never removed or retyped")); let mut renamed = columns_of(&people); renamed[city]["name"] = json!("Town"); assert_eq!(on_shipped(&people, "columns", &renamed, Against::Write).unwrap()[city]["name"], "Town"); let with = |extra: Value| { let mut columns = columns_of(&people); columns.as_array_mut().unwrap().push(extra); columns }; assert!(refused(with(json!({"key": "nickname", "type": "text"}))).contains("own_")); assert!(refused(with(json!({"key": "own_friend", "type": "link", "link_to": "person"}))).contains("also says")); assert!(refused(with(json!({"key": "own_friend", "type": "photo"}))).contains("cannot be a link")); assert!(refused(with(json!({"key": "own_mood", "type": "choice"}))).contains("choices")); assert!(refused(with(json!({"key": "own_note", "type": "text", "readonly": true}))).contains("also says")); let mut title_hidden = columns_of(&people); let title = title_hidden.as_array().unwrap().iter().position(|column| column["key"] == people.title.as_str()).unwrap(); title_hidden[title]["hidden"] = json!(true); assert!(refused(title_hidden).contains("always drawn")); assert!(on_shipped(&people, "columns", &Value::Null, Against::Write).is_err()); // A shipped column named by its key alone is the file's column. let named: Vec = people.columns.iter().map(|column| json!({"key": column.key})).collect(); assert_eq!(on_shipped(&people, "columns", &Value::Array(named), Against::Write).unwrap(), columns_of(&people)); } /// RULE: An added column rides every upgrade, and a hidden flag rides while its column still ships; a newer file's columns are taken whole, so a column it retypes or adds is the file's. #[test] fn added_columns_ride_an_upgrade() { // An older file shipped a column the newer one does not. let people = shipped("person"); let mut older = people.clone(); older.columns.push(serde_json::from_value(json!({"key": "retired", "type": "text"})).unwrap()); let mut row = serde_json::to_value(&older).unwrap(); let columns = row["columns"].as_array_mut().unwrap(); for column in columns.iter_mut().filter(|column| column["key"] == "city" || column["key"] == "retired") { column["hidden"] = json!(true); } columns.push(json!({"key": "own_met_at", "type": "text"})); row["slip"]["subtitle"] = json!(["own_met_at"]); let (drawn, _) = with_edits(&people, &row, &["slip".to_string(), "columns".to_string()]); assert_eq!(drawn.column("own_met_at").map(|column| column.column_type), Some(ColumnType::Text)); assert_eq!(drawn.column("city").and_then(|column| column.hidden), Some(true)); assert!(drawn.column("retired").is_none(), "a column the file dropped is gone, hidden or not"); assert_eq!(serde_json::to_value(&drawn.slip).unwrap()["subtitle"], json!(["own_met_at"])); } /// RULE: A shipped column's name, format, units, hidden and display readonly are look overlays; its key and type and presence stay fixed because the engine reads the typed column, and upgrades preserve valid look edits. #[test] fn shipped_column_look_changes_without_retyping_or_removal() { let note = shipped("note"); let mut columns = columns_of(¬e); let at = columns.as_array().unwrap().iter().position(|column| column["key"] == "updated_at").unwrap(); columns[at]["format"] = json!("ago"); columns[at]["name"] = json!("Last changed"); columns[at]["readonly"] = json!(true); columns[at]["hidden"] = json!(true); let kept = on_shipped(¬e, "columns", &columns, Against::Write).expect("look overlay accepted"); assert_eq!(kept[at], columns[at]); let (upgraded, _) = with_edits(¬e, &json!({"columns":columns}), &["columns".into()]); assert_eq!(serde_json::to_value(upgraded.columns).unwrap()[at], columns[at]); let mut retyped = columns.clone(); retyped[at]["type"] = json!("text"); assert!(on_shipped(¬e, "columns", &retyped, Against::Write).is_err()); let mut removed = columns.clone(); removed.as_array_mut().unwrap().remove(at); assert!(on_shipped(¬e, "columns", &removed, Against::Write).is_err()); } /// RULE: A column's value words are look data, so a person's label edit survives a shipped upgrade without changing the engine's stored choice keys. #[test] fn column_value_words_survive_an_upgrade_as_look_data() { let todo = shipped("todo"); let mut columns = columns_of(&todo); let at = columns.as_array().unwrap().iter().position(|column| column["key"] == "status").unwrap(); let choices = columns[at]["choices"].clone(); columns[at]["labels"] = json!({"open":"Still to do", "done":"Finished", "dropped":"Set aside", "missed":"Missed"}); let kept = on_shipped(&todo, "columns", &columns, Against::Write).expect("word map is editable look"); assert_eq!(kept[at]["labels"], columns[at]["labels"]); let (upgraded, _) = with_edits(&todo, &json!({"columns":columns}), &["columns".into()]); let upgraded = serde_json::to_value(upgraded.columns).unwrap(); assert_eq!(upgraded[at]["labels"], columns[at]["labels"]); assert_eq!(upgraded[at]["choices"], choices); } /// RULE: A made table accepts column look edits but keeps every column and its storage metadata fixed, so marking measured facts readonly never needs re-importing a signed package or changes their values. #[test] fn made_column_look_changes_without_changing_storage() { let mut made = shipped("note"); made.builtin = false; let mut columns = columns_of(&made); let at = columns.as_array().unwrap().iter().position(|c| c["key"] == "updated_at").unwrap(); columns[at]["name"] = json!("Measured on"); columns[at]["readonly"] = json!(true); columns[at]["format"] = json!("ago"); columns[at]["hidden"] = json!(true); columns[at]["placeholder"] = json!("Not measured"); assert_eq!(on_shipped(&made, "columns", &columns, Against::Write).unwrap(), columns); for key in ["type", "indexed", "required", "computed", "link_to", "choices"] { let mut changed = columns.clone(); changed[at][key] = json!("changed"); assert!(on_shipped(&made, "columns", &changed, Against::Write).is_err(), "{key} stays fixed"); } let mut removed = columns.clone(); removed.as_array_mut().unwrap().remove(at); assert!(on_shipped(&made, "columns", &removed, Against::Write).is_err()); let mut added = columns.clone(); added.as_array_mut().unwrap().push(json!({"key":"own_extra", "type":"text"})); assert!(on_shipped(&made, "columns", &added, Against::Write).is_err()); let mut duplicate = columns.clone(); duplicate.as_array_mut().unwrap().push(columns[at].clone()); assert!(on_shipped(&made, "columns", &duplicate, Against::Write).is_err()); assert!(on_shipped(&made, "columns", &Value::Null, Against::Write).is_err()); let named = Value::Array(made.columns.iter().map(|c| json!({"key":c.key})).collect()); assert_eq!(on_shipped(&made, "columns", &named, Against::Write).unwrap(), columns_of(&made)); } /// RULE: A column edit pins only that column's edited look key; later settings on the same column and labels on other columns still follow the shipped definition. #[test] fn a_column_leaf_does_not_pin_other_column_settings() { let people = shipped("person"); let mut row = serde_json::to_value(&people).unwrap(); let city = row["columns"].as_array_mut().unwrap().iter_mut().find(|column| column["key"] == "city").unwrap(); city["name"] = json!("Town"); let paths = leaf_edit_paths(&people, &row, &["columns".into()]); assert_eq!(paths, vec!["columns.city.name"]); let mut newer = people.clone(); newer.columns.iter_mut().find(|column| column.key == "city").unwrap().placeholder = Some("Enter city".into()); newer.columns.iter_mut().find(|column| column.key == people.title).unwrap().name = Some("New name label".into()); let (drawn, _) = with_edits(&newer, &row, &paths); assert_eq!(drawn.column("city").unwrap().name.as_deref(), Some("Town")); assert_eq!(drawn.column("city").unwrap().placeholder.as_deref(), Some("Enter city")); assert_eq!(drawn.column(&people.title).unwrap().name.as_deref(), Some("New name label")); } /// RULE: Keyed column overlays preserve the person's added-column order while fixed shipped columns remain in shipped order. #[test] fn added_column_order_comes_from_the_persons_list() { let people = shipped("person"); let mut row = serde_json::to_value(&people).unwrap(); row["columns"].as_array_mut().unwrap().extend([json!({"key":"own_zebra","type":"text"}), json!({"key":"own_apple","type":"text"})]); let paths = leaf_edit_paths(&people,&row,&["columns".into()]); let (drawn,_) = with_edits(&people,&row,&paths); let added: Vec<_> = drawn.columns.iter().filter(|column| column.key.starts_with("own_") && people.column(&column.key).is_none()).map(|column|column.key.as_str()).collect(); assert_eq!(added,vec!["own_zebra","own_apple"]); } /// One of the personal kit's tables, as the kit ships it. fn kit_table(id: &str) -> TableDefinition { let path = cotton_store::manifest_dir().join(format!("kits/personal/tables/{id}.json")); let definition = TableDefinition::from_json(&std::fs::read_to_string(path).expect("the kit's file")).expect("the kit's table reads"); definition.validate().expect("the kit's table draws"); definition } /// RULE: A stack the kit newly ships lands on a stored definition whose owner never set one, and an owner's own stack (or his none) still wins over it. #[test] fn a_newly_shipped_stack_lands_where_the_owner_set_none() { for id in ["event", "film", "book", "artist"] { let shipped = kit_table(id); let stack = shipped.groups.clone().unwrap_or_else(|| panic!("{id} ships its groups")); // The row as an object held it before the kit declared groups, // with an owner's edit elsewhere riding along. let mut row = serde_json::to_value(&shipped).unwrap(); row.as_object_mut().unwrap().remove("groups"); row["empty"] = json!(null); let (drawn, _) = with_edits(&shipped, &row, &["empty".into()]); assert_eq!(drawn.groups.as_ref(), Some(&stack), "{id} takes the shipped stack"); let mut own = serde_json::to_value(&shipped).unwrap(); let other = if stack.stack.as_str() == "covers" { "slips" } else { "covers" }; own["groups"] = json!({"stack": other}); let (drawn, kept) = with_edits(&shipped, &own, &["groups".into()]); assert_eq!(serde_json::to_value(&drawn.groups).unwrap(), json!({"stack": other}), "{id} keeps the owner's stack"); assert_eq!(kept, vec!["groups.stack"], "{id}"); own["groups"] = Value::Null; let (drawn, _) = with_edits(&shipped, &own, &["groups".into()]); assert_eq!(drawn.groups, None, "{id} keeps the owner's none"); } } /// RULE: A table whose records are worked out keeps no groups: the core gathers none of its records. #[test] fn a_derived_table_keeps_no_groups() { let transaction = kit_table("transaction"); assert_eq!(transaction.derived, Some(true)); assert!(transaction.groups.is_none()); let mut grouped = transaction.clone(); grouped.groups = serde_json::from_value(json!({"stack": "slips"})).unwrap(); assert!(matches!(grouped.validate(), Err(crate::definitions::DefinitionError::GroupsOnDerived))); }