use super::*; use serde_json::json; #[derive(serde::Serialize, serde::Deserialize)] struct Inner { a: i64, #[serde(default, skip_serializing_if = "Vec::is_empty")] list: Vec, } #[derive(serde::Serialize, serde::Deserialize)] struct Outer { inner: Inner, rows: Vec, #[serde(flatten, default)] unknown: Unknown, } fn read(raw: &serde_json::Value) -> Outer { serde_json::from_value(raw.clone()).unwrap() } #[test] fn every_key_the_typed_read_does_not_carry_is_named_by_its_path() { let raw = json!({"inner": {"a": 1, "b": 2}, "rows": [{"a": 1}, {"a": 2, "c": 3}], "top": true}); let outer = read(&raw); assert_eq!(outer.unknown.get("top"), Some(&json!(true))); assert_eq!(unknown_keys(&raw, &outer, &outer.unknown), vec!["inner.b", "rows.1.c", "top"]); let raw = json!({"inner": {"a": 1, "listed": false}, "rows": []}); let outer = read(&raw); assert_eq!(unknown_keys(&raw, &outer, &outer.unknown), vec!["inner.listed"], "a misspelled key holding false"); } #[test] fn an_empty_value_an_encoder_leaves_absent_is_not_unknown() { let raw = json!({"inner": {"a": 1, "list": []}, "rows": [], "gone": null}); let outer = read(&raw); assert!(unknown_keys(&raw, &outer, &Default::default()).is_empty()); } fn shipped_table(id: &str) -> serde_json::Value { serde_json::to_value(crate::definitions::builtin_table_definition(id).unwrap()).unwrap() } /// RULE: A drawing word this build does not name — a newer device's tile face, toggle, page part or tone — never fails the read of the whole definition: it reads as its key's shipped default (the enum's declared default for a required key, absent for an optional one, left out of a list or a map) and the definition names each path it fell back on in `fallbacks`, so the app draws the rest and says one line (architect, 15 Oct 2026: the app never throws on a definition, now at the core's edge too). #[test] fn an_unknown_drawing_word_reads_as_its_keys_default_and_is_named() { use crate::definitions::{PagePart, TableDefinition, TileFace}; let mut raw = shipped_table("todo"); raw["tile"]["face"] = json!("zzz"); raw["page_parts"] = json!(["title", "comic", "facts"]); raw["slip"]["toggle_style"] = json!("slider"); let read = TableDefinition::from_json(&raw.to_string()).expect("a newer device's words read"); assert_eq!(read.tile.face, TileFace::default(), "a required face draws the shipped default"); assert_eq!(read.page_parts, Some(vec![PagePart::Title, PagePart::Facts]), "a list keeps what it can draw"); assert_eq!(read.slip.toggle_style, None, "an optional word reads as absent"); assert_eq!(read.fallbacks, vec!["page_parts.1", "slip.toggle_style", "tile.face"]); read.validate().expect("what fell back is drawable"); // Every decode path, not only from_json, reads the same way. let again: TableDefinition = serde_json::from_value(raw.clone()).unwrap(); assert_eq!(again.fallbacks, read.fallbacks); // A row that reads as it came names nothing; a stored `fallbacks` is never read back. let mut clean = shipped_table("todo"); clean["fallbacks"] = json!(["tile.face"]); assert!(TableDefinition::from_json(&clean.to_string()).unwrap().fallbacks.is_empty()); } /// RULE: A column's type has no safe default and no act to leave out: a newer device's one still refuses the definition (the catalogue draws the shipped copy), because a fallback would make the column hold a value under the wrong type. #[test] fn an_unknown_word_that_decides_a_write_still_refuses() { use crate::definitions::TableDefinition; let mut raw = shipped_table("todo"); raw["columns"][0]["type"] = json!("richtext"); assert!(TableDefinition::from_json(&raw.to_string()).is_err()); } /// RULE: An AUTHORED definition (a package, a built-in file) is held to the words this build draws: a word that would fall back on read is refused with its path, so a typo is never written as a default. #[test] fn an_authored_definition_refuses_an_unknown_drawing_word() { use crate::definitions::{DefinitionError, TableDefinition}; let mut raw = shipped_table("todo"); raw["tile"]["face"] = json!("zzz"); match TableDefinition::authored(&raw) { Err(DefinitionError::UnknownKeys(keys)) => assert!(keys.contains(&"tile.face zzz".to_string()), "{keys:?}"), other => panic!("accepted or refused for another reason: {other:?}"), } } /// RULE: Reading a row whole for the fallback pass refuses a key named twice, as the direct read did — `serde_json::Value` would keep the last one silently. #[test] fn the_whole_read_refuses_a_key_named_twice() { assert!(serde_json::from_str::(r#"{"a": 1, "b": {"c": 1, "c": 2}}"#).is_err()); assert_eq!(serde_json::from_str::(r#"{"a": [1, 2.5, "x", null, true]}"#).unwrap().0, json!({"a": [1, 2.5, "x", null, true]})); } /// RULE: An act holding a word that decides what it does — its kind, its class (`does`), its pick, a scheme — that this build does not name is left out whole as unavailable and named in `unavailable` (its path in `fallbacks`); every other act draws, and a part naming it still reads. A default there would make the act do something else, and refusing the whole tool over one word blanks it (architect, 15 Oct 2026). #[test] fn an_act_with_an_unknown_class_is_unavailable_and_the_rest_draw() { use crate::definitions::TableDefinition; let mut raw = shipped_table("todo"); let at = raw["commands"].as_array().unwrap().iter().position(|command| command["id"] == "drop").unwrap(); raw["commands"][at]["does"] = json!("archives"); let read = TableDefinition::from_json(&raw.to_string()).expect("one act's word never refuses the table"); read.validate().expect("parts naming the unavailable act still read"); assert_eq!(read.unavailable, vec!["drop"]); assert_eq!(read.fallbacks, vec![format!("commands.{at}.does")]); let left: Vec<&str> = read.commands.iter().flatten().map(|command| command.id.as_str()).collect(); assert!(!left.contains(&"drop") && left.contains(&"done") && left.contains(&"delete"), "{left:?}"); assert!(TableDefinition::authored(&raw).is_err(), "an authored definition names only acts this build runs"); } /// RULE: A tool's automation naming an action this build does not name never refuses the tool's read: it is named in the tool's `unavailable` and the tool and its other automations read; the write interpreter still refuses it (`parse`), so a write is refused whole rather than run without it. #[test] fn an_automation_with_an_unknown_action_is_unavailable_and_never_run_in_part() { use crate::definitions::{automations, ToolDefinition}; let raw = json!({"id": "garden", "name": "Garden", "tables": ["note"], "instructions": "", "author": {"device": "x", "name": "X"}, "automations": [ {"id": "ring", "name": "Ring", "when": {"table": "note", "column": "title", "to": "x"}, "then": [{"notify": {"text": "hi"}}, {"ring": {"loud": true}}]}, {"id": "tell", "name": "Tell", "when": {"table": "note", "column": "title", "to": "x"}, "then": [{"notify": {"text": "hi"}}]} ]}); let tool: ToolDefinition = serde_json::from_value(raw).expect("one automation's word never refuses the tool"); tool.validate().expect("the tool reads"); assert_eq!(tool.unavailable, vec!["ring"]); let read: Vec = automations::parse_known(&tool).unwrap().into_iter().map(|automation| automation.id).collect(); assert_eq!(read, vec!["tell"]); assert!(automations::parse(&tool).is_err(), "never run in part"); }