From e0d7f97c55dc173f4d21d2ee89e3d118f62156c7 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 14 Feb 2026 23:11:16 +0000 Subject: [PATCH] refactor(test): share gateway server plugin mocks --- ...r.agent.gateway-server-agent-a.e2e.test.ts | 35 +--------------- ...r.agent.gateway-server-agent-b.e2e.test.ts | 42 +++---------------- ...server.agent.gateway-server-agent.mocks.ts | 36 ++++++++++++++++ 3 files changed, 42 insertions(+), 71 deletions(-) create mode 100644 src/gateway/server.agent.gateway-server-agent.mocks.ts diff --git a/src/gateway/server.agent.gateway-server-agent-a.e2e.test.ts b/src/gateway/server.agent.gateway-server-agent-a.e2e.test.ts index 2e72ec61c63..553b6c45746 100644 --- a/src/gateway/server.agent.gateway-server-agent-a.e2e.test.ts +++ b/src/gateway/server.agent.gateway-server-agent-a.e2e.test.ts @@ -4,7 +4,7 @@ import path from "node:path"; import { afterAll, beforeAll, describe, expect, test, vi } from "vitest"; import type { ChannelPlugin } from "../channels/plugins/types.js"; import type { PluginRegistry } from "../plugins/registry.js"; -import { setActivePluginRegistry } from "../plugins/runtime.js"; +import { setRegistry } from "./server.agent.gateway-server-agent.mocks.js"; import { agentCommand, connectOk, @@ -32,39 +32,6 @@ afterAll(async () => { await server.close(); }); -const registryState = vi.hoisted(() => ({ - registry: { - plugins: [], - tools: [], - channels: [], - providers: [], - gatewayHandlers: {}, - httpHandlers: [], - httpRoutes: [], - cliRegistrars: [], - services: [], - diagnostics: [], - } as PluginRegistry, -})); - -vi.mock("./server-plugins.js", async () => { - const { setActivePluginRegistry } = await import("../plugins/runtime.js"); - return { - loadGatewayPlugins: (params: { baseMethods: string[] }) => { - setActivePluginRegistry(registryState.registry); - return { - pluginRegistry: registryState.registry, - gatewayMethods: params.baseMethods ?? [], - }; - }, - }; -}); - -const setRegistry = (registry: PluginRegistry) => { - registryState.registry = registry; - setActivePluginRegistry(registry); -}; - const BASE_IMAGE_PNG = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+X3mIAAAAASUVORK5CYII="; diff --git a/src/gateway/server.agent.gateway-server-agent-b.e2e.test.ts b/src/gateway/server.agent.gateway-server-agent-b.e2e.test.ts index 85697f6756b..b90ade36bf6 100644 --- a/src/gateway/server.agent.gateway-server-agent-b.e2e.test.ts +++ b/src/gateway/server.agent.gateway-server-agent-b.e2e.test.ts @@ -7,8 +7,8 @@ import type { ChannelPlugin } from "../channels/plugins/types.js"; import type { PluginRegistry } from "../plugins/registry.js"; import { whatsappPlugin } from "../../extensions/whatsapp/src/channel.js"; import { emitAgentEvent, registerAgentRunContext } from "../infra/agent-events.js"; -import { setActivePluginRegistry } from "../plugins/runtime.js"; import { GATEWAY_CLIENT_MODES, GATEWAY_CLIENT_NAMES } from "../utils/message-channel.js"; +import { setRegistry } from "./server.agent.gateway-server-agent.mocks.js"; import { agentCommand, connectOk, @@ -41,34 +41,6 @@ afterAll(async () => { await server.close(); }); -const registryState = vi.hoisted(() => ({ - registry: { - plugins: [], - tools: [], - channels: [], - providers: [], - gatewayHandlers: {}, - httpHandlers: [], - httpRoutes: [], - cliRegistrars: [], - services: [], - diagnostics: [], - } as PluginRegistry, -})); - -vi.mock("./server-plugins.js", async () => { - const { setActivePluginRegistry } = await import("../plugins/runtime.js"); - return { - loadGatewayPlugins: (params: { baseMethods: string[] }) => { - setActivePluginRegistry(registryState.registry); - return { - pluginRegistry: registryState.registry, - gatewayMethods: params.baseMethods ?? [], - }; - }, - }; -}); - const _BASE_IMAGE_PNG = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+X3mIAAAAASUVORK5CYII="; @@ -123,13 +95,11 @@ async function useTempSessionStorePath() { describe("gateway server agent", () => { beforeEach(() => { - registryState.registry = defaultRegistry; - setActivePluginRegistry(defaultRegistry); + setRegistry(defaultRegistry); }); afterEach(() => { - registryState.registry = emptyRegistry; - setActivePluginRegistry(emptyRegistry); + setRegistry(emptyRegistry); }); test("agent falls back when last-channel plugin is unavailable", async () => { @@ -140,8 +110,7 @@ describe("gateway server agent", () => { plugin: createMSTeamsPlugin(), }, ]); - registryState.registry = registry; - setActivePluginRegistry(registry); + setRegistry(registry); await useTempSessionStorePath(); await writeSessionStore({ entries: { @@ -179,8 +148,7 @@ describe("gateway server agent", () => { plugin: createMSTeamsPlugin({ aliases: ["teams"] }), }, ]); - registryState.registry = registry; - setActivePluginRegistry(registry); + setRegistry(registry); await useTempSessionStorePath(); await writeSessionStore({ entries: { diff --git a/src/gateway/server.agent.gateway-server-agent.mocks.ts b/src/gateway/server.agent.gateway-server-agent.mocks.ts new file mode 100644 index 00000000000..3e79278b1c8 --- /dev/null +++ b/src/gateway/server.agent.gateway-server-agent.mocks.ts @@ -0,0 +1,36 @@ +import { vi } from "vitest"; +import type { PluginRegistry } from "../plugins/registry.js"; +import { setActivePluginRegistry } from "../plugins/runtime.js"; + +export const registryState: { registry: PluginRegistry } = { + registry: { + plugins: [], + tools: [], + channels: [], + providers: [], + gatewayHandlers: {}, + httpHandlers: [], + httpRoutes: [], + cliRegistrars: [], + services: [], + diagnostics: [], + } as PluginRegistry, +}; + +export function setRegistry(registry: PluginRegistry) { + registryState.registry = registry; + setActivePluginRegistry(registry); +} + +vi.mock("./server-plugins.js", async () => { + const { setActivePluginRegistry } = await import("../plugins/runtime.js"); + return { + loadGatewayPlugins: (params: { baseMethods: string[] }) => { + setActivePluginRegistry(registryState.registry); + return { + pluginRegistry: registryState.registry, + gatewayMethods: params.baseMethods ?? [], + }; + }, + }; +});