mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 10:30:44 +00:00
test: remove unused agent test helpers
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
import fs from "node:fs/promises";
|
||||
import type { Mock } from "vitest";
|
||||
import { beforeEach, vi } from "vitest";
|
||||
import type { OpenClawConfig } from "../config/types.openclaw.js";
|
||||
import type { requestHeartbeatNow } from "../infra/heartbeat-wake.js";
|
||||
import type { enqueueSystemEvent } from "../infra/system-events.js";
|
||||
import type { CliBackendPlugin } from "../plugin-sdk/cli-backend.js";
|
||||
@@ -32,9 +30,6 @@ type ResolveBootstrapContextForRunMock = Mock<() => Promise<BootstrapContext>>;
|
||||
export const supervisorSpawnMock: UnknownMock = vi.fn();
|
||||
export const enqueueSystemEventMock: UnknownMock = vi.fn();
|
||||
export const requestHeartbeatNowMock: UnknownMock = vi.fn();
|
||||
export const SMALL_PNG_BASE64 =
|
||||
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/woAAn8B9FD5fHAAAAAASUVORK5CYII=";
|
||||
let cliRunnerModulePromise: Promise<typeof import("./cli-runner.js")> | undefined;
|
||||
|
||||
const hoisted = vi.hoisted(
|
||||
(): {
|
||||
@@ -89,12 +84,6 @@ type MockRunExit = {
|
||||
noOutputTimedOut: boolean;
|
||||
};
|
||||
|
||||
type TestCliBackendConfig = {
|
||||
command: string;
|
||||
env?: Record<string, string>;
|
||||
clearEnv?: string[];
|
||||
};
|
||||
|
||||
type ManagedRunMock = {
|
||||
runId: string;
|
||||
pid: number;
|
||||
@@ -311,29 +300,6 @@ export function mockSuccessfulCliRun() {
|
||||
);
|
||||
}
|
||||
|
||||
export const EXISTING_CODEX_CONFIG = {
|
||||
agents: {
|
||||
defaults: {
|
||||
cliBackends: {
|
||||
"codex-cli": {
|
||||
command: "codex",
|
||||
args: ["exec", "--json"],
|
||||
resumeArgs: ["exec", "resume", "{sessionId}", "--json"],
|
||||
output: "text",
|
||||
modelArg: "--model",
|
||||
sessionMode: "existing",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
} satisfies OpenClawConfig;
|
||||
|
||||
export async function setupCliRunnerTestModule() {
|
||||
setupCliRunnerTestRegistry();
|
||||
cliRunnerModulePromise ??= import("./cli-runner.js");
|
||||
return (await cliRunnerModulePromise).runCliAgent;
|
||||
}
|
||||
|
||||
export function setupCliRunnerTestRegistry() {
|
||||
setCliAuthEpochTestDeps({
|
||||
readClaudeCliCredentialsCached: () => null,
|
||||
@@ -368,13 +334,6 @@ export function setupCliRunnerTestRegistry() {
|
||||
});
|
||||
}
|
||||
|
||||
export function stubBootstrapContext(params: {
|
||||
bootstrapFiles: WorkspaceBootstrapFile[];
|
||||
contextFiles: EmbeddedContextFile[];
|
||||
}) {
|
||||
hoisted.resolveBootstrapContextForRunMock.mockResolvedValueOnce(params);
|
||||
}
|
||||
|
||||
export function restoreCliRunnerPrepareTestDeps() {
|
||||
setCliRunnerPrepareTestDeps({
|
||||
makeBootstrapWarn: () => () => {},
|
||||
@@ -383,68 +342,6 @@ export function restoreCliRunnerPrepareTestDeps() {
|
||||
});
|
||||
}
|
||||
|
||||
export async function runCliAgentWithBackendConfig(params: {
|
||||
runCliAgent: typeof import("./cli-runner.js").runCliAgent;
|
||||
backend: TestCliBackendConfig;
|
||||
runId: string;
|
||||
}) {
|
||||
await params.runCliAgent({
|
||||
sessionId: "s1",
|
||||
sessionFile: "/tmp/session.jsonl",
|
||||
workspaceDir: "/tmp",
|
||||
config: {
|
||||
agents: {
|
||||
defaults: {
|
||||
cliBackends: {
|
||||
"codex-cli": params.backend,
|
||||
},
|
||||
},
|
||||
},
|
||||
} satisfies OpenClawConfig,
|
||||
prompt: "hi",
|
||||
provider: "codex-cli",
|
||||
model: "gpt-5.4",
|
||||
timeoutMs: 1_000,
|
||||
runId: params.runId,
|
||||
cliSessionId: "thread-123",
|
||||
});
|
||||
}
|
||||
|
||||
export async function runExistingCodexCliAgent(params: {
|
||||
runCliAgent: typeof import("./cli-runner.js").runCliAgent;
|
||||
runId: string;
|
||||
cliSessionBindingAuthProfileId: string;
|
||||
authProfileId: string;
|
||||
}) {
|
||||
await params.runCliAgent({
|
||||
sessionId: "s1",
|
||||
sessionFile: "/tmp/session.jsonl",
|
||||
workspaceDir: "/tmp",
|
||||
config: EXISTING_CODEX_CONFIG,
|
||||
prompt: "hi",
|
||||
provider: "codex-cli",
|
||||
model: "gpt-5.4",
|
||||
timeoutMs: 1_000,
|
||||
runId: params.runId,
|
||||
cliSessionBinding: {
|
||||
sessionId: "thread-123",
|
||||
authProfileId: params.cliSessionBindingAuthProfileId,
|
||||
},
|
||||
authProfileId: params.authProfileId,
|
||||
});
|
||||
}
|
||||
|
||||
export async function withTempImageFile(
|
||||
prefix: string,
|
||||
): Promise<{ tempDir: string; sourceImage: string }> {
|
||||
const os = await import("node:os");
|
||||
const path = await import("node:path");
|
||||
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), prefix));
|
||||
const sourceImage = path.join(tempDir, "image.png");
|
||||
await fs.writeFile(sourceImage, Buffer.from(SMALL_PNG_BASE64, "base64"));
|
||||
return { tempDir, sourceImage };
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.unstubAllEnvs();
|
||||
});
|
||||
|
||||
@@ -1,51 +0,0 @@
|
||||
import { afterEach, beforeEach, vi } from "vitest";
|
||||
import { resetProviderRuntimeHookCacheForTest } from "../plugins/provider-runtime.js";
|
||||
import { __setModelCatalogImportForTest, resetModelCatalogCacheForTest } from "./model-catalog.js";
|
||||
|
||||
export type PiSdkModule = typeof import("./pi-model-discovery.js");
|
||||
|
||||
vi.mock("./models-config.js", () => ({
|
||||
ensureOpenClawModelsJson: vi.fn().mockResolvedValue({ agentDir: "/tmp", wrote: false }),
|
||||
}));
|
||||
|
||||
vi.mock("./agent-paths.js", () => ({
|
||||
resolveOpenClawAgentDir: () => "/tmp/openclaw",
|
||||
}));
|
||||
|
||||
vi.mock("../plugins/provider-runtime.runtime.js", () => ({
|
||||
augmentModelCatalogWithProviderPlugins: vi.fn().mockResolvedValue([]),
|
||||
}));
|
||||
|
||||
export function installModelCatalogTestHooks() {
|
||||
beforeEach(() => {
|
||||
resetModelCatalogCacheForTest();
|
||||
resetProviderRuntimeHookCacheForTest();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
__setModelCatalogImportForTest();
|
||||
resetModelCatalogCacheForTest();
|
||||
resetProviderRuntimeHookCacheForTest();
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
}
|
||||
|
||||
export function mockCatalogImportFailThenRecover() {
|
||||
let call = 0;
|
||||
__setModelCatalogImportForTest(async () => {
|
||||
call += 1;
|
||||
if (call === 1) {
|
||||
throw new Error("boom");
|
||||
}
|
||||
return {
|
||||
discoverAuthStorage: () => ({}),
|
||||
AuthStorage: function AuthStorage() {},
|
||||
ModelRegistry: class {
|
||||
getAll() {
|
||||
return [{ id: "gpt-4.1", name: "GPT-4.1", provider: "openai" }];
|
||||
}
|
||||
},
|
||||
} as unknown as PiSdkModule;
|
||||
});
|
||||
return () => call;
|
||||
}
|
||||
Reference in New Issue
Block a user