test(home): add self-test suite for log.ts — the read-side counterpart of emit.ts

Symmetric with d0e120f: home/log.ts had zero tests despite being the
read path every consumer relies on. A regression in the replay loop or
the per-line parser would silently drop events for the brain (state
drift) or the worker (lost dispatches). Six inline tests covering the
synchronous replay path:

  • Initial replay classifies every existing line as phase='replay'.
  • Garbled line surfaces via onError AND subsequent valid lines on
    the same replay still arrive — single bad line never poisons the
    rest of the stream.
  • Missing log file (and missing parent dir) is auto-created.
  • Empty file produces zero events, zero errors.
  • position() reflects bytes consumed during replay.
  • Blank lines between events are skipped silently (no onError noise).

Each test uses a fresh os.tmpdir + randomUUID file so cases don't
leak state between each other; everything is cleaned up via fs.rmSync.
Live-append testing is intentionally skipped — fs.watch is async/
debounced/best-effort on Linux, so the same consume() code path is
already exercised by the synchronous replay tests.

home/state 15/15 · home/decide 11/11 · home/projects 7/7 · home/worker
12/12 · home/render 12/12 · home/emit 5/5 · home/log 6/6 (new).
tsc clean.
This commit is contained in:
g-but committed 2026-05-20 06:15:24 +02:00
1 parent d0e120f2ea
commit 95768076f5
1 file changed
+114
+114
View File
@@ -100,3 +100,117 @@ export function tailLog(
position: () => position,
};
}
// ── Self-test ────────────────────────────────────────────────────────────────
// Run with: npx tsx home/log.ts --self-test
// Covers the synchronous replay path + error reporting + truncation reset.
// fs.watch live-append behavior is async; tested by integration only.
function selfTest() {
const os = require("node:os") as typeof import("node:os");
const cryp = require("node:crypto") as typeof import("node:crypto");
// Each test gets a fresh tmp file so state doesn't leak between cases.
function makeLogPath(): string {
return path.join(os.tmpdir(), `cockpit-log-test-${cryp.randomUUID()}.jsonl`);
}
const validEvent = (project: string) =>
`{"v":1,"id":"${cryp.randomUUID()}","ts":"2026-01-01T00:00:00Z","kind":"worker.idle","project":"${project}","handoff":{"done":"","next":"","tests":"","todos":"","health":"good"}}`;
type Case = { name: string; run: () => boolean };
const cases: Case[] = [
{
name: "initial replay classifies all existing lines as phase='replay'",
run: () => {
const log = makeLogPath();
fs.writeFileSync(log, validEvent("A") + "\n" + validEvent("B") + "\n");
const phases: EventPhase[] = [];
const h = tailLog(log, (_e, p) => phases.push(p));
h.close();
fs.rmSync(log, { force: true });
return phases.length === 2 && phases.every((p) => p === "replay");
},
},
{
name: "garbled line in initial replay fires onError without breaking subsequent valid lines",
run: () => {
const log = makeLogPath();
fs.writeFileSync(log,
`not-json-at-all\n` +
validEvent("Good") + "\n" +
`{"v":1,"id":"bad"}\n`, // missing required fields
);
const okCount: Event["kind"][] = [];
const errCount: string[] = [];
const h = tailLog(log,
(e) => okCount.push(e.kind),
(err) => errCount.push(err.message));
h.close();
fs.rmSync(log, { force: true });
return okCount.length === 1 && okCount[0] === "worker.idle"
&& errCount.length === 2;
},
},
{
name: "missing file is created at the path (parent dir included)",
run: () => {
const log = path.join(os.tmpdir(), `cockpit-log-test-${cryp.randomUUID()}/nested/log.jsonl`);
const h = tailLog(log, () => undefined);
h.close();
const exists = fs.existsSync(log);
const isEmpty = exists && fs.statSync(log).size === 0;
fs.rmSync(path.dirname(path.dirname(log)), { recursive: true, force: true });
return exists && isEmpty;
},
},
{
name: "empty file produces zero events and no errors",
run: () => {
const log = makeLogPath();
fs.writeFileSync(log, "");
let events = 0, errors = 0;
const h = tailLog(log, () => events++, () => errors++);
h.close();
fs.rmSync(log, { force: true });
return events === 0 && errors === 0;
},
},
{
name: "position() reflects bytes consumed during replay",
run: () => {
const log = makeLogPath();
const body = validEvent("X") + "\n" + validEvent("Y") + "\n";
fs.writeFileSync(log, body);
const h = tailLog(log, () => undefined);
const pos = h.position();
h.close();
fs.rmSync(log, { force: true });
return pos === body.length;
},
},
{
name: "blank lines between events are skipped silently",
run: () => {
const log = makeLogPath();
fs.writeFileSync(log, "\n\n" + validEvent("Z") + "\n\n");
let events = 0, errors = 0;
const h = tailLog(log, () => events++, () => errors++);
h.close();
fs.rmSync(log, { force: true });
return events === 1 && errors === 0;
},
},
];
let pass = 0, fail = 0;
for (const c of cases) {
if (c.run()) { console.log(` ✓ ${c.name}`); pass++; }
else { console.log(` ✗ ${c.name}`); fail++; }
}
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();
}