Files
openclaw/src/plugins/cli.test.ts
2026-03-28 03:00:51 +00:00

75 lines
1.9 KiB
TypeScript

import { Command } from "commander";
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { OpenClawConfig } from "../config/config.js";
const mocks = vi.hoisted(() => ({
memoryRegister: vi.fn(),
otherRegister: vi.fn(),
loadOpenClawPlugins: vi.fn(),
}));
vi.mock("./loader.js", () => ({
loadOpenClawPlugins: (...args: unknown[]) => mocks.loadOpenClawPlugins(...args),
}));
import { registerPluginCliCommands } from "./cli.js";
function createProgram(existingCommandName?: string) {
const program = new Command();
if (existingCommandName) {
program.command(existingCommandName);
}
return program;
}
function createCliRegistry() {
return {
cliRegistrars: [
{
pluginId: "memory-core",
register: mocks.memoryRegister,
commands: ["memory"],
source: "bundled",
},
{
pluginId: "other",
register: mocks.otherRegister,
commands: ["other"],
source: "bundled",
},
],
};
}
describe("registerPluginCliCommands", () => {
beforeEach(() => {
mocks.memoryRegister.mockClear();
mocks.otherRegister.mockClear();
mocks.loadOpenClawPlugins.mockReset();
mocks.loadOpenClawPlugins.mockReturnValue(createCliRegistry());
});
it("skips plugin CLI registrars when commands already exist", () => {
const program = createProgram("memory");
// oxlint-disable-next-line typescript/no-explicit-any
registerPluginCliCommands(program, {} as any);
expect(mocks.memoryRegister).not.toHaveBeenCalled();
expect(mocks.otherRegister).toHaveBeenCalledTimes(1);
});
it("forwards an explicit env to plugin loading", () => {
const program = createProgram();
const env = { OPENCLAW_HOME: "/srv/openclaw-home" } as NodeJS.ProcessEnv;
registerPluginCliCommands(program, {} as OpenClawConfig, env);
expect(mocks.loadOpenClawPlugins).toHaveBeenCalledWith(
expect.objectContaining({
env,
}),
);
});
});