video_monitor/tests/test_control_preview.cjs

47 lines
2.1 KiB
JavaScript
Raw Permalink Normal View History

2026-09-04 18:16:14 +08:00
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const vm = require('node:vm');
const template = fs.readFileSync(path.join(__dirname, '../templates/app/control/index.html'), 'utf8');
assert.match(template, /openYoloPreview\('\+z\.id\+'\)/);
assert.match(template, /id="yoloPreviewCanvas"/);
assert.match(template, /openPreviewStart/);
assert.match(template, /openPreviewData/);
assert.match(template, /openPreviewStop/);
assert.match(template, /ResizeObserver/);
assert.match(template, /requestAnimationFrame\(animateYoloOverlay\)/);
const start = template.indexOf('function computeContainRect(');
const end = template.indexOf('function resizeYoloCanvas(');
const context = {};
vm.createContext(context);
vm.runInContext(template.slice(start, end), context);
const wide = context.computeContainRect(1000, 1000, 16, 9);
assert.equal(wide.width, 1000);
assert.equal(Math.round(wide.height), 563);
assert.equal(Math.round(wide.y), 219);
const tall = context.computeContainRect(1000, 500, 4, 3);
assert.equal(Math.round(tall.height), 500);
assert.equal(Math.round(tall.width), 667);
assert.equal(Math.round(tall.x), 167);
const projectStart = template.indexOf('function projectTrackBox(');
const projectEnd = template.indexOf('function smoothTrackBox(');
vm.runInContext(template.slice(projectStart, projectEnd), context);
const moving = context.projectTrackBox({
box: [.1, .2, .2, .4], velocity: [.1, 0, .1, 0], observed: true,
}, 10, 10.5);
assert.ok(Math.abs(moving[0] - .15) < 1e-9);
assert.ok(Math.abs(moving[2] - .25) < 1e-9);
const runaway = context.projectTrackBox({
box: [.1, .2, .2, .4], velocity: [10, 10, 10, 10], observed: true,
}, 10, 20);
assert.ok(runaway.every(v => v >= 0 && v <= 1));
assert.ok(runaway[0] < .3, 'prediction displacement must be capped');
const retained = context.projectTrackBox({
box: [.1, .2, .2, .4], velocity: [1, 1, 1, 1], observed: false,
}, 10, 11);
assert.deepEqual(Array.from(retained), [.1, .2, .2, .4]);
console.log('PASS: preview lifecycle, contain coordinates and motion projection');