forked from Blendman974/faceclaw
215 lines
12 KiB
JavaScript
215 lines
12 KiB
JavaScript
const test = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
const vm = require('node:vm');
|
|
const ts = require('typescript');
|
|
const read = (file) => fs.readFileSync(path.join(__dirname, '..', file), 'utf8');
|
|
const js = (source) => ts.transpileModule(source, {
|
|
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 },
|
|
}).outputText;
|
|
function load(file, modules = {}) {
|
|
const context = { exports: {}, console, require(name) {
|
|
assert.ok(name in modules, name); return modules[name];
|
|
} };
|
|
vm.runInNewContext(js(read(file)), context);
|
|
return context.exports;
|
|
}
|
|
const monitor = load('app/ui/input-monitor.ts');
|
|
const event = (type, timestampMs = 1000) => ({ type, source: 'ring', timestampMs });
|
|
|
|
function shellDeclarations() {
|
|
const source = ts.createSourceFile('shell.ts', read('app/ui/shell/shell.ts'), ts.ScriptTarget.Latest, true);
|
|
const names = new Set(['rawInputEventToInputEvent', 'rawInputEventToPayload', 'eventSourceToString', 'scrollEvent']);
|
|
const declarations = source.statements.filter((s) => ts.isFunctionDeclaration(s) && names.has(s.name?.text));
|
|
assert.equal(declarations.length, names.size);
|
|
const shell = source.statements.find((s) => ts.isClassDeclaration(s) && s.name?.text === 'Shell');
|
|
const methods = shell.members.filter((s) => ['receiveInput', 'routeInput'].includes(s.name?.getText(source)));
|
|
const context = { exports: {}, acceptInput: monitor.acceptInput,
|
|
makeInputEvent: (payload) => ({ ...payload, timestampMs: 12345678 }),
|
|
...load('app/g2/events.ts') };
|
|
vm.createContext(context);
|
|
vm.runInContext(js(declarations.map((s) => s.getText(source)).join('\n') +
|
|
'\nexport class Shell {\n' + methods.map((s) => s.getText(source)).join('\n') + '\n}'), context);
|
|
return context;
|
|
}
|
|
|
|
test('new SysEvent decodes independently of click, double-tap, hold and release', () => {
|
|
const d = shellDeclarations();
|
|
for (const source of [0, 2]) {
|
|
const decoded = d.rawInputEventToPayload({ kind: 'sys-event', eventType: 14, eventSource: source });
|
|
assert.equal(decoded.type, 'ring-press'); assert.equal(decoded.source, 'ring');
|
|
}
|
|
// Synthetic watch touch-down (TOUCH_EVENT_FROM_WATCH, never sent by firmware).
|
|
const watch = d.rawInputEventToPayload({ kind: 'sys-event', eventType: 14, eventSource: 4 });
|
|
assert.equal(watch.type, 'ring-press'); assert.equal(watch.source, 'watch');
|
|
for (const source of [1, 3, 99, undefined]) {
|
|
assert.equal(d.rawInputEventToPayload({ kind: 'sys-event', eventType: 14, eventSource: source }).type, 'unknown');
|
|
}
|
|
for (const [id, type] of [[0,'click'],[3,'double-click'],[9,'long-press'],[10,'long-press-release'],[11,'short-then-long-press']]) {
|
|
assert.equal(d.rawInputEventToPayload({ kind: 'sys-event', eventType: id, eventSource: 2 }).type, type);
|
|
}
|
|
});
|
|
|
|
test('press is observed before routing and does not wake, operate shell menus or replace hold state', async () => {
|
|
const { exports: { Shell } } = shellDeclarations();
|
|
for (const screenOn of [true, false]) for (const focus of ['window', 'sidebar']) for (const atBase of [true, false]) {
|
|
const s = new Shell(), seen = [], delivered = [];
|
|
const unsubscribe = monitor.addInputListener((e) => seen.push(e));
|
|
Object.assign(s, { screenOn, focus, stack: { isAtBase: () => atBase },
|
|
foregroundWindow: () => ({ handleInput(e) { assert.equal(seen.length, 1); delivered.push(e); } }),
|
|
syncInputFocus() {}, lastInput: event('long-press'), lastInputAtMs: 123 });
|
|
try {
|
|
const result = await s.receiveInput(event('ring-press'));
|
|
assert.equal(result.shell, false);
|
|
assert.equal(delivered.length, Number(screenOn && focus === 'window' && atBase));
|
|
assert.equal(seen.length, 1);
|
|
assert.equal(s.lastInput.type, 'long-press'); assert.equal(s.lastInputAtMs, 123);
|
|
} finally { unsubscribe(); }
|
|
}
|
|
});
|
|
|
|
test('bounded history preserves equal-time events, snapshots and arrival gaps; pause and clear work', () => {
|
|
const log = new monitor.InputEventLog();
|
|
const first = event('ring-press'); log.add(first); first.type = 'unknown';
|
|
log.add(event('click')); log.add(event('double-click', 1250));
|
|
assert.equal(log.entries[2].event.type, 'ring-press');
|
|
assert.equal(log.entries[2].gapMs, null); assert.equal(log.entries[1].gapMs, 0);
|
|
assert.equal(log.entries[0].gapMs, 250);
|
|
log.paused = true; log.add(event('click')); assert.equal(log.count, 3);
|
|
log.paused = false;
|
|
for (let i = 0; i < 210; i++) log.add(event('click', 2000 + i));
|
|
assert.equal(log.entries.length, 200); assert.equal(log.count, 213);
|
|
log.clear(); log.add(event('ring-press')); assert.equal(log.count, 1); assert.equal(log.entries[0].gapMs, null);
|
|
assert.equal(monitor.inputTimestamp(new Date(2026, 0, 2, 3, 4, 5, 6).getTime()), '03:04:05.006');
|
|
});
|
|
|
|
test('Input events captures while visible, pauses for history, and unsubscribes on removal', () => {
|
|
const textwrap = load('app/graphics/textwrap.ts');
|
|
const graphics = require('../.test-build/app/graphics/image.js');
|
|
const { BdfFont } = load('app/graphics/bdffont.ts', { '@nativescript/core': {} });
|
|
const font = BdfFont.parse(read('app/fonts/terminus/ter-u18n.bdf'));
|
|
class RecordingImage extends graphics.GrayImage {
|
|
texts = [];
|
|
drawText(font, x, y, text, value) {
|
|
this.texts.push({ x, y, text, width: font.measureText(text), height: font.lineHeight });
|
|
super.drawText(font, x, y, text, value);
|
|
}
|
|
}
|
|
const { InputEventsLayer } = load('app/apps/developer/input-events.ts', {
|
|
'../../graphics/ui-fonts': { getDefaultSmallFont: () => font },
|
|
'../../graphics/image': { GrayImage: RecordingImage }, '../../graphics/textwrap': textwrap,
|
|
'../../ui/input-monitor': monitor, '../../ui/menu': { drawListScrollbar() {} },
|
|
});
|
|
let visible = true, renders = 0, removed = 0, pops = 0;
|
|
const layer = new InputEventsLayer(() => renders++, () => visible, () => removed++);
|
|
const ctx = { stack: { getBaseSize: () => ({ width: 576, height: 260 }),
|
|
pop: () => pops++, popThrough: (page) => { assert.equal(page, layer); page.onRemoved(); } } };
|
|
try {
|
|
for (const type of ['ring-press','click','ring-press','ring-press','double-click','long-press','long-press-release','short-then-long-press']) {
|
|
const e = { ...event(type, 1000 + renders * 75), ringInput: { tick: 4294967200 + renders, type: 1, aux:0, speed:0 } };
|
|
monitor.notifyInputListeners(e, renders > 0); layer.handleInput(e);
|
|
}
|
|
assert.equal(renders, 8); assert.equal(layer.log.count, 8); assert.equal(pops, 0);
|
|
visible = false; monitor.notifyInputListeners(event('click')); assert.equal(renders, 8); visible = true;
|
|
const image = layer.paint(ctx);
|
|
assert.ok(image.texts.some(t => t.text.startsWith("R4294967")));
|
|
assert.ok(image.texts.some(t => t.text.startsWith("*")));
|
|
layer.menuItems()[2].onSelect(ctx);
|
|
assert.ok(layer.paint(ctx).texts.some(t => t.text.startsWith("Phone receive time")));
|
|
for (const text of image.texts) {
|
|
assert.ok(text.x >= 0 && text.x + text.width <= image.width, text.text);
|
|
assert.ok(text.y >= 0 && text.y + text.height <= image.height, text.text);
|
|
}
|
|
if (process.env.INPUT_EVENTS_PREVIEW) fs.writeFileSync(process.env.INPUT_EVENTS_PREVIEW, Buffer.concat([
|
|
Buffer.from(`P5\n${image.width} ${image.height}\n255\n`), Buffer.from(image.withDrawsBaked().pixels),
|
|
]));
|
|
layer.menuItems()[0].onSelect(ctx); monitor.notifyInputListeners(event('click'));
|
|
assert.equal(layer.log.count, 8); assert.equal(layer.menuItems()[0].label, 'Resume capture');
|
|
layer.menuItems()[1].onSelect(ctx); assert.equal(layer.log.count, 0);
|
|
layer.menuItems()[0].onSelect(ctx); monitor.notifyInputListeners(event('ring-press'));
|
|
assert.equal(layer.log.count, 1);
|
|
layer.menuItems()[3].onSelect(ctx); monitor.notifyInputListeners(event('click'));
|
|
assert.equal(removed, 1); assert.equal(layer.log.count, 1);
|
|
} finally { layer.onRemoved(); }
|
|
});
|
|
|
|
const ring = (type, tick, received = 1000) => ({ ...event('click', received),
|
|
ringInput: { type, tick, aux: 17, speed: 29 } });
|
|
|
|
test('filter matches stock timing boundaries, exemptions, wrap, reset and zero sentinel', () => {
|
|
const f = new monitor.RingInputFilter();
|
|
for (const [type, tick, accepted] of [
|
|
[1,1000,true], [1,1099,false], [10,1099,true], [1,1100,true],
|
|
[8,1101,true], [2,1200,false], [2,1201,true], [2,1201,false],
|
|
[10,1201,true], [10,1202,true], [9,1301,true],
|
|
]) assert.equal(f.accept(ring(type, tick)), accepted, `${type}@${tick}`);
|
|
f.reset(); assert.equal(f.accept(ring(1,1)), true);
|
|
f.reset(); assert.equal(f.accept(ring(1,0xffffffd0)),true);
|
|
assert.equal(f.accept(ring(1,0x33)),false); assert.equal(f.accept(ring(1,0x34)),true);
|
|
// Backward timestamps / ring restart pass using unsigned subtraction.
|
|
assert.equal(f.accept(ring(1,5)),true);
|
|
f.reset(); assert.equal(f.accept(ring(1,0)),true); assert.equal(f.accept(ring(1,1)),true);
|
|
assert.equal(f.accept(event('click')),true);
|
|
assert.equal(f.accept({ ...event('click'), source: 'watch' }),true);
|
|
// Host scheduling does not affect decisions; filtered events do not extend the window.
|
|
f.reset(); assert.equal(f.accept(ring(4,1000,100000)),true);
|
|
assert.equal(f.accept(ring(4,1050,999999)),false);
|
|
assert.equal(f.accept(ring(4,1100,999999)),true);
|
|
f.reset(); assert.equal(f.accept(ring(99,2000)),true);
|
|
assert.equal(f.accept(ring(1,2050)),false);
|
|
});
|
|
|
|
test('raw debug observer sees suppressed reports once before app routing', async () => {
|
|
monitor.resetRingInputFilter();
|
|
const { exports: { Shell } } = shellDeclarations(), s = new Shell(), seen = [], routed = [];
|
|
s.routeInput = async (e) => { routed.push(e); return { shell:false,window:true }; };
|
|
s.syncInputFocus = () => {};
|
|
const off = monitor.addInputListener((e, filtered) => seen.push([e, filtered]));
|
|
try {
|
|
const first = ring(1,1000), second = ring(1,1050);
|
|
assert.equal(monitor.acceptInput(first),true); await s.receiveInput(first);
|
|
assert.equal(monitor.acceptInput(second),false); await s.receiveInput(second);
|
|
assert.equal(seen.length,2); assert.equal(routed.length,1);
|
|
assert.deepEqual(seen.map((e) => e[1]),[false,true]);
|
|
assert.equal(seen[1][0].ringInput.tick,1050);
|
|
assert.equal(Object.isFrozen(seen[1][0].ringInput),true);
|
|
} finally { off(); monitor.resetRingInputFilter(); }
|
|
});
|
|
|
|
test('raw-to-app conversion retains the unsigned ring clock and explicit scroll source', () => {
|
|
const d = shellDeclarations();
|
|
for (const [wire,eventType,direction] of [[4,1,'scroll-up'],[5,2,'scroll-down']]) {
|
|
const raw = { kind:'sys-event', eventType, eventSource:2, ringInput: ring(wire,0xffffffff).ringInput };
|
|
const input = d.exports.rawInputEventToInputEvent(raw);
|
|
assert.equal(input.type,direction); assert.equal(input.source,'ring');
|
|
assert.equal(input.timestampMs,12345678); assert.equal(input.ringInput.tick,0xffffffff);
|
|
assert.notEqual(input.ringInput,raw.ringInput);
|
|
}
|
|
});
|
|
|
|
test('debug history retains both clocks and uses unsigned gaps across counter wrap', () => {
|
|
const log = new monitor.InputEventLog();
|
|
log.add(ring(1,0xfffffff0,5000)); log.add(ring(1,0x10,5001),true);
|
|
assert.equal(log.entries[0].gapMs,1); assert.equal(log.entries[0].ringGapTicks,32);
|
|
assert.equal(log.entries[0].filtered,true);
|
|
log.clear(); log.add(ring(1,500)); assert.equal(log.entries[0].ringGapTicks,null);
|
|
});
|
|
|
|
test('PressTracker pairs each gesture with the touch-down that began it', () => {
|
|
const { PressTracker } = load('app/ui/gestures.ts');
|
|
const t = new PressTracker();
|
|
const ev = (type, source) => ({ type, source, timestampMs: 0 });
|
|
assert.equal(t.followsPress(ev('ring-press', 'watch')), false);
|
|
assert.equal(t.followsPress(ev('click', 'watch')), true);
|
|
// Input with no touch-down of its own (finger-pinch tap, crown, older watch app) still acts.
|
|
assert.equal(t.followsPress(ev('click', 'watch')), false);
|
|
// A press pairs only with the next event, and only from its own source.
|
|
t.followsPress(ev('ring-press', 'watch'));
|
|
assert.equal(t.followsPress(ev('click', 'left-arm')), false);
|
|
assert.equal(t.followsPress(ev('click', 'watch')), false);
|
|
// Stock ring scroll notifications omit their source.
|
|
t.followsPress(ev('ring-press', 'ring'));
|
|
assert.equal(t.followsPress({ type: 'scroll-up', timestampMs: 0 }), true);
|
|
});
|