test(commands): share agent runtime config fixtures

This commit is contained in:
Vincent Koc
2026-04-12 10:29:45 +01:00
parent 2c918754c2
commit 97fc3ed2ba
3 changed files with 100 additions and 116 deletions

View File

@@ -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<T>(
prefix: string,
fn: (home: string) => Promise<T>,
): Promise<T> {
return withAgentCommandTempHome(prefix, fn);
}
export function mockSharedAgentCommandConfig(
configSpy: typeof configModule.loadConfig,
home: string,
storePath: string,
agentOverrides?: Parameters<typeof mockAgentCommandConfig>[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<ReturnType<typeof configModule.readConfigFileSnapshotForWrite>>);
}

View File

@@ -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<T>(fn: (home: string) => Promise<T>): Promise<T> {
return withAgentCommandTempHome("openclaw-agent-cli-", fn);
return withSharedAgentCommandTempHome("openclaw-agent-cli-", fn);
}
function mockConfig(
home: string,
storePath: string,
agentOverrides?: Parameters<typeof mockAgentCommandConfig>[3],
agentOverrides?: Parameters<typeof mockSharedAgentCommandConfig>[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<ReturnType<typeof configModule.readConfigFileSnapshotForWrite>>);
});
describe("agentCommand CLI provider handling", () => {

View File

@@ -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<T>(fn: (home: string) => Promise<T>): Promise<T> {
return withAgentCommandTempHome("openclaw-agent-", fn);
return withSharedAgentCommandTempHome("openclaw-agent-", fn);
}
function mockConfig(
home: string,
storePath: string,
agentOverrides?: Parameters<typeof mockAgentCommandConfig>[3],
agentOverrides?: Parameters<typeof mockSharedAgentCommandConfig>[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<ReturnType<typeof configModule.readConfigFileSnapshotForWrite>>);
resetSharedAgentCommandRuntimeState(readConfigFileSnapshotForWriteSpy);
});
describe("agentCommand runtime config", () => {