SPB Git

Repository Public

topology.test.ts 3.3 KB 63 lines typescript

1import { describe, expect, it } from "vitest";2import type { Schemas } from "@/api/client";3import { LAYOUT, layoutTopology } from "./topology";45type N = Schemas["TopologyNode"];6type L = Schemas["TopologyLink"];7const node = (label: string): N => ({ id: `v-${label}`, kind: "node", label, node_id: `id-${label}`, freshness: "fresh" });8const gw: N = { id: "gw", kind: "lan_gateway", label: "LAN gateway" };9const hub: N = { id: "hub", kind: "wg_hub", label: "WireGuard hub" };10const link = (id: string, source: string, target: string, kind: L["kind"] = "lan"): L => ({ id, source, target, kind, state: "up", freshness: "fresh", latency_availability: "unavailable", loss_availability: "unavailable" });1112describe("layoutTopology", () => {13  it("is deterministic whatever the input order", () => {14    const nodes = [node("m4ml"), hub, node("M3U96a"), gw, node("e2e-node-02"), node("e2e-node-10")];15    const links = [link("b", "v-m4ml", "gw"), link("a", "v-M3U96a", "hub", "wireguard")];16    const a = layoutTopology(nodes, links);17    const b = layoutTopology([...nodes].reverse(), [...links].reverse());18    expect(b).toEqual(a);19  });2021  it("puts infrastructure in the centre and splits nodes left and right in natural alias order", () => {22    const l = layoutTopology([gw, hub, node("n10"), node("n2"), node("n1"), node("n3")], []);23    const by = (id: string) => l.nodes.find((n) => n.id === id);24    expect(by("gw")?.side).toBe("center");25    expect(by("hub")?.side).toBe("center");26    expect(by("gw")?.x).toBe(LAYOUT.centerX);27    expect((by("gw")?.y ?? 0) < (by("hub")?.y ?? 0)).toBe(true); // LAN gateway above the tunnel hub28    expect(l.nodes.filter((n) => n.side === "left").map((n) => n.label)).toEqual(["n1", "n2"]);29    expect(l.nodes.filter((n) => n.side === "right").map((n) => n.label)).toEqual(["n3", "n10"]);30  });3132  it("keeps every vertex inside the drawing and rows apart", () => {33    const nodes = [gw, hub, ...Array.from({ length: 29 }, (_, i) => node(`e2e-node-${String(i + 1).padStart(2, "0")}`))];34    const l = layoutTopology(nodes, []);35    for (const n of l.nodes) {36      expect(n.y).toBeGreaterThanOrEqual(LAYOUT.padTop);37      expect(n.y).toBeLessThanOrEqual(l.height - LAYOUT.padBottom);38    }39    const leftYs = l.nodes.filter((n) => n.side === "left").map((n) => n.y);40    expect(leftYs.length).toBe(15);41    for (let i = 1; i < leftYs.length; i++) expect((leftYs[i] ?? 0) - (leftYs[i - 1] ?? 0)).toBeCloseTo(LAYOUT.rowHeight, 5);42    expect(new Set(l.nodes.map((n) => `${n.x}:${n.y}`)).size).toBe(l.nodes.length); // no two vertices overlap43  });4445  it("connects links to their vertices and reports the ones it cannot draw", () => {46    const l = layoutTopology([gw, node("a")], [link("ok", "v-a", "gw"), link("lost", "v-zzz", "gw")]);47    expect(l.links).toHaveLength(1);48    const drawn = l.links[0];49    expect(drawn?.x1).toBe(LAYOUT.leftX);50    expect(drawn?.x2).toBe(LAYOUT.centerX);51    expect(drawn?.anchor).toBe("start");52    expect(drawn && drawn.lx > drawn.x1 && drawn.lx < drawn.x2).toBe(true);53    expect(l.dangling.map((d) => d.id)).toEqual(["lost"]);54  });5556  it("handles an empty topology and a single hub", () => {57    expect(layoutTopology([], []).nodes).toEqual([]);58    const l = layoutTopology([gw], []);59    expect(l.nodes[0]?.y).toBeCloseTo(l.height / 2, 5);60    expect(l.height).toBe(LAYOUT.minHeight);61  });62});63
Files main
Menu

Theme