From b531af82d5b5cf20ab3c6b3c83bb1778ad886d32 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Tue, 17 Mar 2026 07:50:57 +0000 Subject: [PATCH] refactor(history-tests): share array content assertion --- .../run/history-image-prune.test.ts | 32 ++++++++++--------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/agents/pi-embedded-runner/run/history-image-prune.test.ts b/src/agents/pi-embedded-runner/run/history-image-prune.test.ts index dbed0335435..e25b447827b 100644 --- a/src/agents/pi-embedded-runner/run/history-image-prune.test.ts +++ b/src/agents/pi-embedded-runner/run/history-image-prune.test.ts @@ -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 | 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 | 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 | 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", () => {