The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { beforeEach, describe, expect, it, vi } from "vite-plus/test"; |
| 2 | |
| 3 | import type { ComposerImageAttachment } from "../../composerDraftStore"; |
| 4 | import { readFileAsDataUrl } from "../../lib/imageCompression"; |
| 5 | import { serializeComposerImages } from "./composerImagePersistence"; |
| 6 | |
| 7 | vi.mock("../../lib/imageCompression", () => ({ readFileAsDataUrl: vi.fn() })); |
| 8 | |
| 9 | function image(id: string): ComposerImageAttachment { |
| 10 | const file = new File([id], `${id}.png`, { type: "image/png" }); |
| 11 | return { |
| 12 | type: "image", |
| 13 | id, |
| 14 | name: file.name, |
| 15 | mimeType: file.type, |
| 16 | sizeBytes: file.size, |
| 17 | previewUrl: `blob:${id}`, |
| 18 | file, |
| 19 | }; |
| 20 | } |
| 21 | |
| 22 | describe("serializeComposerImages", () => { |
| 23 | beforeEach(() => { |
| 24 | vi.mocked(readFileAsDataUrl).mockReset(); |
| 25 | }); |
| 26 | |
| 27 | it("reads only new images and drops removed images without rereading retained bytes", async () => { |
| 28 | const a = image("a"); |
| 29 | const b = image("b"); |
| 30 | vi.mocked(readFileAsDataUrl).mockImplementation(async (file) => |
| 31 | file === a.file ? "data:image/png;base64,YQ==" : "data:image/png;base64,Yg==", |
| 32 | ); |
| 33 | |
| 34 | const first = await serializeComposerImages([a], []); |
| 35 | expect(first).toEqual([ |
| 36 | { |
| 37 | id: "a", |
| 38 | name: "a.png", |
| 39 | mimeType: "image/png", |
| 40 | sizeBytes: 1, |
| 41 | dataUrl: "data:image/png;base64,YQ==", |
| 42 | }, |
| 43 | ]); |
| 44 | expect(readFileAsDataUrl).toHaveBeenCalledTimes(1); |
| 45 | |
| 46 | const second = await serializeComposerImages([a, b], first); |
| 47 | expect(second).toEqual([ |
| 48 | first[0], |
| 49 | { |
| 50 | id: "b", |
| 51 | name: "b.png", |
| 52 | mimeType: "image/png", |
| 53 | sizeBytes: 1, |
| 54 | dataUrl: "data:image/png;base64,Yg==", |
| 55 | }, |
| 56 | ]); |
| 57 | expect(readFileAsDataUrl).toHaveBeenCalledTimes(2); |
| 58 | expect(readFileAsDataUrl).toHaveBeenNthCalledWith(2, b.file); |
| 59 | |
| 60 | expect(await serializeComposerImages([a], second)).toEqual(first); |
| 61 | expect(await serializeComposerImages([], second)).toEqual([]); |
| 62 | expect(readFileAsDataUrl).toHaveBeenCalledTimes(2); |
| 63 | }); |
| 64 | |
| 65 | it("preserves cached bytes without trying to read an unavailable file", async () => { |
| 66 | const a = image("a"); |
| 67 | const persisted = { |
| 68 | id: a.id, |
| 69 | name: a.name, |
| 70 | mimeType: a.mimeType, |
| 71 | sizeBytes: a.sizeBytes, |
| 72 | dataUrl: "data:image/png;base64,YQ==", |
| 73 | }; |
| 74 | vi.mocked(readFileAsDataUrl).mockRejectedValue(new Error("File is unavailable")); |
| 75 | |
| 76 | expect(await serializeComposerImages([a], [persisted])).toEqual([persisted]); |
| 77 | expect(readFileAsDataUrl).not.toHaveBeenCalled(); |
| 78 | }); |
| 79 | |
| 80 | it("keeps composer order when a later image finishes reading first", async () => { |
| 81 | const large = image("large"); |
| 82 | const small = image("small"); |
| 83 | let finishLargeRead = (_dataUrl: string) => {}; |
| 84 | const largeRead = new Promise<string>((resolve) => { |
| 85 | finishLargeRead = resolve; |
| 86 | }); |
| 87 | vi.mocked(readFileAsDataUrl).mockImplementation(async (file) => |
| 88 | file === large.file ? largeRead : "data:image/png;base64,c21hbGw=", |
| 89 | ); |
| 90 | |
| 91 | const serialized = serializeComposerImages([large, small], []); |
| 92 | finishLargeRead("data:image/png;base64,bGFyZ2U="); |
| 93 | expect((await serialized).map(({ id }) => id)).toEqual(["large", "small"]); |
| 94 | }); |
| 95 | |
| 96 | it("omits failed uncached reads while keeping successfully serialized images", async () => { |
| 97 | const a = image("a"); |
| 98 | const b = image("b"); |
| 99 | vi.mocked(readFileAsDataUrl) |
| 100 | .mockRejectedValueOnce(new Error("File is unavailable")) |
| 101 | .mockResolvedValueOnce("data:image/png;base64,Yg=="); |
| 102 | |
| 103 | expect(await serializeComposerImages([a, b], [])).toEqual([ |
| 104 | { |
| 105 | id: "b", |
| 106 | name: "b.png", |
| 107 | mimeType: "image/png", |
| 108 | sizeBytes: 1, |
| 109 | dataUrl: "data:image/png;base64,Yg==", |
| 110 | }, |
| 111 | ]); |
| 112 | expect(readFileAsDataUrl).toHaveBeenCalledTimes(2); |
| 113 | }); |
| 114 | }); |