1 +import assert from "node:assert/strict";
2 +import { spawnSync } from "node:child_process";
3 +import { mkdtempSync, readdirSync, rmSync, writeFileSync } from "node:fs";
4 +import { tmpdir } from "node:os";
5 +import { join } from "node:path";
6 +import { fileURLToPath } from "node:url";
7 +import { describe, test } from "node:test";
9 +const script = fileURLToPath(new URL("./validate-pi.mjs", import.meta.url));
10 +const installedPackage = process.env.PI_CONTEXT_PACKAGE_JSON_PATH;
12 + A: 'flowchart TD\n A["Agent"] --> B["Review"]',
13 + B: 'sequenceDiagram\n participant H as You\n participant A as Agent\n H->>A: Task\n A-->>H: Patch',
14 + C: 'flowchart LR\n A["Prepare environment"] --> B["Run agent assignment"] --> C["Collect test evidence"] --> D["Review proposed changes"] --> E["Integrate accepted revision"]',
15 + D: 'gantt\n title Agent work\n dateFormat YYYY-MM-DD\n section Work\n Review :2026-01-01, 1d',
16 + E: 'flowchart TD\n A[Start --> B',
19 +function invoke(source = examples.A, args = ["--width", "48"], options = {}) {
20 + const result = spawnSync(process.execPath, [script, ...args], {
26 + assert.ifError(result.error);
27 + assert.equal(result.signal, null);
31 +function withoutPi() {
32 + const env = { ...process.env };
33 + delete env.PI_CONTEXT_PACKAGE_JSON_PATH;
37 +test("help requires neither Pi nor source", () => {
38 + const result = invoke("", ["--help"], { env: withoutPi() });
39 + assert.equal(result.status, 0);
40 + assert.match(result.stdout, /--width/);
43 +test("requires explicit positive integer width; rejects file arguments", () => {
44 + for (const args of [[], ["--width", "0"], ["--width", "-1"], ["--width", "1.5"], ["--width", "abc"], ["--width", "9007199254740992"], ["--width", "48", "diagram.mmd"]]) {
45 + const result = invoke("", args, { env: withoutPi() });
46 + assert.equal(result.status, 2);
47 + assert.match(result.stderr, /Usage:/);
51 +test("missing Pi context fails explicitly without fallback", () => {
52 + const result = invoke(examples.A, undefined, { env: withoutPi() });
53 + assert.equal(result.status, 2);
54 + assert.match(result.stderr, /PI_CONTEXT_PACKAGE_JSON_PATH/);
57 +test("relative Pi package path is rejected", () => {
58 + const result = invoke(examples.A, undefined, { env: { ...withoutPi(), PI_CONTEXT_PACKAGE_JSON_PATH: "package.json" } });
59 + assert.equal(result.status, 2);
60 + assert.match(result.stderr, /absolute path/);
63 +test("missing Pi renderer fails without installing dependencies", () => {
64 + const root = mkdtempSync(join(tmpdir(), "mermaid-pi-missing-"));
66 + const manifest = join(root, "package.json");
67 + writeFileSync(manifest, "{}");
68 + const result = invoke(examples.A, undefined, { cwd: root, env: { ...withoutPi(), PI_CONTEXT_PACKAGE_JSON_PATH: manifest } });
69 + assert.equal(result.status, 2);
70 + assert.match(result.stderr, /Cannot load Pi's installed grok-mermaid/);
71 + assert.deepEqual(readdirSync(root), ["package.json"]);
73 + rmSync(root, { recursive: true, force: true });
77 +describe("installed Pi renderer", { skip: !installedPackage && "PI_CONTEXT_PACKAGE_JSON_PATH is needed for renderer integration tests" }, () => {
78 + for (const name of ["A", "B"]) {
79 + test(`${name}: narrow diagram passes with preview`, () => {
80 + const result = invoke(examples[name]);
81 + assert.equal(result.status, 0, result.stderr);
82 + const report = JSON.parse(result.stdout);
83 + assert.equal(report.ok, true);
84 + assert.deepEqual(report.warnings, []);
85 + assert.equal(report.overflow, false);
86 + assert.ok(report.lines.length > 0);
89 + test("C: wide diagram fails width budget", () => {
90 + const result = invoke(examples.C);
91 + assert.equal(result.status, 1, result.stderr);
92 + const report = JSON.parse(result.stdout);
93 + assert.equal(report.ok, false);
94 + assert.equal(report.overflow, true);
95 + assert.ok(report.requiredColumns > 48);
97 + test("D: unsupported diagram fails", () => {
98 + const result = invoke(examples.D);
99 + assert.equal(result.status, 1, result.stderr);
100 + assert.equal(JSON.parse(result.stdout).requiredColumns, null);
102 + test("E: warnings fail even when the partial drawing fits", () => {
103 + const result = invoke(examples.E);
104 + assert.equal(result.status, 1, result.stderr);
105 + const report = JSON.parse(result.stdout);
106 + assert.equal(report.ok, false);
107 + assert.equal(report.overflow, false);
108 + assert.ok(report.warnings.length > 0);
110 + test("empty stdin fails", () => {
111 + const result = invoke("");
112 + assert.equal(result.status, 1, result.stderr);
113 + assert.equal(JSON.parse(result.stdout).ok, false);
115 + test("exact width fits; one column less fails", () => {
116 + const width = JSON.parse(invoke().stdout).requiredColumns;
117 + assert.equal(invoke(examples.A, ["--width", String(width)]).status, 0);
118 + assert.equal(invoke(examples.A, ["--width", String(width - 1)]).status, 1);
120 + test("stdin validation from a Unicode/space cwd creates no files", () => {
121 + const root = mkdtempSync(join(tmpdir(), "mermaid pi #é-"));
123 + const result = invoke(examples.A, undefined, { cwd: root });
124 + assert.equal(result.status, 0, result.stderr);
125 + assert.deepEqual(readdirSync(root), []);
127 + rmSync(root, { recursive: true, force: true });