From e84d89ab06d6d37be1471e63bb62a87cac9704fe Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 21 Feb 2026 23:32:20 +0000 Subject: [PATCH] test(gateway): extract shared parse warning helper --- src/gateway/chat-attachments.test.ts | 111 ++++++++++++--------------- 1 file changed, 48 insertions(+), 63 deletions(-) diff --git a/src/gateway/chat-attachments.test.ts b/src/gateway/chat-attachments.test.ts index de831449b..439825bb1 100644 --- a/src/gateway/chat-attachments.test.ts +++ b/src/gateway/chat-attachments.test.ts @@ -8,6 +8,14 @@ import { const PNG_1x1 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/woAAn8B9FD5fHAAAAAASUVORK5CYII="; +async function parseWithWarnings(message: string, attachments: ChatAttachment[]) { + const logs: string[] = []; + const parsed = await parseMessageWithAttachments(message, attachments, { + log: { warn: (warning) => logs.push(warning) }, + }); + return { parsed, logs }; +} + describe("buildMessageWithAttachments", () => { it("embeds a single image as data URL", () => { const msg = buildMessageWithAttachments("see this", [ @@ -54,18 +62,13 @@ describe("parseMessageWithAttachments", () => { }); it("sniffs mime when missing", async () => { - const logs: string[] = []; - const parsed = await parseMessageWithAttachments( - "see this", - [ - { - type: "image", - fileName: "dot.png", - content: PNG_1x1, - }, - ], - { log: { warn: (message) => logs.push(message) } }, - ); + const { parsed, logs } = await parseWithWarnings("see this", [ + { + type: "image", + fileName: "dot.png", + content: PNG_1x1, + }, + ]); expect(parsed.message).toBe("see this"); expect(parsed.images).toHaveLength(1); expect(parsed.images[0]?.mimeType).toBe("image/png"); @@ -74,39 +77,29 @@ describe("parseMessageWithAttachments", () => { }); it("drops non-image payloads and logs", async () => { - const logs: string[] = []; const pdf = Buffer.from("%PDF-1.4\n").toString("base64"); - const parsed = await parseMessageWithAttachments( - "x", - [ - { - type: "file", - mimeType: "image/png", - fileName: "not-image.pdf", - content: pdf, - }, - ], - { log: { warn: (message) => logs.push(message) } }, - ); + const { parsed, logs } = await parseWithWarnings("x", [ + { + type: "file", + mimeType: "image/png", + fileName: "not-image.pdf", + content: pdf, + }, + ]); expect(parsed.images).toHaveLength(0); expect(logs).toHaveLength(1); expect(logs[0]).toMatch(/non-image/i); }); it("prefers sniffed mime type and logs mismatch", async () => { - const logs: string[] = []; - const parsed = await parseMessageWithAttachments( - "x", - [ - { - type: "image", - mimeType: "image/jpeg", - fileName: "dot.png", - content: PNG_1x1, - }, - ], - { log: { warn: (message) => logs.push(message) } }, - ); + const { parsed, logs } = await parseWithWarnings("x", [ + { + type: "image", + mimeType: "image/jpeg", + fileName: "dot.png", + content: PNG_1x1, + }, + ]); expect(parsed.images).toHaveLength(1); expect(parsed.images[0]?.mimeType).toBe("image/png"); expect(logs).toHaveLength(1); @@ -114,39 +107,31 @@ describe("parseMessageWithAttachments", () => { }); it("drops unknown mime when sniff fails and logs", async () => { - const logs: string[] = []; const unknown = Buffer.from("not an image").toString("base64"); - const parsed = await parseMessageWithAttachments( - "x", - [{ type: "file", fileName: "unknown.bin", content: unknown }], - { log: { warn: (message) => logs.push(message) } }, - ); + const { parsed, logs } = await parseWithWarnings("x", [ + { type: "file", fileName: "unknown.bin", content: unknown }, + ]); expect(parsed.images).toHaveLength(0); expect(logs).toHaveLength(1); expect(logs[0]).toMatch(/unable to detect image mime type/i); }); it("keeps valid images and drops invalid ones", async () => { - const logs: string[] = []; const pdf = Buffer.from("%PDF-1.4\n").toString("base64"); - const parsed = await parseMessageWithAttachments( - "x", - [ - { - type: "image", - mimeType: "image/png", - fileName: "dot.png", - content: PNG_1x1, - }, - { - type: "file", - mimeType: "image/png", - fileName: "not-image.pdf", - content: pdf, - }, - ], - { log: { warn: (message) => logs.push(message) } }, - ); + const { parsed, logs } = await parseWithWarnings("x", [ + { + type: "image", + mimeType: "image/png", + fileName: "dot.png", + content: PNG_1x1, + }, + { + type: "file", + mimeType: "image/png", + fileName: "not-image.pdf", + content: pdf, + }, + ]); expect(parsed.images).toHaveLength(1); expect(parsed.images[0]?.mimeType).toBe("image/png"); expect(parsed.images[0]?.data).toBe(PNG_1x1);