|
1 | 1 | import { afterEach, describe, expect, it, vi } from "vite-plus/test"; |
2 | 2 |
|
3 | 3 | import { |
| 4 | + createComposerImageThumbnail, |
4 | 5 | compressImageForStash, |
5 | 6 | compressImageToByteLimit, |
6 | 7 | dataUrlToFile, |
@@ -123,6 +124,65 @@ afterEach(() => { |
123 | 124 | globalThis.OffscreenCanvas = originalOffscreenCanvas; |
124 | 125 | }); |
125 | 126 |
|
| 127 | +describe("composer image thumbnails", () => { |
| 128 | + it("decodes a tall original once and caches a bounded center crop", async () => { |
| 129 | + const close = vi.fn(); |
| 130 | + const bitmap = { width: 2304, height: 32766, close }; |
| 131 | + const decode = vi.fn(async () => bitmap); |
| 132 | + const drawImage = vi.fn(); |
| 133 | + const dimensions: number[][] = []; |
| 134 | + vi.stubGlobal("createImageBitmap", decode); |
| 135 | + vi.stubGlobal( |
| 136 | + "OffscreenCanvas", |
| 137 | + class { |
| 138 | + constructor(width: number, height: number) { |
| 139 | + dimensions.push([width, height]); |
| 140 | + } |
| 141 | + getContext() { |
| 142 | + return { drawImage }; |
| 143 | + } |
| 144 | + async convertToBlob() { |
| 145 | + return new Blob(["thumbnail"], { type: "image/png" }); |
| 146 | + } |
| 147 | + }, |
| 148 | + ); |
| 149 | + const original = new File(["original bytes"], "tall.png", { type: "image/png" }); |
| 150 | + const [first, second] = await Promise.all([ |
| 151 | + createComposerImageThumbnail(original), |
| 152 | + createComposerImageThumbnail(original), |
| 153 | + ]); |
| 154 | + expect(first).toBe("data:image/png;base64,dGh1bWJuYWls"); |
| 155 | + expect(second).toBe(first); |
| 156 | + expect(await createComposerImageThumbnail(original)).toBe(first); |
| 157 | + expect(decode).toHaveBeenCalledExactlyOnceWith(original); |
| 158 | + expect(dimensions).toEqual([[256, 256]]); |
| 159 | + expect(drawImage).toHaveBeenCalledWith(bitmap, 0, 15231, 2304, 2304, 0, 0, 256, 256); |
| 160 | + expect(close).toHaveBeenCalledOnce(); |
| 161 | + expect(await original.text()).toBe("original bytes"); |
| 162 | + }); |
| 163 | + |
| 164 | + it("releases the decoded image when thumbnail encoding fails", async () => { |
| 165 | + const close = vi.fn(); |
| 166 | + vi.stubGlobal( |
| 167 | + "createImageBitmap", |
| 168 | + vi.fn(async () => ({ width: 500, height: 500, close })), |
| 169 | + ); |
| 170 | + vi.stubGlobal( |
| 171 | + "OffscreenCanvas", |
| 172 | + class { |
| 173 | + getContext() { |
| 174 | + return { drawImage: vi.fn() }; |
| 175 | + } |
| 176 | + async convertToBlob() { |
| 177 | + throw new Error("encoder unavailable"); |
| 178 | + } |
| 179 | + }, |
| 180 | + ); |
| 181 | + expect(await createComposerImageThumbnail(makeFile(5))).toBeNull(); |
| 182 | + expect(close).toHaveBeenCalledOnce(); |
| 183 | + }); |
| 184 | +}); |
| 185 | + |
126 | 186 | describe("dataUrlToFile", () => { |
127 | 187 | it("decodes a captured image without a fetch request", async () => { |
128 | 188 | const file = dataUrlToFile("data:image/png;base64,AAEC/w==", "window.png", "image/png"); |
|
0 commit comments