usesuper::*;usecrate::connection::{ConnectionMode, StoredConnection};usecrate::test_support::{temp_layout, WORKSPACE};const GARDEN: &str = "bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb";const AT: &str = "2026-10-01T00:00:00Z";fn removed() -> (tempfile::TempDir, Layout, ConnectionFile) {let (dir, layout) = temp_layout();let connection = ConnectionFile::at(layout.connection_json()); connection .write(StoredConnection { device_scope: None, takes_placements: false, mode: ConnectionMode::Remote, base_url: "https://synthetic.invalid".into(), workspace_id: Some(WORKSPACE.into()), owner_id: Some("synthetic-owner".into()), connected_at: Some(AT.into()), }) .unwrap();crate::test_support::hold_joined(&layout, GARDEN, "https://garden.invalid", "synthetic-mac"); connection.mark_removed(GARDEN, AT, None).unwrap();for id in [WORKSPACE, GARDEN] {let path = layout.replica(id); std::fs::create_dir_all(path.parent().unwrap()).unwrap(); std::fs::write(path, b"synthetic database contents").unwrap();let media = layout.client_media_dir().join(id); std::fs::create_dir_all(&media).unwrap(); std::fs::write(media.join("synthetic.jpg"), b"synthetic picture").unwrap(); } (dir, layout, connection)}fn now(s: &str) -> chrono::DateTime<chrono::Utc> { chrono::DateTime::parse_from_rfc3339(s).unwrap().to_utc()}/// RULE: A removed copy keeps its exact file and media for thirty days, then purges both without touching Personal; malformed or future dates never authorise erasure.#[test]fn removed_copy_keeps_its_bytes_until_day_thirty() {let (_dir, layout, connection) = removed();let path = layout.replica(GARDEN);let before = std::fs::read(&path).unwrap(); assert!(!purge_removed( &layout, &connection, GARDEN, now("2026-10-30T23:59:59.999Z") ) .unwrap()); assert_eq!(std::fs::read(&path).unwrap(), before); assert!(layout.client_media_dir().join(GARDEN).exists()); assert!(!purge_removed(&layout, &connection, WORKSPACE, now("2026-12-01T00:00:00Z")).unwrap()); assert!(purge_removed(&layout, &connection, GARDEN, now("2026-10-31T00:00:00Z")).unwrap()); assert!(!path.exists()); assert!(!layout.client_media_dir().join(GARDEN).exists()); assert!(layout.replica(WORKSPACE).exists()); assert!(layout.client_media_dir().join(WORKSPACE).exists()); assert!(!purge_removed(&layout, &connection, GARDEN, now("2026-12-01T00:00:00Z")).unwrap());}/// RULE: Purging a failed copy is retried from its retained removal marker; filesystem errors must not silently orphan bytes or forget which database still needs purging.#[test]fn failed_purge_keeps_the_marker_for_retry() {let (_dir, layout, connection) = removed();// QUEUE BAD OP 5: a copy that cannot be asked is kept whole, its media with it; an empty one holds nothing and goes,// so the removal of its media is what fails here. std::fs::write(layout.replica(GARDEN), b"").unwrap();let media = layout.client_media_dir().join(GARDEN); std::fs::remove_dir_all(&media).unwrap(); std::fs::write(&media, b"blocks folder removal").unwrap(); assert!(purge_removed(&layout, &connection, GARDEN, now("2026-10-31T00:00:00Z")).is_err()); assert!(connection .read_record() .unwrap() .places .iter() .any(|place| place.id == GARDEN && place.removed_at.is_some())); std::fs::remove_file(&media).unwrap(); assert!(purge_removed(&layout, &connection, GARDEN, now("2026-10-31T00:00:00Z")).unwrap());}/// RULE: A database id must never let purge erase the shared media folder or traverse into another database's files.#[test]fn purge_cannot_escape_its_database() {let (_dir, layout, _connection) = removed();for id in ["media","..","../media","/tmp","", GARDEN.trim_end_matches('b'), ] { assert!(purge_copy(&layout, id).is_err()); } assert!(layout.replica(WORKSPACE).exists()); assert!(layout.client_media_dir().join(WORKSPACE).exists());}/// RULE: Creator-device copies use the same removedAt retention as joined copies; a fresh signed listing restores an own copy without purging its retained file, and Personal is never marked removed.#[test]fn creator_copy_uses_existing_removal_and_listing_restore() {let (_dir,layout,connection)=removed();letmut record=connection.read_record().unwrap();let place=record.places.iter_mut().find(|place|place.id==GARDEN).unwrap(); place.origin=None;place.server_device_id=None;place.removed_at=None;crate::connection::write_record(&layout.connection_json(),&record).unwrap(); assert!(!connection.mark_removed(WORKSPACE,AT,None).unwrap()); assert!(connection.mark_removed(GARDEN,AT,None).unwrap());let before=std::fs::read(layout.replica(GARDEN)).unwrap(); assert!(!purge_removed(&layout,&connection,GARDEN,now("2026-10-30T00:00:00Z")).unwrap()); connection.apply_listing(&crate::connection::list::Listing{databases:vec![crate::connection::list::ListedDatabase{id:GARDEN.into(),name:"Synthetic garden".into(),kind:"shared".into(),schema_version:1}]},"2026-10-31T00:00:00Z").unwrap(); assert!(!purge_removed(&layout,&connection,GARDEN,now("2026-11-01T00:00:00Z")).unwrap()); assert_eq!(std::fs::read(layout.replica(GARDEN)).unwrap(),before);letmut record=connection.read_record().unwrap();record.places.iter_mut().find(|place|place.id==GARDEN).unwrap().removed_at=Some(AT.into());crate::connection::write_record(&layout.connection_json(),&record).unwrap(); assert!(purge_removed(&layout,&connection,GARDEN,now("2026-11-01T00:00:00Z")).unwrap()); assert!(layout.replica(WORKSPACE).exists());}/// RULE: An archive receipt removes the target's entire local copy at once, including WAL, pending downloads and media, while Personal and another database keep their exact bytes.#[test]fn archive_receipt_drops_every_local_byte_at_once() {let (_dir, layout, connection) = removed();let personal = std::fs::read(layout.replica(WORKSPACE)).unwrap();let kitchen = "cccccccc-cccc-4ccc-8ccc-cccccccccccc";let other = layout.replica(kitchen); std::fs::create_dir_all(other.parent().unwrap()).unwrap(); std::fs::write(&other, b"synthetic kitchen").unwrap(); std::fs::write(layout.replica(GARDEN).with_extension("db-wal"), b"synthetic WAL").unwrap(); std::fs::write(layout.replica(GARDEN).with_extension("download"), b"synthetic download").unwrap(); assert!(crate::join::leave::purge_removed_copy_now(&layout, &connection, GARDEN).unwrap()); assert!(!layout.replica(GARDEN).parent().unwrap().exists()); assert!(!layout.client_media_dir().join(GARDEN).exists());// QUEUE BAD OP 4: a copy that cannot be asked what it holds (its journal does not read) is kept aside whole, never// deleted; the place is free all the same.for kept incrate::copy_keep::kept(&layout, Some(GARDEN)) { assert!(kept.dir.join("copy").join("replica.db").exists(), "kept whole: {kept:?}"); } assert_eq!(std::fs::read(layout.replica(WORKSPACE)).unwrap(), personal); assert_eq!(std::fs::read(other).unwrap(), b"synthetic kitchen");}