From dc2396ba13f91b0f93c3348ee81219f250fe39b3 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 2 May 2026 05:28:40 +0100 Subject: [PATCH] refactor: trim bundled channel contract loader --- extensions/google/web-search-provider.test.ts | 41 +++++++++++++------ .../bundled-channel-plugin-loader.ts | 19 +-------- 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/extensions/google/web-search-provider.test.ts b/extensions/google/web-search-provider.test.ts index 2c4d25eab2e..db9bcc2b2be 100644 --- a/extensions/google/web-search-provider.test.ts +++ b/extensions/google/web-search-provider.test.ts @@ -8,7 +8,7 @@ type TestModelProviderConfig = NonNullable< >[string]; function installGeminiFetch() { - const mockFetch = vi.fn((_input?: unknown, _init?: unknown) => + const mockFetch = vi.fn((_input?: RequestInfo | URL, _init?: RequestInit) => Promise.resolve({ ok: true, json: () => @@ -43,6 +43,29 @@ function getFetchHeaders(mockFetch: ReturnType): Reco return init?.headers ?? {}; } +function getGeminiFetchUrl(mockFetch: ReturnType): string | undefined { + const input = mockFetch.mock.calls[0]?.[0]; + if (typeof input === "string") { + return input; + } + if (input instanceof URL) { + return input.toString(); + } + return input?.url; +} + +function parseGeminiFetchBody(mockFetch: ReturnType): { + tools?: Array<{ google_search?: { timeRangeFilter?: unknown } }>; +} { + const body = mockFetch.mock.calls[0]?.[1]?.body; + if (typeof body !== "string") { + throw new Error("Expected Gemini fetch body string"); + } + return JSON.parse(body) as { + tools?: Array<{ google_search?: { timeRangeFilter?: unknown } }>; + }; +} + afterEach(() => { vi.useRealTimers(); vi.restoreAllMocks(); @@ -124,7 +147,7 @@ describe("google web search provider", () => { await tool?.execute({ query: "OpenClaw docs" }); - expect(String(mockFetch.mock.calls[0]?.[0])).toBe( + expect(getGeminiFetchUrl(mockFetch)).toBe( "https://generativelanguage.googleapis.com/proxy/v1beta/models/gemini-2.5-flash:generateContent", ); }); @@ -205,7 +228,7 @@ describe("google web search provider", () => { await tool?.execute({ query: "OpenClaw provider baseUrl fallback" }); - expect(String(mockFetch.mock.calls[0]?.[0])).toBe( + expect(getGeminiFetchUrl(mockFetch)).toBe( "https://generativelanguage.googleapis.com/provider/v1beta/models/gemini-2.5-flash:generateContent", ); }); @@ -240,7 +263,7 @@ describe("google web search provider", () => { await tool?.execute({ query: "OpenClaw plugin baseUrl precedence" }); - expect(String(mockFetch.mock.calls[0]?.[0])).toBe( + expect(getGeminiFetchUrl(mockFetch)).toBe( "https://generativelanguage.googleapis.com/plugin/v1beta/models/gemini-2.5-flash:generateContent", ); }); @@ -269,10 +292,7 @@ describe("google web search provider", () => { await tool?.execute({ query: "latest ai news", freshness: "week" }); - const init = mockFetch.mock.calls[0]?.[1] as { body?: unknown } | undefined; - const body = JSON.parse(String(init?.body)) as { - tools?: Array<{ google_search?: { timeRangeFilter?: unknown } }>; - }; + const body = parseGeminiFetchBody(mockFetch); expect(body.tools?.[0]?.google_search?.timeRangeFilter).toEqual({ startTime: "2026-04-08T12:00:00.000Z", endTime: "2026-04-15T12:00:00.000Z", @@ -305,10 +325,7 @@ describe("google web search provider", () => { date_before: "2026-04-30", }); - const init = mockFetch.mock.calls[0]?.[1] as { body?: unknown } | undefined; - const body = JSON.parse(String(init?.body)) as { - tools?: Array<{ google_search?: { timeRangeFilter?: unknown } }>; - }; + const body = parseGeminiFetchBody(mockFetch); expect(body.tools?.[0]?.google_search?.timeRangeFilter).toEqual({ startTime: "2026-04-01T00:00:00Z", endTime: "2026-05-01T00:00:00.000Z", diff --git a/src/channels/plugins/contracts/test-helpers/bundled-channel-plugin-loader.ts b/src/channels/plugins/contracts/test-helpers/bundled-channel-plugin-loader.ts index 99b0284c5a5..1b1d3b11df7 100644 --- a/src/channels/plugins/contracts/test-helpers/bundled-channel-plugin-loader.ts +++ b/src/channels/plugins/contracts/test-helpers/bundled-channel-plugin-loader.ts @@ -1,7 +1,4 @@ -import { - loadBundledPluginPublicSurface, - loadBundledPluginPublicSurfaceSync, -} from "../../../../test-utils/bundled-plugin-public-surface.js"; +import { loadBundledPluginPublicSurface } from "../../../../test-utils/bundled-plugin-public-surface.js"; import { listBundledChannelPluginIds as listCatalogBundledChannelPluginIds } from "../../bundled-ids.js"; import type { ChannelId } from "../../channel-id.types.js"; import type { ChannelPlugin } from "../../types.js"; @@ -25,20 +22,6 @@ export function listBundledChannelPluginIds(): readonly ChannelId[] { return listCatalogBundledChannelPluginIds() as ChannelId[]; } -export function getBundledChannelPlugin(id: ChannelId): ChannelPlugin | undefined { - if (channelPluginCache.has(id)) { - return channelPluginCache.get(id) ?? undefined; - } - - const loaded = loadBundledPluginPublicSurfaceSync({ - pluginId: id, - artifactBasename: "channel-plugin-api.js", - }); - const plugin = Object.values(loaded).find(isChannelPlugin) ?? null; - channelPluginCache.set(id, plugin); - return plugin ?? undefined; -} - export async function getBundledChannelPluginAsync( id: ChannelId, ): Promise {