test(agents): share assistant phase text fixtures

This commit is contained in:
Vincent Koc
2026-04-12 10:47:12 +01:00
parent af38536fb9
commit 3686255a55

View File

@@ -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<typeof assistantTextPart>[]) {
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", "<think>secret</think>there"),
);
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: "<think>secret</think>there",
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: "<think>secret</think>there",
textSignature: JSON.stringify({ v: 1, id: "final_2", phase: "final_answer" }),
},
],
};
expect(extractSessionAssistantText(message)).toBe("Hi there");
});
});