Cotton code
196 lines · 9.2 KB · RustRaw
//! The device words, pinned to the sentences the app said//! (DeviceRowFacts.swift, DeviceStatus.swift, DeviceTableSource.swift).use super::*;fn at(iso: &str) -> i64 {    DateTime::parse_from_rfc3339(iso).unwrap().timestamp_millis()}fn clock() -> Clock {    Clock { now_ms: at("2026-10-05T12:00:00Z"), zone: chrono_tz::UTC }}fn mac(id: &str, name: &str, rank: Option<i64>) -> DeviceFacts {    DeviceFacts {        device_id: Some(id.into()),        name: Some(name.into()),        platform: Some("mac".into()),        approved_at: Some("2026-09-01T09:00:00Z".into()),        serving_rank: rank,        sampled_at: Some("2026-10-05T11:55:00Z".into()),        host_version: Some("0.26.0".into()),        publishes_relay_identity: true,        ..DeviceFacts::default()    }}/// RULE: A device row's words are said by the core from the file's own columns — state, kind, serving place, load, approver and typed access — so every read of a device says what the app's devices list said.#[test]fn a_phone_says_when_it_last_synced() {    let mut phone = DeviceFacts { device_id: Some("p".into()), platform: Some("ios".into()), ..DeviceFacts::default() };    phone.sampled_at = Some("2026-10-05T03:24:00Z".into());    assert_eq!(state_line(&phone, None, None, None, &clock()), "synced 3:24 am");    phone.sampled_at = Some("2026-10-04T03:24:00Z".into());    assert_eq!(state_line(&phone, None, None, None, &clock()), "synced Yesterday");    phone.sampled_at = Some("2025-10-01T03:24:00Z".into());    assert_eq!(state_line(&phone, None, None, None, &clock()), "synced Wed 1 Oct 2025");}#[test]fn a_removed_device_says_so_and_has_no_place() {    let mut gone = mac("m3", "Old", Some(2));    gone.revoked_at = Some("2026-10-01T00:00:00Z".into());    gone.revoked_reason = Some("erased".into());    let all = vec![gone.clone()];    let order = serving_order(&all);    let words = table_words(&gone, &all, &order, &clock());    assert_eq!(words["status"], "erased");    assert_eq!(words["state"], "removed");    assert!(words.get("serving_place").is_none());    assert!(words.get("load").is_none());    assert_eq!(words["has_access"], false);    assert_eq!(words["can_serve"], false);    assert!(!words.contains_key("summary"));}#[test]fn the_reader_marks_its_own_row() {    let mut data = Map::new();    data.insert("status".into(), Value::from("approved"));    this_device(&mut data, "this phone");    assert_eq!(data["status"], "this device");    assert_eq!(data["state"], "this phone");    let mut removed = Map::new();    removed.insert("status".into(), Value::from("revoked"));    this_device(&mut removed, "this phone");    assert_eq!(removed["status"], "revoked");}#[test]fn ordinals_and_decimals_read_as_the_app_spells_them() {    assert_eq!(        [1, 2, 3, 4, 11, 12, 13, 21, 22, 111].map(ordinal),        ["1st", "2nd", "3rd", "4th", "11th", "12th", "13th", "21st", "22nd", "111th"].map(String::from)    );    assert_eq!(one_decimal(4.0), "4");    assert_eq!(one_decimal(4.25), "4.3");}/// RULE: Serving requires a current known schema and an eligible Mac; a stale copy may still take over during an outage, but an unknown or older schema cannot safely serve.#[test]fn serving_eligibility_requires_a_current_schema_but_allows_a_stale_copy() {    let mut serving = mac("m1", "mac-c", Some(0));    serving.schema_version = Some(132);    serving.copy_as_of = Some("2026-10-05T12:00:00.000Z".into());    let word = |device: &DeviceFacts, key: &str| {        serve_from(device, Some(&serving)).and_then(|words| words.get(key).and_then(Value::as_str).map(str::to_string))    };    let offered = |device: &DeviceFacts| serve_from(device, Some(&serving)).map(|words| words["offered"] == Value::Bool(true));    // Level with the serving Mac: offered, and the sheet has nothing to say of it.    let mut level = mac("m2", "mac-a", Some(1));    level.schema_version = Some(132);    level.repositories = Some(crate::device::reported_state::RepositoryReadiness::default());    level.copy_as_of = Some("2026-10-05T11:54:00.000Z".into());    assert_eq!(offered(&level), Some(true));    assert_eq!(word(&level, "behind"), None);    // Behind: still offered, and said in the brief's words.    let mut behind = level.clone();    behind.copy_as_of = Some("2026-10-05T11:20:00.000Z".into());    assert_eq!(offered(&behind), Some(true));    let minutes = word(&behind, "behind").expect("a measured delay is reported");    behind.copy_as_of = Some("2026-10-03T09:00:00.000Z".into());    assert_ne!(word(&behind, "behind").as_deref(), Some(minutes.as_str()));    behind.copy_as_of = None;    assert!(word(&behind, "behind").is_some());    // Schema-behind, or silent about its schema: not offered, and told why.    let mut older = level.clone();    older.schema_version = Some(131);    assert_eq!(offered(&older), Some(false));    assert!(word(&older, "whyNot").is_some());    older.schema_version = None;    assert_eq!(offered(&older), Some(false));    // A Mac the relay cannot be told to reach: not offered.    let mut unreachable = level.clone();    unreachable.publishes_relay_identity = false;    assert_eq!(offered(&unreachable), Some(false));    // Never about these.    assert_eq!(serve_from(&serving, Some(&serving)), None, "the Mac that serves");    let mut removed = level.clone();    removed.revoked_at = Some("2026-10-01T00:00:00Z".into());    assert_eq!(serve_from(&removed, Some(&serving)), None);    let phone = DeviceFacts { device_id: Some("p1".into()), platform: Some("ios".into()), ..DeviceFacts::default() };    assert_eq!(serve_from(&phone, Some(&serving)), None);}/// RULE: Serve from this Mac is offered only after that Mac reports all repository ownership and refs checked; an unresolved ref supplies the refusal and a passing report clears it.#[test]fn serving_requires_the_chosen_macs_repository_reading() {    use crate::device::reported_state::{RepositoryReadiness, RepositoryProblem};    let mut chosen = mac("chosen", "Studio", Some(1));    chosen.schema_version = Some(137);    assert_eq!(serve_from(&chosen, None).unwrap()["offered"], false);    chosen.repositories = Some(RepositoryReadiness { problems: vec![RepositoryProblem {        database: "project".into(), repository: "same-id".into(), reason: "Cannot resolve refs/tags/v1".into(),    }] });    let refused = serve_from(&chosen, None).unwrap();    assert_eq!(refused["offered"], false);    assert!(refused["whyNot"].as_str().unwrap().contains("refs/tags/v1"));    chosen.repositories = Some(RepositoryReadiness::default());    let ready = serve_from(&chosen, None).unwrap();    assert_eq!(ready["offered"], true);    assert!(ready["whyNot"].is_null());}/// RULE: A load figure can only name sampled disk space, because an unread/// disk is unknown and must never appear as a measured zero.#[test]fn an_unsampled_disk_has_no_load_figure() {    assert!(load_line(&DeviceFacts::default()).is_none());}/// RULE: Session load reads both measurements together; unknown capacity is never displayed as zero, and disk space does not repeat session activity.#[test]fn sessions_use_one_complete_reading_and_load_only_reports_space() {    assert_eq!(sessions_line(Some(0.0), Some(3.0)).as_deref(), Some("0 of 3"));    assert_eq!(sessions_line(Some(2.0), Some(3.0)).as_deref(), Some("2 of 3"));    assert!(sessions_line(Some(0.0), None).is_none());    assert!(sessions_line(None, Some(3.0)).is_none());    assert!(sessions_line(Some(-1.0), Some(3.0)).is_none());    assert!(sessions_line(Some(0.5), Some(3.0)).is_none());    let device = DeviceFacts { disk_free_gb: Some(88.0), sessions_running: Some(2.0), ..DeviceFacts::default() };    assert_eq!(load_line(&device).as_deref(), Some("88 GB free"));}/// RULE: Device access and serving eligibility are independent typed facts; removal clears both even when a Mac retains its relay identity.#[test]fn device_access_and_serving_are_typed_facts() {    let mut device = mac("m1", "Example Mac", Some(0));    for (removed, identity, access, serving) in [        (false, true, true, true), (false, false, true, false),        (true, true, false, false), (true, false, false, false),    ] {        device.revoked_at = removed.then(|| "2026-10-01T00:00:00Z".into());        device.publishes_relay_identity = identity;        let all = vec![device.clone()];        let data = table_words(&device, &all, &serving_order(&all), &clock());        assert_eq!(data["has_access"], access);        assert_eq!(data["can_serve"], serving);        assert!(!data.contains_key("summary"));    }}/// RULE: This device's notifications tile reads the system's answer: declined by the system it is the declined card, never 'N of 5 on' from the five switches; allowed or not asked yet it is the switches' card (walker292: '5 of 5 on' on a phone that declined notifications at first launch).#[test]fn the_notifications_tile_reads_the_systems_answer() {    assert_eq!(notify_system(Some("denied")), "declined");    assert_eq!(notify_system(Some("authorized")), "on");    assert_eq!(notify_system(None), "on");    assert_eq!(notifications_permission("not_determined"), "undetermined");    assert_eq!(notifications_permission("provisional"), "granted");    assert_eq!(notifications_permission("denied"), "denied");}