From db7a981f4d50e01e8a410db40a4eb50e52be1e6d Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 11 May 2026 23:33:48 +0100 Subject: [PATCH] test: guard chat view mock attachments --- ui/src/ui/views/chat.test.ts | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/ui/src/ui/views/chat.test.ts b/ui/src/ui/views/chat.test.ts index ea5c782ea9c..aeb2e6730eb 100644 --- a/ui/src/ui/views/chat.test.ts +++ b/ui/src/ui/views/chat.test.ts @@ -19,7 +19,7 @@ import { renderWelcomeState } from "../chat/chat-welcome.ts"; import { renderChatSessionSelect } from "../chat/session-controls.ts"; import type { GatewayBrowserClient } from "../gateway.ts"; import type { ModelCatalogEntry } from "../types.ts"; -import type { ChatQueueItem } from "../ui-types.ts"; +import type { ChatAttachment, ChatQueueItem } from "../ui-types.ts"; import { renderChat, resetChatViewState } from "./chat.ts"; const refreshVisibleToolsEffectiveForCurrentSessionMock = vi.hoisted(() => @@ -44,6 +44,20 @@ const loadSessionsMock = vi.hoisted(() => }), ); +function requireFirstAttachmentsChange( + onAttachmentsChange: ReturnType, +): ChatAttachment[] { + const [call] = onAttachmentsChange.mock.calls; + if (!call) { + throw new Error("expected attachments change call"); + } + const [attachments] = call; + if (!Array.isArray(attachments)) { + throw new Error("expected attachments array"); + } + return attachments as ChatAttachment[]; +} + vi.mock("../icons.ts", () => ({ icons: {}, })); @@ -777,16 +791,14 @@ describe("chat attachment picker", () => { input!.dispatchEvent(new Event("change", { bubbles: true })); await vi.waitFor(() => { - const attachments = onAttachmentsChange.mock.calls[0]?.[0] as - | Array<{ fileName?: string; mimeType?: string; sizeBytes?: number }> - | undefined; + const attachments = requireFirstAttachmentsChange(onAttachmentsChange); expect(attachments).toHaveLength(1); - expect(attachments?.[0]?.fileName).toBe("brief.pdf"); - expect(attachments?.[0]?.mimeType).toBe("application/pdf"); - expect(attachments?.[0]?.sizeBytes).toBe(file.size); + expect(attachments[0]?.fileName).toBe("brief.pdf"); + expect(attachments[0]?.mimeType).toBe("application/pdf"); + expect(attachments[0]?.sizeBytes).toBe(file.size); }); - const nextAttachments = onAttachmentsChange.mock.calls[0]?.[0] ?? []; + const nextAttachments = requireFirstAttachmentsChange(onAttachmentsChange); expect(getChatAttachmentDataUrl(nextAttachments[0])).toMatch(/^data:application\/pdf;base64,/); const preview = renderChatView({ attachments: nextAttachments }); expect(preview.querySelectorAll(".chat-attachment-thumb--file")).toHaveLength(1);