import { describe, expect, it } from "vitest"; import type { Edge, Node } from "@xyflow/react"; import { arrangeTopology, clearTopologyLayoutCache, topologyStructureKey } from "./topologyLayout"; const node = (id: string): Node => ({ id, position: { x: 0, y: 0 }, data: {} }); describe("topology layout", () => { it("lays out in-thread without Worker support and reuses cached results", async () => { clearTopologyLayoutCache(); const nodes = [node("a"), node("b")]; const edges: Edge[] = [{ id: "a-b", source: "a", target: "b" }]; const first = await arrangeTopology(nodes, edges); expect(Object.keys(first.positions).sort()).toEqual(["a", "b"]); expect(await arrangeTopology(nodes, edges)).toBe(first); }); it("keeps the layout cache bounded", async () => { clearTopologyLayoutCache(); const firstNodes = [node("n0")]; const first = await arrangeTopology(firstNodes, []); for (let index = 1; index <= 40; index += 1) { await arrangeTopology([node(`n${index}`)], []); } // The oldest entry was evicted, so a fresh object is computed. expect(await arrangeTopology(firstNodes, [])).not.toBe(first); expect(topologyStructureKey(firstNodes, [])).toBe("n0|"); }); });