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(); +}