test(home): add self-test suite for watcher.ts parseHandoff + tabFromFilename

Completes the symmetric tests-for-untested-helpers pass that d0e120f
started with emit.ts and 9576807 continued with log.ts. Watcher
parseHandoff is the function that turns a session.md blob into the
five-field Handoff that flows into worker.idle events — a regex
regression silently corrupts the brain's lastHandoff state for every
project. Eight inline cases:

  • Extracts all five fields from a well-formed session.md.
  • Missing fields default to empty string (no undefined).
  • Trims trailing whitespace from values.
  • Doesn't get fooled by field names embedded INSIDE other values
    (e.g. 'done: implementing next: foo' — next regex shouldn't
    catch 'next:' on a line that starts with 'done:').
  • All-empty Handoff for a blank input.
  • tabFromFilename strips .md.
  • Returns null for non-.md files (README.txt, noextension).
  • Preserves multi-word tab names verbatim (Tab #1, Revamp-Info).

Also adjusted the --start gate: --self-test is now an allowed entry
point alongside --start, with the bottom-of-file dispatch picking the
right branch. Naked invocation still prints the usage banner and
exits 0.

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 · home/watcher
8/8 (new). tsc clean. The only home/ module without a self-test now is
server.ts (HTTP route handlers — hard to exercise without booting the
server).
This commit is contained in:
g-but committed 2026-05-20 06:22:35 +02:00
1 parent 95768076f5
commit e85e249f8e
1 file changed
+91 -3
+91 -3
View File
@@ -30,10 +30,11 @@ import type { Handoff } from "@/lib/events";
// Boot-mode gate — symmetric with server.ts + worker.ts. Naked
// `npx tsx home/watcher.ts` is a help banner; --start opens the fs.watch.
if (!process.argv.includes("--start")) {
if (!process.argv.includes("--start") && !process.argv.includes("--self-test")) {
console.log(`${APP_NAME} watcher — Bridge layer of the home/ stack.
Watches ~/.claude/sessions/*.md and emits worker.idle events into ~/.${APP_SLUG}/events.jsonl.
Usage: npx tsx home/watcher.ts --start
Usage: npx tsx home/watcher.ts --start (boot the watcher)
npx tsx home/watcher.ts --self-test (run inline tests, no I/O)
Env: APP_SESSIONS_DIR override sessions dir (default ~/.claude/sessions)`);
process.exit(0);
}
@@ -185,4 +186,91 @@ function start() {
process.on("SIGTERM", () => shutdown("SIGTERM"));
}
start();
// ── Self-test ────────────────────────────────────────────────────────────────
// Run with: npx tsx home/watcher.ts --self-test
// Pure unit tests for the parsers — no fs.watch, no real session dir.
function selfTest() {
type Case = { name: string; run: () => boolean };
const cases: Case[] = [
{
name: "parseHandoff extracts all five fields from a well-formed session.md",
run: () => {
const h = parseHandoff([
"done: Built thing X",
"next: Test thing X",
"tests: 3 pass · 0 fail",
"todos: 0 TODOs",
"health: good",
"",
].join("\n"));
return h.done === "Built thing X"
&& h.next === "Test thing X"
&& h.tests === "3 pass · 0 fail"
&& h.todos === "0 TODOs"
&& h.health === "good";
},
},
{
name: "parseHandoff handles missing fields gracefully (empty string default)",
run: () => {
const h = parseHandoff("done: only this one\n");
return h.done === "only this one" && h.next === "" && h.health === "";
},
},
{
name: "parseHandoff trims trailing whitespace from values",
run: () => {
const h = parseHandoff("health: good \n");
return h.health === "good";
},
},
{
name: "parseHandoff doesn't get fooled by field names embedded inside other values",
run: () => {
// "done: implementing next: foo behavior" — next regex shouldn't
// match because the line starts with 'done:', not 'next:'.
const h = parseHandoff([
"done: implementing next: foo behavior",
"next: actually do it",
"",
].join("\n"));
return h.done === "implementing next: foo behavior"
&& h.next === "actually do it";
},
},
{
name: "parseHandoff returns all-empty Handoff for a blank session.md",
run: () => {
const h = parseHandoff("");
return h.done === "" && h.next === "" && h.tests === ""
&& h.todos === "" && h.health === "";
},
},
{
name: "tabFromFilename strips the .md extension",
run: () => tabFromFilename("Cockpit.md") === "Cockpit",
},
{
name: "tabFromFilename returns null for non-.md files",
run: () => tabFromFilename("README.txt") === null
&& tabFromFilename("noextension") === null,
},
{
name: "tabFromFilename preserves multi-word tab names verbatim",
run: () => tabFromFilename("Tab #1.md") === "Tab #1"
&& tabFromFilename("Revamp-Info.md") === "Revamp-Info",
},
];
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 (process.argv.includes("--self-test")) selfTest();
else start();