Cotton code
117 lines · 6.0 KB · RustRaw
use super::*;use crate::definitions::builtin_table_definition;use serde_json::json;fn said(table: &str, verb: Verb, before: Value, after: Value, record: Option<Value>) -> Receipt {    let definition =        builtin_table_definition(table).unwrap_or_else(|| panic!("{table} is a built-in"));    receipt(definition, verb, &before, &after, record.as_ref(), crate::records::page::DateWords::shipped(), ReceiptWords::shipped())}/// RULE: Delete and restore retain their distinct receipt kinds; named-record propagation is covered by focused fixtures below.#[test]fn delete_and_restore_keep_their_state_and_supplied_name() {    for definition in crate::definitions::builtin_table_definitions() {        let before = json!({ definition.title.clone(): "fixture record title" });        let receipt = receipt(&definition, Verb::Delete, &before, &Value::Null, None, crate::records::page::DateWords::shipped(), ReceiptWords::shipped());        assert_eq!(receipt.kind, ReceiptKind::Deleted, "{}", definition.id);    }    let restored = said("secret", Verb::Restore, Value::Null, json!({ "name": "fixture record title" }), None);    assert_eq!(restored.kind, ReceiptKind::PutBack);    assert!(restored.said.contains("fixture record title"));}/// RULE: Move and move-in retain distinct stable kinds and carry no before/after word diff.#[test]fn a_move_receipt_is_static() {    let moved = said(        "note",        Verb::Move,        json!({ "title": "Bike shed list" }),        json!({ "title": "Bike shed list" }),        None,    );    assert_eq!(moved.kind, ReceiptKind::Moved);    assert!(moved.words.is_empty());    let received = said(        "note",        Verb::MoveIn,        json!({ "title": "Bike shed list" }),        json!({ "title": "Bike shed list" }),        None,    );    assert_eq!(received.kind, ReceiptKind::MovedIn);    assert!(received.words.is_empty());}/// RULE: A command state change remains a switch, and distinct missed-run deltas remain distinguishable without altering the record name.#[test]fn a_commanded_state_says_the_commands_word_and_counts_the_missed_run() {    let plants = Some(json!({ "title": "Water the plants" }));    let reopened = said("todo", Verb::Update, json!({ "status": "done" }), json!({ "status": "open" }), plants.clone());    assert_eq!(reopened.kind, ReceiptKind::Switched);    assert!(reopened.said.contains("Water the plants"));    let cleared = said(        "todo",        Verb::Update,        json!({ "status": "open", "completed_at": null }),        json!({ "status": "done", "completed_at": "2026-10-05T08:00:00.000Z", "@missed": { "was": ["a"], "now": ["a", "b", "c"] } }),        plants.clone(),    );    let one = said(        "todo",        Verb::Update,        json!({ "status": "open" }),        json!({ "status": "done", "@missed": { "was": [], "now": ["a"] } }),        plants.clone(),    );    let none = said(        "todo",        Verb::Update,        json!({ "status": "open" }),        json!({ "status": "done", "@missed": { "was": ["a"], "now": ["a"] } }),        plants,    );    for receipt in [&cleared, &one, &none] {        assert_eq!(receipt.kind, ReceiptKind::Switched);        assert!(receipt.said.contains("Water the plants"));    }    assert_ne!(cleared.said, one.said);    assert_ne!(one.said, none.said, "a run already missed is not cleared again");}/// RULE: A choice column's receipt form is picked by the column's kind in the definition, never by the look of its label: a state column (the slip's status, caption status or answers column) is said as the verb under the record's name, any other choice is said of the record — so a tool author knows which form their labels fill.#[test]fn a_state_column_says_the_verb_and_a_choice_is_said_of_the_record() {    let bin = builtin_table_definition("bin").expect("bin is built in");    assert!(bin.is_state_column("state"), "the bin's slip answers with its state");    let person = builtin_table_definition("person").expect("person is built in");    assert!(!person.is_state_column("language"), "a language is a choice, not a state");    let restored = said("bin", Verb::Update, json!({ "state": "binned" }), json!({ "state": "restored" }), Some(json!({ "title": "Dentist" })));    assert_eq!(restored.kind, ReceiptKind::Switched);    assert_eq!(restored.said, "Restored Dentist");    let language = said("person", Verb::Update, json!({ "language": "en" }), json!({ "language": "ta" }), Some(json!({ "full_name": "Ada" })));    assert_eq!(language.kind, ReceiptKind::Switched);    assert_eq!(language.said, "Marked Ada Tamil");}/// RULE: a changed receipt's other columns say their before and after as the page does; words, states and stamps are not among them.#[test]fn a_changed_day_says_its_before_and_after() {    let definition = builtin_table_definition("todo").unwrap();    let before = json!({"title": "An invented errand", "due_at": "2026-10-01", "status": "open", "updated_at": "2026-09-30T00:00:00Z"});    let after = json!({"title": "An invented errand", "due_at": "2026-10-03", "updated_at": "2026-10-01T00:00:00Z"});    let receipt = said("todo", Verb::Update, before.clone(), after.clone(), None);    assert_eq!(receipt.kind, ReceiptKind::Changed);    assert!(receipt.values.is_empty(), "the core's receipt leaves values to the caller holding a zone");    let now = chrono::DateTime::parse_from_rfc3339("2026-10-01T10:00:00+05:30").unwrap().with_timezone(&chrono::Utc);    let words = crate::records::page::Words::shipped();    let values = value_changes(definition, &before, &after, Some("Asia/Kolkata"), Some(now), words);    let column = definition.column("due_at").unwrap();    let page = |data: &Value| crate::records::page::fact_words(definition, column, &data["due_at"], data.as_object().unwrap(), Some("Asia/Kolkata"), Some(now), words);    assert_eq!(values, vec![WordsChange { name: column.label(), before: page(&before), after: page(&after) }]);    assert!(values[0].before.is_some() && values[0].after.is_some() && values[0].before != values[0].after);}