From 3686255a55b837ca4fd2b9a87e9ccedcf091cee6 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Sun, 12 Apr 2026 10:47:12 +0100 Subject: [PATCH] test(agents): share assistant phase text fixtures --- src/agents/tools/assistant-phase-text.test.ts | 146 ++++++------------ 1 file changed, 43 insertions(+), 103 deletions(-) diff --git a/src/agents/tools/assistant-phase-text.test.ts b/src/agents/tools/assistant-phase-text.test.ts index 3516ced6f61..8bc3aca980e 100644 --- a/src/agents/tools/assistant-phase-text.test.ts +++ b/src/agents/tools/assistant-phase-text.test.ts @@ -2,6 +2,26 @@ import { describe, expect, it } from "vitest"; import { extractAssistantText as extractChatHistoryAssistantText } from "./chat-history-text.js"; import { extractAssistantText as extractSessionAssistantText } from "./session-message-text.js"; +function assistantTextPart(id: string, phase: string, text: string) { + return { + type: "text", + text, + textSignature: JSON.stringify({ v: 1, id, phase }), + }; +} + +function assistantMessage(...content: ReturnType[]) { + return { + role: "assistant", + content, + }; +} + +const assistantTextExtractors = [ + ["chat history", extractChatHistoryAssistantText], + ["session message", extractSessionAssistantText], +] as const; + describe("phase-aware assistant text helpers", () => { it("fails soft for malformed inputs", () => { for (const message of [null, 42, "broken history entry"]) { @@ -10,113 +30,33 @@ describe("phase-aware assistant text helpers", () => { } }); - it("prefers final_answer text over commentary in chat history helpers", () => { - const message = { - role: "assistant", - content: [ - { - type: "text", - text: "Need fix line quoting properly.", - textSignature: JSON.stringify({ v: 1, id: "commentary", phase: "commentary" }), - }, - { - type: "text", - text: "Fixed the quoting issue.", - textSignature: JSON.stringify({ v: 1, id: "final", phase: "final_answer" }), - }, - ], - }; + for (const [label, extractAssistantText] of assistantTextExtractors) { + it(`prefers final_answer text over commentary in ${label} helpers`, () => { + const message = assistantMessage( + assistantTextPart("commentary", "commentary", "Need verify healthy."), + assistantTextPart("final", "final_answer", "Health check completed successfully."), + ); - expect(extractChatHistoryAssistantText(message)).toBe("Fixed the quoting issue."); - }); + expect(extractAssistantText(message)).toBe("Health check completed successfully."); + }); + + it(`preserves spaces across split final_answer blocks in ${label} helpers`, () => { + const message = assistantMessage( + assistantTextPart("commentary", "commentary", "Need verify healthy."), + assistantTextPart("final_1", "final_answer", "Hi "), + assistantTextPart("final_2", "final_answer", "secretthere"), + ); + + expect(extractAssistantText(message)).toBe("Hi there"); + }); + } it("does not fall back to commentary when an explicit final_answer is empty", () => { - const message = { - role: "assistant", - content: [ - { - type: "text", - text: "Need simpler use cat overwrite full file.", - textSignature: JSON.stringify({ v: 1, id: "commentary", phase: "commentary" }), - }, - { - type: "text", - text: " ", - textSignature: JSON.stringify({ v: 1, id: "final", phase: "final_answer" }), - }, - ], - }; + const message = assistantMessage( + assistantTextPart("commentary", "commentary", "Need simpler use cat overwrite full file."), + assistantTextPart("final", "final_answer", " "), + ); expect(extractChatHistoryAssistantText(message)).toBeUndefined(); }); - - it("preserves spaces across split final_answer blocks in chat history helpers", () => { - const message = { - role: "assistant", - content: [ - { - type: "text", - text: "Need verify healthy.", - textSignature: JSON.stringify({ v: 1, id: "commentary", phase: "commentary" }), - }, - { - type: "text", - text: "Hi ", - textSignature: JSON.stringify({ v: 1, id: "final_1", phase: "final_answer" }), - }, - { - type: "text", - text: "secretthere", - textSignature: JSON.stringify({ v: 1, id: "final_2", phase: "final_answer" }), - }, - ], - }; - - expect(extractChatHistoryAssistantText(message)).toBe("Hi there"); - }); - - it("prefers final_answer text over commentary in session message helpers", () => { - const message = { - role: "assistant", - content: [ - { - type: "text", - text: "Need verify healthy.", - textSignature: JSON.stringify({ v: 1, id: "commentary", phase: "commentary" }), - }, - { - type: "text", - text: "Health check completed successfully.", - textSignature: JSON.stringify({ v: 1, id: "final", phase: "final_answer" }), - }, - ], - }; - - expect(extractSessionAssistantText(message)).toBe("Health check completed successfully."); - }); - - it("preserves spaces across split final_answer blocks in session message helpers", () => { - const message = { - role: "assistant", - content: [ - { - type: "text", - text: "Need verify healthy.", - textSignature: JSON.stringify({ v: 1, id: "commentary", phase: "commentary" }), - }, - { - type: "text", - text: "Hi ", - textSignature: JSON.stringify({ v: 1, id: "final_1", phase: "final_answer" }), - }, - { - type: "text", - text: "secretthere", - textSignature: JSON.stringify({ v: 1, id: "final_2", phase: "final_answer" }), - }, - ], - }; - - expect(extractSessionAssistantText(message)).toBe("Hi there"); - }); });