77 lines
2.8 KiB
TypeScript
77 lines
2.8 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import { parseDiagram, renderSvg } from "../../src/core/diagram";
|
|
describe("diagram grammar", () => {
|
|
it("parses and escapes flowcharts", () => {
|
|
const d = parseDiagram("flowchart LR\nA[<script>] -->|safe| B{Choice}");
|
|
expect(d.nodes).toHaveLength(2);
|
|
const svg = renderSvg(d);
|
|
expect(svg).toContain("<script>");
|
|
expect(svg).not.toContain("<script>");
|
|
});
|
|
it("supports sequence, class and state", () => {
|
|
const sequence = parseDiagram(
|
|
"sequenceDiagram\nparticipant A as Ada\nparticipant B\nA-->>B: hello",
|
|
);
|
|
expect(sequence.mode).toBe("sequence");
|
|
expect(sequence.nodes.map((node) => node.id)).toEqual(["A", "B"]);
|
|
expect(sequence.edges[0]).toMatchObject({
|
|
from: "A",
|
|
to: "B",
|
|
dashed: true,
|
|
});
|
|
expect(
|
|
parseDiagram("classDiagram\nclass A\nA : +field\nA <|-- B").nodes[0]
|
|
?.members,
|
|
).toEqual(["+field"]);
|
|
const state = parseDiagram(
|
|
'stateDiagram-v2\n[*] --> Idle\nstate "Working" as Ready\nIdle --> Ready : go\nReady --> [*]',
|
|
);
|
|
expect(state.mode).toBe("state");
|
|
expect(state.diagnostics).toEqual([]);
|
|
expect(state.edges.map((edge) => edge.label)).toContain("go");
|
|
expect(state.nodes.map((node) => node.id)).toEqual([
|
|
"__start",
|
|
"Idle",
|
|
"Ready",
|
|
"__end",
|
|
]);
|
|
});
|
|
it("supports bounded class member blocks and reports unclosed blocks", () => {
|
|
const diagram = parseDiagram(
|
|
"classDiagram\nclass Account {\n +String id\n +close()\n}\nAccount --> Ledger",
|
|
);
|
|
expect(diagram.diagnostics).toEqual([]);
|
|
expect(diagram.nodes[0]?.members).toEqual(["+String id", "+close()"]);
|
|
expect(
|
|
parseDiagram("classDiagram\nclass Account {\n +String id").diagnostics[0]
|
|
?.message,
|
|
).toMatch(/no closing/iu);
|
|
});
|
|
it("diagnoses directives without executing", () => {
|
|
const d = parseDiagram(
|
|
"flowchart LR\n%%{init: {}}%%\nclick A https://bad.test\nA-->B",
|
|
);
|
|
expect(d.diagnostics).toHaveLength(2);
|
|
expect(renderSvg(d)).not.toContain("bad.test");
|
|
});
|
|
it("supports ER attributes, mind maps, timelines and sequence notes", () => {
|
|
const er = parseDiagram(
|
|
"erDiagram\nUSER {\n string id PK\n}\nUSER ||--o{ ORDER : places",
|
|
);
|
|
expect(er.mode).toBe("er");
|
|
expect(er.nodes.find((node) => node.id === "USER")?.members).toEqual([
|
|
"string id PK",
|
|
]);
|
|
expect(er.edges[0]?.label).toContain("places");
|
|
expect(parseDiagram("mindmap\n Root\n Child").edges).toHaveLength(1);
|
|
expect(
|
|
parseDiagram("timeline\nsection Build\n2026 : Release").nodes,
|
|
).toHaveLength(2);
|
|
expect(
|
|
parseDiagram(
|
|
"sequenceDiagram\nparticipant A\nparticipant B\nNote over A,B: inert",
|
|
).edges[0],
|
|
).toMatchObject({ arrow: false, dashed: true });
|
|
});
|
|
});
|