Files
reverse-engineering-anything/tests/boundary/cli/dispatcher.test.ts
T
gensokyo-devandgensokyo-dev b990eb496d perf(cli): load only the registrar a one-shot command needs
Every rea process imported and registered all 52 command registrars
(about 1,900 modules) before running any command. A one-shot command
now loads only the registrar that defines its top-level command.

- src/cli/cliCommandRegistrations.ts holds every registrar in one table,
  grouped by tool family, with its module loader and its call. The
  complete createCli registers the table with static imports, and the
  types check that each entry matches its module.
- src/cli/cliShell.ts owns what does not depend on which commands are
  registered: identity, globals, argument policy, completions and the
  registration builtins. The full and lazy paths share it.
- scripts/record-cli-commands.mjs records, after tsc, which registrar
  defines each top-level command, from the same compiled table. The
  bundle keeps the record as an external dist module.
- cli/cliLoader.ts selects the recorded registrar when a known top-level
  command runs. It loads the complete CLI for help, --schema, --llms,
  completions, builtins, unknown names ("did you mean"), or a missing
  record.
- Process-owned CLI resources register their closers when their module
  loads (cli/cliResources.ts), so the shell no longer imports
  JavaScript-analysis and guidance owners just to close them.

Measured on one pinned core (median of 5 warm runs, base main vs this
change):
- guidance: 1.95 s / 253 MB → 0.42 s / 101 MB
- list-cutter-sessions: 1.97 s → 0.49 s
- inspect-go-binary: 1.95 s → 0.95 s
- setup --dry-run: 2.40 s → 1.88 s
- analyze: 2.39 s → 1.78 s
- --help: 1.99 s → 2.04 s (complete CLI, as before)

Co-Authored-By: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>
2026-11-14 20:25:11 +08:00

693 lines
26 KiB
TypeScript

