import test from 'node:test'; import assert from 'node:assert/strict'; import { detectImage, validateImage } from '../src/image.js'; import { normalizeToWebp } from '../src/normalize.js'; import { encodePng } from '../src/png.js'; test('detects generated png dimensions', () => { const png = encodePng(32, 24, () => [0, 255, 65, 255]); assert.deepEqual(detectImage(png), { format: 'png', ext: 'png', mime: 'image/png', width: 32, height: 24 }); }); test('rejects unsupported payloads', () => { assert.throws( () => validateImage(Buffer.from(''), { maxBytes: 5 * 1024 * 1024, maxWidth: 6000, maxHeight: 6000, maxPixels: 20_000_000 }), /Only PNG/ ); }); test('rejects gif uploads', () => { const gif = Buffer.from('47494638396101000100800000000000ffffff2c00000000010001000002024401003b', 'hex'); assert.throws( () => validateImage(gif, { maxBytes: 5 * 1024 * 1024, maxWidth: 6000, maxHeight: 6000, maxPixels: 20_000_000 }), /Only PNG and JPEG/ ); }); test('rejects non-square images when square uploads are required', () => { const png = encodePng(32, 24, () => [0, 255, 65, 255]); assert.throws( () => validateImage(png, { maxBytes: 5 * 1024 * 1024, maxWidth: 6000, maxHeight: 6000, maxPixels: 20_000_000, requireSquare: true }), /square/ ); }); test('normalizes png uploads to square webp', async () => { const png = encodePng(32, 32, () => [0, 255, 65, 255]); const normalized = await normalizeToWebp(png); assert.equal(normalized.image.mime, 'image/webp'); assert.equal(normalized.image.ext, 'webp'); assert.equal(normalized.image.width, 32); assert.equal(normalized.image.height, 32); assert.equal(normalized.buffer.subarray(0, 4).toString('ascii'), 'RIFF'); assert.equal(normalized.buffer.subarray(8, 12).toString('ascii'), 'WEBP'); });