72 lines
4.0 KiB
JavaScript
72 lines
4.0 KiB
JavaScript
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("compact list evidence preserves successes before failure and last loop status", () => {
|
|
const loopGraph = { stages: [
|
|
{ key: "a", edges: { success: "b", error: "never" } },
|
|
{ key: "b", edges: { success: "c" } },
|
|
{ key: "c", edges: { retry: "a", error: "never" } },
|
|
{ key: "never", edges: {} },
|
|
] };
|
|
const visits = [
|
|
{ sequence: 1, stage: "a", status: "completed", outcome: "success", target: "b" },
|
|
{ sequence: 2, stage: "b", status: "completed", outcome: "success", target: "c" },
|
|
{ sequence: 3, stage: "c", status: "completed", outcome: "retry", target: "a" },
|
|
{ sequence: 4, stage: "a", status: "failed", outcome: "", target: "" },
|
|
];
|
|
const model = miniatureModel(loopGraph, { status: "failed", graph_evidence: { complete: true, visits } });
|
|
assert.equal(model.nodes.find((n) => n.key === "a").color, "var(--status-failed)");
|
|
assert.equal(model.nodes.find((n) => n.key === "b").color, "var(--ok)");
|
|
assert.equal(model.nodes.find((n) => n.key === "never").color, null);
|
|
assert.equal(model.nodes.find((n) => n.key === "never").untaken, true);
|
|
assert.equal(model.edges.filter((e) => !e.untaken).length, 3);
|
|
assert.ok(model.edges.filter((e) => e.outcome === "error").every((e) => e.untaken));
|
|
for (const status of ["waiting", "running", "queued"]) {
|
|
const next = miniatureModel(loopGraph, { status, current_stage: "a", executing: status !== "waiting",
|
|
graph_evidence: { complete: true, visits: [...visits.slice(0, 3), { sequence: 4, stage: "a", status }] } });
|
|
assert.equal(next.nodes.find((n) => n.key === "a").color, miniatureColor(status));
|
|
}
|
|
});
|
|
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(--warn)");
|
|
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), status === "waiting" ? "var(--warn)" : status === "running" ? "var(--accent)" : `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);
|
|
});
|