From c7808a543d0d8e28c18f3e40978b7fe87ac539b8 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 19 Jan 2026 15:17:44 +0000 Subject: [PATCH] test: stabilize windows gateway sigterm --- src/cli/gateway.sigterm.test.ts | 63 ++++++++++++++++++++++++--------- 1 file changed, 46 insertions(+), 17 deletions(-) diff --git a/src/cli/gateway.sigterm.test.ts b/src/cli/gateway.sigterm.test.ts index 0ead48360..840ff0c62 100644 --- a/src/cli/gateway.sigterm.test.ts +++ b/src/cli/gateway.sigterm.test.ts @@ -88,10 +88,7 @@ describe("gateway SIGTERM", () => { const err: string[] = []; const nodeBin = process.execPath; - const args = [ - "--import", - "tsx", - "src/entry.ts", + const entryArgs = [ "gateway", "--port", String(port), @@ -99,21 +96,53 @@ describe("gateway SIGTERM", () => { "loopback", "--allow-unconfigured", ]; + const env = { + ...process.env, + CLAWDBOT_NO_RESPAWN: "1", + CLAWDBOT_STATE_DIR: stateDir, + CLAWDBOT_CONFIG_PATH: configPath, + CLAWDBOT_SKIP_CHANNELS: "1", + CLAWDBOT_SKIP_BROWSER_CONTROL_SERVER: "1", + CLAWDBOT_SKIP_CANVAS_HOST: "1", + // Avoid port collisions with other test processes that may also start a gateway server. + CLAWDBOT_BRIDGE_HOST: "127.0.0.1", + CLAWDBOT_BRIDGE_PORT: "0", + }; + let childArgs: string[]; + if (process.platform === "win32") { + const bootstrapPath = path.join(stateDir, "clawdbot-entry-bootstrap.mjs"); + fs.writeFileSync( + bootstrapPath, + [ + 'import { pathToFileURL } from "node:url";', + "const entry = process.env.CLAWDBOT_ENTRY_PATH;", + 'const rawArgs = process.env.CLAWDBOT_ENTRY_ARGS ?? "[]";', + "if (!entry) {", + ' console.error("Missing CLAWDBOT_ENTRY_PATH");', + " process.exit(1);", + "}", + "let entryArgs = [];", + "try {", + " entryArgs = JSON.parse(rawArgs);", + "} catch (err) {", + ' console.error("Failed to parse CLAWDBOT_ENTRY_ARGS", err);', + " process.exit(1);", + "}", + "process.argv = [process.argv[0], entry, ...entryArgs];", + "await import(pathToFileURL(entry).href);", + ].join("\n"), + "utf8", + ); + childArgs = ["--import", "tsx", bootstrapPath]; + env.CLAWDBOT_ENTRY_PATH = path.resolve("src/entry.ts"); + env.CLAWDBOT_ENTRY_ARGS = JSON.stringify(entryArgs); + } else { + childArgs = ["--import", "tsx", "src/entry.ts", ...entryArgs]; + } - child = spawn(nodeBin, args, { + child = spawn(nodeBin, childArgs, { cwd: process.cwd(), - env: { - ...process.env, - CLAWDBOT_NO_RESPAWN: "1", - CLAWDBOT_STATE_DIR: stateDir, - CLAWDBOT_CONFIG_PATH: configPath, - CLAWDBOT_SKIP_CHANNELS: "1", - CLAWDBOT_SKIP_BROWSER_CONTROL_SERVER: "1", - CLAWDBOT_SKIP_CANVAS_HOST: "1", - // Avoid port collisions with other test processes that may also start a gateway server. - CLAWDBOT_BRIDGE_HOST: "127.0.0.1", - CLAWDBOT_BRIDGE_PORT: "0", - }, + env, stdio: ["ignore", "pipe", "pipe"], });