| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | import { EnvironmentId } from "@t3tools/contracts"; |
| 3 | import type { FilePreviewItem } from "../components/FilePreviewModal.types"; |
| 4 | import { filePreviewGallery, filePreviewKey, filePreviewName } from "./filePreviewGallery"; |
| 5 | |
| 6 | const first: FilePreviewItem = { kind: "image", uri: "https://example.com/first.png" }; |
| 7 | const second: FilePreviewItem = { kind: "image", uri: "https://example.com/second.png" }; |
| 8 | |
| 9 | describe("image preview groups", () => { |
| 10 | it("preserves ordering, removes duplicates and retains the tapped transition source", () => { |
| 11 | const selected = { ...second, sourceIdentifier: "thumbnail-2" }; |
| 12 | expect(filePreviewGallery(selected, [first, second, first])).toEqual([first, selected]); |
| 13 | }); |
| 14 | |
| 15 | it("keeps documents out of image groups and preserves a standalone image", () => { |
| 16 | const document: FilePreviewItem = { kind: "pdf", uri: "file:///report.pdf" }; |
| 17 | expect(filePreviewGallery(first, [document])).toEqual([first]); |
| 18 | expect(filePreviewGallery(document, [first, second])).toEqual([document]); |
| 19 | }); |
| 20 | |
| 21 | it("matches remote originals despite metadata and property order differences", () => { |
| 22 | const environmentId = EnvironmentId.make("test"); |
| 23 | const a: FilePreviewItem = { |
| 24 | kind: "image", |
| 25 | environmentId, |
| 26 | resource: { _tag: "attachment", attachmentId: "a", fileName: "old.png" }, |
| 27 | }; |
| 28 | const b: FilePreviewItem = { |
| 29 | kind: "image", |
| 30 | environmentId, |
| 31 | resource: { |
| 32 | attachmentId: "a", |
| 33 | _tag: "attachment", |
| 34 | fileName: "new.png", |
| 35 | mimeType: "image/png", |
| 36 | }, |
| 37 | }; |
| 38 | expect(filePreviewKey(a)).toBe(filePreviewKey(b)); |
| 39 | expect(filePreviewGallery(b, [a])).toEqual([b]); |
| 40 | }); |
| 41 | |
| 42 | it("uses actual filenames instead of captions with punctuation and paths", () => { |
| 43 | expect( |
| 44 | filePreviewName({ ...first, name: 'main: "New thread in..." shows pingdotgg/t3code' }), |
| 45 | ).toBe("first.png"); |
| 46 | expect( |
| 47 | filePreviewName({ |
| 48 | kind: "image", |
| 49 | uri: "https://example.com/image", |
| 50 | name: "test: before... / after", |
| 51 | }), |
| 52 | ).toBe("test_ before___ _ after"); |
| 53 | expect( |
| 54 | filePreviewName({ kind: "document", uri: "file:///tmp/report.docx", name: "report.docx" }), |
| 55 | ).toBe("report.docx"); |
| 56 | }); |
| 57 | }); |