import { describe, expect, it } from "vitest"; import { BAND_COLORS, layoutFactionGraph, linkStrokeWidth, type GraphFaction, type GraphScore, } from "@/lib/intelligence/factionGraphLayout"; const FACTION = ( id: number, name: string, type: string, discovered = true, ): GraphFaction => ({ id, name, type, discovered }); describe("faction relation graph layout", () => { const factions: GraphFaction[] = [ FACTION(1, "Batch 41-7", "playerFaction"), FACTION(2, "Worldmark", "majorFaction"), FACTION(3, "Autonomous Remnant", "majorFaction"), FACTION(4, "Harbor Guild", "minorFaction"), FACTION(5, "Vertex Corp", "corporation"), ]; it("puts the player faction at the center and everyone else on rings", () => { const layout = layoutFactionGraph(factions, []); const hub = layout.nodes.find((node) => node.id === 1)!; const majors = layout.nodes.filter((node) => node.type === "majorFaction"); const minor = layout.nodes.find((node) => node.type === "minorFaction")!; const corp = layout.nodes.find((node) => node.type === "corporation")!; expect(layout.hasHub).toBe(true); expect(hub.ring).toBe(0); expect(majors.every((node) => node.ring === 1)).toBe(true); expect(minor.ring).toBe(2); expect(corp.ring).toBe(3); const distance = (node: { x: number; y: number }) => Math.hypot(node.x - hub.x, node.y - hub.y); expect(distance(majors[0])).toBeCloseTo(distance(majors[1]), 5); expect(distance(minor)).toBeGreaterThan(distance(majors[0])); expect(distance(corp)).toBeGreaterThan(distance(minor)); }); it("keeps positions stable regardless of input order", () => { const forward = layoutFactionGraph(factions, []); const shuffled = layoutFactionGraph([...factions].reverse(), []); const positions = (layout: ReturnType) => new Map(layout.nodes.map((node) => [node.id, `${node.x},${node.y}`])); expect(positions(shuffled)).toEqual(positions(forward)); }); it("draws one straight line per direction, running side by side", () => { const scores: GraphScore[] = [ { subjectId: 3, targetId: 1, score: -100 }, { subjectId: 1, targetId: 3, score: 0 }, { subjectId: 2, targetId: 1, score: 25 }, { subjectId: 1, targetId: 2, score: 40 }, { subjectId: 4, targetId: 4, score: 60 }, { subjectId: 99, targetId: 1, score: 10 }, ]; const layout = layoutFactionGraph(factions, scores); // Zero scores, self scores and unknown factions never produce a link. expect(layout.links).toHaveLength(2); const pair = layout.links.find((link) => link.halves.length === 2)!; expect(pair.halves.map((half) => half.key).sort()).toEqual(["1:2", "2:1"]); // Each direction spans the whole pair, and the two lines are parallel and // separated, rather than overlapping or meeting at a midpoint. const forward = pair.halves.find((half) => half.key === "1:2")!; const backward = pair.halves.find((half) => half.key === "2:1")!; const forwardLength = Math.hypot(forward.x2 - forward.x1, forward.y2 - forward.y1); const backwardLength = Math.hypot(backward.x2 - backward.x1, backward.y2 - backward.y1); expect(forwardLength).toBeCloseTo(backwardLength, 0); // Perpendicular distance between the two lines. const dx = forward.x2 - forward.x1; const dy = forward.y2 - forward.y1; const length = Math.hypot(dx, dy) || 1; const separation = Math.abs( (backward.x1 - forward.x1) * (dy / length) - (backward.y1 - forward.y1) * (dx / length), ); expect(separation).toBeGreaterThan(6); expect(separation).toBeLessThan(16); expect(forward.band.label).toBe("Friendly"); expect(backward.band.label).toBe("Friendly"); const single = layout.links.find((link) => link.halves.length === 1)!; expect(single.halves[0].key).toBe("3:1"); expect(single.halves[0].band.label).toBe("Hostile"); expect(BAND_COLORS[single.halves[0].band.key]).toBe("#f87171"); }); it("keeps lines straight and trims them to the node edges", () => { const layout = layoutFactionGraph(factions, [ { subjectId: 2, targetId: 3, score: -20 }, { subjectId: 1, targetId: 4, score: 30 }, ]); const hub = layout.nodes.find((node) => node.id === 1)!; const minor = layout.nodes.find((node) => node.id === 4)!; const half = layout.links.find((link) => link.halves[0].key === "1:4")!.halves[0]; // Coordinates are rounded to two decimals, hence the small tolerance. const startDistance = Math.hypot(half.x1 - hub.x, half.y1 - hub.y); expect(startDistance).toBeGreaterThanOrEqual(hub.radius - 0.05); // The half ends at the segment's midpoint, which lies on the straight line // between the trimmed start and the target's center. const vx = half.x2 - half.x1; const vy = half.y2 - half.y1; const cross = vx * (minor.y - half.y1) - vy * (minor.x - half.x1); // Perpendicular deviation of the far node from the half's line, in pixels. const deviation = Math.abs(cross) / (Math.hypot(vx, vy) || 1); expect(deviation).toBeLessThan(0.5); // The far end stops at the target's edge, not its center. const endDistance = Math.hypot(minor.x - half.x2, minor.y - half.y2); expect(endDistance).toBeGreaterThanOrEqual(minor.radius - 0.05); expect(Math.hypot(half.x2 - half.x1, half.y2 - half.y1)).toBeGreaterThan(startDistance); }); it("nudges a segment aside instead of drawing it through another faction", () => { // Worldmark and Harbor Guild sit on opposite sides of the hub on the same // spoke: a straight Worldmark-to-Harbor segment would cut through the hub. const lineThroughHub: GraphScore[] = [{ subjectId: 2, targetId: 4, score: 30 }]; const layout = layoutFactionGraph(factions, lineThroughHub); const hub = layout.nodes.find((node) => node.id === 1)!; const half = layout.links[0].halves[0]; const start = { x: half.x1, y: half.y1 }; const end = layout.nodes.find((node) => node.id === 4)!; const dx = end.x - start.x; const dy = end.y - start.y; const length = Math.hypot(dx, dy) || 1; // Distance from the hub center to the infinite line through the segment. const distance = Math.abs((hub.x - start.x) * (dy / length) - (hub.y - start.y) * (dx / length)); expect(distance).toBeGreaterThanOrEqual(hub.radius); }); it("scales stroke width with magnitude and caps it", () => { expect(linkStrokeWidth(5)).toBeLessThan(linkStrokeWidth(60)); expect(linkStrokeWidth(100)).toBe(linkStrokeWidth(400)); expect(linkStrokeWidth(-60)).toBe(linkStrokeWidth(60)); }); it("reports a square canvas with one guide ring per used ring", () => { const layout = layoutFactionGraph(factions, []); expect(layout.width).toBe(layout.height); expect(layout.rings).toHaveLength(3); expect(layout.rings).toEqual([...layout.rings].sort((a, b) => a - b)); for (const node of layout.nodes) { if (node.ring === 0) continue; const distance = Math.hypot(node.x - layout.center.x, node.y - layout.center.y); expect(distance).toBeCloseTo(layout.rings[node.ring - 1], 0); } }); it("keeps every node inside the reported canvas", () => { const layout = layoutFactionGraph(factions, []); for (const node of layout.nodes) { expect(node.x - node.radius).toBeGreaterThanOrEqual(0); expect(node.y - node.radius).toBeGreaterThanOrEqual(0); expect(node.x + node.radius).toBeLessThanOrEqual(layout.width); expect(node.y + node.radius).toBeLessThanOrEqual(layout.height); } }); it("returns an empty layout when there are no factions", () => { expect(layoutFactionGraph([], [])).toEqual({ nodes: [], links: [], width: 0, height: 0, center: { x: 0, y: 0 }, rings: [], hasHub: false, }); }); it("still lays out factions when none is the player faction", () => { const layout = layoutFactionGraph( factions.filter((faction) => faction.type !== "playerFaction"), [{ subjectId: 2, targetId: 3, score: 12 }], ); expect(layout.hasHub).toBe(false); expect(layout.nodes).toHaveLength(4); expect(layout.links).toHaveLength(1); }); });