import { test } from "node:test"; import assert from "node:assert/strict"; import { miniatureModel, miniatureColor, createMiniGraphCache } from "../src/miniGraph.js"; const graph = { stages: [ { key: "a", edges: { success: "b", error: "c" } }, { key: "b", edges: { success: "#success" } }, { key: "c", edges: { error: "#failure" } }, ] }; test("miniatures retain real stage routes, without terminals or fabricated visits", () => { const model = miniatureModel(graph, { status: "failed" }); assert.deepEqual(model.nodes.map((n) => n.key).sort(), ["a", "b", "c"]); assert.equal(model.edges.length, 2); assert.equal(model.evidence, false); assert.ok(model.nodes.every((n) => n.color === null && !n.untaken)); }); test("visited stages/routes use evidence; unvisited branches remain hollow", () => { const model = miniatureModel(graph, { status: "waiting", executing: true, current_stage: "b", steps: [{ stage: "a", status: "completed", outcome: "success", target: "b" }] }); assert.equal(model.nodes.find((n) => n.key === "a").color, "var(--ok)"); assert.equal(model.nodes.find((n) => n.key === "b").color, "var(--status-waiting)"); assert.equal(model.nodes.find((n) => n.key === "c").untaken, true); assert.equal(model.edges.find((e) => e.outcome === "success").color, "var(--ok)"); assert.equal(model.edges.find((e) => e.outcome === "error").untaken, true); for (const status of ["succeeded", "failed", "running", "waiting", "interrupted", "queued", "canceled"]) { assert.equal(miniatureColor(status), `var(--status-${status})`); } }); test("cache deduplicates pinned reads, rejects latest mismatches and scopes by endpoint", async () => { const calls = []; const cached = createMiniGraphCache(async (id, version) => { calls.push([id, version]); return { version, graph }; }, () => {}); const run = { pipeline_id: 8, version: 2 }; assert.equal(cached(run, [{ id: 8, version: 3, shape: graph }], "one"), null); cached(run, [], "one"); await new Promise(setImmediate); assert.deepEqual(calls, [[8, 2]]); assert.equal(cached(run, [], "one"), graph); assert.equal(cached(run, [], "two"), null); assert.equal(cached({ pipeline_id: 8 }, []), null); const mismatch = createMiniGraphCache(async () => ({ version: 3, graph }), () => {}); mismatch(run); await new Promise(setImmediate); assert.equal(mismatch(run), null); });