From a6e597eda346dafb4b9bb0e3bea7fa6d7589a6a4 Mon Sep 17 00:00:00 2001 From: Tak Hoffman <781889+Takhoffman@users.noreply.github.com> Date: Fri, 27 Mar 2026 22:33:24 -0500 Subject: [PATCH] fix(regression): preserve plugin identity in hook test helpers --- src/plugins/hooks.test-helpers.ts | 26 ++++++++++++++----- src/plugins/wired-hooks-inbound-claim.test.ts | 19 ++++++++++++++ 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/plugins/hooks.test-helpers.ts b/src/plugins/hooks.test-helpers.ts index 19e9a982ecf..8884153b78a 100644 --- a/src/plugins/hooks.test-helpers.ts +++ b/src/plugins/hooks.test-helpers.ts @@ -4,20 +4,28 @@ import { createPluginRecord } from "./status.test-helpers.js"; import type { PluginHookAgentContext, PluginHookRegistration } from "./types.js"; export function createMockPluginRegistry( - hooks: Array<{ hookName: string; handler: (...args: unknown[]) => unknown }>, + hooks: Array<{ + hookName: string; + handler: (...args: unknown[]) => unknown; + pluginId?: string; + }>, ): PluginRegistry { + const pluginIds = + hooks.length > 0 + ? [...new Set(hooks.map((hook) => hook.pluginId ?? "test-plugin"))] + : ["test-plugin"]; return { - plugins: [ + plugins: pluginIds.map((pluginId) => createPluginRecord({ - id: "test-plugin", + id: pluginId, name: "Test Plugin", source: "test", - hookCount: hooks.length, + hookCount: hooks.filter((hook) => (hook.pluginId ?? "test-plugin") === pluginId).length, }), - ], + ), hooks: hooks as never[], typedHooks: hooks.map((h) => ({ - pluginId: "test-plugin", + pluginId: h.pluginId ?? "test-plugin", hookName: h.hookName, handler: h.handler, priority: 0, @@ -109,7 +117,11 @@ export function addStaticTestHooks( } export function createHookRunnerWithRegistry( - hooks: Array<{ hookName: string; handler: (...args: unknown[]) => unknown }>, + hooks: Array<{ + hookName: string; + handler: (...args: unknown[]) => unknown; + pluginId?: string; + }>, options?: Parameters[1], ) { const registry = createMockPluginRegistry(hooks); diff --git a/src/plugins/wired-hooks-inbound-claim.test.ts b/src/plugins/wired-hooks-inbound-claim.test.ts index 75eeb63df1d..df2ba0027ec 100644 --- a/src/plugins/wired-hooks-inbound-claim.test.ts +++ b/src/plugins/wired-hooks-inbound-claim.test.ts @@ -107,6 +107,25 @@ describe("inbound_claim hook runner", () => { expect(second).not.toHaveBeenCalled(); }); + it("can target a loaded non-default plugin without mutating the helper registry", async () => { + const first = vi.fn().mockResolvedValue({ handled: true }); + const second = vi.fn().mockResolvedValue({ handled: true }); + const { runner } = createHookRunnerWithRegistry([ + { hookName: "inbound_claim", handler: first, pluginId: "alpha-plugin" }, + { hookName: "inbound_claim", handler: second, pluginId: "beta-plugin" }, + ]); + + const result = await runner.runInboundClaimForPlugin( + "beta-plugin", + inboundClaimEvent, + inboundClaimCtx, + ); + + expect(result).toEqual({ handled: true }); + expect(first).not.toHaveBeenCalled(); + expect(second).toHaveBeenCalledTimes(1); + }); + it("reports missing_plugin when the bound plugin is not loaded", async () => { const { registry, runner } = createHookRunnerWithRegistry([]); registry.plugins = [];