refactor(agents): dedupe anthropic turn validation fixtures

This commit is contained in:
Peter Steinberger
2026-03-07 17:23:09 +00:00
parent 02b3e85eac
commit ca49372a8d

View File

@@ -10,6 +10,28 @@ function asMessages(messages: unknown[]): AgentMessage[] {
return messages as 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", () => { describe("validate turn edge cases", () => {
it("returns empty array unchanged", () => { it("returns empty array unchanged", () => {
expect(validateGeminiTurns([])).toEqual([]); expect(validateGeminiTurns([])).toEqual([]);
@@ -410,18 +432,7 @@ describe("validateAnthropicTurns strips dangling tool_use blocks", () => {
}); });
it("should handle multiple dangling tool_use blocks", () => { it("should handle multiple dangling tool_use blocks", () => {
const msgs = asMessages([ const msgs = makeDualToolAnthropicTurns([{ type: "text", text: "OK" }]);
{ 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 result = validateAnthropicTurns(msgs); 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", () => { it("should handle mixed tool_use with some having matching tool_result", () => {
const msgs = asMessages([ const msgs = makeDualToolAnthropicTurns([
{ 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: "toolResult", type: "toolResult",
toolUseId: "tool-1", toolUseId: "tool-1",
content: [{ type: "text", text: "Result 1" }], content: [{ type: "text", text: "Result 1" }],
}, },
{ type: "text", text: "Thanks" }, { type: "text", text: "Thanks" },
],
},
]); ]);
const result = validateAnthropicTurns(msgs); const result = validateAnthropicTurns(msgs);
@@ -486,26 +483,12 @@ describe("validateAnthropicTurns strips dangling tool_use blocks", () => {
}); });
it("is replay-safe across repeated validation passes", () => { it("is replay-safe across repeated validation passes", () => {
const msgs = asMessages([ const msgs = makeDualToolAnthropicTurns([
{ 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: "toolResult", type: "toolResult",
toolUseId: "tool-1", toolUseId: "tool-1",
content: [{ type: "text", text: "Result 1" }], content: [{ type: "text", text: "Result 1" }],
}, },
],
},
]); ]);
const firstPass = validateAnthropicTurns(msgs); const firstPass = validateAnthropicTurns(msgs);