Skip to main content

omgbase_surface/
history.rs

1//! History (`spec/surface/README.md` §3): `history_node`, the block-grain
2//! `diff`, the Myers unified `diff_unified` over the two revisions'
3//! reconstructed files (§3; positional before 1.1, §9), `docs_history`. Port
4//! of `packages/core/src/graph/history.ts`; `changes_since` is the store's.
5
6use std::collections::HashMap;
7
8use omgbase_format::hash::hex;
9use omgbase_store::tree::{from_hex, parse_tree_entries};
10use omgbase_store::{Store, is_id_ref};
11use rusqlite::{Connection, OptionalExtension, params};
12use serde_json::{Map, Value as Json, json};
13
14use crate::error::Result;
15use crate::read::find_doc_by_ref;
16
17/// §3 `history_node`: `[{ commitId, seq, ts, origin, kind, confidence, reason }]`,
18/// newest first.
19pub fn history_node(store: &Store, block_id: &str, limit: Option<i64>) -> Result<Json> {
20    let limit = limit.unwrap_or(100);
21    let mut stmt = store.conn().prepare(
22        "SELECT bc.commit_id, c.seq, c.ts, c.origin, bc.kind, d.confidence, d.reason
23         FROM block_changes bc
24         JOIN commits c ON c.commit_id = bc.commit_id
25         LEFT JOIN dispositions d ON d.commit_id = bc.commit_id AND d.block_id = bc.block_id AND d.kind = bc.kind
26         WHERE bc.block_id = ?1
27         ORDER BY c.seq DESC
28         LIMIT ?2",
29    )?;
30    let rows = stmt.query_map(params![block_id, limit], |r| {
31        Ok(json!({
32            "commitId": r.get::<_, String>(0)?,
33            "seq": r.get::<_, i64>(1)?,
34            "ts": r.get::<_, String>(2)?,
35            "origin": r.get::<_, String>(3)?,
36            "kind": r.get::<_, String>(4)?,
37            "confidence": r.get::<_, Option<f64>>(5)?,
38            "reason": r.get::<_, Option<String>>(6)?,
39        }))
40    })?;
41    Ok(Json::Array(
42        rows.collect::<std::result::Result<Vec<_>, _>>()?,
43    ))
44}
45
46/// §9: an unknown revision is `target_missing` (never an empty tree or an
47/// empty file). One constructor so `diff` and `diff_unified` report it with
48/// the same message and `data`.
49fn unknown_revision(doc_id: &str, rev_id: &str) -> crate::error::SurfaceError {
50    crate::error::SurfaceError::with_data(
51        "target_missing",
52        format!(
53            "no revision {} for document {doc_id}",
54            Json::String(rev_id.to_owned())
55        ),
56        json!({ "doc": doc_id, "rev": rev_id }),
57    )
58}
59
60/// The (block id → raw) map of a document at a revision, walking the Merkle
61/// tree to every depth (insertion order = tree order, roots first). The
62/// block grain of `diff`; not a rendering of the file (a container's raw
63/// already holds its children's).
64fn blocks_at_revision(
65    conn: &Connection,
66    doc_id: &str,
67    rev_id: &str,
68) -> Result<Vec<(String, String)>> {
69    match blocks_at_known_revision(conn, doc_id, rev_id)? {
70        Some(rows) => Ok(rows),
71        None => Err(unknown_revision(doc_id, rev_id)),
72    }
73}
74
75/// `None` when `rev_id` is not one of the doc's revisions (§9: an unknown
76/// revision is `target_missing`, not an empty tree).
77fn blocks_at_known_revision(
78    conn: &Connection,
79    doc_id: &str,
80    rev_id: &str,
81) -> Result<Option<Vec<(String, String)>>> {
82    let root: Option<Vec<u8>> = conn
83        .query_row(
84            "SELECT root_tree FROM revisions WHERE rev_id = ?1 AND doc_id = ?2",
85            params![rev_id, doc_id],
86            |r| r.get(0),
87        )
88        .optional()?;
89    let mut out: Vec<(String, String)> = Vec::new();
90    let Some(root) = root else {
91        return Ok(None);
92    };
93    fn walk(conn: &Connection, tree: &[u8], out: &mut Vec<(String, String)>) -> Result<()> {
94        let entries: Option<String> = conn
95            .query_row(
96                "SELECT entries FROM tree_nodes WHERE hash = ?1",
97                params![tree],
98                |r| r.get(0),
99            )
100            .optional()?;
101        let Some(text) = entries else {
102            return Ok(());
103        };
104        for e in parse_tree_entries(&text)? {
105            let raw = omgbase_store::read::blob_text(conn, &from_hex(&e.raw_hash_hex)?)?;
106            match out.iter_mut().find(|(id, _)| *id == e.block_id) {
107                Some(slot) => slot.1 = raw,
108                None => out.push((e.block_id.clone(), raw)),
109            }
110            if let Some(child) = &e.child_tree_hash_hex {
111                walk(conn, &from_hex(child)?, out)?;
112            }
113        }
114        Ok(())
115    }
116    walk(conn, &root, &mut out)?;
117    Ok(Some(out))
118}
119
120/// §3 `diff`: `removed`, `changed`, `added` entries in that order.
121pub fn diff_blocks(store: &Store, doc_id: &str, from_rev: &str, to_rev: &str) -> Result<Json> {
122    let before = blocks_at_revision(store.conn(), doc_id, from_rev)?;
123    let after = blocks_at_revision(store.conn(), doc_id, to_rev)?;
124    let after_map: HashMap<&str, &str> = after
125        .iter()
126        .map(|(k, v)| (k.as_str(), v.as_str()))
127        .collect();
128    let before_map: HashMap<&str, &str> = before
129        .iter()
130        .map(|(k, v)| (k.as_str(), v.as_str()))
131        .collect();
132    let mut entries = Vec::new();
133    for (id, raw) in &before {
134        match after_map.get(id.as_str()) {
135            None => entries.push(json!({ "kind": "removed", "blockId": id, "before": raw })),
136            Some(a) if *a != raw => {
137                entries
138                    .push(json!({ "kind": "changed", "blockId": id, "before": raw, "after": a }));
139            }
140            Some(_) => {}
141        }
142    }
143    for (id, raw) in &after {
144        if !before_map.contains_key(id.as_str()) {
145            entries.push(json!({ "kind": "added", "blockId": id, "after": raw }));
146        }
147    }
148    Ok(Json::Array(entries))
149}
150
151/// §3 `diff_unified`: a unified diff of the two revisions' reconstructed
152/// files — what `docs_read_at` returns (`spec/store` §6.2: leading trivia,
153/// the revision's frontmatter, then each top-level block's raw and trivia,
154/// children not walked since a container's raw already holds them) — via
155/// [`unified_diff`]. Diffing the block map instead (the rendering this
156/// replaced: every live raw at every depth joined by `\n`) repeated each
157/// list item once inside its container's raw and once as its own block, so
158/// an appended bullet showed up twice, in two hunks. An unknown revision is
159/// `target_missing` (§9), exactly as `diff` reports it.
160pub fn diff_unified_text(
161    store: &Store,
162    doc_id: &str,
163    from_rev: &str,
164    to_rev: &str,
165) -> Result<String> {
166    let file_at = |rev: &str| -> Result<String> {
167        store
168            .read_at_revision(doc_id, rev)?
169            .map(|r| r.content)
170            .ok_or_else(|| unknown_revision(doc_id, rev))
171    };
172    Ok(unified_diff(&file_at(from_rev)?, &file_at(to_rev)?))
173}
174
175// ---- unified diff (spec/surface §3) ------------------------------------------
176// A line-grain unified diff with a deterministic Myers script, so both engines
177// (this port and the reference `packages/core/src/graph/history.ts`) produce
178// the same bytes for the same two texts. Pure, so the unit tests pin it.
179
180/// How a text becomes lines: `split('\n')` exactly — no trimming, no dropping
181/// of a trailing empty element (a raw ending in `\n` yields one) — with a single
182/// special case: the empty text has *no* lines (an empty file is zero lines,
183/// not one empty line), so a diff from/to nothing is `@@ -0,0 +1,n @@`.
184pub fn diff_lines(text: &str) -> Vec<&str> {
185    if text.is_empty() {
186        Vec::new()
187    } else {
188        text.split('\n').collect()
189    }
190}
191
192const DIFF_CONTEXT: usize = 3;
193
194/// One step of the edit script: `Keep` consumes a line from both sides,
195/// `Delete` one from the old text, `Insert` one from the new text.
196#[derive(Debug, Clone, Copy, PartialEq, Eq)]
197pub enum EditOp<'a> {
198    Keep(&'a str),
199    Delete(&'a str),
200    Insert(&'a str),
201}
202
203/// Myers' O(ND) shortest edit script (forward, with a per-`d` trace for the
204/// backtrack). `V[k]` is the furthest x on diagonal `k = x - y` reachable with
205/// `d` edits. The canonical tie rule, identical in both engines: at each step
206/// take the diagonal from `k+1` (moving down — an insertion of `b[y]`) when
207/// `k == -d || (k != d && V[k-1] < V[k+1])`, else from `k-1` (moving right —
208/// a deletion of `a[x]`). On a tie (`V[k-1] == V[k+1]`) that is the deletion.
209pub fn myers_script<'a>(a: &[&'a str], b: &[&'a str]) -> Vec<EditOp<'a>> {
210    let n = a.len();
211    let m = b.len();
212    let max = n + m;
213    // V is indexed by k ∈ [-max-1, max+1]; `off` maps it onto a plain vector.
214    let off = max + 1;
215    let at = |k: isize| -> usize { (off as isize + k) as usize };
216    let mut v = vec![0usize; 2 * max + 3];
217    let mut trace: Vec<Vec<usize>> = Vec::new();
218    let mut found = false;
219    let mut d: isize = 0;
220    while d <= max as isize && !found {
221        trace.push(v.clone());
222        let mut k = -d;
223        while k <= d {
224            let mut x = if k == -d || (k != d && v[at(k - 1)] < v[at(k + 1)]) {
225                v[at(k + 1)]
226            } else {
227                v[at(k - 1)] + 1
228            };
229            let mut y = (x as isize - k) as usize;
230            while x < n && y < m && a[x] == b[y] {
231                x += 1;
232                y += 1;
233            }
234            v[at(k)] = x;
235            if x >= n && y >= m {
236                found = true;
237                break;
238            }
239            k += 2;
240        }
241        d += 1;
242    }
243    // Backtrack from (n, m) through the trace, emitting ops newest-first.
244    let mut ops: Vec<EditOp<'a>> = Vec::new();
245    let mut x = n;
246    let mut y = m;
247    for (d, vd) in trace.iter().enumerate().rev() {
248        let d = d as isize;
249        let k = x as isize - y as isize;
250        let prev_k = if k == -d || (k != d && vd[at(k - 1)] < vd[at(k + 1)]) {
251            k + 1
252        } else {
253            k - 1
254        };
255        let prev_x = vd[at(prev_k)];
256        let prev_y = prev_x as isize - prev_k;
257        while x > prev_x && y as isize > prev_y {
258            x -= 1;
259            y -= 1;
260            ops.push(EditOp::Keep(a[x]));
261        }
262        if d > 0 {
263            if x == prev_x {
264                ops.push(EditOp::Insert(b[prev_y as usize]));
265            } else {
266                ops.push(EditOp::Delete(a[prev_x]));
267            }
268        }
269        x = prev_x;
270        y = prev_y as usize;
271    }
272    ops.reverse();
273    ops
274}
275
276/// The unified diff of two texts (spec/surface §3): hunks of `DIFF_CONTEXT`
277/// (3) lines of context; a change group extends to include the next change
278/// when fewer than `2 * DIFF_CONTEXT + 1` unchanged lines separate them (the
279/// two contexts touch or overlap). Each hunk is `@@ -a,b +c,d @@` (1-based
280/// start and length; a length of 1 is written as the start alone; a length of
281/// 0 as `a,0` with `a` the line before the insertion point, `0` at the very
282/// top) followed by its lines prefixed `-`, `+` or a space with nothing after
283/// the sign; hunks joined by `\n`; no file header; identical texts → `""`.
284pub fn unified_diff(old_text: &str, new_text: &str) -> String {
285    let ops = myers_script(&diff_lines(old_text), &diff_lines(new_text));
286    // Old/new line counts consumed before each op (0-based positions).
287    let mut old_pos = Vec::with_capacity(ops.len() + 1);
288    let mut new_pos = Vec::with_capacity(ops.len() + 1);
289    let (mut o, mut nn) = (0usize, 0usize);
290    for op in &ops {
291        old_pos.push(o);
292        new_pos.push(nn);
293        if !matches!(op, EditOp::Insert(_)) {
294            o += 1;
295        }
296        if !matches!(op, EditOp::Delete(_)) {
297            nn += 1;
298        }
299    }
300    old_pos.push(o);
301    new_pos.push(nn);
302
303    let changes: Vec<usize> = (0..ops.len())
304        .filter(|&i| !matches!(ops[i], EditOp::Keep(_)))
305        .collect();
306    if changes.is_empty() {
307        return String::new();
308    }
309
310    let range = |pos: usize, len: usize| -> String {
311        let start = if len == 0 { pos } else { pos + 1 };
312        if len == 1 {
313            start.to_string()
314        } else {
315            format!("{start},{len}")
316        }
317    };
318    let mut hunks: Vec<String> = Vec::new();
319    let mut g = 0;
320    while g < changes.len() {
321        let first = changes[g];
322        let mut last = first;
323        // Merge rule: the next change joins this hunk iff the unchanged lines
324        // between them number at most 2 * DIFF_CONTEXT.
325        while g + 1 < changes.len() && changes[g + 1] - last - 1 <= 2 * DIFF_CONTEXT {
326            g += 1;
327            last = changes[g];
328        }
329        g += 1;
330        let start = first.saturating_sub(DIFF_CONTEXT);
331        let end = (last + DIFF_CONTEXT).min(ops.len() - 1);
332        let old_len = old_pos[end + 1] - old_pos[start];
333        let new_len = new_pos[end + 1] - new_pos[start];
334        let mut lines = vec![format!(
335            "@@ -{} +{} @@",
336            range(old_pos[start], old_len),
337            range(new_pos[start], new_len)
338        )];
339        for op in &ops[start..=end] {
340            lines.push(match op {
341                EditOp::Keep(l) => format!(" {l}"),
342                EditOp::Delete(l) => format!("-{l}"),
343                EditOp::Insert(l) => format!("+{l}"),
344            });
345        }
346        hunks.push(lines.join("\n"));
347    }
348    hunks.join("\n")
349}
350
351/// The two most recent revisions of a doc, newest first.
352pub fn recent_revs(conn: &Connection, doc_id: &str) -> Result<Vec<String>> {
353    let mut stmt =
354        conn.prepare("SELECT rev_id FROM revisions WHERE doc_id = ?1 ORDER BY seq DESC LIMIT 2")?;
355    let rows = stmt.query_map(params![doc_id], |r| r.get::<_, String>(0))?;
356    Ok(rows.collect::<std::result::Result<Vec<_>, _>>()?)
357}
358
359/// A `docs` row: `(doc_id, path, current_rev, deleted_commit)`.
360pub type DocRow = (String, String, Option<String>, Option<String>);
361
362/// The `docs` row of a ref, looking through a tombstone when `include_deleted`.
363pub fn resolve_doc_row(
364    conn: &Connection,
365    repo_id: &str,
366    r: &str,
367    include_deleted: bool,
368) -> Result<Option<DocRow>> {
369    type Row = DocRow;
370    let by_id = |id: &str| -> Result<Option<Row>> {
371        Ok(conn
372            .query_row(
373                "SELECT doc_id, path, current_rev, deleted_commit FROM docs WHERE doc_id = ?1",
374                params![id],
375                |row| Ok((row.get(0)?, row.get(1)?, row.get(2)?, row.get(3)?)),
376            )
377            .optional()?)
378    };
379    if let Some(info) = find_doc_by_ref(conn, repo_id, r)? {
380        return by_id(&info.doc_id);
381    }
382    if !include_deleted {
383        return Ok(None);
384    }
385    if is_id_ref(r, "d") {
386        return by_id(r);
387    }
388    Ok(conn
389        .query_row(
390            "SELECT doc_id, path, current_rev, deleted_commit FROM docs WHERE repo_id = ?1 AND path = ?2",
391            params![repo_id, r],
392            |row| Ok((row.get(0)?, row.get(1)?, row.get(2)?, row.get(3)?)),
393        )
394        .optional()?)
395}
396
397/// §3 `docs_history`: `{ docs: [{ docId, path, deleted, currentRev, versions }], truncated }`.
398pub fn docs_history(
399    store: &Store,
400    repo_id: &str,
401    path_glob: Option<&str>,
402    doc: Option<&str>,
403    include_deleted: bool,
404    limit: Option<i64>,
405) -> Result<Json> {
406    let conn = store.conn();
407    let limit = usize::try_from(limit.unwrap_or(50).max(0)).unwrap_or(0);
408    type Row = (String, String, Option<String>, Option<String>);
409    let mut doc_rows: Vec<Row> = if let Some(d) = doc {
410        resolve_doc_row(conn, repo_id, d, include_deleted)?
411            .into_iter()
412            .collect()
413    } else if let Some(glob) = path_glob {
414        let deleted_clause = if include_deleted {
415            ""
416        } else {
417            "AND deleted_commit IS NULL"
418        };
419        let (path_clause, param) = if glob.contains('*') {
420            (
421                "path LIKE ?2 ESCAPE '\\'",
422                crate::context::glob_to_like(glob, false),
423            )
424        } else {
425            ("path = ?2", glob.to_owned())
426        };
427        let sql = format!(
428            "SELECT doc_id, path, current_rev, deleted_commit FROM docs WHERE repo_id = ?1 AND {path_clause} {deleted_clause} ORDER BY path LIMIT ?3"
429        );
430        let mut stmt = conn.prepare(&sql)?;
431        let rows = stmt.query_map(
432            params![
433                repo_id,
434                param,
435                i64::try_from(limit).unwrap_or(i64::MAX).saturating_add(1)
436            ],
437            |r| Ok((r.get(0)?, r.get(1)?, r.get(2)?, r.get(3)?)),
438        )?;
439        rows.collect::<std::result::Result<Vec<_>, _>>()?
440    } else {
441        return Err(crate::error::SurfaceError::other(
442            "docHistory requires one of { doc, pathGlob }",
443        ));
444    };
445    let truncated = doc_rows.len() > limit;
446    doc_rows.truncate(limit);
447    let mut rev_stmt = conn.prepare(
448        "SELECT r.rev_id, r.seq, r.commit_id, r.rendered_hash, c.ts, c.origin, c.actor
449         FROM revisions r JOIN commits c ON c.commit_id = r.commit_id
450         WHERE r.doc_id = ?1 ORDER BY r.seq ASC",
451    )?;
452    let mut docs = Vec::new();
453    for (doc_id, path, current_rev, deleted_commit) in doc_rows {
454        let versions: Vec<Json> = rev_stmt
455            .query_map(params![doc_id], |r| {
456                let rev: String = r.get(0)?;
457                let hash: Vec<u8> = r.get(3)?;
458                Ok(json!({
459                    "rev": rev,
460                    "seq": r.get::<_, i64>(1)?,
461                    "commit": r.get::<_, String>(2)?,
462                    "ts": r.get::<_, String>(4)?,
463                    "origin": r.get::<_, String>(5)?,
464                    "actor": r.get::<_, Option<String>>(6)?,
465                    "contentHash": hex(&hash),
466                    "isCurrent": current_rev.as_deref() == Some(rev.as_str()),
467                }))
468            })?
469            .collect::<std::result::Result<_, _>>()?;
470        let mut m = Map::new();
471        m.insert("docId".to_owned(), json!(doc_id));
472        m.insert("path".to_owned(), json!(path));
473        m.insert("deleted".to_owned(), json!(deleted_commit.is_some()));
474        m.insert("currentRev".to_owned(), json!(current_rev));
475        m.insert("versions".to_owned(), Json::Array(versions));
476        docs.push(Json::Object(m));
477    }
478    Ok(json!({ "docs": docs, "truncated": truncated }))
479}
480
481// spec/surface §3 `diff_unified`. The expected strings below are copied
482// verbatim from packages/core/src/graph/history.test.ts so the two engines pin
483// each other byte for byte.
484#[cfg(test)]
485mod tests {
486    use super::*;
487
488    const EIGHT: &str = "l1\nl2\nl3\nl4\nl5\nl6\nl7\nl8";
489    const TWELVE: &str = "l1\nl2\nl3\nl4\nl5\nl6\nl7\nl8\nl9\nl10\nl11\nl12";
490
491    #[test]
492    fn splits_on_newline_exactly_and_the_empty_text_has_no_lines() {
493        assert_eq!(diff_lines("a\nb"), vec!["a", "b"]);
494        assert_eq!(diff_lines("a\nb\n"), vec!["a", "b", ""]);
495        assert_eq!(diff_lines(" a \n\n b "), vec![" a ", "", " b "]);
496        assert_eq!(diff_lines(""), Vec::<&str>::new());
497    }
498
499    #[test]
500    fn identical_texts_are_empty() {
501        assert_eq!(unified_diff("a\nb\nc", "a\nb\nc"), "");
502        assert_eq!(unified_diff("", ""), "");
503    }
504
505    #[test]
506    fn insertion_in_the_middle_is_one_plus_line() {
507        assert_eq!(
508            unified_diff(EIGHT, "l1\nl2\nl3\nl4\nNEW\nl5\nl6\nl7\nl8"),
509            "@@ -2,6 +2,7 @@\n l2\n l3\n l4\n+NEW\n l5\n l6\n l7"
510        );
511    }
512
513    #[test]
514    fn deletion() {
515        assert_eq!(
516            unified_diff(EIGHT, "l1\nl2\nl3\nl4\nl6\nl7\nl8"),
517            "@@ -2,7 +2,6 @@\n l2\n l3\n l4\n-l5\n l6\n l7\n l8"
518        );
519    }
520
521    #[test]
522    fn replacement() {
523        assert_eq!(
524            unified_diff(EIGHT, "l1\nl2\nl3\nl4\nX5\nl6\nl7\nl8"),
525            "@@ -2,7 +2,7 @@\n l2\n l3\n l4\n-l5\n+X5\n l6\n l7\n l8"
526        );
527    }
528
529    #[test]
530    fn change_at_top_and_bottom_truncates_context() {
531        assert_eq!(
532            unified_diff("l1\nl2\nl3\nl4\nl5", "L1\nl2\nl3\nl4\nl5"),
533            "@@ -1,4 +1,4 @@\n-l1\n+L1\n l2\n l3\n l4"
534        );
535        assert_eq!(
536            unified_diff("l1\nl2\nl3\nl4\nl5", "l1\nl2\nl3\nl4\nL5"),
537            "@@ -2,4 +2,4 @@\n l2\n l3\n l4\n-l5\n+L5"
538        );
539    }
540
541    #[test]
542    fn far_changes_are_two_hunks_near_changes_share_one() {
543        assert_eq!(
544            unified_diff(TWELVE, "L1\nl2\nl3\nl4\nl5\nl6\nl7\nl8\nl9\nl10\nl11\nL12"),
545            "@@ -1,4 +1,4 @@\n-l1\n+L1\n l2\n l3\n l4\n@@ -9,4 +9,4 @@\n l9\n l10\n l11\n-l12\n+L12"
546        );
547        assert_eq!(
548            unified_diff(TWELVE, "L1\nl2\nl3\nl4\nl5\nl6\nl7\nl8\nL9\nl10\nl11\nl12"),
549            "@@ -1,4 +1,4 @@\n-l1\n+L1\n l2\n l3\n l4\n@@ -6,7 +6,7 @@\n l6\n l7\n l8\n-l9\n+L9\n l10\n l11\n l12"
550        );
551    }
552
553    #[test]
554    fn hunk_merge_boundary_six_between_merges_seven_splits() {
555        assert_eq!(
556            unified_diff(
557                "l1\nl2\nl3\nl4\nl5\nl6\nl7\nl8\nl9\nl10",
558                "L1\nl2\nl3\nl4\nl5\nl6\nl7\nL8\nl9\nl10"
559            ),
560            "@@ -1,10 +1,10 @@\n-l1\n+L1\n l2\n l3\n l4\n l5\n l6\n l7\n-l8\n+L8\n l9\n l10"
561        );
562        assert_eq!(
563            unified_diff(
564                "l1\nl2\nl3\nl4\nl5\nl6\nl7\nl8\nl9\nl10\nl11",
565                "L1\nl2\nl3\nl4\nl5\nl6\nl7\nl8\nL9\nl10\nl11"
566            ),
567            "@@ -1,4 +1,4 @@\n-l1\n+L1\n l2\n l3\n l4\n@@ -6,6 +6,6 @@\n l6\n l7\n l8\n-l9\n+L9\n l10\n l11"
568        );
569    }
570
571    #[test]
572    fn empty_old_and_empty_new_texts() {
573        assert_eq!(unified_diff("", "a\nb\nc"), "@@ -0,0 +1,3 @@\n+a\n+b\n+c");
574        assert_eq!(unified_diff("a\nb\nc", ""), "@@ -1,3 +0,0 @@\n-a\n-b\n-c");
575    }
576
577    #[test]
578    fn insertion_at_the_very_top() {
579        assert_eq!(
580            unified_diff("a\nb", "z\na\nb"),
581            "@@ -1,2 +1,3 @@\n+z\n a\n b"
582        );
583    }
584
585    #[test]
586    fn trailing_newline_and_empty_lines_are_lines() {
587        assert_eq!(unified_diff("a\nb\n", "a\nb"), "@@ -1,3 +1,2 @@\n a\n b\n-");
588        assert_eq!(
589            unified_diff("a\n\nb", "a\n\n\nb"),
590            "@@ -1,3 +1,4 @@\n a\n \n+\n b"
591        );
592    }
593
594    // ---- diff_unified over a store --------------------------------------------
595    // The bug this pins: a list container's raw already carries every item, so
596    // diffing "every live raw at every depth" showed an appended bullet twice
597    // (at the end of the container's raw, reading as an insertion before the
598    // first item, then again after the last item block). The diff must be over
599    // the reconstructed file, exactly what `docs_read_at` returns.
600
601    use omgbase_reconcile::Config;
602    use omgbase_store::SequentialMinter;
603
604    const REV1: &str = "# Notes\n\n## Log\n\n- first\n\n- second\n";
605    const REV2: &str = "# Notes\n\n## Log\n\n- first\n\n- second\n\n- third\n- fourth\n";
606    const REV3: &str = "# Notes\n\n## Log\n\n- first\n\n- second\n\n- third\n- fourth\n- fifth\n";
607
608    /// A store with `log.md` observed three times (a heading over a loose
609    /// list, then a tight pair appended, then one more bullet); returns the
610    /// store, the doc id and the three rev ids in order.
611    fn log_doc_store() -> (Store, String, Vec<String>) {
612        let mut store =
613            Store::open_in_memory_with_minter(Box::new(SequentialMinter::new())).unwrap();
614        let repo = store.create_repo("fixture").unwrap();
615        let mut revs = Vec::new();
616        let mut doc_id = String::new();
617        for (i, source) in [REV1, REV2, REV3].iter().enumerate() {
618            let ts = format!("2026-09-30T10:0{i}:00.000Z");
619            let out = store
620                .observe_one(&repo, "log.md", source, &ts, &Config::default())
621                .unwrap();
622            assert!(!out.echo, "revision {i} must commit");
623            doc_id = out.doc_id;
624            revs.push(out.rev.expect("a commit has a rev"));
625        }
626        (store, doc_id, revs)
627    }
628
629    #[test]
630    fn diff_unified_is_over_the_reconstructed_file_not_the_block_map() {
631        let (store, doc_id, revs) = log_doc_store();
632        // The two sides are byte for byte what `docs_read_at` returns.
633        for (rev, source) in revs.iter().zip([REV1, REV2, REV3]) {
634            let read = store.read_at_revision(&doc_id, rev).unwrap().unwrap();
635            assert_eq!(read.content, source);
636            assert!(read.rendered_hash_match);
637        }
638        let diff = diff_unified_text(&store, &doc_id, &revs[1], &revs[2]).unwrap();
639        assert_eq!(diff, unified_diff(REV2, REV3));
640        // Exactly one hunk, one `+` line, no `-` lines, the bullet once and
641        // after the previous last item.
642        assert_eq!(diff.matches("@@").count(), 2, "one hunk header: {diff}");
643        let lines: Vec<&str> = diff.lines().collect();
644        let plus: Vec<&str> = lines
645            .iter()
646            .copied()
647            .filter(|l| l.starts_with('+'))
648            .collect();
649        assert_eq!(plus, vec!["+- fifth"], "{diff}");
650        assert!(!lines.iter().any(|l| l.starts_with('-')), "{diff}");
651        assert_eq!(diff.matches("- fifth").count(), 1, "{diff}");
652        let fourth = lines.iter().position(|l| *l == " - fourth").unwrap();
653        let fifth = lines.iter().position(|l| *l == "+- fifth").unwrap();
654        assert_eq!(fifth, fourth + 1, "{diff}");
655        assert_eq!(diff, "@@ -8,4 +8,5 @@\n \n - third\n - fourth\n+- fifth\n ");
656        // The first append (a tight pair onto a loose list) reads the same way.
657        let first = diff_unified_text(&store, &doc_id, &revs[0], &revs[1]).unwrap();
658        assert_eq!(first, unified_diff(REV1, REV2));
659        assert_eq!(first.matches("- third").count(), 1, "{first}");
660        assert_eq!(first.matches("- fourth").count(), 1, "{first}");
661        assert_eq!(first.matches("@@").count(), 2, "one hunk header: {first}");
662        // Same revision on both sides: nothing.
663        assert_eq!(
664            diff_unified_text(&store, &doc_id, &revs[2], &revs[2]).unwrap(),
665            ""
666        );
667    }
668
669    #[test]
670    fn diff_unified_unknown_revision_is_target_missing_like_diff() {
671        let (store, doc_id, revs) = log_doc_store();
672        let err = diff_unified_text(&store, &doc_id, &revs[0], "rv_nope").unwrap_err();
673        assert_eq!(err.code, "target_missing");
674        assert_eq!(
675            err.message,
676            format!("no revision \"rv_nope\" for document {doc_id}")
677        );
678        assert_eq!(err.data, Some(json!({ "doc": doc_id, "rev": "rv_nope" })));
679        // Byte-identical to what the block-grain `diff` reports.
680        let block_err = diff_blocks(&store, &doc_id, &revs[0], "rv_nope").unwrap_err();
681        assert_eq!(err, block_err);
682    }
683
684    #[test]
685    fn textbook_tie_case() {
686        let script: Vec<String> = myers_script(
687            &diff_lines("a\nb\nc\na\nb\nb\na"),
688            &diff_lines("c\nb\na\nb\na\nc"),
689        )
690        .into_iter()
691        .map(|op| match op {
692            EditOp::Keep(l) => format!(" {l}"),
693            EditOp::Delete(l) => format!("-{l}"),
694            EditOp::Insert(l) => format!("+{l}"),
695        })
696        .collect();
697        assert_eq!(script.join("|"), "-a|-b| c|+b| a| b|-b| a|+c");
698        assert_eq!(
699            unified_diff("a\nb\nc\na\nb\nb\na", "c\nb\na\nb\na\nc"),
700            "@@ -1,7 +1,6 @@\n-a\n-b\n c\n+b\n a\n b\n-b\n a\n+c"
701        );
702    }
703}