mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-15 11:11:09 +00:00
refactor: share plugin runtime load context
This commit is contained in:
115
src/plugins/runtime/load-context.test.ts
Normal file
115
src/plugins/runtime/load-context.test.ts
Normal file
@@ -0,0 +1,115 @@
|
||||
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const loadConfigMock = vi.fn();
|
||||
const applyPluginAutoEnableMock = vi.fn();
|
||||
const resolveAgentWorkspaceDirMock = vi.fn(() => "/resolved-workspace");
|
||||
const resolveDefaultAgentIdMock = vi.fn(() => "default");
|
||||
|
||||
let resolvePluginRuntimeLoadContext: typeof import("./load-context.js").resolvePluginRuntimeLoadContext;
|
||||
let buildPluginRuntimeLoadOptions: typeof import("./load-context.js").buildPluginRuntimeLoadOptions;
|
||||
|
||||
vi.mock("../../config/config.js", () => ({
|
||||
loadConfig: () => loadConfigMock(),
|
||||
}));
|
||||
|
||||
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("resolvePluginRuntimeLoadContext", () => {
|
||||
beforeAll(async () => {
|
||||
({ resolvePluginRuntimeLoadContext, buildPluginRuntimeLoadOptions } =
|
||||
await import("./load-context.js"));
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
loadConfigMock.mockReset();
|
||||
applyPluginAutoEnableMock.mockReset();
|
||||
resolveAgentWorkspaceDirMock.mockClear();
|
||||
resolveDefaultAgentIdMock.mockClear();
|
||||
|
||||
loadConfigMock.mockReturnValue({ plugins: {} });
|
||||
applyPluginAutoEnableMock.mockImplementation((params: { config: unknown }) => ({
|
||||
config: params.config,
|
||||
changes: [],
|
||||
autoEnabledReasons: {},
|
||||
}));
|
||||
});
|
||||
|
||||
it("builds the runtime plugin load context from the auto-enabled config", () => {
|
||||
const rawConfig = { plugins: {} };
|
||||
const resolvedConfig = {
|
||||
plugins: {
|
||||
entries: {
|
||||
demo: { enabled: true },
|
||||
},
|
||||
},
|
||||
};
|
||||
const env = { HOME: "/tmp/openclaw-home" } as NodeJS.ProcessEnv;
|
||||
|
||||
applyPluginAutoEnableMock.mockReturnValue({
|
||||
config: resolvedConfig,
|
||||
changes: [],
|
||||
autoEnabledReasons: {
|
||||
demo: ["demo configured"],
|
||||
},
|
||||
});
|
||||
|
||||
const context = resolvePluginRuntimeLoadContext({
|
||||
config: rawConfig,
|
||||
env,
|
||||
});
|
||||
|
||||
expect(context).toEqual(
|
||||
expect.objectContaining({
|
||||
rawConfig,
|
||||
config: resolvedConfig,
|
||||
activationSourceConfig: rawConfig,
|
||||
env,
|
||||
workspaceDir: "/resolved-workspace",
|
||||
autoEnabledReasons: {
|
||||
demo: ["demo configured"],
|
||||
},
|
||||
}),
|
||||
);
|
||||
expect(applyPluginAutoEnableMock).toHaveBeenCalledWith({
|
||||
config: rawConfig,
|
||||
env,
|
||||
});
|
||||
expect(resolveDefaultAgentIdMock).toHaveBeenCalledWith(resolvedConfig);
|
||||
expect(resolveAgentWorkspaceDirMock).toHaveBeenCalledWith(resolvedConfig, "default");
|
||||
});
|
||||
|
||||
it("builds plugin load options from the shared runtime context", () => {
|
||||
const context = resolvePluginRuntimeLoadContext({
|
||||
config: { plugins: {} },
|
||||
env: { HOME: "/tmp/openclaw-home" } as NodeJS.ProcessEnv,
|
||||
workspaceDir: "/explicit-workspace",
|
||||
});
|
||||
|
||||
expect(
|
||||
buildPluginRuntimeLoadOptions(context, {
|
||||
cache: false,
|
||||
activate: false,
|
||||
onlyPluginIds: ["demo"],
|
||||
}),
|
||||
).toEqual(
|
||||
expect.objectContaining({
|
||||
config: context.config,
|
||||
activationSourceConfig: context.activationSourceConfig,
|
||||
autoEnabledReasons: context.autoEnabledReasons,
|
||||
workspaceDir: "/explicit-workspace",
|
||||
env: context.env,
|
||||
logger: context.logger,
|
||||
cache: false,
|
||||
activate: false,
|
||||
onlyPluginIds: ["demo"],
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user