// End-to-end data-layer test: the FULL replay path the server runs, against the // REAL generated masked-relay state-dir. This exercises the FS read surface // (`openStateDir`), the snapshot derivation (`buildSnapshot` — edge lights + // diamond single-wake + cost rollup), and world-model click-through // (`readNodeWorldModel` → `readVersion`). No model key, no running reactor. import { strict as assert } from "node:assert"; import { test } from "node:test"; import { existsSync, mkdtempSync, readdirSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { generateMaskedRelayFixture } from "../fixtures/masked-relay"; import { openStateDir, buildSnapshot, readNodeWorldModel, NotAStateDirError, } from "./index"; function freshFixture(): string { const stateDir = mkdtempSync(join(tmpdir(), "rdt-statedir-")); generateMaskedRelayFixture({ stateDir }); return stateDir; } test("openStateDir + buildSnapshot derive the full wire payload from a real state-dir", () => { const stateDir = freshFixture(); const opened = openStateDir(stateDir); const snap = buildSnapshot(opened); // topology came from the saved compile/topology.json (not the fallback). assert.equal(snap.hasTopology, true); assert.ok(snap.nodes.length >= 10, "≥10 nodes drawn"); assert.ok(snap.edges.length > snap.nodes.length, "per-facet edges outnumber nodes"); assert.ok(snap.entryPoints.includes("gateway.signal-inbox")); assert.equal(snap.acyclic, true); assert.ok(snap.frames.length > 0, "frames present"); // every frame is index-aligned and well-formed. snap.frames.forEach((f, i) => { assert.equal(f.index, i, "frame index = position"); assert.ok(["rendered", "skipped", "failed"].includes(f.status)); assert.ok(["input", "self", "external"].includes(f.wakeSource)); assert.ok(Array.isArray(f.movedFacets)); assert.ok(Array.isArray(f.edgesToLight)); assert.ok(Array.isArray(f.wokenSubscribers)); }); // EDGE LIGHTS: at least one rendered frame lights ≥1 lane, and every lit lane // is a real topology edge whose producer is the frame's node and whose facet // the frame moved (the selector boundary — strict facet match). const edgeKey = (e: { producer: string; subscriber: string; facet: string }) => `${e.producer}${e.subscriber}${e.facet}`; const topoEdges = new Set(snap.edges.map(edgeKey)); let litSomething = false; for (const f of snap.frames) { if (f.edgesToLight.length > 0) litSomething = true; for (const lit of f.edgesToLight) { assert.equal(lit.producer, f.node, "lit lane fans out FROM the frame's node"); assert.ok(topoEdges.has(edgeKey(lit)), "lit lane is a real topology edge"); assert.ok(f.movedFacets.includes(lit.facet), "lit lane's facet moved this frame"); } // skipped / failed never propagate. if (f.status !== "rendered") { assert.equal(f.edgesToLight.length, 0, "non-rendered frame lights no edges"); assert.equal(f.wokenSubscribers.length, 0, "non-rendered frame wakes nothing"); } } assert.ok(litSomething, "at least one render lit a propagation lane"); // DIAMOND SINGLE-WAKE: wokenSubscribers is deduped — no node twice in a frame, // and it never exceeds the distinct subscribers among that frame's lit lanes. for (const f of snap.frames) { const woken = f.wokenSubscribers; assert.equal(new Set(woken).size, woken.length, "each woken subscriber appears once"); const distinctLaneSubs = new Set(f.edgesToLight.map((e) => e.subscriber)); assert.equal(woken.length, distinctLaneSubs.size, "woken = distinct lit-lane subscribers"); } // COST ROLLUP: fresh spend exists; totals are consistent across causes. assert.ok(snap.costRollup.total.fresh > 0, "fresh tokens spent (the meter sings)"); const summedFresh = Object.values(snap.costRollup.byCause).reduce( (acc, b) => acc + b.fresh, 0, ); assert.equal(summedFresh, snap.costRollup.total.fresh, "byCause fresh sums to total"); }); test("openStateDir on an ABSENT dir does NOT create files and signals not-a-state-dir (B5)", () => { const base = mkdtempSync(join(tmpdir(), "rdt-absent-")); const absent = join(base, "no-such-state-dir"); assert.equal(existsSync(absent), false); assert.throws( () => openStateDir(absent), (err: unknown) => err instanceof NotAStateDirError, "absent dir → NotAStateDirError, not a silent create", ); // The pure-read open left the target untouched — it was NOT mkdir'd, and no // registry.json / receipts.json was seeded into it. assert.equal(existsSync(absent), false, "absent dir stays absent (no mkdir)"); }); test("openStateDir on a BARE dir does NOT seed files and signals not-a-state-dir (B5)", () => { // An existing-but-empty directory with neither receipts.json nor compile/ is // not a state-dir; opening it must not mutate it into one. const bare = mkdtempSync(join(tmpdir(), "rdt-bare-")); assert.equal(readdirSync(bare).length, 0, "starts empty"); assert.throws(() => openStateDir(bare), NotAStateDirError); // Still empty — the not-a-state-dir signal is preserved (no seeded files). assert.equal(readdirSync(bare).length, 0, "bare dir stays bare (no seed)"); }); test("readNodeWorldModel reads a node's truth at a frame's atomicVersion (R3)", () => { const stateDir = freshFixture(); const opened = openStateDir(stateDir); const snap = buildSnapshot(opened); // pick a rendered frame for a responsibility node that carries an atomicVersion. const frame = snap.frames.find( (f) => f.status === "rendered" && f.node.startsWith("responsibility.") && f.atomicVersion.length > 0, ); assert.ok(frame, "a rendered responsibility frame exists"); const view = readNodeWorldModel(opened, frame!.node, frame!.atomicVersion); assert.ok(view !== null, "world-model resolves for node@atomicVersion"); assert.equal(view!.node, frame!.node); assert.equal(view!.version, frame!.atomicVersion); assert.ok(view!.files.length > 0, "the version has artifact files"); // at least one file decodes as text (the inspector renders it). assert.ok( view!.files.some((file) => file.text !== null && file.bytes > 0), "a readable text artifact is present", ); // published fingerprint map carries @atomic. assert.ok("@atomic" in view!.publishedFingerprints, "published @atomic fingerprint present"); }); test("readNodeWorldModel returns null for an unknown node or bogus version", () => { const stateDir = freshFixture(); const opened = openStateDir(stateDir); assert.equal( readNodeWorldModel(opened, "nope.not-a-node", "sha256:" + "0".repeat(64)), null, "unknown node → null (not a throw)", ); const snap = buildSnapshot(opened); const real = snap.frames.find((f) => f.atomicVersion.length > 0)!; assert.equal( readNodeWorldModel(opened, real.node, "sha256:" + "f".repeat(64)), null, "unknown version → null", ); });