Repository Public
1import { describe, expect, it } from "vitest";2import { BlockSchema, type BlockOf } from "@/schema";3import { layoutDiagram, rectsOverlap, type DiagramLayout } from "@/renderer/diagram/layout";4import { measureText, wrapText } from "@/renderer/diagram/text-measure";5import { DIAGRAM } from "./renderer.fixture";67const diagram = (input: unknown): BlockOf<"diagram"> => BlockSchema.parse(input) as BlockOf<"diagram">;89const twelve = (extra: Record<string, unknown> = {}) => diagram({10 id: "d", type: "diagram", ...extra,11 nodes: Array.from({ length: 12 }, (_, i) => ({ id: `n${i}`, label: `Nœud numéro ${i}`, sublabel: i % 3 === 0 ? "avec un sous-titre" : undefined, shape: ["rect", "round", "pill", "diamond", "cylinder", "circle", "hexagon", "note"][i % 8], group: i >= 4 && i < 8 ? "g" : undefined, icon: i % 4 === 0 ? "cpu" : undefined })),12 edges: [[0, 1], [0, 2], [1, 3], [2, 3], [3, 4], [4, 5], [4, 6], [5, 7], [6, 7], [7, 8], [8, 9], [8, 10], [9, 11], [10, 11], [0, 11], [11, 3]].map(([a, b], i) => ({ id: `e${i}`, from: `n${a}`, to: `n${b}`, label: i % 4 === 0 ? `lien ${i}` : undefined })),13 groups: [{ id: "g", label: "Groupe" }],14});1516const noOverlap = (l: DiagramLayout) => {17 for (let i = 0; i < l.nodes.length; i++) for (let j = i + 1; j < l.nodes.length; j++) {18 const a = l.nodes[i], b = l.nodes[j];19 expect(rectsOverlap({ x: a.left, y: a.top, width: a.width, height: a.height }, { x: b.left, y: b.top, width: b.width, height: b.height }), `${a.id} overlaps ${b.id}`).toBe(false);20 }21};2223describe("text measure", () => {24 it("is monotonic and scales with size and weight", () => {25 expect(measureText("", 14)).toBe(0);26 expect(measureText("abc", 14)).toBeLessThan(measureText("abcd", 14));27 expect(measureText("abc", 28)).toBeCloseTo(measureText("abc", 14) * 2, 1);28 expect(measureText("abc", 14, 700)).toBeGreaterThan(measureText("abc", 14, 400));29 expect(measureText("iii", 14)).toBeLessThan(measureText("MMM", 14));30 expect(measureText("éàç", 14)).toBe(measureText("eac", 14));31 // Inter at 14 px: "Hello world" is about 72 px wide.32 expect(measureText("Hello world", 14)).toBeGreaterThan(62);33 expect(measureText("Hello world", 14)).toBeLessThan(84);34 });35 it("wraps to the width and to a number of lines", () => {36 const lines = wrapText("Un libellé assez long pour passer sur plusieurs lignes", 120, 14, { maxLines: 5 });37 expect(lines.length).toBeGreaterThan(1);38 for (const l of lines) expect(measureText(l, 14)).toBeLessThanOrEqual(120);39 expect(lines.join(" ")).toBe("Un libellé assez long pour passer sur plusieurs lignes");40 const cut = wrapText("Un libellé assez long pour passer sur plusieurs lignes", 120, 14, { maxLines: 2 });41 expect(cut).toHaveLength(2);42 expect(cut[1].endsWith("…")).toBe(true);43 expect(measureText(cut[1], 14)).toBeLessThanOrEqual(120);44 expect(wrapText("", 100, 14)).toEqual([""]);45 const broken = wrapText("Anticonstitutionnellementissime", 60, 14, { maxLines: 6 });46 expect(broken.length).toBeGreaterThan(1);47 for (const l of broken) expect(measureText(l, 14)).toBeLessThanOrEqual(60);48 });49});5051describe("diagram layout", () => {52 for (const direction of ["TB", "LR"] as const) for (const routing of ["curved", "orthogonal", "straight"] as const) {53 it(`lays out 12 nodes without overlap (${direction}, ${routing})`, () => {54 const block = twelve({ direction, routing });55 const l = layoutDiagram(block);56 expect(l.nodes).toHaveLength(12);57 noOverlap(l);58 expect(l.edges).toHaveLength(block.edges.length);59 for (const e of l.edges) {60 expect(e.d.startsWith("M")).toBe(true);61 expect(e.d).not.toMatch(/NaN|undefined|Infinity/);62 expect(e.points.length).toBeGreaterThanOrEqual(2);63 }64 // Everything is inside the viewBox.65 const vb = l.viewBox;66 for (const n of l.nodes) { expect(n.left).toBeGreaterThanOrEqual(vb.x); expect(n.top).toBeGreaterThanOrEqual(vb.y); expect(n.left + n.width).toBeLessThanOrEqual(vb.x + vb.width); expect(n.top + n.height).toBeLessThanOrEqual(vb.y + vb.height); }67 expect(l.width).toBe(vb.width); expect(l.height).toBe(vb.height);68 // The group box contains its members and no other node centre.69 const g = l.groups[0];70 for (const n of l.nodes) {71 const member = ["n4", "n5", "n6", "n7"].includes(n.id);72 const inside = n.x > g.x && n.x < g.x + g.width && n.y > g.y && n.y < g.y + g.height;73 expect(inside, `${n.id} ${member ? "should be inside" : "should be outside"} the group`).toBe(member);74 }75 // Edge labels do not cover a node.76 for (const e of l.edges) if (e.label) for (const n of l.nodes) expect(rectsOverlap(e.label, { x: n.left, y: n.top, width: n.width, height: n.height }), `label of ${e.id} over ${n.id}`).toBe(false);77 });78 }7980 it("follows the direction", () => {81 const chain = (direction: "TB" | "LR") => layoutDiagram(diagram({ id: "d", type: "diagram", direction, nodes: [{ id: "a", label: "A" }, { id: "b", label: "B" }, { id: "c", label: "C" }], edges: [{ id: "e1", from: "a", to: "b" }, { id: "e2", from: "b", to: "c" }] }));82 const tb = chain("TB"), lr = chain("LR");83 expect(tb.nodes[0].y).toBeLessThan(tb.nodes[1].y); expect(tb.nodes[1].y).toBeLessThan(tb.nodes[2].y);84 expect(lr.nodes[0].x).toBeLessThan(lr.nodes[1].x); expect(lr.nodes[1].x).toBeLessThan(lr.nodes[2].x);85 expect(tb.edges[0].fromSide).toBe("bottom"); expect(tb.edges[0].toSide).toBe("top");86 expect(lr.edges[0].fromSide).toBe("right"); expect(lr.edges[0].toSide).toBe("left");87 });8889 it("respects manual positions and re-routes the edges", () => {90 const base = twelve({ routing: "orthogonal" });91 const moved = diagram({ ...base, nodes: base.nodes.map((n) => (n.id === "n2" ? { ...n, x: 900, y: -300 } : n)) });92 const a = layoutDiagram(base), b = layoutDiagram(moved);93 const n2 = b.nodes.find((n) => n.id === "n2")!;94 expect(n2.x).toBe(900); expect(n2.y).toBe(-300); expect(n2.manual).toBe(true);95 // The other nodes stay where the engine put them.96 for (const n of b.nodes) if (n.id !== "n2") { const o = a.nodes.find((m) => m.id === n.id)!; expect([n.x, n.y]).toEqual([o.x, o.y]); }97 // Edges attached to the moved node end on its new box.98 for (const e of b.edges.filter((x) => x.from === "n2" || x.to === "n2")) {99 const end = e.from === "n2" ? e.points[0] : e.points[e.points.length - 1];100 expect(end.x).toBeGreaterThanOrEqual(n2.left - 1); expect(end.x).toBeLessThanOrEqual(n2.left + n2.width + 1);101 expect(end.y).toBeGreaterThanOrEqual(n2.top - 1); expect(end.y).toBeLessThanOrEqual(n2.top + n2.height + 1);102 expect(e.d).not.toBe(a.edges.find((x) => x.id === e.id)!.d);103 }104 expect(b.viewBox.y).toBeLessThan(n2.top);105 });106107 it("honours explicit ports", () => {108 const l = layoutDiagram(diagram({ id: "d", type: "diagram", nodes: [{ id: "a", label: "A", shape: "rect" }, { id: "b", label: "B", shape: "rect" }], edges: [{ id: "e", from: "a", to: "b", fromPort: "left", toPort: "right" }] }));109 const a = l.nodes[0], b = l.nodes[1], e = l.edges[0];110 expect(e.fromSide).toBe("left"); expect(e.toSide).toBe("right");111 expect(e.points[0].x).toBeCloseTo(a.left, 0); expect(e.points[e.points.length - 1].x).toBeCloseTo(b.left + b.width, 0);112 });113114 it("is deterministic", () => {115 const block = diagram(DIAGRAM);116 expect(JSON.stringify(layoutDiagram(block))).toBe(JSON.stringify(layoutDiagram(diagram(DIAGRAM))));117 expect(JSON.stringify(layoutDiagram(twelve()))).toBe(JSON.stringify(layoutDiagram(twelve())));118 });119120 it("handles a single node, self-loops and cycles", () => {121 const one = layoutDiagram(diagram({ id: "d", type: "diagram", nodes: [{ id: "a", label: "Seul" }] }));122 expect(one.nodes).toHaveLength(1); expect(one.width).toBeGreaterThan(0);123 const loop = layoutDiagram(diagram({ id: "d", type: "diagram", nodes: [{ id: "a", label: "A" }, { id: "b", label: "B" }], edges: [{ id: "s", from: "a", to: "a" }, { id: "x", from: "a", to: "b" }, { id: "y", from: "b", to: "a" }] }));124 expect(loop.edges).toHaveLength(3);125 for (const e of loop.edges) expect(e.d).not.toMatch(/NaN/);126 noOverlap(loop);127 });128});129