| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | |
| 3 | import { imageMimeType } from "./image.ts"; |
| 4 | |
| 5 | describe("imageMimeType", () => { |
| 6 | it("recognizes a picture the file picker left untyped", () => { |
| 7 | // The document picker types every pick as a plain file, often with no mime at all. |
| 8 | expect(imageMimeType({ name: "IMG_4997.PNG", mimeType: "" })).toBe("image/png"); |
| 9 | expect(imageMimeType({ name: "shot.jpg", mimeType: "application/octet-stream" })).toBe( |
| 10 | "image/jpeg", |
| 11 | ); |
| 12 | }); |
| 13 | |
| 14 | it("trusts a declared supported type", () => { |
| 15 | expect(imageMimeType({ name: "no-extension", mimeType: "image/webp" })).toBe("image/webp"); |
| 16 | expect(imageMimeType({ name: "x.png", mimeType: "image/png; charset=binary" })).toBe( |
| 17 | "image/png", |
| 18 | ); |
| 19 | }); |
| 20 | |
| 21 | it("leaves images the provider cannot accept as files", () => { |
| 22 | // Promoting these would put an attachment on the image path that cannot be sent. |
| 23 | expect(imageMimeType({ name: "scan.heic", mimeType: "image/heic" })).toBeNull(); |
| 24 | expect(imageMimeType({ name: "logo.svg", mimeType: "image/svg+xml" })).toBeNull(); |
| 25 | }); |
| 26 | |
| 27 | it("leaves documents and videos alone", () => { |
| 28 | expect(imageMimeType({ name: "notes.txt", mimeType: "text/plain" })).toBeNull(); |
| 29 | expect(imageMimeType({ name: "clip.mp4", mimeType: "video/mp4" })).toBeNull(); |
| 30 | expect(imageMimeType({ name: "noext", mimeType: "" })).toBeNull(); |
| 31 | }); |
| 32 | }); |
| 33 | |
| 34 | describe("legacy attachments", () => { |
| 35 | it("recognizes a picture that was sent before pictures were typed by content", () => { |
| 36 | // Older messages recorded these as plain files; the bytes are still a picture, so the |
| 37 | // chat view can render a thumbnail rather than a download row. |
| 38 | expect(imageMimeType({ name: "IMG_4996.PNG", mimeType: "application/octet-stream" })).toBe( |
| 39 | "image/png", |
| 40 | ); |
| 41 | expect(imageMimeType({ name: "1000000020.png", mimeType: "" })).toBe("image/png"); |
| 42 | }); |
| 43 | }); |
| 44 | |
| 45 | describe("a declared non-image type", () => { |
| 46 | it("wins over a misleading picture extension", () => { |
| 47 | // The filename fallback exists for attachments whose type was never recorded. A definite |
| 48 | // type is what the file actually is, so a `.png` name cannot promote a PDF to a picture. |
| 49 | expect(imageMimeType({ name: "report.png", mimeType: "application/pdf" })).toBeNull(); |
| 50 | expect(imageMimeType({ name: "archive.jpg", mimeType: "application/zip" })).toBeNull(); |
| 51 | expect(imageMimeType({ name: "clip.png", mimeType: "video/mp4" })).toBeNull(); |
| 52 | }); |
| 53 | |
| 54 | it("still falls back when the type is absent or generic", () => { |
| 55 | expect(imageMimeType({ name: "IMG_4996.PNG", mimeType: "application/octet-stream" })).toBe( |
| 56 | "image/png", |
| 57 | ); |
| 58 | expect(imageMimeType({ name: "1000000020.png", mimeType: "" })).toBe("image/png"); |
| 59 | }); |
| 60 | }); |