refactor(tests): share plugin registration helpers

This commit is contained in:
Peter Steinberger 2026-03-16 01:23:45 +00:00
parent 6b28668104
commit bb76a90dd1
5 changed files with 64 additions and 56 deletions

View File

@ -1,23 +1,12 @@
import { describe, expect, it } from "vitest";
import type { ProviderPlugin } from "../../src/plugins/types.js";
import { registerSingleProviderPlugin } from "../../src/test-utils/plugin-registration.js";
import {
createProviderUsageFetch,
makeResponse,
} from "../../src/test-utils/provider-usage-fetch.js";
import anthropicPlugin from "./index.js";
function registerProvider(): ProviderPlugin {
let provider: ProviderPlugin | undefined;
anthropicPlugin.register({
registerProvider(nextProvider: ProviderPlugin) {
provider = nextProvider;
},
} as never);
if (!provider) {
throw new Error("provider registration missing");
}
return provider;
}
const registerProvider = () => registerSingleProviderPlugin(anthropicPlugin);
describe("anthropic plugin", () => {
it("owns anthropic 4.6 forward-compat resolution", () => {

View File

@ -1,19 +1,8 @@
import { describe, expect, it } from "vitest";
import type { ProviderPlugin } from "../../src/plugins/types.js";
import { registerSingleProviderPlugin } from "../../src/test-utils/plugin-registration.js";
import githubCopilotPlugin from "./index.js";
function registerProvider(): ProviderPlugin {
let provider: ProviderPlugin | undefined;
githubCopilotPlugin.register({
registerProvider(nextProvider: ProviderPlugin) {
provider = nextProvider;
},
} as never);
if (!provider) {
throw new Error("provider registration missing");
}
return provider;
}
const registerProvider = () => registerSingleProviderPlugin(githubCopilotPlugin);
describe("github-copilot plugin", () => {
it("owns Copilot-specific forward-compat fallbacks", () => {

View File

@ -1,5 +1,6 @@
import { describe, expect, it } from "vitest";
import type { ProviderPlugin } from "../../src/plugins/types.js";
import { createCapturedPluginRegistration } from "../../src/test-utils/plugin-registration.js";
import {
createProviderUsageFetch,
makeResponse,
@ -15,26 +16,25 @@ function registerGooglePlugin(): {
} | null;
webSearchProviderRegistered: boolean;
} {
let provider: ProviderPlugin | undefined;
let webSearchProviderRegistered = false;
let webSearchProvider: {
id: string;
envVars: string[];
label: string;
} | null = null;
googlePlugin.register({
registerProvider(nextProvider: ProviderPlugin) {
provider = nextProvider;
},
registerWebSearchProvider(nextProvider: { id: string; envVars: string[]; label: string }) {
webSearchProviderRegistered = true;
webSearchProvider = nextProvider;
},
} as never);
const captured = createCapturedPluginRegistration();
googlePlugin.register(captured.api);
const provider = captured.providers[0];
if (!provider) {
throw new Error("provider registration missing");
}
return { provider, webSearchProviderRegistered, webSearchProvider };
const webSearchProvider = captured.webSearchProviders[0] ?? null;
return {
provider,
webSearchProviderRegistered: webSearchProvider !== null,
webSearchProvider:
webSearchProvider === null
? null
: {
id: webSearchProvider.id,
envVars: webSearchProvider.envVars,
label: webSearchProvider.label,
},
};
}
describe("google plugin", () => {

View File

@ -1,23 +1,12 @@
import { describe, expect, it } from "vitest";
import type { ProviderPlugin } from "../../src/plugins/types.js";
import { registerSingleProviderPlugin } from "../../src/test-utils/plugin-registration.js";
import {
createProviderUsageFetch,
makeResponse,
} from "../../src/test-utils/provider-usage-fetch.js";
import zaiPlugin from "./index.js";
function registerProvider(): ProviderPlugin {
let provider: ProviderPlugin | undefined;
zaiPlugin.register({
registerProvider(nextProvider: ProviderPlugin) {
provider = nextProvider;
},
} as never);
if (!provider) {
throw new Error("provider registration missing");
}
return provider;
}
const registerProvider = () => registerSingleProviderPlugin(zaiPlugin);
describe("zai plugin", () => {
it("owns glm-5 forward-compat resolution", () => {

View File

@ -0,0 +1,41 @@
import type {
OpenClawPluginApi,
ProviderPlugin,
WebSearchProviderPlugin,
} from "../plugins/types.js";
export type CapturedPluginRegistration = {
api: OpenClawPluginApi;
providers: ProviderPlugin[];
webSearchProviders: WebSearchProviderPlugin[];
};
export function createCapturedPluginRegistration(): CapturedPluginRegistration {
const providers: ProviderPlugin[] = [];
const webSearchProviders: WebSearchProviderPlugin[] = [];
return {
providers,
webSearchProviders,
api: {
registerProvider(provider: ProviderPlugin) {
providers.push(provider);
},
registerWebSearchProvider(provider: WebSearchProviderPlugin) {
webSearchProviders.push(provider);
},
} as OpenClawPluginApi,
};
}
export function registerSingleProviderPlugin(params: {
register(api: OpenClawPluginApi): void;
}): ProviderPlugin {
const captured = createCapturedPluginRegistration();
params.register(captured.api);
const provider = captured.providers[0];
if (!provider) {
throw new Error("provider registration missing");
}
return provider;
}