Cotton code
151 lines · 8.6 KB · RustRaw
use super::*;use crate::definitions::builtin_table_definitions;fn todo() -> TableDefinition {    builtin_table_definitions().iter().cloned().find(|table| table.id == "todo").expect("todo ships")}fn field(column: &str) -> PartsCard {    PartsCard { column: Some(column.into()), ..PartsCard::default() }}fn act(id: &str) -> PartsCard {    PartsCard { act: Some(id.into()), ..PartsCard::default() }}fn with(cards: Option<Vec<PartsCard>>, strip: Option<Vec<PartsCard>>) -> Result<(), DefinitionError> {    let mut table = todo();    let mut parts = table.resolved_parts();    parts.acts = PartsActs { main: Some("done".into()), more: vec![], away: None };    parts.cards = cards;    parts.strip = strip;    table.parts = Some(parts);    table.validate()}fn with_machine_card(card: PartsCard) -> Result<(), DefinitionError> {    let mut table = builtin_table_definitions()        .iter().cloned()        .find(|table| table.id == "machine_runner")        .expect("machine runner ships");    let mut parts = table.resolved_parts();    parts.cards = Some(vec![card]);    // The fixture tests this one card's contract.  Its derived strip may    // otherwise repeat a field already selected by the card under test.    parts.strip = None;    table.parts = Some(parts);    table.validate()}/// RULE: A newly named card key survives the tolerant definition scanner;/// otherwise an authored device card would retain it as unknown and refuse/// the next save.#[test]fn card_presentation_keys_are_known_to_the_tolerant_scanner() {    let mut table = todo();    let mut parts = table.resolved_parts();    parts.cards = Some(vec![PartsCard {        column: Some("due_at".into()),        when: Some(SectionWhen { column: "status".into(), values: Some(vec!["open".into()]), present: None }),        link_to: Some("project".into()),        clear_command: Some("done".into()),        pick_command: Some("done".into()),        ..PartsCard::default()    }]);    table.parts = Some(parts);    let raw = serde_json::to_value(&table).expect("definition serializes");    let parsed: TableDefinition = serde_json::from_value(raw.clone()).expect("new card keys deserialize");    assert!(crate::definitions::tolerant::unknown_keys(&raw, &parsed, &parsed.unknown).is_empty());}/// RULE: A page's slips are a sheet, a strip, or both (never an act on a strip beside a sheet); a card is one column or (strip only) one act; the title, a column and an act are each drawn once; a slip's acts are a field's and ones a slip fans; an object is a field card's only; a span's end is a date column; a stamp is never a card.#[test]fn a_hand_or_a_strip_draws_each_field_and_act_once() {    assert!(with(Some(vec![field("due_at"), PartsCard { width: Some(SlipWidth::Full), ..field("notes") }]), None).is_ok());    assert!(with(Some(vec![PartsCard { acts: Some(vec!["copy".into(), "clear".into()]), ..field("due_at") }]), None).is_ok());    assert!(with(None, Some(vec![act("reopen"), field("due_at")])).is_ok());    assert!(with(Some(vec![PartsCard { object: Some("bell".into()), ..field("due_at") }]), None).is_ok());    assert!(with(Some(vec![field("due_at")]), Some(vec![field("notes")])).is_ok(), "a strip under a hand");    assert!(with(Some(vec![PartsCard { stock: Some(CardStock::Sage), ..field("due_at") }]), None).is_ok());    assert!(with(Some(vec![PartsCard { until: Some("snoozed_until".into()), ..field("due_at") }]), None).is_ok(), "a span");    let refused = [        (Some(vec![field("due_at")]), Some(vec![act("reopen")]), "an act on the strip under a hand"),        (Some(vec![field("due_at")]), Some(vec![field("due_at")]), "a column in the hand and the strip"),        (Some(vec![act("reopen")]), None, "an act in the hand"),        (Some(vec![field("title")]), None, "the title"),        (Some(vec![field("due_at"), field("due_at")]), None, "a column twice"),        (None, Some(vec![act("done")]), "the tray's main act again"),        (None, Some(vec![act("nope")]), "an unknown act"),        (Some(vec![field("nope")]), None, "an unknown column"),        (Some(vec![PartsCard { acts: Some(vec!["fly".into()]), ..field("due_at") }]), None, "an act no slip fans"),        (Some(vec![PartsCard { column: Some("due_at".into()), act: Some("reopen".into()), ..PartsCard::default() }]), None, "a column and an act"),        (Some(vec![PartsCard::default()]), None, "neither"),        (None, Some(vec![PartsCard { object: Some("bell".into()), ..act("reopen") }]), "an object on an act"),        (Some(vec![PartsCard { object: Some("a bell".into()), ..field("due_at") }]), None, "an object that is no name"),        (Some(vec![PartsCard { until: Some("notes".into()), ..field("due_at") }]), None, "a span closed by words"),        (Some(vec![PartsCard { until: Some("nope".into()), ..field("due_at") }]), None, "a span closed by an unknown column"),        (Some(vec![field("updated_at")]), None, "a stamp, which is the facts line's"),        (None, Some(vec![field("created_at")]), "a stamp on the strip"),    ];    for (cards, strip, case) in refused {        assert!(with(cards, strip).is_err(), "refuses {case}");    }}/// Every value of a closed enum inside `parts` that the phone's decoder reads, as/// the cut before the card page (12 Oct 2026) knew them: Swift's/// `PageParts.Shape` is `sheet | desk` and nothing else in `parts` is a closed/// enum there. Frozen: a value added here reaches only builds that have it.const SHAPES_AN_OLDER_PHONE_READS: &[&str] = &["sheet", "desk"];/// RULE: Every built-in's parts use only enum values an older phone decodes, because its closed-enum decoder drops the whole definition on an unknown value; a new page word is a new optional key, never a new value of an old one.#[test]fn an_older_phone_reads_every_builtin_page() {    for (id, json) in crate::definitions::BUILTIN_DEFINITION_FILES {        let raw: serde_json::Value = serde_json::from_str(json).expect("parses");        let Some(shape) = raw.pointer("/parts/shape") else { continue };        let shape = shape.as_str().unwrap_or_else(|| panic!("{id}: parts.shape is not a word"));        assert!(SHAPES_AN_OLDER_PHONE_READS.contains(&shape), "{id}: parts.shape {shape:?} drops the definition on an older phone");    }}fn shipped_slips() -> SlipsLayout {    builtin_table_definitions()        .iter()        .find(|table| table.role == Some(super::super::TableRole::Database))        .and_then(|table| table.slips.clone())        .expect("the database table ships the slips table")}/// RULE: A slip offers its kind's whole list of acts, else the list for a kind the table leaves out, and never Clear where the record must keep the field; a card's own width and acts win over its kind's (slipacts, 18 Oct 2026: one list per type, so a type with one act does it on a tap and a type with more lifts its list — the list is the data, never a merge the app could see unevenly, R1).#[test]fn a_slip_offers_its_kinds_whole_list_and_never_clear_on_a_kept_field() {    let slips = SlipsLayout {        widths: OrderedMap(vec![("date".into(), SlipWidth::Half), ("longtext".into(), SlipWidth::Full)]),        acts: OrderedMap(vec![            ("date".into(), vec!["open_day".into(), "copy".into(), "edit".into(), "clear".into()]),            (SLIP_OTHER.into(), vec!["edit".into()]),        ]),        words: OrderedMap(vec![]),        objects: OrderedMap(vec![]),        list_rows: None,    };    let mut table = todo();    let mut parts = table.resolved_parts();    parts.cards = Some(vec![field("due_at"), PartsCard { width: Some(SlipWidth::Full), acts: Some(vec!["copy".into()]), ..field("notes") }, field("title")]);    table.deal_slips(&mut parts, &slips);    let cards = parts.cards.clone().unwrap();    assert_eq!(cards[0].width, Some(SlipWidth::Half));    assert_eq!(cards[0].acts.as_deref(), Some(&["open_day".to_string(), "copy".into(), "edit".into(), "clear".into()][..]));    assert_eq!(cards[1].width, Some(SlipWidth::Full), "the card's own width");    assert_eq!(cards[1].acts.as_deref(), Some(&["copy".to_string()][..]), "the card's own acts");    assert_eq!(cards[2].width, Some(SlipWidth::Full), "a kind the table leaves out is full");    assert_eq!(cards[2].acts.as_deref(), Some(&["edit".to_string()][..]), "a kind the table leaves out takes the other list");    // A due day the record must keep offers no Clear.    table.columns.iter_mut().find(|column| column.key == "due_at").unwrap().required = Some(true);    let mut parts = table.resolved_parts();    parts.cards = Some(vec![field("due_at")]);    table.deal_slips(&mut parts, &slips);    assert_eq!(parts.cards.unwrap()[0].acts.as_deref(), Some(&["open_day".to_string(), "copy".into(), "edit".into()][..]));}