openclaw/test/cli-json-stdout.test-support.ts
Peter Steinberger 87af5763bb
fix(test): apply Node shutdown policy to built CLI JSON fixtures (#163630)
runBuiltCli spawned the built CLI with Node 24 defaults, so concurrent
Sparkplug and Maglev stayed enabled in the child. Under CPU contention
process.exit can then deadlock joining a background compiler that waits
for a main-thread GC; spawnSync times out and reports status null after
the CLI already printed its correct output. Gateway e2e shard 4/4
reproduced this in test/cli-json-stdout.gateway.e2e.test.ts.

Prepend the shared resolveVitestNodeArgs policy (skipped under Bun), as
runCliProcessChild already does, and assert in the gateway suite's
preload that children run without concurrent Sparkplug.
2026-10-02 12:26:40 -07:00

34 lines
1.2 KiB
TypeScript

import { spawnSync } from "node:child_process";
import path from "node:path";
import { resolveVitestNodeArgs } from "../scripts/lib/vitest-process-env.mts";
export function runBuiltCli(
tempHome: string,
args: string[],
envOverrides: NodeJS.ProcessEnv = {},
options: { inheritEnvironment?: boolean; execArgv?: string[] } = {},
) {
const env: NodeJS.ProcessEnv = {
...(options.inheritEnvironment === false ? { PATH: process.env.PATH } : process.env),
HOME: tempHome,
USERPROFILE: tempHome,
OPENCLAW_TEST_FAST: "1",
};
delete env.OPENCLAW_HOME;
delete env.OPENCLAW_STATE_DIR;
delete env.OPENCLAW_CONFIG_PATH;
delete env.VITEST;
Object.assign(env, envOverrides);
const entry = path.resolve(process.cwd(), "openclaw.mjs");
// Without the runner's V8 policy, process.exit can deadlock joining a background
// compiler that waits for main-thread GC; spawnSync then reports status null.
const nodeArgs = process.versions.bun ? [] : resolveVitestNodeArgs(env);
return spawnSync(process.execPath, [...nodeArgs, ...(options.execArgv ?? []), entry, ...args], {
cwd: process.cwd(),
env,
encoding: "utf8",
maxBuffer: 10 * 1024 * 1024,
timeout: 60_000,
});
}