Images are downscaled, re-encoded and stored inside the encrypted document. Closes #10 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
89 lines
4.5 KiB
JavaScript
89 lines
4.5 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);
|
|
});
|