From ca49372a8d3efe62eb0fa2da816aeff5cbfda7da Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 7 Mar 2026 17:23:09 +0000 Subject: [PATCH] refactor(agents): dedupe anthropic turn validation fixtures --- ...pi-embedded-helpers.validate-turns.test.ts | 81 ++++++++----------- 1 file changed, 32 insertions(+), 49 deletions(-) diff --git a/src/agents/pi-embedded-helpers.validate-turns.test.ts b/src/agents/pi-embedded-helpers.validate-turns.test.ts index 8ba3f383001..342dbc8dfef 100644 --- a/src/agents/pi-embedded-helpers.validate-turns.test.ts +++ b/src/agents/pi-embedded-helpers.validate-turns.test.ts @@ -10,6 +10,28 @@ function asMessages(messages: unknown[]): AgentMessage[] { return messages as AgentMessage[]; } +function makeDualToolUseAssistantContent() { + return [ + { type: "toolUse", id: "tool-1", name: "test1", input: {} }, + { type: "toolUse", id: "tool-2", name: "test2", input: {} }, + { type: "text", text: "Done" }, + ]; +} + +function makeDualToolAnthropicTurns(nextUserContent: unknown[]) { + return asMessages([ + { role: "user", content: [{ type: "text", text: "Use tools" }] }, + { + role: "assistant", + content: makeDualToolUseAssistantContent(), + }, + { + role: "user", + content: nextUserContent, + }, + ]); +} + describe("validate turn edge cases", () => { it("returns empty array unchanged", () => { expect(validateGeminiTurns([])).toEqual([]); @@ -410,18 +432,7 @@ describe("validateAnthropicTurns strips dangling tool_use blocks", () => { }); it("should handle multiple dangling tool_use blocks", () => { - const msgs = asMessages([ - { role: "user", content: [{ type: "text", text: "Use tools" }] }, - { - role: "assistant", - content: [ - { type: "toolUse", id: "tool-1", name: "test1", input: {} }, - { type: "toolUse", id: "tool-2", name: "test2", input: {} }, - { type: "text", text: "Done" }, - ], - }, - { role: "user", content: [{ type: "text", text: "OK" }] }, - ]); + const msgs = makeDualToolAnthropicTurns([{ type: "text", text: "OK" }]); const result = validateAnthropicTurns(msgs); @@ -432,27 +443,13 @@ describe("validateAnthropicTurns strips dangling tool_use blocks", () => { }); it("should handle mixed tool_use with some having matching tool_result", () => { - const msgs = asMessages([ - { role: "user", content: [{ type: "text", text: "Use tools" }] }, + const msgs = makeDualToolAnthropicTurns([ { - role: "assistant", - content: [ - { type: "toolUse", id: "tool-1", name: "test1", input: {} }, - { type: "toolUse", id: "tool-2", name: "test2", input: {} }, - { type: "text", text: "Done" }, - ], - }, - { - role: "user", - content: [ - { - type: "toolResult", - toolUseId: "tool-1", - content: [{ type: "text", text: "Result 1" }], - }, - { type: "text", text: "Thanks" }, - ], + type: "toolResult", + toolUseId: "tool-1", + content: [{ type: "text", text: "Result 1" }], }, + { type: "text", text: "Thanks" }, ]); const result = validateAnthropicTurns(msgs); @@ -486,25 +483,11 @@ describe("validateAnthropicTurns strips dangling tool_use blocks", () => { }); it("is replay-safe across repeated validation passes", () => { - const msgs = asMessages([ - { role: "user", content: [{ type: "text", text: "Use tools" }] }, + const msgs = makeDualToolAnthropicTurns([ { - role: "assistant", - content: [ - { type: "toolUse", id: "tool-1", name: "test1", input: {} }, - { type: "toolUse", id: "tool-2", name: "test2", input: {} }, - { type: "text", text: "Done" }, - ], - }, - { - role: "user", - content: [ - { - type: "toolResult", - toolUseId: "tool-1", - content: [{ type: "text", text: "Result 1" }], - }, - ], + type: "toolResult", + toolUseId: "tool-1", + content: [{ type: "text", text: "Result 1" }], }, ]);