import { describe, it, expect } from "vitest"; import { splitBlocks, diffBlocks, renderTrackChanges } from "../src/trackChangesModel"; describe("splitBlocks", () => { it("splits prose paragraphs on blank lines, dropping empties", () => { const blocks = splitBlocks("# Heading\n\nFirst para.\n\nSecond para.\n"); expect(blocks.map((b) => b.type)).toEqual(["prose", "prose", "prose"]); expect(blocks[0].raw).toBe("# Heading"); expect(blocks[2].raw).toBe("Second para."); }); it("keeps a fenced code block whole and tags it `code`", () => { const text = "Intro.\n\n```ts\nconst a = 1;\n\nconst b = 2;\n```\n\nOutro.\n"; const blocks = splitBlocks(text); expect(blocks.map((b) => b.type)).toEqual(["prose", "code", "prose"]); expect(blocks[1].raw).toBe("```ts\nconst a = 1;\n\nconst b = 2;\n```"); }); it("tags a mermaid fence `mermaid`", () => { const blocks = splitBlocks("```mermaid\nflowchart LR\n a-->b\n```\n"); expect(blocks).toHaveLength(1); expect(blocks[0].type).toBe("mermaid"); }); it("normalizes the key (whitespace-collapsed) for matching", () => { const blocks = splitBlocks("Hello world\n"); expect(blocks[0].key).toBe("hello world"); }); }); describe("diffBlocks", () => { const kinds = (base: string, cur: string) => diffBlocks(base, cur).map((o) => o.kind); it("unchanged doc → all unchanged", () => { const doc = "Alpha.\n\nBravo.\n"; expect(kinds(doc, doc)).toEqual(["unchanged", "unchanged"]); }); it("a pure addition → an added op", () => { const ops = diffBlocks("Alpha.\n", "Alpha.\n\nBravo.\n"); expect(ops.map((o) => o.kind)).toEqual(["unchanged", "added"]); }); it("a pure deletion → a removed op", () => { const ops = diffBlocks("Alpha.\n\nBravo.\n", "Alpha.\n"); expect(ops.map((o) => o.kind)).toEqual(["unchanged", "removed"]); }); it("a prose modification → a non-atomic changed op", () => { const ops = diffBlocks("The quick fox.\n", "The slow fox.\n"); expect(ops).toHaveLength(1); expect(ops[0].kind).toBe("changed"); expect(ops[0].kind === "changed" && ops[0].atomic).toBe(false); }); it("a code-fence change → an ATOMIC changed op (INV-23)", () => { const base = "```ts\nconst a = 1;\n```\n"; const cur = "```ts\nconst a = 2;\n```\n"; const ops = diffBlocks(base, cur); expect(ops).toHaveLength(1); expect(ops[0].kind).toBe("changed"); expect(ops[0].kind === "changed" && ops[0].atomic).toBe(true); }); it("a mermaid-fence change → an ATOMIC changed op", () => { const base = "```mermaid\nflowchart LR\n a-->b\n```\n"; const cur = "```mermaid\nflowchart LR\n a-->c\n```\n"; const ops = diffBlocks(base, cur); expect(ops[0].kind).toBe("changed"); expect(ops[0].kind === "changed" && ops[0].atomic).toBe(true); }); it("a reorder → keeps a matched block unchanged", () => { const ops = diffBlocks("One.\n\nTwo.\n", "Two.\n\nOne.\n"); const k = ops.map((o) => o.kind); expect(k).toContain("unchanged"); }); }); describe("renderTrackChanges", () => { it("wraps each block in a cw-blk div with its kind class", () => { const html = renderTrackChanges("Alpha.\n", "Alpha.\n\nBravo.\n"); expect(html).toContain('class="cw-blk cw-unchanged"'); expect(html).toContain('class="cw-blk cw-added"'); }); it("emits inline / for a prose modification", () => { const html = renderTrackChanges("The quick fox.\n", "The slow fox.\n"); expect(html).toContain(""); expect(html).toContain(""); expect(html).toContain("cw-changed"); }); it("renders a changed CODE fence atomically: cw-changed, no inline ins/del", () => { const html = renderTrackChanges("```ts\nconst a = 1;\n```\n", "```ts\nconst a = 2;\n```\n"); expect(html).toContain("cw-changed"); expect(html).not.toContain(""); expect(html).not.toContain(""); expect(html).toContain("cw-badge"); }); it('emits
 for a mermaid fence + a changed badge', () => {
    const html = renderTrackChanges(
      "```mermaid\nflowchart LR\n a-->b\n```\n",
      "```mermaid\nflowchart LR\n a-->c\n```\n",
    );
    expect(html).toContain('
');
    expect(html).toContain("a-->c"); // current source, escaped
    expect(html).toContain("cw-badge");
  });

  it("unchanged doc renders with no marks", () => {
    const html = renderTrackChanges("Alpha.\n", "Alpha.\n");
    expect(html).not.toContain("cw-added");
    expect(html).not.toContain("cw-removed");
    expect(html).not.toContain("cw-changed");
  });

  it("is deterministic (same inputs → identical HTML) (INV-22)", () => {
    const a = renderTrackChanges("X.\n\nY.\n", "X.\n\nZ.\n");
    const b = renderTrackChanges("X.\n\nY.\n", "X.\n\nZ.\n");
    expect(a).toBe(b);
  });
});