use super::*; use cotton_core::flavor::Flavor; use cotton_store::migrate::SCHEMA_VERSION; use cotton_store::Driver; const WORKSPACE: &str = "ws-not-moved"; /// A copy an older build stamped, with nothing unsent and no `_meta` — one /// the shape move refuses ("not a whole copy"). fn unmovable_copy(layout: &Layout) -> PathBuf { let path = layout.replica(WORKSPACE); std::fs::create_dir_all(path.parent().expect("a parent")).expect("the folder"); let driver = Driver::open(&path).expect("a file"); driver.exec(&format!("PRAGMA user_version = {}", SCHEMA_VERSION - 1)).expect("the stamp"); driver .exec( "CREATE TABLE _outbox (op_id TEXT PRIMARY KEY); CREATE TABLE _writes (op_id TEXT PRIMARY KEY, held INTEGER NOT NULL); CREATE TABLE _tombstones (op_id TEXT PRIMARY KEY, held INTEGER NOT NULL); CREATE TABLE _parked (op_id TEXT PRIMARY KEY, direction TEXT NOT NULL)", ) .expect("local receipts"); path } /// RULE: A copy the move could not take is marked with the move's why until the copy opens (or the device leaves); the mark is never a stale download. #[test] fn a_copy_not_moved_in_place_is_marked_with_why_until_it_opens() { let root = tempfile::tempdir().expect("a root"); let layout = Layout::new(root.path().join("CottonDB")); let path = unmovable_copy(&layout); assert_eq!(why(&layout, WORKSPACE), None, "nothing to say before an update"); let heal = crate::dev_heal::heal_stale_replica(&layout, WORKSPACE, Some(Flavor::Real), None); assert!(matches!(heal, Some(crate::dev_heal::Heal::NotMoved { .. })), "{heal:?}"); assert!(path.exists(), "the copy stays"); assert_eq!(why(&layout, WORKSPACE).as_deref(), Some("not a whole copy: it names no database")); // A sweep of the folder's stale downloads leaves it: it is not a download. let name = path.file_name().expect("a name").to_string_lossy().to_string(); let beside = format!("{name}{NOT_MOVED_SUFFIX}"); for live in [false, true] { assert!(crate::bootstrap::file::stale_downloads(&[beside.clone()], &name, live).is_empty()); } clear(&layout, WORKSPACE); assert_eq!(why(&layout, WORKSPACE), None, "the copy opened"); // A leave takes it with everything else beside the replica. mark(&layout, WORKSPACE, SCHEMA_VERSION - 1, "why"); crate::join::leave::delete_replica_files(&layout, WORKSPACE, crate::copy_keep::Ending::keep("a test leave")); assert_eq!(why(&layout, WORKSPACE), None, "a leave forgets it"); }