import { describe, expect, it } from "vitest"; import { samplePath } from "../edges/edgeRoute"; import { ogdenApps, ogdenLinks } from "./ogden.fixture"; import { arrowHead, planGraphLayout, usableEdges } from "./plan"; import type { GraphDirection, GraphLayoutEdge, GraphLayoutNode, GraphPlan, PlannedNode, } from "./plan"; const spacing = { nodeSpacing: 16, rankSpacing: 32, margin: 16, strokeWidth: 1.5 }; function ogden(): { nodes: GraphLayoutNode[]; edges: GraphLayoutEdge[] } { return { nodes: ogdenApps.map((id) => ({ id, width: 90, height: 110 })), edges: ogdenLinks.map(([source, target]) => ({ source, target })), }; } function randomDag( count: number, seed: number, ): { nodes: GraphLayoutNode[]; edges: GraphLayoutEdge[] } { let state = seed; const next = () => { state = (state * 1103515245 + 12345) % 2147483648; return state / 2147483648; }; const nodes = Array.from({ length: count }, (_, index) => ({ id: `n${index}`, width: 60 + Math.floor(next() * 60), height: 40 + Math.floor(next() * 40), })); const edges: GraphLayoutEdge[] = []; for (let index = 1; index < count; index++) { if (next() < 0.15) continue; const links = 1 + Math.floor(next() * 2); for (let link = 0; link < links; link++) { edges.push({ source: `n${Math.floor(next() * index)}`, target: `n${index}`, emphasis: next() < 0.1, }); } } return { nodes, edges }; } function overlaps(a: PlannedNode, b: PlannedNode): boolean { return a.x < b.x + b.width && b.x < a.x + a.width && a.y < b.y + b.height && b.y < a.y + a.height; } function onEdge(point: { x: number; y: number }, node: PlannedNode): boolean { const within = (value: number, from: number, to: number) => value >= from - 0.5 && value <= to + 0.5; const onX = within(point.x, node.x, node.x + node.width); const onY = within(point.y, node.y, node.y + node.height); const nearSide = Math.abs(point.x - node.x) <= 0.5 || Math.abs(point.x - node.x - node.width) <= 0.5 || Math.abs(point.y - node.y) <= 0.5 || Math.abs(point.y - node.y - node.height) <= 0.5; return onX && onY && nearSide; } function strictlyInside(point: { x: number; y: number }, node: PlannedNode): boolean { return ( point.x > node.x + 0.5 && point.x < node.x + node.width - 0.5 && point.y > node.y + 0.5 && point.y < node.y + node.height - 0.5 ); } function audit(plan: GraphPlan, direction: GraphDirection) { const byId = new Map(plan.nodes.map((node) => [node.id, node])); let overlapCount = 0; for (let a = 0; a < plan.nodes.length; a++) { for (let b = a + 1; b < plan.nodes.length; b++) if (overlaps(plan.nodes[a]!, plan.nodes[b]!)) overlapCount++; } let offEdge = 0; let headsUnder = 0; let inversions = 0; for (const edge of plan.edges) { const source = byId.get(edge.source)!; const target = byId.get(edge.target)!; const points = samplePath(edge.d); if ( !onEdge(points[0]!, source) || !onEdge(points[points.length - 1]!, target) || !onEdge(edge.tip, target) ) offEdge++; const head = samplePath(edge.head); if (plan.nodes.some((node) => head.some((point) => strictlyInside(point, node)))) headsUnder++; if ( direction === "LR" ? source.x + source.width > target.x : source.y + source.height > target.y ) inversions++; } return { overlapCount, offEdge, headsUnder, inversions }; } describe("planGraphLayout (FZ-8149, LC-294)", () => { for (const direction of ["LR", "TB"] as const) { it(`lays Ogden corp's 57 apps and 59 links out ${direction} with no overlap, inversion or stray head`, () => { const { nodes, edges } = ogden(); const plan = planGraphLayout({ nodes, edges, direction, ...spacing, wrapWidth: 390 }); expect(plan.nodes).toHaveLength(57); expect(plan.edges).toHaveLength(usableEdges(nodes, edges).length); expect(audit(plan, direction)).toEqual({ overlapCount: 0, offEdge: 0, headsUnder: 0, inversions: 0, }); }); it(`holds the same on a 120-node DAG ${direction}`, () => { const { nodes, edges } = randomDag(120, 7); const plan = planGraphLayout({ nodes, edges, direction, ...spacing }); expect(plan.edges.length).toBeGreaterThan(100); expect(audit(plan, direction)).toEqual({ overlapCount: 0, offEdge: 0, headsUnder: 0, inversions: 0, }); }); } it("packs isolated nodes in rows under the graph, wrapping at the wider of the graph and wrapWidth", () => { const { nodes, edges } = ogden(); const plan = planGraphLayout({ nodes, edges, direction: "LR", ...spacing, wrapWidth: 390 }); const linked = new Set(usableEdges(nodes, edges).flatMap((edge) => [edge.source, edge.target])); const graph = plan.nodes.filter((node) => linked.has(node.id)); const loose = plan.nodes.filter((node) => !linked.has(node.id)); expect(loose.length).toBeGreaterThan(0); const graphBottom = Math.max(...graph.map((node) => node.y + node.height)); const graphRight = Math.max(...graph.map((node) => node.x + node.width)); for (const node of loose) { expect(node.y).toBeGreaterThanOrEqual(graphBottom); expect(node.x + node.width).toBeLessThanOrEqual( Math.max(graphRight, 390 + spacing.margin) + 0.5, ); } }); it("drops self-loops, unknown ends and repeats, and keeps a host's own positions", () => { const nodes = [ { id: "a", width: 40, height: 20 }, { id: "b", width: 40, height: 20 }, ]; const edges = [ { source: "a", target: "a" }, { source: "a", target: "zz" }, { source: "a", target: "b" }, { source: "a", target: "b" }, ]; const plan = planGraphLayout({ nodes, edges, direction: "LR", ...spacing, positions: { a: { x: 0, y: 0 }, b: { x: 200, y: 50 } }, }); expect(plan.edges.map((edge) => edge.id)).toEqual(["a\nb"]); expect(plan.nodes.map(({ id, x, y }) => ({ id, x, y }))).toEqual([ { id: "a", x: 16, y: 16 }, { id: "b", x: 216, y: 66 }, ]); expect(plan).toMatchObject({ width: 272, height: 102 }); }); it("paints emphasis in the theme accent and a kind in its token, never a literal", () => { const nodes = [ { id: "a", width: 40, height: 20 }, { id: "b", width: 40, height: 20 }, { id: "c", width: 40, height: 20 }, ]; const plan = planGraphLayout({ nodes, edges: [ { source: "a", target: "b", emphasis: true }, { source: "a", target: "c", kind: "error" }, ], direction: "LR", ...spacing, }); expect(plan.edges.map((edge) => edge.stroke)).toEqual(["$accent-color", "$red-9"]); expect(plan.edges[1]!.dash).toBe("6 4"); for (const edge of plan.edges) expect(edge.stroke.startsWith("$")).toBe(true); }); it("draws the head as a closed triangle whose tip is the end point, four strokes long", () => { expect(arrowHead({ x: 100, y: 50 }, "left" as never, 2)).toBe("M 100 50 L 92 53 L 92 47 Z"); expect(arrowHead({ x: 100, y: 50 }, "top" as never, 2)).toBe("M 100 50 L 97 42 L 103 42 Z"); }); });