import { describe, expect, it, vi } from 'vitest' import { AssetFailure } from '@faunosteo/supabase' import { MAX_PICKED_BYTES, prepareAvatarUpload, sniffImageType, transportSize, type TransportDeps, } from '@/account/avatarTransport' const JPEG = new Uint8Array([0xff, 0xd8, 0xff, 0xe0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) const PNG = new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0, 0, 0, 0, 0, 0, 0, 0]) const WEBP = new Uint8Array([...'RIFF'].map((c) => c.charCodeAt(0)).concat([0, 0, 0, 0], [...'WEBP'].map((c) => c.charCodeAt(0)), [0, 0, 0, 0])) function file(bytes: Uint8Array, name = 'photo.jpg', type = 'image/jpeg', size?: number): File { const f = new File([bytes as BlobPart], name, { type }) if (size !== undefined) Object.defineProperty(f, 'size', { value: size }) return f } function deps(bitmap: { width: number; height: number } | Error, blobSize = 1000): TransportDeps & { canvas: { width: number; height: number }; drawn: unknown[] } { const canvas = { width: 0, height: 0 } const drawn: unknown[] = [] return { canvas, drawn, createImageBitmap: async () => { if (bitmap instanceof Error) throw bitmap return { ...bitmap, close: vi.fn() } }, createCanvas: () => ({ get width() { return canvas.width }, set width(v: number) { canvas.width = v }, get height() { return canvas.height }, set height(v: number) { canvas.height = v }, getContext: () => ({ drawImage: (...args: unknown[]) => drawn.push(args) }), toBlob: (cb: (b: Blob | null) => void) => { const blob = new Blob([new Uint8Array(blobSize)], { type: 'image/jpeg' }) cb(blob) }, }), createObjectURL: () => 'blob:preview', } } async function failureKind(p: Promise) { try { await p return null } catch (e) { return e instanceof AssetFailure ? e.kind : String(e) } } describe('sniffImageType', () => { it('recognises JPEG, PNG and WebP by magic bytes only', () => { expect(sniffImageType(JPEG)).toBe('image/jpeg') expect(sniffImageType(PNG)).toBe('image/png') expect(sniffImageType(WEBP)).toBe('image/webp') }) it('refuses SVG, GIF, a RIFF that is not WEBP and empty input', () => { const svg = new TextEncoder().encode('') expect(sniffImageType(svg)).toBeNull() expect(sniffImageType(new TextEncoder().encode('GIF89a......'))).toBeNull() expect(sniffImageType(new TextEncoder().encode('RIFF....WAVE....'))).toBeNull() expect(sniffImageType(new Uint8Array())).toBeNull() }) }) describe('transportSize', () => { it('caps the longest side at 1024 and never upscales', () => { expect(transportSize(4000, 3000)).toEqual({ width: 1024, height: 768 }) expect(transportSize(3000, 4000)).toEqual({ width: 768, height: 1024 }) expect(transportSize(500, 500)).toEqual({ width: 500, height: 500 }) expect(transportSize(1024, 10)).toEqual({ width: 1024, height: 10 }) }) }) describe('prepareAvatarUpload', () => { it('refuses an oversized file before any decode', async () => { const d = deps({ width: 10, height: 10 }) const spy = vi.spyOn(d, 'createImageBitmap') expect(await failureKind(prepareAvatarUpload(file(JPEG, 'big.jpg', 'image/jpeg', MAX_PICKED_BYTES + 1), d))).toBe('too_large') expect(spy).not.toHaveBeenCalled() }) it('refuses a file whose bytes are not a raster image, whatever its name or type', async () => { const d = deps({ width: 10, height: 10 }) const svg = new TextEncoder().encode('') expect(await failureKind(prepareAvatarUpload(file(svg, 'logo.jpg', 'image/jpeg'), d))).toBe('unsupported_type') }) it('maps a bitmap decode failure to unsupported_type', async () => { const d = deps(new Error('not decodable')) expect(await failureKind(prepareAvatarUpload(file(JPEG), d))).toBe('unsupported_type') }) it('draws a downscaled canvas and returns a JPEG blob with a preview URL', async () => { const d = deps({ width: 4000, height: 3000 }) const result = await prepareAvatarUpload(file(PNG, 'p.png', 'image/png'), d) expect(d.canvas).toEqual({ width: 1024, height: 768 }) expect(d.drawn[0]).toEqual([expect.objectContaining({ width: 4000 }), 0, 0, 1024, 768]) expect(result.blob.type).toBe('image/jpeg') expect(result.previewUrl).toBe('blob:preview') }) it('keeps a small image at its size', async () => { const d = deps({ width: 500, height: 500 }) await prepareAvatarUpload(file(JPEG), d) expect(d.canvas).toEqual({ width: 500, height: 500 }) }) it('refuses a transport blob over 5 MiB', async () => { const d = deps({ width: 1000, height: 1000 }, 5 * 1024 * 1024 + 1) expect(await failureKind(prepareAvatarUpload(file(JPEG), d))).toBe('too_large') }) })