fix(regression): preserve plugin identity in hook test helpers

This commit is contained in:
Tak Hoffman 2026-03-27 22:33:24 -05:00
parent 8075641ce4
commit a6e597eda3
No known key found for this signature in database
2 changed files with 38 additions and 7 deletions

View File

@ -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<TResult>(
}
export function createHookRunnerWithRegistry(
hooks: Array<{ hookName: string; handler: (...args: unknown[]) => unknown }>,
hooks: Array<{
hookName: string;
handler: (...args: unknown[]) => unknown;
pluginId?: string;
}>,
options?: Parameters<typeof createHookRunner>[1],
) {
const registry = createMockPluginRegistry(hooks);

View File

@ -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 = [];