From ad46acf102fc3ce9a026dbdc2f76cda1e4e24dc6 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sun, 2 Aug 2026 23:38:58 -0700 Subject: [PATCH] test(system-agent): reuse CLI backend fixture (#118541) Co-authored-by: Peter Steinberger --- src/system-agent/agent-turn.test.ts | 32 +++---------------- src/system-agent/chat-engine.test.ts | 13 ++++++++ src/system-agent/system-agent.test-helpers.ts | 30 +++++++++++++++++ 3 files changed, 48 insertions(+), 27 deletions(-) diff --git a/src/system-agent/agent-turn.test.ts b/src/system-agent/agent-turn.test.ts index 0b54db6c445d..485b60603418 100644 --- a/src/system-agent/agent-turn.test.ts +++ b/src/system-agent/agent-turn.test.ts @@ -16,6 +16,7 @@ import { SystemAgentInferenceUnavailableError } from "./inference-error.js"; import { resolveSystemAgentConfiguredRouteFromConfig } from "./inference-route.js"; import { createSystemAgentVerifiedInferenceTestFixture, + installSystemAgentClaudeCliBackendTestFixture, installSystemAgentPluginMetadataTestSnapshot, type SystemAgentPluginMetadataTestSnapshot, } from "./system-agent.test-helpers.js"; @@ -62,6 +63,7 @@ vi.mock("../config/config.js", async (importOriginal) => ({ })); const tempDirs: string[] = []; +let restoreCliBackendFixture: (() => void) | undefined; let pluginMetadataSnapshot: SystemAgentPluginMetadataTestSnapshot | undefined; function useTempStateDir(): string { @@ -109,36 +111,12 @@ afterAll(() => { }); beforeEach(() => { - // Core tests install a contract-level selectable backend instead of loading - // a plugin's generated setup artifact from dist/. - cliBackendsTesting.setDepsForTest({ - resolveRuntimeCliBackends: () => [ - { - id: "claude-cli", - pluginId: "anthropic", - modelProvider: "anthropic", - bundleMcp: true, - bundleMcpMode: "claude-config-file", - config: { command: "claude" }, - normalizeConfig: (config, context) => ({ - ...config, - args: [ - ...(config.args ?? []), - "--test-exec-policy", - JSON.stringify(context?.config?.tools?.exec ?? null), - ], - }), - nativeToolMode: "selectable", - toolAvailabilityEnforcement: "execution-args", - sideQuestionToolMode: "disabled", - resolveExecutionArgs: (context) => context.baseArgs, - }, - ], - }); + restoreCliBackendFixture = installSystemAgentClaudeCliBackendTestFixture(); }); afterEach(() => { - cliBackendsTesting.resetDepsForTest(); + restoreCliBackendFixture?.(); + restoreCliBackendFixture = undefined; vi.unstubAllEnvs(); pluginMetadataSnapshot?.rebindForCurrentEnv(); vi.clearAllMocks(); diff --git a/src/system-agent/chat-engine.test.ts b/src/system-agent/chat-engine.test.ts index d63e3fb18299..a542bd0ab49d 100644 --- a/src/system-agent/chat-engine.test.ts +++ b/src/system-agent/chat-engine.test.ts @@ -27,6 +27,7 @@ import { import { verifyConfigAfterSystemAgentWrite } from "./post-write-verification.js"; import { createSystemAgentVerifiedInferenceTestFixture, + installSystemAgentClaudeCliBackendTestFixture, installSystemAgentPluginMetadataTestSnapshot, type SystemAgentPluginMetadataTestSnapshot, } from "./system-agent.test-helpers.js"; @@ -3147,6 +3148,18 @@ describe("SystemAgentChatEngine", () => { }); describe("OpenClaw agent loop backends", () => { + let restoreCliBackendFixture: (() => void) | undefined; + + beforeAll(() => { + // These cases own chat routing and CLI session continuity. Anthropic setup tests own loading + // the generated backend artifact, so keep this integration on the same contract-level fixture. + restoreCliBackendFixture = installSystemAgentClaudeCliBackendTestFixture(); + }); + + afterAll(() => { + restoreCliBackendFixture?.(); + }); + it("runs a configured claude-cli model through the CLI loop with the ring-zero MCP tool", async () => { useTempStateDir(); const config = { diff --git a/src/system-agent/system-agent.test-helpers.ts b/src/system-agent/system-agent.test-helpers.ts index 21a440eb4e33..25738c13893e 100644 --- a/src/system-agent/system-agent.test-helpers.ts +++ b/src/system-agent/system-agent.test-helpers.ts @@ -1,6 +1,7 @@ import { expect } from "vitest"; import { resolveDefaultAgentId } from "../agents/agent-scope.js"; import { resolveCliBackendConfig } from "../agents/cli-backends.js"; +import { testing as cliBackendsTesting } from "../agents/cli-backends.test-support.js"; // OpenClaw test helpers build runtime environments for rescue tests. import { fingerprintAuthProfileOwnerShape, @@ -37,6 +38,35 @@ export type SystemAgentPluginMetadataTestSnapshot = { restore: () => void; }; +/** Install the contract-level selectable CLI backend used by core system-agent tests. */ +export function installSystemAgentClaudeCliBackendTestFixture(): () => void { + cliBackendsTesting.setDepsForTest({ + resolveRuntimeCliBackends: () => [ + { + id: "claude-cli", + pluginId: "anthropic", + modelProvider: "anthropic", + bundleMcp: true, + bundleMcpMode: "claude-config-file", + config: { command: "claude" }, + normalizeConfig: (config, context) => ({ + ...config, + args: [ + ...(config.args ?? []), + "--test-exec-policy", + JSON.stringify(context?.config?.tools?.exec ?? null), + ], + }), + nativeToolMode: "selectable", + toolAvailabilityEnforcement: "execution-args", + sideQuestionToolMode: "disabled", + resolveExecutionArgs: (context) => context.baseArgs, + }, + ], + }); + return () => cliBackendsTesting.resetDepsForTest(); +} + /** Install the process-stable plugin metadata snapshot that the real Gateway owns. */ export function installSystemAgentPluginMetadataTestSnapshot( config: OpenClawConfig = {},