From 97fc3ed2baef1d5da2bf2d964ea5c40108b7215c Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Sun, 12 Apr 2026 10:29:45 +0100 Subject: [PATCH] test(commands): share agent runtime config fixtures --- .../agent-runtime-config.test-support.ts | 81 +++++++++++++++++++ src/commands/agent.cli-provider.test.ts | 67 +++------------ src/commands/agent.runtime-config.test.ts | 68 +++------------- 3 files changed, 100 insertions(+), 116 deletions(-) create mode 100644 src/commands/agent-runtime-config.test-support.ts diff --git a/src/commands/agent-runtime-config.test-support.ts b/src/commands/agent-runtime-config.test-support.ts new file mode 100644 index 00000000000..c8e07b794f5 --- /dev/null +++ b/src/commands/agent-runtime-config.test-support.ts @@ -0,0 +1,81 @@ +import { vi } from "vitest"; +import { __testing as acpManagerTesting } from "../acp/control-plane/manager.js"; +import { loadModelCatalog } from "../agents/model-catalog.js"; +import * as modelSelectionModule from "../agents/model-selection.js"; +import { runEmbeddedPiAgent } from "../agents/pi-embedded.js"; +import type { OpenClawConfig } from "../config/config.js"; +import * as configModule from "../config/config.js"; +import { clearSessionStoreCacheForTest } from "../config/sessions.js"; +import { resetAgentEventsForTest, resetAgentRunContextForTest } from "../infra/agent-events.js"; +import { resetPluginRuntimeStateForTest } from "../plugins/runtime.js"; +import type { RuntimeEnv } from "../runtime.js"; +import { + createDefaultAgentCommandResult, + mockAgentCommandConfig, + withAgentCommandTempHome, +} from "./agent-command.test-support.js"; + +vi.mock("../agents/auth-profiles.js", () => { + return { + ensureAuthProfileStore: vi.fn(() => ({ version: 1, profiles: {} })), + }; +}); + +vi.mock("../agents/auth-profiles/store.js", () => { + const createEmptyStore = () => ({ version: 1, profiles: {} }); + return { + clearRuntimeAuthProfileStoreSnapshots: vi.fn(), + ensureAuthProfileStore: vi.fn(createEmptyStore), + ensureAuthProfileStoreForLocalUpdate: vi.fn(createEmptyStore), + hasAnyAuthProfileStoreSource: vi.fn(() => false), + loadAuthProfileStore: vi.fn(createEmptyStore), + loadAuthProfileStoreForRuntime: vi.fn(createEmptyStore), + loadAuthProfileStoreForSecretsRuntime: vi.fn(createEmptyStore), + replaceRuntimeAuthProfileStoreSnapshots: vi.fn(), + saveAuthProfileStore: vi.fn(), + updateAuthProfileStoreWithLock: vi.fn(async () => createEmptyStore()), + }; +}); + +export const runtime: RuntimeEnv = { + log: vi.fn(), + error: vi.fn(), + exit: vi.fn(() => { + throw new Error("exit"); + }), +}; + +export async function withSharedAgentCommandTempHome( + prefix: string, + fn: (home: string) => Promise, +): Promise { + return withAgentCommandTempHome(prefix, fn); +} + +export function mockSharedAgentCommandConfig( + configSpy: typeof configModule.loadConfig, + home: string, + storePath: string, + agentOverrides?: Parameters[3], +) { + return mockAgentCommandConfig(configSpy, home, storePath, agentOverrides); +} + +export function resetSharedAgentCommandRuntimeState( + readConfigFileSnapshotForWriteSpy: typeof configModule.readConfigFileSnapshotForWrite, +) { + vi.clearAllMocks(); + clearSessionStoreCacheForTest(); + resetAgentEventsForTest(); + resetAgentRunContextForTest(); + resetPluginRuntimeStateForTest(); + acpManagerTesting.resetAcpSessionManagerForTests(); + configModule.clearRuntimeConfigSnapshot(); + vi.mocked(runEmbeddedPiAgent).mockResolvedValue(createDefaultAgentCommandResult()); + vi.mocked(loadModelCatalog).mockResolvedValue([]); + vi.mocked(modelSelectionModule.isCliProvider).mockImplementation(() => false); + vi.mocked(readConfigFileSnapshotForWriteSpy).mockResolvedValue({ + snapshot: { valid: false, resolved: {} as OpenClawConfig }, + writeOptions: {}, + } as Awaited>); +} diff --git a/src/commands/agent.cli-provider.test.ts b/src/commands/agent.cli-provider.test.ts index 40cab942f77..6e2dd58f9c8 100644 --- a/src/commands/agent.cli-provider.test.ts +++ b/src/commands/agent.cli-provider.test.ts @@ -3,69 +3,35 @@ import path from "node:path"; import { beforeEach, describe, expect, it, vi } from "vitest"; import "./agent-command.test-mocks.js"; import "../cron/isolated-agent.mocks.js"; -import { __testing as acpManagerTesting } from "../acp/control-plane/manager.js"; import * as cliRunnerModule from "../agents/cli-runner.js"; import { FailoverError } from "../agents/failover-error.js"; import { loadModelCatalog } from "../agents/model-catalog.js"; import * as modelSelectionModule from "../agents/model-selection.js"; import { runEmbeddedPiAgent } from "../agents/pi-embedded.js"; -import type { OpenClawConfig } from "../config/config.js"; import * as configModule from "../config/config.js"; -import { clearSessionStoreCacheForTest } from "../config/sessions.js"; -import { resetAgentEventsForTest, resetAgentRunContextForTest } from "../infra/agent-events.js"; -import { resetPluginRuntimeStateForTest } from "../plugins/runtime.js"; -import type { RuntimeEnv } from "../runtime.js"; +import { createDefaultAgentCommandResult } from "./agent-command.test-support.js"; import { - createDefaultAgentCommandResult, - mockAgentCommandConfig, - withAgentCommandTempHome, -} from "./agent-command.test-support.js"; + mockSharedAgentCommandConfig, + resetSharedAgentCommandRuntimeState, + runtime, + withSharedAgentCommandTempHome, +} from "./agent-runtime-config.test-support.js"; import { agentCommand } from "./agent.js"; -vi.mock("../agents/auth-profiles.js", () => { - return { - ensureAuthProfileStore: vi.fn(() => ({ version: 1, profiles: {} })), - }; -}); - -vi.mock("../agents/auth-profiles/store.js", () => { - const createEmptyStore = () => ({ version: 1, profiles: {} }); - return { - clearRuntimeAuthProfileStoreSnapshots: vi.fn(), - ensureAuthProfileStore: vi.fn(createEmptyStore), - ensureAuthProfileStoreForLocalUpdate: vi.fn(createEmptyStore), - hasAnyAuthProfileStoreSource: vi.fn(() => false), - loadAuthProfileStore: vi.fn(createEmptyStore), - loadAuthProfileStoreForRuntime: vi.fn(createEmptyStore), - loadAuthProfileStoreForSecretsRuntime: vi.fn(createEmptyStore), - replaceRuntimeAuthProfileStoreSnapshots: vi.fn(), - saveAuthProfileStore: vi.fn(), - updateAuthProfileStoreWithLock: vi.fn(async () => createEmptyStore()), - }; -}); - -const runtime: RuntimeEnv = { - log: vi.fn(), - error: vi.fn(), - exit: vi.fn(() => { - throw new Error("exit"); - }), -}; - const configSpy = vi.spyOn(configModule, "loadConfig"); const readConfigFileSnapshotForWriteSpy = vi.spyOn(configModule, "readConfigFileSnapshotForWrite"); const runCliAgentSpy = vi.spyOn(cliRunnerModule, "runCliAgent"); async function withTempHome(fn: (home: string) => Promise): Promise { - return withAgentCommandTempHome("openclaw-agent-cli-", fn); + return withSharedAgentCommandTempHome("openclaw-agent-cli-", fn); } function mockConfig( home: string, storePath: string, - agentOverrides?: Parameters[3], + agentOverrides?: Parameters[3], ) { - return mockAgentCommandConfig(configSpy, home, storePath, agentOverrides); + return mockSharedAgentCommandConfig(configSpy, home, storePath, agentOverrides); } function writeSessionStoreSeed( @@ -87,21 +53,8 @@ function expectLastEmbeddedProviderModel(provider: string, model: string): void } beforeEach(() => { - vi.clearAllMocks(); - clearSessionStoreCacheForTest(); - resetAgentEventsForTest(); - resetAgentRunContextForTest(); - resetPluginRuntimeStateForTest(); - acpManagerTesting.resetAcpSessionManagerForTests(); - configModule.clearRuntimeConfigSnapshot(); + resetSharedAgentCommandRuntimeState(readConfigFileSnapshotForWriteSpy); runCliAgentSpy.mockResolvedValue(createDefaultAgentCommandResult() as never); - vi.mocked(runEmbeddedPiAgent).mockResolvedValue(createDefaultAgentCommandResult()); - vi.mocked(loadModelCatalog).mockResolvedValue([]); - vi.mocked(modelSelectionModule.isCliProvider).mockImplementation(() => false); - readConfigFileSnapshotForWriteSpy.mockResolvedValue({ - snapshot: { valid: false, resolved: {} as OpenClawConfig }, - writeOptions: {}, - } as Awaited>); }); describe("agentCommand CLI provider handling", () => { diff --git a/src/commands/agent.runtime-config.test.ts b/src/commands/agent.runtime-config.test.ts index 4c1376e12dd..870563d5560 100644 --- a/src/commands/agent.runtime-config.test.ts +++ b/src/commands/agent.runtime-config.test.ts @@ -2,46 +2,17 @@ import path from "node:path"; import { beforeEach, describe, expect, it, vi } from "vitest"; import "./agent-command.test-mocks.js"; import "../cron/isolated-agent.mocks.js"; -import { __testing as acpManagerTesting } from "../acp/control-plane/manager.js"; import { __testing as agentCommandTesting } from "../agents/agent-command.js"; import { resolveSession } from "../agents/command/session.js"; -import { loadModelCatalog } from "../agents/model-catalog.js"; -import * as modelSelectionModule from "../agents/model-selection.js"; -import { runEmbeddedPiAgent } from "../agents/pi-embedded.js"; import * as commandConfigResolutionModule from "../cli/command-config-resolution.js"; import type { OpenClawConfig } from "../config/config.js"; import * as configModule from "../config/config.js"; -import { clearSessionStoreCacheForTest } from "../config/sessions.js"; -import { resetAgentEventsForTest, resetAgentRunContextForTest } from "../infra/agent-events.js"; -import { resetPluginRuntimeStateForTest } from "../plugins/runtime.js"; -import type { RuntimeEnv } from "../runtime.js"; import { - createDefaultAgentCommandResult, - mockAgentCommandConfig, - withAgentCommandTempHome, -} from "./agent-command.test-support.js"; - -vi.mock("../agents/auth-profiles.js", () => { - return { - ensureAuthProfileStore: vi.fn(() => ({ version: 1, profiles: {} })), - }; -}); - -vi.mock("../agents/auth-profiles/store.js", () => { - const createEmptyStore = () => ({ version: 1, profiles: {} }); - return { - clearRuntimeAuthProfileStoreSnapshots: vi.fn(), - ensureAuthProfileStore: vi.fn(createEmptyStore), - ensureAuthProfileStoreForLocalUpdate: vi.fn(createEmptyStore), - hasAnyAuthProfileStoreSource: vi.fn(() => false), - loadAuthProfileStore: vi.fn(createEmptyStore), - loadAuthProfileStoreForRuntime: vi.fn(createEmptyStore), - loadAuthProfileStoreForSecretsRuntime: vi.fn(createEmptyStore), - replaceRuntimeAuthProfileStoreSnapshots: vi.fn(), - saveAuthProfileStore: vi.fn(), - updateAuthProfileStoreWithLock: vi.fn(async () => createEmptyStore()), - }; -}); + mockSharedAgentCommandConfig, + resetSharedAgentCommandRuntimeState, + runtime, + withSharedAgentCommandTempHome, +} from "./agent-runtime-config.test-support.js"; vi.mock("../agents/command/session-store.js", () => { return { @@ -49,44 +20,23 @@ vi.mock("../agents/command/session-store.js", () => { }; }); -const runtime: RuntimeEnv = { - log: vi.fn(), - error: vi.fn(), - exit: vi.fn(() => { - throw new Error("exit"); - }), -}; - const configSpy = vi.spyOn(configModule, "loadConfig"); const readConfigFileSnapshotForWriteSpy = vi.spyOn(configModule, "readConfigFileSnapshotForWrite"); async function withTempHome(fn: (home: string) => Promise): Promise { - return withAgentCommandTempHome("openclaw-agent-", fn); + return withSharedAgentCommandTempHome("openclaw-agent-", fn); } function mockConfig( home: string, storePath: string, - agentOverrides?: Parameters[3], + agentOverrides?: Parameters[3], ) { - return mockAgentCommandConfig(configSpy, home, storePath, agentOverrides); + return mockSharedAgentCommandConfig(configSpy, home, storePath, agentOverrides); } beforeEach(() => { - vi.clearAllMocks(); - clearSessionStoreCacheForTest(); - resetAgentEventsForTest(); - resetAgentRunContextForTest(); - resetPluginRuntimeStateForTest(); - acpManagerTesting.resetAcpSessionManagerForTests(); - configModule.clearRuntimeConfigSnapshot(); - vi.mocked(runEmbeddedPiAgent).mockResolvedValue(createDefaultAgentCommandResult()); - vi.mocked(loadModelCatalog).mockResolvedValue([]); - vi.mocked(modelSelectionModule.isCliProvider).mockImplementation(() => false); - readConfigFileSnapshotForWriteSpy.mockResolvedValue({ - snapshot: { valid: false, resolved: {} as OpenClawConfig }, - writeOptions: {}, - } as Awaited>); + resetSharedAgentCommandRuntimeState(readConfigFileSnapshotForWriteSpy); }); describe("agentCommand runtime config", () => {