From 087a7a1bb1d72531e508f363906d5ea33ade9594 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Thu, 3 Dec 2026 21:19:07 +0100 Subject: [PATCH] Check /healthz directly in valid-config entrypoint smoke test --- tests/entrypoint-secrets.test.ts | 119 ++++++++++++++++++------------- 1 file changed, 70 insertions(+), 49 deletions(-) diff --git a/tests/entrypoint-secrets.test.ts b/tests/entrypoint-secrets.test.ts index 0c44bbc..6d14f54 100644 --- a/tests/entrypoint-secrets.test.ts +++ b/tests/entrypoint-secrets.test.ts @@ -1,6 +1,7 @@ import { spawn } from "node:child_process"; import { chmodSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { createRequire } from "node:module"; +import { createServer } from "node:net"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { fileURLToPath } from "node:url"; @@ -22,8 +23,20 @@ function secretFile(name: string, contents: string): string { return filePath; } -function canaryTokenKey(): string { - return `${CANARY}key`.padEnd(43, "A") + "="; +function reserveLoopbackPort(): Promise { + return new Promise((resolve, reject) => { + const server = createServer(); + server.once("error", reject); + server.listen(0, "127.0.0.1", () => { + const address = server.address(); + if (!address || typeof address === "string") { + server.close(() => reject(new Error("Expected numeric ephemeral port"))); + return; + } + const { port } = address; + server.close((err) => (err ? reject(err) : resolve(port))); + }); + }); } function killProcessTree(child: ReturnType): void { @@ -65,6 +78,10 @@ function runEntrypoint(name: string, env: Record): Promise<{ cod }); } +function canaryTokenKey(): string { + return `${CANARY}key`.padEnd(43, "A") + "="; +} + describe("entrypoint startup and secret redaction smoke checks", () => { it("startup failure redacts configured secrets from stdout and stderr", async () => { const run = await runEntrypoint("redact", { @@ -90,57 +107,61 @@ describe("entrypoint startup and secret redaction smoke checks", () => { expect(run.output).toMatch(/GC_ACCESS_BASE_URL is required when GAMECULT_IDUNN_CANDIDATE_BIND is set/); }, 45_000); - it("valid config starts and listens without printing configured secrets", async () => { - const output = await new Promise((resolve, reject) => { - const child = spawn(process.execPath, [tsxCli, "src/index.ts"], { - cwd: repoRoot, - detached: process.platform !== "win32", - env: { - PATH: process.env.PATH ?? "", - HOME: scratch, - GC_ACCESS_DATA_ROOT: join(scratch, "data-valid"), - PORT: "0", - GC_ACCESS_PRIVATE_COMMAND_PORT: "0", - GC_ACCESS_TOKEN_ENCRYPTION_KEY_BASE64_FILE: secretFile("valid-token-key", `${canaryTokenKey()}\n`), - GC_ACCESS_APP_BIFROST_SHARED_SECRET_FILE: secretFile("valid-bifrost", `${CANARY}bifrost\n`), - GC_ACCESS_PROVIDER_DISCORD_CLIENT_ID: "discord-client-id", - GC_ACCESS_PROVIDER_DISCORD_CLIENT_SECRET_FILE: secretFile("valid-discord", `${CANARY}discord\n`), - }, - stdio: ["ignore", "pipe", "pipe"], - }); + it("valid config starts and serves /healthz without printing configured secrets", async () => { + const port = await reserveLoopbackPort(); + const child = spawn(process.execPath, [tsxCli, "src/index.ts"], { + cwd: repoRoot, + detached: process.platform !== "win32", + env: { + PATH: process.env.PATH ?? "", + HOME: scratch, + GC_ACCESS_DATA_ROOT: join(scratch, "data-valid"), + HOST: "127.0.0.1", + PORT: String(port), + GC_ACCESS_PRIVATE_COMMAND_PORT: "0", + GC_ACCESS_TOKEN_ENCRYPTION_KEY_BASE64_FILE: secretFile("valid-token-key", `${canaryTokenKey()}\n`), + GC_ACCESS_APP_BIFROST_SHARED_SECRET_FILE: secretFile("valid-bifrost", `${CANARY}bifrost\n`), + GC_ACCESS_PROVIDER_DISCORD_CLIENT_ID: "discord-client-id", + GC_ACCESS_PROVIDER_DISCORD_CLIENT_SECRET_FILE: secretFile("valid-discord", `${CANARY}discord\n`), + }, + stdio: ["ignore", "pipe", "pipe"], + }); - let buf = ""; - let stopping = false; - let finishErr: Error | undefined; - - const stop = (err?: Error) => { - if (stopping) return; - stopping = true; - finishErr = err; - clearTimeout(timer); - killProcessTree(child); - }; - - const onData = (chunk: Buffer) => { - buf += chunk.toString("utf8"); - if (buf.includes("Heimdall listening on")) { - stop(); - } - }; - - child.stdout.on("data", onData); - child.stderr.on("data", onData); - const timer = setTimeout(() => stop(new Error(`Timed out waiting for listener; output:\n${buf}`)), 30_000); - child.on("error", (err) => stop(err)); + let buf = ""; + let exited: number | null | undefined; + const closed = new Promise((resolve) => { + child.stdout.on("data", (chunk: Buffer) => (buf += chunk.toString("utf8"))); + child.stderr.on("data", (chunk: Buffer) => (buf += chunk.toString("utf8"))); child.on("close", (code) => { - clearTimeout(timer); - if (finishErr) reject(finishErr); - else if (!stopping) reject(new Error(`Exited unexpectedly with code ${code}; output:\n${buf}`)); - else resolve(buf); + exited = code; + resolve(); }); }); - expect(output).toContain("Heimdall listening on"); - expect(output).not.toContain(CANARY); + try { + const deadline = Date.now() + 25_000; + let health: { ok?: boolean; service?: string } | undefined; + while (Date.now() < deadline && exited === undefined) { + try { + const response = await fetch(`http://127.0.0.1:${port}/healthz`); + if (response.ok) { + health = (await response.json()) as { ok?: boolean; service?: string }; + break; + } + } catch { + // listener not ready yet + } + await new Promise((resolve) => setTimeout(resolve, 100)); + } + + expect(health, `Expected /healthz to respond 200; exited=${exited}; output:\n${buf}`).toMatchObject({ + ok: true, + service: "heimdall", + }); + expect(buf).not.toContain(CANARY); + } finally { + killProcessTree(child); + await closed; + } }, 45_000); });