Files
gstack/design/test/daemon-tests-fixtures.ts
T
Garry Tan e3c235ae5c feat(design): daemon-client with lock + identity-verified spawn
design/src/daemon-client.ts implements the CLI side of the daemon lifecycle:
ensureDaemon() (the spawn-or-attach decision), publishBoard(), and the
$D daemon stop|status helpers.

Modeled on browse/src/cli.ts:317-415 — same health-check-first attach,
same fs.openSync('wx') lock, same re-read-state-INSIDE-the-lock guard
against two CLIs both deciding "no daemon, spawn." Two design-specific
safety properties added beyond browse:

1. verifyIdentity before any SIGTERM/SIGKILL. Reads the running process's
   cmdline (/proc/PID/cmdline on Linux, `ps -p PID -o command=` on macOS)
   and only signals if it contains CMDLINE_MARKER ("gstack-design-daemon",
   passed as argv at spawn time). Prevents a stale state file from
   causing us to kill an unrelated process that inherited the PID.

2. Refuse-kill-with-active-boards on version mismatch. Browse silently
   restarts; here in-memory board history would vanish, so the client
   prints a user-actionable WARNING and exit 1 instead. Users explicitly
   `$D daemon stop` to override.

Spawn uses Node child_process.spawn (NOT Bun.spawn().unref) because of
the macOS session-detach quirks browse already discovered. Stdio is
redirected to ~/.gstack/design-daemon-startup.log, which the client
tails into stderr if waitForHealthOrError times out — no more silent
"daemon failed for some unknowable reason."

daemon-state.ts gains DESIGN_DAEMON_STATE_FILE env override so tests
can point both client and spawned daemon at a per-test path without a
shared cwd.

design/test/daemon-discovery.test.ts: 17 tests, all green in ~8s. Covers:
spawn-fresh, attach-existing, stale-state-file (pid dead), PID-reuse
safety (uses the test runner's own PID as the bait — verifyIdentity
catches the cmdline mismatch, daemon not signaled), version-mismatch
with/without active boards (the active-boards case runs a subprocess
and asserts exit 1 + WARNING in stderr), publishBoard 200 + 409,
shutdownDaemon refuse/force/unresponsive paths, daemonStatus.

The daemon-discovery suite is split out of daemon.test.ts because each
real spawn costs ~200ms; the in-process daemon.test.ts (30 tests, 70ms)
covers the same handler logic without the spawn overhead.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-25 15:26:07 -07:00

136 lines
4.2 KiB
TypeScript

/**
* Shared helpers for daemon + daemon-client tests.
*
* Two test styles live here:
* - In-process: import fetchHandler from daemon.ts and call it with a
* synthesized Request. Fast, no spawn, no HTTP. Covers routing +
* handler semantics. Used by most of daemon.test.ts.
* - Out-of-process: spawn `bun run design/src/daemon.ts` with a tmp
* state file + env overrides, then HTTP against the bound port.
* Slow but only path that proves real spawn + state file + signal
* handling work. Used by daemon-discovery.test.ts.
*/
import { spawn, type ChildProcess } from "child_process";
import fs from "fs";
import os from "os";
import path from "path";
import { __testInternals__ } from "../src/daemon";
export const DAEMON_SCRIPT = path.join(import.meta.dir, "..", "src", "daemon.ts");
export function makeTmpDir(prefix = "design-daemon-test"): string {
return fs.mkdtempSync(path.join(os.tmpdir(), `${prefix}-`));
}
export function makeBoardHtml(tmpDir: string, body = "<p>Test board</p>"): string {
const p = path.join(tmpDir, "design-board.html");
fs.writeFileSync(
p,
`<!DOCTYPE html><html><head></head><body>${body}</body></html>`,
);
return p;
}
/** Reset the in-process daemon state between tests. */
export function resetDaemon(): void {
__testInternals__.resetForTest();
}
/** Build a Request for the in-process fetchHandler tests. */
export function req(method: string, urlPath: string, body?: unknown): Request {
const init: RequestInit = { method };
if (body !== undefined) {
init.body = typeof body === "string" ? body : JSON.stringify(body);
init.headers = { "Content-Type": "application/json" };
}
return new Request(`http://127.0.0.1:1234${urlPath}`, init);
}
export interface SpawnedDaemon {
proc: ChildProcess;
port: number;
stateFile: string;
stop: () => Promise<void>;
}
/**
* Spawn a real daemon process pointed at a per-test state file, with an
* aggressive idle window so idle-shutdown tests don't take 24h. Resolves
* when stdout emits `DAEMON_STARTED port=<N>`.
*/
export async function spawnDaemonForTest(
opts: { stateFile?: string; idleMs?: number; checkMs?: number; env?: Record<string, string> } = {},
): Promise<SpawnedDaemon> {
const stateFile = opts.stateFile ?? path.join(makeTmpDir("daemon-state"), "design.json");
const env: Record<string, string> = {
...(process.env as Record<string, string>),
// DESIGN_DAEMON_STATE_FILE points both daemon and any same-process
// discovery at this test's state file (overrides resolveStateFilePath).
DESIGN_DAEMON_STATE_FILE: stateFile,
DESIGN_DAEMON_IDLE_MS: String(opts.idleMs ?? 60_000),
DESIGN_DAEMON_CHECK_MS: String(opts.checkMs ?? 1000),
DESIGN_DAEMON_VERSION: "test-version",
...(opts.env ?? {}),
};
// Spawn with a marker in argv so cmdline-based identity verification
// exercises the real CMDLINE_MARKER ("gstack-design-daemon").
const proc = spawn(
"bun",
["run", DAEMON_SCRIPT, "--marker", "gstack-design-daemon"],
{
env,
stdio: ["ignore", "pipe", "pipe"],
cwd: path.dirname(stateFile),
},
);
const port = await new Promise<number>((resolve, reject) => {
const onTimeout = setTimeout(() => {
proc.kill("SIGKILL");
reject(new Error("Daemon failed to emit DAEMON_STARTED within 5s"));
}, 5000);
proc.stdout!.on("data", (chunk: Buffer) => {
const line = chunk.toString();
const m = line.match(/DAEMON_STARTED port=(\d+)/);
if (m) {
clearTimeout(onTimeout);
resolve(parseInt(m[1]!, 10));
}
});
proc.on("error", (e) => {
clearTimeout(onTimeout);
reject(e);
});
proc.on("exit", (code) => {
clearTimeout(onTimeout);
reject(new Error(`Daemon exited early with code ${code}`));
});
});
return {
proc,
port,
stateFile,
stop: async () => {
proc.kill("SIGTERM");
await new Promise<void>((r) => {
const t = setTimeout(() => {
try {
proc.kill("SIGKILL");
} catch {
// gone
}
r();
}, 2000);
proc.on("exit", () => {
clearTimeout(t);
r();
});
});
},
};
}