import { access, copyFile, cp, symlink } from "node:fs/promises";
import { join, resolve } from "node:path";
import { pathToFileURL } from "node:url";
import { describe, expect } from "vitest";
import { unsupportedRuntimeMessage } from "../../../scripts/lib/runtime-admission.mjs";
import { workspaceCliTest } from "../../support/cli/workspaceCliFixture.js";
import type { TestWorkspace } from "../../support/workspace/workspaceFixture.js";
const installCliRuntime = async (
workspace: TestWorkspace,
cliSource: string,
resultOutput = "export const createCliResultOutput = () => undefined;\n",
): Promise<void> => {
await workspace.mkdir("scripts/lib");
await workspace.mkdir("dist");
// The bundle's entrypoints and shared chunks form one emitted runtime. The
// one-shot owner and its output admission stay production code; only the
// command runtime, its recorded registrars, and result output are replaced.
await cp("dist/cli-runtime", workspace.path("dist/cli-runtime"), {
recursive: true,
});
// Production chunks import their packages from the installation root.
await symlink(
resolve("node_modules"),
workspace.path("node_modules"),
"junction",
);
await Promise.all([
copyFile("scripts/rea.mjs", workspace.path("scripts/rea.mjs")),
copyFile(
"scripts/lib/cli-process-environment.mjs",
workspace.path("scripts/lib/cli-process-environment.mjs"),
),
copyFile(
"scripts/lib/runtime-admission.mjs",
workspace.path("scripts/lib/runtime-admission.mjs"),
),
workspace.write(
"package.json",
JSON.stringify({ type: "module", version: "1.0.0" }),
),
workspace.write("dist/cli-runtime/cli.js", cliSource),
workspace.write("dist/cli-runtime/cliResultOutput.js", resultOutput),
// Without a recording, every command loads the replaced complete CLI.
workspace.write(
"dist/recordedCliCommandRegistrations.js",
"export const RECORDED_CLI_COMMAND_REGISTRATIONS = undefined;\n",
),
]);
};
describe("version report dispatch", () => {
workspaceCliTest(
"reports formatted versions without loading analysis commands and preserves preflight errors",
async ({ processes, workspace }) => {
const blocked =
'throw new Error("analysis command runtime was loaded");\n';
await installCliRuntime(workspace, blocked, blocked);
await workspace.write(
"package.json",
JSON.stringify({
type: "module",
name: "rea-fixture",
version: "1.0.0",
}),
);
const invoke = (arguments_: readonly string[], invalidLogLevel = false) =>
processes.run(
process.execPath,
[workspace.path("scripts/rea.mjs"), ...arguments_],
{
cwd: workspace.root,
env: {
...process.env,
REA_LOG_LEVEL: invalidLogLevel ? "invalid-level" : "silent",
},
},
);
const report = await invoke(["--version", "--json"]);
expect(report.exitCode, report.stderr).toBe(0);
expect(JSON.parse(report.stdout)).toMatchObject({
name: "rea-fixture",
version: "1.0.0",
runtime: { id: "node", version: process.versions.node },
});
const invalidOutput = await invoke([
"--version",
"--json",
"--token-limit",
"5",
]);
expect(invalidOutput.exitCode, invalidOutput.stderr).toBe(1);
expect(JSON.parse(invalidOutput.stdout)).toMatchObject({
error: { code: "UNSUPPORTED_OUTPUT_COMBINATION" },
});
const invalidConfiguration = await invoke(["--version", "--json"], true);
expect(invalidConfiguration.exitCode).toBe(1);
expect(invalidConfiguration.stdout).toBe("");
expect(invalidConfiguration.stderr).toContain("REA_LOG_LEVEL");
expect(invalidConfiguration.stderr).not.toContain(
"analysis command runtime was loaded",
);
},
);
});
describe("doctor configuration dispatch", () => {
workspaceCliTest(
"exempts only the production root doctor tokens from invalid logging configuration",
async ({ processes, workspace }) => {
const imported = "CLI command runtime imported";
await installCliRuntime(
workspace,
`process.stderr.write(${JSON.stringify(`${imported}\n`)}); export const closeCliResources = () => {}; export const createCli = () => ({ serve: (args, options) => options.stdout(JSON.stringify({ adapter: "cli", args })) });\n`,
);
const invoke = (args: readonly string[], completion = "") =>
processes.run(
process.execPath,
[workspace.path("scripts/rea.mjs"), ...args],
{
cwd: workspace.root,
env: {
...process.env,
REA_LOG_LEVEL: "invalid-level",
COMPLETE: completion,
},
},
);
for (const [args, normalized] of [
[["doctor"], ["doctor"]],
[
["--json", "doctor"],
["--json", "doctor"],
],
[
["--format=json", "doctor"],
["--format", "json", "doctor"],
],
[
["--filter-output", "healthy", "doctor"],
["--filter-output", "healthy", "doctor"],
],
[
["--token-limit", "10", "doctor"],
["--token-limit", "10", "doctor"],
],
[
["--token-offset", "1", "doctor"],
["--token-offset", "1", "doctor"],
],
] as const) {
const result = await invoke(args);
expect(
result.exitCode,
`${JSON.stringify(args)}: ${result.stderr}`,
).toBe(0);
expect(result.stderr).toBe(`${imported}\n`);
expect(JSON.parse(result.stdout)).toEqual({
adapter: "cli",
args: normalized,
});
}
for (const args of [
["--filter-output", "doctor", "providers"],
["providers", "doctor"],
["--format", "", "doctor"],
["--filter-output", "", "doctor"],
["--token-limit", "", "doctor"],
["--token-offset", "", "doctor"],
["--unknown", "doctor"],
["--version", "doctor"],
["--", "doctor"],
]) {
const result = await invoke(args);
expect(result.exitCode, JSON.stringify(args)).toBe(1);
expect(result.stdout, JSON.stringify(args)).toBe("");
expect(result.stderr, JSON.stringify(args)).toContain("REA_LOG_LEVEL");
expect(result.stderr, JSON.stringify(args)).not.toContain(imported);
}
const completion = await invoke(["doctor"], "bash");
expect(completion.exitCode).toBe(1);
expect(completion.stdout).toBe("");
expect(completion.stderr).toContain("REA_LOG_LEVEL");
expect(completion.stderr).not.toContain(imported);
},
);
});
describe("runtime admission dispatch", () => {
workspaceCliTest(
"refuses an unsupported runtime before loading the compiled runtime",
async ({ processes, workspace }) => {
const marker = workspace.path("compiled-runtime-loaded");
const loaded = `import { writeFileSync } from "node:fs";\nwriteFileSync(${JSON.stringify(marker)}, "loaded");\n`;
const range = ">=999.0.0";
await installCliRuntime(workspace, loaded, loaded);
await workspace.write(
"package.json",
JSON.stringify({
type: "module",
version: "1.0.0",
engines: { node: range },
}),
);
const result = await processes.run(
process.execPath,
[workspace.path("scripts/rea.mjs"), "--version"],
{ cwd: workspace.root },
);
expect(result.exitCode).toBe(1);
expect(result.stdout).toBe("");
expect(result.stderr).toBe(
`${unsupportedRuntimeMessage(range, process.versions.node)}\n`,
);
await expect(access(marker)).rejects.toMatchObject({ code: "ENOENT" });
},
);
workspaceCliTest(
"admits a runtime the package engines field allows",
async ({ processes, workspace }) => {
await installCliRuntime(
workspace,
"export const closeCliResources = () => {}; export const createCli = () => ({ serve: () => { process.stdout.write('runtime-served\\n'); } });\n",
);
const previousMajor = Number(process.versions.node.split(".")[0]) - 1;
for (const range of [
`^${process.versions.node}`,
`>=${previousMajor}.0.0`,
]) {
await workspace.write(
"package.json",
JSON.stringify({
type: "module",
version: "1.0.0",
engines: { node: range },
}),
);
const result = await processes.run(
process.execPath,
[workspace.path("scripts/rea.mjs"), "fixture"],
{ cwd: workspace.root },
);
expect(result.exitCode).toBe(0);
expect(result.stdout).toBe("runtime-served\n");
expect(result.stderr).toBe("");
}
},
);
});
describe("executable dispatcher", () => {
workspaceCliTest(
"does not append recovery output after delegated stdout has begun",
async ({ processes, workspace }) => {
await installCliRuntime(
workspace,
'export const closeCliResources = () => {}; export const createCli = () => ({ serve: (_args, options) => { options.stdout("first document\\n"); throw new RangeError("Invalid string length"); } });\n',
'export const createCliResultOutput = () => ({ handled: false, failed: false, captureSdkOutput: () => false, finishSdkOutput: async () => {}, recoverFormattingFailure: () => { process.stdout.write("unexpected second document"); return true; } });\n',
);
const result = await processes.run(
process.execPath,
[workspace.path("scripts/rea.mjs"), "fixture"],
{ cwd: workspace.root },
);
expect(result.exitCode).toBe(1);
expect(result.stdout).toBe("first document\n");
expect(result.stderr).toContain("RangeError: Invalid string length");
},
);
workspaceCliTest(
"reserves production MCP routing for bare mode arguments",
async ({ processes, workspace }) => {
await installCliRuntime(
workspace,
'export const closeCliResources = () => {}; export const createCli = () => ({ serve: (args) => process.stdout.write(JSON.stringify({ adapter: "cli", args })) });\n',
);
const scripts = workspace.path("scripts");
await workspace.mkdir("dist/mcp");
await Promise.all([
workspace.write(
"dist/mcp/main.js",
'export const runEntrypoint = () => process.stdout.write(JSON.stringify({ adapter: "mcp" })); export const runMcpHttpEntrypoint = (args) => process.stdout.write(JSON.stringify({ adapter: "mcp-http", args }));\n',
),
workspace.write(
"dist/cli-runtime/mcpDoctor.js",
'export const runProductionMcpDoctorCli = (args, input) => Promise.resolve({ output: JSON.stringify({ adapter: "mcp-doctor", args, stdoutIsTerminal: input.stdoutIsTerminal }), exitCode: 0 });\n',
),
]);
const invoke = async (args: readonly string[]): Promise<unknown> => {
const { exitCode, stdout } = await processes.run(
process.execPath,
[join(scripts, "rea.mjs"), ...args],
{ cwd: workspace.root },
);
expect(exitCode).toBe(0);
return JSON.parse(stdout) as unknown;
};
await expect(invoke(["mcp"])).resolves.toEqual({ adapter: "mcp" });
await expect(invoke(["--mcp"])).resolves.toEqual({ adapter: "mcp" });
for (const args of [
["mcp", "doctor", "--json"],
["--json", "mcp", "doctor"],
["mcp", "--json", "doctor"],
["--format", "json", "mcp", "doctor"],
["mcp", "--format=json", "doctor"],
]) {
await expect(invoke(args)).resolves.toEqual({
adapter: "mcp-doctor",
args: args.includes("--json") ? ["--json"] : ["--format", "json"],
stdoutIsTerminal: false,
});
}
await expect(
invoke(["--filter-output", "mcp", "doctor"]),
).resolves.toEqual({
adapter: "cli",
args: ["--filter-output", "mcp", "doctor"],
});
await expect(invoke(["mcp", "add"])).resolves.toEqual({
adapter: "cli",
args: ["mcp", "add"],
});
await expect(invoke(["--mcp", "extra"])).resolves.toEqual({
adapter: "cli",
args: ["--mcp", "extra"],
});
for (const args of [
["mcp", "--http", "--port", "0"],
["--mcp", "--http"],
["mcp", "-h", "--http"],
["mcp", "--host", "127.0.0.1", "--http"],
])
await expect(invoke(args)).resolves.toEqual({
adapter: "mcp-http",
args: args.slice(1),
});
await expect(invoke(["mcp", "doctor", "--http"])).resolves.toEqual({
adapter: "mcp-doctor",
args: ["--http"],
stdoutIsTerminal: false,
});
await expect(invoke(["mcp", "add", "--http"])).resolves.toEqual({
adapter: "cli",
args: ["mcp", "add", "--http"],
});
},
);
workspaceCliTest(
"explains how to restore a missing compiled runtime",
async ({ processes, workspace }) => {
const scripts = await workspace.mkdir("scripts");
await workspace.mkdir("scripts/lib");
await Promise.all([
copyFile("scripts/rea.mjs", join(scripts, "rea.mjs")),
copyFile(
"scripts/lib/cli-process-environment.mjs",
workspace.path("scripts/lib/cli-process-environment.mjs"),
),
copyFile(
"scripts/lib/runtime-admission.mjs",
workspace.path("scripts/lib/runtime-admission.mjs"),
),
workspace.write(
"package.json",
JSON.stringify({ type: "module", version: "1.0.0" }),
),
]);
for (const args of [["mcp"], ["mcp", "doctor"], ["--help"]]) {
const result = await processes.run(
process.execPath,
[join(scripts, "rea.mjs"), ...args],
{ cwd: workspace.root },
);
expect(result).toMatchObject({
exitCode: 1,
stderr: expect.stringContaining(
"REA's compiled runtime is missing. Run `npm ci && npm run build:cached` in",
),
});
expect(result.stderr).toContain(
"If this is an installed package, reinstall rea-agents.",
);
expect(result).toMatchObject({
stderr: expect.not.stringContaining("ERR_MODULE_NOT_FOUND"),
});
}
},
);
});
describe("executable dispatcher JavaScript shutdown", () => {
workspaceCliTest.for([false, true])(
"handles a thrown pipe error while preserving mixed cleanup failure: %s",
async (cleanupFails, { processes, workspace }) => {
await installCliRuntime(
workspace,
`export const closeCliResources = async () => { ${cleanupFails ? 'throw new Error("worker still owned");' : ""} }; export const createCli = () => ({ serve: () => { throw Object.assign(new Error("broken pipe"), { code: "EPIPE" }); } });\n`,
);
const result = await processes.run(
process.execPath,
[workspace.path("scripts/rea.mjs"), "fixture"],
{ cwd: workspace.root },
);
expect(result.exitCode).toBe(cleanupFails ? 1 : 141);
expect(result.stdout).toBe("");
if (cleanupFails) {
expect(result.stderr).toContain("broken pipe");
expect(result.stderr).toContain("worker still owned");
} else expect(result.stderr).toBe("");
},
);
workspaceCliTest.for([
"success",
"cleanup-failure",
"write-failure",
"closed-pipe",
] as const)(
"awaits SDK output after cleanup and retains finalization status: %s",
async (scenario, { processes, workspace }) => {
const cleanupFails = scenario === "cleanup-failure";
const finish =
scenario === "closed-pipe"
? 'process.stdout.emit("error", Object.assign(new Error("broken pipe"), { code: "EPIPE" })); throw new Error("broken pipe");'
: scenario === "write-failure"
? 'throw new Error("final write failed");'
: 'process.stdout.write("complete document\\n");';
await installCliRuntime(
workspace,
`export const closeCliResources = () => { globalThis.cleaned = true; ${cleanupFails ? 'return Promise.reject(new Error("worker still owned"));' : ""} }; export const createCli = () => ({ serve: (_args, options) => { options.stdout("pending document"); options.exit(0); } });\n`,
`export const createCliResultOutput = () => ({ handled: false, failed: false, captureSdkOutput: () => true, finishSdkOutput: async (status) => { if (!globalThis.cleaned) throw new Error("output preceded cleanup"); if (status !== ${cleanupFails ? 1 : 0}) throw new Error("unexpected finish status: " + status); await new Promise(resolve => setTimeout(resolve, 10)); ${finish} } });\n`,
);
const result = await processes.run(
process.execPath,
[workspace.path("scripts/rea.mjs"), "fixture"],
{ cwd: workspace.root },
);
expect(result.exitCode).toBe(
scenario === "closed-pipe" ? 141 : scenario === "success" ? 0 : 1,
);
expect(result.stdout).toBe(
scenario === "success" || cleanupFails ? "complete document\n" : "",
);
expect(result.stderr).not.toContain("output preceded cleanup");
expect(result.stderr).not.toContain("unexpected finish status");
if (cleanupFails) expect(result.stderr).toContain("worker still owned");
if (scenario === "write-failure")
expect(result.stderr).toContain("final write failed");
if (scenario === "closed-pipe") expect(result.stderr).toBe("");
},
);
workspaceCliTest(
"reports a failed JavaScript worker shutdown instead of exiting successfully",
async ({ processes, workspace }) => {
await installCliRuntime(
workspace,
'export const closeCliResources = () => Promise.reject(new Error("worker still owned")); export const createCli = () => ({ serve: (_args, options) => { options.exit(0); } });\n',
);
const result = await processes.run(
process.execPath,
[workspace.path("scripts/rea.mjs"), "fixture"],
{ cwd: workspace.root },
);
expect(result.exitCode).toBe(1);
expect(result.stderr).toContain("worker still owned");
},
);
workspaceCliTest(
"reports a failed JavaScript worker shutdown when exit is called without a code and passes failure status to SDK output",
async ({ processes, workspace }) => {
await installCliRuntime(
workspace,
'export const closeCliResources = () => Promise.reject(new Error("worker still owned")); export const createCli = () => ({ serve: (_args, options) => { options.exit(); } });\n',
'export const createCliResultOutput = () => ({ handled: false, failed: false, captureSdkOutput: () => false, finishSdkOutput: async (status) => { if (status !== 1) throw new Error("unexpected finish status: " + status); } });\n',
);
const result = await processes.run(
process.execPath,
[workspace.path("scripts/rea.mjs"), "fixture"],
{ cwd: workspace.root },
);
expect(result.exitCode).toBe(1);
expect(result.stderr).toContain("worker still owned");
expect(result.stderr).not.toContain("unexpected finish status");
},
);
workspaceCliTest(
"reports a failed SDK finalization when exit is called without a code",
async ({ processes, workspace }) => {
await installCliRuntime(
workspace,
"export const closeCliResources = () => Promise.resolve(); export const createCli = () => ({ serve: (_args, options) => { options.exit(); } });\n",
'export const createCliResultOutput = () => ({ handled: false, failed: false, captureSdkOutput: () => false, finishSdkOutput: async (status) => { if (status !== 0) throw new Error("unexpected finish status: " + status); throw new Error("SDK finalization failed"); } });\n',
);
const result = await processes.run(
process.execPath,
[workspace.path("scripts/rea.mjs"), "fixture"],
{ cwd: workspace.root },
);
expect(result.exitCode).toBe(1);
expect(result.stderr).toContain("SDK finalization failed");
expect(result.stderr).not.toContain("unexpected finish status");
},
);
workspaceCliTest(
"keeps the closed-pipe status when JavaScript worker shutdown fails",
async ({ processes, workspace }) => {
await installCliRuntime(
workspace,
'export const closeCliResources = () => Promise.reject(new Error("worker still owned")); export const createCli = () => ({ serve: (_args, options) => { process.stdout.emit("error", Object.assign(new Error("broken pipe"), { code: "EPIPE" })); options.exit(0); } });\n',
);
const result = await processes.run(
process.execPath,
[workspace.path("scripts/rea.mjs"), "fixture"],
{ cwd: workspace.root },
);
expect(result.exitCode).toBe(141);
expect(result.stderr).toContain("worker still owned");
},
);
});
describe("project-scoped Amp registration dispatch", () => {
const runtimeReached = "cli-runtime reached";
const installGuardRuntime = async (workspace: TestWorkspace) =>
installCliRuntime(
workspace,
`export const closeCliJavaScriptResources = () => {}; export const createCli = () => ({ serve: (_args, options) => { options.stdout(${JSON.stringify(runtimeReached)}); } });\n`,
);
workspaceCliTest(
"rejects a real project-scoped Amp registration before the CLI runtime loads",
async ({ processes, workspace }) => {
await installGuardRuntime(workspace);
const result = await processes.run(
process.execPath,
[
workspace.path("scripts/rea.mjs"),
"mcp",
"add",
"--no-global",
"--agent",
"amp",
],
{
cwd: workspace.root,
env: { ...process.env, REA_LOG_LEVEL: "silent" },
},
);
expect(result.exitCode, result.stderr).toBe(1);
expect(result.stdout).toBe("");
expect(result.stderr.length).toBeGreaterThan(0);
expect(result.stdout).not.toContain(runtimeReached);
},
);
workspaceCliTest(
"passes discovery requests through to the CLI runtime",
async ({ processes, workspace }) => {
await installGuardRuntime(workspace);
const result = await processes.run(
process.execPath,
[
workspace.path("scripts/rea.mjs"),
"mcp",
"add",
"--no-global",
"--agent",
"amp",
"--help",
],
{
cwd: workspace.root,
env: { ...process.env, REA_LOG_LEVEL: "silent" },
},
);
expect(result.exitCode, result.stderr).toBe(0);
expect(result.stdout).toContain(runtimeReached);
expect(result.stderr).toBe("");
},
);
workspaceCliTest(
"renders the Amp scope refusal as a structured document when a structured format is requested",
async ({ processes, workspace }) => {
await installGuardRuntime(workspace);
const invoke = (arguments_: readonly string[]) =>
processes.run(
process.execPath,
[workspace.path("scripts/rea.mjs"), ...arguments_],
{
cwd: workspace.root,
env: { ...process.env, REA_LOG_LEVEL: "silent" },
},
);
const registration = ["mcp", "add", "--no-global", "--agent", "amp"];
const jsonLines = await invoke([...registration, "--format", "jsonl"]);
expect(jsonLines.exitCode, jsonLines.stderr).toBe(1);
expect(JSON.parse(jsonLines.stdout)).toEqual({
$rea: "jsonl/v1",
type: "error",
ok: false,
result: {
ok: false,
error: {
code: "AMP_PROJECT_SCOPE_UNSUPPORTED",
message: expect.any(String),
},
},
});
expect(jsonLines.stdout).not.toContain(runtimeReached);
expect(jsonLines.stderr).toBe("");
const json = await invoke([...registration, "--json"]);
expect(json.exitCode, json.stderr).toBe(1);
expect(JSON.parse(json.stdout)).toEqual({
ok: false,
error: {
code: "AMP_PROJECT_SCOPE_UNSUPPORTED",
message: expect.any(String),
},
});
expect(json.stdout).not.toContain(runtimeReached);
expect(json.stderr).toBe("");
const yaml = await invoke([...registration, "--format", "yaml"]);
expect(yaml.exitCode, yaml.stderr).toBe(1);
expect(yaml.stdout.split("\n").slice(0, 3)).toEqual([
"ok: false",
"error:",
" code: AMP_PROJECT_SCOPE_UNSUPPORTED",
]);
expect(yaml.stdout.split("\n")[3]).toMatch(/^ {2}message: /u);
expect(yaml.stdout).not.toContain(runtimeReached);
expect(yaml.stderr).toBe("");
const plain = await invoke(registration);
expect(plain.exitCode, plain.stderr).toBe(1);
expect(plain.stdout).toBe("");
expect(plain.stderr.length).toBeGreaterThan(0);
expect(plain.stdout).not.toContain(runtimeReached);
},
);
});
describe("executable dispatcher exit", () => {
workspaceCliTest(
"exits with the requested status once the command's handles close",
async ({ processes, workspace }) => {
// The interval stops after the exit request: the loop drains, the
// process does not wait for the forced-exit grace, and the status holds.
await installCliRuntime(
workspace,
"export const closeCliResources = () => {}; export const createCli = () => ({ serve: (_args, options) => { const timer = setInterval(() => {}, 50); options.exit(3); setTimeout(() => clearInterval(timer), 100); } });\n",
);
const result = await processes.run(
process.execPath,
[workspace.path("scripts/rea.mjs"), "fixture"],
{ cwd: workspace.root },
);
expect(result.exitCode, result.stderr).toBe(3);
},
);
workspaceCliTest(
"forces the requested exit when a command leaves a handle open",
async ({ processes, workspace }) => {
await installCliRuntime(
workspace,
"export const closeCliResources = () => {}; export const createCli = () => ({ serve: (_args, options) => { setInterval(() => {}, 1_000); options.exit(4); } });\n",
);
const result = await processes.run(
process.execPath,
[workspace.path("scripts/rea.mjs"), "fixture"],
{ cwd: workspace.root },
);
expect(result.exitCode, result.stderr).toBe(4);
expect(result.signal).toBeNull();
},
);
});