//! The port of `apps/cotton/__tests__/replica/deviceRefusal.test.ts` and //! `terminalNote.security.test.ts`. use super::*; /// The server every note in this file is about. const SERVER: &str = "https://studio.relay.cottondb.com"; fn note(kind: RefusalKind, code: &str, server: Option<&str>) -> DeviceRefusal { DeviceRefusal { kind, code: code.to_string(), server_url: server.map(str::to_string), server_name: None, at: "2026-09-08T10:00:00.000Z".to_string(), erase: false, refuser: Refuser::Unnamed, serving_epoch: None, device: None, } } /// The same note, said by a named machine. fn said_by(tag: &str, note: DeviceRefusal) -> DeviceRefusal { DeviceRefusal { refuser: Refuser::Server(tag.to_string()), ..note } } #[test] fn the_four_that_stop_the_loop_are_terminal_and_the_clock_is_not() { for terminal in [ "device_not_approved", "device_revoked", "device_bad_signature", "no_device_key", "device_key_unreadable", ] { let kind = RefusalKind::of_code(terminal).expect(terminal); assert!(kind.is_terminal(), "{terminal}"); } assert!(!RefusalKind::of_code("device_clock") .expect("a kind") .is_terminal()); } #[test] fn a_code_this_build_has_no_opinion_about_files_under_nothing() { for unclassified in ["rate_limited", "upgrade_required", "device_replay", "moved"] { assert_eq!(RefusalKind::of_code(unclassified), None, "{unclassified}"); } } #[test] fn both_spellings_of_an_unreadable_key_read_back_as_one_state() { assert_eq!( RefusalKind::of_code("no_device_key"), RefusalKind::of_code("device_key_unreadable") ); } #[test] fn the_same_kind_from_the_same_server_keeps_the_first_note() { let memory = RefusalMemory::default(); let first = note( RefusalKind::Revoked, "device_revoked", Some("https://a.invalid"), ); memory.note(Some(first.clone())); let mut later = first.clone(); later.at = "2026-09-09T10:00:00.000Z".to_string(); memory.note(Some(later)); assert_eq!( memory.standing().expect("a note").at, first.at, "the screen shows when it happened, not when something last asked" ); } #[test] fn the_same_kind_from_another_server_replaces_it_because_join_pre_fills_from_the_note() { let memory = RefusalMemory::default(); memory.note(Some(note( RefusalKind::Revoked, "device_revoked", Some("https://a.invalid"), ))); memory.note(Some(note( RefusalKind::Revoked, "device_revoked", Some("https://b.invalid"), ))); assert_eq!( memory.standing().expect("a note").server_url.as_deref(), Some("https://b.invalid") ); } #[test] fn a_different_kind_replaces_it() { let memory = RefusalMemory::default(); memory.note(Some(note( RefusalKind::NotApproved, "device_not_approved", Some("https://a.invalid"), ))); memory.note(Some(note( RefusalKind::Revoked, "device_revoked", Some("https://a.invalid"), ))); assert_eq!( memory.standing().expect("a note").kind, RefusalKind::Revoked ); } #[test] fn a_terminal_note_is_written_down_so_a_relaunch_paints_join_rather_than_a_spinner() { let dir = tempfile::tempdir().expect("a temp dir"); let path = dir.path().join("client").join("refusal.json"); let memory = RefusalMemory::at(&path); memory.note(Some(note( RefusalKind::Revoked, "device_revoked", Some("https://a.invalid"), ))); assert!(path.exists()); let relaunched = RefusalMemory::at(&path); let standing = relaunched.standing().expect("the note survived"); assert_eq!(standing.kind, RefusalKind::Revoked); assert_eq!(standing.server_url.as_deref(), Some("https://a.invalid")); } #[test] fn a_clock_note_is_never_written_down_because_it_is_a_condition_not_a_decision() { let dir = tempfile::tempdir().expect("a temp dir"); let path = dir.path().join("refusal.json"); let memory = RefusalMemory::at(&path); memory.note(Some(note( RefusalKind::Clock, "device_clock", Some("https://a.invalid"), ))); assert!(!path.exists(), "a relaunch asks again"); assert!(memory.standing().is_some(), "and this launch still knows"); assert!(memory.standing_terminal().is_none()); } #[test] fn clearing_the_note_removes_the_file_and_lifts_every_hold() { let dir = tempfile::tempdir().expect("a temp dir"); let path = dir.path().join("refusal.json"); let memory = RefusalMemory::at(&path); memory.note(Some(note( RefusalKind::Revoked, "device_revoked", Some("https://a.invalid"), ))); memory.note_clock_hold("https://a.invalid", 999); memory.note(None); assert!(!path.exists()); assert!(memory.standing().is_none()); assert_eq!(memory.clock_hold_until("https://a.invalid"), None); } #[test] fn a_clock_offset_and_a_hold_are_both_per_origin() { // A Mac with a bad clock must not stop the phone talking to a relay. let memory = RefusalMemory::default(); memory.note_server_clock("https://a.invalid", 5_000, 1_000); assert_eq!(memory.clock_offset_ms("https://a.invalid"), 4_000); assert_eq!(memory.clock_offset_ms("https://b.invalid"), 0); memory.note_clock_hold("https://a.invalid", 42); assert_eq!(memory.clock_hold_until("https://a.invalid"), Some(42)); assert_eq!(memory.clock_hold_until("https://b.invalid"), None); } #[test] fn a_good_answer_clears_a_clock_note_and_leaves_a_terminal_one_standing() { let memory = RefusalMemory::default(); memory.note(Some(note( RefusalKind::Clock, "device_clock", Some("https://a.invalid"), ))); memory.note_clock_hold("https://a.invalid", 42); memory.note_success("https://a.invalid"); assert!(memory.standing().is_none()); assert_eq!(memory.clock_hold_until("https://a.invalid"), None); memory.note(Some(note( RefusalKind::Revoked, "device_revoked", Some("https://a.invalid"), ))); memory.note_success("https://a.invalid"); assert!( memory.standing_terminal().is_some(), "a 200 never un-revokes a device" ); } #[test] fn a_note_file_from_another_era_is_dropped_rather_than_honoured() { let dir = tempfile::tempdir().expect("a temp dir"); let path = dir.path().join("refusal.json"); std::fs::write(&path, r#"{"mine":{"kind":"clock","code":"device_clock","serverUrl":null,"serverName":null,"at":"2026-01-01T00:00:00.000Z"}}"#) .expect("the file"); assert!(RefusalMemory::at(&path).standing().is_none()); std::fs::write(&path, "{ not json").expect("the file"); assert!(RefusalMemory::at(&path).standing().is_none()); } // ── Which refusals outlive the process ──────────────────────────────────── // // The bug these three were written for: the phone was force-quit, relaunched, // and showed the Join screen while the server still listed it as approved. One // `401 device_bad_signature` — which a server also answers while it is // verifying against an origin it was not reached on — had been written to // `refusal.json`, and every launch after it refused every owner route locally, // before sending, for ever. #[test] fn a_decision_is_written_down_and_an_infrastructure_refusal_is_not() { let dir = tempfile::tempdir().expect("a temp dir"); let path = dir.path().join("client").join("refusal.json"); for decision in [RefusalKind::NotApproved, RefusalKind::Revoked] { let memory = RefusalMemory::at(&path); memory.note(Some(note( decision, "device_revoked", Some("https://a.invalid"), ))); assert!(path.exists(), "{decision:?} is a decision a person reverses"); std::fs::remove_file(&path).expect("the note"); } for infrastructure in [RefusalKind::BadSignature, RefusalKind::KeyUnreadable] { let memory = RefusalMemory::at(&path); memory.note(Some(note( infrastructure, "device_bad_signature", Some("https://a.invalid"), ))); assert!( !path.exists(), "{infrastructure:?} must not outlive the process — a fresh launch asks once more" ); // And it still gates THIS process, so nothing hammers the server. assert!(memory.standing_terminal().is_some()); } } #[test] fn an_infrastructure_note_never_erases_a_decision_already_on_disk() { let dir = tempfile::tempdir().expect("a temp dir"); let path = dir.path().join("refusal.json"); let memory = RefusalMemory::at(&path); memory.note(Some(note( RefusalKind::Revoked, "device_revoked", Some("https://a.invalid"), ))); memory.note(Some(note( RefusalKind::BadSignature, "device_bad_signature", Some("https://b.invalid"), ))); assert!( path.exists(), "the decision stands: a note that is not written down is not an opinion about the file" ); } #[test] fn a_note_an_older_build_persisted_about_a_signature_is_dropped_on_read() { // The repair for a phone that is already stuck: the file exists, this build // does not honour it, and the launch that finds it asks the server again. let dir = tempfile::tempdir().expect("a temp dir"); let path = dir.path().join("refusal.json"); std::fs::write( &path, serde_json::to_string_pretty(&serde_json::json!({ "mine": note( RefusalKind::BadSignature, "device_bad_signature", Some("https://a.invalid"), ) })) .expect("json"), ) .expect("the note"); let memory = RefusalMemory::at(&path); assert!(memory.standing().is_none(), "not honoured on a fresh launch"); } // ── the erase flag ──────────────────────────────────────────────────────── #[test] fn a_revocation_the_owner_marked_as_an_erase_says_so_and_it_survives_a_relaunch() { // The owner ticked the box on another device; the phone learns it on the // first request it makes afterwards, and the screen takes the second verb // without asking rather than offering it. let dir = tempfile::tempdir().expect("a temp dir"); let path = dir.path().join("refusal.json"); let memory = RefusalMemory::at(&path); memory.note(Some(DeviceRefusal { erase: true, ..note(RefusalKind::Revoked, "device_revoked", Some(SERVER)) })); let relaunched = RefusalMemory::at(&path); let held = relaunched.standing().expect("the note outlived the launch"); assert!(held.erase, "a decision is written down, flag and all"); assert!(held.kind.is_a_decision()); } #[test] fn a_revocation_that_did_not_say_is_a_decision_with_two_verbs_and_never_an_erase() { // The default, and the one that matters: absent is `false`, because the // safe reading of a server that said nothing is to ASK. The screen draws // "Keep my copy" and "Erase Cotton from this iPhone" and waits. let memory = RefusalMemory::default(); memory.note(Some(note(RefusalKind::Revoked, "device_revoked", Some(SERVER)))); let held = memory.standing().expect("a note"); assert!(!held.erase); assert!( held.kind.is_a_decision(), "it is still somebody's answer about this device's row" ); } #[test] fn a_note_written_by_a_build_that_had_no_flag_reads_back_as_ask_the_person() { // Forward compatibility in the direction that matters: `#[serde(default)]` // means an older `refusal.json` reads as "no erase", which is the reading // that offers the choice rather than the one that wipes a phone. let dir = tempfile::tempdir().expect("a temp dir"); let path = dir.path().join("refusal.json"); std::fs::write( &path, r#"{"mine":{"kind":"revoked","code":"device_revoked","serverUrl":"https://studio.relay.cottondb.com","serverName":"studio","at":"2026-09-08T10:00:00.000Z","servingEpoch":null}}"#, ) .expect("the file"); let held = RefusalMemory::at(&path).standing().expect("a note"); assert!(!held.erase); } // ── which machine said it ───────────────────────────────────────────────── // // 2026-09-28: a stale Mac held the household's relay name for sixty seconds and // refused a device its copy had not caught up on. The name stays the same when // the machine behind it changes, so a note that remembered only the name spoke // for the household for two hours. #[test] fn a_note_remembers_the_machine_that_said_it_across_a_relaunch() { let dir = tempfile::tempdir().expect("a temp dir"); let path = dir.path().join("refusal.json"); let memory = RefusalMemory::at(&path); memory.note(Some(said_by( "mac-b", note(RefusalKind::NotApproved, "device_not_approved", Some(SERVER)), ))); let relaunched = RefusalMemory::at(&path); assert_eq!( relaunched.standing().expect("a note").refuser, Refuser::Server("mac-b".to_string()) ); } #[test] fn a_note_an_older_build_wrote_recorded_no_machine_and_reads_back_as_unrecorded() { let dir = tempfile::tempdir().expect("a temp dir"); let path = dir.path().join("refusal.json"); std::fs::write( &path, r#"{"mine":{"kind":"not_approved","code":"device_not_approved","serverUrl":"https://studio.relay.cottondb.com","serverName":"studio","at":"2026-09-28T10:00:00.000Z","servingEpoch":null}}"#, ) .expect("the file"); let held = RefusalMemory::at(&path).standing().expect("a note"); assert_eq!(held.refuser, Refuser::Unrecorded); } #[test] fn the_same_kind_from_the_same_name_but_another_machine_replaces_it() { // The dedupe keeps the FIRST note only when it is the same verdict from the // same machine; mac-a's answer after mac-b's is a new note, not a repeat. let memory = RefusalMemory::default(); memory.note(Some(said_by( "mac-b", note(RefusalKind::NotApproved, "device_not_approved", Some(SERVER)), ))); let mut later = said_by( "mac-a", note(RefusalKind::NotApproved, "device_not_approved", Some(SERVER)), ); later.at = "2026-09-28T12:00:00.000Z".to_string(); memory.note(Some(later.clone())); assert_eq!(memory.standing().expect("a note"), later); } #[test] fn only_a_machine_that_names_itself_is_another_machine() { let mac_b = Refuser::Server("mac-b".to_string()); assert!(mac_b.is_other_than(Some("mac-a"))); assert!(!mac_b.is_other_than(Some("mac-b"))); assert!( !mac_b.is_other_than(None), "a name that answers with no tag might be mac-b itself" ); assert!(Refuser::Unnamed.is_other_than(Some("mac-a"))); assert!(!Refuser::Unnamed.is_other_than(None)); } #[test] fn a_blank_header_said_nothing() { assert_eq!(Refuser::of_header(None), Refuser::Unnamed); assert_eq!(Refuser::of_header(Some(" ")), Refuser::Unnamed); assert_eq!( Refuser::of_header(Some("mac-a")), Refuser::Server("mac-a".to_string()) ); } #[test] fn an_older_builds_note_gets_one_retry_and_the_spent_retry_is_written_down() { // One fresh attempt, never a loop — across a relaunch too. let dir = tempfile::tempdir().expect("a temp dir"); let path = dir.path().join("refusal.json"); std::fs::write( &path, r#"{"mine":{"kind":"revoked","code":"device_revoked","serverUrl":"https://studio.relay.cottondb.com","serverName":"studio","at":"2026-09-28T10:00:00.000Z","erase":true,"servingEpoch":null}}"#, ) .expect("the file"); let memory = RefusalMemory::at(&path); memory.spend_stale_retry(); let relaunched = RefusalMemory::at(&path).standing().expect("the note stands"); assert_eq!(relaunched.refuser, Refuser::Unnamed); assert!(relaunched.erase, "spending the retry changes nothing else about it"); assert_eq!(relaunched.kind, RefusalKind::Revoked); } #[test] fn spending_the_retry_leaves_a_note_that_names_its_machine_alone() { let memory = RefusalMemory::default(); let named = said_by( "mac-a", note(RefusalKind::Revoked, "device_revoked", Some(SERVER)), ); memory.note(Some(named.clone())); memory.spend_stale_retry(); assert_eq!(memory.standing().expect("a note"), named); } #[test] fn the_same_answerer_is_not_asked_again_for_a_minute_and_a_new_note_forgets_it() { let memory = RefusalMemory::default(); memory.note(Some(said_by( "mac-b", note(RefusalKind::NotApproved, "device_not_approved", Some(SERVER)), ))); memory.note_same_answerer(SERVER, 1_000); assert!(memory.answerer_is_quiet(SERVER, 1_000 + ANSWERER_QUIET_MS - 1)); assert!(!memory.answerer_is_quiet(SERVER, 1_000 + ANSWERER_QUIET_MS)); assert!( !memory.answerer_is_quiet("https://other.invalid", 1_000), "per origin" ); memory.note(Some(said_by( "mac-a", note(RefusalKind::NotApproved, "device_not_approved", Some(SERVER)), ))); assert!( !memory.answerer_is_quiet(SERVER, 1_001), "what the gate learned was about the old note" ); } /// RULE: A refusal is of a key: a written note about another key — the key a device replaced when it re-keyed — does not stand against the key now reading it, and is dropped from the file, while a note about this key, or one that names no key, still stands. #[test] fn a_note_about_a_replaced_key_does_not_stand_against_its_successor() { let dir = tempfile::tempdir().unwrap(); let path = dir.path().join("refusal.json"); RefusalMemory::at(&path).note(Some(DeviceRefusal { device: Some("old-key".into()), ..note(RefusalKind::Revoked, "device_revoked", Some(SERVER)) })); assert!(RefusalMemory::at_for(&path, "old-key").standing().is_some(), "the refused key's own note fell away"); assert!(RefusalMemory::at_for(&path, "new-key").standing().is_none(), "the replaced key's note stood against its successor"); assert!(RefusalMemory::at(&path).standing().is_none(), "the replaced key's note stayed in the file"); RefusalMemory::at(&path).note(Some(note(RefusalKind::Revoked, "device_revoked", Some(SERVER)))); assert!(RefusalMemory::at_for(&path, "new-key").standing().is_some(), "a note naming no key stopped standing"); }