67 lines
1.9 KiB
JavaScript
67 lines
1.9 KiB
JavaScript
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('<svg></svg>'), {
|
|
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('accepts non-square png uploads', () => {
|
|
const png = encodePng(32, 24, () => [0, 255, 65, 255]);
|
|
const image = validateImage(png, {
|
|
maxBytes: 5 * 1024 * 1024,
|
|
maxWidth: 6000,
|
|
maxHeight: 6000,
|
|
maxPixels: 20_000_000
|
|
});
|
|
|
|
assert.equal(image.width, 32);
|
|
assert.equal(image.height, 24);
|
|
});
|
|
|
|
test('normalizes png uploads to webp while preserving aspect ratio', async () => {
|
|
const png = encodePng(32, 24, () => [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, 24);
|
|
assert.equal(normalized.buffer.subarray(0, 4).toString('ascii'), 'RIFF');
|
|
assert.equal(normalized.buffer.subarray(8, 12).toString('ascii'), 'WEBP');
|
|
});
|