//! Groundwork slice 5a-ii, the store core of the five kinds of delete //! (design 6.2): an ERASE takes its record, its owned descendants, their //! receipts, history and search rows for good, keeps itself in `_gone` as its //! minted receipt on every device, and a later write at an erased id parks //! `gone`; `mint` refuses a delete that names no kind, and an act that //! deletes an owning child beside its owner; a content-keyed importer takes //! the next generation. //! //! Every row is synthetic ([nothing-real-ever-goes-in-cotton-s-public-tree]). use cotton_store::actor::{Actor, JobName}; use cotton_store::changes::changes_since; use cotton_store::erase::{generation_id, is_gone}; use cotton_store::fixtures::{file_at_version, seed_workspace_row, NOW, OWNER, WORKSPACE}; use cotton_store::fold::{arrive, Arrival, FoldResult}; use cotton_store::meta::{seed_meta, Meta}; use cotton_store::mint::{changes_of, first_author, mint, Act, AuthorReason, Change, Clock, Delete, Held, History, IdSource, Kind}; use cotton_store::outbox::LocalChange; use cotton_store::protocol::derived_id::derived_id; use cotton_store::protocol::types::{CottonOp, OpKind}; use cotton_store::schema::ddl::SCHEMA_VERSION; use cotton_store::Driver; use rusqlite::types::Value as SqlValue; use serde_json::{json, Map, Value}; const T0: i64 = 1_759_480_000_000; const NOTES: &str = "cotton_notes"; const ATTACHMENTS: &str = "cotton_note_attachments"; const PROJECTS: &str = "cotton_memory_projects"; const PROJECT_LINKS: &str = "cotton_project_links"; const LINKS: &str = "cotton_links"; const NOTE: &str = "00000000-0000-4000-8000-0000000e0001"; const OTHER_NOTE: &str = "00000000-0000-4000-8000-0000000e0002"; const ATTACHMENT: &str = "00000000-0000-4000-8000-0000000e0003"; const PROJECT: &str = "00000000-0000-4000-8000-0000000e0004"; const PROJECT_LINK: &str = "00000000-0000-4000-8000-0000000e0005"; const LINK: &str = "00000000-0000-4000-8000-0000000e0006"; const PHOTO: &str = "00000000-0000-4000-8000-0000000e0007"; const REPOS: &str = "cotton_repos"; const REPO_VARS: &str = "cotton_repo_vars"; const REPO: &str = "00000000-0000-4000-8000-0000000e0008"; const REPO_VAR: &str = "00000000-0000-4000-8000-0000000e0009"; const OTHER_REPO_VAR: &str = "00000000-0000-4000-8000-0000000e000a"; struct Ids(u32, &'static str); impl IdSource for Ids { fn next_op_id(&mut self, _now_ms: i64) -> cotton_store::Result { self.0 += 1; Ok(format!("{}-{:05}", self.1, self.0)) } } struct Dev { db: Driver, ids: Ids, author: String, fact: CottonOp, } fn device(tag: &'static str) -> Dev { let db = file_at_version(SCHEMA_VERSION); seed_meta( &db, &Meta { workspace_id: WORKSPACE.to_string(), owner_id: OWNER.to_string(), device_id: format!("device:{tag}"), last_server_seq: 0, last_hlc: None, }, SCHEMA_VERSION, ) .expect("seeds"); seed_workspace_row(&db); let mut ids = Ids(0, tag); let author = first_author(&db, &format!("key-{tag}"), Some(tag), AuthorReason::First, &Clock::wall(T0 - 10), &mut ids, Held::Served) .expect("the author"); Dev { db, ids, author: author.id, fact: author.fact } } /// Two devices that have heard each other's author. fn pair() -> (Dev, Dev) { let a = device("a"); let b = device("b"); a.take(std::slice::from_ref(&b.fact)); b.take(std::slice::from_ref(&a.fact)); (a, b) } fn obj(value: Value) -> Map { value.as_object().expect("an object").clone() } fn note(id: &str, body: &str) -> Change { Change::create( NOTES, id, obj(json!({"id": id, "workspace_id": WORKSPACE, "title": "Synthetic", "body": body, "created_at": NOW, "updated_at": NOW})), ) } fn attachment(id: &str, note_id: &str) -> Change { Change::create( ATTACHMENTS, id, obj(json!({"id": id, "workspace_id": WORKSPACE, "note_id": note_id, "record_kind": "photo", "record_id": PHOTO, "position": 0, "created_at": NOW, "updated_at": NOW})), ) } fn project() -> Change { Change::create( PROJECTS, PROJECT, obj(json!({"id": PROJECT, "workspace_id": WORKSPACE, "slug": "synthetic", "name": "Synthetic", "created_at": NOW, "updated_at": NOW})), ) } fn project_link(record_id: &str) -> Change { Change::create( PROJECT_LINKS, PROJECT_LINK, obj(json!({"id": PROJECT_LINK, "workspace_id": WORKSPACE, "project_id": PROJECT, "record_kind": "note", "record_id": record_id, "applied_by": "user", "created_at": NOW, "updated_at": NOW})), ) } fn link(from: &str, to: &str) -> Change { Change::create( LINKS, LINK, obj(json!({"id": LINK, "workspace_id": WORKSPACE, "from_kind": "note", "from_id": from, "to_kind": "note", "to_id": to, "created_at": NOW})), ) } fn body(text: &str) -> Change { Change::update(NOTES, NOTE, obj(json!({"body": text, "updated_at": NOW}))) } impl Dev { fn act(&mut self, at_ms: i64, changes: Vec) -> Vec { self.try_act(at_ms, changes).expect("mints") } fn try_act(&mut self, at_ms: i64, changes: Vec) -> cotton_store::Result> { let act = Act { actor: Actor::device_job(JobName::named("test").expect("a job name")), verb: "test".to_string(), history: History::Kept, changes, }; mint(&self.db, &act, &Clock::wall(at_ms), &mut self.ids, Held::Served).map(|minted| minted.ops) } /// Mint what a writer collected, the way a writer commits it: through /// `changes_of`, which decides each change's kind against the file. fn write(&mut self, at_ms: i64, local: &[LocalChange]) -> cotton_store::Result> { let changes = changes_of(&self.db, local); self.try_act(at_ms, changes) } fn take(&self, ops: &[CottonOp]) -> FoldResult { arrive(&self.db, ops, Arrival::Pulled).expect("folds") } /// Everything this device's own author wrote, as a full exchange hands it. fn exchange(&self) -> Vec { changes_since(&self.db, &self.author, "", 10_000).expect("a page").ops } fn count(&self, sql: &str, params: &[&str]) -> i64 { let params: Vec = params.iter().map(|p| SqlValue::Text((*p).to_string())).collect(); self.db.get(sql, ¶ms).expect("reads").and_then(|row| row.int("n")).unwrap_or(-1) } fn present(&self, table: &str, id: &str) -> bool { self.count(&format!("SELECT COUNT(*) AS n FROM {table} WHERE id = ?"), &[id]) > 0 } fn receipts(&self, table: &str, id: &str) -> i64 { self.count("SELECT COUNT(*) AS n FROM _writes WHERE tbl = ? AND row_id = ?", &[table, id]) } fn gone_row(&self, table: &str, id: &str) -> Option<(String, Vec, Option>)> { self.db .get( "SELECT op_id, bytes, sig FROM _gone WHERE tbl = ? AND row_id = ?", &[SqlValue::Text(table.to_string()), SqlValue::Text(id.to_string())], ) .expect("reads") .map(|row| { let blob = |name: &str| match row.get(name) { Some(SqlValue::Blob(b)) => Some(b.clone()), Some(SqlValue::Text(t)) => Some(t.as_bytes().to_vec()), _ => None, }; (row.text("op_id").unwrap_or_default().to_string(), blob("bytes").unwrap_or_default(), blob("sig")) }) } fn gone_reason(&self, table: &str, id: &str) -> Option { self.db .get( "SELECT reason FROM _gone WHERE tbl = ? AND row_id = ?", &[SqlValue::Text(table.to_string()), SqlValue::Text(id.to_string())], ) .expect("reads") .and_then(|row| row.text("reason").map(str::to_string)) } fn tombstoned(&self, table: &str, id: &str) -> bool { self.count("SELECT COUNT(*) AS n FROM _tombstones WHERE tbl = ? AND row_id = ?", &[table, id]) > 0 } fn parked_class(&self, op_id: &str) -> Option { self.db .get("SELECT class FROM _parked WHERE op_id = ?", &[SqlValue::Text(op_id.to_string())]) .expect("reads") .and_then(|row| row.text("class").map(str::to_string)) } /// Whether any place a value can live after a write still holds `text`. fn holds_text(&self, text: &str) -> Vec<&'static str> { let like = format!("%{text}%"); let mut places = Vec::new(); for (place, sql) in [ ("_writes", "SELECT COUNT(*) AS n FROM _writes WHERE CAST(bytes AS TEXT) LIKE ?"), ("_tombstones", "SELECT COUNT(*) AS n FROM _tombstones WHERE CAST(bytes AS TEXT) LIKE ?"), ("_outbox", "SELECT COUNT(*) AS n FROM _outbox WHERE CAST(bytes AS TEXT) LIKE ?"), ("_gone", "SELECT COUNT(*) AS n FROM _gone WHERE CAST(bytes AS TEXT) LIKE ?"), ("cotton_changes", "SELECT COUNT(*) AS n FROM cotton_changes WHERE coalesce(before, '') || coalesce(after, '') LIKE ?"), ("cotton_notes", "SELECT COUNT(*) AS n FROM cotton_notes WHERE body LIKE ?"), ] { if self.count(sql, &[&like]) > 0 { places.push(place); } } places } fn fts_matches(&self, word: &str) -> i64 { self.count("SELECT COUNT(*) AS n FROM fts_notes WHERE fts_notes MATCH ?", &[word]) } } /// RULE: A write at an erased id is never applied: mint refuses it locally, and one arriving from a peer parks `gone`, never recreating the record. #[test] fn erased_record_is_never_recreated_by_a_later_write() { let (mut a, mut b) = pair(); let made = a.act(T0, vec![note(NOTE, "synthetic body")]); b.take(&made); // B edits the note before it hears of the erase. let edit = b.act(T0 + 5_000, vec![body("an edit after the erase")]); let erase = a.act(T0 + 1_000, vec![Change::erase(NOTES, NOTE, "bin")]); assert!(!a.present(NOTES, NOTE)); assert!(is_gone(&a.db, NOTES, NOTE).expect("reads")); // Arriving later, at a higher stamp, the edit parks `gone`. let folded = a.take(&edit); assert!(folded.applied.is_empty(), "{folded:?}"); assert!(!a.present(NOTES, NOTE), "the edit recreated the erased note"); assert_eq!(a.parked_class(&edit[0].op_id).as_deref(), Some("gone")); // A fresh create at the id, here or on the peer once it holds the erase. let local = a.try_act(T0 + 9_000, vec![note(NOTE, "recreated")]); assert!(local.is_err(), "mint recreated an erased id"); b.take(&erase); assert!(!b.present(NOTES, NOTE)); let again = b.try_act(T0 + 9_000, vec![note(NOTE, "recreated")]); assert!(again.is_err(), "mint recreated an erased id on the peer"); } /// RULE: An erase takes its record's owned descendants — owning children present or dormant and the links, project links and attachments naming it — with every receipt, history row and search row. #[test] fn erase_of_parent_leaves_no_child_receipt_history_or_fts() { let (mut a, b) = pair(); let mut ops = a.act(T0, vec![note(NOTE, "zebracorn body"), note(OTHER_NOTE, "other"), project()]); ops.extend(a.act(T0 + 1_000, vec![attachment(ATTACHMENT, NOTE), project_link(NOTE), link(OTHER_NOTE, NOTE)])); ops.extend(a.act(T0 + 2_000, vec![body("zebracorn edited")])); b.take(&ops); assert!(a.count("SELECT COUNT(*) AS n FROM cotton_changes WHERE record_id = ?", &[NOTE]) > 0, "history to erase"); assert_eq!(a.fts_matches("zebracorn"), 1); let erase = a.act(T0 + 3_000, vec![Change::erase(NOTES, NOTE, "bin")]); b.take(&erase); for dev in [&a, &b] { for (table, id) in [(NOTES, NOTE), (ATTACHMENTS, ATTACHMENT), (PROJECT_LINKS, PROJECT_LINK), (LINKS, LINK)] { assert!(!dev.present(table, id), "{table} {id} outlived the erase"); assert_eq!(dev.receipts(table, id), 0, "{table} {id} kept a receipt"); assert!(is_gone(&dev.db, table, id).expect("reads"), "{table} {id} has no _gone row"); } assert_eq!(dev.count("SELECT COUNT(*) AS n FROM cotton_changes WHERE record_id = ?", &[NOTE]), 0, "history outlived the erase"); assert_eq!(dev.fts_matches("zebracorn"), 0, "the search index outlived the erase"); // What it did not own stands. assert!(dev.present(NOTES, OTHER_NOTE) && dev.present(PROJECTS, PROJECT)); } // A DORMANT child — its owner retracted, not erased — goes with the owner's erase. let (mut a, _) = pair(); a.act(T0, vec![note(NOTE, "dormant owner")]); a.act(T0 + 1_000, vec![attachment(ATTACHMENT, NOTE)]); a.act(T0 + 2_000, vec![Change::retract(NOTES, NOTE)]); assert!(!a.present(ATTACHMENTS, ATTACHMENT)); assert!(a.receipts(ATTACHMENTS, ATTACHMENT) > 0, "a dormant child keeps its receipts"); a.act(T0 + 3_000, vec![Change::erase(NOTES, NOTE, "bin")]); assert_eq!(a.receipts(ATTACHMENTS, ATTACHMENT), 0, "the dormant child's receipts outlived the erase"); assert!(is_gone(&a.db, ATTACHMENTS, ATTACHMENT).expect("reads")); } fn repo() -> Change { Change::create( REPOS, REPO, obj(json!({"id": REPO, "workspace_id": WORKSPACE, "key": "example.invalid/synthetic/repo", "host": "example.invalid", "owner": "synthetic", "name": "repo", "kind": "cotton", "web_url": "https://example.invalid/synthetic/repo", "created_at": NOW, "updated_at": NOW})), ) } fn repo_var(id: &str, name: &str) -> Change { Change::create( REPO_VARS, id, obj(json!({"id": id, "workspace_id": WORKSPACE, "repo_id": REPO, "name": name, "value": "1", "created_at": NOW, "updated_at": NOW})), ) } /// RULE: An erase of a repository takes its server-only variables with it: no `cotton_repo_vars` row or receipt naming the repository outlives the erase, and each is kept in `_gone`. #[test] fn erase_of_a_repository_takes_its_server_only_vars() { let (mut a, _) = pair(); a.write(T0, &[local(repo())]).expect("mints"); a.write(T0 + 1_000, &[local(repo_var(REPO_VAR, "SYNTHETIC_FLAG")), local(repo_var(OTHER_REPO_VAR, "RUST_BACKTRACE"))]) .expect("mints"); assert!(a.present(REPO_VARS, REPO_VAR) && a.present(REPO_VARS, OTHER_REPO_VAR), "the vars to erase"); a.write(T0 + 2_000, &[LocalChange::delete(REPOS, REPO).erase("bin")]).expect("mints"); assert!(!a.present(REPOS, REPO), "the erased repository stands"); for id in [REPO_VAR, OTHER_REPO_VAR] { assert!(!a.present(REPO_VARS, id), "cotton_repo_vars {id} outlived its repository"); assert_eq!(a.receipts(REPO_VARS, id), 0, "cotton_repo_vars {id} kept a receipt"); assert!(is_gone(&a.db, REPO_VARS, id).expect("reads"), "cotton_repo_vars {id} has no _gone row"); } } /// RULE: An erase is kept in `_gone` as its minted receipt: the eraser, a device that folded it from the exchange and a snapshot of that device hold the same bytes and signature. #[test] fn erasure_travels_as_its_minted_bytes() { let (mut a, b) = pair(); a.act(T0, vec![note(NOTE, "synthetic")]); let erase = a.act(T0 + 1_000, vec![Change::erase(NOTES, NOTE, "bin")]); b.take(&a.exchange()); assert!(!b.present(NOTES, NOTE)); let path = std::env::temp_dir().join(format!("five-kinds-snapshot-{}.db", std::process::id())); let _ = std::fs::remove_file(&path); b.db.exec(&format!("VACUUM INTO '{}'", path.display())).expect("copies"); let snapshot = Driver::open(&path).expect("opens the copy"); cotton_store::snapshot::strip_for_phone(&snapshot).expect("strips"); let minted = erase[0].receipt.as_bytes().to_vec(); let on_a = a.gone_row(NOTES, NOTE).expect("the eraser keeps it"); assert_eq!(on_a.0, erase[0].op_id); assert_eq!(on_a.1, minted, "the eraser's _gone is not the minted receipt"); let on_b = b.gone_row(NOTES, NOTE).expect("the peer keeps it"); let on_snapshot = Dev { db: snapshot, ids: Ids(0, "c"), author: String::new(), fact: erase[0].clone() } .gone_row(NOTES, NOTE) .expect("the snapshot keeps it"); assert_eq!(on_b, on_a, "the peer's erase is not byte-equal to the writer's"); assert_eq!(on_snapshot, on_a, "the snapshot's erase is not byte-equal to the writer's"); let _ = std::fs::remove_file(&path); } /// RULE: mint refuses an act that deletes an owning child together with an owning ancestor of it, whatever kinds the two name: the owner's delete takes the child, so the act names only the owner. #[test] fn no_act_retracts_an_owning_child_and_its_parent_together() { let (mut a, _) = pair(); a.act(T0, vec![note(NOTE, "owner"), note(OTHER_NOTE, "another")]); a.act(T0 + 1_000, vec![attachment(ATTACHMENT, NOTE)]); let plain = a.try_act(T0 + 2_000, vec![Change::retract(ATTACHMENTS, ATTACHMENT), Change::retract(NOTES, NOTE)]); assert!(plain.is_err(), "mint took two plain retracts of a child and its owner"); let written = a.write(T0 + 2_000, &[LocalChange::delete(NOTES, NOTE).retract(), LocalChange::delete(ATTACHMENTS, ATTACHMENT).retract()]); assert!(written.is_err(), "mint took a writer's two retracts of an owner and its child"); let both = a.try_act( T0 + 2_000, vec![Change::delete(NOTES, NOTE, Delete::Retract), Change::delete(ATTACHMENTS, ATTACHMENT, Delete::Retract)], ); assert!(both.is_err(), "mint took a child's retract beside its owner's"); let erased = a.try_act(T0 + 2_000, vec![Change::retract(ATTACHMENTS, ATTACHMENT), Change::erase(NOTES, NOTE, "bin")]); assert!(erased.is_err(), "mint took a child's retract beside its owner's erase"); // A child alone, or beside a note that does not own it, is a delete like any other. a.act(T0 + 3_000, vec![Change::retract(ATTACHMENTS, ATTACHMENT), Change::retract(NOTES, OTHER_NOTE)]); assert!(a.present(NOTES, NOTE) && !a.present(ATTACHMENTS, ATTACHMENT)); } /// RULE: A delete names its kind or is not minted: mint refuses a retract that names no kind of delete, and a create or change that names one. #[test] fn no_delete_is_minted_without_its_kind() { let (mut a, _) = pair(); a.act(T0, vec![note(NOTE, "kept")]); let kindless = Change { kind: Kind::Retract, table: NOTES.to_string(), row_id: NOTE.to_string(), patch: None, delete: Default::default() }; let refused = a.try_act(T0 + 1_000, vec![kindless]).expect_err("mint took a delete that names no kind"); assert!(refused.to_string().contains("without naming which kind of delete"), "{refused}"); let local = LocalChange { kind: OpKind::Delete, table: NOTES.to_string(), row_id: NOTE.to_string(), patch: None, delete: Default::default() }; assert!(a.write(T0 + 1_000, &[local]).is_err(), "mint took a writer's delete that names no kind"); assert!(a.present(NOTES, NOTE), "a kindless delete removed the row"); let upsert = Change { delete: Delete::Retract.into(), ..body("an upsert naming a delete") }; assert!(a.try_act(T0 + 2_000, vec![upsert]).is_err(), "mint took a change that names a kind of delete"); a.act(T0 + 3_000, vec![Change::retract(NOTES, NOTE)]); assert!(!a.present(NOTES, NOTE), "a named retract did not delete"); } /// RULE: A content-keyed id found in `_gone` takes the next generation, derived_id(table, [key…, "gen", n]), and every device holding the same erasures derives the same one. #[test] fn reingested_content_after_erase_takes_the_next_generation_on_every_device() { let (mut a, b) = pair(); let key = ["synthetic-source", "message-1"]; let first = generation_id(&a.db, NOTES, &key).expect("an id"); assert_eq!(first, derived_id(NOTES, &key)); a.act(T0, vec![note(&first, "imported")]); a.act(T0 + 1_000, vec![Change::delete(NOTES, &first, Delete::SourceErase { job: "synthetic".to_string() })]); b.take(&a.exchange()); let next_a = generation_id(&a.db, NOTES, &key).expect("an id"); let next_b = generation_id(&b.db, NOTES, &key).expect("an id"); assert_ne!(next_a, first, "re-ingest derived the erased id"); assert_eq!(next_a, derived_id(NOTES, &["synthetic-source", "message-1", "gen", "1"])); assert_eq!(next_a, next_b, "two devices picked different generations"); a.act(T0 + 2_000, vec![note(&next_a, "imported again")]); assert!(a.present(NOTES, &next_a)); assert_eq!( a.count("SELECT COUNT(*) AS n FROM _gone WHERE row_id = ? AND reason = 'source:synthetic'", &[&first]), 1, "a source-erase keeps its job as the reason" ); } /// RULE: An overwritten value stays in its receipt while that write lives, and an erase of the record removes it from every receipt, tombstone, queue, history row and search row. #[test] fn overwritten_value_is_kept_only_while_its_write_lives_and_erase_removes_it() { let (mut a, _) = pair(); a.act(T0, vec![note(NOTE, "quillback first value")]); a.act(T0 + 1_000, vec![body("second value")]); assert!(a.holds_text("quillback").contains(&"_writes"), "the overwritten value's write was dropped while it lives"); a.act(T0 + 2_000, vec![Change::erase(NOTES, NOTE, "bin")]); assert_eq!(a.holds_text("quillback"), Vec::<&str>::new(), "an old value outlived the erase"); assert_eq!(a.fts_matches("quillback"), 0); } fn local(change: Change) -> LocalChange { LocalChange::upsert(&change.table, &change.row_id, change.patch.expect("an upsert names columns")) } /// RULE: An Erase a writer collects as a LocalChange erases for good: the row, its owned children, their receipts, history and search rows leave on the writer and on a peer that pulls it, and both keep the minted receipt in `_gone`. #[test] fn an_erase_minted_through_a_writer_erases_here_and_on_a_peer() { let (mut a, b) = pair(); let mut ops = a.write(T0, &[local(note(NOTE, "wombatine body")), local(project())]).expect("mints"); ops.extend(a.write(T0 + 1_000, &[local(attachment(ATTACHMENT, NOTE)), local(project_link(NOTE))]).expect("mints")); ops.extend(a.write(T0 + 2_000, &[local(body("wombatine edited"))]).expect("mints")); b.take(&ops); assert_eq!(b.fts_matches("wombatine"), 1, "the peer holds the note to erase"); let erase = a.write(T0 + 3_000, &[LocalChange::delete(NOTES, NOTE).erase("bin")]).expect("mints"); b.take(&a.exchange()); let minted = erase[0].receipt.as_bytes().to_vec(); for (name, dev) in [("writer", &a), ("peer", &b)] { for (table, id) in [(NOTES, NOTE), (ATTACHMENTS, ATTACHMENT), (PROJECT_LINKS, PROJECT_LINK)] { assert!(!dev.present(table, id), "{name}: {table} {id} outlived the erase"); assert_eq!(dev.receipts(table, id), 0, "{name}: {table} {id} kept a receipt"); assert!(!dev.tombstoned(table, id) || table == NOTES, "{name}: {table} {id} kept a tombstone"); let gone = dev.gone_row(table, id).unwrap_or_else(|| panic!("{name}: {table} {id} has no _gone row")); assert_eq!(gone.1, minted, "{name}: {table} {id}'s _gone is not the minted receipt"); } assert_eq!(dev.count("SELECT COUNT(*) AS n FROM cotton_changes WHERE record_id = ?", &[NOTE]), 0, "{name}: history outlived the erase"); assert_eq!(dev.fts_matches("wombatine"), 0, "{name}: the search index outlived the erase"); assert!(dev.present(PROJECTS, PROJECT), "{name}: the erase took a project it did not own"); } } /// RULE: A Source-erase a writer collects keeps its job as the reason: `_gone.reason` is `source:` on the writer and on a peer that pulls it, so the job's next pass cannot bring the row back. #[test] fn a_source_erase_records_its_job() { let (mut a, b) = pair(); let made = a.write(T0, &[local(note(NOTE, "mirrored from outside"))]).expect("mints"); b.take(&made); a.write(T0 + 1_000, &[LocalChange::delete(NOTES, NOTE).source_erase("gmail")]).expect("mints"); b.take(&a.exchange()); for (name, dev) in [("writer", &a), ("peer", &b)] { assert!(!dev.present(NOTES, NOTE), "{name}: the source-erased note stands"); assert_eq!(dev.gone_reason(NOTES, NOTE).as_deref(), Some("source:gmail"), "{name}: the job is not the reason"); } } /// RULE: A Retract a writer collects is not an Erase: it leaves a tombstone and no `_gone` row, and a later create at the same id lands on the writer and on a peer, which is what relinking after an unlink relies on. #[test] fn a_retract_is_not_an_erase() { let (mut a, b) = pair(); let mut ops = a.write(T0, &[local(note(NOTE, "first life"))]).expect("mints"); ops.extend(a.write(T0 + 1_000, &[LocalChange::delete(NOTES, NOTE).retract()]).expect("mints")); b.take(&ops); for (name, dev) in [("writer", &a), ("peer", &b)] { assert!(!dev.present(NOTES, NOTE), "{name}: the retracted note stands"); assert!(dev.tombstoned(NOTES, NOTE), "{name}: a retract left no tombstone"); assert!(dev.gone_row(NOTES, NOTE).is_none(), "{name}: a retract was kept as an erase"); } let again = a.write(T0 + 2_000, &[local(note(NOTE, "second life"))]).expect("a create at a retracted id mints"); b.take(&again); assert!(a.present(NOTES, NOTE) && b.present(NOTES, NOTE), "the create at the retracted id did not land"); }