test: tighten after tool call hook assertions

This commit is contained in:
Shakker
2026-05-11 06:58:26 +01:00
parent 31beaeb4c8
commit 7e21db747d

View File

@@ -93,8 +93,10 @@ function expectAfterToolCallPayload(params: {
expectedContext: Record<string, unknown>;
}) {
const { event, context } = requireAfterToolCallCall(params.index);
expect(event).toEqual(expect.objectContaining(params.expectedEvent));
expect(context).toEqual(expect.objectContaining(params.expectedContext));
const { durationMs, ...stableEvent } = event;
expect(typeof durationMs).toBe("number");
expect(stableEvent).toEqual(params.expectedEvent);
expect(context).toEqual(params.expectedContext);
}
let handleToolExecutionStart: typeof import("../agents/pi-embedded-subscribe.handlers.tools.js").handleToolExecutionStart;
@@ -155,6 +157,7 @@ describe("after_tool_call hook wiring", () => {
error: undefined,
runId: "test-run-1",
toolCallId: "wired-hook-call-1",
result: { content: [{ type: "text", text: "file contents" }] },
},
expectedContext: {
toolName: "read",
@@ -165,7 +168,6 @@ describe("after_tool_call hook wiring", () => {
toolCallId: "wired-hook-call-1",
},
});
expect(typeof getAfterToolCallCall().event?.durationMs).toBe("number");
});
it("includes error in after_tool_call event on tool failure", async () => {
@@ -279,13 +281,41 @@ describe("after_tool_call hook wiring", () => {
expect(hookMocks.runner.runAfterToolCall).toHaveBeenCalledTimes(2);
expectAfterToolCallPayload({
index: 0,
expectedEvent: { runId: "run-a", params: { path: "/tmp/path-a.txt" } },
expectedContext: {},
expectedEvent: {
toolName: "read",
params: { path: "/tmp/path-a.txt" },
runId: "run-a",
toolCallId: sharedToolCallId,
result: { content: [{ type: "text", text: "done-a" }] },
error: undefined,
},
expectedContext: {
toolName: "read",
agentId: "agent-a",
sessionKey: "session-a",
sessionId: "ephemeral-a",
runId: "run-a",
toolCallId: sharedToolCallId,
},
});
expectAfterToolCallPayload({
index: 1,
expectedEvent: { runId: "run-b", params: { path: "/tmp/path-b.txt" } },
expectedContext: {},
expectedEvent: {
toolName: "read",
params: { path: "/tmp/path-b.txt" },
runId: "run-b",
toolCallId: sharedToolCallId,
result: { content: [{ type: "text", text: "done-b" }] },
error: undefined,
},
expectedContext: {
toolName: "read",
agentId: "agent-b",
sessionKey: "session-b",
sessionId: "ephemeral-b",
runId: "run-b",
toolCallId: sharedToolCallId,
},
});
});
});