Cotton code
89 lines · 5.5 KB · RustRaw
use serde_json::json;use super::*;fn seen_counting() -> Value {    json!({ "state": "catching_up", "line": "Syncing 1,200 of 3,108", "percent": 38 })}/// RULE (FIRST SYNC STATES): a copy is in its first sync while it has not come whole, and, once its first pull has/// ended, while its first fill still owes a table the answer reads; the pictures' rows, which every first fill leaves/// for later, keep no other list loading. A last pass that failed says unreached.#[test]fn a_first_sync_lasts_until_what_the_answer_reads_has_come_once() {    let mail = Some(MAIL);    let owed = |tables: &[&str]| tables.iter().map(|t| t.to_string()).collect::<Vec<_>>();    assert_eq!(judged(Standing::Bringing, None, mail, false), Some(Standing::Bringing));    assert_eq!(judged(Standing::Unreached, None, None, true), Some(Standing::Unreached));    assert_eq!(judged(Standing::Waiting, None, None, false), Some(Standing::Waiting));    assert_eq!(judged(Standing::Held, None, mail, false), None, "nothing owed: over");    assert_eq!(judged(Standing::Held, Some(&owed(&["email", "media"])), mail, false), Some(Standing::Bringing), "Post's mail still coming after the first pull");    assert_eq!(judged(Standing::Held, Some(&owed(&["email", "media"])), mail, true), Some(Standing::Unreached));    assert_eq!(judged(Standing::Held, Some(&owed(&["media"])), mail, false), None, "only the pictures owed: Post's homes are whole");    assert_eq!(judged(Standing::Held, Some(&owed(&["media"])), None, false), None, "the pictures keep no list loading");    assert_eq!(judged(Standing::Held, Some(&owed(&["note"])), None, false), Some(Standing::Bringing));    assert_eq!(judged(Standing::Held, Some(&owed(&["note"])), mail, false), None, "a table Post does not read");}/// RULE: in a first sync an empty line says the things are coming, a count in words says the count Account says/// (wherever else the answer says the same words), a number stays a number, and the answer carries `firstSync`.#[test]fn an_answer_in_its_first_sync_says_its_things_are_coming() {    let first = said(Standing::Bringing, seen_counting());    assert_eq!(first["state"], "bringing");    assert_eq!(first["line"], "Loading your data…");    let mut home = json!({ "rows": [], "empty": "No mail here yet. Add an account to bring it in." });    apply(spec("mail.home").unwrap(), &first, &mut home);    assert_eq!(home["empty"], "Loading your data…");    assert_eq!(home["firstSync"]["seen"]["line"], "Syncing 1,200 of 3,108");    // Post's You says its first sync once itself (the card), never on a row (the owner, 26 Nov 2026): apply leaves it.    let mut you = json!({ "card": { "count": "Syncing 1,200 of 3,108" }, "groups": [{ "rows": [{ "title": "Storage" }, { "title": "Accounts" }] }] });    apply(spec("mail.you").unwrap(), &first, &mut you);    assert_eq!(you["card"]["count"], "Syncing 1,200 of 3,108");    assert_eq!(you["groups"][0]["rows"][0]["title"], "Storage");    // A slow or missed answer mid-sync keeps the loading words for a home, with the small line under them.    let missed = said(Standing::Unreached, seen_counting());    assert_eq!(missed["state"], "unreached");    assert_eq!(missed["bringing"], "Loading your data…");    assert_eq!(missed["trying"], "Can't reach Cotton right now. Trying again.");    assert!(first.get("trying").is_none(), "only when the last pass failed");    let not_counting = said(Standing::Bringing, json!({ "state": "up_to_date", "line": "Signed in" }));    let mut notes = json!({ "rows": [], "count": 0, "count_words": "0 notes", "empty": "No notes yet." });    apply(spec("notes.list").unwrap(), &not_counting, &mut notes);    assert_eq!((notes["count_words"].clone(), notes["count"].clone()), (json!("Loading your data…"), json!(0)), "no pass counting: the line; a number stays");    let mut vault = json!({ "total": 0, "empty": { "title": "Nothing saved yet" } });    apply(spec("vault.items").unwrap(), &first, &mut vault);    assert_eq!(vault["empty"]["title"], "Loading your data…");    let mut with_rows = json!({ "rows": [{ "id": "a" }], "empty": null });    apply(spec("mail.home").unwrap(), &first, &mut with_rows);    assert!(with_rows["empty"].is_null(), "an answer with rows says no empty line");    assert_eq!(with_rows["firstSync"]["state"], "bringing");}/// RULE: a refusal that a record or a table is not here is `not_here_yet` while the first sync runs ("No tool draws/// this table" is what a copy whose definitions have not come says); any other refusal is as it was.#[test]fn a_table_not_here_yet_is_said_as_coming() {    let first = said(Standing::Unreached, seen_counting());    let unread = refused(&first, MethodError::new("unreadable", "No tool draws this table."));    assert_eq!(unread.code, copy_standing::NOT_HERE_YET);    assert!(unread.message.starts_with("Cotton could not be reached just now"), "{}", unread.message);    assert_eq!(refused(&first, MethodError::new("write_refused", "No tool draws this table.")).code, copy_standing::NOT_HERE_YET, "Story's list");    assert_eq!(refused(&first, MethodError::new("not_found", "gone")).code, copy_standing::NOT_HERE_YET);    assert_eq!(refused(&first, MethodError::new("bad_request", "no id")).code, "bad_request");    assert_eq!(refused(&first, MethodError::new("unreadable", "the file is broken")).code, "unreadable");}/// Every spec names a method this build answers.#[test]fn every_spec_is_a_method_this_build_answers() {    let known = crate::methods::methods();    for spec in SPECS {        assert!(known.iter().any(|name| *name == spec.method), "{} is not answered", spec.method);    }}