Add mark validation and pure blur/geometry helpers for step annotation (Refs #25)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-24 13:01:07 -05:00
parent 6066aac6aa
commit eb988b96aa
7 changed files with 108 additions and 4 deletions

View File

@@ -86,3 +86,41 @@ test('displayValue for images: masked when secret, never the data itself', () =>
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);
});

View File

@@ -104,6 +104,10 @@ test('sequence validation: layout, image, text, secret, id, title', () => {
bad((x) => { x.frames[0].image = 5; }, /image must be a string/);
bad((x) => { x.frames[0].text = null; }, /text must be a string/);
bad((x) => { x.frames[0].secret = 'yes'; }, /secret must be a boolean/);
bad((x) => { x.frames[0].marks = 'no'; }, /marks must be an array/);
bad((x) => { x.frames[0].marks = [{ type: 'box', x: 0.9, y: 0, w: 0.5, h: 0.5 }]; }, /not a valid marker or box/);
const okm = structuredClone(d); okm.sections[0].records[1].frames[0].marks = [{ type: 'marker', x: 0.3, y: 0.4 }, { type: 'box', x: 0.1, y: 0.1, w: 0.2, h: 0.2 }];
assert.deepEqual(validate(okm), []);
bad((x) => { x.frames[0].ifStuck = 5; }, /ifStuck must be a string/);
bad((x) => { x.frames[0].stop = 'yes'; }, /stop must be a boolean/);
bad((x) => { x.captured = 'yesterday'; }, /captured must be YYYY-MM-DD/);

View File

@@ -356,8 +356,9 @@ test('addCapture adds a frame and stamps the sequence captured date; clearSecret
assert.equal(q.frames.length, 1);
assert.equal(f.layout, 'under');
assert.match(q.captured, /^\d{4}-\d{2}-\d{2}$/);
f.secret = true; f.ifStuck = 'call 555-0100'; f.stop = true;
f.secret = true; f.ifStuck = 'call 555-0100'; f.stop = true; f.image = 'data:image/png;base64,AAAA'; f.marks = [{ type: 'marker', x: 0.5, y: 0.5 }];
assert.equal(clearSecrets(doc), 1);
assert.equal(f.marks, undefined);
assert.equal(f.ifStuck, '');
assert.equal(f.stop, true);
assert.deepEqual(validate(doc), []);