feat: re-emit changed mermaid diagrams through mermaidDiff in the built-in preview (plan T7 §2.6, INV-29 parity)

Task 7 scoped out reusing the F7.1 (#22) intra-diagram mermaid diff in the
built-in Markdown preview's mermaid fence rendering; Task 8 then deleted the
webview that still had it, leaving mermaidDiff.ts/mermaidFlowchartDiff.ts/
mermaidSequenceDiff.ts production-dead. Wires diffMermaid into
cowritingMarkdownItPlugin's options.highlight override via a per-render
mermaid-fence queue (built in the core rule where state.env is visible,
consumed by highlight in fence-encounter order — highlight itself has no env
param) so a mermaid fence changed since the F6/F7 baseline is re-emitted with
classDef/class/linkStyle styling directives + legend before mermaid.js ever
sees it, reusing diffBlocks' baseline/current block pairing (same pairing
renderReview used). Unchanged diagrams, disabled annotations, no baseline, and
pinned-zero-diff all pass through verbatim.

Ports the substance of the 6 mermaid-diff assertions Task 8 deleted with the
webview's E2E suite as pure unit tests in previewAnnotations.test.ts instead
(DOM-free, source-level).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
BenStullsBets
2026-07-02 15:08:46 -07:00
parent 2170a0d282
commit 705de3169b
5 changed files with 167 additions and 16 deletions
+59
View File
@@ -73,3 +73,62 @@ describe("preview annotations (PUC-3/D21)", () => {
expect(html).not.toContain("cw-");
});
});
// Task 7 §2.6 / cross-task review follow-up (plan-mandated, INV-29..31): the
// built-in preview's mermaid fence rendering reuses the F7.1 (#22) pure
// re-emit, same as the sunset webview did — a changed mermaid diagram gets
// intra-diagram change styling, not just the plain current diagram. Ports the
// substance of the E2E assertions Task 8 deleted with the webview
// (`git show 2170a0d^:test/e2e/suite/trackChangesPreview.test.ts`, "a changed
// flowchart augments the emitted mermaid source (#22, INV-29)") as pure unit
// tests here instead — no DOM/webview needed to verify SOURCE augmentation.
describe("intra-diagram mermaid diff in the built-in preview (plan T7 §2.6, INV-29)", () => {
const flowchartBefore = "```mermaid\nflowchart LR\n a --> b\n```\n";
const flowchartAfter = "```mermaid\nflowchart LR\n a --> b\n a --> c\n```\n";
it("a changed flowchart fence is re-emitted with cwAdded styling + a legend", () => {
const html = render(flowchartAfter, { ...base, baselineText: flowchartBefore, spans: [] });
expect(html).toMatch(/classDef cwAdded/);
expect(html).toMatch(/class\s+c\s+cwAdded/);
expect(html).toContain("cw-mermaid-legend");
expect(html).toContain('class="mermaid"');
});
it("an unchanged mermaid fence passes through byte-identical (no diff markup)", () => {
const html = render(flowchartBefore, { ...base, baselineText: flowchartBefore, spans: [] });
expect(html).not.toContain("cwAdded");
expect(html).not.toContain("cw-mermaid-legend");
expect(html).toContain("flowchart LR");
expect(html).toContain("a --&gt; b");
});
it("annotations disabled: a changed mermaid fence renders the plain current diagram", () => {
const html = render(flowchartAfter, { ...base, enabled: false, baselineText: flowchartBefore, spans: [] });
expect(html).not.toContain("cwAdded");
expect(html).not.toContain("cw-mermaid-legend");
expect(html).toContain("a --&gt; c");
});
it("pinned baseline with zero landed diff: mermaid fence renders the plain current diagram (INV-33/#48 parity)", () => {
const html = render(flowchartAfter, {
...base, baselineReason: "pinned", baselineText: flowchartAfter, spans: [],
});
expect(html).not.toContain("cwAdded");
expect(html).not.toContain("cw-mermaid-legend");
});
it("no baseline (no coediting context established): mermaid fence renders the plain current diagram", () => {
const html = render(flowchartAfter, { ...base, baselineText: undefined, spans: [] });
expect(html).not.toContain("cwAdded");
expect(html).not.toContain("cw-mermaid-legend");
});
it("a non-mermaid changed fence is untouched by the mermaid queue (fence-kind gate)", () => {
const before = "```js\nconst a = 1;\n```\n";
const after = "```js\nconst a = 2;\n```\n";
const html = render(after, { ...base, baselineText: before, spans: [] });
expect(html).not.toContain("cwAdded");
expect(html).not.toContain("cw-mermaid-legend");
expect(html).toContain('class="language-js"');
});
});