146 lines
4.8 KiB
TypeScript
146 lines
4.8 KiB
TypeScript
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
const loadOpenClawPluginsMock = vi.fn();
|
|
const getActivePluginRegistryMock = vi.fn();
|
|
const resolveConfiguredChannelPluginIdsMock = vi.fn();
|
|
const resolveChannelPluginIdsMock = vi.fn();
|
|
const applyPluginAutoEnableMock = vi.fn();
|
|
const resolveAgentWorkspaceDirMock = vi.fn(() => "/resolved-workspace");
|
|
const resolveDefaultAgentIdMock = vi.fn(() => "default");
|
|
|
|
let ensurePluginRegistryLoaded: typeof import("./runtime-registry-loader.js").ensurePluginRegistryLoaded;
|
|
let resetPluginRegistryLoadedForTests: typeof import("./runtime-registry-loader.js").__testing.resetPluginRegistryLoadedForTests;
|
|
|
|
vi.mock("../loader.js", () => ({
|
|
loadOpenClawPlugins: (...args: unknown[]) => loadOpenClawPluginsMock(...args),
|
|
}));
|
|
|
|
vi.mock("../runtime.js", () => ({
|
|
getActivePluginRegistry: (...args: unknown[]) => getActivePluginRegistryMock(...args),
|
|
}));
|
|
|
|
vi.mock("../channel-plugin-ids.js", () => ({
|
|
resolveConfiguredChannelPluginIds: (...args: unknown[]) =>
|
|
resolveConfiguredChannelPluginIdsMock(...args),
|
|
resolveChannelPluginIds: (...args: unknown[]) => resolveChannelPluginIdsMock(...args),
|
|
}));
|
|
|
|
vi.mock("../../config/plugin-auto-enable.js", () => ({
|
|
applyPluginAutoEnable: (...args: unknown[]) => applyPluginAutoEnableMock(...args),
|
|
}));
|
|
|
|
vi.mock("../../agents/agent-scope.js", () => ({
|
|
resolveAgentWorkspaceDir: (...args: unknown[]) => resolveAgentWorkspaceDirMock(...args),
|
|
resolveDefaultAgentId: (...args: unknown[]) => resolveDefaultAgentIdMock(...args),
|
|
}));
|
|
|
|
describe("ensurePluginRegistryLoaded", () => {
|
|
beforeAll(async () => {
|
|
const mod = await import("./runtime-registry-loader.js");
|
|
ensurePluginRegistryLoaded = mod.ensurePluginRegistryLoaded;
|
|
resetPluginRegistryLoadedForTests = () => mod.__testing.resetPluginRegistryLoadedForTests();
|
|
});
|
|
|
|
beforeEach(() => {
|
|
loadOpenClawPluginsMock.mockReset();
|
|
getActivePluginRegistryMock.mockReset();
|
|
resolveConfiguredChannelPluginIdsMock.mockReset();
|
|
resolveChannelPluginIdsMock.mockReset();
|
|
applyPluginAutoEnableMock.mockReset();
|
|
resolveAgentWorkspaceDirMock.mockClear();
|
|
resolveDefaultAgentIdMock.mockClear();
|
|
resetPluginRegistryLoadedForTests();
|
|
|
|
getActivePluginRegistryMock.mockReturnValue({
|
|
plugins: [],
|
|
channels: [],
|
|
tools: [],
|
|
});
|
|
applyPluginAutoEnableMock.mockImplementation((params: { config: unknown }) => ({
|
|
config:
|
|
params.config && typeof params.config === "object"
|
|
? {
|
|
...params.config,
|
|
plugins: {
|
|
entries: {
|
|
demo: { enabled: true },
|
|
},
|
|
},
|
|
}
|
|
: params.config,
|
|
changes: [],
|
|
autoEnabledReasons: {
|
|
demo: ["demo configured"],
|
|
},
|
|
}));
|
|
});
|
|
|
|
it("uses the shared runtime load context for configured-channel loads", () => {
|
|
const rawConfig = { channels: { demo: { enabled: true } } };
|
|
const resolvedConfig = {
|
|
...rawConfig,
|
|
plugins: {
|
|
entries: {
|
|
demo: { enabled: true },
|
|
},
|
|
},
|
|
};
|
|
const env = { HOME: "/tmp/openclaw-home" } as NodeJS.ProcessEnv;
|
|
|
|
resolveConfiguredChannelPluginIdsMock.mockReturnValue(["demo-channel"]);
|
|
ensurePluginRegistryLoaded({
|
|
scope: "configured-channels",
|
|
config: rawConfig as never,
|
|
env,
|
|
activationSourceConfig: { plugins: { allow: ["demo-channel"] } } as never,
|
|
});
|
|
|
|
expect(resolveConfiguredChannelPluginIdsMock).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
config: resolvedConfig,
|
|
env,
|
|
workspaceDir: "/resolved-workspace",
|
|
}),
|
|
);
|
|
expect(applyPluginAutoEnableMock).toHaveBeenCalledWith({
|
|
config: rawConfig,
|
|
env,
|
|
});
|
|
expect(loadOpenClawPluginsMock).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
config: resolvedConfig,
|
|
activationSourceConfig: { plugins: { allow: ["demo-channel"] } },
|
|
autoEnabledReasons: {
|
|
demo: ["demo configured"],
|
|
},
|
|
workspaceDir: "/resolved-workspace",
|
|
onlyPluginIds: ["demo-channel"],
|
|
throwOnLoadError: true,
|
|
}),
|
|
);
|
|
});
|
|
|
|
it("does not cache scoped loads by explicit plugin ids", () => {
|
|
ensurePluginRegistryLoaded({
|
|
scope: "configured-channels",
|
|
config: {} as never,
|
|
onlyPluginIds: ["demo-a"],
|
|
});
|
|
ensurePluginRegistryLoaded({
|
|
scope: "configured-channels",
|
|
config: {} as never,
|
|
onlyPluginIds: ["demo-b"],
|
|
});
|
|
|
|
expect(loadOpenClawPluginsMock).toHaveBeenCalledTimes(2);
|
|
expect(loadOpenClawPluginsMock).toHaveBeenNthCalledWith(
|
|
1,
|
|
expect.objectContaining({ onlyPluginIds: ["demo-a"] }),
|
|
);
|
|
expect(loadOpenClawPluginsMock).toHaveBeenNthCalledWith(
|
|
2,
|
|
expect.objectContaining({ onlyPluginIds: ["demo-b"] }),
|
|
);
|
|
});
|
|
});
|