Files
openclaw/src/cli/plugin-registry-loader.test.ts
2026-04-06 13:51:51 +01:00

67 lines
2.4 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
const ensurePluginRegistryLoadedMock = vi.hoisted(() => vi.fn());
vi.mock("./plugin-registry.js", () => ({
ensurePluginRegistryLoaded: ensurePluginRegistryLoadedMock,
}));
describe("plugin-registry-loader", () => {
let originalForceStderr: boolean;
let ensureCliPluginRegistryLoaded: typeof import("./plugin-registry-loader.js").ensureCliPluginRegistryLoaded;
let resolvePluginRegistryScopeForCommandPath: typeof import("./plugin-registry-loader.js").resolvePluginRegistryScopeForCommandPath;
let loggingState: typeof import("../logging/state.js").loggingState;
beforeEach(async () => {
vi.clearAllMocks();
vi.resetModules();
({ ensureCliPluginRegistryLoaded, resolvePluginRegistryScopeForCommandPath } =
await import("./plugin-registry-loader.js"));
({ loggingState } = await import("../logging/state.js"));
originalForceStderr = loggingState.forceConsoleToStderr;
loggingState.forceConsoleToStderr = false;
});
afterEach(() => {
loggingState.forceConsoleToStderr = originalForceStderr;
});
it("routes plugin load logs to stderr and restores state", async () => {
const captured: boolean[] = [];
ensurePluginRegistryLoadedMock.mockImplementation(() => {
captured.push(loggingState.forceConsoleToStderr);
});
await ensureCliPluginRegistryLoaded({
scope: "configured-channels",
routeLogsToStderr: true,
});
expect(ensurePluginRegistryLoadedMock).toHaveBeenCalledWith({
scope: "configured-channels",
});
expect(captured).toEqual([true]);
expect(loggingState.forceConsoleToStderr).toBe(false);
});
it("keeps stdout routing unchanged when stderr routing is not requested", async () => {
const captured: boolean[] = [];
ensurePluginRegistryLoadedMock.mockImplementation(() => {
captured.push(loggingState.forceConsoleToStderr);
});
await ensureCliPluginRegistryLoaded({
scope: "all",
});
expect(captured).toEqual([false]);
expect(loggingState.forceConsoleToStderr).toBe(false);
});
it("maps command paths to plugin registry scopes", () => {
expect(resolvePluginRegistryScopeForCommandPath(["status"])).toBe("channels");
expect(resolvePluginRegistryScopeForCommandPath(["health"])).toBe("channels");
expect(resolvePluginRegistryScopeForCommandPath(["agents"])).toBe("all");
});
});