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 }]); }); });