|
1 | | -import { EnvironmentId, PROVIDER_SEND_TURN_MAX_FILE_BYTES } from "@t3tools/contracts"; |
| 1 | +import { |
| 2 | + EnvironmentId, |
| 3 | + getProviderAttachmentLimitError, |
| 4 | + PROVIDER_SEND_TURN_MAX_FILE_BYTES, |
| 5 | +} from "@t3tools/contracts"; |
2 | 6 | import { describe, expect, it } from "vite-plus/test"; |
3 | 7 |
|
| 8 | +import { isFileAttachment, isImageAttachment } from "../../types"; |
4 | 9 | import type { ComposerFileAttachment, ComposerImageAttachment } from "../../composerDraftStore"; |
5 | 10 | import { |
6 | 11 | attachmentsToReleaseOnUploadCapabilityLoss, |
| 12 | + classifyComposerAttachmentFile, |
| 13 | + composerFileAttachmentMimeType, |
7 | 14 | fileAttachmentCapabilityBlockReason, |
8 | 15 | fileAttachmentStagingLimit, |
| 16 | + normalizeComposerImageFileMimeType, |
9 | 17 | shouldHandleComposerAttachmentPaste, |
10 | 18 | } from "./composerAttachmentFiles"; |
11 | 19 |
|
12 | 20 | describe("composer attachment files", () => { |
| 21 | + it("keeps DNG originals on the file path without changing their bytes", async () => { |
| 22 | + const bytes = new Uint8Array([0x49, 0x49, 0x2a, 0x00, 0x08, 0, 0, 0, 0xfe, 0xff, 0x00, 0x80]); |
| 23 | + const files = [ |
| 24 | + new File([bytes], "photo.DNG", { type: "image/tiff" }), |
| 25 | + new File([bytes], "photo.dng", { type: "image/png" }), |
| 26 | + new File([bytes], "photo.dng", { type: "image/jpeg" }), |
| 27 | + new File([bytes], "clipboard", { type: "image/dng" }), |
| 28 | + new File([bytes], "clipboard", { type: "image/x-dng" }), |
| 29 | + new File([bytes], "clipboard", { type: "image/x-adobe-dng" }), |
| 30 | + ]; |
| 31 | + |
| 32 | + const attachments = await Promise.all( |
| 33 | + files.map(async (file) => ({ |
| 34 | + kind: classifyComposerAttachmentFile(file), |
| 35 | + bytes: new Uint8Array(await normalizeComposerImageFileMimeType(file).arrayBuffer()), |
| 36 | + })), |
| 37 | + ); |
| 38 | + |
| 39 | + expect(attachments).toEqual(files.map(() => ({ kind: "file", bytes }))); |
| 40 | + }); |
| 41 | + |
| 42 | + it("keeps misleading DNG MIME labels out of downstream image detection and budgets", () => { |
| 43 | + for (const type of ["image/png", "image/jpeg", "image/tiff", "image/dng", ""]) { |
| 44 | + const attachment = { |
| 45 | + type: "file" as const, |
| 46 | + id: "raw", |
| 47 | + name: "photo.DNG", |
| 48 | + mimeType: composerFileAttachmentMimeType({ name: "photo.DNG", type }), |
| 49 | + sizeBytes: 81 * 1024 * 1024, |
| 50 | + }; |
| 51 | + |
| 52 | + expect(isImageAttachment(attachment)).toBe(false); |
| 53 | + expect(isFileAttachment(attachment)).toBe(true); |
| 54 | + expect(getProviderAttachmentLimitError([attachment])).toBeUndefined(); |
| 55 | + } |
| 56 | + }); |
| 57 | + |
| 58 | + it("claims a captioned DNG paste even when its MIME is generic", () => { |
| 59 | + const file = new File(["raw"], "photo.dng", { type: "application/octet-stream" }); |
| 60 | + |
| 61 | + expect( |
| 62 | + shouldHandleComposerAttachmentPaste({ files: [file], plainText: "Inspect this original" }), |
| 63 | + ).toBe(true); |
| 64 | + }); |
| 65 | + |
| 66 | + it("keeps ordinary images on their existing paths", () => { |
| 67 | + const files = [ |
| 68 | + { name: "photo.jpg", type: "image/jpeg" }, |
| 69 | + { name: "export.dng.png", type: "image/png" }, |
| 70 | + { name: "photo.webp", type: "image/webp" }, |
| 71 | + { name: "photo.gif", type: "image/gif" }, |
| 72 | + { name: "photo.heic", type: "" }, |
| 73 | + { name: "scan.tif", type: "image/tiff" }, |
| 74 | + { name: "photo.cr2", type: "image/x-canon-cr2" }, |
| 75 | + ]; |
| 76 | + |
| 77 | + expect(files.map(classifyComposerAttachmentFile)).toEqual([ |
| 78 | + "image", |
| 79 | + "image", |
| 80 | + "image", |
| 81 | + "image", |
| 82 | + "image", |
| 83 | + "unsupported-image", |
| 84 | + "unsupported-image", |
| 85 | + ]); |
| 86 | + }); |
| 87 | + |
13 | 88 | it("preserves text paste when an application adds a synthetic generic file", () => { |
14 | 89 | const file = new File(["clipboard"], "clipboard.rtf", { type: "application/rtf" }); |
15 | 90 |
|
|
0 commit comments