From d0e120f2eae2aa812e3528e1bdf6f43ae160e14e Mon Sep 17 00:00:00 2001 From: g-but <41178744+g-but@users.noreply.github.com> Date: Wed, 20 May 2026 06:08:45 +0200 Subject: [PATCH] =?UTF-8?q?test(home):=20add=20self-test=20suite=20for=20e?= =?UTF-8?q?mit.ts=20=E2=80=94=20the=20single=20append-only=20writer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit home/emit.ts had zero tests despite being the only path that writes to ~/./events.jsonl in production. If appendEvent corrupted the wire format, every consumer (server.ts tail, worker.ts tail) would silently skip the bad lines and the brain's state would drift without any signal. Adding a small inline suite, same pattern as the other home/ modules (--self-test flag, no extra deps): • Envelope stamping: v/id/ts present, id is a UUID, ts is ISO 8601. • Round-trip via parseEvent — wire format survives serialize→parse. • Two appends produce distinct ids (no accidental reuse). • appendEvent creates the parent directory if missing. • DistributiveOmit sanity: bridge.dispatch-specific fields (prompt, autonomy, confidence) survive serialization — guards against the union flattening that bit me earlier when introducing the type. • worker.crashed payload preserves the error message. All writes go to a unique mkdtemp directory, cleaned up at the end even on assertion failure — the real production log is never touched. home/state 15/15 · home/decide 11/11 · home/projects 7/7 · home/worker 12/12 · home/render 12/12 · home/emit 5/5 (new). tsc clean. --- home/emit.ts | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 108 insertions(+), 1 deletion(-) diff --git a/home/emit.ts b/home/emit.ts index 07267b5fa..f57c16f21 100644 --- a/home/emit.ts +++ b/home/emit.ts @@ -16,7 +16,7 @@ import path from "node:path"; import os from "node:os"; import { randomUUID } from "node:crypto"; import { APP_SLUG } from "@/config/brand"; -import { EVENT_VERSION, serializeEvent, type Event } from "@/lib/events"; +import { EVENT_VERSION, parseEvent, serializeEvent, type Event } from "@/lib/events"; export const LOG_PATH = path.join(os.homedir(), `.${APP_SLUG}`, "events.jsonl"); @@ -41,3 +41,110 @@ export function appendEvent(payload: EventPayload, logPath: string = LOG_PATH): } as Event; fs.appendFileSync(logPath, serializeEvent(event)); } + +// ── Self-test ──────────────────────────────────────────────────────────────── +// Run with: npx tsx home/emit.ts --self-test +// Writes to a unique temp file (NOT the real log) so production state isn't +// touched. Round-trips through parseEvent to verify the wire format holds. + +function selfTest() { + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), `${APP_SLUG}-emit-test-`)); + const tmpLog = path.join(tmpDir, "events.jsonl"); + + type Case = { name: string; run: () => boolean }; + const cases: Case[] = [ + { + name: "appendEvent stamps v, id, ts and round-trips via parseEvent", + run: () => { + appendEvent({ + kind: "worker.idle", + project: "TestProject", + handoff: { done: "", next: "", tests: "", todos: "", health: "good" }, + }, tmpLog); + const line = fs.readFileSync(tmpLog, "utf8").trim(); + const parsed = parseEvent(line); + if (!parsed.ok) return false; + return parsed.event.v === EVENT_VERSION + && typeof parsed.event.id === "string" + && parsed.event.id.length === 36 // UUID + && parsed.event.ts.endsWith("Z") // ISO 8601 + && parsed.event.kind === "worker.idle" + && parsed.event.project === "TestProject"; + }, + }, + { + name: "two consecutive appends produce distinct ids", + run: () => { + const log = path.join(tmpDir, "distinct.jsonl"); + appendEvent({ kind: "worker.idle", project: "A", + handoff: { done: "", next: "", tests: "", todos: "", health: "" }}, log); + appendEvent({ kind: "worker.idle", project: "A", + handoff: { done: "", next: "", tests: "", todos: "", health: "" }}, log); + const lines = fs.readFileSync(log, "utf8").trim().split("\n"); + if (lines.length !== 2) return false; + const r1 = parseEvent(lines[0]); const r2 = parseEvent(lines[1]); + return r1.ok && r2.ok && r1.event.id !== r2.event.id; + }, + }, + { + name: "appendEvent creates the parent directory if missing", + run: () => { + const nested = path.join(tmpDir, "deeply/nested/path/log.jsonl"); + appendEvent({ kind: "worker.idle", project: "Nest", + handoff: { done: "", next: "", tests: "", todos: "", health: "" }}, nested); + return fs.existsSync(nested) && fs.statSync(nested).size > 0; + }, + }, + { + name: "bridge.dispatch payload preserves kind-specific fields (DistributiveOmit sanity check)", + run: () => { + const log = path.join(tmpDir, "dispatch.jsonl"); + appendEvent({ + kind: "bridge.dispatch", + project: "X", + intent: "next_best", + prompt: "go", + runId: "550e8400-e29b-41d4-a716-446655440000", + autonomy: "manual", + reason: "test", + confidence: 0.8, + }, log); + const parsed = parseEvent(fs.readFileSync(log, "utf8").trim()); + if (!parsed.ok || parsed.event.kind !== "bridge.dispatch") return false; + return parsed.event.prompt === "go" + && parsed.event.autonomy === "manual" + && parsed.event.confidence === 0.8; + }, + }, + { + name: "worker.crashed payload preserves the error message", + run: () => { + const log = path.join(tmpDir, "crashed.jsonl"); + appendEvent({ + kind: "worker.crashed", + project: "X", + runId: "550e8400-e29b-41d4-a716-446655440000", + error: "inject failed: tab not found", + }, log); + const parsed = parseEvent(fs.readFileSync(log, "utf8").trim()); + return parsed.ok + && parsed.event.kind === "worker.crashed" + && parsed.event.error === "inject failed: tab not found"; + }, + }, + ]; + + let pass = 0, fail = 0; + for (const c of cases) { + if (c.run()) { console.log(` ✓ ${c.name}`); pass++; } + else { console.log(` ✗ ${c.name}`); fail++; } + } + // Clean up the temp tree even on assertion failure. + fs.rmSync(tmpDir, { recursive: true, force: true }); + console.log(`\n${pass}/${pass + fail} passed`); + if (fail > 0) process.exit(1); +} + +if (import.meta.url === `file://${process.argv[1]}`) { + if (process.argv.includes("--self-test")) selfTest(); +}