test: dedupe plugin hook runner suites

This commit is contained in:
Peter Steinberger
2026-03-28 03:28:15 +00:00
parent 0946fdf625
commit 1256943a46
15 changed files with 510 additions and 420 deletions

View File

@@ -4,8 +4,29 @@
* Tests the hook runner methods directly since session init is deeply integrated.
*/
import { describe, expect, it, vi } from "vitest";
import { createHookRunner } from "./hooks.js";
import { createMockPluginRegistry } from "./hooks.test-helpers.js";
import { createHookRunnerWithRegistry } from "./hooks.test-helpers.js";
import type {
PluginHookSessionContext,
PluginHookSessionEndEvent,
PluginHookSessionStartEvent,
} from "./types.js";
async function expectSessionHookCall(params: {
hookName: "session_start" | "session_end";
event: PluginHookSessionStartEvent | PluginHookSessionEndEvent;
sessionCtx: PluginHookSessionContext & { sessionKey: string; agentId: string };
}) {
const handler = vi.fn();
const { runner } = createHookRunnerWithRegistry([{ hookName: params.hookName, handler }]);
if (params.hookName === "session_start") {
await runner.runSessionStart(params.event as PluginHookSessionStartEvent, params.sessionCtx);
} else {
await runner.runSessionEnd(params.event as PluginHookSessionEndEvent, params.sessionCtx);
}
expect(handler).toHaveBeenCalledWith(params.event, params.sessionCtx);
}
describe("session hook runner methods", () => {
const sessionCtx = { sessionId: "abc-123", sessionKey: "agent:main:abc", agentId: "main" };
@@ -14,32 +35,21 @@ describe("session hook runner methods", () => {
{
name: "runSessionStart invokes registered session_start hooks",
hookName: "session_start" as const,
methodName: "runSessionStart" as const,
event: { sessionId: "abc-123", sessionKey: "agent:main:abc", resumedFrom: "old-session" },
},
{
name: "runSessionEnd invokes registered session_end hooks",
hookName: "session_end" as const,
methodName: "runSessionEnd" as const,
event: { sessionId: "abc-123", sessionKey: "agent:main:abc", messageCount: 42 },
},
] as const)("$name", async ({ hookName, methodName, event }) => {
const handler = vi.fn();
const registry = createMockPluginRegistry([{ hookName, handler }]);
const runner = createHookRunner(registry);
if (methodName === "runSessionStart") {
await runner.runSessionStart(event, sessionCtx);
} else {
await runner.runSessionEnd(event, sessionCtx);
}
expect(handler).toHaveBeenCalledWith(event, sessionCtx);
] as const)("$name", async ({ hookName, event }) => {
await expectSessionHookCall({ hookName, event, sessionCtx });
});
it("hasHooks returns true for registered session hooks", () => {
const registry = createMockPluginRegistry([{ hookName: "session_start", handler: vi.fn() }]);
const runner = createHookRunner(registry);
const { runner } = createHookRunnerWithRegistry([
{ hookName: "session_start", handler: vi.fn() },
]);
expect(runner.hasHooks("session_start")).toBe(true);
expect(runner.hasHooks("session_end")).toBe(false);