use super::*; use crate::test_support::{open_test_replica, queue_one_change, WORKSPACE}; /// A phone's copy holding three unsent edits, then stamped by ANOTHER build: /// its stamp moved, a table it had is gone, and its outbox carries a column /// this build never wrote — everything but the receipts' three columns /// changed under it. Returns the copy's path. fn stale_copy_with_three_edits() -> (tempfile::TempDir, std::path::PathBuf) { let (dir, layout, replica) = open_test_replica(); for title in ["Buy cat litter", "Pay maintenance", "Call the plumber"] { queue_one_change(&replica, title); } drop(replica); let path = layout.replica(WORKSPACE); let copy = Driver::open(&path).expect("the copy"); copy.exec(&format!("PRAGMA user_version = {}", SCHEMA_VERSION - 1)).expect("another build's stamp"); copy.exec("ALTER TABLE _outbox ADD COLUMN another_builds_column TEXT").expect("a changed outbox"); copy.exec("DROP TABLE cotton_feedback").expect("a changed schema"); drop(copy); (dir, path) } /// RULE: a copy stamped by another build lists its unsent changes from the receipts alone — the new build reads only op_id, bytes and sig from the receipt tables and decodes the rest from the signed wire bytes, so a copy whose schema it cannot open as a store still names every change it holds, for the screen and for the discard's Feedback row; build 231 kept such a copy "until they are sent" behind a Try again that never could, and the owner lost five changes by reinstalling (architect ruling, 10 Oct 2026). #[test] fn a_copy_another_build_stamped_lists_its_unsent_changes_from_the_receipts_alone() { let (_dir, path) = stale_copy_with_three_edits(); assert!( cotton_store::migrate::migration_plan(&Driver::open(&path).expect("the copy")).is_err(), "the store refuses the copy, as it must" ); let listed = unsent_changes(&path).expect("the list"); // The three edits, and the fact naming this device as their author. assert_eq!(listed.iter().filter(|change| change.table == "cotton_todos").count(), 3, "{listed:?}"); assert_eq!(listed.iter().filter(|change| change.is_bookkeeping()).count(), 1, "{listed:?}"); assert!(listed.iter().all(|change| change.waiting == Waiting::Queued)); } /// RULE (QUEUE BAD OP 6, M1): the kept rows are written whole or not at all, and a list that does not parse is set /// aside under a numbered name of its own, never written over or removed. #[test] fn kept_rows_that_do_not_parse_are_set_aside_and_never_written_over() { let (_dir, layout) = crate::test_support::temp_layout(); let path = kept_path(&layout, WORKSPACE); std::fs::create_dir_all(path.parent().unwrap()).unwrap(); let row = |id: &str| KeptRow { table: "cotton_todos".into(), row_id: id.into(), data: serde_json::json!({"title": "invented"}), why: "a test".into(), at: "2026-11-27T09:00:00.000Z".into() }; for (n, damaged) in [(1, b"[{\"table\": half a list".to_vec()), (2, b"\x00\xff not json".to_vec())] { std::fs::write(&path, &damaged).unwrap(); keep_row(&layout, WORKSPACE, row(&format!("r{n}"))).unwrap(); let aside = path.with_file_name(format!("kept-rows.unreadable.{n}.json")); assert_eq!(std::fs::read(&aside).unwrap(), damaged, "set aside whole, under a name of its own"); assert_eq!(kept_rows(&layout, WORKSPACE).len(), 1, "the new list holds the new row"); } // Emptying a list that does not parse sets it aside first; it is never removed. std::fs::write(&path, b"not a list").unwrap(); set_kept_rows(&layout, WORKSPACE, &[]).unwrap(); assert_eq!(std::fs::read(path.with_file_name("kept-rows.unreadable.3.json")).unwrap(), b"not a list"); // Written whole: no half-written list stands, and the file of its own is gone once it is in place. keep_row(&layout, WORKSPACE, row("r9")).unwrap(); assert!(!path.with_file_name("kept-rows.json.next").exists()); assert_eq!(kept_rows(&layout, WORKSPACE), vec![row("r9")]); }