|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +const { mockDownloadFile, mockUploadFile, mockTranscode } = vi.hoisted(() => ({ |
| 7 | + mockDownloadFile: vi.fn(), |
| 8 | + mockUploadFile: vi.fn(), |
| 9 | + mockTranscode: vi.fn(), |
| 10 | +})) |
| 11 | + |
| 12 | +vi.mock('@/lib/uploads/core/storage-service', () => ({ |
| 13 | + downloadFile: mockDownloadFile, |
| 14 | + uploadFile: mockUploadFile, |
| 15 | +})) |
| 16 | + |
| 17 | +vi.mock('@/lib/uploads/server/heic', async (importOriginal) => ({ |
| 18 | + ...(await importOriginal<typeof import('@/lib/uploads/server/heic')>()), |
| 19 | + transcodeHeicToJpeg: mockTranscode, |
| 20 | +})) |
| 21 | + |
| 22 | +import { resolveServableImageBytes } from '@/lib/uploads/server/image-derivative' |
| 23 | + |
| 24 | +/** An ISO-BMFF `ftyp` box declaring a HEVC-coded HEIF still. */ |
| 25 | +function heifBytes(): Buffer { |
| 26 | + const header = Buffer.alloc(16) |
| 27 | + header.writeUInt32BE(16, 0) |
| 28 | + header.write('ftyp', 4, 'ascii') |
| 29 | + header.write('heic', 8, 'ascii') |
| 30 | + return header |
| 31 | +} |
| 32 | + |
| 33 | +const JPEG = Buffer.from([0xff, 0xd8, 0xff, 0xe0]) |
| 34 | + |
| 35 | +describe('resolveServableImageBytes', () => { |
| 36 | + beforeEach(() => { |
| 37 | + vi.clearAllMocks() |
| 38 | + mockDownloadFile.mockRejectedValue(new Error('not found')) |
| 39 | + mockUploadFile.mockResolvedValue(undefined) |
| 40 | + mockTranscode.mockResolvedValue(JPEG) |
| 41 | + }) |
| 42 | + |
| 43 | + it('leaves non-HEIF bytes untouched', async () => { |
| 44 | + expect(await resolveServableImageBytes(JPEG, 'workspace/ws/a.jpg')).toBeNull() |
| 45 | + expect(mockDownloadFile).not.toHaveBeenCalled() |
| 46 | + expect(mockTranscode).not.toHaveBeenCalled() |
| 47 | + }) |
| 48 | + |
| 49 | + it('transcodes and caches on a miss', async () => { |
| 50 | + const result = await resolveServableImageBytes(heifBytes(), 'workspace/ws/a.heic') |
| 51 | + |
| 52 | + expect(result).toEqual({ buffer: JPEG, contentType: 'image/jpeg' }) |
| 53 | + expect(mockTranscode).toHaveBeenCalledOnce() |
| 54 | + expect(mockUploadFile).toHaveBeenCalledOnce() |
| 55 | + }) |
| 56 | + |
| 57 | + it('serves the cached derivative without decoding again', async () => { |
| 58 | + const cached = Buffer.from([0xff, 0xd8, 0xca, 0xce]) |
| 59 | + mockDownloadFile.mockResolvedValue(cached) |
| 60 | + |
| 61 | + const result = await resolveServableImageBytes(heifBytes(), 'workspace/ws/a.heic') |
| 62 | + |
| 63 | + expect(result).toEqual({ buffer: cached, contentType: 'image/jpeg' }) |
| 64 | + expect(mockTranscode).not.toHaveBeenCalled() |
| 65 | + }) |
| 66 | + |
| 67 | + it('keys the derivative by storage key, so replaced content misses the old entry', async () => { |
| 68 | + await resolveServableImageBytes(heifBytes(), 'workspace/ws/111-a.heic') |
| 69 | + await resolveServableImageBytes(heifBytes(), 'workspace/ws/222-a.heic') |
| 70 | + |
| 71 | + const [first, second] = mockUploadFile.mock.calls.map((call) => call[0].customKey) |
| 72 | + expect(first).not.toEqual(second) |
| 73 | + }) |
| 74 | + |
| 75 | + it('still serves the image when caching the derivative fails', async () => { |
| 76 | + mockUploadFile.mockRejectedValue(new Error('s3 down')) |
| 77 | + |
| 78 | + const result = await resolveServableImageBytes(heifBytes(), 'workspace/ws/a.heic') |
| 79 | + |
| 80 | + expect(result).toEqual({ buffer: JPEG, contentType: 'image/jpeg' }) |
| 81 | + }) |
| 82 | + |
| 83 | + it('falls back to the stored bytes when the decode fails', async () => { |
| 84 | + mockTranscode.mockResolvedValue(null) |
| 85 | + |
| 86 | + expect(await resolveServableImageBytes(heifBytes(), 'workspace/ws/a.heic')).toBeNull() |
| 87 | + expect(mockUploadFile).not.toHaveBeenCalled() |
| 88 | + }) |
| 89 | +}) |
0 commit comments