|
| 1 | +// The real channels-list route must resolve catalog-row repair hints from prepared |
| 2 | +// manifest facts instead of rebuilding the manifest registry once per catalog row. |
| 3 | +import fs from "node:fs"; |
| 4 | +import os from "node:os"; |
| 5 | +import path from "node:path"; |
| 6 | +import { afterAll, beforeEach, expect, it, vi } from "vitest"; |
| 7 | +import type { ChannelPluginCatalogEntry } from "../channels/plugins/catalog.js"; |
| 8 | +import type { OpenClawConfig } from "../config/types.openclaw.js"; |
| 9 | + |
| 10 | +const testState = vi.hoisted(() => ({ |
| 11 | + config: {} as OpenClawConfig, |
| 12 | + json: [] as unknown[], |
| 13 | + catalogEntries: [] as ChannelPluginCatalogEntry[], |
| 14 | + manifestRegistryRebuilds: 0, |
| 15 | +})); |
| 16 | + |
| 17 | +vi.mock("../plugins/plugin-registry-contributions.js", async (importOriginal) => { |
| 18 | + const actual = |
| 19 | + await importOriginal<typeof import("../plugins/plugin-registry-contributions.js")>(); |
| 20 | + return { |
| 21 | + ...actual, |
| 22 | + loadPluginManifestRegistryForPluginRegistry: ( |
| 23 | + ...args: Parameters<typeof actual.loadPluginManifestRegistryForPluginRegistry> |
| 24 | + ) => { |
| 25 | + testState.manifestRegistryRebuilds += 1; |
| 26 | + return actual.loadPluginManifestRegistryForPluginRegistry(...args); |
| 27 | + }, |
| 28 | + }; |
| 29 | +}); |
| 30 | + |
| 31 | +vi.mock("./command-execution-startup.js", () => ({ |
| 32 | + applyCliExecutionStartupPresentation: vi.fn(async () => {}), |
| 33 | + ensureCliExecutionBootstrap: vi.fn(async () => {}), |
| 34 | + resolveCliExecutionStartupContext: vi.fn(() => ({ |
| 35 | + startupPolicy: { loadPlugins: false, suppressDoctorStdout: true }, |
| 36 | + })), |
| 37 | +})); |
| 38 | + |
| 39 | +vi.mock("../commands/channels/shared.js", () => ({ |
| 40 | + formatChannelAccountLabel: vi.fn(), |
| 41 | + requireValidConfig: vi.fn(async () => testState.config), |
| 42 | +})); |
| 43 | + |
| 44 | +vi.mock("../commands/channel-setup/trusted-catalog.js", () => ({ |
| 45 | + listTrustedChannelPluginCatalogEntries: vi.fn(() => testState.catalogEntries), |
| 46 | +})); |
| 47 | + |
| 48 | +vi.mock("../agents/agent-scope.js", () => ({ |
| 49 | + resolveAgentWorkspaceDir: vi.fn(() => undefined), |
| 50 | + resolveDefaultAgentId: vi.fn(() => "main"), |
| 51 | +})); |
| 52 | + |
| 53 | +vi.mock("../runtime.js", () => ({ |
| 54 | + defaultRuntime: { |
| 55 | + error: vi.fn(), |
| 56 | + exit: vi.fn(), |
| 57 | + log: vi.fn(), |
| 58 | + writeJson: (value: unknown) => testState.json.push(value), |
| 59 | + writeStdout: vi.fn(), |
| 60 | + }, |
| 61 | + writeRuntimeJson: vi.fn((runtime: { writeJson: (value: unknown) => void }, value: unknown) => |
| 62 | + runtime.writeJson(value), |
| 63 | + ), |
| 64 | +})); |
| 65 | + |
| 66 | +import { tryRouteCli } from "./route.js"; |
| 67 | + |
| 68 | +const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-channels-list-catalog-rows-")); |
| 69 | + |
| 70 | +// Official external channels whose owner plugin is not installed. Each one is |
| 71 | +// configured, so `channels list` renders it as a catalog-only row and resolves a |
| 72 | +// repair hint for it. |
| 73 | +const OWNERLESS_CHANNEL_IDS = ["feishu", "googlechat", "matrix", "twitch"] as const; |
| 74 | + |
| 75 | +function officialExternalCatalogEntry(channelId: string): ChannelPluginCatalogEntry { |
| 76 | + return { |
| 77 | + id: channelId, |
| 78 | + meta: { label: channelId }, |
| 79 | + install: { npmSpec: `@openclaw/${channelId}` }, |
| 80 | + } as ChannelPluginCatalogEntry; |
| 81 | +} |
| 82 | + |
| 83 | +async function runChannelsListJson(channelIds: readonly string[]): Promise<{ |
| 84 | + rebuilds: number; |
| 85 | + chat: unknown; |
| 86 | +}> { |
| 87 | + testState.catalogEntries = channelIds.map(officialExternalCatalogEntry); |
| 88 | + testState.config = { |
| 89 | + channels: Object.fromEntries(channelIds.map((channelId) => [channelId, { enabled: true }])), |
| 90 | + } as OpenClawConfig; |
| 91 | + testState.json = []; |
| 92 | + testState.manifestRegistryRebuilds = 0; |
| 93 | + await expect(tryRouteCli(["node", "openclaw", "channels", "list", "--json"])).resolves.toBe(true); |
| 94 | + return { |
| 95 | + rebuilds: testState.manifestRegistryRebuilds, |
| 96 | + chat: (testState.json[0] as { chat: unknown }).chat, |
| 97 | + }; |
| 98 | +} |
| 99 | + |
| 100 | +beforeEach(() => { |
| 101 | + vi.stubEnv("OPENCLAW_DISABLE_BUNDLED_PLUGINS", "1"); |
| 102 | + vi.stubEnv("OPENCLAW_HOME", path.join(tempRoot, "home")); |
| 103 | + vi.stubEnv("OPENCLAW_STATE_DIR", path.join(tempRoot, "state")); |
| 104 | +}); |
| 105 | + |
| 106 | +afterAll(() => { |
| 107 | + fs.rmSync(tempRoot, { recursive: true, force: true }); |
| 108 | + vi.unstubAllEnvs(); |
| 109 | +}); |
| 110 | + |
| 111 | +it("resolves catalog-row repair hints without rebuilding the manifest registry", async () => { |
| 112 | + const empty = await runChannelsListJson([]); |
| 113 | + const rows = await runChannelsListJson(OWNERLESS_CHANNEL_IDS); |
| 114 | + |
| 115 | + // Every row still resolves its repair hint, so the presence policy really ran. |
| 116 | + expect(rows.chat).toEqual( |
| 117 | + Object.fromEntries( |
| 118 | + OWNERLESS_CHANNEL_IDS.map((channelId) => [ |
| 119 | + channelId, |
| 120 | + { accounts: [], installed: false, origin: "configured" }, |
| 121 | + ]), |
| 122 | + ), |
| 123 | + ); |
| 124 | + // Discovery is prepared once per invocation, so row count cannot move this number. |
| 125 | + expect({ noRows: empty.rebuilds, fourRows: rows.rebuilds }).toEqual({ noRows: 0, fourRows: 0 }); |
| 126 | +}); |
0 commit comments