test: fix provider runtime mocks and test planner load shedding

This commit is contained in:
Ayaan Zaidi
2026-03-31 11:02:33 +05:30
parent aebdb8f8cf
commit 3059eadca2
4 changed files with 70 additions and 32 deletions

View File

@@ -1,8 +1,18 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import type { OpenClawConfig } from "../config/config.js";
import type { PluginAutoEnableResult } from "../config/plugin-auto-enable.js";
import type { PluginManifestRecord } from "./manifest-registry.js";
import { createEmptyPluginRegistry } from "./registry-empty.js";
import type { ProviderPlugin } from "./types.js";
const loadOpenClawPluginsMock = vi.fn();
const loadPluginManifestRegistryMock = vi.fn();
const applyPluginAutoEnableMock = vi.fn();
type LoadOpenClawPlugins = typeof import("./loader.js").loadOpenClawPlugins;
type LoadPluginManifestRegistry =
typeof import("./manifest-registry.js").loadPluginManifestRegistry;
type ApplyPluginAutoEnable = typeof import("../config/plugin-auto-enable.js").applyPluginAutoEnable;
const loadOpenClawPluginsMock = vi.fn<LoadOpenClawPlugins>();
const loadPluginManifestRegistryMock = vi.fn<LoadPluginManifestRegistry>();
const applyPluginAutoEnableMock = vi.fn<ApplyPluginAutoEnable>();
let resolveOwningPluginIdsForProvider: typeof import("./providers.js").resolveOwningPluginIdsForProvider;
let resolvePluginProviders: typeof import("./providers.runtime.js").resolvePluginProviders;
@@ -11,15 +21,22 @@ function createManifestProviderPlugin(params: {
id: string;
providerIds: string[];
origin?: "bundled" | "workspace";
}) {
}): PluginManifestRecord {
return {
id: params.id,
channels: [],
cliBackends: [],
providers: params.providerIds,
skills: [],
hooks: [],
origin: params.origin ?? "bundled",
rootDir: `/tmp/${params.id}`,
source: params.origin ?? "bundled",
manifestPath: `/tmp/${params.id}/openclaw.plugin.json`,
};
}
function setManifestPlugins(plugins: Array<Record<string, unknown>>) {
function setManifestPlugins(plugins: PluginManifestRecord[]) {
loadPluginManifestRegistryMock.mockReturnValue({
plugins,
diagnostics: [],
@@ -91,10 +108,10 @@ function createBundledProviderCompatOptions(params?: { onlyPluginIds?: readonly
}
function createAutoEnabledProviderConfig() {
const rawConfig = {
const rawConfig: OpenClawConfig = {
plugins: {},
};
const autoEnabledConfig = {
const autoEnabledConfig: OpenClawConfig = {
...rawConfig,
plugins: {
entries: {
@@ -151,29 +168,42 @@ function expectBundledProviderLoad(params?: { config?: unknown; env?: NodeJS.Pro
}
describe("resolvePluginProviders", () => {
beforeEach(async () => {
beforeAll(async () => {
vi.resetModules();
vi.doMock("./loader.js", () => ({
loadOpenClawPlugins: (...args: unknown[]) => loadOpenClawPluginsMock(...args),
loadOpenClawPlugins: (...args: Parameters<LoadOpenClawPlugins>) =>
loadOpenClawPluginsMock(...args),
}));
vi.doMock("../config/plugin-auto-enable.js", () => ({
applyPluginAutoEnable: (...args: unknown[]) => applyPluginAutoEnableMock(...args),
applyPluginAutoEnable: (...args: Parameters<ApplyPluginAutoEnable>) =>
applyPluginAutoEnableMock(...args),
}));
vi.doMock("./manifest-registry.js", () => ({
loadPluginManifestRegistry: (...args: unknown[]) => loadPluginManifestRegistryMock(...args),
loadPluginManifestRegistry: (...args: Parameters<LoadPluginManifestRegistry>) =>
loadPluginManifestRegistryMock(...args),
}));
({ resolveOwningPluginIdsForProvider } = await import("./providers.js"));
({ resolvePluginProviders } = await import("./providers.runtime.js"));
});
beforeEach(() => {
loadOpenClawPluginsMock.mockReset();
loadOpenClawPluginsMock.mockReturnValue({
providers: [{ pluginId: "google", provider: { id: "demo-provider" } }],
});
const provider: ProviderPlugin = {
id: "demo-provider",
label: "Demo Provider",
auth: [],
};
const registry = createEmptyPluginRegistry();
registry.providers.push({ pluginId: "google", provider, source: "bundled" });
loadOpenClawPluginsMock.mockReturnValue(registry);
loadPluginManifestRegistryMock.mockReset();
applyPluginAutoEnableMock.mockReset();
applyPluginAutoEnableMock.mockImplementation((params: { config: unknown }) => ({
config: params.config,
changes: [],
}));
applyPluginAutoEnableMock.mockImplementation(
(params): PluginAutoEnableResult => ({
config: params.config,
changes: [],
}),
);
setManifestPlugins([
createManifestProviderPlugin({ id: "google", providerIds: ["google"] }),
createManifestProviderPlugin({ id: "browser", providerIds: [] }),
@@ -196,7 +226,9 @@ describe("resolvePluginProviders", () => {
env,
});
expectResolvedProviders(providers, [{ id: "demo-provider", pluginId: "google" }]);
expectResolvedProviders(providers, [
{ id: "demo-provider", label: "Demo Provider", auth: [], pluginId: "google" },
]);
expect(loadOpenClawPluginsMock).toHaveBeenCalledWith(
expect.objectContaining({
workspaceDir: "/workspace/explicit",