//! Whether a particular record can be put back. //! //! The reason this is one function: two of the refusals are facts about //! the ROW, not the kind, so a screen deciding for itself would offer a way //! back that the write then refuses. One answer, read by the screen and //! enforced by the service. use serde_json::json; use super::*; use crate::constants::VersionRecordKind; fn row(value: serde_json::Value) -> RecordRow { match value { serde_json::Value::Object(map) => map, other => panic!("a row fixture must be an object, got {other}"), } } /// A refusal as the kit's words say it (no database rows). fn said(block: Option) -> Option<&'static str> { block.map(|block| &*Box::leak(block.words(&[]).into_boxed_str())) } fn block(kind: VersionRecordKind, current: serde_json::Value) -> Option<&'static str> { said(restore_block(kind, Some(&row(current)))) } #[test] fn allows_a_restore_on_an_ordinary_live_record() { assert_eq!( block(VersionRecordKind::Todo, json!({ "id": "t1", "title": "a" })), None ); assert_eq!( block( VersionRecordKind::Note, json!({ "id": "n1", "managed_by": null }) ), None ); } #[test] fn refuses_a_kind_whose_text_is_written_elsewhere_in_that_kinds_words() { assert!( block(VersionRecordKind::TodoRecurrence, json!({ "id": "r1" })) .expect("a refusal") .contains("re-project") ); } #[test] fn refuses_when_the_record_itself_is_gone() { assert!(said(restore_block(VersionRecordKind::Todo, None)) .expect("a refusal") .contains("gone")); } #[test] fn refuses_a_managed_note_whose_words_the_mirror_owns() { for kind in [VersionRecordKind::Note, VersionRecordKind::Journal] { assert!(block(kind, json!({ "id": "n1", "managed_by": "claude" })) .expect("a refusal") .contains("overwritten by the next sync")); } } #[test] fn does_not_read_managed_by_on_a_kind_that_has_no_such_column() { assert_eq!( block( VersionRecordKind::Todo, json!({ "id": "t1", "managed_by": "claude" }) ), None ); } #[test] fn puts_the_kinds_own_refusal_before_the_rows_since_it_is_the_stronger_fact() { assert!(said(restore_block(VersionRecordKind::TodoRecurrence, None)) .expect("a refusal") .contains("re-project")); } // ── Node vectors ───────────────────────────────────────────────────────────── // // Twenty-five `(kind, row)` pairs were run through the TypeScript. The twelve // above are its own test file's; the rest are below, and the five that pin // JavaScript truthiness are the reason `is_truthy` exists at all. /// The sentences, verbatim, so a screen reading them cannot drift a word. const GONE: &str = "this record is gone, so there is nothing left to put the old values onto — creating one would be a new record wearing an old record's name"; const MANAGED: &str = "an assistant keeps this note in step with its source, so old words put back here would be overwritten by the next sync"; const RECURRENCE: &str = "changing a repeat re-projects the open future todos it has already made, so putting an old rule back is a decision about those todos rather than an edit — change the rule on its own page instead"; #[test] fn the_refusals_are_the_typescripts_sentences_verbatim() { assert_eq!(said(restore_block(VersionRecordKind::Todo, None)), Some(GONE)); assert_eq!( block(VersionRecordKind::Note, json!({ "managed_by": "claude" })), Some(MANAGED) ); assert_eq!( block(VersionRecordKind::TodoRecurrence, json!({})), Some(RECURRENCE) ); } /// `if (current.managed_by)` is JavaScript truthiness, and it is not /// `is_empty`: an EMPTY ARRAY is truthy and refuses, while `0`, `false` and the /// empty string are falsy and allow. All five were read out of Node. #[test] fn managed_by_is_read_as_javascript_truthiness() { let vectors: &[(serde_json::Value, Option<&'static str>)] = &[ (json!({ "managed_by": "" }), None), (json!({ "managed_by": 0 }), None), (json!({ "managed_by": false }), None), (json!({ "managed_by": null }), None), (json!({}), None), (json!({ "managed_by": " " }), Some(MANAGED)), (json!({ "managed_by": [] }), Some(MANAGED)), (json!({ "managed_by": "claude" }), Some(MANAGED)), (json!({ "managed_by": true }), Some(MANAGED)), (json!({ "managed_by": 1 }), Some(MANAGED)), (json!({ "managed_by": {} }), Some(MANAGED)), ]; for (current, expected) in vectors { assert_eq!( block(VersionRecordKind::Note, current.clone()), *expected, "{current}" ); assert_eq!( block(VersionRecordKind::Journal, current.clone()), *expected, "{current}" ); } } /// The other kinds read no row column, and a live row of any of them /// restores. `memory` and `person` are the two the vectors probed. #[test] fn every_other_kind_answers_from_the_kind_alone() { for kind in crate::constants::VersionRecordKind::ALL { let refusal = said(crate::rules::history::fields::restore_refusal(kind)); assert_eq!(block(kind, json!({ "id": "x" })), refusal, "{kind}"); // A gone record is the gone sentence unless the KIND already refused — // the kind's answer is the stronger fact. assert_eq!( said(restore_block(kind, None)), Some(refusal.unwrap_or(GONE)), "{kind}" ); } assert_eq!( block( VersionRecordKind::Person, json!({ "id": "p1", "managed_by": "claude" }) ), None ); assert_eq!( block(VersionRecordKind::Memory, json!({ "id": "m1" })), None ); } /// RULE: a restore writes only the kind's restore columns that differ, and never turns a column the old snapshot did not hold into null. #[test] fn a_restore_writes_only_the_restorable_columns_that_differ() { let version = row(json!({ "title": "Buy milk", "notes": "two litres", "status": "open", "id": "t1" })); let current = row(json!({ "title": "Buy oat milk", "notes": "two litres", "status": "done", "due_at": "2026-10-07" })); let patch = restore_patch(VersionRecordKind::Todo, &version, ¤t); assert_eq!(patch.get("title"), Some(&json!("Buy milk"))); assert!(!patch.contains_key("notes"), "an unchanged column is not written"); assert!(!patch.contains_key("id"), "an identity column is never a restore column"); assert!(!patch.contains_key("due_at"), "a column the snapshot never held is left alone"); } #[test] fn a_kind_that_refuses_restore_has_nothing_to_put_back() { let version = row(json!({ "rrule": "FREQ=DAILY" })); let current = row(json!({ "rrule": "FREQ=WEEKLY" })); assert!(restore_patch(VersionRecordKind::TodoRecurrence, &version, ¤t).is_empty()); } #[test] fn a_version_s_lines_are_read_whether_stored_as_an_array_or_as_text() { let array = row(json!({ "lines": [{ "id": "l1", "account_id": "groceries" }, { "id": "l2" }] })); assert_eq!(version_lines(&array), [("l1".to_string(), "groceries".to_string())]); let text = row(json!({ "lines": "[{\"id\":\"l1\",\"account_id\":\"dining\"}]" })); assert_eq!(version_lines(&text), [("l1".to_string(), "dining".to_string())]); assert!(version_lines(&row(json!({}))).is_empty()); } /// RULE: a transaction's sides differ from a version only where a line still there now sits on another account — a version with no lines snapshot differs in nothing. #[test] fn lines_differ_only_where_a_line_still_there_moved() { let version = row(json!({ "lines": [{ "id": "l1", "account_id": "groceries" }, { "id": "l2", "account_id": "bank" }] })); let same = [("l1".to_string(), "groceries".to_string()), ("l2".to_string(), "bank".to_string())]; let moved = [("l1".to_string(), "dining".to_string()), ("l2".to_string(), "bank".to_string())]; assert!(!lines_differ(&version, &same)); assert!(lines_differ(&version, &moved)); assert!(!lines_differ(&row(json!({})), &moved)); } /// A rewording of a restore refusal is a row of the `refusal` set, never a /// build. #[test] fn a_refusal_is_the_databases_line() { let rows = [LineRow { set: "refusal".into(), line: "history.gone".into(), words: "Gone for good".into() }]; assert_eq!(restore_block(VersionRecordKind::Todo, None).map(|block| block.words(&rows)), Some("Gone for good".into())); }