import assert from "node:assert/strict"; import { mkdir, mkdtemp, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { test } from "bun:test"; import { nodeWebObserveStatusNodeScript } from "./web-observe-scripts"; async function runStatusScript(stateDir: string, commandId: string): Promise> { const child = Bun.spawn(["bash", "-lc", nodeWebObserveStatusNodeScript(1, "NC01", "v03", commandId)], { env: { ...process.env, state_dir: stateDir }, stdout: "pipe", stderr: "pipe", }); const [stdout, stderr, exitCode] = await Promise.all([ new Response(child.stdout).text(), new Response(child.stderr).text(), child.exited, ]); assert.equal(exitCode, 0, stderr); return JSON.parse(stdout) as Record; } test("observe status returns one exact completed command without prompt payloads", async () => { const stateDir = await mkdtemp(join(tmpdir(), "unidesk-web-observe-exact-status-")); const commandId = "cmd-fixture"; await mkdir(join(stateDir, "commands", "done"), { recursive: true }); await mkdir(join(stateDir, "commands", "processing"), { recursive: true }); await writeFile(join(stateDir, "commands", "done", `${commandId}.json`), JSON.stringify({ ok: true, commandId, type: "validateRealtimeFanout", completedAt: "2026-07-10T12:00:00.000Z", result: { ok: true, status: "passed", profile: "pure-kafka-live", sessionId: "ses_fixture", traceIds: ["trc_first", "trc_second"], runIds: ["run_fixture"], commandIds: ["cmd_runner_first", "cmd_runner_second"], reportPath: "/tmp/report.json", reportSha256: "sha256:fixture", prompt: "must-not-leak", }, }) + "\n"); await writeFile(join(stateDir, "commands", "processing", `${commandId}.json`), JSON.stringify({ ok: null, commandId, type: "validateRealtimeFanout", }) + "\n"); const status = await runStatusScript(stateDir, commandId); assert.equal(status.exactCommand.phase, "done"); assert.equal(status.exactCommand.type, "validateRealtimeFanout"); assert.equal(status.exactCommand.result.reportSha256, "sha256:fixture"); assert.equal(status.exactCommand.result.prompt, undefined); assert.equal(status.exactCommand.valuesRedacted, true); }); test("observe status keeps exact command not-found structured", async () => { const stateDir = await mkdtemp(join(tmpdir(), "unidesk-web-observe-exact-missing-")); const status = await runStatusScript(stateDir, "cmd-missing"); assert.deepEqual(status.exactCommand, { commandId: "cmd-missing", phase: "not-found", ok: false, valuesRedacted: true, }); }); test("observe status preserves bounded Workbench Kafka debug replay evidence", async () => { const stateDir = await mkdtemp(join(tmpdir(), "unidesk-web-observe-debug-replay-status-")); const commandId = "cmd-debug-replay"; await mkdir(join(stateDir, "commands", "done"), { recursive: true }); await writeFile(join(stateDir, "commands", "done", `${commandId}.json`), JSON.stringify({ ok: true, commandId, type: "validateWorkbenchKafkaDebugReplay", completedAt: "2026-07-10T12:00:00.000Z", result: { ok: true, status: "passed", sessionId: "ses_fixture", traceId: "trc_fixture", topic: "hwlab.event.debug.v1", groupId: "hwlab-v03-workbench-isolated-debug-123-fixture", groupPrefix: "hwlab-v03-workbench-isolated-debug", phase: "terminal", code: "terminal_complete", receivedCount: 35, appliedCount: 35, layerCounts: { server: { scanned: 35, matched: 35, delivered: 35 }, client: { received: 35, decoded: 35, applied: 35 }, }, traceTimeline: { contract: "trace-sequence-authority-v1", sourceAuthorityRowCount: 35, readableSourceRowCount: 35, }, rawHwlabEventWindow: { available: true, samePage: true, unfilteredContractPresent: true, productEventSourceRequestCount: 0, counts: { received: 35, retained: 30, rejected: 0, evicted: 5, bytes: 8192 }, }, terminalStatus: "completed", reportPath: "/tmp/report.json", reportSha256: "sha256:report", screenshot: { path: "/tmp/replay.png", sha256: "sha256:image" }, }, }) + "\n"); const status = await runStatusScript(stateDir, commandId); assert.equal(status.exactCommand.type, "validateWorkbenchKafkaDebugReplay"); assert.equal(status.exactCommand.result.traceId, "trc_fixture"); assert.equal(status.exactCommand.result.topic, "hwlab.event.debug.v1"); assert.equal(status.exactCommand.result.groupPrefix, "hwlab-v03-workbench-isolated-debug"); assert.equal(status.exactCommand.result.receivedCount, 35); assert.equal(status.exactCommand.result.appliedCount, 35); assert.equal(status.exactCommand.result.phase, "terminal"); assert.equal(status.exactCommand.result.code, "terminal_complete"); assert.equal(status.exactCommand.result.layerCounts.server.scanned, 35); assert.equal(status.exactCommand.result.traceTimeline.sourceAuthorityRowCount, 35); assert.equal(status.exactCommand.result.rawHwlabEventWindow.counts.retained, 30); assert.equal(status.exactCommand.result.screenshot.sha256, "sha256:image"); });