test: isolate bundled plugin coverage from unit

This commit is contained in:
Peter Steinberger
2026-04-03 10:56:55 +01:00
parent 64755c52f2
commit 55e43cbc7f
21 changed files with 629 additions and 89 deletions

View File

@@ -6,6 +6,7 @@ import { resolveBundledPluginsDir } from "./bundled-dir.js";
const tempDirs: string[] = [];
const originalBundledDir = process.env.OPENCLAW_BUNDLED_PLUGINS_DIR;
const originalDisableBundledPlugins = process.env.OPENCLAW_DISABLE_BUNDLED_PLUGINS;
const originalVitest = process.env.VITEST;
const originalArgv1 = process.argv[1];
@@ -52,6 +53,7 @@ function expectResolvedBundledDir(params: {
expectedDir: string;
argv1?: string;
bundledDirOverride?: string;
disableBundledPlugins?: string;
vitest?: string;
}) {
vi.spyOn(process, "cwd").mockReturnValue(params.cwd);
@@ -66,6 +68,11 @@ function expectResolvedBundledDir(params: {
} else {
process.env.OPENCLAW_BUNDLED_PLUGINS_DIR = params.bundledDirOverride;
}
if (params.disableBundledPlugins === undefined) {
delete process.env.OPENCLAW_DISABLE_BUNDLED_PLUGINS;
} else {
process.env.OPENCLAW_DISABLE_BUNDLED_PLUGINS = params.disableBundledPlugins;
}
expect(fs.realpathSync(resolveBundledPluginsDir() ?? "")).toBe(
fs.realpathSync(params.expectedDir),
@@ -122,6 +129,11 @@ afterEach(() => {
} else {
process.env.OPENCLAW_BUNDLED_PLUGINS_DIR = originalBundledDir;
}
if (originalDisableBundledPlugins === undefined) {
delete process.env.OPENCLAW_DISABLE_BUNDLED_PLUGINS;
} else {
process.env.OPENCLAW_DISABLE_BUNDLED_PLUGINS = originalDisableBundledPlugins;
}
if (originalVitest === undefined) {
delete process.env.VITEST;
} else {
@@ -192,6 +204,25 @@ describe("resolveBundledPluginsDir", () => {
});
});
it("returns a stable empty bundled plugin directory when bundled plugins are disabled", () => {
const repoRoot = createOpenClawRoot({
prefix: "openclaw-bundled-dir-disabled-",
hasExtensions: true,
hasSrc: true,
hasGitCheckout: true,
});
vi.spyOn(process, "cwd").mockReturnValue(repoRoot);
process.argv[1] = "/usr/bin/env";
process.env.OPENCLAW_DISABLE_BUNDLED_PLUGINS = "1";
delete process.env.OPENCLAW_BUNDLED_PLUGINS_DIR;
const bundledDir = resolveBundledPluginsDir();
expect(bundledDir).toBeTruthy();
expect(fs.existsSync(bundledDir ?? "")).toBe(true);
expect(fs.readdirSync(bundledDir ?? "")).toEqual([]);
});
it.each([
{
name: "prefers the running CLI package root over an unrelated cwd checkout",

View File

@@ -1,9 +1,22 @@
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { resolveOpenClawPackageRootSync } from "../infra/openclaw-root.js";
import { resolveUserPath } from "../utils.js";
const DISABLED_BUNDLED_PLUGINS_DIR = path.join(os.tmpdir(), "openclaw-empty-bundled-plugins");
function bundledPluginsDisabled(env: NodeJS.ProcessEnv): boolean {
const raw = env.OPENCLAW_DISABLE_BUNDLED_PLUGINS?.trim().toLowerCase();
return raw === "1" || raw === "true";
}
function resolveDisabledBundledPluginsDir(): string {
fs.mkdirSync(DISABLED_BUNDLED_PLUGINS_DIR, { recursive: true });
return DISABLED_BUNDLED_PLUGINS_DIR;
}
function isSourceCheckoutRoot(packageRoot: string): boolean {
return (
fs.existsSync(path.join(packageRoot, ".git")) &&
@@ -38,6 +51,10 @@ function resolveBundledDirFromPackageRoot(
}
export function resolveBundledPluginsDir(env: NodeJS.ProcessEnv = process.env): string | undefined {
if (bundledPluginsDisabled(env)) {
return resolveDisabledBundledPluginsDir();
}
const override = env.OPENCLAW_BUNDLED_PLUGINS_DIR?.trim();
if (override) {
const resolvedOverride = resolveUserPath(override, env);

View File

@@ -1,58 +1,137 @@
import { beforeAll, describe, expect, it } from "vitest";
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { OpenClawConfig } from "../config/config.js";
import { loadBundledCapabilityRuntimeRegistry } from "./bundled-capability-runtime.js";
import { BUNDLED_WEB_SEARCH_PLUGIN_IDS } from "./bundled-web-search-ids.js";
import { hasBundledWebSearchCredential } from "./bundled-web-search-registry.js";
import {
listBundledWebSearchPluginIds,
listBundledWebSearchProviders,
resolveBundledWebSearchPluginId,
resolveBundledWebSearchPluginIds,
} from "./bundled-web-search.js";
import { loadPluginManifestRegistry } from "./manifest-registry.js";
let hasBundledWebSearchCredential: typeof import("./bundled-web-search-registry.js").hasBundledWebSearchCredential;
let listBundledWebSearchProviders: typeof import("./bundled-web-search.js").listBundledWebSearchProviders;
let resolveBundledWebSearchPluginIds: typeof import("./bundled-web-search.js").resolveBundledWebSearchPluginIds;
vi.mock("./manifest-registry.js", () => ({
loadPluginManifestRegistry: vi.fn(),
}));
function resolveManifestBundledWebSearchPluginIds() {
return loadPluginManifestRegistry({})
.plugins.filter(
(plugin) =>
plugin.origin === "bundled" && (plugin.contracts?.webSearchProviders?.length ?? 0) > 0,
)
.map((plugin) => plugin.id)
.toSorted((left, right) => left.localeCompare(right));
}
vi.mock("./bundled-capability-runtime.js", () => ({
loadBundledCapabilityRuntimeRegistry: vi.fn(),
}));
async function resolveRegistryBundledWebSearchPluginIds() {
return listBundledWebSearchProviders()
.map(({ pluginId }) => pluginId)
.filter((value, index, values) => values.indexOf(value) === index)
.toSorted((left, right) => left.localeCompare(right));
}
const resolveBundledPluginWebSearchProvidersMock = vi.hoisted(() => vi.fn());
beforeAll(async () => {
({ listBundledWebSearchProviders, resolveBundledWebSearchPluginIds } =
await import("./bundled-web-search.js"));
({ hasBundledWebSearchCredential } = await import("./bundled-web-search-registry.js"));
});
vi.mock("./web-search-providers.js", () => ({
resolveBundledPluginWebSearchProviders: resolveBundledPluginWebSearchProvidersMock,
}));
function expectBundledWebSearchIds(actual: readonly string[], expected: readonly string[]) {
expect(actual).toEqual(expected);
}
function expectBundledWebSearchAlignment(params: {
actual: readonly string[];
expected: readonly string[];
function createMockedBundledWebSearchProvider(params: {
pluginId: string;
providerId: string;
configuredCredential?: unknown;
scopedCredential?: unknown;
envVars?: string[];
}) {
expectBundledWebSearchIds(params.actual, params.expected);
return {
pluginId: params.pluginId,
id: params.providerId,
label: params.providerId,
hint: `${params.providerId} provider`,
envVars: params.envVars ?? [],
placeholder: `${params.providerId}-key`,
signupUrl: `https://example.com/${params.providerId}`,
autoDetectOrder: 10,
credentialPath: `plugins.entries.${params.pluginId}.config.webSearch.apiKey`,
getCredentialValue: () => params.scopedCredential,
getConfiguredCredentialValue: () => params.configuredCredential,
setCredentialValue: () => {},
createTool: () => ({
description: params.providerId,
parameters: {},
execute: async () => ({}),
}),
};
}
describe("bundled web search metadata", () => {
it("keeps bundled web search compat ids aligned with bundled manifests", async () => {
expectBundledWebSearchAlignment({
actual: resolveBundledWebSearchPluginIds({}),
expected: resolveManifestBundledWebSearchPluginIds(),
describe("bundled web search helpers", () => {
beforeEach(() => {
vi.clearAllMocks();
vi.mocked(loadPluginManifestRegistry).mockReturnValue({
plugins: [
{ id: "xai", origin: "bundled" },
{ id: "google", origin: "bundled" },
{ id: "noise", origin: "bundled" },
{ id: "external-google", origin: "workspace" },
] as never[],
diagnostics: [],
});
vi.mocked(loadBundledCapabilityRuntimeRegistry).mockReturnValue({
webSearchProviders: [
{
pluginId: "xai",
provider: createMockedBundledWebSearchProvider({
pluginId: "xai",
providerId: "grok",
}),
},
{
pluginId: "google",
provider: createMockedBundledWebSearchProvider({
pluginId: "google",
providerId: "gemini",
}),
},
],
} as never);
});
it("filters bundled manifest entries down to known bundled web search plugins", () => {
expect(
resolveBundledWebSearchPluginIds({
config: {
plugins: {
allow: ["google", "xai"],
},
},
workspaceDir: "/tmp/workspace",
env: { OPENCLAW_HOME: "/tmp/openclaw-home" },
}),
).toEqual(["google", "xai"]);
expect(loadPluginManifestRegistry).toHaveBeenCalledWith({
config: {
plugins: {
allow: ["google", "xai"],
},
},
workspaceDir: "/tmp/workspace",
env: { OPENCLAW_HOME: "/tmp/openclaw-home" },
});
});
it("keeps bundled web search fast-path ids aligned with the registry", async () => {
expectBundledWebSearchAlignment({
actual: [...BUNDLED_WEB_SEARCH_PLUGIN_IDS],
expected: await resolveRegistryBundledWebSearchPluginIds(),
it("returns a copy of the bundled plugin id fast-path list", () => {
const listed = listBundledWebSearchPluginIds();
expect(listed).toEqual([...BUNDLED_WEB_SEARCH_PLUGIN_IDS]);
expect(listed).not.toBe(BUNDLED_WEB_SEARCH_PLUGIN_IDS);
});
it("maps bundled provider ids back to their owning plugins", () => {
expect(resolveBundledWebSearchPluginId(" gemini ")).toBe("google");
expect(resolveBundledWebSearchPluginId("missing")).toBeUndefined();
});
it("loads bundled provider entries through the capability runtime registry once", () => {
expect(listBundledWebSearchProviders()).toEqual([
expect.objectContaining({ pluginId: "xai", id: "grok" }),
expect.objectContaining({ pluginId: "google", id: "gemini" }),
]);
expect(listBundledWebSearchProviders()).toEqual([
expect.objectContaining({ pluginId: "xai", id: "grok" }),
expect.objectContaining({ pluginId: "google", id: "gemini" }),
]);
expect(loadBundledCapabilityRuntimeRegistry).toHaveBeenCalledTimes(1);
expect(loadBundledCapabilityRuntimeRegistry).toHaveBeenCalledWith({
pluginIds: BUNDLED_WEB_SEARCH_PLUGIN_IDS,
pluginSdkResolution: "dist",
});
});
});
@@ -64,45 +143,68 @@ describe("hasBundledWebSearchCredential", () => {
tools: { web: { fetch: { enabled: false } } },
} satisfies OpenClawConfig;
beforeEach(() => {
resolveBundledPluginWebSearchProvidersMock.mockReset();
});
it.each([
{
name: "detects google plugin web search credentials",
config: {
...baseCfg,
plugins: {
entries: {
google: { enabled: true, config: { webSearch: { apiKey: "AIza-test" } } },
},
},
} satisfies OpenClawConfig,
name: "detects configured plugin credentials",
providers: [
createMockedBundledWebSearchProvider({
pluginId: "google",
providerId: "gemini",
configuredCredential: "AIza-test",
}),
],
config: baseCfg,
env: {},
},
{
name: "detects gemini env credentials",
name: "detects scoped tool credentials",
providers: [
createMockedBundledWebSearchProvider({
pluginId: "google",
providerId: "gemini",
scopedCredential: "AIza-test",
}),
],
config: baseCfg,
env: { GEMINI_API_KEY: "AIza-test" },
env: {},
searchConfig: { provider: "gemini" },
},
{
name: "detects xai env credentials",
name: "detects env credentials",
providers: [
createMockedBundledWebSearchProvider({
pluginId: "xai",
providerId: "grok",
envVars: ["XAI_API_KEY"],
}),
],
config: baseCfg,
env: { XAI_API_KEY: "xai-test" },
},
{
name: "detects kimi env credentials",
config: baseCfg,
env: { KIMI_API_KEY: "sk-kimi-test" },
},
{
name: "detects moonshot env credentials",
config: baseCfg,
env: { MOONSHOT_API_KEY: "sk-moonshot-test" },
},
{
name: "detects openrouter env credentials through bundled web search providers",
config: baseCfg,
env: { OPENROUTER_API_KEY: "sk-or-v1-test" },
},
] as const)("$name", async ({ config, env }) => {
expect(hasBundledWebSearchCredential({ config, env })).toBe(true);
] as const)("$name", ({ providers, config, env, searchConfig }) => {
resolveBundledPluginWebSearchProvidersMock.mockReturnValue(providers);
expect(hasBundledWebSearchCredential({ config, env, searchConfig })).toBe(true);
expect(resolveBundledPluginWebSearchProvidersMock).toHaveBeenCalledWith({
config,
env,
bundledAllowlistCompat: true,
});
});
it("returns false when no bundled provider exposes a configured credential", () => {
resolveBundledPluginWebSearchProvidersMock.mockReturnValue([
createMockedBundledWebSearchProvider({
pluginId: "google",
providerId: "gemini",
envVars: ["GEMINI_API_KEY"],
}),
]);
expect(hasBundledWebSearchCredential({ config: baseCfg, env: {} })).toBe(false);
});
});

View File

@@ -40,6 +40,7 @@ describe("registerPluginCliCommands browser plugin integration", () => {
cache: false,
env: {
...process.env,
OPENCLAW_DISABLE_BUNDLED_PLUGINS: undefined,
OPENCLAW_BUNDLED_PLUGINS_DIR:
bundledFixture?.rootDir ?? path.join(process.cwd(), "extensions"),
} as NodeJS.ProcessEnv,
@@ -61,6 +62,12 @@ describe("registerPluginCliCommands browser plugin integration", () => {
},
} as OpenClawConfig,
cache: false,
env: {
...process.env,
OPENCLAW_DISABLE_BUNDLED_PLUGINS: undefined,
OPENCLAW_BUNDLED_PLUGINS_DIR:
bundledFixture?.rootDir ?? path.join(process.cwd(), "extensions"),
} as NodeJS.ProcessEnv,
});
expect(registry.cliRegistrars.flatMap((entry) => entry.commands)).not.toContain("browser");

View File

@@ -699,6 +699,7 @@ describe("discoverOpenClawPlugins", () => {
const result = discoverOpenClawPlugins({
env: {
...process.env,
OPENCLAW_DISABLE_BUNDLED_PLUGINS: undefined,
OPENCLAW_STATE_DIR: stateDir,
OPENCLAW_BUNDLED_PLUGINS_DIR: bundledDir,
},

View File

@@ -338,6 +338,7 @@ describe("stageBundledPluginRuntime", () => {
const env = {
...process.env,
OPENCLAW_DISABLE_BUNDLED_PLUGINS: undefined,
OPENCLAW_BUNDLED_PLUGINS_DIR: runtimeExtensionsDir,
};
const discovery = discoverOpenClawPlugins({

View File

@@ -1,7 +1,15 @@
import { describe, expect, it } from "vitest";
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { PluginWebSearchProviderEntry } from "./types.js";
import { resolveBundledPluginWebSearchProviders } from "./web-search-providers.js";
const WEB_SEARCH_PROVIDER_TEST_TIMEOUT_MS = 300_000;
const listBundledWebSearchProvidersMock = vi.hoisted(() => vi.fn());
const resolveBundledWebSearchPluginIdsMock = vi.hoisted(() => vi.fn());
vi.mock("./bundled-web-search.js", () => ({
listBundledWebSearchProviders: listBundledWebSearchProvidersMock,
resolveBundledWebSearchPluginIds: resolveBundledWebSearchPluginIdsMock,
}));
const EXPECTED_BUNDLED_WEB_SEARCH_PROVIDER_KEYS = [
"brave:brave",
"duckduckgo:duckduckgo",
@@ -39,6 +47,119 @@ const EXPECTED_BUNDLED_WEB_SEARCH_CREDENTIAL_PATHS = [
"plugins.entries.tavily.config.webSearch.apiKey",
] as const;
function createBundledWebSearchProviderEntry(params: {
pluginId: string;
providerId: string;
credentialPath: string;
order: number;
withApplySelectionConfig?: boolean;
withResolveRuntimeMetadata?: boolean;
}): PluginWebSearchProviderEntry {
return {
pluginId: params.pluginId,
id: params.providerId,
label: params.providerId,
hint: `${params.providerId} provider`,
envVars: [],
placeholder: `${params.providerId}-key`,
signupUrl: `https://example.com/${params.providerId}`,
autoDetectOrder: params.order,
credentialPath: params.credentialPath,
getCredentialValue: () => undefined,
setCredentialValue: () => {},
...(params.withApplySelectionConfig
? {
applySelectionConfig: () => ({
plugins: {
entries: {
[params.pluginId]: {
enabled: true,
},
},
},
}),
}
: {}),
...(params.withResolveRuntimeMetadata
? {
resolveRuntimeMetadata: () => ({
selectedProvider: params.providerId,
}),
}
: {}),
createTool: () => ({
description: params.providerId,
parameters: {},
execute: async () => ({}),
}),
};
}
const BUNDLED_WEB_SEARCH_PROVIDERS: PluginWebSearchProviderEntry[] = [
createBundledWebSearchProviderEntry({
pluginId: "duckduckgo",
providerId: "duckduckgo",
credentialPath: "",
order: 100,
}),
createBundledWebSearchProviderEntry({
pluginId: "moonshot",
providerId: "kimi",
credentialPath: "plugins.entries.moonshot.config.webSearch.apiKey",
order: 40,
}),
createBundledWebSearchProviderEntry({
pluginId: "brave",
providerId: "brave",
credentialPath: "plugins.entries.brave.config.webSearch.apiKey",
order: 10,
}),
createBundledWebSearchProviderEntry({
pluginId: "perplexity",
providerId: "perplexity",
credentialPath: "plugins.entries.perplexity.config.webSearch.apiKey",
order: 50,
withResolveRuntimeMetadata: true,
}),
createBundledWebSearchProviderEntry({
pluginId: "firecrawl",
providerId: "firecrawl",
credentialPath: "plugins.entries.firecrawl.config.webSearch.apiKey",
order: 60,
withApplySelectionConfig: true,
}),
createBundledWebSearchProviderEntry({
pluginId: "google",
providerId: "gemini",
credentialPath: "plugins.entries.google.config.webSearch.apiKey",
order: 20,
}),
createBundledWebSearchProviderEntry({
pluginId: "tavily",
providerId: "tavily",
credentialPath: "plugins.entries.tavily.config.webSearch.apiKey",
order: 80,
}),
createBundledWebSearchProviderEntry({
pluginId: "exa",
providerId: "exa",
credentialPath: "plugins.entries.exa.config.webSearch.apiKey",
order: 55,
}),
createBundledWebSearchProviderEntry({
pluginId: "searxng",
providerId: "searxng",
credentialPath: "plugins.entries.searxng.config.webSearch.baseUrl",
order: 70,
}),
createBundledWebSearchProviderEntry({
pluginId: "xai",
providerId: "grok",
credentialPath: "plugins.entries.xai.config.webSearch.apiKey",
order: 30,
}),
];
function toProviderKeys(
providers: ReturnType<typeof resolveBundledPluginWebSearchProviders>,
): string[] {
@@ -91,6 +212,15 @@ function expectBundledWebSearchResolution(params: {
}
describe("resolveBundledPluginWebSearchProviders", () => {
beforeEach(() => {
listBundledWebSearchProvidersMock.mockReset();
listBundledWebSearchProvidersMock.mockReturnValue(BUNDLED_WEB_SEARCH_PROVIDERS);
resolveBundledWebSearchPluginIdsMock.mockReset();
resolveBundledWebSearchPluginIdsMock.mockReturnValue([
...EXPECTED_BUNDLED_WEB_SEARCH_PROVIDER_PLUGIN_IDS,
]);
});
it.each([
{
title: "returns bundled providers in alphabetical order",
@@ -102,7 +232,7 @@ describe("resolveBundledPluginWebSearchProviders", () => {
bundledAllowlistCompat: true,
},
},
] as const)("$title", { timeout: WEB_SEARCH_PROVIDER_TEST_TIMEOUT_MS }, ({ options }) => {
] as const)("$title", ({ options }) => {
const providers = resolveBundledPluginWebSearchProviders(options);
expectBundledWebSearchProviders(providers);