refactor(history-tests): share array content assertion

This commit is contained in:
Peter Steinberger 2026-03-17 07:50:57 +00:00
parent 2847ad1f8f
commit b531af82d5

View File

@ -4,6 +4,16 @@ import { describe, expect, it } from "vitest";
import { castAgentMessage } from "../../test-helpers/agent-message-fixtures.js";
import { PRUNED_HISTORY_IMAGE_MARKER, pruneProcessedHistoryImages } from "./history-image-prune.js";
function expectArrayMessageContent(
message: AgentMessage | undefined,
errorMessage: string,
): Array<{ type: string; text?: string; data?: string }> {
if (!message || !Array.isArray(message.content)) {
throw new Error(errorMessage);
}
return message.content as Array<{ type: string; text?: string; data?: string }>;
}
describe("pruneProcessedHistoryImages", () => {
const image: ImageContent = { type: "image", data: "abc", mimeType: "image/png" };
@ -22,9 +32,7 @@ describe("pruneProcessedHistoryImages", () => {
const didMutate = pruneProcessedHistoryImages(messages);
expect(didMutate).toBe(true);
const firstUser = messages[0] as Extract<AgentMessage, { role: "user" }> | undefined;
expect(Array.isArray(firstUser?.content)).toBe(true);
const content = firstUser?.content as Array<{ type: string; text?: string; data?: string }>;
const content = expectArrayMessageContent(messages[0], "expected user array content");
expect(content).toHaveLength(2);
expect(content[0]?.type).toBe("text");
expect(content[1]).toMatchObject({ type: "text", text: PRUNED_HISTORY_IMAGE_MARKER });
@ -41,12 +49,9 @@ describe("pruneProcessedHistoryImages", () => {
const didMutate = pruneProcessedHistoryImages(messages);
expect(didMutate).toBe(false);
const first = messages[0] as Extract<AgentMessage, { role: "user" }> | undefined;
if (!first || !Array.isArray(first.content)) {
throw new Error("expected array content");
}
expect(first.content).toHaveLength(2);
expect(first.content[1]).toMatchObject({ type: "image", data: "abc" });
const content = expectArrayMessageContent(messages[0], "expected user array content");
expect(content).toHaveLength(2);
expect(content[1]).toMatchObject({ type: "image", data: "abc" });
});
it("prunes image blocks from toolResult messages that already have assistant replies", () => {
@ -65,12 +70,9 @@ describe("pruneProcessedHistoryImages", () => {
const didMutate = pruneProcessedHistoryImages(messages);
expect(didMutate).toBe(true);
const firstTool = messages[0] as Extract<AgentMessage, { role: "toolResult" }> | undefined;
if (!firstTool || !Array.isArray(firstTool.content)) {
throw new Error("expected toolResult array content");
}
expect(firstTool.content).toHaveLength(2);
expect(firstTool.content[1]).toMatchObject({ type: "text", text: PRUNED_HISTORY_IMAGE_MARKER });
const content = expectArrayMessageContent(messages[0], "expected toolResult array content");
expect(content).toHaveLength(2);
expect(content[1]).toMatchObject({ type: "text", text: PRUNED_HISTORY_IMAGE_MARKER });
});
it("does not change messages when no assistant turn exists", () => {