import assert from "node:assert/strict"; import { spawn } from "node:child_process"; import { test } from "node:test"; // Exercise the actual native fixture, suspending its body after resource // acquisition. The external deadline must never be what stops the process. for (const suspension of ["readiness", "HTTP", "cron polling", "reconnect"]) { test( suspension === "reconnect" ? "execution readiness follows the native SDK after an initial disconnect" : `execution timeout releases native resources during ${suspension}`, { timeout: 65_000 }, async () => { const path = new URL( "./fixtures/execution-cleanup-probe.mjs", import.meta.url, ); let child, deadline; try { let output = "", forced = false, ready = false; const env = { ...process.env, CI: "true", FLAREBOT_EXECUTION_CLEANUP_PROBE: suspension, }; delete env.NODE_TEST_CONTEXT; const result = await new Promise((resolve, reject) => { child = spawn( process.execPath, ["--test", "--test-reporter=tap", path.pathname], { detached: true, env, stdio: ["ignore", "pipe", "pipe"], }, ); const armDeadline = (milliseconds) => { clearTimeout(deadline); deadline = setTimeout(() => { forced = true; process.kill(-child.pid, "SIGKILL"); }, milliseconds); }; child.stdout.on("data", (chunk) => { output += chunk; if (!ready && output.includes("PROBE_NATIVE_READY")) { ready = true; armDeadline(18_000); } }); child.stderr.on("data", (chunk) => (output += chunk)); child.once("error", reject); child.once("close", (code, signal) => resolve({ code, signal })); armDeadline(45_000); }); assert.match(output, /PROBE_NATIVE_READY/, output); assert.equal(forced, false, output); if (suspension === "reconnect") { assert.deepEqual(result, { code: 0, signal: null }, output); assert.match(output, /PROBE_INITIAL_IDENTITY_DROPPED/, output); assert.doesNotMatch(output, /RPC error:/, output); return; } assert.deepEqual(result, { code: 1, signal: null }, output); assert.match(output, /testTimeoutFailure/, output); assert.match(output, /Execution fixture interrupted:/, output); assert.match( output, suspension === "cron polling" ? /waiting: Native completion before any incoming request/ : /native client readiness/, output, ); if (suspension === "cron polling") assert.match(output, /two real UTC cron occurrences/, output); } finally { clearTimeout(deadline); if ( child?.pid && child.exitCode === null && child.signalCode === null ) { try { process.kill(-child.pid, "SIGKILL"); } catch (error) { if (error.code !== "ESRCH") throw error; } } } }, ); }