Skip to content

Commit 239087d

Browse files
steipeteampagent
andauthored
improve: speed up CLI spawn tests (#122891)
* test(agents): trim cli spawn test imports * test(agents): distinguish prerelease policies --------- Co-authored-by: Amp <[email protected]>
1 parent 326501f commit 239087d

2 files changed

Lines changed: 83 additions & 129 deletions

File tree

‎src/agents/cli-runner.spawn.test.ts‎

Lines changed: 83 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,6 @@ import {
2323
} from "../logging/diagnostic-run-activity.js";
2424
import type { getProcessSupervisor } from "../process/supervisor/index.js";
2525
import { createTestAdmittedRunContext } from "./admitted-run-context.test-support.js";
26-
import {
27-
registerExecApprovalRequestForHostOrThrow,
28-
resolveRegisteredExecApprovalDecision,
29-
} from "./bash-tools.exec-approval-request.js";
30-
import {
31-
makeBootstrapWarn as realMakeBootstrapWarn,
32-
resolveBootstrapContextForRun as realResolveBootstrapContextForRun,
33-
} from "./bootstrap-files.js";
3426
import {
3527
buildClaudeLiveRunContext,
3628
buildPreparedCliRunContext,
@@ -45,12 +37,6 @@ import {
4537
requireRecord,
4638
requireRegexMatch,
4739
} from "./cli-runner.test-helpers.js";
48-
import {
49-
createManagedRun,
50-
mockSuccessfulCliRun,
51-
restoreCliRunnerPrepareTestDeps,
52-
supervisorSpawnMock,
53-
} from "./cli-runner.test-support.js";
5440
import { resetClaudeLiveSessionsForTest } from "./cli-runner/claude-live-session.test-support.js";
5541
import {
5642
attachCliMessagingDeliveryEvidence,
@@ -60,13 +46,20 @@ import { executePreparedCliRun } from "./cli-runner/execute.js";
6046
import {
6147
buildCliEnvAuthLog,
6248
buildCliExecLogLine,
49+
createManagedRun,
6350
setCliRunnerExecuteTestDeps,
51+
supervisorSpawnMock,
6452
} from "./cli-runner/execute.test-support.js";
6553
import { buildCliAgentSystemPrompt, writeCliSystemPromptFile } from "./cli-runner/helpers.js";
6654
import { cliBackendLog, formatCliBackendOutputDigest } from "./cli-runner/log.js";
67-
import { setCliRunnerPrepareTestDeps } from "./cli-runner/prepare.test-support.js";
6855
import type { PreparedCliRunContext } from "./cli-runner/types.js";
6956

57+
// Approval behavior is injected below; loading its gateway/tool graph here is incidental.
58+
vi.mock("./bash-tools.exec-approval-request.js", () => ({
59+
registerExecApprovalRequestForHostOrThrow: vi.fn(),
60+
resolveRegisteredExecApprovalDecision: vi.fn(),
61+
}));
62+
7063
// Gateway unit coverage owns quiet-admission timing. These spawn cases only
7164
// need to drain calls already in flight, so skip the repeated 250 ms quiet window.
7265
vi.mock("../gateway/mcp-http.loopback-runtime.js", async (importOriginal) => {
@@ -97,12 +90,15 @@ beforeEach(() => {
9790
resetDiagnosticRunActivityForTest();
9891
startDiagnosticRunActivityTracking();
9992
resetClaudeLiveSessionsForTest();
100-
restoreCliRunnerPrepareTestDeps();
10193
setCliRunnerExecuteTestDeps({
10294
writeCliSystemPromptFile,
10395
invokeNodeClaudeCliRun,
104-
registerExecApprovalRequestForHostOrThrow,
105-
resolveRegisteredExecApprovalDecision,
96+
registerExecApprovalRequestForHostOrThrow: async () => {
97+
throw new Error("unexpected exec approval registration");
98+
},
99+
resolveRegisteredExecApprovalDecision: async () => {
100+
throw new Error("unexpected exec approval resolution");
101+
},
106102
});
107103
supervisorSpawnMock.mockClear();
108104
});
@@ -121,6 +117,21 @@ const GEMINI_OK_JSONL = `${[
121117
].join("\n")}\n`;
122118
const tempDirs = useAutoCleanupTempDirTracker(afterEach);
123119

120+
function mockSuccessfulCliRun(stdout = "ok") {
121+
supervisorSpawnMock.mockResolvedValueOnce(
122+
createManagedRun({
123+
reason: "exit",
124+
exitCode: 0,
125+
exitSignal: null,
126+
durationMs: 50,
127+
stdout,
128+
stderr: "",
129+
timedOut: false,
130+
noOutputTimedOut: false,
131+
}),
132+
);
133+
}
134+
124135
async function createCliPackageFixture(version: string): Promise<{
125136
root: string;
126137
entrypoint: string;
@@ -1316,47 +1327,63 @@ describe("runCliAgent spawn path", () => {
13161327
});
13171328

13181329
it.each([
1319-
{ version: "0.40.0-preview.2", admitted: false },
1320-
{ version: "0.40.0-preview.3", admitted: true },
1321-
{ version: "0.41.0-nightly.20260423.gd1c91f526", admitted: false },
1322-
{ version: "0.41.0-nightly.20260427.g42587de73", admitted: true },
1323-
{ version: "0.53.0-beta.0", admitted: false },
1324-
])("applies the exact tool-availability policy to $version", async ({ version, admitted }) => {
1325-
const fixture = await createCliPackageFixture(version);
1326-
const run = () =>
1327-
executePreparedCliRun(
1328-
buildPreparedCliRunContext({
1329-
provider: "google-gemini-cli",
1330-
model: "gemini-3.1-pro-preview",
1331-
backend: { command: fixture.entrypoint },
1332-
cliToolAvailability: { native: [], openClaw: [] },
1333-
runtimeArtifact: {
1334-
kind: "bundled-package-tree",
1335-
packageName: "@fixture/versioned-cli",
1336-
entrypoint: "command",
1337-
exactToolAvailabilityVersionPolicy: {
1338-
stableMinimum: "0.39.1",
1339-
prereleaseMinimums: {
1340-
preview: "0.40.0-preview.3",
1341-
nightly: "0.41.0-nightly.20260427.g42587de73",
1330+
{
1331+
version: "0.40.0-preview.2",
1332+
admitted: false,
1333+
expectedError: "requires >=0.40.0-preview.3; found 0.40.0-preview.2",
1334+
},
1335+
{
1336+
version: "0.41.0-nightly.20260427.g42587de73",
1337+
admitted: true,
1338+
stableMinimum: "99.0.0",
1339+
expectedError: undefined,
1340+
},
1341+
{
1342+
version: "0.53.0-beta.0",
1343+
admitted: false,
1344+
expectedError: "unsupported release line; found 0.53.0-beta.0",
1345+
},
1346+
])(
1347+
"applies the exact tool-availability policy to $version",
1348+
async ({ version, admitted, stableMinimum = "0.39.1", expectedError }) => {
1349+
const fixture = await createCliPackageFixture(version);
1350+
const run = () =>
1351+
executePreparedCliRun(
1352+
buildPreparedCliRunContext({
1353+
provider: "google-gemini-cli",
1354+
model: "gemini-3.1-pro-preview",
1355+
backend: { command: fixture.entrypoint },
1356+
cliToolAvailability: { native: [], openClaw: [] },
1357+
runtimeArtifact: {
1358+
kind: "bundled-package-tree",
1359+
packageName: "@fixture/versioned-cli",
1360+
entrypoint: "command",
1361+
exactToolAvailabilityVersionPolicy: {
1362+
stableMinimum,
1363+
prereleaseMinimums: {
1364+
preview: "0.40.0-preview.3",
1365+
nightly: "0.41.0-nightly.20260427.g42587de73",
1366+
},
13421367
},
13431368
},
1344-
},
1345-
}),
1346-
);
1347-
try {
1348-
if (admitted) {
1349-
mockSuccessfulCliRun(GEMINI_OK_JSONL);
1350-
await expect(run()).resolves.toBeDefined();
1351-
expect(supervisorSpawnMock).toHaveBeenCalledOnce();
1352-
} else {
1353-
await expect(run()).rejects.toThrow("requires a supported package version");
1354-
expect(supervisorSpawnMock).not.toHaveBeenCalled();
1369+
}),
1370+
);
1371+
try {
1372+
if (admitted) {
1373+
mockSuccessfulCliRun(GEMINI_OK_JSONL);
1374+
await expect(run()).resolves.toBeDefined();
1375+
expect(supervisorSpawnMock).toHaveBeenCalledOnce();
1376+
} else {
1377+
await expect(run()).rejects.toThrow(
1378+
expectDefined(expectedError, "rejected version error"),
1379+
);
1380+
expect(supervisorSpawnMock).not.toHaveBeenCalled();
1381+
}
1382+
} finally {
1383+
await fs.rm(fixture.root, { recursive: true, force: true });
13551384
}
1356-
} finally {
1357-
await fs.rm(fixture.root, { recursive: true, force: true });
1358-
}
1359-
});
1385+
},
1386+
);
13601387

13611388
it("does not apply the exact tool-availability version floor to normal agent turns", async () => {
13621389
const fixture = await createCliPackageFixture("0.39.0");
@@ -2627,61 +2654,5 @@ describe("runCliAgent spawn path", () => {
26272654
expect(promptCarrier).toContain("- AGENTS.md: 200 raw -> 20 injected");
26282655
expect(promptCarrier).toContain("hi");
26292656
});
2630-
2631-
it("loads workspace bootstrap files into the Claude CLI system prompt", async () => {
2632-
const workspaceDir = await fs.mkdtemp(
2633-
path.join(os.tmpdir(), "openclaw-cli-bootstrap-context-"),
2634-
);
2635-
2636-
await fs.writeFile(
2637-
path.join(workspaceDir, "AGENTS.md"),
2638-
[
2639-
"# AGENTS.md",
2640-
"",
2641-
"Read SOUL.md and IDENTITY.md before replying.",
2642-
"Use the injected workspace bootstrap files as standing instructions.",
2643-
].join("\n"),
2644-
"utf-8",
2645-
);
2646-
await fs.writeFile(path.join(workspaceDir, "SOUL.md"), "SOUL-SECRET\n", "utf-8");
2647-
await fs.writeFile(path.join(workspaceDir, "IDENTITY.md"), "IDENTITY-SECRET\n", "utf-8");
2648-
await fs.writeFile(path.join(workspaceDir, "USER.md"), "USER-SECRET\n", "utf-8");
2649-
2650-
setCliRunnerPrepareTestDeps({
2651-
makeBootstrapWarn: realMakeBootstrapWarn,
2652-
resolveBootstrapContextForRun: realResolveBootstrapContextForRun,
2653-
});
2654-
2655-
try {
2656-
const { contextFiles } = await realResolveBootstrapContextForRun({
2657-
workspaceDir,
2658-
});
2659-
const allArgs = buildCliAgentSystemPrompt({
2660-
workspaceDir,
2661-
modelDisplay: "claude-cli/sonnet",
2662-
contextFiles,
2663-
tools: [],
2664-
});
2665-
const agentsPath = path.join(workspaceDir, "AGENTS.md");
2666-
const soulPath = path.join(workspaceDir, "SOUL.md");
2667-
const identityPath = path.join(workspaceDir, "IDENTITY.md");
2668-
const userPath = path.join(workspaceDir, "USER.md");
2669-
expect(allArgs).toContain("# Project Context");
2670-
expect(allArgs).toContain(`## ${agentsPath}`);
2671-
expect(allArgs).toContain("Read SOUL.md and IDENTITY.md before replying.");
2672-
expect(allArgs).toContain(`## ${soulPath}`);
2673-
expect(allArgs).toContain("SOUL-SECRET");
2674-
expect(allArgs).toContain(
2675-
"SOUL.md: persona/tone. Follow it unless higher-priority instructions override.",
2676-
);
2677-
expect(allArgs).toContain(`## ${identityPath}`);
2678-
expect(allArgs).toContain("IDENTITY-SECRET");
2679-
expect(allArgs).toContain(`## ${userPath}`);
2680-
expect(allArgs).toContain("USER-SECRET");
2681-
} finally {
2682-
await fs.rm(workspaceDir, { recursive: true, force: true });
2683-
restoreCliRunnerPrepareTestDeps();
2684-
}
2685-
});
26862657
});
26872658
/* oxlint-disable max-lines -- TODO: split this grandfathered oversized file. */

‎src/agents/cli-runner.test-support.ts‎

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import type { Mock } from "vitest";
33
import { beforeEach, vi } from "vitest";
44
import { getClaudeGeneration } from "./cli-runner/claude-live-registry.js";
5-
import { createManagedRun, supervisorSpawnMock } from "./cli-runner/execute.test-support.js";
65
import { setCliRunnerPrepareTestDeps } from "./cli-runner/prepare.test-support.js";
76
import type { EmbeddedContextFile } from "./embedded-agent-helpers.js";
87
import type { WorkspaceBootstrapFile } from "./workspace.js";
@@ -41,22 +40,6 @@ setCliRunnerPrepareTestDeps({
4140
resolveOpenClawReferencePaths: async () => ({ docsPath: null, sourcePath: null }),
4241
});
4342

44-
/** Queue one successful CLI supervisor run. */
45-
export function mockSuccessfulCliRun(stdout = "ok") {
46-
supervisorSpawnMock.mockResolvedValueOnce(
47-
createManagedRun({
48-
reason: "exit",
49-
exitCode: 0,
50-
exitSignal: null,
51-
durationMs: 50,
52-
stdout,
53-
stderr: "",
54-
timedOut: false,
55-
noOutputTimedOut: false,
56-
}),
57-
);
58-
}
59-
6043
/** Restore prepare-time CLI runner test dependencies after a test overrides them. */
6144
export function restoreCliRunnerPrepareTestDeps() {
6245
setCliRunnerPrepareTestDeps({

0 commit comments

Comments
 (0)