//! `seal.*` over a real attached copy, on a fixed clock. Synthetic keys only.use base64::engine::general_purpose::URL_SAFE_NO_PAD;use base64::Engineas _;use std::sync::Arc;use serde_json::{json, Value};use cotton_core::vault::Vmk;usesuper::{call_on, MINTED_ALREADY, NOT_MINTED};usecrate::shell::client::phone_tests::attached_on_a_clock;fn call(client: &crate::shell::client::ClientController, method: &str, request: Value) -> Result<Value, crate::methods::MethodError> { call_on(client, method, &request)}/// RULE: the shell hands the key over, the core holds it for the database/// the call is on, and a key is forgotten on request; the DK opens from the/// object's row only with the vault unlocked, and each refusal is by name.#[test]fn the_data_key_is_handed_over_opened_with_the_vault_and_refused_by_name() {let (_dir, client, _clock) = attached_on_a_clock();// A signed-in copy that has synced: before its first pass it says nothing (vault_tests, a_copy_before_its_first_pass_says_no_locked_line). client.note_pull_for_test("2026-11-11T09:00:00.000Z");let database = client.current_database().expect("a database");let status = call(&client, "seal.status", json!({})).expect("a status"); assert_eq!((status["keyHeld"].clone(), status["minted"].clone(), status["parked"].clone()), (json!(false), json!(false), json!([])));// The whole object keeps the kit's sealed tables (messages, mail), so a phone with no key is asked for it, in the core's words. assert_eq!(status["needsKey"], true); assert!(status["said"]["line"].as_str().is_some_and(|l| l.contains("locked")) && status["said"]["act"] == "Unlock", "{status}"); assert_eq!(call(&client, "seal.heldKey", json!({})).expect_err("none held").code, cotton_client::seal::SEAL_KEY_MISSING); assert_eq!(call(&client, "seal.openKey", json!({})).expect_err("no row").code, NOT_MINTED); assert_eq!(call(&client, "seal.useKey", json!({"key": "short"})).expect_err("not a key").code, "bad_request");// The object's row of the DK, as a pull keeps it beside the copy.let vmk = Vmk::from_bytes([3; 32]);let (dk, row) = cotton_client::seal::mint_data_key(&vmk, 1, "2026-11-11T10:00:00.000Z").expect("a minted key");let file = rusqlite::Connection::open(client.layout().replica(&database)).expect("the copy"); cotton_client::seal::write_notes(&file, &[cotton_client::seal::Note::Key { id: "dk".into(), data: row }]).expect("kept"); drop(file); assert_eq!(call(&client, "seal.openKey", json!({})).expect_err("locked").code, "vault_locked"); assert_eq!(call(&client, "seal.mintKey", json!({})).expect_err("minted").code, MINTED_ALREADY); client.vault().expect("a door").unlock(Vmk::from_bytes([3; 32]), 1, client.now());let opened = call(&client, "seal.openKey", json!({})).expect("it opens"); assert_eq!(opened["key"], json!(URL_SAFE_NO_PAD.encode(dk.expose())), "the shell stores what it is handed");let held = call(&client, "seal.status", json!({})).expect("a status"); assert_eq!((held["keyHeld"].clone(), held["needsKey"].clone(), held["said"].clone()), (json!(true), json!(false), Value::Null));// The shell keeps what the core holds, for the next launch's seal.useKey. assert_eq!(call(&client, "seal.heldKey", json!({})).expect("held")["key"], json!(URL_SAFE_NO_PAD.encode(dk.expose()))); assert_eq!(call(&client, "seal.forgetKey", json!({})).expect("forgotten"), json!({"forgotten": true}));let forgot = call(&client, "seal.status", json!({})).expect("a status"); assert_eq!((forgot["keyHeld"].clone(), forgot["minted"].clone(), forgot["needsKey"].clone()), (json!(false), json!(true), json!(true))); call(&client, "seal.useKey", json!({"key": URL_SAFE_NO_PAD.encode(dk.expose())})).expect("held"); assert_eq!(call(&client, "seal.status", json!({})).expect("a status")["keyHeld"], true);}/// RULE (L7): `seal.search` searches only a sealed table, only with the key/// held, and only once the database is on its object; each refusal is by/// name and nothing is asked in the clear. (An open kit table answers/// `not_sealed`, a sealed one with no key held `seal_key_missing`; the search/// itself is `cotton_client::seal::search`'s, tested there against a fake/// object.)#[test]fn a_search_beyond_the_copy_is_refused_by_name() {let (_dir, client, _clock) = attached_on_a_clock(); assert_eq!(call(&client, "seal.search", json!({"query": "x"})).expect_err("no table").code, "bad_request"); assert_eq!(call(&client, "seal.search", json!({"table": "todo"})).expect_err("no query").code, "bad_request"); assert_eq!(call(&client, "seal.search", json!({"table": "no_such", "query": "x"})).expect_err("unknown").code, "not_sealed");// Projects stay open (seal/default.json pending); todos are sealed now (e2e rulings R3). assert_eq!(call(&client, "seal.search", json!({"table": "project", "query": "milk"})).expect_err("open table").code, "not_sealed");let sealed = call(&client, "seal.search", json!({"table": "message", "query": "milk"})).expect_err("no key"); assert!(sealed.code.starts_with("seal_") || sealed.code == "sign_in_first", "{sealed:?}");}/// RULE (R6): an app that keeps a person's files (Photos, Books, Music, Cinemas) needs the data key as/// an app keeping a sealed table does: its files go up sealed under it and open only with it, so a first sign-in/// founds it and a second phone asks for it, whether or not it keeps a sealed table.#[test]fn an_app_keeping_a_persons_files_needs_the_data_key() {for (app, needs) in [("photos", true), ("books", true), ("music", true), ("cinemas", true)] {let (_dir, client, _clock) = attached_on_a_clock();let kit = cotton_core::kit::AppKit::named(app).unwrap_or_else(|| panic!("the {app} kit")); cotton_core::kit::AppKit::keep(Some(&kit), client.layout().root()).expect("the kit kept");let status = call(&client, "seal.status", json!({})).expect("a status"); assert_eq!(status["needsKey"], needs, "{app}: {status}"); }}/// RULE (R1 and R5): an app whose kit keeps a table the seal rule seals needs the data key, though/// the table is the kit's own and not in the personal storage map (Journal's journal_entry), where the app keeps the/// vault's rows to found the key or be approved for it: its rows go up sealed/// and what it sends is sealed with the sharing key the data key derives. Walk send-sealed: a fresh Journal person/// never founded the key, so its first page waited unsent and Send refused `seal_key_missing`.#[test]fn an_app_keeping_a_kit_table_the_rule_seals_needs_the_data_key() {for app in ["journal"] {let (_dir, client, _clock) = attached_on_a_clock();let kit = cotton_core::kit::AppKit::named(app).unwrap_or_else(|| panic!("the {app} kit")); cotton_core::kit::AppKit::keep(Some(&kit), client.layout().root()).expect("the kit kept");let status = call(&client, "seal.status", json!({})).expect("a status"); assert_eq!(status["needsKey"], true, "{app}: {status}"); }}/// RULE (no Unlock before sign-in, 20 Nov 2026): a phone that has not signed in, on the database it started itself,/// is never asked for a key: the core says `makes` and no line, and `seal.makeKey` makes one with no question, held/// at once and answered for the shell to keep as a first sign-in's key is kept. Asked again, it answers the key it/// holds and never makes a second; a key handed back at the next launch (`seal.useKey`) is the one held.#[test]fn a_phone_that_has_not_signed_in_makes_its_key_with_no_question() {let (_dir, client, _clock) = crate::shell::client::phone_tests::attached_on_a_clock_in("local"); assert!(!client.signed_in());let status = call(&client, "seal.status", json!({})).expect("a status"); assert_eq!((status["needsKey"].clone(), status["makes"].clone(), status["founds"].clone()), (json!(true), json!(true), json!(false)), "{status}"); assert_eq!(status["said"], Value::Null, "no banner on a phone that has not signed in: {status}");let made = call(&client, "seal.makeKey", json!({})).expect("made"); assert_eq!(made["made"], true);let key = made["key"].as_str().expect("a key").to_string(); assert_eq!(URL_SAFE_NO_PAD.decode(&key).expect("base64url").len(), 32);let held = call(&client, "seal.status", json!({})).expect("a status"); assert_eq!((held["keyHeld"].clone(), held["needsKey"].clone(), held["makes"].clone(), held["said"].clone()), (json!(true), json!(false), json!(false), Value::Null)); assert_eq!(call(&client, "seal.heldKey", json!({})).expect("held")["key"], json!(key), "the shell keeps what was made");let again = call(&client, "seal.makeKey", json!({})).expect("answered again"); assert_eq!((again["made"].clone(), again["key"].clone()), (json!(false), json!(key)), "never a second key");// The next launch: the shell hands back the key it kept, and nothing new is made. call(&client, "seal.forgetKey", json!({})).expect("a new process holds nothing"); call(&client, "seal.useKey", json!({"key": key})).expect("handed back"); assert_eq!(call(&client, "seal.makeKey", json!({})).expect("held")["key"], json!(key));}/// RULE: a key is made here only on a phone's own signed-out database. A signed-in copy is asked (its key is the/// vault's: approve from another device, a code), and so is a copy that ever learned a key from an object (a former/// copy kept after a sign-out or a 401): `seal.makeKey` refuses by name and holds nothing, so number and code alone/// never open a sealed database, and an object's copy never takes a rival key.#[test]fn a_key_is_never_made_for_a_database_an_object_keys() {let (_dir, client, _clock) = attached_on_a_clock(); client.note_pull_for_test("2026-11-11T09:00:00.000Z");let status = call(&client, "seal.status", json!({})).expect("a status"); assert_eq!((status["makes"].clone(), status["said"]["act"].clone()), (json!(false), json!("Unlock")), "{status}"); assert_eq!(call(&client, "seal.makeKey", json!({})).expect_err("signed in").code, super::NOT_MADE_HERE);let (_dir, local, _clock) = crate::shell::client::phone_tests::attached_on_a_clock_in("local");let database = local.current_database().expect("a database");let (_dk, row) = cotton_client::seal::mint_data_key(&Vmk::from_bytes([3; 32]), 1, "2026-11-11T10:00:00.000Z").expect("a minted key");let file = rusqlite::Connection::open(local.layout().replica(&database)).expect("the copy"); cotton_client::seal::write_notes(&file, &[cotton_client::seal::Note::Key { id: "dk".into(), data: row }]).expect("kept"); drop(file);let status = call(&local, "seal.status", json!({})).expect("a status"); assert_eq!((status["makes"].clone(), status["needsKey"].clone()), (json!(false), json!(true)), "{status}"); assert!(status["said"]["line"].is_string(), "the one real case still asks: {status}"); assert_eq!(call(&local, "seal.makeKey", json!({})).expect_err("an object's key").code, super::NOT_MADE_HERE);use cotton_client::seal::DataKeyStoreas _; assert!(local.data_keys().data_key(&database).expect("read").is_none());}/// RULE (back up as a new account): the signed-out phone's key, carried into the object it is adopted into, becomes/// that object's key when the object has none: wrapped under the vault key and kept on the object, so every other/// device and key store gets it as they get a first sign-in's, and what was made with it keeps its ids.#[test]fn a_new_account_takes_the_signed_out_key_as_its_own() {use cotton_client::seal::DataKeyStoreas _;let (_dir, client, _clock) = attached_on_a_clock();let database = client.current_database().expect("a database");let silent = cotton_core::seal::DataKey::from_bytes([42; 32]); client.data_keys().hold("local-phone-db", &silent); client.set_carried_key(Some((database.clone(), "local-phone-db".into())));let sealer = crate::phone::personal_object::sealer_of(&client, &database); sealer.learned_all();let (door, exchange) = crate::phone::unlock::tests::door([cotton_client::transport::scripted::Reply::ok("{}")]); client.set_object_door(Some(Arc::new(door)));let vmk = crate::phone::unlock::tests::fixture_vmk();let dk = super::ensure(&client, &vmk, 1).expect("the key taken"); assert_eq!(dk.expose(), silent.expose(), "the silent key is the account's key");let kept: Value = serde_json::from_slice(&exchange.only().body).expect("a body"); assert_eq!((kept["act"].clone(), kept["args"]["table"].clone(), kept["args"]["id"].clone()), (json!("create"), json!("seal_key"), json!("dk")));let row = kept["args"]["data"].as_object().expect("the row").clone(); assert_eq!(cotton_client::seal::open_data_key(&vmk, 1, &row).expect("it opens").expose(), silent.expose(), "kept on the object under the vault key"); assert_eq!(client.data_keys().data_key(&database).expect("read").map(|k| *k.expose()), Some(*silent.expose())); assert_eq!(client.carried_key_from(&database), None, "carried once");}/// RULE (sign in to an account that has a key): the object's key stands and the signed-out phone's is retired;/// nothing is sent, and the carried key is forgotten. The phone's records were kept in the clear on the phone, so/// they go up sealed under the account's key.#[test]fn an_existing_account_keeps_its_key_and_retires_the_signed_out_one() {use cotton_client::seal::DataKeyStoreas _;let (_dir, client, _clock) = attached_on_a_clock();let database = client.current_database().expect("a database");let vmk = crate::phone::unlock::tests::fixture_vmk();let (account, row) = cotton_client::seal::mint_data_key(&vmk, 1, "2026-11-11T10:00:00.000Z").expect("the account's key");let file = rusqlite::Connection::open(client.layout().replica(&database)).expect("the copy"); cotton_client::seal::write_notes(&file, &[cotton_client::seal::Note::Key { id: "dk".into(), data: row }]).expect("kept"); drop(file); client.data_keys().hold("local-phone-db", &cotton_core::seal::DataKey::from_bytes([42; 32])); client.set_carried_key(Some((database.clone(), "local-phone-db".into())));let (door, exchange) = crate::phone::unlock::tests::door([]); client.set_object_door(Some(Arc::new(door)));let dk = super::ensure(&client, &vmk, 1).expect("the key taken"); assert_eq!(dk.expose(), account.expose(), "the account's key stands"); assert!(exchange.seen().is_empty(), "nothing is sent"); assert_eq!(client.carried_key_from(&database), None); assert_eq!(client.data_keys().data_key(&database).expect("read").map(|k| *k.expose()), Some(*account.expose()));}/// RULE (the launch walk, 22 Nov 2026): the line a phone with no screen lock is told when its key opens stands alone/// (it comes as a toast right after a first sign-in, with no Unlock tapped), so it says what is open in a whole/// sentence. It read "Open. A screen lock … would also keep it from anyone …": "Open." of nothing, "it" of nothing.#[test]fn the_no_lock_line_says_what_is_open() {let lines = cotton_core::rules::shell_lines::LineSet::shipped("unlock").expect("the unlock lines");let said = lines.say("no_lock_opened", &[]).expect("the line"); assert!(!said.starts_with("Open."), "{said}"); assert!(said.contains("What you keep here") && said.contains("screen lock"), "{said}");}/// RULE (the launch walk, 22 Nov 2026): Your Claude's opening line stands above "Claude is not connected" too, so it/// never says the person connected it. It read "Your Claude can, because you connected it" on a phone with none.#[test]fn your_claudes_promise_holds_before_it_is_connected() {let lines = cotton_core::rules::shell_lines::LineSet::shipped("unlock").expect("the unlock lines");let said = lines.say("account.promise", &[]).expect("the line"); assert!(!said.contains("because you connected"), "{said}"); assert!(said.contains("once you connect it"), "{said}");}/// RULE (SEAL KEY LATE, the coordinator 26 Nov 2026): a vault opened before any pass (a recovery code entered first)/// takes no key, as the copy holds no `seal_key` row; once a pass brings the row, the key is taken and held with no/// second ask. Nothing is ever minted late.#[test]fn a_vault_opened_before_any_pass_takes_its_key_once_the_row_comes() {use cotton_client::seal::DataKeyStoreas _;let (_dir, client, _clock) = attached_on_a_clock();let database = client.current_database().expect("a database");let vmk = Vmk::from_bytes([3; 32]); client.vault().expect("a door").unlock(Vmk::from_bytes([3; 32]), 1, client.now()); assert!(super::take_late(&client).is_empty(), "no row yet: nothing taken"); assert!(client.data_keys().data_key(&database).expect("read").is_none()); assert_eq!(call(&client, "seal.status", json!({})).expect("a status")["minted"], false, "nothing minted late");// The pass brings the object's row.let (dk, row) = cotton_client::seal::mint_data_key(&vmk, 1, "2026-11-11T10:00:00.000Z").expect("a minted key");let file = rusqlite::Connection::open(client.layout().replica(&database)).expect("the copy"); cotton_client::seal::write_notes(&file, &[cotton_client::seal::Note::Key { id: "dk".into(), data: row }]).expect("kept"); drop(file); assert_eq!(super::take_late(&client), vec![database.clone()]); assert_eq!(client.data_keys().data_key(&database).expect("read").map(|held| held.expose().to_vec()), Some(dk.expose().to_vec())); assert!(super::take_late(&client).is_empty(), "held: nothing more");}/// RULE (SEAL KEY LATE): with two databases on one phone, each key is taken only through its own vault: one vault/// unlocked never opens the other's key, and each takes its own once both are open.#[test]fn two_databases_each_take_their_key_through_their_own_vault() {use cotton_client::seal::DataKeyStoreas _;let (_dir, client, _clock) = attached_on_a_clock();let first = client.current_database().expect("a database");let second = "invented-household-db".to_string();let path = client.layout().replica(&second); std::fs::create_dir_all(path.parent().expect("a parent")).expect("the folder"); drop(crate::fixture::seeded(&path));let keep = |database: &str, vmk_byte: u8| {let (dk, row) = cotton_client::seal::mint_data_key(&Vmk::from_bytes([vmk_byte; 32]), 1, "2026-11-11T10:00:00.000Z").expect("a minted key");let file = rusqlite::Connection::open(client.layout().replica(database)).expect("the copy"); cotton_client::seal::write_notes(&file, &[cotton_client::seal::Note::Key { id: "dk".into(), data: row }]).expect("kept"); dk };let first_dk = keep(&first, 3);let second_dk = keep(&second, 4);// Only the first's vault is open. client.vault_session().of(&first).unlock(Vmk::from_bytes([3; 32]), 1, client.now()); assert_eq!(super::take_late_for(&client, &second).expect("asked"), false, "the second's vault is locked"); assert!(super::take_late_for(&client, &first).expect("asked")); assert!(client.data_keys().data_key(&second).expect("read").is_none(), "never opened by the first's vault"); client.vault_session().of(&second).unlock(Vmk::from_bytes([4; 32]), 1, client.now()); assert!(super::take_late_for(&client, &second).expect("asked")); assert_eq!(client.data_keys().data_key(&first).expect("read").map(|k| k.expose().to_vec()), Some(first_dk.expose().to_vec())); assert_eq!(client.data_keys().data_key(&second).expect("read").map(|k| k.expose().to_vec()), Some(second_dk.expose().to_vec()));}/// RULE (SEAL KEY LATE REVIEW): a row the open vault does not open is never taken: one wrapped under another vault/// key, or under a vault version this phone did not unlock. Each refuses by its sealing code, and no key is held.#[test]fn a_row_the_open_vault_does_not_open_is_not_taken() {use cotton_client::seal::DataKeyStoreas _;let (_dir, client, _clock) = attached_on_a_clock();let database = client.current_database().expect("a database");let (_, row) = cotton_client::seal::mint_data_key(&Vmk::from_bytes([9; 32]), 1, "2026-11-11T10:00:00.000Z").expect("a minted key");let file = rusqlite::Connection::open(client.layout().replica(&database)).expect("the copy"); cotton_client::seal::write_notes(&file, &[cotton_client::seal::Note::Key { id: "dk".into(), data: row }]).expect("kept"); drop(file);// Another vault key. client.vault().expect("a door").unlock(Vmk::from_bytes([3; 32]), 1, client.now()); assert!(super::take_late_for(&client, &database).is_err(), "another vault key does not open it"); assert!(super::take_late(&client).is_empty()); assert!(client.data_keys().data_key(&database).expect("read").is_none());// The right key, another version. client.vault().expect("a door").unlock(Vmk::from_bytes([9; 32]), 2, client.now()); assert!(super::take_late_for(&client, &database).is_err(), "a version mismatch does not open it"); assert!(client.data_keys().data_key(&database).expect("read").is_none());// The right key at its version. client.vault().expect("a door").unlock(Vmk::from_bytes([9; 32]), 1, client.now()); assert!(super::take_late_for(&client, &database).expect("opened"));}/// RULE (SEAL KEY LATE REVIEW): taking a key late retires only a key carried into THAT database; a key carried into/// another stays, so that one is never minted afresh.#[test]fn a_late_key_retires_only_its_own_databases_carried_key() {let (_dir, client, _clock) = attached_on_a_clock();let database = client.current_database().expect("a database");let (_, row) = cotton_client::seal::mint_data_key(&Vmk::from_bytes([3; 32]), 1, "2026-11-11T10:00:00.000Z").expect("a minted key");let file = rusqlite::Connection::open(client.layout().replica(&database)).expect("the copy"); cotton_client::seal::write_notes(&file, &[cotton_client::seal::Note::Key { id: "dk".into(), data: row }]).expect("kept"); drop(file); client.vault().expect("a door").unlock(Vmk::from_bytes([3; 32]), 1, client.now()); client.set_carried_key(Some(("invented-account-a".into(), "local-before-sign-in".into()))); assert!(super::take_late_for(&client, &database).expect("taken")); assert_eq!(client.carried_key_from("invented-account-a").as_deref(), Some("local-before-sign-in"), "another database's carried key stays");// Carried into this database: retired once its own key is held.let (_dir2, client2, _clock2) = attached_on_a_clock();let database2 = client2.current_database().expect("a database");let (_, row) = cotton_client::seal::mint_data_key(&Vmk::from_bytes([3; 32]), 1, "2026-11-11T10:00:00.000Z").expect("a minted key");let file = rusqlite::Connection::open(client2.layout().replica(&database2)).expect("the copy"); cotton_client::seal::write_notes(&file, &[cotton_client::seal::Note::Key { id: "dk".into(), data: row }]).expect("kept"); drop(file); client2.vault().expect("a door").unlock(Vmk::from_bytes([3; 32]), 1, client2.now()); client2.set_carried_key(Some((database2.clone(), "local-before-sign-in".into()))); assert!(super::take_late_for(&client2, &database2).expect("taken")); assert_eq!(client2.carried_key_from(&database2), None, "its own carried key is retired");}/// RULE (SEAL KEY LATE REVIEW): through the hook a finished pass calls (`handle.rs`, the after-pass thread), a vault/// opened before the row came takes its key once the copy holds the row.#[test]fn the_after_pass_hook_takes_a_late_key() {use cotton_client::seal::DataKeyStoreas _;let dir = tempfile::tempdir().expect("a temp container");let handle = crate::handle::CottonHandle::open(crate::handle::OpenConfig { mode: crate::handle::OpenMode::Phone { container_path: dir.path().to_string_lossy().to_string(), previous_container_path: None }, app_version: None, time_zone: Some("Asia/Kolkata".to_string()), device_secret: Some(crate::fixture::paired_secret()), vault_secret: None, flavor: cotton_core::flavor::Flavor::Real, manifest: None, }) .expect("an open core");crate::methods::dispatch(&handle, "workspace.startLocal", &json!({"deviceName": "An invented phone"})).expect("started");let client = handle.client();let database = client.current_database().expect("a database"); client.vault().expect("a door").unlock(Vmk::from_bytes([3; 32]), 1, client.now());let (dk, row) = cotton_client::seal::mint_data_key(&Vmk::from_bytes([3; 32]), 1, "2026-11-11T10:00:00.000Z").expect("a minted key");let file = rusqlite::Connection::open(client.layout().replica(&database)).expect("the copy"); cotton_client::seal::write_notes(&file, &[cotton_client::seal::Note::Key { id: "dk".into(), data: row }]).expect("kept"); drop(file);// A local copy may already hold a key of its own (made before sign-in): the late take is for one that holds none. client.data_keys().forget(&database); client.fire_after_pass_for_test();let deadline = std::time::Instant::now() + std::time::Duration::from_secs(30);loop {ifletSome(held) = client.data_keys().data_key(&database).expect("read") { assert_eq!(held.expose().to_vec(), dk.expose().to_vec());break; } assert!(std::time::Instant::now() < deadline, "the hook took no key in 30 s"); std::thread::sleep(std::time::Duration::from_millis(50)); }}/// RULE (FIRST SYNC 6 REVIEW (4)): `ensure`, taking a database's key with the vault open, retires only a key carried/// into that database, as the late take does.#[test]fn ensure_retires_only_its_own_databases_carried_key() {let (_dir, client, _clock) = attached_on_a_clock();let database = client.current_database().expect("a database");let (_, row) = cotton_client::seal::mint_data_key(&Vmk::from_bytes([3; 32]), 1, "2026-11-11T10:00:00.000Z").expect("a minted key");let file = rusqlite::Connection::open(client.layout().replica(&database)).expect("the copy"); cotton_client::seal::write_notes(&file, &[cotton_client::seal::Note::Key { id: "dk".into(), data: row }]).expect("kept"); drop(file); client.set_carried_key(Some(("invented-account-a".into(), "local-before-sign-in".into())));super::ensure(&client, &Vmk::from_bytes([3; 32]), 1).expect("taken"); assert_eq!(client.carried_key_from("invented-account-a").as_deref(), Some("local-before-sign-in"), "another database's carried key stays");use cotton_client::seal::DataKeyStoreas _; client.data_keys().forget(&database); client.set_carried_key(Some((database.clone(), "local-before-sign-in".into())));super::ensure(&client, &Vmk::from_bytes([3; 32]), 1).expect("taken"); assert_eq!(client.carried_key_from(&database), None, "its own is retired");}/// RULE (FIRST SYNC 7 REVIEW, low): `ensure` minting a database's first key (none on its object, nothing carried into/// it) leaves a key carried into ANOTHER database where it was, as the take does.#[test]fn ensure_minting_leaves_another_databases_carried_key() {let (_dir, client, _clock) = attached_on_a_clock();let database = client.current_database().expect("a database");let sealer = crate::phone::personal_object::sealer_of(&client, &database); sealer.learned_all();let (door, exchange) = crate::phone::unlock::tests::door([cotton_client::transport::scripted::Reply::ok("{}")]); client.set_object_door(Some(Arc::new(door))); client.set_carried_key(Some(("invented-account-a".into(), "local-before-sign-in".into())));super::ensure(&client, &crate::phone::unlock::tests::fixture_vmk(), 1).expect("minted");let kept: Value = serde_json::from_slice(&exchange.only().body).expect("a body"); assert_eq!(kept["args"]["table"], json!("seal_key"), "a first key minted and kept"); assert_eq!(client.carried_key_from("invented-account-a").as_deref(), Some("local-before-sign-in"), "another database's carried key stays");}/// RULE (FIRST SYNC 9c): the phone reports each copy's fill slice holds this launch: how many, the longest and the/// 99th, and nothing for a copy that ran none.#[test]fn the_fill_holds_are_reported_by_count_longest_and_99th() {let path = std::path::PathBuf::from("/invented/holds/replica.db"); assert_eq!(super::fill_holds(&path), None, "no slice ran");super::FILL_HOLDS.lock().unwrap().insert(path.clone(), (1..=200).rev().collect());let holds = super::fill_holds(&path).expect("reported"); assert_eq!((holds.slices, holds.max_ms, holds.p99_ms), (200, 200, 199)); assert_eq!(serde_json::to_value(holds).unwrap(), json!({ "slices": 200, "maxMs": 200, "p99Ms": 199 }));}