270 lines · 9.6 KB · RustRaw
//! Pairing a trail into readable edits.//!//! The subtlety this exists for: a version row holds the state BEFORE the edit//! that dated it, so "what this edit changed" is that row read against the NEXT//! NEWER one — and, for the newest row, against the record as it stands now.//! Getting that off by one puts every change under the wrong timestamp and the//! wrong name, which is worse than showing nothing.use serde_json::json;use super::*;use crate::rules::history::diff::FieldChangeKind;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}"), }}fn version(id: &str, created_at: &str, data: serde_json::Value, op: VersionOp) -> RecordVersion { RecordVersion { id: id.to_string(), workspace_id: "ws-1".to_string(), record_kind: VersionRecordKind::Todo, record_id: "t-1".to_string(), op, data: row(data), actor_role: "local".to_string(), created_at: created_at.to_string(), }}fn updated(id: &str, created_at: &str, data: serde_json::Value) -> RecordVersion { version(id, created_at, data, VersionOp::Update)}// W12-HISTORY-PIN-01: Pin and its inverse leave real history even when the// note's words stay the same. Both directions must survive the readable diff.#[test]fn note_pin_and_undo_are_visible_history_changes() { let mut versions = vec![ updated( "fern-unpin", "2026-09-02T11:00:00.000Z", json!({ "body": "Water the fern", "pinned": 1 }), ), updated( "fern-pin", "2026-09-02T10:00:00.000Z", json!({ "body": "Water the fern", "pinned": 0 }), ), ]; for version in &mut versions { version.record_kind = VersionRecordKind::Note; version.record_id = "fern-note".to_string(); } let current = row(json!({ "body": "Water the fern", "pinned": 0 })); let entries = history_entries(VersionRecordKind::Note, &versions, Some(¤t)); for (entry, (before, after)) in entries.iter().zip([(1, 0), (0, 1)]) { assert_eq!( entry.changes, vec![FieldChange { column: "pinned".to_string(), change: FieldChangeKind::Changed, before: json!(before), after: json!(after), }] ); } assert_eq!(entries.len(), 2);}// Generic write snapshots carry booleans; the live SQLite row carries 0/1.// A word edit must not invent a Pin transition just because those forms differ.#[test]fn note_history_compares_flags_by_value_across_snapshot_forms() { for (before, after) in [ (json!(false), json!(0)), (json!(true), json!(1)), (json!(0), json!(false)), (json!(1), json!(true)), ] { let mut version = updated( "fern-edit", "2026-09-02T10:00:00.000Z", json!({ "body": "Water the fern", "pinned": before }), ); version.record_kind = VersionRecordKind::Note; let versions = [version]; let current = row(json!({ "body": "Water the fern gently", "pinned": after })); let entries = history_entries(VersionRecordKind::Note, &versions, Some(¤t)); assert_eq!(entries[0].changes.len(), 1, "only the words changed"); assert_eq!(entries[0].changes[0].column, "body"); }}#[test]fn reads_the_newest_version_against_the_record_as_it_stands_now() { let versions = vec![updated( "v1", "2026-09-02T10:00:00.000Z", json!({ "title": "Ping Ravi" }), )]; let current = row(json!({ "title": "Ping Ravi tomorrow" })); let entries = history_entries(VersionRecordKind::Todo, &versions, Some(¤t)); assert_eq!(entries.len(), 1); assert_eq!( entries[0].changes, vec![FieldChange { column: "title".to_string(), change: FieldChangeKind::Changed, before: json!("Ping Ravi"), after: json!("Ping Ravi tomorrow"), }] );}#[test]fn reads_an_older_version_against_the_version_above_it_not_the_live_row() { let versions = vec![ updated("v2", "2026-09-02T11:00:00.000Z", json!({ "title": "b" })), updated("v1", "2026-09-02T10:00:00.000Z", json!({ "title": "a" })), ]; let current = row(json!({ "title": "c" })); let entries = history_entries(VersionRecordKind::Todo, &versions, Some(¤t)); assert_eq!( entries .iter() .map(|e| e.version.id.as_str()) .collect::<Vec<_>>(), ["v2", "v1"] ); assert_eq!(entries[0].changes[0].before, json!("b")); assert_eq!(entries[0].changes[0].after, json!("c")); assert_eq!(entries[1].changes[0].before, json!("a")); assert_eq!(entries[1].changes[0].after, json!("b"));}#[test]fn reads_a_delete_as_everything_the_record_had_going_away() { let versions = vec![version( "v1", "2026-09-02T10:00:00.000Z", json!({ "title": "Ping Ravi" }), VersionOp::Delete, )]; let entries = history_entries(VersionRecordKind::Todo, &versions, None); assert!(entries[0].after.is_none()); assert_eq!( entries[0].changes, vec![FieldChange { column: "title".to_string(), change: FieldChangeKind::Removed, before: json!("Ping Ravi"), after: json!(null), }] );}#[test]fn still_pairs_the_versions_under_a_deleted_records_delete_row() { let versions = vec![ version( "v2", "2026-09-02T11:00:00.000Z", json!({ "title": "b" }), VersionOp::Delete, ), updated("v1", "2026-09-02T10:00:00.000Z", json!({ "title": "a" })), ]; let entries = history_entries(VersionRecordKind::Todo, &versions, None); assert_eq!(entries[1].changes[0].before, json!("a")); assert_eq!(entries[1].changes[0].after, json!("b"));}#[test]fn reports_no_changes_when_an_edit_only_touched_columns_history_does_not_draw() { let versions = vec![updated( "v1", "2026-09-02T10:00:00.000Z", json!({ "title": "same" }), )]; let current = row(json!({ "title": "same" })); let entries = history_entries(VersionRecordKind::Todo, &versions, Some(¤t)); assert_eq!(entries[0].changes, vec![]);}#[test]fn returns_nothing_for_an_empty_trail() { let current = row(json!({ "title": "a" })); assert!(history_entries(VersionRecordKind::Todo, &[], Some(¤t)).is_empty());}#[test]fn carries_the_actor_token_beside_each_entry() { let mut versions = vec![ updated("v2", "2026-09-02T11:00:00.000Z", json!({ "title": "b" })), updated("v1", "2026-09-02T10:00:00.000Z", json!({ "title": "a" })), ]; versions[0].actor_role = "agent".to_string(); let current = row(json!({ "title": "c" })); let entries = history_entries(VersionRecordKind::Todo, &versions, Some(¤t)); assert_eq!(entries[0].actor, VersionActor::Assistant); assert_eq!(entries[1].actor, VersionActor::Direct);}// ── Rust-side guards the TypeScript gets from its type system ───────────────/// The wire shape of a version row, so a server handing one to this crate and/// a phone reading the same JSON cannot disagree about a key.#[test]fn a_version_row_round_trips_through_the_typescripts_camel_case_json() { let wire = r#"{"id":"v1","workspaceId":"ws-1","recordKind":"note","recordId":"n-1","op":"delete","data":{"title":"Kitchen"},"actorRole":"agent","createdAt":"2026-09-02T10:00:00.000Z"}"#; let parsed: RecordVersion = serde_json::from_str(wire).expect("a version row parses"); assert_eq!(parsed.record_kind, VersionRecordKind::Note); assert_eq!(parsed.op, VersionOp::Delete); assert_eq!( serde_json::to_string(&parsed).expect("a version row serialises"), wire );}/// The columns drawn are the CALLER's kind, not the row's — `journal` and/// `note` are one table read two ways, which is exactly why the TypeScript/// takes them as two arguments.#[test]fn the_kind_argument_decides_the_columns_not_the_rows_own_kind() { let versions = vec![RecordVersion { record_kind: VersionRecordKind::Note, data: row(json!({ "body": "old", "archived_at": null, "journal_date": "2026-09-01" })), ..updated("v1", "2026-09-02T10:00:00.000Z", json!({})) }]; let current = row(json!({ "body": "new", "archived_at": null, "journal_date": "2026-09-02" })); let as_note = history_entries(VersionRecordKind::Note, &versions, Some(¤t)); assert_eq!( as_note[0] .changes .iter() .map(|c| c.column.as_str()) .collect::<Vec<_>>(), ["body"] ); let as_journal = history_entries(VersionRecordKind::Journal, &versions, Some(¤t)); assert_eq!( as_journal[0] .changes .iter() .map(|c| c.column.as_str()) .collect::<Vec<_>>(), ["body", "journal_date"] );}/// An `after` of `None` on the newest row is a delete; an `after` of `None`/// because the record is gone is the caller passing `None` for `current`. The/// two coincide here, and every column reads as removed either way.#[test]fn a_live_record_that_is_gone_reads_the_newest_edit_against_nothing() { let versions = vec![updated( "v1", "2026-09-02T10:00:00.000Z", json!({ "title": "Ping Ravi" }), )]; let entries = history_entries(VersionRecordKind::Todo, &versions, None); assert!(entries[0].after.is_none()); assert_eq!(entries[0].changes[0].change, FieldChangeKind::Removed);}