|
| 1 | +// The default plugin inventory route must render manifest facts without executing plugin code. |
| 2 | +import fs from "node:fs"; |
| 3 | +import os from "node:os"; |
| 4 | +import path from "node:path"; |
| 5 | +import { afterAll, expect, it, vi } from "vitest"; |
| 6 | +import type { OpenClawConfig } from "../config/types.openclaw.js"; |
| 7 | +import { |
| 8 | + createColdPluginFixture, |
| 9 | + isColdPluginRuntimeLoaded, |
| 10 | +} from "../plugins/test-helpers/cold-plugin-fixtures.js"; |
| 11 | + |
| 12 | +const testState = vi.hoisted(() => ({ |
| 13 | + config: {} as OpenClawConfig, |
| 14 | + loadedModules: new Set<string>(), |
| 15 | + logs: [] as string[], |
| 16 | +})); |
| 17 | + |
| 18 | +vi.mock("./command-execution-startup.js", () => ({ |
| 19 | + applyCliExecutionStartupPresentation: vi.fn(async () => {}), |
| 20 | + ensureCliExecutionBootstrap: vi.fn(async () => {}), |
| 21 | + resolveCliExecutionStartupContext: vi.fn(() => ({ |
| 22 | + startupPolicy: { loadPlugins: false, suppressDoctorStdout: true }, |
| 23 | + })), |
| 24 | +})); |
| 25 | + |
| 26 | +vi.mock("../config/config.js", () => ({ |
| 27 | + getRuntimeConfig: () => testState.config, |
| 28 | +})); |
| 29 | + |
| 30 | +vi.mock("../runtime.js", () => ({ |
| 31 | + defaultRuntime: { |
| 32 | + error: vi.fn(), |
| 33 | + exit: vi.fn(), |
| 34 | + log: (...args: unknown[]) => testState.logs.push(args.map(String).join(" ")), |
| 35 | + writeJson: vi.fn(), |
| 36 | + writeStdout: vi.fn(), |
| 37 | + }, |
| 38 | + writeRuntimeJson: vi.fn(), |
| 39 | +})); |
| 40 | + |
| 41 | +vi.mock("./plugins-cli.js", () => { |
| 42 | + testState.loadedModules.add("plugins-cli"); |
| 43 | + return { registerPluginsCli: vi.fn() }; |
| 44 | +}); |
| 45 | + |
| 46 | +vi.mock("../plugins/loader-module-runtime.js", () => { |
| 47 | + testState.loadedModules.add("plugin-module-runtime"); |
| 48 | + return {}; |
| 49 | +}); |
| 50 | + |
| 51 | +import { tryRouteCli } from "./route.js"; |
| 52 | + |
| 53 | +const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-plugins-list-route-")); |
| 54 | + |
| 55 | +afterAll(() => { |
| 56 | + fs.rmSync(tempRoot, { recursive: true, force: true }); |
| 57 | + vi.unstubAllEnvs(); |
| 58 | +}); |
| 59 | + |
| 60 | +it("renders the default list from metadata without loading plugin modules", async () => { |
| 61 | + const pluginRoot = path.join(tempRoot, "route-demo"); |
| 62 | + fs.mkdirSync(pluginRoot, { recursive: true }); |
| 63 | + const fixture = createColdPluginFixture({ |
| 64 | + rootDir: pluginRoot, |
| 65 | + pluginId: "route-demo", |
| 66 | + packageName: "@example/route-demo", |
| 67 | + packageVersion: "4.2.0", |
| 68 | + manifest: { |
| 69 | + name: "Route Demo", |
| 70 | + description: "Prepared inventory metadata", |
| 71 | + }, |
| 72 | + }); |
| 73 | + const bundledRoot = path.join(tempRoot, "bundled"); |
| 74 | + fs.mkdirSync(bundledRoot, { recursive: true }); |
| 75 | + vi.stubEnv("OPENCLAW_BUNDLED_PLUGINS_DIR", bundledRoot); |
| 76 | + vi.stubEnv("OPENCLAW_DISABLE_BUNDLED_PLUGINS", "1"); |
| 77 | + vi.stubEnv("OPENCLAW_HOME", path.join(tempRoot, "home")); |
| 78 | + vi.stubEnv("OPENCLAW_STATE_DIR", path.join(tempRoot, "state")); |
| 79 | + testState.config = { |
| 80 | + plugins: { |
| 81 | + load: { paths: [pluginRoot] }, |
| 82 | + entries: { "route-demo": { enabled: true } }, |
| 83 | + }, |
| 84 | + }; |
| 85 | + |
| 86 | + await expect(tryRouteCli(["node", "openclaw", "plugins", "list"])).resolves.toBe(true); |
| 87 | + |
| 88 | + const output = testState.logs.join("\n"); |
| 89 | + expect(output).toContain("Route Demo"); |
| 90 | + expect(output).toContain("route-demo"); |
| 91 | + expect(output).toContain("Prepared inventory metadata"); |
| 92 | + expect(output).toContain("4.2.0"); |
| 93 | + expect(isColdPluginRuntimeLoaded(fixture)).toBe(false); |
| 94 | + expect(testState.loadedModules).not.toContain("plugin-module-runtime"); |
| 95 | + expect(testState.loadedModules).not.toContain("plugins-cli"); |
| 96 | +}); |
0 commit comments