This commit is contained in:
2026-05-08 18:18:36 -03:00
parent c4bb073ca1
commit 5e10af882b
26 changed files with 3150 additions and 0 deletions
+67
View File
@@ -0,0 +1,67 @@
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('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');
});