1
0
Fork 0
polaris-task-force/tests/int/flow-edge-routing.int.spec.ts
Z8MB1E efab41ba29 refactor(flow): share the edge shape builder between both flow canvases
Extract the dialogue canvas's routing upgrades into routeEdge() in the
shared lib: perpendicular handle stubs, candidate scoring (Z-step vs
straight for forward edges, top/bottom bus corridors for back edges),
per-edge detour lane spreading, and multi-pass obstacle clearing. The
NPC dialogue RoutedFlowEdge becomes a thin renderer over it, and the
tech tree RoutedEdge now inherits the same logic while keeping its own
features intact: manual waypoints route verbatim (and define the
add-a-bend base), per-source stepPosition lanes feed the Z-step, and
its fixed right/left handles map to the new direction params.

New pure spec covers the Z-step shape, blocked-forward detouring,
back-edge bus routing, per-id lane spread, and waypoint pass-through.
2026-10-06 14:47:14 -04:00

111 lines
3.4 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
laneFor,
routeEdge,
segmentCrossesRect,
type RoutePoint,
type RouteRect,
} from "@/lib/flow-edge-routing";
const crossings = (points: RoutePoint[], rects: RouteRect[]) => {
let n = 0;
for (let i = 0; i < points.length - 1; i += 1) {
if (rects.some((r) => segmentCrossesRect(points[i], points[i + 1], r))) n += 1;
}
return n;
};
const A: RouteRect = { id: "a", x: 0, y: 0, width: 200, height: 100 };
const B: RouteRect = { id: "b", x: 600, y: 0, width: 200, height: 100 };
const BLOCKER: RouteRect = { id: "x", x: 340, y: -20, width: 80, height: 140 };
describe("shared edge router (routeEdge)", () => {
it("draws a stubbed Z-step for a clear forward edge", () => {
const { points } = routeEdge({
id: "e",
start: { x: 200, y: 50 },
end: { x: 600, y: 50 },
sourceDirection: "right",
targetDirection: "left",
nodes: [A, B],
sourceId: "a",
targetId: "b",
});
// start, sStub, two step corners (coincident on a straight run), tStub,
// end — no detours needed.
expect(points).toHaveLength(6);
expect(points[0]).toEqual({ x: 200, y: 50 });
expect(points[1]).toEqual({ x: 224, y: 50 }); // 24px stub off the handle
expect(points[2]).toEqual({ x: 400, y: 50 });
expect(points[3]).toEqual({ x: 400, y: 50 });
expect(points[points.length - 1]).toEqual({ x: 600, y: 50 });
expect(points[points.length - 2]).toEqual({ x: 576, y: 50 });
});
it("routes a blocked forward edge with zero card crossings", () => {
const { points } = routeEdge({
id: "e",
start: { x: 200, y: 50 },
end: { x: 600, y: 50 },
sourceDirection: "right",
targetDirection: "left",
nodes: [A, B, BLOCKER],
sourceId: "a",
targetId: "b",
});
expect(crossings(points, [BLOCKER])).toBe(0);
});
it("sends a back edge around the cards via a bus corridor", () => {
const { points } = routeEdge({
id: "back",
start: { x: 800, y: 50 },
end: { x: 0, y: 50 },
sourceDirection: "right",
targetDirection: "left",
nodes: [A, B, BLOCKER],
sourceId: "b",
targetId: "a",
});
expect(crossings(points, [A, B, BLOCKER])).toBe(0);
// It must actually leave the card band (above or below everything).
const ys = points.map((p) => p.y);
expect(Math.min(...ys) < -20 || Math.max(...ys) > 120).toBe(true);
});
it("spreads detour lanes per edge id", () => {
const route = (id: string) =>
JSON.stringify(
routeEdge({
id,
start: { x: 800, y: 50 },
end: { x: 0, y: 50 },
sourceDirection: "right",
targetDirection: "left",
nodes: [A, B, BLOCKER],
sourceId: "b",
targetId: "a",
}).points,
);
// laneFor buckets ids into distinct depths; these two land on different ones.
expect(laneFor("e1")).not.toBe(laneFor("e2"));
expect(route("e1")).not.toBe(route("e2"));
});
it("routes manual waypoints verbatim as the base shape", () => {
const wp: RoutePoint[] = [{ x: 400, y: 300 }];
const { base } = routeEdge({
id: "e",
start: { x: 200, y: 50 },
end: { x: 600, y: 50 },
sourceDirection: "right",
targetDirection: "left",
nodes: [A, B],
sourceId: "a",
targetId: "b",
waypoints: wp,
});
expect(base).toEqual([{ x: 200, y: 50 }, ...wp, { x: 600, y: 50 }]);
});
});