|
| 1 | +// The real route-first channel status path must stay out of local config/plugin runtimes. |
| 2 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | + |
| 4 | +const loaded = vi.hoisted(() => { |
| 5 | + const modules = new Set<string>(); |
| 6 | + return { |
| 7 | + modules, |
| 8 | + mark(name: string) { |
| 9 | + modules.add(name); |
| 10 | + }, |
| 11 | + }; |
| 12 | +}); |
| 13 | +const callGatewayMock = vi.hoisted(() => vi.fn(async () => ({ channelAccounts: {} }))); |
| 14 | +const runtime = vi.hoisted(() => ({ |
| 15 | + error: vi.fn(), |
| 16 | + exit: vi.fn(), |
| 17 | + log: vi.fn(), |
| 18 | + writeJson: vi.fn(), |
| 19 | + writeStdout: vi.fn(), |
| 20 | +})); |
| 21 | + |
| 22 | +vi.mock("./program/config-guard.js", () => { |
| 23 | + loaded.mark("config-guard"); |
| 24 | + return { ensureConfigReady: vi.fn(async () => {}) }; |
| 25 | +}); |
| 26 | + |
| 27 | +vi.mock("../commands/channels/status.runtime.js", () => { |
| 28 | + loaded.mark("channels-status-runtime"); |
| 29 | + return { |
| 30 | + formatGatewayChannelsStatusLines: vi.fn(() => []), |
| 31 | + renderChannelsStatusFallback: vi.fn(async () => {}), |
| 32 | + }; |
| 33 | +}); |
| 34 | + |
| 35 | +vi.mock("../gateway/call.js", () => ({ |
| 36 | + callGateway: callGatewayMock, |
| 37 | +})); |
| 38 | + |
| 39 | +vi.mock("./progress.js", () => ({ |
| 40 | + withProgress: vi.fn(async (_opts, run: () => Promise<unknown>) => await run()), |
| 41 | +})); |
| 42 | + |
| 43 | +vi.mock("../runtime.js", () => ({ |
| 44 | + defaultRuntime: runtime, |
| 45 | + writeRuntimeJson: vi.fn((target, value) => target.writeJson(value, 2)), |
| 46 | +})); |
| 47 | + |
| 48 | +import { tryRouteCli } from "./route.js"; |
| 49 | + |
| 50 | +describe("routed channels status cold imports", () => { |
| 51 | + beforeEach(() => { |
| 52 | + // Never clear `loaded.modules`: mock factories mark at module-load time, so an |
| 53 | + // eager import during route.js's own static import graph is exactly the |
| 54 | + // regression evidence this test exists to keep. |
| 55 | + vi.clearAllMocks(); |
| 56 | + }); |
| 57 | + |
| 58 | + it("keeps successful JSON routes cold with and without probing", async () => { |
| 59 | + for (const probe of [false, true]) { |
| 60 | + vi.clearAllMocks(); |
| 61 | + const argv = ["node", "openclaw", "channels", "status", "--json"]; |
| 62 | + if (probe) { |
| 63 | + argv.push("--probe"); |
| 64 | + } |
| 65 | + |
| 66 | + await expect(tryRouteCli(argv)).resolves.toBe(true); |
| 67 | + |
| 68 | + expect(callGatewayMock).toHaveBeenCalledOnce(); |
| 69 | + expect(runtime.writeJson).toHaveBeenCalledWith({ channelAccounts: {} }, 2); |
| 70 | + expect(loaded.modules).not.toContain("config-guard"); |
| 71 | + expect(loaded.modules).not.toContain("channels-status-runtime"); |
| 72 | + } |
| 73 | + }); |
| 74 | +}); |
0 commit comments