Buckets:
| // Copyright (c) 2026 the Zuke contributors | |
| // SPDX-License-Identifier: MIT | |
| /** | |
| * Unit tests for `build/graph_doc.ts` — the generator behind `./zuke graphDoc` | |
| * and the `graphDocCheck` gate target. Exercises the pure rendering (rows, | |
| * Mermaid diagram, table, escaping, determinism) against small fixture builds, | |
| * without touching the real `docs/graph.md`. | |
| */ | |
| import { Build, discoverTargets, target } from "@zuke/core"; | |
| import { | |
| assertEquals, | |
| assertStringIncludes, | |
| } from "../packages/core/tests/_assert.ts"; | |
| import { | |
| canonicalGraphDoc, | |
| checkGraphDoc, | |
| graphDocTargets, | |
| renderGraphDoc, | |
| renderMermaid, | |
| writeGraphDoc, | |
| } from "../build/graph_doc.ts"; | |
| import { formatText } from "../build/fmt_text.ts"; | |
| class Demo extends Build { | |
| lint = target().description("Lint the workspace").executes(() => {}); | |
| test = target() | |
| .description("Run the test suite") | |
| .dependsOn(this.lint) | |
| .executes(() => {}); | |
| ci = target() | |
| .description("The gate | with a pipe") | |
| .dependsOn(this.lint, this.test) | |
| .executes(() => {}); | |
| } | |
| Deno.test("graphDocTargets extracts rows in declaration order with deps", () => { | |
| const rows = graphDocTargets(discoverTargets(new Demo())); | |
| assertEquals(rows.map((r) => r.name), ["lint", "test", "ci"]); | |
| assertEquals(rows[0].deps, []); | |
| assertEquals(rows[1].deps, ["lint"]); | |
| assertEquals(rows[2].deps, ["lint", "test"]); | |
| assertEquals(rows[1].description, "Run the test suite"); | |
| }); | |
| Deno.test("renderMermaid declares every node and edge with synthetic ids", () => { | |
| const mermaid = renderMermaid(graphDocTargets(discoverTargets(new Demo()))); | |
| assertStringIncludes(mermaid, "flowchart TD"); | |
| assertStringIncludes(mermaid, 't0["lint"]'); | |
| assertStringIncludes(mermaid, 't1["test"]'); | |
| assertStringIncludes(mermaid, 't2["ci"]'); | |
| assertStringIncludes(mermaid, "t0 --> t1"); | |
| assertStringIncludes(mermaid, "t0 --> t2"); | |
| assertStringIncludes(mermaid, "t1 --> t2"); | |
| }); | |
| Deno.test("renderMermaid escapes Mermaid-reserved label characters", () => { | |
| const mermaid = renderMermaid([ | |
| { name: 'a"b#c', description: "", deps: [] }, | |
| ]); | |
| assertStringIncludes(mermaid, 't0["a#quot;b#35;c"]'); | |
| }); | |
| Deno.test("renderMermaid skips an edge whose dependency names no row", () => { | |
| const mermaid = renderMermaid([ | |
| { name: "known", description: "", deps: ["missing"] }, | |
| ]); | |
| assertEquals(mermaid.includes("-->"), false); | |
| assertEquals(mermaid.includes("undefined"), false); | |
| }); | |
| Deno.test("renderGraphDoc renders the page with marker, diagram, and table", () => { | |
| const page = renderGraphDoc(discoverTargets(new Demo())); | |
| assertStringIncludes(page, "# Zuke's build graph"); | |
| assertStringIncludes( | |
| page, | |
| "<!-- Generated by `./zuke graphDoc` — do not edit by hand. -->", | |
| ); | |
| assertStringIncludes(page, "3 target(s), 3 dependency edge(s)."); | |
| assertStringIncludes(page, "```mermaid"); | |
| assertStringIncludes(page, "| Target | Description | Depends on |"); | |
| assertStringIncludes(page, "| `lint` | Lint the workspace | — |"); | |
| assertStringIncludes(page, "| `test` | Run the test suite | `lint` |"); | |
| // A `|` in a description must not break the table. | |
| assertStringIncludes( | |
| page, | |
| "| `ci` | The gate \\| with a pipe | `lint`, `test` |", | |
| ); | |
| }); | |
| Deno.test("renderGraphDoc is deterministic", () => { | |
| assertEquals( | |
| renderGraphDoc(discoverTargets(new Demo())), | |
| renderGraphDoc(discoverTargets(new Demo())), | |
| ); | |
| }); | |
| Deno.test("renderGraphDoc handles a build with no targets", () => { | |
| const page = renderGraphDoc(new Map()); | |
| assertStringIncludes(page, "0 target(s), 0 dependency edge(s)."); | |
| assertStringIncludes(page, "No targets defined."); | |
| assertEquals(page.includes("```mermaid"), false); | |
| }); | |
| Deno.test("writeGraphDoc writes, reports no-op, and checkGraphDoc sees drift", async () => { | |
| const dir = await Deno.makeTempDir({ prefix: "zuke-graph-doc-" }); | |
| const path = `${dir}/graph.md`; | |
| try { | |
| const targets = discoverTargets(new Demo()); | |
| // Missing file: check reports it, write creates it. | |
| assertEquals(await checkGraphDoc(targets, path), [`${path} (missing)`]); | |
| assertEquals(await writeGraphDoc(targets, path), true); | |
| assertEquals(await checkGraphDoc(targets, path), []); | |
| // Unchanged build: writing again is a no-op. | |
| assertEquals(await writeGraphDoc(targets, path), false); | |
| // Hand-edited page: check reports it stale, write repairs it. | |
| await Deno.writeTextFile(path, "tampered"); | |
| assertEquals(await checkGraphDoc(targets, path), [`${path} (stale)`]); | |
| assertEquals(await writeGraphDoc(targets, path), true); | |
| assertEquals(await checkGraphDoc(targets, path), []); | |
| } finally { | |
| await Deno.remove(dir, { recursive: true }); | |
| } | |
| }); | |
| Deno.test("the canonical page is formatter-clean", async () => { | |
| // `docs/` is inside the formatter's scope, so a page the formatter would | |
| // rewrite puts `format` and `graphDocCheck` in contradiction: each one's fix | |
| // breaks the other, which is exactly how this surfaced. Formatting is | |
| // idempotent, so the canonical page is a fixed point of it — assert that, | |
| // rather than that some particular layout was produced, which would only | |
| // restate the formatter's rules in a second place. | |
| const page = await canonicalGraphDoc(discoverTargets(new Demo())); | |
| assertEquals(await formatText(page, "md"), page); | |
| }); | |
| Deno.test("an empty build still renders a formatter-clean page", async () => { | |
| // The no-targets branch takes a different path through the renderer, and it | |
| // is the one a fresh project hits first. | |
| const page = await canonicalGraphDoc(new Map()); | |
| assertEquals(await formatText(page, "md"), page); | |
| }); | |
Xet Storage Details
- Size:
- 5.66 kB
- Xet hash:
- 485739da79bb7abdff66e55d4e3f4f33b4adf93b0a8122e59ea2d6440ea2cfc1
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.