Cotton code
122 lines · 6.7 KB · RustRaw
use super::*;const MIN: i64 = 60_000;/// The one sync line is chosen from typed states, in order — not reached,/// receiving, sending, fetching, reader only, up to date — and said in the/// shell's words, every one of which the shipped file has: a key it lacked/// would put an empty line on Home, which no compiler sees (synthetic facts;/// the real numbers come from the owner's phone).#[test]fn the_line_is_one_typed_state_said_in_the_shells_words() {    let words = SyncWords::shipped();    for key in Line::KEYS {        assert!(words.lines.get(*key).is_some_and(|said| !said.is_empty()), "kits/shell/lines/sync.json says {key}");    }    let now = 1_000 * MIN;    let level = Facts {        serving: Some("Studio Mac".into()),        contact: Contact { answered_at: Some(now - 6 * MIN), silent_since: None },        ..Facts::default()    };    let say = |facts: &Facts| {        let line = Line::of(facts, now);        (line.key(), words.say(&line, now))    };    assert_eq!(say(&level), ("up_to_date", "Up to date with Studio Mac · 6 min ago".into()));    let fetching = Facts { bytes: Bytes { to_fetch: Pile { count: Some(12), bytes: Some(340_000_000) }, ..Bytes::default() }, ..level.clone() };    assert_eq!(say(&fetching), ("fetching", "Fetching 12 photos and documents · 340 MB".into()));    let sending = Facts { receipts: Receipts { to_send: 1, ..Receipts::default() }, ..fetching.clone() };    assert_eq!(say(&sending), ("sending_one", "Sending 1 change to Studio Mac".into()));    // Records before bytes, and the Mac's changes before this device's.    let receiving = Facts { receipts: Receipts { behind: Some(41_000), at_least: false, to_send: 3, last_applied_at: None }, ..fetching.clone() };    assert_eq!(say(&receiving), ("receiving", "Receiving 41,000 changes from Studio Mac".into()));    // A reader sends nothing, whatever its outbox says.    let reader = Facts { role: Role::Reader, receipts: Receipts { to_send: 4, ..Receipts::default() }, ..level.clone() };    assert_eq!(say(&reader), ("reader_only", "Reading from Studio Mac · 6 min ago".into()));    // A missed pass is a blip; fifteen minutes unanswered is not reached.    let blip = Facts { contact: Contact { answered_at: Some(now - 6 * MIN), silent_since: Some(now - 2 * MIN) }, ..receiving.clone() };    assert_eq!(say(&blip).0, "receiving");    let away = Facts { contact: Contact { answered_at: Some(now - 3 * 60 * MIN), silent_since: Some(now - 170 * MIN) }, ..receiving };    assert_eq!(say(&away), ("not_reached", "Studio Mac not reached since 3 h ago".into()));    let never = Facts { serving: None, contact: Contact::default(), ..Facts::default() };    assert_eq!(say(&never), ("not_reached_never", "Your Mac not reached yet".into()));    // What came since the app opened is counted by tool; a table no tool    // draws is the words' other.    let received = BTreeMap::from([("cotton_todos".to_string(), 5), ("cotton_nobody".to_string(), 2), ("cotton_empty".to_string(), 0)]);    let status = Status::of(level, &received, |table| (table == "cotton_todos").then_some("Todos"), None, words, now);    assert_eq!(status.received_since_open, BTreeMap::from([("Other".to_string(), 2), ("Todos".to_string(), 5)]));    let json = serde_json::to_value(&status).unwrap();    assert_eq!(json["line"]["state"], "up_to_date");    assert_eq!(json["line"]["words"], "Up to date with Studio Mac · 6 min ago");    assert_eq!(json["receipts"]["toSend"], 0);}/// GUARD: a database no other device holds never reports this device as a/// peer not reached. A phone that founded its own database is recorded as/// its serving device and has never had an answer from anyone; read as/// "not reached", Home painted "iPhone 17 Pro — not reached yet" in red over/// a database that was perfectly well (rigbystart, 22 Oct 2026). Alone is a/// calm state of its own, said from the sync set, and it wins over every/// other fact: with no peer there is nothing to receive, send or fetch.#[test]fn a_database_on_this_device_alone_never_says_it_is_not_reached() {    let words = SyncWords::shipped();    let now = 1_000 * MIN;    let alone = Facts {        alone: true,        here: Some(DevicePlatform::Ios),        serving: Some("iPhone 17 Pro".into()),        contact: Contact::default(),        receipts: Receipts { to_send: 7, ..Receipts::default() },        ..Facts::default()    };    let line = Line::of(&alone, now);    assert!(!matches!(line, Line::NotReached { .. }), "{line:?}");    assert_eq!((line.key(), words.say(&line, now)), ("alone_ios", "On this iPhone only".into()));    let unknown = Line::of(&Facts { here: None, ..alone }, now);    assert_eq!((unknown.key(), words.say(&unknown, now)), ("alone", "On this device only".into()));    assert_eq!(serde_json::to_value(&unknown).unwrap()["state"], "alone");}/// A phone syncs with Cotton's server, never a Mac: its line names no Mac/// (launch walk of Words, 22 Nov 2026: "Up to date with your Mac" and/// "Sending 4 changes to your Mac" on a phone). A device that really syncs/// with a Mac keeps the Mac's words.#[test]fn a_device_syncing_with_cotton_names_no_mac() {    let words = SyncWords::shipped();    let now = 1_000 * MIN;    let level = Facts {        with_cotton: true,        contact: Contact { answered_at: Some(now - 6 * MIN), silent_since: None },        ..Facts::default()    };    let say = |facts: &Facts| Status::of(facts.clone(), &BTreeMap::new(), |_| None, None, words, now).line.words;    assert_eq!(say(&level), "Up to date · 6 min ago");    let sending = Facts { receipts: Receipts { to_send: 4, ..Receipts::default() }, ..level.clone() };    assert_eq!(say(&sending), "Sending 4 changes");    let one = Facts { receipts: Receipts { to_send: 1, ..Receipts::default() }, ..level.clone() };    assert_eq!(say(&one), "Sending 1 change");    let receiving = Facts { receipts: Receipts { behind: Some(41_000), ..Receipts::default() }, ..level.clone() };    assert_eq!(say(&receiving), "Receiving 41,000 changes");    let never = Facts { contact: Contact::default(), ..level.clone() };    assert_eq!(say(&never), "Cotton not reached yet");    for facts in [&level, &sending, &one, &receiving, &never] {        assert!(!say(facts).contains("Mac"), "{}", say(facts));    }    // Every line that would name a Mac has Cotton's own words.    for key in Line::KEYS.iter().filter(|key| !key.starts_with("alone") && !key.starts_with("fetching")) {        assert!(words.cotton.get(*key).is_some_and(|said| !said.is_empty() && !said.contains("{with}")), "sync.json's cotton says {key}");    }    // A device syncing with a Mac still names it.    let mac = Facts { with_cotton: false, serving: None, receipts: Receipts { to_send: 4, ..Receipts::default() }, ..level };    assert_eq!(say(&mac), "Sending 4 changes to your Mac");}