//! The one reader of `cotton_serving_machine`: the latest epoch, and the keys //! the epochs before it recorded. use rusqlite::types::Value as SqlValue; use super::*; use crate::test_support::{seeded_file, WORKSPACE}; /// One fact, as a hand-over would have written it. fn fact(db: &Driver, id: &str, epoch: i64, device: &str, access_key: Option<&str>) { db.run( "INSERT INTO cotton_serving_machine \ (id, workspace_id, epoch, device_credential_id, author_id, backup_access_key_id, since) \ VALUES (?, ?, ?, ?, 'author-1', ?, '2026-10-03T00:00:00.000Z')", &[ SqlValue::Text(id.to_string()), SqlValue::Text(WORKSPACE.to_string()), SqlValue::Integer(epoch), SqlValue::Text(device.to_string()), access_key.map_or(SqlValue::Null, |key| SqlValue::Text(key.to_string())), ], ) .expect("the serving fact"); } #[test] fn a_file_with_no_fact_answers_none_and_retires_no_key() { let db = seeded_file("device:mac"); assert_eq!(serving_fact(&db, WORKSPACE).expect("reads"), None); assert!(retired_backup_access_keys(&db, WORKSPACE) .expect("reads") .is_empty()); } #[test] fn the_highest_epoch_is_the_machine_that_serves_whatever_order_the_rows_arrived_in() { let db = seeded_file("device:mac"); fact(&db, "s-2", 2, "device:mini", Some("KEY-2")); fact(&db, "s-1", 1, "device:mac", Some("KEY-1")); fact(&db, "s-3", 3, "device:studio", None); let latest = serving_fact(&db, WORKSPACE) .expect("reads") .expect("a fact"); assert_eq!(latest.id, "s-3"); assert_eq!(latest.epoch, 3); assert_eq!(latest.device_credential_id, "device:studio"); assert_eq!(latest.author_id, "author-1"); assert_eq!(latest.backup_access_key_id, None); assert_eq!(latest.relay_key, None); assert_eq!(latest.confirmed_by, None); } #[test] fn a_tie_on_the_epoch_breaks_the_same_way_on_every_copy() { let db = seeded_file("device:mac"); fact(&db, "s-b", 4, "device:mini", None); fact(&db, "s-a", 4, "device:mac", None); let latest = serving_fact(&db, WORKSPACE) .expect("reads") .expect("a fact"); assert_eq!(latest.id, "s-b"); } #[test] fn a_fact_of_another_workspace_is_not_this_ones() { let db = seeded_file("device:mac"); db.run( "INSERT INTO cotton_workspaces (id, name, owner_user_id, created_at) \ VALUES ('22222222-2222-4222-8222-222222222222', 'Other', 'owner-2', '2026-10-03T00:00:00.000Z')", &[], ) .expect("a second workspace"); db.run( "INSERT INTO cotton_serving_machine \ (id, workspace_id, epoch, device_credential_id, author_id, since) \ VALUES ('s-x', '22222222-2222-4222-8222-222222222222', 9, 'device:other', 'a', \ '2026-10-03T00:00:00.000Z')", &[], ) .expect("the other workspace's fact"); assert_eq!(serving_fact(&db, WORKSPACE).expect("reads"), None); } #[test] fn the_retired_keys_are_every_older_epochs_but_never_the_latest_facts_own_row() { let db = seeded_file("device:mac"); fact(&db, "s-1", 1, "device:mac", Some("KEY-B")); fact(&db, "s-2", 2, "device:mini", Some("KEY-A")); fact(&db, "s-3", 3, "device:mac", None); fact(&db, "s-4", 4, "device:studio", Some("KEY-C")); // An older epoch that recorded no key retires nothing, and a key two // older epochs both recorded is named once. fact(&db, "s-0", 0, "device:mac", Some("KEY-B")); assert_eq!( retired_backup_access_keys(&db, WORKSPACE).expect("reads"), vec!["KEY-A".to_string(), "KEY-B".to_string()] ); } #[test] fn a_key_an_older_fact_recorded_is_retired_even_when_the_latest_fact_records_it_again() { // A machine handed back the database it once served does not get its old // key back: whoever served in between held it. let db = seeded_file("device:mac"); fact(&db, "s-1", 1, "device:mac", Some("KEY-SAME")); fact(&db, "s-2", 2, "device:mini", Some("KEY-MINI")); fact(&db, "s-3", 3, "device:mac", Some("KEY-SAME")); assert_eq!( retired_backup_access_keys(&db, WORKSPACE).expect("reads"), vec!["KEY-MINI".to_string(), "KEY-SAME".to_string()] ); } // RULE: a backup key is retired only by a newer serving fact that records a key of its own. A planned switch between two Macs of one household records none — nobody was revoked and the household's one pair is sealed in its vault for whichever Mac serves — so it retires nothing, and the Mac it names backs up with no typing. A take-over records a new key and retires every key below it, the one handed back included: that is the fence between two writers and one prefix. #[test] fn a_planned_switch_retires_no_key_and_a_take_over_retires_every_key_below_it() { // A planned switch alone: the household's key stands. let db = seeded_file("device:mac"); fact(&db, "s-1", 1, "device:mac", Some("KEY-HOUSEHOLD")); fact(&db, "s-2", 2, "device:mini", None); fact(&db, "s-3", 3, "device:mac", None); assert!(retired_backup_access_keys(&db, WORKSPACE).expect("reads").is_empty()); // A planned switch after a take-over keeps that take-over's key valid, // and the key the take-over retired stays retired. fact(&db, "s-4", 4, "device:studio", Some("KEY-TAKEN-OVER")); fact(&db, "s-5", 5, "device:mini", None); assert_eq!( retired_backup_access_keys(&db, WORKSPACE).expect("reads"), vec!["KEY-HOUSEHOLD".to_string()] ); // The first key handed back by a later take-over is still retired, and so // is the one in between — planned switches around them change nothing. fact(&db, "s-6", 6, "device:mac", Some("KEY-HOUSEHOLD")); fact(&db, "s-7", 7, "device:mini", None); assert_eq!( retired_backup_access_keys(&db, WORKSPACE).expect("reads"), vec!["KEY-HOUSEHOLD".to_string(), "KEY-TAKEN-OVER".to_string()] ); } // RULE: a Mac the latest serving fact does not name is a client Mac. A file with no fact, or whose fact names this machine, is served as before; a fact naming another machine — or any fact on a machine with no device id — means this one does not serve. #[test] fn a_fact_naming_another_machine_is_served_elsewhere() { let db = seeded_file("device:mac"); assert_eq!(served_elsewhere(&db, WORKSPACE, Some("device:mac")).expect("reads"), None); fact(&db, "s-1", 1, "device:mac", None); assert_eq!(served_elsewhere(&db, WORKSPACE, Some("device:mac")).expect("reads"), None); fact(&db, "s-2", 2, "device:other", None); let elsewhere = served_elsewhere(&db, WORKSPACE, Some("device:mac")).expect("reads"); assert_eq!(elsewhere.map(|fact| fact.device_credential_id).as_deref(), Some("device:other")); assert!(served_elsewhere(&db, WORKSPACE, None).expect("reads").is_some()); }