Skip to content

Commit 7fc23f9

Browse files
steipetePeter Steinberger
andauthored
improve(tests): reuse verified inference fixtures (#118420)
* test(system-agent): reuse verified route fixtures * test(system-agent): drop stale fixture awaits --------- Co-authored-by: Peter Steinberger <[email protected]>
1 parent 40c641a commit 7fc23f9

2 files changed

Lines changed: 140 additions & 37 deletions

File tree

‎src/system-agent/assistant.configured.test.ts‎

Lines changed: 72 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Configured OpenClaw assistant tests cover route-owned, tool-free planning.
2-
import { describe, expect, it, vi } from "vitest";
2+
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
3+
import { testing as cliBackendsTesting } from "../agents/cli-backends.test-support.js";
34
import type { RunCliAgentParams } from "../agents/cli-runner/types.js";
45
import { fingerprintResolvedProviderAuth } from "../agents/execution-auth-binding.js";
56
import type { OpenClawConfig } from "../config/types.openclaw.js";
@@ -8,7 +9,14 @@ import { SystemAgentInferenceUnavailableError } from "./inference-error.js";
89
import { resolveSystemAgentConfiguredRouteFromConfig } from "./inference-route.js";
910
import type { SystemAgentOverview } from "./overview.js";
1011
import { createSystemAgentVerifiedInferenceTestFixture } from "./system-agent.test-helpers.js";
11-
import { createSystemAgentVerifiedInferenceBinding } from "./verified-inference.js";
12+
import {
13+
createSystemAgentVerifiedInferenceBinding,
14+
type SystemAgentVerifiedInferenceBinding,
15+
} from "./verified-inference.js";
16+
17+
const inferenceMocks = vi.hoisted(() => ({
18+
fastBindings: new WeakSet<object>(),
19+
}));
1220

1321
vi.mock("../plugins/providers.js", async (importOriginal) => ({
1422
...(await importOriginal<typeof import("../plugins/providers.js")>()),
@@ -23,6 +31,52 @@ vi.mock("../agents/harness/runtime-plugin.js", async (importOriginal) => ({
2331
),
2432
}));
2533

34+
vi.mock("./verified-inference.js", async (importOriginal) => {
35+
const actual = await importOriginal<typeof import("./verified-inference.js")>();
36+
return {
37+
...actual,
38+
resolveSystemAgentVerifiedInferenceRoute: (
39+
...args: Parameters<typeof actual.resolveSystemAgentVerifiedInferenceRoute>
40+
) => {
41+
// These cases own planner dispatch, not repeated inference ownership checks.
42+
// Unmarked bindings still exercise the real resolver for every drift boundary.
43+
if (inferenceMocks.fastBindings.has(args[0])) {
44+
return Promise.resolve(args[0].execution);
45+
}
46+
return actual.resolveSystemAgentVerifiedInferenceRoute(...args);
47+
},
48+
};
49+
});
50+
51+
function useFastVerifiedInference(
52+
binding: SystemAgentVerifiedInferenceBinding,
53+
): SystemAgentVerifiedInferenceBinding {
54+
inferenceMocks.fastBindings.add(binding);
55+
return binding;
56+
}
57+
58+
beforeAll(() => {
59+
cliBackendsTesting.setDepsForTest({
60+
resolvePluginSetupCliBackend: () => undefined,
61+
resolvePluginSetupRegistry: () => ({ cliBackends: [] }) as never,
62+
resolveRuntimeCliBackends: () => [
63+
{
64+
id: "claude-cli",
65+
pluginId: "anthropic",
66+
modelProvider: "anthropic",
67+
bundleMcp: true,
68+
bundleMcpMode: "claude-config-file",
69+
config: { command: "claude" },
70+
sideQuestionToolMode: "disabled",
71+
},
72+
],
73+
});
74+
});
75+
76+
afterAll(() => {
77+
cliBackendsTesting.resetDepsForTest();
78+
});
79+
2680
function overview(defaultModel?: string): SystemAgentOverview {
2781
return {
2882
config: {
@@ -121,17 +175,19 @@ describe("OpenClaw configured-model planner", () => {
121175
if (!authFingerprint) {
122176
throw new Error("missing test auth fingerprint");
123177
}
124-
const binding = await createSystemAgentVerifiedInferenceBinding({
125-
configuredRoute,
126-
executionRoute: { ...configuredRoute, authProfileId: "openai:p2" },
127-
auth: {
128-
authProfileId: "openai:p2",
129-
authFingerprint,
130-
modelId: configuredRoute.model,
131-
modelApi: "openai-responses",
132-
},
133-
deps: authDeps,
134-
});
178+
const binding = useFastVerifiedInference(
179+
await createSystemAgentVerifiedInferenceBinding({
180+
configuredRoute,
181+
executionRoute: { ...configuredRoute, authProfileId: "openai:p2" },
182+
auth: {
183+
authProfileId: "openai:p2",
184+
authFingerprint,
185+
modelId: configuredRoute.model,
186+
modelApi: "openai-responses",
187+
},
188+
deps: authDeps,
189+
}),
190+
);
135191
const runEmbeddedAgent = vi.fn(async () => ({
136192
payloads: [{ text: '{"reply":"Ready.","command":"gateway status"}' }],
137193
}));
@@ -255,6 +311,7 @@ describe("OpenClaw configured-model planner", () => {
255311
}));
256312
const removeTempDir = vi.fn(async () => {});
257313
const { binding, deps } = await createSystemAgentVerifiedInferenceTestFixture(config);
314+
useFastVerifiedInference(binding);
258315

259316
const result = await planSystemAgentCommandWithConfiguredModel({
260317
input: "please finish setup",
@@ -311,6 +368,7 @@ describe("OpenClaw configured-model planner", () => {
311368
payloads: [{ text: '{"reply":"Ready.","command":"gateway status"}' }],
312369
}));
313370
const { binding, deps } = await createSystemAgentVerifiedInferenceTestFixture(config);
371+
useFastVerifiedInference(binding);
314372

315373
const result = await planSystemAgentCommandWithConfiguredModel({
316374
input: "is the gateway healthy",
@@ -367,6 +425,7 @@ describe("OpenClaw configured-model planner", () => {
367425
},
368426
} satisfies OpenClawConfig;
369427
const { binding, deps } = await createSystemAgentVerifiedInferenceTestFixture(config);
428+
useFastVerifiedInference(binding);
370429
const runEmbeddedAgent = vi.fn(async () => ({
371430
payloads: [{ text: '{"reply":"Ready.","command":"gateway status"}' }],
372431
}));

‎src/system-agent/system-agent.test.ts‎

Lines changed: 68 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// OpenClaw tests cover main rescue and audit command behavior.
2-
import { describe, expect, it, vi } from "vitest";
2+
import { beforeAll, describe, expect, it, vi } from "vitest";
33
import type { OpenClawConfig } from "../config/types.openclaw.js";
44
import { SystemAgentInferenceUnavailableError } from "./inference-error.js";
55
import type { SystemAgentCommandDeps } from "./operations.js";
@@ -9,13 +9,35 @@ import {
99
createSystemAgentTestRuntime,
1010
createSystemAgentVerifiedInferenceTestFixture,
1111
} from "./system-agent.test-helpers.js";
12+
import type { SystemAgentVerifiedInferenceBinding } from "./verified-inference.js";
13+
14+
const inferenceMocks = vi.hoisted(() => ({
15+
sharedVerifiedInference: undefined as SystemAgentVerifiedInferenceBinding | undefined,
16+
}));
1217

1318
vi.mock("../plugins/providers.js", async (importOriginal) => ({
1419
...(await importOriginal<typeof import("../plugins/providers.js")>()),
1520
resolveOwningPluginIdsForModelRefs: vi.fn(() => []),
1621
resolveOwningPluginIdsForProviderRef: vi.fn(() => []),
1722
}));
1823

24+
vi.mock("./verified-inference.js", async (importOriginal) => {
25+
const actual = await importOriginal<typeof import("./verified-inference.js")>();
26+
return {
27+
...actual,
28+
resolveSystemAgentVerifiedInferenceRoute: (
29+
...args: Parameters<typeof actual.resolveSystemAgentVerifiedInferenceRoute>
30+
) => {
31+
// Ordinary runner cases share one immutable verified fixture. Distinct
32+
// bindings still use the real resolver for route and apply-boundary drift.
33+
if (args[0] === inferenceMocks.sharedVerifiedInference) {
34+
return Promise.resolve(args[0].execution);
35+
}
36+
return actual.resolveSystemAgentVerifiedInferenceRoute(...args);
37+
},
38+
};
39+
});
40+
1941
const overview: SystemAgentOverview = {
2042
defaultAgentId: "main",
2143
defaultModel: "openai/gpt-5.5",
@@ -71,10 +93,22 @@ function configSnapshot(config: OpenClawConfig) {
7193
};
7294
}
7395

74-
async function createVerifiedRunOptions(deps: SystemAgentCommandDeps = {}) {
75-
const fixture = await createSystemAgentVerifiedInferenceTestFixture(verifiedConfig);
96+
let sharedVerifiedFixture: Awaited<
97+
ReturnType<typeof createSystemAgentVerifiedInferenceTestFixture>
98+
>;
99+
100+
beforeAll(async () => {
101+
sharedVerifiedFixture = await createSystemAgentVerifiedInferenceTestFixture(verifiedConfig);
102+
inferenceMocks.sharedVerifiedInference = sharedVerifiedFixture.binding;
103+
});
104+
105+
function createVerifiedRunOptions(
106+
deps: SystemAgentCommandDeps = {},
107+
options: { revalidate?: boolean } = {},
108+
) {
109+
const fixture = sharedVerifiedFixture;
76110
return {
77-
verifiedInference: fixture.binding,
111+
verifiedInference: options.revalidate ? { ...fixture.binding } : fixture.binding,
78112
deps: {
79113
...fixture.deps,
80114
readConfigFileSnapshot: vi.fn(async () => configSnapshot(verifiedConfig)) as never,
@@ -123,7 +157,7 @@ describe("runSystemAgent", () => {
123157
const { runtime, lines } = createSystemAgentTestRuntime();
124158
let runGatewayRestartCalls = 0;
125159
let onReadyCalls = 0;
126-
const verified = await createVerifiedRunOptions({
160+
const verified = createVerifiedRunOptions({
127161
runGatewayRestart: async () => {
128162
runGatewayRestartCalls += 1;
129163
},
@@ -166,10 +200,13 @@ describe("runSystemAgent", () => {
166200
.mockResolvedValueOnce(configSnapshot(verifiedConfig))
167201
.mockResolvedValue(configSnapshot(changedConfig));
168202
const runGatewayRestart = vi.fn(async () => {});
169-
const verified = await createVerifiedRunOptions({
170-
readConfigFileSnapshot: readConfigFileSnapshot as never,
171-
runGatewayRestart,
172-
});
203+
const verified = createVerifiedRunOptions(
204+
{
205+
readConfigFileSnapshot: readConfigFileSnapshot as never,
206+
runGatewayRestart,
207+
},
208+
{ revalidate: true },
209+
);
173210

174211
await expect(
175212
runSystemAgent(
@@ -195,12 +232,16 @@ describe("runSystemAgent", () => {
195232
.fn()
196233
.mockResolvedValueOnce(configSnapshot(verifiedConfig))
197234
.mockResolvedValueOnce(configSnapshot(verifiedConfig))
235+
.mockResolvedValueOnce(configSnapshot(verifiedConfig))
198236
.mockResolvedValue(configSnapshot(changedConfig));
199237
const runGatewayRestart = vi.fn(async () => {});
200-
const verified = await createVerifiedRunOptions({
201-
readConfigFileSnapshot: readConfigFileSnapshot as never,
202-
runGatewayRestart,
203-
});
238+
const verified = createVerifiedRunOptions(
239+
{
240+
readConfigFileSnapshot: readConfigFileSnapshot as never,
241+
runGatewayRestart,
242+
},
243+
{ revalidate: true },
244+
);
204245

205246
await expect(
206247
runSystemAgent(
@@ -215,15 +256,15 @@ describe("runSystemAgent", () => {
215256
),
216257
).rejects.toBeInstanceOf(SystemAgentInferenceUnavailableError);
217258

218-
expect(readConfigFileSnapshot).toHaveBeenCalledTimes(3);
259+
expect(readConfigFileSnapshot).toHaveBeenCalledTimes(4);
219260
expect(runGatewayRestart).not.toHaveBeenCalled();
220261
});
221262

222263
it("keeps exact one-shot parsing ahead of the assistant planner", async () => {
223264
const { runtime, lines } = createSystemAgentTestRuntime();
224265
let plannerCalls = 0;
225266
let onReadyCalls = 0;
226-
const verified = await createVerifiedRunOptions();
267+
const verified = createVerifiedRunOptions();
227268

228269
await runSystemAgent(
229270
{
@@ -248,7 +289,7 @@ describe("runSystemAgent", () => {
248289

249290
it("prints an explicit one-shot overview exactly once", async () => {
250291
const { runtime, lines } = createSystemAgentTestRuntime();
251-
const verified = await createVerifiedRunOptions();
292+
const verified = createVerifiedRunOptions();
252293

253294
await runSystemAgent(
254295
{
@@ -268,7 +309,7 @@ describe("runSystemAgent", () => {
268309
{ name: "invalid command", plan: { command: "invent a new operation" } },
269310
])("fails a fuzzy one-shot when inference returns $name", async ({ plan }) => {
270311
const { runtime } = createSystemAgentTestRuntime();
271-
const verified = await createVerifiedRunOptions();
312+
const verified = createVerifiedRunOptions();
272313

273314
await expect(
274315
runSystemAgent(
@@ -285,7 +326,7 @@ describe("runSystemAgent", () => {
285326

286327
it("prints a valid reply-only one-shot plan", async () => {
287328
const { runtime, lines } = createSystemAgentTestRuntime();
288-
const verified = await createVerifiedRunOptions();
329+
const verified = createVerifiedRunOptions();
289330

290331
await runSystemAgent(
291332
{
@@ -306,9 +347,12 @@ describe("runSystemAgent", () => {
306347
agents: { defaults: { model: "anthropic/claude-opus-4-8" } },
307348
} satisfies OpenClawConfig;
308349
let currentConfig: OpenClawConfig = verifiedConfig;
309-
const verified = await createVerifiedRunOptions({
310-
readConfigFileSnapshot: vi.fn(async () => configSnapshot(currentConfig)) as never,
311-
});
350+
const verified = createVerifiedRunOptions(
351+
{
352+
readConfigFileSnapshot: vi.fn(async () => configSnapshot(currentConfig)) as never,
353+
},
354+
{ revalidate: true },
355+
);
312356

313357
await expect(
314358
runSystemAgent(
@@ -331,7 +375,7 @@ describe("runSystemAgent", () => {
331375
const { runtime, lines } = createSystemAgentTestRuntime();
332376
let runInteractiveTuiCalls = 0;
333377
let onReadyCalls = 0;
334-
const verified = await createVerifiedRunOptions();
378+
const verified = createVerifiedRunOptions();
335379

336380
await runSystemAgent(
337381
{
@@ -357,7 +401,7 @@ describe("runSystemAgent", () => {
357401
const { runtime, lines } = createSystemAgentTestRuntime();
358402
let loadOverviewCalls = 0;
359403
let runInteractiveTuiCalls = 0;
360-
const verified = await createVerifiedRunOptions();
404+
const verified = createVerifiedRunOptions();
361405

362406
await runSystemAgent(
363407
{
@@ -396,7 +440,7 @@ describe("runSystemAgent", () => {
396440
])("exits non-zero when $name", async ({ input, output, interactive }) => {
397441
const { runtime, lines } = createSystemAgentTestRuntime();
398442
let runInteractiveTuiCalls = 0;
399-
const verified = await createVerifiedRunOptions();
443+
const verified = createVerifiedRunOptions();
400444

401445
await expect(
402446
runSystemAgent(

0 commit comments

Comments
 (0)