Cotton code
169 lines · 9.4 KB · RustRaw
use super::*;use chrono::TimeZone;use serde_json::json;fn now() -> DateTime<Utc> {    Utc.with_ymd_and_hms(2026, 11, 20, 9, 0, 0).unwrap()}fn lines() -> LineSet {    LineSet::shipped(crate::rules::unlock::LINES).expect("the unlock lines ship")}fn label(table: &str) -> Option<String> {    match table {        "message" => Some("Messages".into()),        "note" => Some("Notes".into()),        "money_entry" => Some("Money".into()),        _ => None,    }}fn grant(id: &str, at: &str) -> Value {    json!({        "id": id, "account": 1, "member_id": format!("mem-{id}"), "client": "Claude", "granted_at": at, "standing": 1,        "tables": "[\"message\",\"note\"]", "withheld": "[\"code_credential\",\"money_entry\",\"passkey\",\"secret\"]", "ask": "[\"note\"]", "off": "[]",    })}fn sealed() -> Vec<String> {    ["message", "note", "money_entry", "secret", "passkey"].map(String::from).to_vec()}#[test]fn the_screen_lists_connections_and_each_tables_access_without_saved_secrets() {    let grants = vec![grant("g1", "2026-11-17T09:00:00Z"), json!({"id": "w", "account": 0, "granted_at": "2026-11-20T08:00:00Z"}), {        let mut ended = grant("g0", "2026-11-01T09:00:00Z");        ended["ended_at"] = json!("2026-11-02T09:00:00Z");        ended    }];    let s = screen(&grants, &[], &sealed(), now(), label, &lines());    assert_eq!(s.heading, "Your Claude");    assert_eq!(s.connections.len(), 1);    assert_eq!(s.connections[0].said, "Claude · connected 3 days ago");    assert!(s.none.is_none());    assert_eq!(s.remove.as_deref(), Some("Remove"));    assert_eq!(s.connect, None, "connected: Connect Claude is not offered (owner's iPhone, Comms build 19)");    let by: Vec<(&str, &str, bool)> = s.tables.iter().map(|t| (t.table.as_str(), t.access, t.needs_lock)).collect();    // Messages on; Money left off on the card (turning it on is signed); Notes ask each time; no secret, no passkey.    // Turning either of those on widens what Claude reads, so it asks the phone's lock; Messages, on, asks none.    assert_eq!(by, [("message", "on", false), ("money_entry", "off", true), ("note", "ask", true)]);    assert_eq!(s.tables[0].choices.iter().map(|c| c.label.as_str()).collect::<Vec<_>>(), ["On", "Ask each time", "Off"]);}#[test]fn with_no_connection_the_screen_offers_connect_and_lists_nothing() {    let s = screen(&[], &[], &sealed(), now(), label, &lines());    assert!(s.connections.is_empty() && s.tables.is_empty() && s.remove.is_none());    assert_eq!(s.connect.as_deref(), Some("Connect Claude"));    assert!(s.none.is_some());}/// Connect Claude makes the pair and its grant before any Claude connects: until something reads through it, the/// screen says it waits, never "connected just now" (the emulator, 21 Nov 2026).#[test]fn a_pair_connect_claude_made_waits_until_something_reads_through_it() {    let mut made = grant("g2", "2026-11-20T08:59:00Z");    made["request_id"] = json!("connect:mem-g2");    let carded = {        let mut g = grant("g1", "2026-11-17T09:00:00Z");        g["request_id"] = json!("req-card");        g    };    let grants = vec![made, carded];    let s = screen(&grants, &[], &sealed(), now(), label, &lines());    let said: Vec<&str> = s.connections.iter().map(|c| c.said.as_str()).collect();    assert_eq!(said, ["Claude · waiting for Claude to connect", "Claude · connected 3 days ago"]);    let s = screen(&grants, &["g2".to_string()], &sealed(), now(), label, &lines());    assert_eq!(s.connections[0].said, "Claude · connected 1 minute ago");    // Only a pair waiting for Claude: Connect Claude is still offered, so the pairing can be made again.    let waiting = vec![grants[0].clone()];    assert_eq!(screen(&waiting, &[], &sealed(), now(), label, &lines()).connect.as_deref(), Some("Connect Claude"));}#[test]fn the_phone_hands_new_tables_and_new_versions_inside_the_policy_only() {    let g = grant("g1", "2026-11-17T09:00:00Z");    let mut all = sealed();    all.push("photo".into());    let handed = vec![("message".to_string(), 1)];    let current = |t: &str| match t {        "message" => 2,        "photo" => 1,        "money_entry" | "note" | "secret" => 3,        _ => 0,    };    // message moved to v2; photo is new; note is ask, money withheld, secret never, passkey has no keys yet.    assert_eq!(to_hand(&g, &all, &handed, current), [("message".to_string(), 2), ("photo".to_string(), 1)]);    assert!(to_hand(&g, &all, &[("message".into(), 2), ("photo".into(), 1)], current).is_empty());}fn comms_groups() -> Vec<(String, Vec<String>)> {    crate::kit::AppKit::reads_of("messages")}/// Comms' Your Claude is six groups a person knows (Recordings its own since 23 Nov 2026: his assistant reads their/// words to summarise them), never every sealed table (you-grouped.html, 22 Nov 2026: about 140 rows on the/// emulator). Mail is no longer one of them (COMMS WITHOUT MAIL): it is Cotton Post's, and its Your Claude says it; a/// grant on mail is left as it stands. Each group sets its tables together; tables that stand apart make it mixed, no/// choice on.#[test]fn comms_folds_its_tables_into_six_groups_a_person_knows() {    let groups = comms_groups();    assert_eq!(groups.iter().map(|(id, _)| id.as_str()).collect::<Vec<_>>(), ["chats", "photos", "calls", "recordings", "contacts", "files"]);    let sealed: Vec<String> = ["thread", "message", "email", "email_thread", "call", "person", "photo", "secret"].map(String::from).to_vec();    let mut g = grant("g1", "2026-11-17T09:00:00Z");    g["ask"] = json!("[\"message\"]");    g["off"] = json!("[\"call\"]");    g["withheld"] = json!("[\"person\",\"secret\"]");    let shown = super::groups(&[g], &groups, &sealed, &lines(), false);    let by: Vec<(&str, &str, &str, bool)> = shown.iter().map(|s| (s.id.as_str(), s.label.as_str(), s.access, s.needs_lock)).collect();    // Recordings and Files have no sealed table here: left out; so is mail, which Comms no longer reads. Chats stands    // apart (one ask, one on): mixed. Every group not on now asks the phone's lock to be turned on (it widens what    // Claude reads); one on asks none.    assert_eq!(by, [("chats", "Chats", "mixed", true), ("photos", "Photos", "on", false), ("calls", "Calls", "off", true), ("contacts", "Contacts", "off", true)]);    assert_eq!(shown[0].tables, ["thread", "message"]);    assert_eq!(shown[0].line, "Messages and voice notes");    assert!(shown[0].choices.iter().all(|c| !c.on), "a mixed group has no choice on");    assert_eq!(shown[2].choices.iter().map(|c| (c.label.as_str(), c.on)).collect::<Vec<_>>(), [("On", false), ("Ask each time", false), ("Off", true)]);    assert!(super::groups(&[], &groups, &sealed, &lines(), false).is_empty(), "nothing connected: no group to choose, never all On");}fn clock(iso: &str) -> String {    iso.get(11..16).unwrap_or_default().to_string()}/// Standing access is said as standing, with Ask each time instead; the timed one as until when, with Stop now; and/// the row that opens the page says which (the owner's review of you-grouped.html, item 1).#[test]fn your_claude_says_whether_it_reads_any_time_for_a_while_or_asks() {    let g = grant("g1", "2026-11-17T09:00:00Z");    let on = vec![("message".to_string(), "on"), ("email".to_string(), "ask")];    let r = reading(&[g.clone()], &[], &on, now(), clock, &lines());    assert_eq!((r.state, r.row.as_str(), r.title.as_deref()), ("standing", "Can read any time", Some("Claude can read any time")));    assert_eq!(r.act.as_ref().map(|a| (a.id, a.label.as_str())), Some(("ask_all", "Ask each time instead")));    assert_eq!(r.connected.as_deref(), Some("Connected"));    assert_eq!(r.disconnect.as_deref(), Some("Disconnect Claude"));    assert_eq!(to_ask(&on), ["message"]);    let asks = vec![("message".to_string(), "ask"), ("email".to_string(), "off")];    let r = reading(&[g.clone()], &[], &asks, now(), clock, &lines());    assert_eq!((r.state, r.row.as_str(), r.act.is_none()), ("asks", "Asks each time", true));    let off = vec![("message".to_string(), "off")];    assert_eq!(reading(&[g.clone()], &[], &off, now(), clock, &lines()).state, "nothing");    // A window for a while, open now: until when, and Stop now ends it (and any other open beside it).    let window = json!({"id": "w1", "account": 0, "client": "Claude", "granted_at": "2026-11-20T08:30:00Z", "ends_at": "2026-11-20T10:30:00Z", "tables": "[\"email\"]"});    let gone = json!({"id": "w0", "account": 0, "client": "Claude", "granted_at": "2026-11-20T07:00:00Z", "ends_at": "2026-11-20T08:00:00Z"});    let r = reading(&[g.clone(), window, gone], &[], &asks, now(), clock, &lines());    assert_eq!((r.state, r.row.as_str(), r.title.as_deref()), ("timed", "Can read until 10:30", Some("Claude can read until 10:30")));    assert_eq!(r.act.as_ref().map(|a| (a.id, a.label.as_str(), a.grant_ids.clone())), Some(("stop", "Stop now", vec!["w1".to_string()])));    // Not connected: no state to choose, no Disconnect; a pair Connect Claude made waits for Claude.    let r = reading(&[], &[], &[], now(), clock, &lines());    assert_eq!((r.state, r.row.as_str(), r.title.is_none(), r.disconnect.is_none()), ("none", "Not connected", true, true));    let mut made = grant("g2", "2026-11-20T08:59:00Z");    made["request_id"] = json!("connect:mem-g2");    let r = reading(&[made], &[], &on, now(), clock, &lines());    assert_eq!((r.state, r.row.as_str(), r.disconnect.as_deref()), ("waiting", "Waiting for Claude", Some("Disconnect Claude")));}