refactor(test): share tool hook handler ctx

This commit is contained in:
Peter Steinberger
2026-02-15 22:04:07 +00:00
parent 5fb4032fb6
commit d9d93485d9

View File

@@ -20,29 +20,19 @@ vi.mock("../infra/agent-events.js", () => ({
emitAgentEvent: vi.fn(), emitAgentEvent: vi.fn(),
})); }));
describe("after_tool_call hook wiring", () => { function createToolHandlerCtx(params: {
beforeEach(() => { runId: string;
hookMocks.runner.hasHooks.mockReset(); sessionKey?: string;
hookMocks.runner.hasHooks.mockReturnValue(false); agentId?: string;
hookMocks.runner.runBeforeToolCall.mockReset(); onBlockReplyFlush?: unknown;
hookMocks.runner.runBeforeToolCall.mockResolvedValue(undefined); }) {
hookMocks.runner.runAfterToolCall.mockReset(); return {
hookMocks.runner.runAfterToolCall.mockResolvedValue(undefined);
});
it("calls runAfterToolCall in handleToolExecutionEnd when hook is registered", async () => {
hookMocks.runner.hasHooks.mockReturnValue(true);
const { handleToolExecutionEnd, handleToolExecutionStart } =
await import("../agents/pi-embedded-subscribe.handlers.tools.js");
const ctx = {
params: { params: {
runId: "test-run-1", runId: params.runId,
session: { messages: [] }, session: { messages: [] },
agentId: "main", agentId: params.agentId,
sessionKey: "test-session", sessionKey: params.sessionKey,
onBlockReplyFlush: undefined, onBlockReplyFlush: params.onBlockReplyFlush,
}, },
state: { state: {
toolMetaById: new Map<string, string | undefined>(), toolMetaById: new Map<string, string | undefined>(),
@@ -64,6 +54,29 @@ describe("after_tool_call hook wiring", () => {
emitToolOutput: vi.fn(), emitToolOutput: vi.fn(),
trimMessagingToolSent: vi.fn(), trimMessagingToolSent: vi.fn(),
}; };
}
describe("after_tool_call hook wiring", () => {
beforeEach(() => {
hookMocks.runner.hasHooks.mockReset();
hookMocks.runner.hasHooks.mockReturnValue(false);
hookMocks.runner.runBeforeToolCall.mockReset();
hookMocks.runner.runBeforeToolCall.mockResolvedValue(undefined);
hookMocks.runner.runAfterToolCall.mockReset();
hookMocks.runner.runAfterToolCall.mockResolvedValue(undefined);
});
it("calls runAfterToolCall in handleToolExecutionEnd when hook is registered", async () => {
hookMocks.runner.hasHooks.mockReturnValue(true);
const { handleToolExecutionEnd, handleToolExecutionStart } =
await import("../agents/pi-embedded-subscribe.handlers.tools.js");
const ctx = createToolHandlerCtx({
runId: "test-run-1",
agentId: "main",
sessionKey: "test-session",
});
await handleToolExecutionStart( await handleToolExecutionStart(
ctx as never, ctx as never,
@@ -103,32 +116,7 @@ describe("after_tool_call hook wiring", () => {
const { handleToolExecutionEnd, handleToolExecutionStart } = const { handleToolExecutionEnd, handleToolExecutionStart } =
await import("../agents/pi-embedded-subscribe.handlers.tools.js"); await import("../agents/pi-embedded-subscribe.handlers.tools.js");
const ctx = { const ctx = createToolHandlerCtx({ runId: "test-run-2" });
params: {
runId: "test-run-2",
session: { messages: [] },
onBlockReplyFlush: undefined,
},
state: {
toolMetaById: new Map<string, string | undefined>(),
toolMetas: [] as Array<{ toolName?: string; meta?: string }>,
toolSummaryById: new Set<string>(),
lastToolError: undefined,
pendingMessagingTexts: new Map<string, string>(),
pendingMessagingTargets: new Map<string, unknown>(),
messagingToolSentTexts: [] as string[],
messagingToolSentTextsNormalized: [] as string[],
messagingToolSentTargets: [] as unknown[],
blockBuffer: "",
},
log: { debug: vi.fn(), warn: vi.fn() },
flushBlockReplyBuffer: vi.fn(),
shouldEmitToolResult: () => false,
shouldEmitToolOutput: () => false,
emitToolSummary: vi.fn(),
emitToolOutput: vi.fn(),
trimMessagingToolSent: vi.fn(),
};
await handleToolExecutionStart( await handleToolExecutionStart(
ctx as never, ctx as never,
@@ -163,26 +151,7 @@ describe("after_tool_call hook wiring", () => {
const { handleToolExecutionEnd } = const { handleToolExecutionEnd } =
await import("../agents/pi-embedded-subscribe.handlers.tools.js"); await import("../agents/pi-embedded-subscribe.handlers.tools.js");
const ctx = { const ctx = createToolHandlerCtx({ runId: "r" });
params: { runId: "r", session: { messages: [] } },
state: {
toolMetaById: new Map<string, string | undefined>(),
toolMetas: [] as Array<{ toolName?: string; meta?: string }>,
toolSummaryById: new Set<string>(),
lastToolError: undefined,
pendingMessagingTexts: new Map<string, string>(),
pendingMessagingTargets: new Map<string, unknown>(),
messagingToolSentTexts: [] as string[],
messagingToolSentTextsNormalized: [] as string[],
messagingToolSentTargets: [] as unknown[],
},
log: { debug: vi.fn(), warn: vi.fn() },
shouldEmitToolResult: () => false,
shouldEmitToolOutput: () => false,
emitToolSummary: vi.fn(),
emitToolOutput: vi.fn(),
trimMessagingToolSent: vi.fn(),
};
await handleToolExecutionEnd( await handleToolExecutionEnd(
ctx as never, ctx as never,