|
| 1 | +import fs from "node:fs"; |
| 2 | +import os from "node:os"; |
| 3 | +import path from "node:path"; |
| 4 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 5 | +import type { OpenClawConfig } from "../config/config.js"; |
| 6 | +import type { RuntimeEnv } from "../runtime.js"; |
| 7 | +import type { agentCliCommand as AgentCliCommand } from "./agent-via-gateway.js"; |
| 8 | +import type { agentCommand as AgentCommand } from "./agent.js"; |
| 9 | + |
| 10 | +const loadConfig = vi.hoisted(() => vi.fn()); |
| 11 | +const callGateway = vi.hoisted(() => vi.fn()); |
| 12 | +const agentCommand = vi.hoisted(() => vi.fn()); |
| 13 | + |
| 14 | +let agentCliCommand: typeof AgentCliCommand; |
| 15 | + |
| 16 | +const runtime: RuntimeEnv = { |
| 17 | + log: vi.fn(), |
| 18 | + error: vi.fn(), |
| 19 | + exit: vi.fn(), |
| 20 | +}; |
| 21 | + |
| 22 | +function mockConfig(storePath: string, overrides?: Partial<OpenClawConfig>) { |
| 23 | + loadConfig.mockReturnValue({ |
| 24 | + agents: { |
| 25 | + defaults: { |
| 26 | + timeoutSeconds: 600, |
| 27 | + ...overrides?.agents?.defaults, |
| 28 | + }, |
| 29 | + }, |
| 30 | + session: { |
| 31 | + store: storePath, |
| 32 | + mainKey: "main", |
| 33 | + ...overrides?.session, |
| 34 | + }, |
| 35 | + gateway: overrides?.gateway, |
| 36 | + }); |
| 37 | +} |
| 38 | + |
| 39 | +async function withTempStore( |
| 40 | + fn: (ctx: { dir: string; store: string }) => Promise<void>, |
| 41 | + overrides?: Partial<OpenClawConfig>, |
| 42 | +) { |
| 43 | + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-agent-cli-")); |
| 44 | + const store = path.join(dir, "sessions.json"); |
| 45 | + mockConfig(store, overrides); |
| 46 | + try { |
| 47 | + await fn({ dir, store }); |
| 48 | + } finally { |
| 49 | + fs.rmSync(dir, { recursive: true, force: true }); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +function mockGatewaySuccessReply(text = "hello") { |
| 54 | + callGateway.mockResolvedValue({ |
| 55 | + runId: "idem-1", |
| 56 | + status: "ok", |
| 57 | + result: { |
| 58 | + payloads: [{ text }], |
| 59 | + meta: { stub: true }, |
| 60 | + }, |
| 61 | + }); |
| 62 | +} |
| 63 | + |
| 64 | +function mockLocalAgentReply(text = "local") { |
| 65 | + agentCommand.mockImplementationOnce(async (_opts, rt) => { |
| 66 | + rt?.log?.(text); |
| 67 | + return { |
| 68 | + payloads: [{ text }], |
| 69 | + meta: { durationMs: 1, agentMeta: { sessionId: "s", provider: "p", model: "m" } }, |
| 70 | + } as unknown as Awaited<ReturnType<typeof AgentCommand>>; |
| 71 | + }); |
| 72 | +} |
| 73 | + |
| 74 | +beforeEach(async () => { |
| 75 | + vi.clearAllMocks(); |
| 76 | + vi.resetModules(); |
| 77 | + vi.doMock("../config/config.js", () => ({ loadConfig })); |
| 78 | + vi.doMock("../gateway/call.js", () => ({ |
| 79 | + callGateway, |
| 80 | + randomIdempotencyKey: () => "idem-1", |
| 81 | + })); |
| 82 | + vi.doMock("./agent.js", () => ({ agentCommand })); |
| 83 | + ({ agentCliCommand } = await import("./agent-via-gateway.js")); |
| 84 | +}); |
| 85 | + |
| 86 | +describe("agentCliCommand", () => { |
| 87 | + it("uses a timer-safe max gateway timeout when --timeout is 0", async () => { |
| 88 | + await withTempStore(async () => { |
| 89 | + mockGatewaySuccessReply(); |
| 90 | + |
| 91 | + await agentCliCommand({ message: "hi", to: "+1555", timeout: "0" }, runtime); |
| 92 | + |
| 93 | + expect(callGateway).toHaveBeenCalledTimes(1); |
| 94 | + const request = callGateway.mock.calls[0]?.[0] as { timeoutMs?: number }; |
| 95 | + expect(request.timeoutMs).toBe(2_147_000_000); |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + it("uses gateway by default", async () => { |
| 100 | + await withTempStore(async () => { |
| 101 | + mockGatewaySuccessReply(); |
| 102 | + |
| 103 | + await agentCliCommand({ message: "hi", to: "+1555" }, runtime); |
| 104 | + |
| 105 | + expect(callGateway).toHaveBeenCalledTimes(1); |
| 106 | + expect(agentCommand).not.toHaveBeenCalled(); |
| 107 | + expect(runtime.log).toHaveBeenCalledWith("hello"); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + it("falls back to embedded agent when gateway fails", async () => { |
| 112 | + await withTempStore(async () => { |
| 113 | + callGateway.mockRejectedValue(new Error("gateway not connected")); |
| 114 | + mockLocalAgentReply(); |
| 115 | + |
| 116 | + await agentCliCommand({ message: "hi", to: "+1555" }, runtime); |
| 117 | + |
| 118 | + expect(callGateway).toHaveBeenCalledTimes(1); |
| 119 | + expect(agentCommand).toHaveBeenCalledTimes(1); |
| 120 | + expect(runtime.log).toHaveBeenCalledWith("local"); |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + it("skips gateway when --local is set", async () => { |
| 125 | + await withTempStore(async () => { |
| 126 | + mockLocalAgentReply(); |
| 127 | + |
| 128 | + await agentCliCommand( |
| 129 | + { |
| 130 | + message: "hi", |
| 131 | + to: "+1555", |
| 132 | + local: true, |
| 133 | + }, |
| 134 | + runtime, |
| 135 | + ); |
| 136 | + |
| 137 | + expect(callGateway).not.toHaveBeenCalled(); |
| 138 | + expect(agentCommand).toHaveBeenCalledTimes(1); |
| 139 | + expect(agentCommand.mock.calls[0]?.[0]).toMatchObject({ |
| 140 | + cleanupBundleMcpOnRunEnd: true, |
| 141 | + }); |
| 142 | + expect(runtime.log).toHaveBeenCalledWith("local"); |
| 143 | + }); |
| 144 | + }); |
| 145 | + |
| 146 | + it("does not force bundle MCP cleanup on gateway fallback", async () => { |
| 147 | + await withTempStore(async () => { |
| 148 | + callGateway.mockRejectedValue(new Error("gateway not connected")); |
| 149 | + mockLocalAgentReply(); |
| 150 | + |
| 151 | + await agentCliCommand({ message: "hi", to: "+1555" }, runtime); |
| 152 | + |
| 153 | + expect(agentCommand).toHaveBeenCalledTimes(1); |
| 154 | + expect(agentCommand.mock.calls[0]?.[0]).not.toMatchObject({ |
| 155 | + cleanupBundleMcpOnRunEnd: true, |
| 156 | + }); |
| 157 | + }); |
| 158 | + }); |
| 159 | +}); |
0 commit comments