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,6 +20,42 @@ vi.mock("../infra/agent-events.js", () => ({
emitAgentEvent: vi.fn(), emitAgentEvent: vi.fn(),
})); }));
function createToolHandlerCtx(params: {
runId: string;
sessionKey?: string;
agentId?: string;
onBlockReplyFlush?: unknown;
}) {
return {
params: {
runId: params.runId,
session: { messages: [] },
agentId: params.agentId,
sessionKey: params.sessionKey,
onBlockReplyFlush: params.onBlockReplyFlush,
},
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(),
};
}
describe("after_tool_call hook wiring", () => { describe("after_tool_call hook wiring", () => {
beforeEach(() => { beforeEach(() => {
hookMocks.runner.hasHooks.mockReset(); hookMocks.runner.hasHooks.mockReset();
@@ -36,34 +72,11 @@ 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({
params: { runId: "test-run-1",
runId: "test-run-1", agentId: "main",
session: { messages: [] }, sessionKey: "test-session",
agentId: "main", });
sessionKey: "test-session",
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,
@@ -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,