mirror of
https://git.undefined-dev.fr/Blendman974/faceclaw.git
synced 2026-11-19 21:03:09 +00:00
53 lines
3.1 KiB
JavaScript
53 lines
3.1 KiB
JavaScript
const test = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const fs = require('node:fs');
|
|
const vm = require('node:vm');
|
|
const ts = require('typescript');
|
|
function fixture() {
|
|
const timers = new Map(), saved = []; let id = 0, listener, ended = 0, released = 0;
|
|
const context = { exports: {}, global: { isAndroid: false },
|
|
require: () => ({ headUpAngleSetting: { set: value => saved.push(value) } }),
|
|
setTimeout(fn, ms) { timers.set(++id, { fn, ms }); return id; }, clearTimeout: id => timers.delete(id) };
|
|
vm.runInNewContext(ts.transpileModule(fs.readFileSync('app/g2/head-angle.ts', 'utf8'), {
|
|
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 },
|
|
}).outputText, context);
|
|
const device = { beginHeadAngleMeasurement: () => true, endHeadAngleMeasurement: () => ended++, getHeadUpCalibration: () => 10,
|
|
subscribe(fn) { listener = fn; return () => released++; } };
|
|
const sample = (degrees, source = 1) => {
|
|
const r = degrees * Math.PI / 180;
|
|
listener({ x: Math.sin(r), y: 0, z: Math.cos(r), source });
|
|
};
|
|
return { api: context.exports, device, saved, sample, ended: () => ended, released: () => released,
|
|
tick(ms) { for (const [key, timer] of [...timers]) if (timer.ms === ms) { timers.delete(key); timer.fn(); } } };
|
|
}
|
|
test('short measurement compensates firmware calibration, persists one shared angle and releases resources', async () => {
|
|
const h = fixture(); const pending = h.api.setAngleFromCurrentHead(h.device);
|
|
h.sample(30); h.sample(30); h.sample(30); h.tick(1200);
|
|
assert.match(await pending, /Saved.*20°/);
|
|
assert.deepEqual(h.saved, ['20']); assert.equal(h.ended(), 1); assert.equal(h.released(), 1);
|
|
});
|
|
test('no data, motion, unknown calibration and unsupported offsets leave the old angle unchanged', async () => {
|
|
for (const kind of ['none', 'motion', 'unknown', 'range']) {
|
|
const h = fixture();
|
|
if (kind === 'unknown') h.device.getHeadUpCalibration = () => NaN;
|
|
if (kind === 'range') h.device.getHeadUpCalibration = () => 70;
|
|
const pending = h.api.setAngleFromCurrentHead(h.device);
|
|
if (kind !== 'none') { h.sample(30); h.sample(kind === 'motion' ? 0 : 30); h.sample(30); h.tick(1200); }
|
|
else h.tick(4200);
|
|
assert.doesNotMatch(await pending, /Saved/); assert.equal(h.saved.length, 0);
|
|
assert.equal(h.ended(), 1); assert.equal(h.released(), 1);
|
|
}
|
|
});
|
|
test('ring readings never calibrate the glasses; concurrent measurements are rejected', async () => {
|
|
const h = fixture(); const pending = h.api.setAngleFromCurrentHead(h.device);
|
|
assert.match(await h.api.setAngleFromCurrentHead(h.device), /already running/);
|
|
h.sample(30, 2); h.sample(30, 2); h.sample(30, 2); h.tick(4200);
|
|
assert.doesNotMatch(await pending, /Saved/); assert.equal(h.saved.length, 0);
|
|
});
|
|
test('failure to subscribe releases the IMU lease and allows a later attempt', async () => {
|
|
const h = fixture(); h.device.subscribe = () => { throw new Error('offline'); };
|
|
assert.match(await h.api.setAngleFromCurrentHead(h.device), /failed/);
|
|
assert.equal(h.ended(), 1);
|
|
assert.match(await h.api.setAngleFromCurrentHead(null), /Connect/);
|
|
});
|