import assert from 'node:assert/strict' import { test } from 'node:test' import jpeg from 'jpeg-js' import { buildSrcset, planVariantSizes, pickVariant, variantFormats, variantFileName, } from '../src/domain/image-plan.ts' import type { Photo } from '../src/domain/types.ts' import { readExif } from '../src/images/exif.ts' import { processImage } from '../src/images/process.ts' test('plans every standard width that fits, plus the largest size, never upscaling', () => { assert.deepEqual( planVariantSizes(4000, 3000).map((s) => s.width), [480, 960, 1600, 2400], ) assert.deepEqual(planVariantSizes(4000, 3000).at(-1), { width: 2400, height: 1800, }) assert.deepEqual( planVariantSizes(1200, 800).map((s) => s.width), [480, 960, 1200], ) assert.deepEqual(planVariantSizes(300, 200), [{ width: 300, height: 200 }]) }) test('caps the long edge of tall portraits at 2400px', () => { const sizes = planVariantSizes(2000, 3000) assert.deepEqual(sizes.at(-1), { width: 1600, height: 2400 }) assert.deepEqual( sizes.map((s) => s.width), [480, 960, 1600], ) }) test('rejects oversized sources with a fix-it message', () => { assert.throws(() => planVariantSizes(6000, 4000), /24\.0 MP.*at most 12 MP/) assert.throws(() => planVariantSizes(0, 10), /no pixels/) }) test('every size gets WebP and JPEG; file names and srcsets line up', () => { const variants = planVariantSizes(1600, 1000).flatMap((size) => variantFormats.map((format) => ({ ...size, format })), ) assert.deepEqual(variantFormats, ['webp', 'jpeg']) assert.equal(variants.length, 6) assert.equal(variantFileName({ width: 960, format: 'jpeg' }), '960.jpg') const photo = { id: 'p1', variants: variants.map((v) => ({ ...v, bytes: 1 })), } as Photo assert.equal( buildSrcset( photo, 'webp', (p, v) => `/media/${p.id}/${variantFileName(v)}`, ), '/media/p1/480.webp 480w, /media/p1/960.webp 960w, /media/p1/1600.webp 1600w', ) assert.equal(pickVariant(photo, 'jpeg', 1200)?.width, 1600) assert.equal(pickVariant(photo, 'jpeg', 5000)?.width, 1600) }) /** Little-endian TIFF/EXIF payload with the given IFD0 entries plus a GPS IFD. */ function exifWithGps(orientation: number, artist: string) { const enc = new TextEncoder() const artistBytes = enc.encode(`${artist}\0`) const tiff = new Uint8Array(200) const v = new DataView(tiff.buffer) tiff.set([0x49, 0x49]) v.setUint16(2, 42, true) v.setUint32(4, 8, true) v.setUint16(8, 3, true) // Orientation (SHORT) v.setUint16(10, 0x0112, true) v.setUint16(12, 3, true) v.setUint32(14, 1, true) v.setUint16(18, orientation, true) // Artist (ASCII) at offset 100 v.setUint16(22, 0x013b, true) v.setUint16(24, 2, true) v.setUint32(26, artistBytes.length, true) v.setUint32(30, 100, true) // GPS IFD pointer (LONG) to offset 60 v.setUint16(34, 0x8825, true) v.setUint16(36, 4, true) v.setUint32(38, 1, true) v.setUint32(42, 60, true) v.setUint32(46, 0, true) // GPS IFD: GPSLatitudeRef = "N" v.setUint16(60, 1, true) v.setUint16(62, 0x0001, true) v.setUint16(64, 2, true) v.setUint32(66, 2, true) tiff.set(enc.encode('N\0'), 70) v.setUint32(74, 0, true) tiff.set(artistBytes, 100) const payload = new Uint8Array(6 + tiff.length) payload.set([0x45, 0x78, 0x69, 0x66, 0, 0]) payload.set(tiff, 6) return payload } /** The EXIF APP1 segment of a JPEG, or null. */ function app1Segment(bytes: Uint8Array) { let offset = 2 while (offset + 4 < bytes.length && bytes[offset] === 0xff) { const marker = bytes[offset + 1]! const length = (bytes[offset + 2]! << 8) | bytes[offset + 3]! if (marker === 0xe1) return bytes.subarray(offset + 4, offset + 2 + length) if (marker === 0xda) return null offset += 2 + length } return null } function hasTag(segment: Uint8Array, tag: number) { for (let i = 0; i + 1 < segment.length; i += 1) { if (segment[i] === (tag & 0xff) && segment[i + 1] === tag >> 8) return true } return false } function testJpeg(width: number, height: number, exif?: Uint8Array) { const data = new Uint8Array(width * height * 4) for (let y = 0; y < height; y += 1) { for (let x = 0; x < width; x += 1) { const i = (y * width + x) * 4 data[i] = (x / width) * 255 data[i + 1] = (y / height) * 255 data[i + 2] = 128 data[i + 3] = 255 } } return new Uint8Array( jpeg.encode( { data, width, height, ...(exif ? { exifBuffer: exif } : {}), } as Parameters[0], 90, ).data, ) } test('processImage rotates by EXIF orientation, drops GPS, and writes only the credit', async () => { const source = testJpeg(1200, 800, exifWithGps(6, 'Original Camera Owner')) assert.equal(readExif(source).orientation, 6) assert.equal(readExif(source).credits.artist, 'Original Camera Owner') const result = await processImage(source, { creditExif: { artist: 'Jane Doe', copyright: '© 2026 Jane Doe' }, }) assert.deepEqual( [result.width, result.height], [800, 1200], 'portrait after rotation', ) assert.deepEqual( result.files.map( (f) => `${f.variant.width}x${f.variant.height}.${f.variant.format}`, ), ['480x720.webp', '480x720.jpeg', '800x1200.webp', '800x1200.jpeg'], ) for (const file of result.files) { assert.equal(file.variant.bytes, file.bytes.length) const text = new TextDecoder('latin1').decode(file.bytes) assert.ok( !text.includes('Original Camera Owner'), 'source EXIF is not copied', ) if (file.variant.format === 'jpeg') { const exif = readExif(file.bytes) assert.equal(exif.orientation, 1, 'outputs are already upright') assert.deepEqual(exif.credits, { artist: 'Jane Doe', copyright: '© 2026 Jane Doe', }) const app1 = app1Segment(file.bytes) assert.ok(app1, 'credit EXIF is present') assert.ok(!hasTag(app1, 0x8825), 'no GPS IFD pointer') const decoded = jpeg.decode(file.bytes) assert.deepEqual( [decoded.width, decoded.height], [file.variant.width, file.variant.height], ) } else { assert.equal(text.slice(0, 4), 'RIFF') assert.equal(text.slice(8, 12), 'WEBP') } } assert.match(result.blurhash, /^[0-9A-Za-z#$%*+,\-.:;=?@[\]^_{|}~]{20,}$/) assert.match(result.lqip, /^data:image\/jpeg;base64,/) assert.equal( result.credits.artist, 'Original Camera Owner', 'source credits are reported, not embedded', ) }) test('processImage refuses formats it cannot decode', async () => { await assert.rejects( processImage(new TextEncoder().encode('not an image at all')), /JPEG, PNG, or WebP/, ) })