From f8da3b51bc2b8ff963465e205a1ce62811eb8e13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20Dr=C4=83gan=20R=C4=83dule=C8=9B?= Date: Thu, 16 Jul 2026 21:05:57 +0200 Subject: [PATCH] test(contracts,web): cover file attachment variant, drafts, and rendering Co-Authored-By: Claude Fable 5 --- .../components/chat/MessagesTimeline.test.tsx | 82 ++++++++++ apps/web/src/composerDraftStore.test.ts | 145 ++++++++++++++++++ apps/web/src/historyBootstrap.test.ts | 37 +++++ apps/web/src/lib/attachmentSize.test.ts | 22 +++ packages/contracts/src/orchestration.test.ts | 80 ++++++++++ 5 files changed, 366 insertions(+) create mode 100644 apps/web/src/lib/attachmentSize.test.ts diff --git a/apps/web/src/components/chat/MessagesTimeline.test.tsx b/apps/web/src/components/chat/MessagesTimeline.test.tsx index b340a248fb..176e5f984d 100644 --- a/apps/web/src/components/chat/MessagesTimeline.test.tsx +++ b/apps/web/src/components/chat/MessagesTimeline.test.tsx @@ -381,6 +381,88 @@ describe("MessagesTimeline", () => { expect(onAnchorSizeChanged).toHaveBeenCalledWith(secondEntry.message.id, 240); }); + it("renders file attachments as chips with name and size instead of images", async () => { + const { MessagesTimeline } = await import("./MessagesTimeline"); + const entry = { + ...buildUserTimelineEntry("Please review the attached report."), + message: { + ...buildUserTimelineEntry("Please review the attached report.").message, + attachments: [ + { + type: "file" as const, + id: "attachment-file-1", + name: "report.pdf", + mimeType: "application/pdf", + sizeBytes: 2_048, + }, + ], + }, + }; + const markup = renderToStaticMarkup( + , + ); + + expect(markup).toContain("report.pdf"); + expect(markup).toContain("2.0 KB"); + expect(markup).toContain("lucide-file-text"); + expect(markup).not.toContain(" { + const { MessagesTimeline } = await import("./MessagesTimeline"); + const entry = { + ...buildUserTimelineEntry("Here is the file."), + message: { + ...buildUserTimelineEntry("Here is the file.").message, + attachments: [ + { + type: "file" as const, + id: "attachment-file-2", + name: "notes.txt", + mimeType: "text/plain", + sizeBytes: 512, + previewUrl: "data:text/plain;base64,aGVsbG8=", + }, + ], + }, + }; + const markup = renderToStaticMarkup( + , + ); + + expect(markup).toContain('download="notes.txt"'); + expect(markup).toContain('href="data:text/plain;base64,aGVsbG8="'); + expect(markup).toContain("512 B"); + }); + + it("renders image attachments in the image grid without file chips", async () => { + const { MessagesTimeline } = await import("./MessagesTimeline"); + const entry = { + ...buildUserTimelineEntry("Take a look."), + message: { + ...buildUserTimelineEntry("Take a look.").message, + attachments: [ + { + type: "image" as const, + id: "attachment-image-1", + name: "screenshot.png", + mimeType: "image/png", + sizeBytes: 1, + previewUrl: "data:image/png;base64,iVBORw0KGgo=", + }, + ], + }, + }; + const markup = renderToStaticMarkup( + , + ); + + expect(markup).toContain('src="data:image/png;base64,iVBORw0KGgo="'); + expect(markup).toContain('alt="screenshot.png"'); + expect(markup).not.toContain("lucide-file-text"); + expect(markup).not.toContain("download="); + }); + it("renders collapse controls for long user messages", async () => { const { MessagesTimeline } = await import("./MessagesTimeline"); const markup = renderToStaticMarkup( diff --git a/apps/web/src/composerDraftStore.test.ts b/apps/web/src/composerDraftStore.test.ts index bc1b710730..3ccd86d0c7 100644 --- a/apps/web/src/composerDraftStore.test.ts +++ b/apps/web/src/composerDraftStore.test.ts @@ -65,6 +65,7 @@ import { markPromotedDraftThreadByRef, markPromotedDraftThreads, markPromotedDraftThreadsByRef, + type ComposerFileAttachment, type ComposerImageAttachment, useComposerDraftStore, DraftId, @@ -104,6 +105,33 @@ function makeImage(input: { }; } +function makeFile(input: { + id: string; + previewUrl: string; + name?: string; + mimeType?: string; + sizeBytes?: number; + lastModified?: number; +}): ComposerFileAttachment { + const name = input.name ?? "report.pdf"; + const mimeType = input.mimeType ?? "application/pdf"; + const sizeBytes = input.sizeBytes ?? 4; + const lastModified = input.lastModified ?? 1_700_000_000_000; + const file = new File([new Uint8Array(sizeBytes).fill(1)], name, { + type: mimeType, + lastModified, + }); + return { + type: "file", + id: input.id, + name, + mimeType, + sizeBytes: file.size, + previewUrl: input.previewUrl, + file, + }; +} + function makeTerminalContext(input: { id: string; text?: string; @@ -345,6 +373,123 @@ describe("composerDraftStore syncPersistedAttachments", () => { }); }); +describe("composerDraftStore file attachments", () => { + const threadId = ThreadId.make("thread-file-attachments"); + const threadRef = scopeThreadRef(TEST_ENVIRONMENT_ID, threadId); + + beforeEach(() => { + removeLocalStorageItem(COMPOSER_DRAFT_STORAGE_KEY); + resetComposerDraftStore(); + }); + + afterEach(() => { + removeLocalStorageItem(COMPOSER_DRAFT_STORAGE_KEY); + }); + + function mergePersistedDraft(attachment: Record) { + const persistApi = useComposerDraftStore.persist as unknown as { + getOptions: () => { + merge: ( + persistedState: unknown, + currentState: ReturnType, + ) => ReturnType; + }; + }; + return persistApi.getOptions().merge( + { + draftsByThreadId: { + [threadId]: { + prompt: "", + attachments: [attachment], + }, + }, + draftThreadsByThreadId: {}, + projectDraftThreadIdByProjectKey: {}, + }, + useComposerDraftStore.getInitialState(), + ); + } + + it("hydrates persisted attachments without a type as images (legacy drafts)", () => { + const mergedState = mergePersistedDraft({ + id: "img-legacy", + name: "legacy.png", + mimeType: "image/png", + sizeBytes: 5, + dataUrl: "data:image/png;base64,aGVsbG8=", + }); + + expect(mergedState.draftsByThreadKey[threadKeyFor(threadId)]?.images).toMatchObject([ + { + type: "image", + id: "img-legacy", + name: "legacy.png", + mimeType: "image/png", + sizeBytes: 5, + previewUrl: "data:image/png;base64,aGVsbG8=", + }, + ]); + }); + + it("hydrates persisted attachments with type file as file attachments", () => { + const mergedState = mergePersistedDraft({ + type: "file", + id: "file-persisted", + name: "report.pdf", + mimeType: "application/pdf", + sizeBytes: 5, + dataUrl: "data:application/pdf;base64,aGVsbG8=", + }); + + expect(mergedState.draftsByThreadKey[threadKeyFor(threadId)]?.images).toMatchObject([ + { + type: "file", + id: "file-persisted", + name: "report.pdf", + mimeType: "application/pdf", + sizeBytes: 5, + previewUrl: "data:application/pdf;base64,aGVsbG8=", + }, + ]); + }); + + it("persists the file type for staged file attachments (round trip)", () => { + const attachment = makeFile({ + id: "file-round-trip", + previewUrl: "data:application/pdf;base64,aGVsbG8=", + }); + useComposerDraftStore.getState().addImage(threadRef, attachment); + useComposerDraftStore.getState().syncPersistedAttachments(threadRef, [ + { + type: "file", + id: attachment.id, + name: attachment.name, + mimeType: attachment.mimeType, + sizeBytes: attachment.sizeBytes, + dataUrl: attachment.previewUrl, + }, + ]); + + const persistApi = useComposerDraftStore.persist as unknown as { + getOptions: () => { + partialize: (state: ReturnType) => unknown; + }; + }; + const persisted = persistApi.getOptions().partialize(useComposerDraftStore.getState()) as { + draftsByThreadKey?: Record> }>; + }; + + expect( + persisted.draftsByThreadKey?.[threadKeyFor(threadId, TEST_ENVIRONMENT_ID)]?.attachments?.[0], + ).toMatchObject({ + type: "file", + id: "file-round-trip", + name: "report.pdf", + mimeType: "application/pdf", + }); + }); +}); + describe("composerDraftStore terminal contexts", () => { const threadId = ThreadId.make("thread-dedupe"); const threadRef = scopeThreadRef(TEST_ENVIRONMENT_ID, threadId); diff --git a/apps/web/src/historyBootstrap.test.ts b/apps/web/src/historyBootstrap.test.ts index b4be13716e..91b62268d8 100644 --- a/apps/web/src/historyBootstrap.test.ts +++ b/apps/web/src/historyBootstrap.test.ts @@ -136,4 +136,41 @@ describe("buildBootstrapInput", () => { expect(result.text).toContain("Attached image"); expect(result.text).toContain("screenshot.png"); }); + + it("captures mixed image and file attachment context in transcript blocks", () => { + const result = buildBootstrapInput( + [ + { + id: messageId("u-mixed"), + role: "user", + text: "Compare the screenshot with the report.", + attachments: [ + { + type: "image", + id: "img-1", + name: "screenshot.png", + mimeType: "image/png", + sizeBytes: 2_048, + }, + { + type: "file", + id: "file-1", + name: "report.pdf", + mimeType: "application/pdf", + sizeBytes: 4_096, + }, + ], + createdAt: "2026-02-09T00:00:00.000Z", + turnId: null, + updatedAt: "2026-02-09T00:00:00.000Z", + streaming: false, + }, + ], + "What differs?", + 1_500, + ); + + expect(result.text).toContain("[Attached image: screenshot.png]"); + expect(result.text).toContain("[Attached file: report.pdf]"); + }); }); diff --git a/apps/web/src/lib/attachmentSize.test.ts b/apps/web/src/lib/attachmentSize.test.ts new file mode 100644 index 0000000000..4cc2158b10 --- /dev/null +++ b/apps/web/src/lib/attachmentSize.test.ts @@ -0,0 +1,22 @@ +import { describe, expect, it } from "vite-plus/test"; + +import { formatAttachmentSizeLabel } from "./attachmentSize"; + +describe("formatAttachmentSizeLabel", () => { + it("formats sub-kilobyte sizes in bytes", () => { + expect(formatAttachmentSizeLabel(0)).toBe("0 B"); + expect(formatAttachmentSizeLabel(512)).toBe("512 B"); + expect(formatAttachmentSizeLabel(1_023)).toBe("1023 B"); + }); + + it("formats kilobyte sizes with one decimal", () => { + expect(formatAttachmentSizeLabel(1_024)).toBe("1.0 KB"); + expect(formatAttachmentSizeLabel(2_048)).toBe("2.0 KB"); + expect(formatAttachmentSizeLabel(1_024 * 1_024 - 1)).toBe("1024.0 KB"); + }); + + it("formats megabyte sizes with one decimal", () => { + expect(formatAttachmentSizeLabel(1_024 * 1_024)).toBe("1.0 MB"); + expect(formatAttachmentSizeLabel(2_202_010)).toBe("2.1 MB"); + }); +}); diff --git a/packages/contracts/src/orchestration.test.ts b/packages/contracts/src/orchestration.test.ts index 29a732ca69..42ba06e2be 100644 --- a/packages/contracts/src/orchestration.test.ts +++ b/packages/contracts/src/orchestration.test.ts @@ -3,6 +3,9 @@ import * as Effect from "effect/Effect"; import * as Schema from "effect/Schema"; import { + ChatAttachment, + ChatFileAttachment, + ChatImageAttachment, DEFAULT_PROVIDER_INTERACTION_MODE, DEFAULT_RUNTIME_MODE, ModelSelection, @@ -743,3 +746,80 @@ it.effect("ModelSelection rejects malformed instance ids", () => assert.strictEqual(result._tag, "Failure"); }), ); + +// ── ChatAttachment: image/file union ────────────────────────────────── + +const decodeChatAttachment = Schema.decodeUnknownEffect(ChatAttachment); +const decodeChatAttachmentArray = Schema.decodeUnknownEffect(Schema.Array(ChatAttachment)); +const decodeChatImageAttachment = Schema.decodeUnknownEffect(ChatImageAttachment); +const decodeChatFileAttachment = Schema.decodeUnknownEffect(ChatFileAttachment); + +it.effect("ChatAttachment decodes a file attachment with a non-image mime type", () => + Effect.gen(function* () { + const parsed = yield* decodeChatAttachment({ + type: "file", + id: "file-1", + name: "report.pdf", + mimeType: "application/pdf", + sizeBytes: 2_048, + }); + assert.strictEqual(parsed.type, "file"); + assert.strictEqual(parsed.name, "report.pdf"); + assert.strictEqual(parsed.mimeType, "application/pdf"); + assert.strictEqual(parsed.sizeBytes, 2_048); + }), +); + +it.effect("ChatAttachment arrays still decode legacy image-only attachments", () => + Effect.gen(function* () { + const parsed = yield* decodeChatAttachmentArray([ + { + type: "image", + id: "img-1", + name: "screenshot.png", + mimeType: "image/png", + sizeBytes: 1_024, + }, + { + type: "image", + id: "img-2", + name: "diagram.jpeg", + mimeType: "image/jpeg", + sizeBytes: 4_096, + }, + ]); + assert.strictEqual(parsed.length, 2); + assert.strictEqual(parsed[0]?.type, "image"); + assert.strictEqual(parsed[1]?.type, "image"); + }), +); + +it.effect("ChatImageAttachment rejects non-image mime types", () => + Effect.gen(function* () { + const result = yield* Effect.exit( + decodeChatImageAttachment({ + type: "image", + id: "img-1", + name: "report.pdf", + mimeType: "application/pdf", + sizeBytes: 2_048, + }), + ); + assert.strictEqual(result._tag, "Failure"); + }), +); + +it.effect("ChatFileAttachment rejects sizeBytes over the 10MB limit", () => + Effect.gen(function* () { + const result = yield* Effect.exit( + decodeChatFileAttachment({ + type: "file", + id: "file-1", + name: "big.bin", + mimeType: "application/octet-stream", + sizeBytes: 10 * 1024 * 1024 + 1, + }), + ); + assert.strictEqual(result._tag, "Failure"); + }), +);