Files
vscode-cowriting-plugin/test/mermaidSequenceDiff.test.ts
T
2026-06-11 16:32:08 -07:00

66 lines
2.5 KiB
TypeScript

import { describe, it, expect } from "vitest";
import { parseSequence } from "../src/mermaidSequenceDiff";
describe("parseSequence", () => {
it("captures explicit participants in order", () => {
const d = parseSequence("sequenceDiagram\n participant A\n participant B\n A->>B: hi");
expect(d.participants).toEqual(["A", "B"]);
});
it("captures message statements verbatim (trimmed)", () => {
const d = parseSequence("sequenceDiagram\n A->>B: hi\n B-->>A: bye");
expect(d.statements).toEqual(["A->>B: hi", "B-->>A: bye"]);
});
it("infers participants from messages when not declared", () => {
const d = parseSequence("sequenceDiagram\n A->>B: hi\n B->>C: on");
expect(d.participants).toEqual(["A", "B", "C"]);
});
it("ignores comments and blank lines", () => {
const d = parseSequence("sequenceDiagram\n%% note\n\n A->>B: hi");
expect(d.statements).toEqual(["A->>B: hi"]);
});
});
import { diffSequence } from "../src/mermaidSequenceDiff";
const rectCount = (s: string, rgb: string) => s.split(`rect rgb(${rgb})`).length - 1;
describe("diffSequence", () => {
it("wraps an added message in a green rect", () => {
const before = "sequenceDiagram\n A->>B: hi";
const current = "sequenceDiagram\n A->>B: hi\n B->>C: on";
const out = diffSequence(before, current);
expect(rectCount(out, "46, 160, 67")).toBe(1); // CW_COLORS.added as rgb
expect(out).toContain("B->>C: on");
});
it("ghosts a removed message back in a grey rect", () => {
const before = "sequenceDiagram\n A->>B: hi\n B->>C: gone";
const current = "sequenceDiagram\n A->>B: hi";
const out = diffSequence(before, current);
expect(out).toContain("B->>C: gone");
expect(rectCount(out, "128, 128, 128")).toBe(1);
});
it("re-declares a removed participant so it still appears", () => {
const before = "sequenceDiagram\n participant A\n participant B\n participant C\n A->>C: x";
const current = "sequenceDiagram\n participant A\n participant B\n A->>B: y";
const out = diffSequence(before, current);
expect(out).toMatch(/participant C/);
});
it("keeps an unchanged message outside any rect", () => {
const doc = "sequenceDiagram\n A->>B: hi";
const out = diffSequence(doc, doc);
expect(out).not.toContain("rect rgb");
});
it("is deterministic", () => {
const before = "sequenceDiagram\n A->>B: hi";
const current = "sequenceDiagram\n A->>B: hi\n B->>A: bye";
expect(diffSequence(before, current)).toBe(diffSequence(before, current));
});
});