Cotton code
219 lines · 9.4 KB · RustRaw
//! Which device can host, whether its report is current, and when a usage//! figure stops being one — as arithmetic over explicit instants.//!//! Every device and stamp below is invented//! ([nothing-real-ever-goes-in-cotton-s-public-tree]).use chrono::{DateTime, Duration, SecondsFormat, Utc};use super::*;const NOW: &str = "2026-09-18T09:00:00Z";fn now() -> DateTime<Utc> {    DateTime::parse_from_rfc3339(NOW)        .expect("a literal instant")        .with_timezone(&Utc)}fn ago(minutes: i64) -> String {    (now() - Duration::minutes(minutes)).to_rfc3339_opts(SecondsFormat::Millis, true)}// ── the window ──────────────────────────────────────────────────────────────/// RULE: A machine that has not reported in ten minutes is silent — one five-minute report interval and five minutes' grace.////// Written as the sum so the window can never again be shorter than the gap it/// waits for: until 27 Sep 2026 it was ten minutes against a fifteen-minute/// report, and every healthy Mac read as gone for a third of the day.#[test]fn the_seen_window_is_the_report_interval_and_its_grace() {    assert_eq!(REPORT_INTERVAL_MINUTES, 5);    assert_eq!(REPORT_INTERVAL_MINUTES + REPORT_GRACE_MINUTES, 10);    assert_eq!(SEEN_WINDOW_MINUTES, 10);    const { assert!(SEEN_WINDOW_MINUTES > REPORT_INTERVAL_MINUTES) };}/// RULE: A machine whose last report is more than ten minutes old is silent, and says since when.////// The phone draws "has not reported since …" from `since`, so a silent/// machine never shows its last numbers as if they were live.#[test]fn a_report_older_than_the_window_is_silent_since_that_report() {    let stamp = ago(11);    assert_eq!(        report_state(Some(stamp.as_str()), now()),        ReportState::Silent { since: Some(stamp) }    );    assert_eq!(        report_state_at(Some(now() - Duration::minutes(11)), now()),        ReportState::Silent {            since: Some(ago(11))        }    );}/// RULE: A report exactly ten minutes old is still current — a machine reporting on its own tick never drops out on a rounding.#[test]fn a_report_exactly_on_the_window_is_current() {    assert_eq!(report_state(Some(ago(10).as_str()), now()), ReportState::Current);    assert_eq!(        report_state_at(Some(now() - Duration::minutes(10)), now()),        ReportState::Current    );    assert_eq!(report_state(Some(ago(REPORT_INTERVAL_MINUTES).as_str()), now()), ReportState::Current);}// ── a fast clock ────────────────────────────────────────────────────────────/// RULE: A report stamped more than two minutes ahead of the clock that receives it is kept under its receipt time, so a fast Mac never reads as live after it goes quiet.////// A Mac one hour fast wrote a stamp an hour ahead, every reader compared it/// with its own `now`, and the Mac read as reporting for seventy minutes after/// it went to sleep — its last numbers drawn as live all that time.#[test]fn a_stamp_more_than_two_minutes_ahead_is_kept_under_its_receipt_time() {    assert_eq!(REPORT_LEAD_MINUTES, 2);    let hour_fast = (now() + Duration::hours(1)).to_rfc3339_opts(SecondsFormat::Millis, true);    let received = received_stamp(&hour_fast, now()).expect("an hour ahead is stamped on receipt");    assert_eq!(received, ago(0));    // And so, seventy minutes after its last report, the Mac is silent.    assert_eq!(        report_state(Some(received.as_str()), now() + Duration::minutes(70)),        ReportState::Silent { since: Some(ago(0)) }    );    let just_over = (now() + Duration::minutes(2) + Duration::seconds(1))        .to_rfc3339_opts(SecondsFormat::Millis, true);    assert_eq!(received_stamp(&just_over, now()), Some(ago(0)));}#[test]fn a_stamp_within_the_lead_or_in_the_past_is_kept_as_written() {    let two_ahead = (now() + Duration::minutes(2)).to_rfc3339_opts(SecondsFormat::Millis, true);    assert_eq!(received_stamp(&two_ahead, now()), None, "a skew of seconds rewrites nothing");    // A reading pushed late by a Mac that was offline is exactly as old as it    // says; stamping it on receipt would draw an hour-old number as fresh.    assert_eq!(received_stamp(&ago(60), now()), None);    assert_eq!(received_stamp("yesterday", now()), None);}#[test]fn a_report_from_the_future_is_current_because_clocks_disagree_by_seconds() {    let ahead = (now() + Duration::minutes(2)).to_rfc3339_opts(SecondsFormat::Secs, true);    assert_eq!(report_state(Some(ahead.as_str()), now()), ReportState::Current);}#[test]fn no_report_or_an_unreadable_one_is_silent_with_no_time() {    for stamp in [None, Some(""), Some("   "), Some("yesterday")] {        assert_eq!(            report_state(stamp, now()),            ReportState::Silent { since: None },            "{stamp:?}"        );    }    assert_eq!(report_state_at(None, now()), ReportState::Silent { since: None });}// ── who can host ────────────────────────────────────────────────────────────/// RULE: A phone that has never reported cannot host a session, so it is never offered work and never named as a machine that is late.#[test]fn a_phone_that_never_reported_cannot_host() {    assert!(!can_host(Some("ios"), None, None, None));    assert!(!can_host(Some("android"), None, None, None));    assert!(!can_host(None, None, None, None), "a device that says nothing about itself");}#[test]fn a_mac_or_linux_machine_can_host_before_its_first_report() {    assert!(can_host(Some("mac"), None, None, None));    assert!(can_host(Some("linux"), None, None, None));}#[test]fn any_device_that_has_reported_can_host() {    let stamp = ago(1);    assert!(can_host(None, Some(stamp.as_str()), None, None));    assert!(can_host(Some("ios"), None, Some("1.4.0"), None));}#[test]fn a_revoked_device_cannot_host_whatever_it_reported() {    let stamp = ago(1);    assert!(!can_host(Some("mac"), Some(stamp.as_str()), Some("1.4.0"), Some("2026-09-01T00:00:00Z")));}#[test]fn an_empty_stamp_is_not_a_report() {    assert!(!can_host(Some("ios"), Some(""), Some("  "), None));}// ── usage windows ───────────────────────────────────────────────────────────/// RULE: A usage window whose reset time has passed shows no percentage, because the figure was for a window that is over.#[test]fn a_window_that_has_reset_shows_no_figure() {    assert_eq!(window_pct(Some(84.0), Some(ago(1).as_str()), now()), None);    assert_eq!(window_pct(Some(84.0), Some(ago(0).as_str()), now()), None, "a reset at now has happened");}#[test]fn a_window_still_open_or_with_no_reset_keeps_its_figure() {    let later = (now() + Duration::hours(2)).to_rfc3339_opts(SecondsFormat::Secs, true);    assert_eq!(window_pct(Some(21.0), Some(later.as_str()), now()), Some(21.0));    assert_eq!(window_pct(Some(21.0), None, now()), Some(21.0));    assert_eq!(window_pct(None, Some(later.as_str()), now()), None);}// ── one row per machine ─────────────────────────────────────────────────────/// RULE: A re-joined Mac is one machine: its old key's silent row is not listed once a row with the same machine name has reported more recently.////// A Mac that joined again under a new key left the old key's row unrevoked,/// and the Code page drew the same Mac twice — once live, once "has not/// reported since …".#[test]fn a_re_joined_mac_s_old_silent_row_is_not_listed() {    let old_key = ago(60 * 24);    let new_key = ago(1);    assert_eq!(        listed_machines(            &[("Mac (mac-b)", Some(old_key.as_str())), ("mac-b", Some(new_key.as_str()))],            now()        ),        [false, true]    );    // A ghost that never reported is older than any report.    assert_eq!(listed_machines(&[("mac-b", None), ("MAC-B ", Some(ago(30).as_str()))], now()), [false, true]);}#[test]fn two_live_macs_that_share_a_name_are_both_listed() {    assert_eq!(        listed_machines(&[("mac-b", Some(ago(1).as_str())), ("mac-b", Some(ago(2).as_str()))], now()),        [true, true]    );}/// RULE: A machine silent for more than the history's thirty days is not listed — a retired Mac never says "has not reported since …" for ever.#[test]fn a_machine_silent_past_the_retirement_bound_is_not_listed() {    assert_eq!(RETIRED_AFTER_DAYS, crate::rules::samples::RETENTION_DAYS);    let retired = ago(60 * 24 * RETIRED_AFTER_DAYS + 1);    let recent = ago(60 * 24 * RETIRED_AFTER_DAYS);    assert_eq!(        listed_machines(&[("attic", Some(retired.as_str())), ("study", Some(recent.as_str()))], now()),        [false, true]    );    // A Mac that has just joined says so until its first report.    assert_eq!(listed_machines(&[("new", None)], now()), [true]);}#[test]fn machine_name_unwraps_the_one_retired_spelling_and_nothing_else() {    assert_eq!(machine_name("Mac (mac-b)"), "mac-b");    assert_eq!(machine_name(" MAC-B "), "mac-b");    assert_eq!(machine_name("Mac mini (kitchen)"), "mac mini (kitchen)");}