test: isolate agent gateway cli command mocks

This commit is contained in:
Peter Steinberger
2026-04-08 12:23:14 +01:00
parent 2b6b627fd1
commit e17cc51839

View File

@@ -2,21 +2,16 @@ import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { beforeEach, describe, expect, it, vi } from "vitest";
vi.mock("../gateway/call.js", () => ({
callGateway: vi.fn(),
randomIdempotencyKey: () => "idem-1",
}));
vi.mock("./agent.js", () => ({
agentCommand: vi.fn(),
}));
import type { OpenClawConfig } from "../config/config.js";
import * as configModule from "../config/config.js";
import { callGateway } from "../gateway/call.js";
import type { RuntimeEnv } from "../runtime.js";
import { agentCliCommand } from "./agent-via-gateway.js";
import { agentCommand } from "./agent.js";
import type { agentCliCommand as AgentCliCommand } from "./agent-via-gateway.js";
import type { agentCommand as AgentCommand } from "./agent.js";
const loadConfig = vi.hoisted(() => vi.fn());
const callGateway = vi.hoisted(() => vi.fn());
const agentCommand = vi.hoisted(() => vi.fn());
let agentCliCommand: typeof AgentCliCommand;
const runtime: RuntimeEnv = {
log: vi.fn(),
@@ -24,10 +19,8 @@ const runtime: RuntimeEnv = {
exit: vi.fn(),
};
const configSpy = vi.spyOn(configModule, "loadConfig");
function mockConfig(storePath: string, overrides?: Partial<OpenClawConfig>) {
configSpy.mockReturnValue({
loadConfig.mockReturnValue({
agents: {
defaults: {
timeoutSeconds: 600,
@@ -58,7 +51,7 @@ async function withTempStore(
}
function mockGatewaySuccessReply(text = "hello") {
vi.mocked(callGateway).mockResolvedValue({
callGateway.mockResolvedValue({
runId: "idem-1",
status: "ok",
result: {
@@ -69,17 +62,25 @@ function mockGatewaySuccessReply(text = "hello") {
}
function mockLocalAgentReply(text = "local") {
vi.mocked(agentCommand).mockImplementationOnce(async (_opts, rt) => {
agentCommand.mockImplementationOnce(async (_opts, rt) => {
rt?.log?.(text);
return {
payloads: [{ text }],
meta: { durationMs: 1, agentMeta: { sessionId: "s", provider: "p", model: "m" } },
} as unknown as Awaited<ReturnType<typeof agentCommand>>;
} as unknown as Awaited<ReturnType<typeof AgentCommand>>;
});
}
beforeEach(() => {
beforeEach(async () => {
vi.clearAllMocks();
vi.resetModules();
vi.doMock("../config/config.js", () => ({ loadConfig }));
vi.doMock("../gateway/call.js", () => ({
callGateway,
randomIdempotencyKey: () => "idem-1",
}));
vi.doMock("./agent.js", () => ({ agentCommand }));
({ agentCliCommand } = await import("./agent-via-gateway.js"));
});
describe("agentCliCommand", () => {
@@ -90,7 +91,7 @@ describe("agentCliCommand", () => {
await agentCliCommand({ message: "hi", to: "+1555", timeout: "0" }, runtime);
expect(callGateway).toHaveBeenCalledTimes(1);
const request = vi.mocked(callGateway).mock.calls[0]?.[0] as { timeoutMs?: number };
const request = callGateway.mock.calls[0]?.[0] as { timeoutMs?: number };
expect(request.timeoutMs).toBe(2_147_000_000);
});
});
@@ -109,7 +110,7 @@ describe("agentCliCommand", () => {
it("falls back to embedded agent when gateway fails", async () => {
await withTempStore(async () => {
vi.mocked(callGateway).mockRejectedValue(new Error("gateway not connected"));
callGateway.mockRejectedValue(new Error("gateway not connected"));
mockLocalAgentReply();
await agentCliCommand({ message: "hi", to: "+1555" }, runtime);
@@ -135,7 +136,7 @@ describe("agentCliCommand", () => {
expect(callGateway).not.toHaveBeenCalled();
expect(agentCommand).toHaveBeenCalledTimes(1);
expect(vi.mocked(agentCommand).mock.calls[0]?.[0]).toMatchObject({
expect(agentCommand.mock.calls[0]?.[0]).toMatchObject({
cleanupBundleMcpOnRunEnd: true,
});
expect(runtime.log).toHaveBeenCalledWith("local");
@@ -144,13 +145,13 @@ describe("agentCliCommand", () => {
it("does not force bundle MCP cleanup on gateway fallback", async () => {
await withTempStore(async () => {
vi.mocked(callGateway).mockRejectedValue(new Error("gateway not connected"));
callGateway.mockRejectedValue(new Error("gateway not connected"));
mockLocalAgentReply();
await agentCliCommand({ message: "hi", to: "+1555" }, runtime);
expect(agentCommand).toHaveBeenCalledTimes(1);
expect(vi.mocked(agentCommand).mock.calls[0]?.[0]).not.toMatchObject({
expect(agentCommand.mock.calls[0]?.[0]).not.toMatchObject({
cleanupBundleMcpOnRunEnd: true,
});
});