Files
final-wishes/test/images.test.js

127 lines
6.7 KiB
JavaScript

import { test } from 'node:test';
import assert from 'node:assert/strict';
import { sniffType, fitSize, isImageDataUrl, dataUrlBytes, formatBytes, labelFromFilename, MAX_IMAGE_DATA_CHARS } from '../src/images.js';
import { validate } from '../src/model.js';
import { addImageField, addRecord, addSection } from '../src/ops.js';
import { displayValue, countSecrets } from '../src/print.js';
import { emptyDocument } from '../src/model.js';
const PNG = Uint8Array.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0, 0]);
const JPG = Uint8Array.from([0xff, 0xd8, 0xff, 0xe0, 0, 0]);
const TINY_PNG = 'data:image/png;base64,iVBORw0KGgo=';
const TINY_JPG = 'data:image/jpeg;base64,/9j/4AAQ';
test('sniffType goes by file signature, not name or claimed type', () => {
assert.equal(sniffType(PNG), 'image/png');
assert.equal(sniffType(JPG), 'image/jpeg');
assert.equal(sniffType(new TextEncoder().encode('<svg xmlns="http://www.w3.org/2000/svg"/>')), '');
assert.equal(sniffType(new TextEncoder().encode('GIF89a......')), '');
assert.equal(sniffType(new Uint8Array([0x89, 0x50])), '');
assert.equal(sniffType(new Uint8Array(0)), '');
});
test('fitSize shrinks the long side to the limit, keeps ratio, never upscales', () => {
assert.deepEqual(fitSize(4000, 3000), { width: 1600, height: 1200 });
assert.deepEqual(fitSize(3000, 4000), { width: 1200, height: 1600 });
assert.deepEqual(fitSize(800, 600), { width: 800, height: 600 });
assert.deepEqual(fitSize(1600, 1600), { width: 1600, height: 1600 });
assert.deepEqual(fitSize(10000, 1), { width: 1600, height: 1 });
assert.deepEqual(fitSize(4000, 3000, 400), { width: 400, height: 300 });
});
test('isImageDataUrl accepts PNG/JPEG data URLs only', () => {
assert.ok(isImageDataUrl(TINY_PNG));
assert.ok(isImageDataUrl(TINY_JPG));
for (const bad of ['', 'data:image/gif;base64,R0lGOD', 'data:image/svg+xml;base64,PHN2Zz4=', 'data:text/html;base64,PGI+',
'data:image/png,rawtext', 'data:image/png;base64,', 'javascript:alert(1)', 'http://x/y.png', 'data:image/png;base64,AA"onerror="x', 5, null]) {
assert.equal(isImageDataUrl(bad), false, String(bad));
}
assert.equal(isImageDataUrl('data:image/png;base64,' + 'A'.repeat(MAX_IMAGE_DATA_CHARS)), false, 'oversize');
});
test('dataUrlBytes, formatBytes, labelFromFilename', () => {
assert.equal(dataUrlBytes('data:image/png;base64,QUJD'), 3);
assert.equal(dataUrlBytes('data:image/png;base64,QUI='), 2);
assert.equal(dataUrlBytes('data:image/png;base64,QQ=='), 1);
assert.equal(formatBytes(500), '500 B');
assert.equal(formatBytes(2048), '2 KB');
assert.equal(formatBytes(3 * 1048576), '3.0 MB');
assert.equal(labelFromFilename('drivers_license-front.PNG'), 'drivers license front');
assert.equal(labelFromFilename('C:\\scans\\passport.jpg'), 'passport');
assert.equal(labelFromFilename('.png'), 'Image');
assert.equal(labelFromFilename(''), 'Image');
});
function docWithField(field) {
const d = emptyDocument('Me');
const s = addSection(d, 'records', 'S');
const r = addRecord(d, s);
r.fields.push(field);
return d;
}
test('model validates image fields', () => {
assert.deepEqual(validate(docWithField({ kind: 'image', label: 'Front', value: TINY_PNG })), []);
assert.deepEqual(validate(docWithField({ kind: 'image', label: 'Front', value: '' })), []);
assert.deepEqual(validate(docWithField({ kind: 'text', label: 'a', value: 'x' })), []);
assert.match(validate(docWithField({ kind: 'image', label: 'F', value: 'data:image/gif;base64,R0lG' })).join('\n'), /PNG or JPEG/);
assert.match(validate(docWithField({ kind: 'image', label: 'F', value: 'not an image' })).join('\n'), /PNG or JPEG/);
assert.match(validate(docWithField({ kind: 'video', label: 'F', value: '' })).join('\n'), /unknown kind/);
});
test('addImageField appends a valid image field', () => {
const d = emptyDocument('Me');
const r = addRecord(d, addSection(d, 'records', 'S'));
const f = addImageField(r, '', TINY_JPG);
assert.equal(f.kind, 'image');
assert.equal(f.label, 'Image');
assert.deepEqual(validate(d), []);
});
test('displayValue for images: masked when secret, never the data itself', () => {
const img = { kind: 'image', label: 'a', value: TINY_PNG };
assert.equal(displayValue(img, false), '[image]');
assert.equal(displayValue({ ...img, secret: true }, false), '[withheld]');
assert.equal(displayValue({ ...img, secret: true }, true), '[image]');
assert.equal(displayValue({ ...img, value: '' }, false), '(no image)');
assert.equal(countSecrets(docWithField({ ...img, secret: true })), 1);
});
import { normRect, isMark, blurBlock, pixelateRegion } from '../src/images.js';
test('normRect orders corners, clamps to the picture, drops stray clicks', () => {
assert.deepEqual(normRect(0.5, 0.6, 0.2, 0.1), { x: 0.2, y: 0.1, w: 0.3, h: 0.5 });
const c = normRect(-1, -1, 2, 0.5);
assert.deepEqual([c.x, c.y, c.w, c.h], [0, 0, 1, 0.5]);
assert.equal(normRect(0.5, 0.5, 0.503, 0.9), null);
});
test('isMark accepts markers and boxes inside the picture only', () => {
assert.equal(isMark({ type: 'marker', x: 0.5, y: 0.5 }), true);
assert.equal(isMark({ type: 'box', x: 0.1, y: 0.1, w: 0.5, h: 0.5 }), true);
assert.equal(isMark({ type: 'box', x: 0.6, y: 0.1, w: 0.5, h: 0.5 }), false); // runs off the right edge
assert.equal(isMark({ type: 'box', x: 0.1, y: 0.1, w: 0, h: 0.5 }), false);
assert.equal(isMark({ type: 'marker', x: 2, y: 0.5 }), false);
assert.equal(isMark({ type: 'blur', x: 0.1, y: 0.1, w: 0.2, h: 0.2 }), false);
assert.equal(isMark(null), false);
});
test('pixelateRegion averages blocks inside the rectangle and leaves the rest alone', () => {
const W = 40, H = 30;
const data = new Uint8ClampedArray(W * H * 4);
for (let y = 0; y < H; y++) for (let x = 0; x < W; x++) { // a pattern with detail in every pixel
const i = (y * W + x) * 4; data[i] = (x * 37 + y * 11) % 256; data[i + 1] = (x * 5) % 256; data[i + 2] = (y * 9) % 256; data[i + 3] = 255;
}
const before = Uint8ClampedArray.from(data);
pixelateRegion(data, W, H, { x: 10, y: 5, w: 24, h: 20 }, 12);
const px = (a, x, y) => [...a.slice((y * W + x) * 4, (y * W + x) * 4 + 4)].join();
assert.equal(px(data, 10, 5), px(data, 21, 16)); // same 12x12 block, same colour
assert.notEqual(px(data, 10, 5), px(data, 22, 5)); // next block differs
assert.notEqual(px(data, 12, 7), px(before, 12, 7)); // detail is gone
assert.equal(px(data, 9, 5), px(before, 9, 5)); // left of the rectangle: untouched
assert.equal(px(data, 34, 5), px(before, 34, 5)); // right of it
assert.equal(px(data, 15, 25), px(before, 15, 25)); // below it
// total of each channel inside a block is preserved up to rounding (it is an average)
assert.ok(blurBlock(300, 14) >= 12 && blurBlock(900, 600) === 200);
});