[verified] merge upstream 25cb255b into all-features

This commit is contained in:
MasterControl committed 2026-11-19 22:21:26 +00:00
commit ffcdae660d
169 files changed
+16201 -597

No files matched your search

+23 -1
View File
@@ -2,7 +2,7 @@ const test = require('node:test');
const assert = require('node:assert/strict');
const { loader } = require('./helpers/load-typescript.cjs');
function settingsEnv(initialUrl = '', initialModel = 'openai-compatible') {
function settingsEnv(initialUrl = '', initialModel = 'openai-compatible', glassesType = 'g2') {
let url = initialUrl;
let model = initialModel;
let webSearch = false;
@@ -31,6 +31,7 @@ function settingsEnv(initialUrl = '', initialModel = 'openai-compatible') {
assistantHermesUrlSetting: { ...dummySetting, id: 'assistant-hermes-url', label: 'Hermes URL' },
assistantHermesKeySetting: { ...dummySetting, id: 'assistant-hermes-key', label: 'Hermes key' },
veniceWebSearchSetting: searchSetting,
glassesTypeSetting: { ...dummySetting, id: 'glasses-type', get: () => (typeof glassesType === 'function' ? glassesType() : glassesType) },
enumSettingMenuItem: row,
textSettingMenuItem: row,
toggleSettingMenuItem: row,
@@ -76,6 +77,8 @@ function settingsEnv(initialUrl = '', initialModel = 'openai-compatible') {
},
'../font-picker': { uiFontPickerMenuItem: () => ({ label: 'Font' }) },
'../notification-filter': { NotificationFilterLayer: class {} },
'../keyboard-settings': { keyboardSettingsMenuItem: () => ({ label: 'Bluetooth keyboard' }) },
'../keybinding-settings': { keybindingsMenuItem: () => ({ label: 'Keybindings' }) },
'../openai-compatible-model-picker': {
compatibleModelDiscoveryMenuItem: () => ({ label: 'Discover models' }),
},
@@ -86,6 +89,7 @@ function settingsEnv(initialUrl = '', initialModel = 'openai-compatible') {
assert.ok(section);
return {
section,
sections: panel.sections,
labels: () => section.items.map(item => item.label),
setUrl: value => { url = value; },
setModel: value => { model = value; },
@@ -151,3 +155,21 @@ test('OpenAI-compatible rows appear only while OpenAI-compatible is the selected
const labels = env.labels();
assert.equal(labels.indexOf('Compatible API URL'), labels.indexOf('Assistant model') + 1, 'compatible rows follow the model row');
});
test('the Keyboard section, with its pairing row, follows the glasses type while Settings stays open', () => {
let glassesType = 'g1';
const sections = settingsEnv('', 'openai-compatible', () => glassesType).sections;
const labels = sections.map(section => section.label);
// Always in the list, between the other input devices and the developer
// options; whether it shows is asked again on every paint.
assert.equal(labels.indexOf('Keyboard'), labels.indexOf('Watch') + 1);
const keyboard = sections.find(section => section.label === 'Keyboard');
assert.equal(keyboard.items.map(item => item.label).join(), 'Bluetooth keyboard,Keybindings');
assert.equal(keyboard.hidden(), true);
glassesType = 'rayneo-io';
assert.equal(keyboard.hidden(), true);
glassesType = 'g2';
assert.equal(keyboard.hidden(), false);
// Nothing else is conditional this way.
assert.equal(sections.filter(section => section.hidden).length, 1);
});
+47
View File
@@ -0,0 +1,47 @@
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');
// Local-time day arithmetic: pin the zone so the expectations hold on any machine.
process.env.TZ = 'Europe/Brussels';
function load(file) {
const sandbox = { exports: {}, require: id => { throw new Error(`Unstubbed import: ${id}`); } };
vm.runInNewContext(ts.transpileModule(fs.readFileSync(path.join(__dirname, '..', file), 'utf8'), {
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 },
}).outputText, sandbox);
return sandbox.exports;
}
const { listingDayMs, spansDays, spanLabel } = load('app/apps/calendar/event-days.ts');
const local = (y, m, d, h = 0, min = 0) => new Date(y, m - 1, d, h, min).getTime();
// Mon Nov 16 – Wed Nov 18, all day, as the provider stores it (end = midnight after the last day).
const threeDays = { startMs: local(2026, 11, 16), endMs: local(2026, 11, 19), allDay: true };
test('an all-day event still going is listed under today, not its first day', () => {
assert.equal(listingDayMs(threeDays, local(2026, 11, 18, 0, 24)), local(2026, 11, 18));
assert.equal(listingDayMs(threeDays, local(2026, 11, 17, 9)), local(2026, 11, 17));
});
test('events that have not started are listed under their start day', () => {
assert.equal(listingDayMs(threeDays, local(2026, 11, 14, 12)), local(2026, 11, 16));
const meeting = { startMs: local(2026, 11, 19, 9), endMs: local(2026, 11, 19, 12), allDay: false };
assert.equal(listingDayMs(meeting, local(2026, 11, 18, 0, 24)), local(2026, 11, 19));
});
test('a timed event running past midnight is listed under today the next morning', () => {
const lateShift = { startMs: local(2026, 11, 17, 22), endMs: local(2026, 11, 18, 2), allDay: false };
assert.equal(listingDayMs(lateShift, local(2026, 11, 18, 0, 30)), local(2026, 11, 18));
assert.equal(spansDays(lateShift), true);
});
test('only events covering more than one day get a span, and it reads naturally', () => {
assert.equal(spansDays(threeDays), true);
assert.equal(spanLabel(threeDays), 'Mon 16 – Wed 18 Nov');
assert.equal(spansDays({ startMs: local(2026, 11, 16), endMs: local(2026, 11, 17), allDay: true }), false);
assert.equal(spansDays({ startMs: local(2026, 11, 19, 9), endMs: local(2026, 11, 19, 12), allDay: false }), false);
assert.equal(spanLabel({ startMs: local(2026, 11, 30), endMs: local(2026, 12, 2), allDay: true }), 'Mon 30 Nov – Tue 1 Dec');
});
+45
View File
@@ -0,0 +1,45 @@
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');
// Local-time formatting: pin the zone so the expectations hold on any machine.
process.env.TZ = 'Europe/Brussels';
function load(file) {
const sandbox = { exports: {}, require: id => { throw new Error(`Unstubbed import: ${id}`); } };
vm.runInNewContext(ts.transpileModule(fs.readFileSync(path.join(__dirname, '..', file), 'utf8'), {
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 },
}).outputText, sandbox);
return sandbox.exports;
}
const { formatEvent, formatEventSpan } = load('app/assistant/date-format.ts');
const local = (y, m, d, h = 0, min = 0) => new Date(y, m - 1, d, h, min).getTime();
test('a multi-day all-day event names its last day (all-day ends are exclusive)', () => {
// Mon Nov 16 – Wed Nov 18, as the calendar provider stores it: end = midnight after the last day.
const span = formatEventSpan({ startMs: local(2026, 11, 16), endMs: local(2026, 11, 19), allDay: true });
assert.equal(span, 'Mon Nov 16 – Wed Nov 18 (all day)');
});
test('a one-day all-day event stays a single date', () => {
assert.equal(formatEventSpan({ startMs: local(2026, 11, 16), endMs: local(2026, 11, 17), allDay: true }), 'Mon Nov 16 (all day)');
});
test('a timed event shows its end time, and the end date when it crosses midnight', () => {
assert.equal(formatEventSpan({ startMs: local(2026, 11, 19, 9), endMs: local(2026, 11, 19, 12), allDay: false }), 'Thu Nov 19, 9:00 AM – 12:00 PM');
assert.equal(formatEventSpan({ startMs: local(2026, 11, 19, 22), endMs: local(2026, 11, 20, 2), allDay: false }), 'Thu Nov 19, 10:00 PM – Fri Nov 20, 2:00 AM');
});
test('a timed event without a usable end keeps the old start-only form', () => {
assert.equal(formatEventSpan({ startMs: local(2026, 11, 19, 9), endMs: local(2026, 11, 19, 9), allDay: false }), 'Thu Nov 19, 9:00 AM');
});
test('formatEvent keeps the title and location around the span', () => {
const line = formatEvent({ title: 'Office', startMs: local(2026, 11, 16), endMs: local(2026, 11, 19), allDay: true, location: 'HQ' });
assert.equal(line, '- Office — Mon Nov 16 – Wed Nov 18 (all day) @ HQ');
assert.equal(formatEvent({ title: '', startMs: local(2026, 11, 16), endMs: local(2026, 11, 17), allDay: true, location: '' }), '- (untitled) — Mon Nov 16 (all day)');
});
+24
View File
@@ -275,3 +275,27 @@ test("names that only look like a G1 are not admitted", () => {
assert.equal(classifyAdvertisement(g1Raw(name, "")), null, name);
}
});
test("an iOS arm rebuilt from a saved pairing joins its advertising other arm", () => {
const { g2ManufacturerData } = require("../.test-build/app/g2/even-advertisement.js");
const { identifyIosPeripheral } = require("../.test-build/app/g2/ios-peripheral-identity.js");
const { DiscoveryAggregator } = require("../.test-build/app/g2/pairing-candidates.js");
const mfg = g2ManufacturerData("S211GBBC180304", "E0:12:14:B6:EC:E1");
assert.equal(extractG2Serial(hexToBytes(mfg)), "S211GBBC180304");
assert.equal(extractG2Mac(hexToBytes(mfg)), "E0:12:14:B6:EC:E1");
const right = identifyIosPeripheral({ identifier: "14b99254-0798-d1a8-3eec-b141bdabfae8", name: "Even G2_32_R_B6ECE1",
manufacturerData: mfg, rssi: 0, connectable: true });
assert.equal(right.address, "E0:12:14:B6:EC:E1");
assert.equal(right.role, "right");
assert.equal(right.note, null);
const aggregator = new DiscoveryAggregator();
aggregator.ingest(raw({}));
aggregator.ingest({ ...right, rssi: null, source: "paired" });
const [pair] = aggregator.pairs();
assert.equal(pair.completeness, "complete");
assert.equal(pair.rssi, -55);
assert.equal(g2ManufacturerData("S211GBBC180304XX", "E0:12:14:B6:EC:E1"), null);
assert.equal(g2ManufacturerData("", "E0:12:14:B6:EC:E1"), null);
assert.equal(g2ManufacturerData("S211GBBC180304", "not a mac"), null);
});
+66 -4
View File
@@ -4,7 +4,9 @@ const assert = require("node:assert/strict");
const {
GLANCE_HIDDEN,
GLANCE_TIMEOUT_MS,
GLANCE_TILT_MAX_MS,
glanceEventForGesture,
glanceTiltMaxMs,
reduceGlance,
} = require("../.test-build/app/g2/glance-state.js");
@@ -12,7 +14,7 @@ const T0 = 1_000_000;
test("a press shows the board and arms the auto-hide timer", () => {
const shown = reduceGlance(GLANCE_HIDDEN, { type: "press" }, T0);
assert.deepEqual(shown, { visible: true, holding: false, hideAtMs: T0 + GLANCE_TIMEOUT_MS });
assert.deepEqual(shown, { visible: true, holding: false, tilted: false, tiltMaxMs: 0, hideAtMs: T0 + GLANCE_TIMEOUT_MS });
// A timer firing early is ignored; on time it hides.
assert.equal(reduceGlance(shown, { type: "timeout" }, T0 + GLANCE_TIMEOUT_MS - 1), shown);
assert.deepEqual(reduceGlance(shown, { type: "timeout" }, T0 + GLANCE_TIMEOUT_MS), GLANCE_HIDDEN);
@@ -27,7 +29,7 @@ test("a further press renews the timer from the new press", () => {
test("a hold shows the board without a timer and the release hides it", () => {
const held = reduceGlance(GLANCE_HIDDEN, { type: "hold" }, T0);
assert.deepEqual(held, { visible: true, holding: true, hideAtMs: null });
assert.deepEqual(held, { visible: true, holding: true, tilted: false, tiltMaxMs: 0, hideAtMs: null });
assert.equal(reduceGlance(held, { type: "timeout" }, T0 + 60_000), held);
// A tap mid-hold neither hides nor starts a timer.
assert.equal(reduceGlance(held, { type: "press" }, T0 + 500), held);
@@ -47,9 +49,11 @@ test("dismiss hides from any state", () => {
assert.deepEqual(reduceGlance(shown, { type: "dismiss" }, T0), GLANCE_HIDDEN);
});
test("gesture mapping: taps and head-tilt press, both long-press gestures hold, double-tap dismisses only a visible board", () => {
test("gesture mapping: taps press, head-tilt tilts, both long-press gestures hold, double-tap dismisses only a visible board", () => {
assert.deepEqual(glanceEventForGesture("click", false), { type: "press" });
assert.deepEqual(glanceEventForGesture("head-tilt", false), { type: "press" });
assert.deepEqual(glanceEventForGesture("head-tilt", false), { type: "tilt" });
assert.deepEqual(glanceEventForGesture("head-down", true), { type: "head-down" });
assert.equal(glanceEventForGesture("head-down", false), null);
assert.deepEqual(glanceEventForGesture("long-press", false), { type: "hold" });
assert.deepEqual(glanceEventForGesture("short-then-long-press", false), { type: "hold" });
assert.deepEqual(glanceEventForGesture("short-then-long-press", true), { type: "hold" });
@@ -65,3 +69,61 @@ test("the press timeout is the host's when supplied", () => {
assert.equal(reduceGlance(shown, { type: "timeout" }, T0 + GLANCE_TIMEOUT_MS), shown);
assert.deepEqual(reduceGlance(shown, { type: "timeout" }, T0 + 10_000), GLANCE_HIDDEN);
});
test("a head-tilt shows the board until the head comes back down", () => {
const up = reduceGlance(GLANCE_HIDDEN, { type: "tilt" }, T0);
assert.deepEqual(up, { visible: true, holding: false, tilted: true, tiltMaxMs: GLANCE_TILT_MAX_MS, hideAtMs: T0 + GLANCE_TILT_MAX_MS });
// The tap duration no longer applies, and taps change nothing.
assert.equal(reduceGlance(up, { type: "timeout" }, T0 + GLANCE_TIMEOUT_MS), up);
assert.equal(reduceGlance(up, { type: "press" }, T0 + 500), up);
assert.deepEqual(reduceGlance(up, { type: "head-down" }, T0 + 9000), GLANCE_HIDDEN);
// The safety cap still hides it if the head-down never arrives.
assert.deepEqual(reduceGlance(up, { type: "timeout" }, T0 + GLANCE_TILT_MAX_MS), GLANCE_HIDDEN);
});
test("head-down leaves a tap's or a hold's showing alone", () => {
const shown = reduceGlance(GLANCE_HIDDEN, { type: "press" }, T0);
assert.equal(reduceGlance(shown, { type: "head-down" }, T0 + 100), shown);
const held = reduceGlance(GLANCE_HIDDEN, { type: "hold" }, T0);
assert.equal(reduceGlance(held, { type: "head-down" }, T0 + 100), held);
});
test("a hold and a head-tilt together: the board stays until both end", () => {
const both = reduceGlance(reduceGlance(GLANCE_HIDDEN, { type: "tilt" }, T0), { type: "hold" }, T0 + 100);
assert.deepEqual(both, { visible: true, holding: true, tilted: true, tiltMaxMs: GLANCE_TILT_MAX_MS, hideAtMs: null });
const released = reduceGlance(both, { type: "release" }, T0 + 200);
assert.deepEqual(released, { visible: true, holding: false, tilted: true, tiltMaxMs: GLANCE_TILT_MAX_MS, hideAtMs: T0 + 200 + GLANCE_TILT_MAX_MS });
const downWhileHeld = reduceGlance(both, { type: "head-down" }, T0 + 200);
assert.deepEqual(downWhileHeld, { visible: true, holding: true, tilted: false, tiltMaxMs: 0, hideAtMs: null });
assert.deepEqual(reduceGlance(downWhileHeld, { type: "release" }, T0 + 300), GLANCE_HIDDEN);
});
test("a tilt with a shorter cap keeps it, also across a hold", () => {
const up = reduceGlance(GLANCE_HIDDEN, { type: "tilt", maxMs: 5000 }, T0);
assert.equal(up.hideAtMs, T0 + 5000);
assert.deepEqual(reduceGlance(up, { type: "timeout" }, T0 + 5000), GLANCE_HIDDEN);
const held = reduceGlance(up, { type: "hold" }, T0 + 100);
assert.equal(reduceGlance(held, { type: "release" }, T0 + 1000).hideAtMs, T0 + 1000 + 5000);
});
test("the tilt cap is the Head tilt timeout, or the tap duration if shorter where the head-down is unlikely", () => {
const tapTimeoutMs = 5000;
const cap = (capMs, resumedSession, triggerDegrees) => glanceTiltMaxMs({ capMs, resumedSession, triggerDegrees, tapTimeoutMs });
assert.equal(cap(60_000, false, 30), 60_000);
assert.equal(cap(15_000, false, null), 15_000);
assert.equal(cap(null, false, 30), null);
assert.equal(cap(60_000, true, 30), tapTimeoutMs);
assert.equal(cap(60_000, false, 20), tapTimeoutMs);
assert.equal(cap(null, false, 20), tapTimeoutMs);
assert.equal(cap(3000, true, 30), 3000);
assert.equal(cap(60_000, false, 25), 60_000);
});
test("a tilt with no timeout stays until the head-down, also across a hold", () => {
const up = reduceGlance(GLANCE_HIDDEN, { type: "tilt", maxMs: null }, T0);
assert.equal(up.hideAtMs, null);
assert.equal(reduceGlance(up, { type: "timeout" }, T0 + 3_600_000), up);
const held = reduceGlance(up, { type: "hold" }, T0 + 100);
assert.equal(reduceGlance(held, { type: "release" }, T0 + 1000).hideAtMs, null);
assert.deepEqual(reduceGlance(up, { type: "head-down" }, T0 + 9000), GLANCE_HIDDEN);
});
+30
View File
@@ -0,0 +1,30 @@
const test = require("node:test");
const assert = require("node:assert/strict");
const { imuPitchDegrees } = require("../.test-build/app/native/imu-types.js");
const rad = (deg) => (deg * Math.PI) / 180;
/**
* A reading at rest in the canonical frame (+x forward, +y left, +z up), in
* m/s^2 to show the scale does not matter: the head pitched up by pitchDeg,
* then rolled by rollDeg.
*/
function reading(pitchDeg, rollDeg = 0) {
const p = rad(pitchDeg);
const r = rad(rollDeg);
return { x: 9.8 * Math.sin(p), y: 9.8 * Math.cos(p) * Math.sin(r), z: 9.8 * Math.cos(p) * Math.cos(r) };
}
test("imuPitchDegrees reads the head's pitch from one reading, past 45° and with the head rolled", () => {
for (const pitch of [-30, 0, 5, 20, 45, 50, 60, 75]) {
for (const roll of [0, 30, -40]) {
assert.ok(Math.abs(imuPitchDegrees(reading(pitch, roll)) - pitch) < 1e-6, `pitch ${pitch} roll ${roll}`);
}
}
// The poses measured on a G2 (in g).
const s = Math.SQRT1_2;
assert.ok(Math.abs(imuPitchDegrees({ x: 0, y: 0, z: 1 })) < 1e-9);
assert.ok(Math.abs(imuPitchDegrees({ x: s, y: 0, z: s }) - 45) < 1e-9);
assert.ok(Math.abs(imuPitchDegrees({ x: 0, y: s, z: s })) < 1e-9);
});
+2 -1
View File
@@ -163,7 +163,7 @@ function screenFixture() {
'util/numeric-util': { clamp: (n, min, max) => Math.max(min, Math.min(max, n)) },
};
const modules = depth => Object.fromEntries(Object.entries(common).map(([key, value]) => [depth + key, value]));
const calendar = load('app/apps/calendar/calendar.ts', modules('../../'));
const calendar = load('app/apps/calendar/calendar.ts', { ...modules('../../'), './event-days': load('app/apps/calendar/event-days.ts') });
const widget = load('app/apps/glanceboard/widgets/calendar-widget.ts', {
...modules('../../../'), '../../calendar/calendar': calendar,
'../glance-font': { glanceFont: { small: () => font, medium: () => font } },
@@ -244,6 +244,7 @@ test('iOS assistant calendar tool awaits events, reports denied access and failu
'../native/media-controller': { mediaControllerBridge: { start: async () => {} } },
'../native/spotify': { spotifyService: () => null },
'../native/notification-icons': {}, '../ui/shell/shell': {}, './tool-registry': {},
'./date-format': load('app/assistant/date-format.ts'),
}, { global: { isIOS: true } });
registerSystemTools({ registerSystemTool: (definition, handler) => registered.set(definition.name, handler) });
assert.deepEqual([...registered.keys()], ['glasses.get_state', 'glasses.show_alert', 'calendar.list_events',
+1 -1
View File
@@ -33,7 +33,7 @@ function harness(refused = new Set()) {
FaceclawKitIosGlassesSession: { alloc: () => ({ initWithRightAddressLeftAddressRingAddress: () => session }) },
FaceclawKitFaceclawBleCommunicatorListener: {}, FaceclawKitIosAncsListener: {}, FaceclawKitIosAudioPacketListener: {},
FaceclawKitFaceclawCompassListener: {}, FaceclawKitFaceclawImuListener: {}, FaceclawKitIosAmbientLightListener: {},
FaceclawKitIosMicStatusListener: {}, console: { log() {}, warn() {} },
FaceclawKitIosMicStatusListener: {}, FaceclawKitIosKeyboardEventListener: {}, console: { log() {}, warn() {} },
}, {
'@nativescript/core': { File: {}, knownFolders: {}, path: {} },
'../g2/firmware-compat': { REQUIRED_FACECLAW_FIRMWARE_VERSION: 37 },
+55 -1
View File
@@ -30,6 +30,9 @@ function fixture() {
let now = 1000, nextTask = 0, screenOn = true, shellOptions;
const battery = {};
const tasks = new Map(), sent = [], previews = [], received = [], errors = [];
// The keyboard bridge's service, as the controller sees it: what it was told, and the host it was given.
const keyboard = { host: null, calls: [], setHost(host) { this.host = host; }, linkReady() { this.calls.push('ready'); },
linkLost(release) { this.calls.push(release ? 'lost+release' : 'lost'); }, retryNow() { this.calls.push('retry'); }, handleUnhandledKey() {} };
const boardStats = { starts: 0, stops: 0, paints: 0 };
const registered = [], inProcessApps = [];
const settings = { lock: true, enabled: true, tap: true, hold: true, tilt: true, duration: 3000, depth: 0 };
@@ -165,6 +168,7 @@ function fixture() {
'./device-addresses': { loadDeviceAddresses: () => ({ right: 'AA', left: 'BB', ring: '' }) }, './ios-peripheral-identity': { deviceAddressError: () => null },
'./wear-remote': { WearRemote: class { schedulePublish() {} } },
'./firmware-compat': load('app/g2/firmware-compat.ts', {}), './reconnect-policy': load('app/g2/reconnect-policy.ts', {}),
'./keyboard/service': { keyboardService: keyboard },
'../apps/launcher': { launcherEntries: () => [] },
'../apps/evenhub/installed-apps': {}, '../apps/evenhub/manager': { onPhoneShownChanged: () => () => {}, phoneUiButton: () => null },
'../apps/evenhub/updates': {}, '../apps/evenhub': {},
@@ -223,7 +227,7 @@ function fixture() {
}
async function phone(type, origin = 'ring') { controller.gesture(type, origin); await render(); }
async function connect() { await controller.connect(); await render(); }
return { relay, controller, shell, battery, remoteHost, keyboardChanged: session => shellOptions.onKeyboardInputChanged(session), settings, phoneState, observers,
return { relay, controller, shell, battery, remoteHost, keyboard, keyboardChanged: session => shellOptions.onKeyboardInputChanged(session), settings, phoneState, observers,
get shellOptions() { return shellOptions; }, registered, inProcessApps,
get bridge() { return bridge; }, connect, wear: wearing => bridge.emitWear(wearing), settingsChanged: () => settingsChanged(), sent, previews, received, boardStats, hardware, phone, advance, render };
}
@@ -348,3 +352,53 @@ test('the switcher can open the Notifications window in the background, once, wi
assert.equal(f.shellOptions.openNotificationsWindow().opened, true);
assert.equal(f.inProcessApps.length, 2);
});
test('a paired keyboard: the bridge follows the G2 link, and its keys\' gestures go in with physical input', async () => {
const f = fixture();
await f.connect();
// Connected is not enough: the firmware has to be one with the bridge.
f.bridge.emitState('connected');
assert.ok(!f.keyboard.calls.includes('ready'));
f.bridge.listeners.firmware({ leftVersion: '2.3.0.24', rightVersion: '2.3.0.24', extension: 'Faceclaw/39' });
assert.ok(!f.keyboard.calls.includes('ready'));
f.bridge.listeners.firmware({ leftVersion: '2.3.0.24', rightVersion: '2.3.0.24', extension: 'Faceclaw/40' });
assert.equal(f.keyboard.calls.at(-1), 'ready');
assert.equal(f.keyboard.host.ready(), true);
// The link dropping on its own ends it without a word to the firmware; coming back restores it.
f.bridge.emitState('retrying', 'Reconnecting');
assert.equal(f.keyboard.calls.at(-1), 'lost'); assert.equal(f.keyboard.host.ready(), false);
f.bridge.emitState('connected');
assert.equal(f.keyboard.calls.at(-1), 'ready');
// A key's gesture is ordinary input from there on, tagged with the device it stands in for.
f.received.length = 0;
await f.keyboard.host.gesture('swipe-left', 'watch'); await f.keyboard.host.gesture('click', 'ring');
await f.keyboard.host.gesture('long-press', 'watch');
assert.deepEqual(f.received.map(input => `${input.type}/${input.source}`),
['swipe-left/watch', 'click/ring', 'long-press/watch', 'long-press-release/watch']);
// A dark display: the bridge stops looking for an absent keyboard, a key wakes it, and it looks again.
f.shell.sleep();
assert.equal(f.keyboard.host.shouldReconnect(), false);
f.keyboard.calls.length = 0; f.received.length = 0;
f.keyboard.host.wake(); await f.render();
assert.deepEqual(f.received.map(input => input.type), ['display-wake']);
assert.equal(f.shell.isScreenOn(), true); assert.ok(f.keyboard.calls.includes('retry'));
assert.equal(f.keyboard.host.shouldReconnect(), true);
// Disconnecting on purpose lets the keyboard go while the link is still up.
f.keyboard.calls.length = 0;
await f.controller.disconnect();
assert.equal(f.keyboard.calls[0], 'lost+release');
assert.equal(f.keyboard.host.ready(), false);
});
test('a keyboard needs a G2: other glasses never bring the bridge up', async () => {
glassesType = 'g1';
try {
const f = fixture();
await f.connect();
f.bridge.emitState('connected');
f.bridge.listeners.firmware({ leftVersion: 'g1v0 0.5.66', rightVersion: 'g1v0 0.5.66', extension: 'Faceclaw/40' });
assert.ok(!f.keyboard.calls.includes('ready'), f.keyboard.calls.join());
} finally { glassesType = 'g2'; }
});
+1 -1
View File
@@ -37,7 +37,7 @@ function harness() {
FaceclawKitIosGlassesSession: { alloc: () => ({ initWithRightAddressLeftAddressRingAddress: () => session }) },
FaceclawKitFaceclawBleCommunicatorListener: {}, FaceclawKitIosAncsListener: {}, FaceclawKitIosAudioPacketListener: {},
FaceclawKitFaceclawCompassListener: {}, FaceclawKitFaceclawImuListener: {}, FaceclawKitIosAmbientLightListener: {},
FaceclawKitIosMicStatusListener: {}, console: { log() {}, warn() {} },
FaceclawKitIosMicStatusListener: {}, FaceclawKitIosKeyboardEventListener: {}, console: { log() {}, warn() {} },
}, {
'@nativescript/core': { File: {}, knownFolders: {}, path: {} },
'../g2/firmware-compat': { REQUIRED_FACECLAW_FIRMWARE_VERSION: 37 },
+1
View File
@@ -511,6 +511,7 @@ test('the assistant offers Spotify search-and-play once signed in and refreshes
snapshot: () => ({ accessEnabled: true, playbackState: 'playing', title: 'Song', artist: 'Band', appName: 'Spotify' }) } },
'../native/spotify': { spotifyService: () => spotify },
'../native/notification-icons': {}, '../ui/shell/shell': {}, './tool-registry': {},
'./date-format': load('app/assistant/date-format.ts'),
}, { global: { isIOS: true } });
registerSystemTools({
registerSystemTool: (definition, handler) => systemTools.set(definition.name, handler),
+2 -2
View File
@@ -36,7 +36,7 @@ function harness() {
static open(name) {
recordingAttempts++;
if (failRecordings) return null;
const r = new FakeRecorder(); r.name = name; r.path = `/Downloads/${name}.wav`; r.pcm = []; r.closed = false; recordings.push(r); return r;
const r = new FakeRecorder(); r.name = name; r.path = `/Downloads/${name}.mka`; r.pcm = []; r.closed = false; recordings.push(r); return r;
}
append(bytes) { this.pcm.push(...bytes); }
close() { this.closed = true; }
@@ -49,7 +49,7 @@ function harness() {
setTimeout: fn => { timers.set(fn, fn); return fn; }, clearTimeout: fn => timers.delete(fn),
NSData: { dataWithBytesLength: (b, n) => Buffer.from(b, 0, n) }, interop: { handleof: b => b, bufferFromData: d => d },
require: name => name === './cloud-stt-provider' ? provider : name === './speech-analyzer-stt.ios' ? analyzer
: name === './wav-recorder.ios' ? { WavRecorder: FakeRecorder } : {} };
: name === './mic-recorder.ios' ? { MicRecorder: FakeRecorder } : {} };
vm.runInNewContext(ts.transpileModule(fs.readFileSync('app/native/voice-control.ios.ts', 'utf8'), {
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 } }).outputText, sandbox);
const bridge = new sandbox.exports.IosVoiceControlBridge();
+160
View File
@@ -0,0 +1,160 @@
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');
function load(file, modules) {
const source = fs.readFileSync(path.join(__dirname, '..', file), 'utf8');
const context = { exports: {}, console, require(name) { assert.ok(name in modules, name); return modules[name]; } };
vm.runInNewContext(ts.transpileModule(source, {
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 },
}).outputText, context);
return context.exports;
}
const graphics = require('../.test-build/app/graphics/image.js');
const metrics = require('../.test-build/app/ui/metrics.js');
const menuCore = require('../.test-build/app/ui/menu-core.js');
const bindings = require('../.test-build/app/g2/keyboard/bindings.js');
const { BdfFont } = load('app/graphics/bdffont.ts', { '@nativescript/core': {} });
const font = BdfFont.parse(fs.readFileSync(path.join(__dirname, '../app/fonts/terminus/ter-u12n.bdf'), 'utf8'));
const textwrap = load('app/graphics/textwrap.ts', {});
const fonts = { getDefaultSmallFont: () => font };
const menu = load('app/ui/menu.ts', {
'./menu-animation-pref': require('../.test-build/app/ui/menu-animation-pref.js'), './animation-speed': require('../.test-build/app/ui/animation-speed.js'), '../native/settings-store': { getStringSetting: (_key, fallback) => fallback }, '../graphics/screen-size': { screenWidth: () => 640, screenHeight: () => 480 },
'../graphics/image': graphics, '../graphics/textwrap': textwrap, '../graphics/ui-fonts': fonts,
'../util/numeric-util': { clamp: (n, lo, hi) => Math.max(lo, Math.min(hi, n)) }, './gestures': {},
'./metrics': metrics, './menu-core': menuCore,
});
/** The settings menu over a fake keyboard service and layer stack. */
function fixture({ connected = true, stored = [], room = true } = {}) {
const service = {
list: [...stored], capture: null, cancelled: 0,
bridge: { isConnected: () => connected },
keyBindings() { return this.list; },
setKeyBinding(chord, action) {
if (!room) return false;
const id = bindings.chordId(chord);
const index = this.list.findIndex(b => bindings.chordId(b.chord) === id);
const next = [...this.list];
if (index < 0) next.push({ chord, action }); else next[index] = { chord, action };
this.list = next;
return true;
},
removeKeyBinding(chord) { this.list = this.list.filter(b => bindings.chordId(b.chord) !== bindings.chordId(chord)); },
captureNextKey(onChord) { this.capture = onChord; return () => { if (this.capture === onChord) { this.capture = null; this.cancelled++; } }; },
};
class TextViewerLayer { constructor(text, title) { this.text = text; this.title = title; } handleInput(event, ctx) { if (event.type === 'double-click') ctx.stack.pop(); } }
const { keybindingsMenuItem } = load('app/ui/keybinding-settings.ts', {
'../graphics/ui-fonts': fonts, '../g2/keyboard/bindings': bindings, '../g2/keyboard/service': { keyboardService: service },
'../apps/files/text-viewer': { TextViewerLayer }, './menu': menu,
});
const layers = [{ base: true }];
let renders = 0;
const removed = layer => layer.onRemoved?.();
const ctx = {
actions: { requestRender: () => { renders++; } },
stack: {
getBaseSize: () => ({ width: 576, height: 260, padding: { top: 0, right: 0, bottom: 0, left: 0 } }), isFocused: () => true,
push: layer => { layers.push(layer); },
pop: () => { if (layers.length > 1) removed(layers.pop()); },
popThrough: layer => {
const index = layers.indexOf(layer);
if (index <= 0) return false;
for (const gone of layers.splice(index).reverse()) removed(gone);
return true;
},
},
};
const top = () => layers[layers.length - 1];
const input = type => top().handleInput({ type, timestampMs: 0 }, ctx);
const labels = () => top().items.map(item => item.label);
/** Move the highlight of the menu on top to the row with this label, and select it. */
async function choose(label) {
const layer = top();
for (let i = 0; i <= layer.items.length && layer.selectedItem?.label !== label; i++) await input('scroll-down');
assert.equal(layer.selectedItem?.label, label, `${label} in ${labels().join(' | ')}`);
await input('click');
}
const item = keybindingsMenuItem();
return { service, item, ctx, layers, top, input, labels, choose, renders: () => renders,
open: () => item.onSelect(ctx), chord: (code, mods = {}) => ({ code, shift: false, ctrl: false, alt: false, meta: false, ...mods }) };
}
test('adding a keybinding: the key is read from the keyboard, then an action is picked', async () => {
const f = fixture();
f.open();
assert.deepEqual(f.labels(), ['Add keybinding', 'Default keys']);
await f.choose('Add keybinding');
assert.ok(f.service.capture, 'waiting for a key');
assert.match(f.top().body ?? f.top().constructor.name, /Press the key|KeyCaptureLayer/);
f.service.capture(f.chord('AudioVolumeUp'));
// The waiting page is gone and the picker for that key is up, with every action in it.
assert.equal(f.layers.length, 3); assert.equal(f.top().title, 'Volume Up');
assert.deepEqual(f.labels(), ['Select', 'Back', 'Swipe up', 'Swipe down', 'Swipe left', 'Swipe right',
'Scroll up', 'Scroll down', 'System menu', 'App menu', 'Nothing']);
await f.choose('Scroll up');
assert.deepEqual(f.service.list, [{ chord: f.chord('AudioVolumeUp'), action: 'scroll-up' }]);
// Back on the list, on the new row.
assert.equal(f.layers.length, 2);
assert.deepEqual(f.labels(), ['Volume Up', 'Add keybinding', 'Default keys']);
assert.equal(f.top().selectedItem.label, 'Volume Up');
assert.ok(f.renders() > 0);
});
test('a keybinding can be changed or removed, and a chord is one keybinding however often it is added', async () => {
const f = fixture();
f.service.list = [{ chord: f.chord('Escape', { shift: true }), action: 'none' }, { chord: f.chord('KeyQ', { ctrl: true }), action: 'long-press' }];
f.open();
assert.deepEqual(f.labels(), ['Shift+Esc', 'Ctrl+Q', 'Add keybinding', 'Default keys']);
await f.choose('Ctrl+Q');
// The picker opens on the current action, marks it, and offers removal.
assert.equal(f.top().selectedItem.label, 'System menu *'); assert.equal(f.labels().at(-1), 'Remove keybinding');
await f.choose('App menu');
assert.equal(f.service.list[1].action, 'short-then-long-press'); assert.equal(f.service.list.length, 2);
// Adding the same chord again edits it rather than making a second one.
await f.choose('Add keybinding');
f.service.capture(f.chord('KeyQ', { ctrl: true }));
assert.equal(f.top().selectedItem.label, 'App menu *');
await f.choose('Back');
assert.deepEqual(f.service.list.map(b => b.action), ['none', 'double-click']);
await f.choose('Shift+Esc');
await f.choose('Remove keybinding');
assert.deepEqual(f.labels(), ['Ctrl+Q', 'Add keybinding', 'Default keys']);
});
test('backing out of the wait calls the capture off; with no keyboard there is nothing to wait for', async () => {
const f = fixture();
f.open();
await f.choose('Add keybinding');
await f.input('double-click');
assert.equal(f.service.capture, null); assert.equal(f.service.cancelled, 1); assert.equal(f.layers.length, 2);
// A key pressed after that is not taken for a keybinding.
assert.deepEqual(f.service.list, []);
const away = fixture({ connected: false });
away.open();
await away.choose('Add keybinding');
assert.equal(away.service.capture, null);
assert.match(away.top().body, /No keyboard is connected/);
await away.input('double-click');
assert.equal(away.layers.length, 2);
});
test('the defaults are a page of their own, and a full list says so', async () => {
const f = fixture();
f.open();
await f.choose('Default keys');
assert.equal(f.top().title, 'Default keys');
for (const phrase of ['Arrow keys', 'Enter', 'Shift+Esc', 'terminal']) assert.ok(f.top().text.includes(phrase), phrase);
const full = fixture({ room: false });
full.open();
await full.choose('Add keybinding');
full.service.capture(full.chord('F5'));
await full.choose('Select');
assert.match(full.top().body, /no room/); assert.deepEqual(full.service.list, []);
});
+302
View File
@@ -0,0 +1,302 @@
const test = require('node:test')
const assert = require('node:assert/strict')
const {
KEY_ACTIONS, MAX_KEY_BINDINGS, GestureScheme, chordId, chordOf, defaultKeyAction, describeChord, gestureRepeats,
isModifierCode, keyActionLabel, keyBindingMap, parseChordId, parseKeyBindings, resolveKey, serializeKeyBindings,
} = require('../.test-build/app/g2/keyboard/bindings.js')
const { KeyRouter } = require('../.test-build/app/g2/keyboard/key-router.js')
const key = (k, overrides = {}) => ({
phase: 'down', key: k, code: overrides.code ?? k, repeat: false, timestampMs: 0,
...overrides, modifiers: { shift: false, ctrl: false, alt: false, meta: false, ...overrides.modifiers },
})
const chord = (code, mods = {}) => ({ code, shift: false, ctrl: false, alt: false, meta: false, ...mods })
const none = new Map()
test('built-in keys: arrows are the watch\'s swipes, Enter selects, Esc backs out', () => {
const action = (k, overrides) => defaultKeyAction(key(k, overrides))
assert.deepEqual(action('ArrowLeft'), { action: 'swipe-left', overridesTyping: false })
assert.deepEqual(action('ArrowRight'), { action: 'swipe-right', overridesTyping: false })
assert.deepEqual(action('ArrowUp'), { action: 'swipe-up', overridesTyping: false })
assert.deepEqual(action('ArrowDown'), { action: 'swipe-down', overridesTyping: false })
assert.deepEqual(action('Enter'), { action: 'click', overridesTyping: false })
assert.deepEqual(action('Enter', { code: 'NumpadEnter' }), { action: 'click', overridesTyping: false })
assert.deepEqual(action('Escape'), { action: 'double-click', overridesTyping: false })
// With a modifier, Escape means "back" even where keys are being typed.
for (const modifier of ['shift', 'ctrl', 'alt']) {
assert.deepEqual(action('Escape', { modifiers: { [modifier]: true } }), { action: 'double-click', overridesTyping: true })
}
assert.equal(action('Escape', { modifiers: { meta: true } }), null)
assert.equal(action('ArrowLeft', { modifiers: { ctrl: true } }), null)
assert.equal(action('Enter', { modifiers: { alt: true } }), null)
for (const k of ['a', ' ', 'Backspace', 'Tab', 'F1', 'AudioVolumeUp']) assert.equal(action(k), null)
})
test('chords: canonical ids round-trip, and read naturally', () => {
const c = chord('Escape', { shift: true, ctrl: true })
assert.equal(chordId(c), 'Ctrl+Shift+Escape'); assert.deepEqual(parseChordId('Ctrl+Shift+Escape'), c)
assert.deepEqual(parseChordId('ArrowLeft'), chord('ArrowLeft'))
assert.deepEqual(chordOf(key('A', { code: 'KeyA', modifiers: { shift: true } })), chord('KeyA', { shift: true }))
for (const bad of ['', 'Shift+', 'Shift+Shift+KeyA', 'Hyper+KeyA', 'ShiftLeft', 'Ctrl+ControlRight', 'Key A', '+'.repeat(3)]) {
assert.equal(parseChordId(bad), null, bad)
}
assert.equal(isModifierCode('AltRight'), true); assert.equal(isModifierCode('CapsLock'), false)
const said = (code, mods) => describeChord(chord(code, mods))
assert.equal(said('Escape', { shift: true }), 'Shift+Esc'); assert.equal(said('ArrowLeft'), 'Left')
assert.equal(said('KeyA', { ctrl: true, alt: true }), 'Ctrl+Alt+A'); assert.equal(said('Digit7'), '7')
assert.equal(said('AudioVolumeUp'), 'Volume Up'); assert.equal(said('MediaPlayPause'), 'Play/Pause')
assert.equal(said('NumpadEnter'), 'Numpad Enter'); assert.equal(said('F5'), 'F5'); assert.equal(said('Slash'), '/')
// A remote's unnamed buttons keep the only name they have.
assert.equal(said('Consumer1cb'), 'Media key 0x1cb'); assert.equal(said('Usagea5'), 'Key 0xa5')
})
test('custom keybindings survive storage; junk in storage is dropped', () => {
const bindings = [
{ chord: chord('AudioVolumeUp'), action: 'scroll-up' },
{ chord: chord('KeyQ', { ctrl: true }), action: 'long-press' },
{ chord: chord('ArrowLeft'), action: 'none' },
]
assert.deepEqual(parseKeyBindings(serializeKeyBindings(bindings)), bindings)
assert.deepEqual([...keyBindingMap(bindings)], [['AudioVolumeUp', 'scroll-up'], ['Ctrl+KeyQ', 'long-press'], ['ArrowLeft', 'none']])
assert.deepEqual(parseKeyBindings(''), []); assert.deepEqual(parseKeyBindings('{'), []); assert.deepEqual(parseKeyBindings('{"a":1}'), [])
assert.deepEqual(parseKeyBindings(JSON.stringify([
null, 7, { key: 'KeyA' }, { key: 'KeyA', action: 'explode' }, { key: 'Shift+ShiftLeft', action: 'click' },
{ key: 'KeyB', action: 'click' }, { key: 'KeyB', action: 'double-click' },
])), [{ chord: chord('KeyB'), action: 'double-click' }])
const many = Array.from({ length: 200 }, (_, i) => ({ key: `F${i + 1}`, action: 'click' }))
assert.equal(parseKeyBindings(JSON.stringify(many)).length, MAX_KEY_BINDINGS)
// Every action has a label for the picker, and the list has no duplicates.
assert.equal(new Set(KEY_ACTIONS.map(a => a.action)).size, KEY_ACTIONS.length)
assert.equal(keyActionLabel('double-click'), 'Back'); assert.equal(keyActionLabel('none'), 'Nothing')
})
test('resolution: custom first and everywhere, then the built-ins; "Nothing" only removes the built-in', () => {
const custom = keyBindingMap([
{ chord: chord('AudioVolumeUp'), action: 'scroll-up' },
{ chord: chord('ArrowLeft'), action: 'double-click' },
{ chord: chord('ArrowRight'), action: 'none' },
{ chord: chord('Escape', { shift: true }), action: 'none' },
])
// Unbound keys: typed where something takes keys, otherwise the built-in.
assert.deepEqual(resolveKey(key('a', { code: 'KeyA' }), custom), { consume: false, gesture: null })
assert.deepEqual(resolveKey(key('ArrowUp'), custom), { consume: false, gesture: 'swipe-up' })
assert.deepEqual(resolveKey(key('Escape'), custom), { consume: false, gesture: 'double-click' })
assert.deepEqual(resolveKey(key('Escape', { modifiers: { ctrl: true } }), custom), { consume: true, gesture: 'double-click' })
// Custom: spoken for even in a terminal.
assert.deepEqual(resolveKey(key('AudioVolumeUp'), custom), { consume: true, gesture: 'scroll-up' })
assert.deepEqual(resolveKey(key('ArrowLeft'), custom), { consume: true, gesture: 'double-click' })
// The chord is exact: Shift+Left is not the Left keybinding.
assert.deepEqual(resolveKey(key('ArrowLeft', { modifiers: { shift: true } }), custom), { consume: false, gesture: 'swipe-left' })
// "Nothing": no navigation, still typed; and it can take a built-in override away.
assert.deepEqual(resolveKey(key('ArrowRight'), custom), { consume: false, gesture: null })
assert.deepEqual(resolveKey(key('Escape', { modifiers: { shift: true } }), custom), { consume: false, gesture: null })
// Held keys: moving repeats, the rest acts once but stays spoken for.
assert.deepEqual(resolveKey(key('AudioVolumeUp', { repeat: true }), custom), { consume: true, gesture: 'scroll-up' })
assert.deepEqual(resolveKey(key('ArrowLeft', { repeat: true }), custom), { consume: true, gesture: null })
assert.deepEqual(resolveKey(key('ArrowDown', { repeat: true }), none), { consume: false, gesture: 'swipe-down' })
assert.deepEqual(resolveKey(key('ArrowRight', { repeat: true }), none), { consume: false, gesture: null })
assert.deepEqual(resolveKey(key('Enter', { repeat: true }), none), { consume: false, gesture: null })
assert.deepEqual(['swipe-left', 'swipe-right', 'click', 'double-click', 'long-press'].filter(gestureRepeats), [])
})
test('taps go with whichever scheme the keyboard last moved by', () => {
const scheme = new GestureScheme()
assert.equal(scheme.originFor('click'), 'watch')
assert.equal(scheme.originFor('scroll-down'), 'ring'); assert.equal(scheme.originFor('click'), 'ring')
assert.equal(scheme.originFor('double-click'), 'ring'); assert.equal(scheme.originFor('long-press'), 'ring')
assert.equal(scheme.originFor('swipe-left'), 'watch'); assert.equal(scheme.originFor('click'), 'watch')
assert.equal(scheme.originFor('short-then-long-press'), 'watch')
})
function router(options = {}) {
const state = {
ready: true, locked: false, screenOn: true, windowTakesKeys: false, now: 1000,
bindings: keyBindingMap(options.bindings ?? []), gestures: [], offered: [], wakes: 0, activity: 0, pending: [],
hold: false,
}
const r = new KeyRouter({
ready: () => state.ready, locked: () => state.locked, screenOn: () => state.screenOn,
wake: () => { state.wakes++ },
offerToWindow: event => { state.offered.push(`${event.phase}:${event.key}`); return state.windowTakesKeys },
gesture: (gesture, origin) => {
state.gestures.push(`${gesture}/${origin}`)
return state.hold ? new Promise(resolve => state.pending.push(resolve)) : Promise.resolve()
},
bindings: () => state.bindings, noteActivity: () => { state.activity++ }, log: () => {}, now: () => state.now,
})
return { r, state }
}
const up = (k, overrides) => key(k, { ...overrides, phase: 'up' })
const flush = () => new Promise(resolve => setImmediate(resolve))
test('router: navigation where nothing takes keys, typing where something does, Shift+Esc out of either', () => {
const { r, state } = router()
r.handleKey(key('ArrowLeft')); r.handleKey(up('ArrowLeft')); r.handleKey(key('Enter')); r.handleKey(key('Escape'))
assert.deepEqual(state.gestures, ['swipe-left/watch', 'click/watch', 'double-click/watch'])
// A terminal session: the same keys are its own, Escape included.
state.windowTakesKeys = true; state.gestures.length = 0; state.offered.length = 0
for (const k of ['ArrowLeft', 'Enter', 'Escape', 'a']) { r.handleKey(key(k)); r.handleKey(up(k)) }
assert.deepEqual(state.gestures, [])
assert.deepEqual(state.offered, ['down:ArrowLeft', 'up:ArrowLeft', 'down:Enter', 'up:Enter', 'down:Escape', 'up:Escape', 'down:a', 'up:a'])
// Shift+Esc (or Ctrl, or Alt) backs out, and neither its press nor its release is typed.
state.offered.length = 0
for (const modifier of ['shift', 'ctrl', 'alt']) {
r.handleKey(key('Escape', { modifiers: { [modifier]: true } })); r.handleKey(up('Escape'))
}
assert.deepEqual(state.gestures, ['double-click/watch', 'double-click/watch', 'double-click/watch'])
assert.deepEqual(state.offered, [])
})
test('router: a key the window hands back gets its navigation meaning; one it kept does not', () => {
const { r, state } = router()
state.windowTakesKeys = true
r.handleKey(key('ArrowDown'))
assert.deepEqual(state.gestures, [])
// The terminal was showing its session list: the key comes back.
r.handleUnhandledKey(key('ArrowDown'))
assert.deepEqual(state.gestures, ['swipe-down/watch'])
r.handleUnhandledKey(key('a')); r.handleUnhandledKey(up('ArrowDown'))
assert.equal(state.gestures.length, 1)
// Stale by the time it comes back: the display went dark, or locked.
state.screenOn = false; r.handleUnhandledKey(key('ArrowDown'))
state.screenOn = true; state.locked = true; r.handleUnhandledKey(key('ArrowDown'))
assert.equal(state.gestures.length, 1); assert.equal(state.wakes, 0)
})
test('router: custom keybindings work in a terminal too, and "Nothing" leaves the key to be typed', () => {
const { r, state } = router({ bindings: [
{ chord: chord('AudioVolumeUp'), action: 'scroll-up' },
{ chord: chord('AudioVolumeDown'), action: 'scroll-down' },
{ chord: chord('MediaPlayPause'), action: 'click' },
{ chord: chord('ArrowLeft'), action: 'none' },
] })
state.windowTakesKeys = true
r.handleKey(key('AudioVolumeDown')); r.handleKey(up('AudioVolumeDown')); r.handleKey(key('MediaPlayPause')); r.handleKey(up('MediaPlayPause'))
// A remote bound to scroll is a ring: its select is a ring's tap.
assert.deepEqual(state.gestures, ['scroll-down/ring', 'click/ring']); assert.deepEqual(state.offered, [])
r.handleKey(key('ArrowLeft'))
assert.deepEqual(state.offered, ['down:ArrowLeft'])
// And where nothing takes keys, "Nothing" is nothing.
state.windowTakesKeys = false; state.gestures.length = 0
r.handleKey(key('ArrowLeft')); r.handleUnhandledKey(key('ArrowLeft'))
assert.deepEqual(state.gestures, [])
})
test('router: a dark display only wakes, a locked one stays locked, and nothing happens off the glasses', () => {
const { r, state } = router()
state.screenOn = false
r.handleKey(key('a')); r.handleKey(key('a', { repeat: true })); r.handleKey(up('a'))
assert.equal(state.wakes, 1); assert.deepEqual(state.gestures, []); assert.deepEqual(state.offered, [])
state.locked = true
r.handleKey(key('Enter'))
assert.equal(state.wakes, 2)
state.screenOn = true
r.handleKey(key('Enter')); r.handleKey(key('Escape', { modifiers: { shift: true } }))
assert.equal(state.wakes, 2); assert.deepEqual(state.gestures, [])
state.locked = false; state.ready = false
r.handleKey(key('Enter'))
assert.deepEqual(state.gestures, [])
})
test('router: a held key repeats movement at a rate the display can follow, and never queues behind itself', async () => {
const { r, state } = router()
r.handleKey(key('ArrowDown'))
for (let i = 1; i <= 20; i++) { await flush(); state.now = 1400 + i * 50; r.handleKey(key('ArrowDown', { repeat: true })) }
// Twenty repeats a second arrive; every other one is acted on.
assert.equal(state.gestures.length, 1 + 10)
// While a gesture is still being handled, repeats are dropped rather than queued.
state.gestures.length = 0; state.hold = true; state.now += 1000
r.handleKey(key('ArrowUp'))
for (let i = 1; i <= 5; i++) { state.now += 200; r.handleKey(key('ArrowUp', { repeat: true })) }
assert.equal(state.gestures.length, 1)
state.pending.splice(0).forEach(resolve => resolve()); await flush()
state.now += 200; r.handleKey(key('ArrowUp', { repeat: true }))
assert.equal(state.gestures.length, 2)
// A fresh press is never dropped, even on the heels of another gesture.
r.handleKey(up('ArrowUp')); r.handleKey(key('Enter'))
assert.equal(state.gestures.at(-1), 'click/watch')
})
test('router: capturing a key for a new keybinding takes the chord and nothing else happens', () => {
const { r, state } = router()
const captured = []
r.captureNextKey(c => captured.push(c))
// Modifiers alone are waited through; the key pressed with them is the chord.
r.handleKey(key('Shift', { code: 'ShiftLeft', modifiers: { shift: true } }))
assert.deepEqual(captured, [])
r.handleKey(key('Escape', { modifiers: { shift: true } }))
assert.deepEqual(captured, [chord('Escape', { shift: true })])
// Its release, and the modifier's, do nothing; the next key is ordinary again.
r.handleKey(up('Escape')); r.handleKey(up('Shift', { code: 'ShiftLeft' }))
assert.deepEqual(state.gestures, []); assert.ok(state.activity >= 2)
r.handleKey(key('Enter'))
assert.deepEqual(state.gestures, ['click/watch'])
// Called off: keys go back to meaning what they mean.
const cancel = r.captureNextKey(c => captured.push(c))
cancel()
r.handleKey(key('ArrowUp'))
assert.equal(captured.length, 1); assert.equal(state.gestures.at(-1), 'swipe-up/watch')
// A second capture replaces the first, and the first one's cancel can't undo it.
const first = r.captureNextKey(() => captured.push('first'))
r.captureNextKey(c => captured.push(c))
first()
r.handleKey(key('x', { code: 'KeyX' }))
assert.deepEqual(captured.at(-1), chord('KeyX'))
})
test('a mouse by default: left taps, right double taps, middle holds, the wheel is the ring\'s scroll', () => {
const action = (k, overrides) => defaultKeyAction(key(k, overrides))
// Nothing types a mouse button, so these hold in a terminal session too.
assert.deepEqual(action('MouseButton1'), { action: 'click', overridesTyping: true })
assert.deepEqual(action('MouseButton2'), { action: 'double-click', overridesTyping: true })
assert.deepEqual(action('MouseButton3'), { action: 'long-press', overridesTyping: true })
assert.deepEqual(action('WheelUp'), { action: 'scroll-up', overridesTyping: true })
assert.deepEqual(action('WheelDown'), { action: 'scroll-down', overridesTyping: true })
// Whatever is held on the keyboard.
assert.deepEqual(action('MouseButton1', { modifiers: { ctrl: true, shift: true } }), { action: 'click', overridesTyping: true })
// Extra buttons and the horizontal wheel mean nothing until bound.
for (const k of ['MouseButton4', 'MouseButton5', 'WheelLeft', 'WheelRight']) assert.equal(action(k), null)
const said = code => describeChord(chord(code))
assert.equal(said('MouseButton1'), 'Left button'); assert.equal(said('MouseButton2'), 'Right button')
assert.equal(said('MouseButton3'), 'Middle button'); assert.equal(said('MouseButton4'), 'Mouse button 4')
assert.equal(said('WheelUp'), 'Wheel up'); assert.equal(describeChord(chord('WheelDown', { ctrl: true })), 'Ctrl+Wheel down')
assert.deepEqual(parseChordId('Shift+MouseButton2'), chord('MouseButton2', { shift: true }))
})
test('router: the mouse works where keys are being typed, its wheel is paced, and its buttons can be rebound', async () => {
const { r, state } = router({ bindings: [{ chord: chord('MouseButton4'), action: 'short-then-long-press' }, { chord: chord('MouseButton2'), action: 'none' }] })
const wheel = k => { r.handleKey(key(k, { burst: true })); r.handleKey(up(k, { burst: true })) }
state.windowTakesKeys = true
r.handleKey(key('MouseButton1')); r.handleKey(up('MouseButton1'))
r.handleKey(key('MouseButton3')); r.handleKey(up('MouseButton3'))
await flush(); state.now += 500
wheel('WheelDown')
// The wheel is the ring's scroll, and what follows it is the ring's too.
await flush(); state.now += 500
r.handleKey(key('MouseButton1')); r.handleKey(up('MouseButton1'))
assert.deepEqual(state.gestures, ['click/watch', 'long-press/watch', 'scroll-down/ring', 'click/ring'])
assert.deepEqual(state.offered, [], 'never typed')
// A custom keybinding for an extra button; "Nothing" on the right button leaves it inert.
state.gestures.length = 0
r.handleKey(key('MouseButton4')); r.handleKey(up('MouseButton4')); r.handleKey(key('MouseButton2')); r.handleKey(up('MouseButton2'))
assert.deepEqual(state.gestures, ['short-then-long-press/ring'])
// A two-finger scroll reports far faster than the display can follow: every 100 ms at most.
state.gestures.length = 0; state.offered.length = 0
for (let i = 0; i < 40; i++) { await flush(); state.now += 25; wheel('WheelUp') }
assert.equal(state.gestures.length, 10); assert.ok(state.gestures.every(g => g === 'scroll-up/ring'))
// On a dark display a click wakes it and does nothing else.
state.screenOn = false; state.gestures.length = 0
r.handleKey(key('MouseButton1')); r.handleKey(up('MouseButton1'))
assert.equal(state.wakes, 1); assert.deepEqual(state.gestures, [])
})
test('router: a mouse button or a turn of the wheel can be captured for a keybinding', () => {
const { r, state } = router()
const captured = []
r.captureNextKey(c => captured.push(c))
r.handleKey(key('MouseButton2')); r.handleKey(up('MouseButton2'))
r.captureNextKey(c => captured.push(c))
r.handleKey(key('WheelUp', { burst: true, modifiers: { shift: true } })); r.handleKey(up('WheelUp', { burst: true }))
assert.deepEqual(captured, [chord('MouseButton2'), chord('WheelUp', { shift: true })])
assert.deepEqual(state.gestures, [])
})
+583
View File
@@ -0,0 +1,583 @@
const test = require('node:test')
const assert = require('node:assert/strict')
const { HidKeys, parseReportMap, reportHasKeys, reportHasPointer, reportKeys, reportPointer } = require('../.test-build/app/g2/keyboard/hid.js')
const {
KeyboardRecords, keyboardControl, le16, u16, parseKeyboardAdvert, mergeKeyboardAdverts,
parseKeyboardAddress, formatKeyboardAddress,
} = require('../.test-build/app/g2/keyboard/protocol.js')
const { KeyboardClient, KeyboardClientError, attributeUuid } = require('../.test-build/app/g2/keyboard/client.js')
const { KeyboardBridge } = require('../.test-build/app/g2/keyboard/bridge.js')
// The HID vectors and the simulated HOGP keyboard follow g2flash demos/keyboard.test.ts.
const boot = Uint8Array.from([
0x05,1,0x09,6,0xa1,1,0x05,7,0x19,0xe0,0x29,0xe7,0x15,0,0x25,1,0x75,1,0x95,8,0x81,2,
0x75,8,0x95,1,0x81,1,0x95,6,0x75,8,0x15,0,0x25,0x65,0x05,7,0x19,0,0x29,0x65,0x81,0,0xc0,
])
const numbered = Uint8Array.from([
...boot.slice(0, 6), 0x85,1,...boot.slice(6, -1),
0x85,2,0x05,0x0c,0x15,0,0x25,1,0x75,1,0x95,3,0x09,0xe9,0x09,0xea,0x09,0xe2,0x81,2,
0x75,5,0x95,1,0x81,1,0xc0,
])
function frame(kind, seq, epoch, request, code, handle, data, offset = 0, total = data.length) {
const p = new Uint8Array(24 + data.length), v = new DataView(p.buffer)
p.set([75,66,1,kind]); v.setUint32(4,seq,true); v.setUint16(8,epoch,true); v.setUint16(10,request,true)
v.setUint16(12,code,true); v.setUint16(14,handle,true); v.setUint16(16,total,true); v.setUint16(18,offset,true)
v.setUint32(20,1234,true); p.set(data,24); return p
}
const flush = () => new Promise(resolve => setImmediate(resolve))
async function settle(rounds = 20) { for (let i = 0; i < rounds; i++) await flush() }
test('boot array, modifiers, release, repeats and rollover', () => {
const r = parseReportMap(boot).get(0), keys = new HidKeys()
assert.deepEqual(keys.update(12,r,Uint8Array.of(2,0,4,0,0,0,0,0)), [
{page:7,usage:0xe1,down:true},{page:7,usage:4,down:true},
])
assert.deepEqual(keys.update(12,r,Uint8Array.of(2,0,4,0,0,0,0,0)), [])
assert.throws(() => keys.update(12,r,Uint8Array.of(0,0,1,1,1,1,1,1)), /rollover/)
assert.deepEqual(keys.reset().map(k => k.down), [false,false])
assert.throws(() => reportKeys(r,Uint8Array.of(0)), /length/)
})
test('HOGP report IDs are out of band, with consumer keys', () => {
const maps = parseReportMap(numbered)
assert.deepEqual(reportKeys(maps.get(1),Uint8Array.of(0,0,5,0,0,0,0,0)), [{page:7,usage:5}])
assert.deepEqual(reportKeys(maps.get(2),Uint8Array.of(5)), [{page:12,usage:0xe9},{page:12,usage:0xe2}])
assert.throws(() => reportKeys(maps.get(2),Uint8Array.of(2,5)), /length/)
})
test('NKRO bitmap with non-byte-aligned fields and report union', () => {
const map = Uint8Array.of(5,7,0x19,4,0x29,19,0x15,0,0x25,1,0x75,1,0x95,16,0x81,2)
const r = parseReportMap(map).get(0), keys = new HidKeys()
assert.deepEqual(reportKeys(r,Uint8Array.of(0x81,0x80)), [{page:7,usage:4},{page:7,usage:11},{page:7,usage:19}])
assert.equal(keys.update(10,r,Uint8Array.of(1,0)).length, 1)
assert.deepEqual(keys.update(11,r,Uint8Array.of(1,0)), [])
assert.deepEqual(keys.update(10,r,Uint8Array.of(0,0)), [])
assert.deepEqual(keys.update(11,r,Uint8Array.of(0,0)), [{page:7,usage:4,down:false}])
})
test('array values index usages relative to logical minimum', () => {
const r = parseReportMap(Uint8Array.of(5,7,0x19,4,0x29,6,0x15,1,0x25,3,0x75,8,0x95,1,0x81,0)).get(0)
assert.deepEqual(reportKeys(r,Uint8Array.of(2)), [{page:7,usage:5}])
assert.deepEqual(reportKeys(r,Uint8Array.of(0)), [])
})
test('descriptor resource bounds and malformed items', () => {
for (const data of [new Uint8Array(),new Uint8Array(513),Uint8Array.of(0xfe),Uint8Array.of(0x75),
Uint8Array.of(0xb4),Uint8Array.of(0xc0),Uint8Array.of(0x85,0),Uint8Array.of(0x75,33,0x95,1,0x81,2),
Uint8Array.of(0x75,32,0x96,0,2,0x81,2),Uint8Array.of(0xa1,1)]) assert.throws(() => parseReportMap(data))
// Deterministic malformed corpus; parsing must be bounded and never hang.
let seed = 1
for (let i = 0; i < 1000; i++) {
const data = Uint8Array.from({length:i%128+1}, () => { seed=(Math.imul(seed,1664525)+1013904223)>>>0; return seed>>>24 })
try { parseReportMap(data) } catch (e) { assert.ok(e instanceof Error) }
}
})
// A wheel mouse as most are described: three buttons, 8-bit X/Y and wheel.
const mouseMap = Uint8Array.from([0x05,1,0x09,2,0xa1,1,0x09,1,0xa1,0,0x05,9,0x19,1,0x29,3,0x15,0,0x25,1,0x75,1,0x95,3,0x81,2,
0x75,5,0x95,1,0x81,1,0x05,1,0x09,0x30,0x09,0x31,0x09,0x38,0x15,0x81,0x25,0x7f,0x75,8,0x95,3,0x81,6,0xc0,0xc0])
test('a mouse report: buttons, movement and wheel; not keys', () => {
const r = parseReportMap(mouseMap).get(0)
assert.equal(reportHasPointer(r), true); assert.equal(reportHasKeys(r), false)
assert.deepEqual(reportPointer(r, Uint8Array.of(0, 0, 0, 0)), { buttons: 0, dx: 0, dy: 0, wheel: 0, pan: 0 })
assert.deepEqual(reportPointer(r, Uint8Array.of(0b101, 10, 0xf6, 1)), { buttons: 5, dx: 10, dy: -10, wheel: 1, pan: 0 })
assert.deepEqual(reportPointer(r, Uint8Array.of(0b010, 0, 0, 0xff)), { buttons: 2, dx: 0, dy: 0, wheel: -1, pan: 0 })
// Some devices leave trailing bytes off; what is missing is zero. Extra bytes are ignored.
assert.deepEqual(reportPointer(r, Uint8Array.of(1, 3)), { buttons: 1, dx: 3, dy: 0, wheel: 0, pan: 0 })
assert.deepEqual(reportPointer(r, Uint8Array.of(0, 1, 1, 1, 9, 9)), { buttons: 0, dx: 1, dy: 1, wheel: 1, pan: 0 })
// -128 is outside the declared range: no reading, rather than a jump.
assert.deepEqual(reportPointer(r, Uint8Array.of(0, 0x80, 0, 0)), { buttons: 0, dx: 0, dy: 0, wheel: 0, pan: 0 })
assert.deepEqual(reportKeys(r, Uint8Array.of(7, 1, 1, 1)), [])
// A keyboard's report is not a pointer's.
assert.equal(reportHasPointer(parseReportMap(boot).get(0)), false)
})
test('a touchpad with 12-bit axes and a horizontal wheel; an absolute digitizer is not a mouse', () => {
// Five buttons, 3 bits of padding, X/Y packed 12 bits each, wheel, then AC Pan from the consumer page.
const map = Uint8Array.from([0x05,1,0x09,2,0xa1,1,0x05,9,0x19,1,0x29,5,0x15,0,0x25,1,0x75,1,0x95,5,0x81,2,
0x75,3,0x95,1,0x81,1,0x05,1,0x09,0x30,0x09,0x31,0x16,0x01,0xf8,0x26,0xff,0x07,0x75,12,0x95,2,0x81,6,
0x09,0x38,0x15,0x81,0x25,0x7f,0x75,8,0x95,1,0x81,6,0x05,0x0c,0x0a,0x38,0x02,0x81,6,0xc0])
const r = parseReportMap(map).get(0)
assert.equal(reportHasPointer(r), true)
// X = 0x123 (291), Y = 0xfff (-1): packed little endian, 12 bits each.
assert.deepEqual(reportPointer(r, Uint8Array.of(0b10000, 0x23, 0xf1, 0xff, 2, 0xfe)), { buttons: 16, dx: 291, dy: -1, wheel: 2, pan: -2 })
// The pan field is on the consumer page, but it is a movement, not a key going down.
assert.equal(reportHasKeys(r), false); assert.deepEqual(reportKeys(r, Uint8Array.of(0, 0, 0, 0, 0, 5)), [])
// Absolute X/Y (a touchscreen, a pen) is something else, and is left alone.
const absolute = parseReportMap(Uint8Array.of(0x05,1,0x09,0x30,0x09,0x31,0x15,0,0x26,0xff,0x0f,0x75,16,0x95,2,0x81,2)).get(0)
assert.equal(reportHasPointer(absolute), false)
assert.deepEqual(reportPointer(absolute, Uint8Array.of(1, 2, 3, 4)), { buttons: 0, dx: 0, dy: 0, wheel: 0, pan: 0 })
})
test('control records, UUIDs and addresses', () => {
assert.deepEqual([...keyboardControl(4,0x1234,0x5678,[1,2,3,4,5,6,7])], [75,66,1,4,0x34,0x12,0x78,0x56,1,2,3,4,5,6,7])
assert.throws(() => keyboardControl(13,1,1))
assert.throws(() => keyboardControl(11,1,1,new Array(73).fill(0)))
assert.equal(attributeUuid(Uint8Array.of(0xfb,0x34,0x9b,0x5f,0x80,0,0,0x80,0,0x10,0,0,0x12,0x18,0,0)), 0x1812)
const address = parseKeyboardAddress('AA:bb:cc:dd:ee:01')
assert.deepEqual([...address], [1,0xee,0xdd,0xcc,0xbb,0xaa])
assert.equal(formatKeyboardAddress(address), 'aa:bb:cc:dd:ee:01')
assert.equal(parseKeyboardAddress('aa:bb:cc:dd:ee'), null)
assert.equal(parseKeyboardAddress('aa:bb:cc:dd:ee:gg'), null)
})
test('fragments, duplicate delivery, gaps, epochs and sequence wrap', () => {
let gaps = 0; const stream = new KeyboardRecords(() => gaps++)
const data = Uint8Array.from({length:200}, (_,i) => i)
assert.equal(stream.push(frame(3,0xffffffff,1,0,13,33,data.slice(0,96),0,200)), undefined)
assert.equal(stream.push(frame(3,0xffffffff,1,0,13,33,data.slice(96,192),96,200)), undefined)
assert.deepEqual(stream.push(frame(3,0xffffffff,1,0,13,33,data.slice(192),192,200)).data, data)
const status = frame(1,0,1,0,0,0,new Uint8Array(10))
assert.equal(stream.push(status).sequence, 0)
assert.equal(stream.push(status), undefined); assert.equal(gaps, 1)
stream.push(frame(1,2,1,0,0,0,new Uint8Array(10))); assert.equal(gaps, 2)
assert.equal(stream.push(status), undefined); assert.equal(gaps, 2)
stream.push(frame(1,3,2,0,0,0,new Uint8Array(10))); assert.equal(gaps, 3)
})
test('missing or mismatched fragments never emit partial input', () => {
let gaps = 0; const stream = new KeyboardRecords(() => gaps++)
stream.push(frame(3,1,1,0,13,10,new Uint8Array(96),0,100))
assert.equal(stream.push(frame(3,1,1,0,13,11,new Uint8Array(4),96,100)), undefined)
assert.equal(gaps, 2)
assert.equal(stream.push(frame(3,1,1,0,13,10,new Uint8Array(4),96,100)), undefined)
assert.equal(stream.push(new Uint8Array(513)), undefined)
// Another protocol version is not ours to decode.
const future = frame(1,5,1,0,0,0,new Uint8Array(10)); future[2] = 2
assert.equal(stream.push(future), undefined)
})
test('adverts: name from the scan response, HID from the service list or the appearance', () => {
const body = (ad, rssi = -60) => Uint8Array.from([1, 6,5,4,3,2,1, rssi & 255, 0, ad.length, ...ad])
const services = parseKeyboardAdvert(body([2,1,6, 3,3,0x12,0x18]))
assert.deepEqual(services, { addressType: 1, address: '01:02:03:04:05:06', rssi: -60, name: '', hid: true })
const named = parseKeyboardAdvert(body([5,9,0x4b,0x33,0x38,0x30], -48))
assert.equal(named.name, 'K380'); assert.equal(named.hid, false)
assert.deepEqual(mergeKeyboardAdverts(services, named), { ...services, name: 'K380', rssi: -48 })
assert.equal(parseKeyboardAdvert(body([3,0x19,0xc1,0x03])).hid, true)
assert.equal(parseKeyboardAdvert(body([3,0x19,0x41,0x03])).hid, false)
assert.equal(parseKeyboardAdvert(Uint8Array.of(1,2,3)), null)
// A truncated AD structure ends the parse without reading past the body.
assert.equal(parseKeyboardAdvert(body([9,9,0x41])).name, '')
})
/**
* The firmware bridge and a HOGP keyboard behind it, as far as the phone can
* tell: answers control records with the records the real one sends.
*/
function fakeGlasses(deliver) {
const attrs = new Map()
const add = (h, uuid, bytes) => attrs.set(h, { uuid, value: Uint8Array.from(bytes) })
const mockMap = Uint8Array.from([...numbered.slice(0,-1),0x85,3,0x05,6,0x09,0x20,0x15,0,0x25,100,0x75,8,0x95,1,0x81,2,0xc0])
add(1,0x2800,[1,0x18]); add(2,0x2803,[0x20,3,0,5,0x2a]); add(3,0x2a05,[]); add(4,0x2902,[0,0])
add(20,0x2800,[0x12,0x18])
add(21,0x2803,[2,22,0,0x4b,0x2a]); add(22,0x2a4b,mockMap); add(23,0x2907,[0x19,0x2a])
add(24,0x2803,[6,25,0,0x4e,0x2a]); add(25,0x2a4e,[1])
add(26,0x2803,[0x12,27,0,0x4d,0x2a]); add(27,0x2a4d,[]); add(28,0x2902,[0,0]); add(29,0x2908,[1,1])
add(30,0x2803,[0x12,31,0,0x4d,0x2a]); add(31,0x2a4d,[]); add(32,0x2902,[0,0]); add(33,0x2908,[2,1])
add(40,0x2800,[0x0f,0x18]); add(41,0x2803,[0x12,42,0,0x19,0x2a]); add(42,0x2a19,[100]); add(43,0x2902,[0,0]); add(44,0x2908,[3,1])
const g = {
seq: 0, epoch: 0, enabled: 0, conn: 0, connected: 0, encrypted: 0, scanning: 0,
ops: [], writes: [], watches: [], adverts: [], pins: [], compares: [],
reachable: true, bonded: false, pairing: 'just-works', up: true,
// The keyboard's attribute table, and how its stack answers a direct read
// of a characteristic declaration: 'full', 'short' (the properties byte
// alone) or 'refused'.
attrs, add, declarations: 'full',
}
g.emit = (kind, request, code, handle, data = []) =>
deliver(frame(kind, ++g.seq, g.epoch, request, code, handle, Uint8Array.from(data)))
g.status = (request = 0, code = 0) =>
g.emit(1, request, code, 0, [g.enabled, g.conn, g.connected, g.encrypted, g.scanning, 0, 0,0,0,0])
g.dm = (event, status = 0, data = []) => g.emit(4, 0, event | (status << 8), 0, data)
g.close = () => { if (!g.conn) return; g.conn = g.connected = g.encrypted = 0; g.watches = []; g.dm(0x28); g.status() }
g.encrypt = () => { g.encrypted = 1; g.dm(0x2c); g.status() }
g.input = (handle, bytes) => g.emit(3, 0, 13, handle, bytes)
g.send = (record) => {
if (!g.up) return false
const op = record[3], req = u16(record,4), epoch = u16(record,6), h = record.length >= 10 ? u16(record,8) : 0
g.ops.push(op)
if (op !== 0 && op !== 1 && epoch !== g.epoch) { g.status(req, 4); return true }
if (op === 0) g.status(req)
else if (op === 1) { g.enabled = 1; g.status(req) }
else if (op === 2) { g.enabled = 0; g.status(req); g.close() }
else if (op === 3) {
if (g.conn) { g.status(req, 3); return true }
g.scanning = 1; g.status(req)
for (const advert of g.adverts) g.emit(5, 0, 0, 0, advert)
g.scanning = 0; g.status()
} else if (op === 4) {
if (g.conn) { g.status(req, 3); return true }
g.epoch = (g.epoch + 1) & 0xffff; g.conn = 3; g.status(req)
if (!g.reachable) return true
g.connected = 1; g.dm(0x27); g.status()
if (g.bonded) g.encrypt()
} else if (op === 5) { g.status(req); g.close() }
else if (op === 6) {
if (!g.connected) { g.status(req, 6); return true }
g.status(req)
if (g.pairing === 'just-works') { g.dm(0x2a); g.encrypt() }
else if (g.pairing === 'display') g.dm(0x2e, 0, [0, 1])
else if (g.pairing === 'entry') g.dm(0x2e, 0, [0, 0])
else if (g.pairing === 'compare') g.dm(0x35, 0, [0x40, 0xe2, 0x01, 0])
else if (g.pairing === 'fail') { g.dm(0x2b, 5); g.close() }
} else if (op === 7) {
g.pins.push(record[8] | (record[9] << 8) | (record[10] << 16)); g.status(req); g.dm(0x2a); g.encrypt()
} else if (op === 8) {
g.compares.push(record[8]); g.status(req)
if (record[8]) { g.dm(0x2a); g.encrypt() } else { g.dm(0x2b, 4); g.close() }
} else if (op === 9) {
const batch = [...attrs].filter(([handle]) => handle >= h).slice(0,4)
g.emit(2, req, batch.length ? 2 : (10<<8)|2, h, batch.length ? [1, ...batch.flatMap(([handle,a]) => [...le16(handle), ...le16(a.uuid)])] : [])
} else if (op === 10) {
const attr = attrs.get(h), offset = u16(record,10)
if (attr.uuid === 0x2803 && g.declarations === 'refused') { g.emit(2, req, (2<<8)|5, h); return true }
const data = attr.uuid === 0x2803 && g.declarations === 'short' ? attr.value.slice(0,1) : attr.value
g.emit(2, req, offset >= data.length ? (7<<8)|6 : offset ? 6 : 5, h, data.slice(offset, offset+20))
} else if (op === 11) { g.writes.push(h); g.emit(2, req, record[10] ? 10 : 9, h) }
else if (op === 12) { g.watches = Array.from({length:record[8]}, (_,i) => u16(record,9+2*i)); g.status(req) }
else throw new Error(`Unexpected op ${op}`)
return true
}
return g
}
test('client: HOGP discovery, long Report Map, subscriptions, typing and loss recovery', async () => {
const events = []
let client
const g = fakeGlasses(payload => client.accept(payload))
client = new KeyboardClient(g.send, { keys: e => events.push(...e) })
await client.enable(); await client.connect(1, new Uint8Array(6)); const config = await client.configure()
// The battery level (an external report) is found, but it can't carry a
// key, so it isn't subscribed: only the two key reports and Service Changed.
assert.deepEqual(g.watches, [27,31,3]); assert.deepEqual(g.writes, [25,4,28,32])
assert.deepEqual(config.reports.map(r => r.id), [1,2,3]); assert.deepEqual(config.watched, [27,31])
assert.deepEqual(config.reports.map(r => r.properties), [0x12,0x12,0x12])
g.input(27, [0,0,4,0,0,0,0,0]); g.input(31, [1])
assert.deepEqual(events.map(e => [e.usage,e.down]), [[4,true],[0xe9,true]])
g.seq++ // a release lost in the firmware queue or on the way to the phone
g.status()
assert.deepEqual(events.slice(-2).map(e => e.down), [false,false])
// Another client on the same connection can take over with the mapping.
const restored = new KeyboardClient(() => true, { keys: e => events.push(...e) })
restored.restore(config); restored.accept(frame(3, g.seq + 1, g.epoch, 0, 13, 27, Uint8Array.of(0,0,5,0,0,0,0,0)))
assert.deepEqual([events.at(-1).usage, events.at(-1).down], [5, true]); restored.lostPhoneLink()
assert.deepEqual([events.at(-1).usage, events.at(-1).down], [5, false])
// Service Changed voids the handles: no more keys until configure() runs again.
g.emit(3, 0, 14, 3, [1,0,255,255])
const count = events.length; g.input(27, [0,0,6,0,0,0,0,0])
assert.equal(events.length, count)
client.close(); restored.close()
})
// A pointer's report, as a keyboard with a built-in trackpad adds: buttons and relative X/Y.
const mouseCollection = [0x05,1,0x09,2,0xa1,1,0x85,4,0x09,1,0xa1,0,0x05,9,0x19,1,0x29,3,0x15,0,0x25,1,0x75,1,0x95,3,0x81,2,
0x75,5,0x95,1,0x81,1,0x05,1,0x09,0x30,0x09,0x31,0x15,0x81,0x25,0x7f,0x75,8,0x95,2,0x81,6,0xc0,0xc0]
/** Turn the fake into a keyboard with a trackpad, whose battery level has no Report Reference. */
function makeCombo(g) {
g.add(22, 0x2a4b, [...g.attrs.get(22).value, ...mouseCollection])
g.add(34,0x2803,[0x12,35,0,0x4d,0x2a]); g.add(35,0x2a4d,[]); g.add(36,0x2902,[0,0]); g.add(37,0x2908,[4,1])
g.attrs.delete(44)
const sorted = [...g.attrs].sort((a, b) => a[0] - b[0])
g.attrs.clear(); for (const [handle, attr] of sorted) g.attrs.set(handle, attr)
}
for (const declarations of ['short', 'refused']) {
test(`client: a keyboard with a trackpad whose stack answers declaration reads ${declarations} types and points`, async () => {
const events = [], pointer = [], logs = []
let client
const g = fakeGlasses(payload => client.accept(payload))
makeCombo(g); g.declarations = declarations
client = new KeyboardClient(g.send, { keys: e => events.push(...e), pointer: p => pointer.push(p), log: line => logs.push(line) })
await client.enable(); await client.connect(1, new Uint8Array(6)); const config = await client.configure()
// Keys, media keys and the trackpad are subscribed; the battery level is not.
assert.deepEqual(config.watched, [27,31,35]); assert.equal(config.pointer, true)
assert.deepEqual(g.watches, [27,31,35,3]); assert.deepEqual(g.writes, [25,4,28,32,36])
assert.deepEqual(config.reports.map(r => [r.handle, r.id]), [[27,1],[31,2],[35,4]])
assert.deepEqual(config.reports.map(r => r.properties), declarations === 'short' ? [0x12,0x12,0x12] : [undefined,undefined,undefined])
g.input(27, [0,0,4,0,0,0,0,0])
assert.deepEqual(events.map(e => [e.usage,e.down]), [[4,true]])
// The trackpad: movement and buttons, and never a key.
g.input(35, [0, 5, 0xfd]); g.input(35, [1, 0, 0]); g.input(35, [5, 0x81, 0x7f])
assert.deepEqual(pointer, [
{ buttons: 0, dx: 5, dy: -3, wheel: 0, pan: 0 },
{ buttons: 1, dx: 0, dy: 0, wheel: 0, pan: 0 },
{ buttons: 5, dx: -127, dy: 127, wheel: 0, pan: 0 },
])
assert.equal(events.length, 1)
// A record lost on the way: the held buttons are let go along with the held keys.
g.seq++; g.status()
assert.deepEqual(pointer.at(-1), { buttons: 0, dx: 0, dy: 0, wheel: 0, pan: 0 })
assert.deepEqual([events.at(-1).usage, events.at(-1).down], [4, false])
// With nothing held, another gap has nothing to say.
const count = pointer.length; g.seq++; g.status()
assert.equal(pointer.length, count)
// What was skipped, and why, is in the log; so is the table it came from.
assert.ok(logs.some(line => /^gatt 1=2800 2=2803 /.test(line)), logs.join('\n'))
assert.ok(logs.some(line => /report 42 skipped: no Report Reference/.test(line)), logs.join('\n'))
assert.ok(logs.some(line => /report 35 \(id 4\) is a pointer's/.test(line)), logs.join('\n'))
assert.ok(logs.some(line => line.startsWith('report map 22: 0501')))
client.close()
})
}
test('client: one unusable report is skipped, and a device with no key report is refused with the reasons', async () => {
let client
const g = fakeGlasses(payload => client.accept(payload))
client = new KeyboardClient(g.send)
g.add(29, 0x2908, [1]) // a Report Reference of the wrong length
await client.enable(); await client.connect(1, new Uint8Array(6))
assert.deepEqual((await client.configure()).watched, [31])
g.add(33, 0x2908, [9,1]) // and one naming a report the map doesn't have
await assert.rejects(client.configure(), /No usable keyboard input report \(27: Report Reference 29 is 01; 31: input report id 9 is not in the Report Map\)/)
assert.equal(client.configuration, undefined)
// Not a HID device at all.
g.add(20, 0x2800, [0x0f,0x18])
await assert.rejects(client.configure(), /no HID service/)
client.close()
})
test('client: the link dropping during setup ends it, rather than being skipped over', async () => {
let client
const g = fakeGlasses(payload => client.accept(payload))
client = new KeyboardClient(g.send)
await client.enable(); await client.connect(1, new Uint8Array(6))
const send = g.send
g.send = (record) => {
// The keyboard goes away as the first Report Reference is read.
if (record[3] === 10 && u16(record, 8) === 29) { g.close(); return true }
return send(record)
}
client = new KeyboardClient(record => g.send(record))
await client.query()
await assert.rejects(client.configure(), /stream gap|link changed/)
client.close()
})
test('client: an unreachable keyboard fails the connect when the glasses give up, not by timeout', async () => {
let client
const g = fakeGlasses(payload => client.accept(payload)); g.reachable = false
client = new KeyboardClient(g.send)
await client.enable()
const pending = client.connect(1, new Uint8Array(6))
await settle(); g.close()
await assert.rejects(pending, /didn't answer/)
client.close()
})
test('client: firmware errors carry their code, and a dead link rejects at once', async () => {
let client
const g = fakeGlasses(payload => client.accept(payload))
client = new KeyboardClient(g.send)
await client.enable(); await client.connect(1, new Uint8Array(6))
await assert.rejects(client.command(4, [1,0,0,0,0,0,0]), e => e instanceof KeyboardClientError && e.code === 3)
g.up = false
await assert.rejects(client.query(), /aren't ready/)
client.close()
await assert.rejects(client.query(), /closed/)
})
function bridgeSetup(saved = null) {
const host = { keys: [], pointer: [], gone: 0, changes: 0, logs: [], prompts: [], saved, reconnect: true }
let bridge
const g = fakeGlasses(payload => bridge.accept(payload))
bridge = new KeyboardBridge({
send: g.send,
loadSaved: () => host.saved,
storeSaved: value => { host.saved = value },
onKeys: events => host.keys.push(...events),
onPointer: sample => host.pointer.push(sample),
onKeyboardGone: () => { host.gone++ },
onChanged: () => {
host.changes++
const pairing = bridge.snapshot().pairing
if (pairing && host.prompts.at(-1) !== pairing) host.prompts.push(pairing)
},
log: line => host.logs.push(line),
randomPasskey: () => 123456,
shouldReconnect: () => host.reconnect,
})
return { bridge, g, host }
}
const K380 = { address: 'aa:bb:cc:dd:ee:01', addressType: 1, name: 'K380' }
const advertOf = (lastByte, ad) => [1, lastByte,0xee,0xdd,0xcc,0xbb,0xaa, 0xc4, 0, ad.length, ...ad]
test('bridge: scan, pick, pair by typing the displayed passkey, type, forget', async () => {
const { bridge, g, host } = bridgeSetup()
assert.equal(bridge.snapshot().state, 'unavailable')
await assert.rejects(bridge.scan(), /Connect to G2 glasses/)
bridge.linkReady()
g.adverts = [
advertOf(2, [2,1,6]), // some other device
advertOf(1, [2,1,6, 3,3,0x12,0x18]), // the keyboard's advertisement
advertOf(1, [5,9,0x4b,0x33,0x38,0x30]), // and its scan response, with the name
]
const found = await bridge.scan()
assert.deepEqual(found.map(a => [a.address, a.name, a.hid]), [[K380.address,'K380',true],['aa:bb:cc:dd:ee:02','',false]])
assert.equal(bridge.snapshot().state, 'idle')
g.pairing = 'display'
await bridge.connect(K380)
// The passkey went up for the user to type, and to the glasses as the answer.
assert.deepEqual(host.prompts, [{ kind: 'display', pin: 123456 }])
assert.deepEqual(g.pins, [123456])
assert.equal(bridge.snapshot().state, 'connected'); assert.equal(bridge.snapshot().pairing, null)
assert.deepEqual(host.saved, K380); assert.ok(bridge.isConnected())
g.input(27, [2,0,4,0,0,0,0,0])
assert.deepEqual(host.keys.map(k => [k.usage, k.down]), [[0xe1,true],[4,true]])
await bridge.forget()
assert.equal(host.saved, null); assert.equal(host.gone, 1); assert.equal(g.conn, 0); assert.equal(g.enabled, 0)
assert.equal(bridge.snapshot().state, 'idle')
bridge.linkLost()
})
test('bridge: numeric comparison waits for the answer; declining fails the pairing', async () => {
const { bridge, g } = bridgeSetup()
bridge.linkReady(); g.pairing = 'compare'
const pending = bridge.connect(K380)
await settle()
assert.deepEqual(bridge.snapshot().pairing, { kind: 'compare', value: 123456 })
assert.equal(bridge.snapshot().state, 'pairing')
await bridge.answerCompare(false)
await assert.rejects(pending, /Pairing with the keyboard failed/)
assert.equal(bridge.snapshot().state, 'error'); assert.deepEqual(g.compares, [0])
await assert.rejects(bridge.answerCompare(true), /No pairing comparison/)
bridge.linkLost()
})
test('bridge: a keyboard that shows a code is paired with the code typed in', async () => {
const { bridge, g } = bridgeSetup()
bridge.linkReady(); g.pairing = 'entry'
const pending = bridge.connect(K380)
await settle()
assert.deepEqual(bridge.snapshot().pairing, { kind: 'entry' })
await bridge.answerPasskey(42)
await pending
assert.deepEqual(g.pins, [42]); assert.equal(bridge.snapshot().state, 'connected')
bridge.linkLost()
})
test('bridge: the remembered keyboard comes back with the link, and again after it drops', async (t) => {
t.mock.timers.enable({ apis: ['setTimeout'] })
const { bridge, g, host } = bridgeSetup(K380)
g.bonded = true
bridge.linkReady()
assert.equal(bridge.snapshot().waiting, true)
t.mock.timers.tick(1000); await settle()
assert.equal(bridge.snapshot().state, 'connected'); assert.equal(g.connected, 1)
// The keyboard goes to sleep: held keys are dropped, and it is looked for again.
g.input(27, [0,0,4,0,0,0,0,0])
g.close(); await settle()
assert.equal(host.gone, 1); assert.equal(bridge.snapshot().state, 'idle'); assert.equal(bridge.snapshot().waiting, true)
g.reachable = false
t.mock.timers.tick(2000); await settle()
assert.equal(bridge.snapshot().state, 'connecting')
g.close(); await settle()
// Asleep is the ordinary case, not an error in front of the user.
assert.equal(bridge.snapshot().state, 'idle'); assert.equal(bridge.snapshot().error, '')
g.reachable = true
t.mock.timers.tick(2000); await settle()
assert.equal(bridge.snapshot().state, 'connected')
// Disconnecting by hand stops the retries until asked again.
await bridge.disconnect()
assert.equal(bridge.snapshot().waiting, false); assert.equal(g.enabled, 0)
t.mock.timers.tick(60000); await settle()
assert.equal(bridge.snapshot().state, 'idle'); assert.equal(g.connected, 0)
bridge.linkLost()
})
test('bridge: the glasses kept the keyboard through a phone-side drop, so only discovery is redone', async (t) => {
t.mock.timers.enable({ apis: ['setTimeout'] })
const { bridge, g, host } = bridgeSetup(K380)
g.bonded = true
bridge.linkReady(); t.mock.timers.tick(1000); await settle()
assert.equal(bridge.snapshot().state, 'connected')
const epoch = g.epoch
bridge.linkLost()
assert.equal(bridge.snapshot().state, 'unavailable'); assert.equal(host.gone, 1)
g.ops.length = 0
bridge.linkReady(); t.mock.timers.tick(1000); await settle()
assert.equal(bridge.snapshot().state, 'connected')
assert.equal(g.epoch, epoch, 'no reconnect of the keyboard itself')
assert.ok(!g.ops.includes(4) && !g.ops.includes(2))
g.input(27, [0,0,7,0,0,0,0,0])
assert.deepEqual([host.keys.at(-1).usage, host.keys.at(-1).down], [7, true])
bridge.linkLost()
})
test('bridge: a scan started by hand takes over from an attempt in flight, and nothing retries while charging', async (t) => {
t.mock.timers.enable({ apis: ['setTimeout'] })
const { bridge, g, host } = bridgeSetup(K380)
g.reachable = false
bridge.linkReady(); t.mock.timers.tick(1000); await settle()
assert.equal(bridge.snapshot().state, 'connecting'); assert.equal(g.conn, 3)
g.adverts = [advertOf(1, [3,3,0x12,0x18])]
const scan = bridge.scan()
await settle(); t.mock.timers.tick(200); await settle()
assert.equal((await scan).length, 1)
assert.equal(g.conn, 0, 'the pending connection was released first')
assert.equal(bridge.snapshot().state, 'idle'); assert.equal(bridge.snapshot().busy, false)
// Reconnecting waits for the glasses to be in use.
host.reconnect = false
bridge.linkLost(); bridge.linkReady(); g.ops.length = 0
t.mock.timers.tick(1000); await settle()
t.mock.timers.tick(120000); await settle()
assert.deepEqual(g.ops, [])
// A bridge left enabled by an attempt is switched off for the wait, so the
// firmware's timer and its once-a-second status stop.
host.reconnect = true; g.reachable = false
bridge.retryNow(); t.mock.timers.tick(0); await settle()
assert.equal(g.enabled, 1); assert.equal(bridge.snapshot().state, 'connecting')
g.close(); await settle()
host.reconnect = false
t.mock.timers.tick(2000); await settle()
assert.equal(g.enabled, 0)
g.ops.length = 0
t.mock.timers.tick(120000); await settle()
assert.deepEqual(g.ops, [])
// The display woke: no waiting out the rest of the backoff.
host.reconnect = true; g.reachable = true; g.bonded = true
bridge.retryNow()
t.mock.timers.tick(0); await settle()
assert.equal(bridge.snapshot().state, 'connected')
// With the keyboard in use, or none remembered, there is nothing to retry.
g.ops.length = 0
bridge.retryNow(); t.mock.timers.tick(0); await settle()
assert.deepEqual(g.ops, [])
bridge.linkLost()
})
test('bridge: a keyboard\'s touchpad is passed on while it is in use, and the snapshot says it has one', async () => {
const plain = bridgeSetup()
plain.bridge.linkReady(); await plain.bridge.connect(K380)
assert.equal(plain.bridge.snapshot().pointer, false)
plain.bridge.linkLost()
const { bridge, g, host } = bridgeSetup()
makeCombo(g)
bridge.linkReady()
assert.equal(bridge.snapshot().pointer, false)
await bridge.connect(K380)
assert.equal(bridge.snapshot().pointer, true)
assert.ok(host.logs.some(line => /keyboard ready: K380, 3 of 3 input reports subscribed, with a pointer/.test(line)), host.logs.join('\n'))
g.input(35, [2, 1, 1])
assert.deepEqual(host.pointer, [{ buttons: 2, dx: 1, dy: 1, wheel: 0, pan: 0 }])
// The keyboard drops with a button down: the button is let go, as held keys
// are, before the host hears the keyboard is gone; and the snapshot follows.
g.close(); await settle()
assert.deepEqual(host.pointer.at(-1), { buttons: 0, dx: 0, dy: 0, wheel: 0, pan: 0 }); assert.equal(host.pointer.length, 2)
assert.equal(host.gone, 1); assert.equal(bridge.snapshot().pointer, false)
bridge.linkLost()
})
test('bridge: leaving on purpose tells the glasses to let the keyboard go', async (t) => {
t.mock.timers.enable({ apis: ['setTimeout'] })
const { bridge, g } = bridgeSetup(K380)
g.bonded = true
bridge.linkReady(); t.mock.timers.tick(1000); await settle()
assert.equal(g.connected, 1)
bridge.linkClosing(); bridge.linkLost()
assert.equal(g.enabled, 0); assert.equal(g.conn, 0)
// With no keyboard in use there is nothing to say.
const idle = bridgeSetup()
idle.bridge.linkReady(); idle.bridge.linkClosing(); idle.bridge.linkLost()
assert.deepEqual(idle.g.ops, [])
})
test('bridge: a keyboard forgotten while the glasses were away is released when they return', async (t) => {
t.mock.timers.enable({ apis: ['setTimeout'] })
const { bridge, g, host } = bridgeSetup(K380)
g.bonded = true
bridge.linkReady(); t.mock.timers.tick(1000); await settle()
assert.equal(g.connected, 1)
bridge.linkLost()
await bridge.forget()
assert.equal(host.saved, null); assert.equal(g.connected, 1, 'out of reach, so still attached')
bridge.linkReady(); await settle()
assert.equal(g.enabled, 0); assert.equal(g.conn, 0)
t.mock.timers.tick(60000); await settle()
assert.equal(bridge.snapshot().state, 'idle'); assert.equal(bridge.snapshot().waiting, false)
bridge.linkLost()
})
+165
View File
@@ -0,0 +1,165 @@
const test = require('node:test')
const assert = require('node:assert/strict')
const { KeyTranslator, translateUsage } = require('../.test-build/app/g2/keyboard/keys.js')
const { encodeTerminalKey, terminalScrollbackKey } = require('../.test-build/app/apps/terminal/terminal-keys.js')
const K = 7
const down = (usage, page = K) => ({ page, usage, down: true })
const up = (usage, page = K) => ({ page, usage, down: false })
const SHIFT = 225, RSHIFT = 229, CTRL = 224, ALT = 226, META = 227, CAPS = 57
const A = 4, ONE = 30, ENTER = 40, SLASH = 56
function translator() {
const events = [], timers = new Map()
let nextTimer = 1, now = 1000
const t = new KeyTranslator({
emit: e => events.push(e),
now: () => now,
setTimer: (callback, delayMs) => { timers.set(nextTimer, { callback, at: now + delayMs }); return nextTimer++ },
clearTimer: handle => timers.delete(handle),
})
const advance = ms => {
const end = now + ms
for (;;) {
const due = [...timers].filter(([, timer]) => timer.at <= end).sort((a, b) => a[1].at - b[1].at)[0]
if (!due) break
timers.delete(due[0]); now = due[1].at; due[1].callback()
}
now = end
}
return { t, events, timers, advance }
}
const key = (overrides = {}) => ({
phase: 'down', key: 'a', code: 'KeyA', text: 'a', repeat: false, timestampMs: 0,
...overrides, modifiers: { shift: false, ctrl: false, alt: false, meta: false, ...overrides.modifiers },
})
test('US layout: letters, digits, punctuation, keypad, function and consumer keys', () => {
assert.deepEqual(translateUsage(K, A, false, false), { key: 'a', code: 'KeyA', text: 'a' })
assert.deepEqual(translateUsage(K, A, true, false), { key: 'A', code: 'KeyA', text: 'A' })
assert.deepEqual(translateUsage(K, A, true, true), { key: 'a', code: 'KeyA', text: 'a' })
assert.deepEqual(translateUsage(K, ONE, true, true), { key: '!', code: 'Digit1', text: '!' })
assert.deepEqual(translateUsage(K, SLASH, true, false), { key: '?', code: 'Slash', text: '?' })
assert.deepEqual(translateUsage(K, 44, false, false), { key: ' ', code: 'Space', text: ' ' })
assert.deepEqual(translateUsage(K, 89, false, false), { key: '1', code: 'Numpad1', text: '1' })
assert.deepEqual(translateUsage(K, 88, false, false), { key: 'Enter', code: 'NumpadEnter' })
assert.deepEqual(translateUsage(K, 69, false, false), { key: 'F12', code: 'F12' })
assert.deepEqual(translateUsage(K, 104, false, false), { key: 'F13', code: 'F13' })
assert.deepEqual(translateUsage(12, 0xe9, false, false), { key: 'AudioVolumeUp', code: 'AudioVolumeUp' })
assert.equal(translateUsage(K, 200, false, false).key, 'Unidentified')
})
test('modifiers follow the stream, on either side, and chords carry no text', () => {
const { t, events } = translator()
t.handle([down(SHIFT), down(A)])
assert.deepEqual(events.map(e => [e.phase, e.key, e.text]), [['down', 'Shift', undefined], ['down', 'A', 'A']])
assert.equal(events[1].modifiers.shift, true)
// The other Shift takes over; releasing the first leaves it held.
t.handle([down(RSHIFT), up(SHIFT), up(A), down(A)])
assert.equal(events.at(-1).key, 'A')
t.handle([up(RSHIFT), up(A), down(CTRL), down(A)])
assert.deepEqual([events.at(-1).key, events.at(-1).text, events.at(-1).modifiers.ctrl], ['a', undefined, true])
t.handle([up(CTRL), up(A), down(ALT), down(A)])
assert.equal(events.at(-1).text, undefined)
t.handle([up(ALT), up(A), down(META), down(A)])
assert.equal(events.at(-1).text, undefined)
// A duplicate report changes nothing.
const count = events.length; t.handle([down(A)]); assert.equal(events.length, count)
t.handle([up(200)]); assert.equal(events.length, count)
})
test('Caps Lock toggles letters only', () => {
const { t, events } = translator()
t.handle([down(CAPS), up(CAPS), down(A), up(A), down(ONE), up(ONE), down(SHIFT), down(A)])
const typed = events.filter(e => e.phase === 'down' && e.text).map(e => e.text)
assert.deepEqual(typed, ['A', '1', 'a'])
})
test('a held key repeats until it is released; the newest key is the one that repeats', () => {
const { t, events, timers, advance } = translator()
t.handle([down(A)])
advance(399); assert.equal(events.length, 1)
advance(1); assert.deepEqual([events.at(-1).key, events.at(-1).repeat], ['a', true])
advance(100); assert.equal(events.filter(e => e.repeat).length, 3)
t.handle([down(ONE)]); advance(400)
assert.deepEqual([events.at(-1).key, events.at(-1).repeat], ['1', true])
// Releasing the older key leaves the newer one repeating.
t.handle([up(A)]); const before = events.length; advance(50)
assert.equal(events.length, before + 1)
t.handle([up(ONE)]); const after = events.length; advance(1000)
assert.equal(events.length, after); assert.equal(timers.size, 0)
// Modifiers never repeat.
t.handle([down(SHIFT)]); advance(2000); assert.equal(events.filter(e => e.key === 'Shift').length, 1)
})
test('reset forgets held keys and modifiers without emitting, and stops the repeat', () => {
const { t, events, timers, advance } = translator()
t.handle([down(SHIFT), down(CAPS), down(A)])
const count = events.length
t.reset(); advance(5000)
assert.equal(events.length, count); assert.equal(timers.size, 0)
t.handle([down(A)])
assert.deepEqual([events.at(-1).key, events.at(-1).modifiers.shift], ['a', false])
})
test('terminal: printable keys, Enter, Tab, Backspace, Escape', () => {
const enc = (overrides, appCursor = false) => encodeTerminalKey(key(overrides), appCursor)
assert.equal(enc({}), 'a')
assert.equal(enc({ key: 'A', text: 'A', modifiers: { shift: true } }), 'A')
assert.equal(enc({ key: 'Enter', text: undefined }), '\r')
assert.equal(enc({ key: 'Tab', text: undefined }), '\t')
assert.equal(enc({ key: 'Tab', text: undefined, modifiers: { shift: true } }), '\x1b[Z')
assert.equal(enc({ key: 'Backspace', text: undefined }), '\x7f')
assert.equal(enc({ key: 'Backspace', text: undefined, modifiers: { ctrl: true } }), '\x08')
assert.equal(enc({ key: 'Escape', text: undefined }), '\x1b')
assert.equal(enc({ key: ' ', text: ' ' }), ' ')
assert.equal(enc({ phase: 'up' }), null)
assert.equal(enc({ key: 'Shift', code: 'ShiftLeft', text: undefined, modifiers: { shift: true } }), null)
assert.equal(enc({ key: 'AudioVolumeUp', text: undefined }), null)
assert.equal(enc({ modifiers: { meta: true }, text: undefined }), null)
// A repeat types again.
assert.equal(enc({ repeat: true }), 'a')
})
test('terminal: Ctrl chords are control characters, Alt prefixes ESC', () => {
const enc = (k, modifiers) => encodeTerminalKey(key({ key: k, text: undefined, modifiers }), false)
assert.equal(enc('c', { ctrl: true }), '\x03'); assert.equal(enc('C', { ctrl: true, shift: true }), '\x03')
assert.equal(enc('z', { ctrl: true }), '\x1a'); assert.equal(enc('[', { ctrl: true }), '\x1b')
assert.equal(enc(' ', { ctrl: true }), '\x00'); assert.equal(enc('/', { ctrl: true }), '\x7f')
assert.equal(enc('=', { ctrl: true }), null)
assert.equal(enc('f', { alt: true }), '\x1bf'); assert.equal(enc('F', { alt: true, shift: true }), '\x1bF')
assert.equal(enc('c', { ctrl: true, alt: true }), '\x1b\x03')
assert.equal(enc('Enter', { alt: true }), '\x1b\r'); assert.equal(enc('Backspace', { alt: true }), '\x1b\x7f')
})
test('terminal: cursor keys follow DECCKM; modified ones and function keys use the xterm forms', () => {
const enc = (k, modifiers, appCursor = false) => encodeTerminalKey(key({ key: k, text: undefined, modifiers }), appCursor)
assert.equal(enc('ArrowUp'), '\x1b[A'); assert.equal(enc('ArrowUp', {}, true), '\x1bOA')
assert.equal(enc('ArrowLeft', { ctrl: true }), '\x1b[1;5D'); assert.equal(enc('ArrowLeft', { ctrl: true }, true), '\x1b[1;5D')
assert.equal(enc('ArrowRight', { shift: true, alt: true }), '\x1b[1;4C')
assert.equal(enc('Home'), '\x1b[H'); assert.equal(enc('End', {}, true), '\x1bOF')
assert.equal(enc('PageUp'), '\x1b[5~'); assert.equal(enc('Delete'), '\x1b[3~'); assert.equal(enc('Delete', { ctrl: true }), '\x1b[3;5~')
assert.equal(enc('F1'), '\x1bOP'); assert.equal(enc('F4', { shift: true }), '\x1b[1;2S')
assert.equal(enc('F5'), '\x1b[15~'); assert.equal(enc('F12'), '\x1b[24~'); assert.equal(enc('F13'), null)
})
test('terminal: Shift+PageUp and Shift+PageDown page the scrollback instead', () => {
const page = (k, modifiers, phase = 'down') => terminalScrollbackKey(key({ key: k, text: undefined, modifiers, phase }))
assert.equal(page('PageUp', { shift: true }), -1); assert.equal(page('PageDown', { shift: true }), 1)
assert.equal(page('PageUp', {}), null); assert.equal(page('PageUp', { shift: true, ctrl: true }), null)
assert.equal(page('PageUp', { shift: true }, 'up'), null); assert.equal(page('ArrowUp', { shift: true }), null)
})
test('mouse buttons are keys that neither repeat nor take the repeat from a held key; a wheel detent is a press and release', () => {
const { t, events, advance } = translator()
t.handle([down(A)])
t.handle([{ page: 9, usage: 1, down: true }])
assert.deepEqual([events.at(-1).key, events.at(-1).code, events.at(-1).text], ['MouseButton1', 'MouseButton1', undefined])
// The held key goes on repeating; the held button never does.
advance(500)
assert.ok(events.filter(e => e.repeat).every(e => e.key === 'a')); assert.ok(events.some(e => e.repeat))
t.handle([up(A), { page: 9, usage: 1, down: false }])
assert.deepEqual([events.at(-1).phase, events.at(-1).key], ['up', 'MouseButton1'])
assert.equal(translateUsage(9, 3, false, false).code, 'MouseButton3')
// The wheel: down and up at once, marked as droppable, with the keyboard's modifiers.
events.length = 0
t.handle([down(SHIFT)]); t.pulse('WheelUp')
assert.deepEqual(events.slice(1).map(e => [e.phase, e.key, e.code, e.burst, e.repeat, e.modifiers.shift]),
[['down', 'WheelUp', 'WheelUp', true, false, true], ['up', 'WheelUp', 'WheelUp', true, false, true]])
const count = events.length; advance(2000)
assert.equal(events.length, count)
})
+58
View File
@@ -0,0 +1,58 @@
const test = require('node:test')
const assert = require('node:assert/strict')
const { PointerInput } = require('../.test-build/app/g2/keyboard/pointer.js')
const sample = (buttons, extra = {}) => ({ buttons, dx: 0, dy: 0, wheel: 0, pan: 0, ...extra })
function pointer() {
const out = []
const input = new PointerInput({
buttons: events => out.push(...events.map(e => `${e.down ? 'down' : 'up'}:${e.page}:${e.usage}`)),
wheel: key => out.push(key),
})
return { input, out }
}
test('buttons become key transitions; the wheel a key per report; movement nothing', () => {
const { input, out } = pointer()
input.handle(sample(0, { dx: 40, dy: -3 }))
assert.deepEqual(out, [])
input.handle(sample(0b001)); input.handle(sample(0b001, { dx: 2 })); input.handle(sample(0b110)); input.handle(sample(0))
assert.deepEqual(out, ['down:9:1', 'up:9:1', 'down:9:2', 'down:9:3', 'up:9:2', 'up:9:3'])
out.length = 0
// However many detents a report claims, it is one step; a two-finger scroll sends many.
input.handle(sample(0, { wheel: 1 })); input.handle(sample(0, { wheel: -3 })); input.handle(sample(0, { pan: 2 })); input.handle(sample(0, { pan: -1 }))
assert.deepEqual(out, ['WheelUp', 'WheelDown', 'WheelRight', 'WheelLeft'])
})
test('taking the pointer: samples go to the taker, held buttons are let go first, and its clicks are nobody else\'s', () => {
const { input, out } = pointer()
const taken = []
input.handle(sample(0b001))
const release = input.take(s => taken.push(s))
assert.deepEqual(out, ['down:9:1', 'up:9:1'])
out.length = 0
input.handle(sample(0b001, { dx: 5 })); input.handle(sample(0b011, { wheel: 1 })); input.handle(sample(0b010))
assert.deepEqual(taken, [sample(0b001, { dx: 5 }), sample(0b011, { wheel: 1 }), sample(0b010)])
assert.deepEqual(out, [])
// Given back with the right button still down: its release is not a key-up for anyone.
release()
input.handle(sample(0)); input.handle(sample(0b100)); input.handle(sample(0))
assert.deepEqual(out, ['down:9:3', 'up:9:3'])
// A stale release function can't take the pointer away from a later taker.
const second = []
const releaseSecond = input.take(s => second.push(s))
release()
input.handle(sample(0, { dx: 1 }))
assert.equal(second.length, 1)
releaseSecond()
})
test('the keyboard going away forgets held buttons without reporting releases', () => {
const { input, out } = pointer()
input.handle(sample(0b001))
input.reset()
assert.deepEqual(out, ['down:9:1'])
// The next keyboard starts clean: a press is a press.
input.handle(sample(0b001))
assert.deepEqual(out, ['down:9:1', 'down:9:1'])
})
@@ -0,0 +1,63 @@
package com.faceclaw.app
import java.io.File
import kotlin.math.PI
import kotlin.math.sin
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
class MicRecordingFileTest {
/**
* A capture recorded through the real file adapters and read back. Set FACECLAW_MKA_SAMPLE
* to a path to keep the file, for checking it against other Matroska tools (ffprobe, mpv).
*/
@Test
fun recordsACaptureToAFileThatReadsBack() {
val kept = System.getenv("FACECLAW_MKA_SAMPLE")
val file = if (kept.isNullOrEmpty()) File.createTempFile("capture", ".mka") else File(kept)
try {
val writer = MicRecordingWriter(openFileOutput(file.path), "{\"mics\":4,\"app\":\"test\"}", 1_700_000_000_000)
val mixer = SingleTempleMixer()
// Five seconds: each temple streams a stereo pair of tones on its own tick.
for (index in 0 until 100) {
for (side in 0..1) {
val pcm = ShortArray(1600) {
val frame = index * 800 + it / 2
val hz = if (side == 0) 440.0 else 660.0
(sin(2 * PI * hz * frame / 16000) * (if (it % 2 == 0) 8000 else 4000)).toInt().toShort()
}
val chunk = MicChunk(if (side == 0) MicStream.LEFT else MicStream.RIGHT, 2, 16000, pcm, 800, 10_000 + index * 50L + side * 7)
chunk.sequence = index
chunk.deviceTickMs = (if (side == 0) 500_000L else 123_456L) + index * 50L
chunk.hasDirection = true
chunk.angleDegrees = if (side == 0) -30 else 25
chunk.ssr = 12
writer.write(chunk)
mixer.mix(chunk)?.let { writer.write(it) }
}
}
// The right temple's chunks arrive 7 ms behind the left's.
assertEquals(5007L, writer.finish())
val reader = MicRecordingReader(openFileInput(file.path))
assertEquals(5007L, reader.durationMs)
assertEquals("left temple 2ch + right temple 2ch", reader.description)
assertEquals("{\"mics\":4,\"app\":\"test\"}", reader.configJson)
var left = 0
var right = 0
while (true) {
val chunk = reader.next() ?: break
if (chunk.stream == MicStream.LEFT) left++ else right++
assertEquals(12, chunk.ssr)
}
assertEquals(100, left)
assertEquals(100, right)
reader.close()
// Audio (three tracks) plus the frame metadata.
assertTrue(file.length() > 100 * 2 * 3200 + 100 * 1600)
} finally {
if (kept.isNullOrEmpty()) file.delete()
}
}
}
@@ -347,9 +347,9 @@ class DisplayListTest {
val gray = ByteArray(640 * 480) { 48 }; val app = BmpUtil.pack4bppFromGray8(gray, 640, 480)
fun layer(key: Int, y: Int, h: Int, nibble: Int, depth: Int = 0) =
ShellScene.Layer(key, 14, y, 612, h, 256, ByteArray(306 * h) { (nibble * 17).toByte() }, depth = depth)
// Each 612x214 layer is just under the 64 KiB resource cap; three exceed the arena on their own.
// Each 612x200 layer takes 60 KiB of the 320 KiB arena: five fit and six exceed it on their own.
fun chrome(depth: Int) = layer(1, 444, 36, 7, depth)
val modals = listOf(layer(2, 10, 214, 9), layer(3, 120, 214, 11), layer(4, 230, 214, 13, depth = 4))
val modals = (0..5).map { layer(2 + it, 10 + 50 * it, 200, 8 + it, depth = if (it == 5) 4 else 0) }
val overflow = ShellScene(listOf(chrome(6)) + modals)
val plan = planner.plan(app, 640, 480, null, overflow, 1)
assertEquals(2, plan.bakedLayers); assertFalse(plan.bakedSelections)
@@ -363,7 +363,7 @@ class DisplayListTest {
assertContentEquals(BmpUtil.pack4bppFromGray8(expected.preview(gray, 640, 480, glasses.right), 640, 480), glasses.composition)
}
// Once the shell fits again, the next frame is fully live and the screen is the app's again.
val fits = ShellScene(listOf(chrome(0), modals[2]))
val fits = ShellScene(listOf(chrome(0), modals.last()))
val next = planner.plan(app, 640, 480, null, fits, plan.nextFid)
assertEquals(0, next.bakedLayers)
for (glasses in lenses) {
@@ -372,13 +372,13 @@ class DisplayListTest {
assertContentEquals(BmpUtil.pack4bppFromGray8(fits.preview(gray, 640, 480, glasses.right), 640, 480), glasses.composition)
}
}
@Test fun noisyFramesRemainBoundedAndResourceBudgetIs192KiB() {
@Test fun noisyFramesRemainBoundedAndResourceBudgetIs322KiB() {
val cache=ResourceCacheState();val planner=ScenePlanner(cache);val glasses=Glasses()
val app=ByteArray(640*480/2){(it*37).toByte()}
val plan=planner.plan(app,640,480,null,ShellScene.EMPTY,1)
assertTrue(plan.commands.all { it.size<=65535 });glasses.apply(plan.commands)
assertContentEquals(app,glasses.screen);assertContentEquals(app,glasses.composition)
assertEquals(196608,ResourceCacheState.CACHE_SIZE)
assertEquals(2048+320*1024,ResourceCacheState.CACHE_SIZE)
}
/** Revision 35. Same calls and expectations as ClipTest in g2flash/tests/test_display_list.py. */
@Test fun clipRectsNarrowEveryOpNestThroughListsAndMoveWithDepth() {
@@ -1130,7 +1130,8 @@ class G1FeaturesTest {
}
s.core.setImuReportEnabled(true, 200)
assertTrue(awaitTrue(3_000) { lock.withLock { readings.size >= 5 } }, "samples: " + lock.withLock { readings.size } + "\n" + s.tail())
assertEquals(listOf(0.1, -1.0, 0.25, BleProtocol.EVENT_SOURCE_GLASSES_R.toDouble()), lock.withLock { readings.first() })
// The G1's (x, y, z) arrive in the G2's frame as (-z, -x, y) (ImuOrientation).
assertEquals(listOf(-0.25, -0.1, -1.0, BleProtocol.EVENT_SOURCE_GLASSES_R.toDouble()), lock.withLock { readings.first() })
assertTrue(awaitTrue(1_000) { s.glasses.lock.withLock { s.glasses.rightArm.props[G1Protocol.PROP_IMU_RATE] == G1Protocol.IMU_RATE_STREAM } })
s.core.setImuReportEnabled(false, 0)
assertTrue(awaitTrue(1_000) { s.glasses.lock.withLock { s.glasses.rightArm.props[G1Protocol.PROP_IMU_RATE] == G1Protocol.IMU_RATE_IDLE } })
@@ -316,6 +316,197 @@ private class Session {
}
class GlassesSessionCoreTest {
@Test fun headUpUnsetPreferencesKeepUpstreamRightArmPolicyAndRestore() {
for (preferences in listOf<Pair<Int, Int>?>(null, -1 to -1)) {
val s = Session()
try {
s.core.monitor.withLock {
s.core.running = true
s.core.sessionReady = true
s.core.headUpPreferences = preferences
s.core.headUpSwitch = 0
s.core.headUpAngle = 10
s.core.headUpCalibration = 25
s.core.enqueueHeadUpPreferencesLocked()
assertTrue(s.core.pendingMessages.isEmpty(), "unset preferences leave firmware alone")
s.core.setHeadUpTrigger(20, -1, 0)
val writes = s.core.pendingMessages.filter { it.kind == "head-up-control" }
assertEquals(2, writes.size)
assertTrue(writes.none { it.isLeftArmMessage })
assertContentEquals(BleProtocol.buildSetHeadUpWake(writes[0].magic, true), writes[0].message,
"unset preferences preserve upstream switch-before-angle ordering")
assertContentEquals(BleProtocol.buildSetHeadUp(writes[1].magic, BleProtocol.HEAD_UP_FIELD_ANGLE, -5), writes[1].message)
s.core.setHeadUpTrigger(0, -1, 0)
assertTrue(s.core.pendingMessages.isEmpty())
assertFalse(s.core.headUpRestorePending)
}
} finally { s.core.close() }
}
}
@Test fun headUpPartialPreferencesRestoreOnlyUnspecifiedFieldsFromOriginal() {
val s = Session()
try {
s.core.monitor.withLock {
s.core.running = true
s.core.sessionReady = true
s.core.headUpSwitch = 0
s.core.headUpAngle = 10
s.core.headUpCalibration = 5
s.core.configureHeadUpWake(-1, 30)
s.core.setHeadUpTrigger(20, 0, 10)
assertEquals(4, s.core.pendingMessages.count { it.kind == "head-up-control" })
s.core.headUpSwitch = 1
s.core.headUpAngle = 15
s.core.setHeadUpTrigger(0, 0, 10)
val writes = s.core.pendingMessages.filter { it.kind == "head-up-control" }
assertEquals(4, writes.size)
assertTrue(writes.filter { it.label?.contains("angle=") == true }.all { it.label == "head-up angle=30" })
assertTrue(writes.filter { it.label?.contains("wake=") == true }.all { it.label == "head-up wake=false" })
}
} finally { s.core.close() }
}
@Test fun headUpQueueCancellationAndTimeoutReapplyCompletePolicy() {
val s = Session()
try {
s.core.monitor.withLock {
s.core.sessionReady = true
// Matching right-arm settings cannot prove the left arm is set.
s.core.headUpSwitch = 0
s.core.headUpAngle = 30
s.core.configureHeadUpWake(0, 30)
s.core.enqueueHeadUpPreferencesLocked()
assertEquals(4, s.core.pendingMessages.size)
s.core.clearPendingMessagesLocked("test cancellation")
s.core.enqueueHeadUpPreferencesLocked()
assertEquals(4, s.core.pendingMessages.size)
val timeout = s.core.pendingMessages.first().onTimeout!!
while (s.core.pendingMessages.isNotEmpty()) {
val m = s.core.pendingMessages.removeFirst()
s.core.magicPool.release(m.sid, m.magic, m.label, "test ack")
}
timeout.run()
s.core.enqueueHeadUpPreferencesLocked()
assertEquals(4, s.core.pendingMessages.size)
}
} finally { s.core.close() }
}
@Test fun headUpQueuedOffSurvivesGlanceboardQueueReplacement() {
val s = Session()
try {
s.core.monitor.withLock {
s.core.running = true
s.core.sessionReady = true
s.core.headUpSwitch = 1
s.core.configureHeadUpWake(0, 30)
s.core.enqueueHeadUpPreferencesLocked()
s.core.setHeadUpTrigger(20, -1, 0)
assertEquals(4, s.core.pendingMessages.count { it.kind == "head-up-control" },
"clearing the old batch must invalidate its optimistic sent cache")
assertTrue(s.core.pendingMessages.none { it.label == "head-up wake=true" })
}
} finally { s.core.close() }
}
@Test fun headUpPreferenceChangeWaitsForInflightThenReplacesBothArms() {
val s = Session()
try {
s.core.monitor.withLock {
s.core.sessionReady = true
s.core.headUpSwitch = 1
s.core.configureHeadUpWake(1, 30)
s.core.enqueueHeadUpPreferencesLocked()
val inflight = s.core.pendingMessages.removeFirst()
s.core.inFlightMessages.addLast(inflight)
s.core.configureHeadUpWake(0, 40)
assertTrue(s.core.pendingMessages.none { it.kind == "head-up-control" },
"unsent old-policy writes must be cancelled")
s.core.enqueueHeadUpPreferencesLocked()
assertTrue(s.core.pendingMessages.isEmpty(), "do not race an old in-flight write")
s.core.inFlightMessages.remove(inflight)
s.core.magicPool.release(inflight.sid, inflight.magic, inflight.label, "test ack")
s.core.enqueueHeadUpPreferencesLocked()
assertEquals(4, s.core.pendingMessages.count { it.kind == "head-up-control" })
assertTrue(s.core.pendingMessages.filter { it.label?.contains("wake=") == true }
.all { it.label == "head-up wake=false" })
}
} finally { s.core.close() }
}
@Test fun headUpGlanceboardOverrideRestoresForkBaselineNotStaleOriginal() {
val s = Session()
try {
s.core.monitor.withLock {
s.core.running = true
s.core.sessionReady = true
s.core.headUpSwitch = 1
s.core.headUpAngle = 30
s.core.headUpCalibration = 25
s.core.configureHeadUpWake(1, 30)
s.core.setHeadUpTrigger(20, 0, 10)
var writes = s.core.pendingMessages.filter { it.kind == "head-up-control" }
assertEquals(4, writes.size)
assertEquals(setOf(true, false), writes.map { it.isLeftArmMessage }.toSet())
assertTrue(writes.filter { it.label?.contains("angle=") == true }.all {
it.message.contentEquals(BleProtocol.buildSetHeadUp(it.magic, BleProtocol.HEAD_UP_FIELD_ANGLE, -5))
}, "Glanceboard offsets stay signed, not fork-clamped")
s.core.setHeadUpTrigger(0, 0, 10)
writes = s.core.pendingMessages.filter { it.kind == "head-up-control" }
assertEquals(4, writes.size)
assertTrue(writes.filter { it.label?.contains("angle=") == true }.all { it.label == "head-up angle=30" })
assertTrue(writes.filter { it.label?.contains("wake=") == true }.all { it.label == "head-up wake=true" })
}
} finally { s.core.close() }
}
@Test fun headUpRefusedSettingRetriesOnRegularPollNotSenderLoop() {
val s = Session()
try {
s.core.monitor.withLock {
s.core.sessionReady = true
s.core.headUpSwitch = 1
s.core.configureHeadUpWake(0, 30)
s.core.enqueueHeadUpPreferencesLocked()
while (s.core.pendingMessages.isNotEmpty()) {
val m = s.core.pendingMessages.removeFirst()
s.core.magicPool.release(m.sid, m.magic, m.label, "test ack")
}
val read = BleProtocol.encodeBytesField(4,
BleProtocol.encodeVarintField(7, 1) + BleProtocol.encodeVarintField(8, 10)) + byteArrayOf(0, 0)
s.core.reconcileHeadUpLocked(read)
s.core.enqueueHeadUpPreferencesLocked()
assertTrue(s.core.pendingMessages.isEmpty(), "verifying disagreement must not spin")
s.core.reconcileHeadUpLocked(read)
assertEquals(4, s.core.pendingMessages.count { it.kind == "head-up-control" },
"next regular poll retries a refused batch")
}
} finally { s.core.close() }
}
@Test fun explicitHeadUpOffWinsOverGlanceboardOnBothArms() {
val s = Session()
try {
s.core.monitor.withLock {
s.core.sessionReady = true
s.core.headUpSwitch = 1
s.core.headUpAngle = 10
s.core.headUpCalibration = 5
s.core.configureHeadUpWake(0, 30)
s.core.desiredHeadUpTrigger = 20
s.core.applyHeadUpLocked(false)
val writes = s.core.pendingMessages.filter { it.kind == "head-up-control" }
assertEquals(4, writes.size, "one effective policy must reach both arms")
for (left in listOf(true, false)) {
val arm = writes.filter { it.isLeftArmMessage == left }
assertContentEquals(BleProtocol.buildSetHeadUpAngle(arm[0].magic, 30), arm[0].message)
assertContentEquals(BleProtocol.buildSetHeadUpWake(arm[1].magic, false), arm[1].message)
}
}
} finally { s.core.close() }
}
@Test fun headUpEnqueueIsG2OnlyAndPreservesPreferencesForOtherModels() {
for (model in listOf(GlassesType.G2, GlassesType.G1, GlassesType.RAYNEO_IO)) {
val s = Session()
@@ -900,4 +1091,62 @@ class GlassesSessionCoreTest {
s.core.close()
}
}
@Test
fun keyboardControlsGoToTheRightArmAndKeyboardRecordsReachTheirListenerOnly() {
val s = Session()
val records = ArrayList<ByteArray>()
val lock = s.platform.createLock()
val listener = object : FaceclawKeyboardEventListener {
override fun onKeyboardEvent(body: ByteArray?) { lock.withLock { records.add(body!!) } }
}
s.core.addKeyboardEventListener(listener)
// ['K','B',1,QUERY, request 7, epoch 0]
val control = byteArrayOf(75, 66, 1, 0, 7, 0, 0, 0)
try {
assertFalse(s.core.sendFaceclawKeyboardControl(control, "query"), "refused before the session is up")
s.startAndAwaitLayout()
assertFalse(s.core.sendFaceclawKeyboardControl(null, "none"))
assertFalse(s.core.sendFaceclawKeyboardControl(byteArrayOf(75, 66, 1), "short"))
assertTrue(s.core.sendFaceclawKeyboardControl(control, "query"))
fun controls() = s.link.writes.filter { it.sid == BleProtocol.SID_UI_SETTING && it.pb.containsSequence(control) }
assertTrue(waitUntil(5_000) { controls().isNotEmpty() })
// One copy, down the right arm: a second one would run the control twice.
assertEquals(listOf(RIGHT), controls().map { it.address })
assertContentEquals(
BleProtocol.encodeVarintField(1, 1) + BleProtocol.encodeVarintField(2, 0) +
BleProtocol.encodeBytesField(BleProtocol.FACECLAW_KEYBOARD_CONTROL_FIELD, control),
controls().single().pb,
)
// A field-131 record: ['K','B',1,STATUS, seq 1, epoch 0, request 7, ...] with a 10-byte body.
val record = byteArrayOf(75, 66, 1, 1, 1, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 10, 0, 0, 0, 1, 2, 3, 4) + ByteArray(10)
fun push(address: String, body: ByteArray, flag: Int) = s.core.onNotification(address, BleProtocol.NOTIFY_CHAR_UUID,
BleProtocol.framePb(
BleProtocol.encodeVarintField(1, 3) + BleProtocol.encodeVarintField(2, 0) +
BleProtocol.encodeBytesField(BleProtocol.FACECLAW_KEYBOARD_EVENT_FIELD, body),
BleProtocol.SID_UI_SETTING, flag, 33)[0])
val eventsBefore = s.listener.events.size
val activityBefore = s.core.monitor.withLock { s.core.lastConnectionOrInputAtMs }
s.platform.offsetMs += 5_000
push(RIGHT, record, BleProtocol.FLAG_NOTIFY)
// Whichever temple hosts the keyboard reports it, and the flag doesn't matter.
push(LEFT, record, BleProtocol.FLAG_REQUEST)
assertEquals(2, lock.withLock { records.size })
assertContentEquals(record, lock.withLock { records[0] })
// Not glasses input, not user activity, and not an ACK for anything.
assertEquals(eventsBefore, s.listener.events.size)
assertEquals(activityBefore, s.core.monitor.withLock { s.core.lastConnectionOrInputAtMs })
assertFalse(s.host.hasLog("unexpected ack"), s.host.logs().takeLast(5).toString())
// Too short to hold the fixed header, or not a keyboard record: left to the normal path.
push(RIGHT, record.copyOf(23), BleProtocol.FLAG_NOTIFY)
push(RIGHT, byteArrayOf(77, 67) + ByteArray(30), BleProtocol.FLAG_NOTIFY)
assertEquals(2, lock.withLock { records.size })
s.core.removeKeyboardEventListener(listener)
push(RIGHT, record, BleProtocol.FLAG_NOTIFY)
assertEquals(2, lock.withLock { records.size })
} finally {
s.core.close()
}
}
}
@@ -2,6 +2,9 @@ package com.faceclaw.app
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue
class HeadUpProtocolTest {
private fun bytes(hex: String) = hex.chunked(2).map { it.toInt(16).toByte() }.toByteArray()
@@ -14,4 +17,46 @@ class HeadUpProtocolTest {
assertContentEquals(bytes("080110071a0422021000"), BleProtocol.buildSetHeadUpAngle(7, -10))
assertContentEquals(bytes("080110071a042202103c"), BleProtocol.buildSetHeadUpAngle(7, 100))
}
@Test fun generalizedAngleKeepsSignedOffsetsAndDoesNotUseForkClamp() {
assertContentEquals(bytes("080110071a08220610f6ffffff0f"),
BleProtocol.buildSetHeadUp(7, BleProtocol.HEAD_UP_FIELD_ANGLE, -10))
assertContentEquals(bytes("080110071a042202105a"),
BleProtocol.buildSetHeadUp(7, BleProtocol.HEAD_UP_FIELD_ANGLE, 90))
// The generic API must also retain fields other than switch/angle.
assertContentEquals(bytes("080110071a0422021801"), BleProtocol.buildSetHeadUp(7, 3, 1))
}
@Test fun buildersPreserveGeneralizedValuesAndForkArmTargeting() {
val builder = MessageBuilder(BleMagicPool(testPlatform()))
val generic = builder.setHeadUp(BleProtocol.HEAD_UP_FIELD_ANGLE, -10)
assertContentEquals(BleProtocol.buildSetHeadUp(generic.magic, BleProtocol.HEAD_UP_FIELD_ANGLE, -10), generic.message)
assertFalse(generic.isLeftArmMessage)
assertEquals("head-up field 2=-10", generic.label)
for (leftArm in listOf(true, false)) {
val angle = builder.headUpControl(leftArm, null, 100)
assertEquals(leftArm, angle.isLeftArmMessage)
assertEquals("head-up angle=100", angle.label)
assertContentEquals(BleProtocol.buildSetHeadUpAngle(angle.magic, 100), angle.message)
val wake = builder.headUpControl(leftArm, false, -1)
assertEquals(leftArm, wake.isLeftArmMessage)
assertEquals("head-up wake=false", wake.label)
assertContentEquals(BleProtocol.buildSetHeadUpWake(wake.magic, false), wake.message)
for (message in listOf(generic, angle, wake)) {
assertEquals("head-up-control", message.kind)
assertEquals(BleProtocol.SID_UI_SETTING, message.sid)
assertEquals(BleProtocol.FLAG_REQUEST, message.flag)
assertEquals(3_500, message.ackTimeoutMs)
assertEquals(-1, message.tileIndex)
}
assertTrue(angle.magic != wake.magic)
}
}
@Test fun settingsReadKeepsSignedAngleAndCalibration() {
// parseSettingsHeadUp accepts a payload with its trailing CRC still attached.
val reply = bytes("08021007220e380140f6ffffff0f4882ffffff0f0000")
assertContentEquals(intArrayOf(1, -10, -126), BleProtocol.parseSettingsHeadUp(reply))
assertContentEquals(intArrayOf(0, 0, 0), BleProtocol.parseSettingsHeadUp(bytes("22000000")))
}
}
@@ -0,0 +1,25 @@
package com.faceclaw.app
import kotlin.math.abs
import kotlin.math.sqrt
import kotlin.test.*
class ImuOrientationTest {
private val s = sqrt(0.5)
private fun assertVec(expected: DoubleArray, actual: DoubleArray, what: String) {
for (i in 0..2) assertTrue(abs(expected[i] - actual[i]) < 1e-9, "$what: expected ${expected.toList()}, got ${actual.toList()}")
}
/** The poses measured on hardware (level, 45° up, 45° to the right) read the G2's values on every model. */
@Test fun measuredPosesMatchTheG2Frame() {
val level = doubleArrayOf(0.0, 0.0, 1.0)
val up = doubleArrayOf(s, 0.0, s)
val right = doubleArrayOf(0.0, s, s)
val g1 = listOf(doubleArrayOf(0.0, 1.0, 0.0), doubleArrayOf(0.0, s, -s), doubleArrayOf(-s, s, 0.0))
for ((name, canonical, raw) in listOf(Triple("level", level, g1[0]), Triple("up", up, g1[1]), Triple("right", right, g1[2]))) {
assertVec(canonical, ImuOrientation.toCanonical(GlassesType.G1, raw[0], raw[1], raw[2]), "G1 $name")
assertVec(canonical, ImuOrientation.toCanonical(GlassesType.G2, canonical[0], canonical[1], canonical[2]), "G2 $name")
}
}
}
@@ -0,0 +1,572 @@
package com.faceclaw.app
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertFalse
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.assertTrue
/** A file held in memory: written through [SeekableOutput], read back through [input]. */
internal class MemoryFile : SeekableOutput {
var bytes = ByteArray(0)
var closed = false
override val size: Long get() = bytes.size.toLong()
override fun write(bytes: ByteArray, offset: Int, length: Int) {
this.bytes += bytes.copyOfRange(offset, offset + length)
}
override fun writeAt(position: Long, bytes: ByteArray) {
bytes.copyInto(this.bytes, position.toInt())
}
override fun close() {
closed = true
}
/** The file as it stands, or its first [length] bytes. */
fun input(length: Int = bytes.size): SeekableInput {
val snapshot = bytes.copyOf(length)
return object : SeekableInput {
override val length = snapshot.size.toLong()
override fun readAt(position: Long, buffer: ByteArray, offset: Int, length: Int): Int {
val count = minOf(length.toLong(), maxOf(0L, snapshot.size - position)).toInt()
snapshot.copyInto(buffer, offset, position.toInt(), position.toInt() + count)
return count
}
override fun close() {}
}
}
}
private fun pcmBytes(frames: Int, channels: Int, value: Int): ByteArray =
AudioSegmentation.pcm16ToLittleEndian(ShortArray(frames * channels) { value.toShort() }, 0, frames * channels)
private fun chunk(stream: Int, timeMs: Long, value: Int, channels: Int = 1, frames: Int = 800): MicChunk =
MicChunk(stream, channels, 16000, ShortArray(frames * channels) { (value + it % channels).toShort() }, frames, timeMs)
private fun drain(reader: MicRecordingReader): List<MicChunk> {
val out = ArrayList<MicChunk>()
while (true) out.add(reader.next() ?: break)
return out
}
class MatroskaTest {
@Test
fun roundTripsTracksBlocksAndAttachments() {
val file = MemoryFile()
val writer = MkaWriter(file, "session", 1_700_000_000_000, listOf(MkaAttachment("capture.json", "application/json", "{\"a\":1}".encodeToByteArray())))
val temple = writer.addAudioTrack("left temple", 16000, 2, false)
val text = writer.addTextTrack("frames")
for (index in 0 until 30) {
writer.writeText(text, index * 50L, 50, "{\"n\":$index}")
writer.writeAudio(temple, index * 50L, pcmBytes(800, 2, index), 0, 3200)
}
// A track that first appears mid-file, and is the one players should pick.
val mix = writer.addAudioTrack("mix", 16000, 1, true)
for (index in 30 until 60) {
writer.writeAudio(temple, index * 50L, pcmBytes(800, 2, index), 0, 3200)
writer.writeAudio(mix, index * 50L, pcmBytes(800, 1, -index), 0, 1600)
}
assertEquals(3000L, writer.durationMs)
writer.finish()
assertTrue(file.closed)
val reader = MkaReader(file.input())
// Listed with the preferred audio track first, then the others, then text; numbers are as added.
assertEquals(listOf("mix", "left temple", "frames"), reader.tracks.map { it.name })
assertEquals(listOf(mix, temple, text), reader.tracks.map { it.number })
val templeInfo = reader.track(temple)!!
assertTrue(templeInfo.isPcm16)
assertEquals(2, templeInfo.channels)
assertEquals(16000, templeInfo.sampleRate)
assertFalse(reader.track(text)!!.isPcm16)
assertEquals("{\"a\":1}", reader.attachments.single().data.decodeToString())
assertEquals("application/json", reader.attachments.single().mimeType)
assertEquals(3000L, reader.durationMs)
val blocks = ArrayList<MkaBlock>()
while (true) blocks.add(reader.next() ?: break)
assertEquals(30 + 60 + 30, blocks.size)
assertEquals(text, blocks[0].track)
assertEquals("{\"n\":0}", blocks[0].data.decodeToString())
assertEquals(50L, blocks[0].durationMs)
val templeBlocks = blocks.filter { it.track == temple }
assertEquals((0 until 60).map { it * 50L }, templeBlocks.map { it.timeMs })
assertEquals(pcmBytes(800, 2, 59).toList(), templeBlocks.last().data.toList())
assertEquals(1500L, blocks.first { it.track == mix }.timeMs)
reader.seekTo(2000)
val afterSeek = ArrayList<MkaBlock>()
while (true) afterSeek.add(reader.next() ?: break)
assertEquals(2000L, afterSeek.first().timeMs)
assertEquals(40, afterSeek.size)
reader.seekTo(99_000)
assertNull(reader.next())
}
@Test
fun recordingCutShortIsReadableUpToItsLastCompleteBlock() {
val file = MemoryFile()
val writer = MkaWriter(file, null, 0, emptyList())
val track = writer.addAudioTrack("mic", 16000, 1, true)
// 2.5 s: clusters of a second each reach the file as they fill; the last half-second is still buffered.
for (index in 0 until 50) writer.writeAudio(track, index * 50L, pcmBytes(800, 1, index), 0, 1600)
val whole = MkaReader(file.input())
assertEquals("mic", whole.tracks.single().name)
var count = 0
while (whole.next() != null) count++
assertEquals(40, count)
assertEquals(2000L, whole.durationMs)
// A crash mid-write tears the last cluster: its complete blocks still read.
val torn = MkaReader(file.input(file.bytes.size - 1000))
count = 0
while (torn.next() != null) count++
assertEquals(39, count)
assertEquals(1950L, torn.durationMs)
}
@Test
fun rejectsOtherFiles() {
val wav = MemoryFile()
wav.write(BinaryEncoding.wavHeader(0, 16000, 1), 0, 44)
assertFailsWith<IllegalArgumentException> { MkaReader(wav.input()) }
assertFailsWith<IllegalArgumentException> { MkaReader(MemoryFile().input()) }
}
@Test
fun paddedRegionsFillTheirReservedSpaceWithAVoidElement() {
assertNull(EbmlWriter.padded(ByteArray(10), 9))
assertNull(EbmlWriter.padded(ByteArray(10), 11))
assertEquals(listOf(1, 2, 0xEC, 0x80), EbmlWriter.padded(byteArrayOf(1, 2), 4)!!.map { it.toInt() and 0xff })
val long = EbmlWriter.padded(ByteArray(0), 300)!!
assertEquals(300, long.size)
// An eight-byte size field: 300 - 9 = 0x123.
assertEquals(listOf(0xEC, 0x01, 0, 0, 0, 0, 0, 0x01, 0x23), long.take(9).map { it.toInt() and 0xff })
}
}
class MicRecordingTest {
private val warnings = mutableListOf<String>()
private fun recorder(file: MemoryFile, config: String? = "{\"mics\":4}") =
MicRecordingWriter(file, config, 1_700_000_000_000, testPlatform()) { warnings.add(it) }
@Test
fun recordsEveryStreamWithItsMetadataAndReplaysTheSources() {
val file = MemoryFile()
val writer = recorder(file)
for (index in 0 until 4) {
val left = chunk(MicStream.LEFT, 5000 + index * 50L, 100 + index, channels = 2)
left.sequence = 7 + index
left.deviceTickMs = 900_000 + index * 50L
left.hasDirection = true
left.angleDegrees = -12
left.ssr = 40
left.truncated = index == 3
writer.write(left)
val feed = SingleTempleMixer().mix(left)!!
writer.write(feed)
val right = chunk(MicStream.RIGHT, 5010 + index * 50L, 200 + index, channels = 2)
right.sequence = 3 + index
right.deviceTickMs = 40_000 + index * 50L
writer.write(right)
}
// The capture went back to the mixed stream part-way through.
writer.event("{\"event\":\"capture\",\"mics\":1}")
val mono = chunk(MicStream.MONO, 5200, 55)
mono.hasDirection = true
mono.angleDegrees = 30
mono.ssr = 9
writer.write(mono)
assertEquals(250L, writer.finish())
assertFalse(writer.failed)
assertTrue(warnings.isEmpty(), warnings.toString())
val mka = MkaReader(file.input())
// What a player picks by default is a mixed track.
assertEquals(listOf("mix", "mic", "left temple", "right temple", "frames"), mka.tracks.map { it.name })
val reader = MicRecordingReader(file.input())
assertEquals("{\"mics\":4}", reader.configJson)
assertEquals("mic + left temple 2ch + right temple 2ch", reader.description)
assertEquals(250L, reader.durationMs)
val chunks = drain(reader)
// The mix is derived again on a replay, so only the sources come back.
assertEquals(4, chunks.count { it.stream == MicStream.LEFT })
assertEquals(4, chunks.count { it.stream == MicStream.RIGHT })
assertEquals(1, chunks.count { it.stream == MicStream.MONO })
val left = chunks.filter { it.stream == MicStream.LEFT }
assertEquals(listOf(0L, 50L, 100L, 150L), left.map { it.timeMs })
assertEquals(listOf(7, 8, 9, 10), left.map { it.sequence })
assertEquals(900_050L, left[1].deviceTickMs)
assertEquals(2, left[0].channels)
assertEquals(listOf<Short>(100, 101, 100, 101), left[0].pcm.take(4))
assertTrue(left.all { it.hasDirection && it.angleDegrees == -12 && it.ssr == 40 })
assertEquals(listOf(false, false, false, true), left.map { it.truncated })
val right = chunks.filter { it.stream == MicStream.RIGHT }
assertEquals(listOf(10L, 60L, 110L, 160L), right.map { it.timeMs })
assertFalse(right[0].hasDirection)
val replayedMono = chunks.single { it.stream == MicStream.MONO }
assertEquals(200L, replayedMono.timeMs)
assertEquals(30, replayedMono.angleDegrees)
assertEquals(800, replayedMono.frames)
reader.seekTo(100)
assertEquals(100L, reader.next()!!.timeMs)
}
@Test
fun chunksHeldUpInDeliveryLandWhereTheyWereCaptured() {
val file = MemoryFile()
val writer = recorder(file, null)
// A temple's ticks say when each chunk was captured. The first ten arrive as captured;
// the link then stalls for a second and delivers the next twenty at once.
for (index in 0 until 30) {
val arrival = if (index < 10) 1000 + index * 50L else maxOf(2500L, 1000 + index * 50L)
val chunk = chunk(MicStream.LEFT, arrival, index)
chunk.deviceTickMs = 77_000 + index * 50L
writer.write(chunk)
}
// Two chunks were lost on the way (their ticks are skipped): a gap, not a splice.
val late = chunk(MicStream.LEFT, 2650, 99)
late.deviceTickMs = 77_000 + 32 * 50L
writer.write(late)
writer.finish()
val times = drain(MicRecordingReader(file.input())).map { it.timeMs }
assertEquals((0 until 30).map { it * 50L } + 1600L, times)
assertNull(MicRecordingReader(file.input()).configJson)
}
@Test
fun aSourceWithoutAClockIsPlacedBySampleCount() {
val file = MemoryFile()
val writer = recorder(file)
for (index in 0 until 10) {
// Arrivals jitter around the capture cadence; the stream's samples are contiguous.
val jitter = if (index % 3 == 1) 30L else 0L
writer.write(chunk(MicStream.MONO, 400 + index * 50L + jitter, index))
}
// The decoder reported one packet missing before this chunk.
val afterLoss = chunk(MicStream.MONO, 400 + 11 * 50L, 11)
afterLoss.lostFramesBefore = 800
writer.write(afterLoss)
// A capture that resumes much later is placed by its arrival, not spliced on.
writer.write(chunk(MicStream.MONO, 60_000, 12))
writer.finish()
val chunks = drain(MicRecordingReader(file.input()))
assertEquals((0 until 10).map { it * 50L } + 550L + 59_600L, chunks.map { it.timeMs })
assertEquals(800, chunks[10].lostFramesBefore)
}
@Test
fun aForeignFilePlaysItsFirstTrackAsTheMicrophone() {
val file = MemoryFile()
val mka = MkaWriter(file, null, 0, emptyList())
val track = mka.addAudioTrack("Stereo", 16000, 2, false)
// One long block of stereo frames [100, 300].
val samples = ShortArray(2000 * 2) { if (it % 2 == 0) 100 else 300 }
mka.writeAudio(track, 0, AudioSegmentation.pcm16ToLittleEndian(samples, 0, samples.size), 0, samples.size * 2)
mka.finish()
val reader = MicRecordingReader(file.input())
assertEquals("mic 2ch", reader.description)
val chunks = drain(reader)
assertEquals(listOf(800, 800, 400), chunks.map { it.frames })
assertEquals(listOf(0L, 50L, 100L), chunks.map { it.timeMs })
assertTrue(chunks.all { it.stream == MicStream.MONO && it.channels == 1 && it.pcm.all { sample -> sample == 200.toShort() } })
val fast = MemoryFile()
val other = MkaWriter(fast, null, 0, emptyList())
other.writeAudio(other.addAudioTrack("mic", 48000, 1, true), 0, pcmBytes(480, 1, 1), 0, 960)
other.finish()
val error = assertFailsWith<IllegalArgumentException> { MicRecordingReader(fast.input()) }
assertTrue(error.message!!.contains("48000 Hz"), error.message)
}
@Test
fun theFeedFollowsTheLeftTempleAndFallsBackToTheRight() {
val mixer = SingleTempleMixer()
// Until the left temple is heard from, the right one feeds.
assertNotNull(mixer.mix(chunk(MicStream.RIGHT, 0, 10, channels = 2)))
val left = chunk(MicStream.LEFT, 20, 100, channels = 2)
left.deviceTickMs = 5
val feed = mixer.mix(left)!!
assertEquals(MicStream.MIX, feed.stream)
assertEquals(1, feed.channels)
assertEquals(800, feed.frames)
// Channels [100, 101] average to 100.
assertTrue(feed.pcm.all { it == 100.toShort() })
assertEquals(MicStream.LEFT, feed.clockStream)
assertEquals(5L, feed.deviceTickMs)
assertNull(mixer.mix(chunk(MicStream.RIGHT, 60, 10)))
assertNull(mixer.mix(chunk(MicStream.RIGHT, 300, 10)))
// The left temple went quiet: the right stands in until it is back.
assertNotNull(mixer.mix(chunk(MicStream.RIGHT, 320, 10)))
assertNotNull(mixer.mix(chunk(MicStream.LEFT, 330, 100)))
assertNull(mixer.mix(chunk(MicStream.RIGHT, 340, 10)))
assertNull(mixer.mix(chunk(MicStream.MONO, 350, 10)))
}
@Test
fun decodesTheTemplesStreamFrames() {
val frame = micStreamFrame(sequence = 0x1234, tick = 0x89ABCDEFL, channels = 2, samples = shortArrayOf(1, -2, 3, -4), angle = -45, ssr = 300, truncated = true)
val decoded = MicArrayProtocol.decodeFrame(frame)!!
assertEquals(0x1234, decoded.sequence)
assertEquals(0x89ABCDEFL, decoded.deviceTickMs)
assertEquals(2, decoded.channels)
assertEquals(16000, decoded.sampleRate)
assertEquals(-45, decoded.angleDegrees)
assertEquals(300, decoded.ssr)
assertTrue(decoded.truncated)
assertEquals(listOf<Short>(1, -2, 3, -4), MicArrayProtocol.pcm16(frame, decoded)!!.toList())
assertNull(MicArrayProtocol.decodeFrame(ByteArray(205)))
// A payload length beyond the packet is cut to what arrived.
val short = frame.copyOf(frame.size - 4)
assertEquals(listOf<Short>(1, -2), MicArrayProtocol.pcm16(short, MicArrayProtocol.decodeFrame(short)!!)!!.toList())
assertEquals(listOf(0x4d, 0x43, 1, 4), MicArrayProtocol.control(MicArrayProtocol.OP_RENEW).map { it.toInt() })
assertTrue(MicArrayProtocol.isConfigure(CONFIGURE_RECORD))
assertFalse(MicArrayProtocol.isConfigure(MicArrayProtocol.control(MicArrayProtocol.OP_STOP)))
}
}
/** A mic_control CONFIGURE record: codec front end, both mics, raw PCM16 at 16 kHz, hardware armed. */
internal val CONFIGURE_RECORD = byteArrayOf(0x4d, 0x43, 1, 1, 0, 3, 1, 0, 160.toByte(), 0, 0, 0, 2)
/** An 'SM' stream frame as a temple sends it. */
internal fun micStreamFrame(
sequence: Int,
tick: Long,
channels: Int,
samples: ShortArray,
angle: Int = 0,
ssr: Int = 0,
truncated: Boolean = false,
): ByteArray {
val payload = AudioSegmentation.pcm16ToLittleEndian(samples, 0, samples.size)
val header = ByteArray(MicArrayProtocol.HEADER_BYTES)
fun le16(at: Int, value: Int) {
header[at] = value.toByte()
header[at + 1] = (value shr 8).toByte()
}
header[0] = 'S'.code.toByte()
header[1] = 'M'.code.toByte()
header[2] = 1
header[3] = (if (truncated) 0x82 else 0x02).toByte()
le16(4, sequence)
le16(6, (tick and 0xffff).toInt())
le16(8, ((tick shr 16) and 0xffff).toInt())
header[10] = channels.toByte()
le16(11, 160)
header[13] = 0
header[14] = 1
le16(15, angle)
le16(17, ssr)
le16(19, payload.size)
return header + payload
}
class VoiceCaptureSourcesTest {
private class Events : FaceclawVoiceControllerListener {
val statuses = mutableListOf<String?>()
val transcripts = mutableListOf<Pair<String?, Boolean>>()
val pcmChunks = mutableListOf<ByteArray>()
val replay = mutableListOf<Pair<String?, Long>>()
val stoppedLatch = Latch(1, testPlatform())
override fun onStatus(status: String?) { statuses.add(status) }
override fun onTranscript(text: String?, isFinal: Boolean) { transcripts.add(text to isFinal) }
override fun onStopped(captureId: Int) { stoppedLatch.countDown() }
override fun onPcm(pcm16le: ByteArray?) { pcmChunks.add(pcm16le!!) }
override fun onFrameMeta(angleDegrees: Int, ssr: Int) {}
override fun onSpeechEnd() {}
override fun onSpeakerVerified(isWearer: Boolean, similarity: Float) {}
override fun onReplay(event: String?, positionMs: Long, durationMs: Long) { replay.add(event to positionMs) }
}
private class FakeLink : MicArrayLink {
var listener: FaceclawAudioPacketListener? = null
val controls = mutableListOf<String>()
override fun startForwarding(listener: FaceclawAudioPacketListener): Boolean { this.listener = listener; return true }
override fun stopForwarding() { listener = null }
override fun isForwarding(): Boolean = listener != null
override fun sendControl(record: ByteArray, label: String, rightTemple: Boolean, leftTemple: Boolean): Boolean {
controls.add(label + ":" + record[3] + (if (leftTemple) "L" else "") + (if (rightTemple) "R" else ""))
return true
}
}
private class FakeHost(val link: FakeLink? = null, val recording: MemoryFile? = null) : VoiceCaptureHost {
override val dispatcher = CallbackDispatcher { it() }
override fun onSessionStarting() {}
override fun isG2SessionReady(): Boolean = link != null
override fun isG2AudioCaptureActive(): Boolean = link?.isForwarding() == true
override fun g2AudioSource(): PacketAudioSource? = null
override fun createPacketDecoder(): AudioPacketDecoder = error("no stock stream in these tests")
override fun openPhoneMic(): PcmAudioSource? = null
override fun hasTranscriberModel(kind: VoiceModelKind): Boolean = true
override fun loadTranscriber(kind: VoiceModelKind): OfflineTranscriber = object : OfflineTranscriber {
override fun recognize(samples: FloatArray, count: Int): String = "x"
override fun release() {}
}
override fun speakerEmbedder(modelPath: String): SpeakerEmbedder = error("no verification in these tests")
override fun micArrayLink(): MicArrayLink? = link
override fun openRecording(path: String): SeekableInput = if (path == "missing") error("no such file") else recording!!.input()
}
private val logs = mutableListOf<String>()
private fun session(host: VoiceCaptureHost, name: String) =
VoiceCaptureSession(host, testPlatform(), name, { logs.add("I $it") }, { logs.add("W $it") }, { m, e -> logs.add("E $m ${e.message}") })
private fun waitFor(timeoutMs: Long, condition: () -> Boolean): Boolean {
val deadline = testPlatform().elapsedRealtimeMs() + timeoutMs
while (!condition()) {
if (testPlatform().elapsedRealtimeMs() > deadline) return false
sleepMs(5)
}
return true
}
private fun monoRecording(chunks: Int): MemoryFile {
val file = MemoryFile()
val writer = MicRecordingWriter(file, null, 0, testPlatform())
for (index in 0 until chunks) writer.write(chunk(MicStream.MONO, index * 50L, 1000 + index))
writer.finish()
return file
}
@Test
fun micArrayCaptureArmsTheTemplesRecordsThemAndTranscribesTheFeed() {
val link = FakeLink()
val host = FakeHost(link)
val events = Events()
val session = session(host, "voice-test-array")
val file = MemoryFile()
val recorder = MicRecordingWriter(file, "{}", 0, testPlatform())
session.setListener(events)
session.setMicArray(CONFIGURE_RECORD)
session.setRecorder(recorder)
session.start("cloud", 1)
assertTrue(waitFor(3000) { link.controls.isNotEmpty() })
assertTrue(session.isCapturing())
val listener = link.listener!!
for (index in 0 until 5) {
val left = ShortArray(1600) { if (it % 2 == 0) 100 else 300 }
listener.onAudioPacket(micStreamFrame(index, 1000 + index * 50L, 2, left, angle = 20, ssr = 7), "L", 500 + index * 50L)
listener.onAudioPacket(micStreamFrame(index, 9000 + index * 50L, 2, ShortArray(1600) { 50 }), "R", 505 + index * 50L)
}
// A stock-format packet from before arming took effect is not audio.
listener.onAudioPacket(ByteArray(205), "L", 760)
assertTrue(waitFor(3000) { events.pcmChunks.size >= 5 })
session.stop()
assertTrue(events.stoppedLatch.await(3000))
assertEquals(listOf("configure:1LR"), link.controls.take(1))
assertEquals("stop:3LR", link.controls.last())
assertFalse(link.isForwarding())
assertFalse(session.isCapturing())
// The feed is the left temple's microphones averaged.
assertEquals(5, events.pcmChunks.size)
assertEquals(1600, events.pcmChunks[0].size)
assertTrue(AudioSegmentation.pcm16leToFloat(events.pcmChunks[0]).all { it == 200 / 32768f })
assertTrue(events.statuses.contains("Listening (cloud)..."))
recorder.finish()
assertEquals(listOf("mix", "left temple", "right temple", "frames"), MkaReader(file.input()).tracks.map { it.name })
val recorded = drain(MicRecordingReader(file.input()))
assertEquals(5, recorded.count { it.stream == MicStream.LEFT })
assertEquals(5, recorded.count { it.stream == MicStream.RIGHT })
val left = recorded.first { it.stream == MicStream.LEFT }
assertEquals(2, left.channels)
assertEquals(1000L, left.deviceTickMs)
assertEquals(20, left.angleDegrees)
}
@Test
fun micArrayCaptureNeedsGlassesThatHaveTheChannel() {
val events = Events()
val session = session(FakeHost(), "voice-test-array-none")
session.setListener(events)
session.setMicArray(CONFIGURE_RECORD)
session.start("cloud", 1)
assertEquals<List<String?>>(listOf("Voice control needs an active G2 connection."), events.statuses)
// A record that is not a CONFIGURE leaves the capture on the mixed stream.
val mixed = Events()
session.setListener(mixed)
session.setMicArray(MicArrayProtocol.control(MicArrayProtocol.OP_STOP))
session.setUsePhoneMic(true)
session.start("cloud", 2)
assertTrue(mixed.stoppedLatch.await(3000))
assertEquals<List<String?>>(listOf("Could not start the phone microphone."), mixed.statuses)
}
@Test
fun replayPlaysARecordingThroughThePipelineInRealTime() {
val host = FakeHost(recording = monoRecording(30))
val events = Events()
val session = session(host, "voice-test-replay")
session.setListener(events)
// A replay is never recorded again.
val copy = MemoryFile()
val recorder = MicRecordingWriter(copy, null, 0, testPlatform())
session.setRecorder(recorder)
session.setReplay("recording.mka", 1000)
val startedAt = testPlatform().elapsedRealtimeMs()
session.start("onboard", 5)
assertTrue(events.stoppedLatch.await(5000))
val elapsed = testPlatform().elapsedRealtimeMs() - startedAt
// Half a second of audio, paced as it was captured.
assertTrue(elapsed >= 400, "replay took $elapsed ms")
assertEquals(10, events.pcmChunks.size)
assertEquals(1020.toShort(), AudioSegmentation.pcm16le(events.pcmChunks[0][0], events.pcmChunks[0][1]))
assertEquals("start" to 1000L, events.replay.first())
assertEquals("end" to 1500L, events.replay.last())
assertEquals(VoiceCaptureSession.REPLAY_FINISHED_STATUS, events.statuses.last())
assertEquals("x" to true, events.transcripts.last())
assertFalse(session.isCapturing())
recorder.finish()
assertTrue(MkaReader(copy.input()).tracks.isEmpty())
}
@Test
fun replayJumpsAndPauses() {
val host = FakeHost(recording = monoRecording(200))
val events = Events()
val session = session(host, "voice-test-replay-seek")
session.setListener(events)
session.setReplay("recording.mka", 0)
session.start("onboard", 6)
assertTrue(waitFor(3000) { events.pcmChunks.size >= 2 })
session.setReplayPaused(true)
sleepMs(100)
val heldAt = events.pcmChunks.size
sleepMs(200)
assertEquals(heldAt, events.pcmChunks.size)
assertTrue(session.isCapturing())
// Jump to the last quarter-second while paused, then go on.
session.seekReplay(9750)
session.setReplayPaused(false)
assertTrue(events.stoppedLatch.await(5000))
assertTrue(events.replay.contains("seek" to 9750L), events.replay.toString())
assertEquals("end" to 10_000L, events.replay.last())
assertEquals(heldAt + 5, events.pcmChunks.size)
// What played before the jump ended as its own utterance.
assertEquals(2, events.transcripts.count { it.second })
val last = events.pcmChunks.last()
assertEquals(1199.toShort(), AudioSegmentation.pcm16le(last[0], last[1]))
}
@Test
fun replayOfAnUnreadableFileReportsAndStops() {
val events = Events()
val session = session(FakeHost(), "voice-test-replay-missing")
session.setListener(events)
session.setReplay("missing", 0)
session.start("cloud", 7)
assertTrue(events.stoppedLatch.await(3000))
assertEquals<List<String?>>(listOf("Could not open the recording: no such file"), events.statuses)
assertEquals(listOf<Pair<String?, Long>>("error" to 0L), events.replay)
}
}
@@ -64,10 +64,12 @@ class ResourceCacheTest {
@Test fun lruEvictsByBytesAndProtectsAllCurrentFrameHits() {
val cache = ResourceCacheState()
// Five of these fill the 320 KiB arena; a sixth needs an eviction.
val a = resource(60000, 1); val b = resource(60000, 2); val c = resource(60000, 3); val d = resource(60000, 4)
assertContentEquals(intArrayOf(0, 1, 2), cache.prepare(listOf(a, b, c)))
val e = resource(60000, 5); val f = resource(60000, 6)
assertContentEquals(intArrayOf(0, 1, 2, 3, 4), cache.prepare(listOf(a, b, c, e, f)))
cache.drainCommands(3600)
cache.prepare(listOf(a)); cache.drainCommands(3600) // b is oldest now.
cache.prepare(listOf(a, e, f)); cache.drainCommands(3600) // b is oldest now.
assertContentEquals(intArrayOf(1, 2), cache.prepare(listOf(d, c))) // protect later hit c before d.
val commands = cache.drainCommands(3600)
assertContentEquals(byteArrayOf(22, 1, 0, 1, 0), commands.first())
@@ -75,10 +77,10 @@ class ResourceCacheTest {
assertEquals(0, cache.resourceId(a)); assertEquals(2, cache.resourceId(c))
assertEquals(1, cache.resourceId(d))
assertContentEquals(d.bytes, uploaded(commands).getValue(1))
val tooLarge = cache.prepare(listOf(a, c, d, b))
val tooLarge = cache.prepare(listOf(a, c, d, e, f, b))
assertEquals(-1, tooLarge.last())
assertTrue(cache.drainCommands(3600).isEmpty())
assertEquals(3, cache.residentCount())
assertEquals(5, cache.residentCount())
}
@Test fun idPressureUsesLruEvenWhenBytesAreAvailable() {
@@ -96,15 +98,15 @@ class ResourceCacheTest {
@Test fun rejectedPlansRestoreResidencyAndLru() {
val cache = ResourceCacheState()
val resources = (0..3).map { resource(65536, it) }
cache.prepare(resources.take(3)); cache.drainCommands(3600)
val resources = (0..5).map { resource(60000, it) } // five fit, the sixth evicts
assertContentEquals(intArrayOf(0, 1, 2, 3, 4), cache.prepare(resources.take(5))); cache.drainCommands(3600)
val rollback = cache.checkpoint()
cache.prepare(listOf(resources[3]))
cache.prepare(listOf(resources[5]))
assertEquals(-1, cache.resourceId(resources[0]))
rollback()
assertEquals(0, cache.resourceId(resources[0])); assertEquals(-1, cache.resourceId(resources[3]))
assertEquals(0, cache.resourceId(resources[0])); assertEquals(-1, cache.resourceId(resources[5]))
assertTrue(cache.drainCommands(3600).isEmpty())
cache.prepare(listOf(resources[3]))
cache.prepare(listOf(resources[5]))
assertContentEquals(byteArrayOf(22, 1, 0, 0, 0), cache.drainCommands(3600).first())
}
@@ -29,6 +29,8 @@ class VoiceCaptureSessionTest {
override fun onFrameMeta(angleDegrees: Int, ssr: Int) { frameMeta.add(angleDegrees to ssr) }
override fun onSpeechEnd() { speechEnds++ }
override fun onSpeakerVerified(isWearer: Boolean, similarity: Float) { verified = isWearer to similarity }
val replay = mutableListOf<Pair<String?, Long>>()
override fun onReplay(event: String?, positionMs: Long, durationMs: Long) { replay.add(event to positionMs) }
}
/** Scripted phone mic: each entry is a chunk amplitude; after the script the read fails (-1). */
@@ -0,0 +1,46 @@
package com.faceclaw.app
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.assertTrue
import platform.Foundation.NSTemporaryDirectory
import platform.Foundation.NSUUID
class IosMicRecorderTest {
@Test
fun recordsPcmToAMatroskaFileThroughThePosixFileAdapters() {
val path = NSTemporaryDirectory() + NSUUID().UUIDString + ".mka"
try {
val recorder = assertNotNull(IosMicRecordings.open(path, "{\"platform\":\"ios\"}"))
// 2.5 s in 50 ms chunks: enough for whole clusters to reach the file before finish().
for (index in 0 until 50) {
val samples = ShortArray(800) { (index * 10 + it % 7).toShort() }
recorder.append(AudioSegmentation.pcm16ToLittleEndian(samples, 0, samples.size).data())
}
assertFalse(recorder.failed)
val length = recorder.finish()
assertTrue(length >= 2500, "recorded $length ms")
assertEquals(fileLength(path), openFileInput(path).length)
val reader = MicRecordingReader(openFileInput(path))
assertEquals("{\"platform\":\"ios\"}", reader.configJson)
assertEquals("mic", reader.description)
var chunks = 0
var last: MicChunk? = null
while (true) {
last = reader.next() ?: break
assertEquals(800, last.frames)
assertEquals((chunks * 10).toShort(), last.pcm[0])
chunks++
}
assertEquals(50, chunks)
reader.close()
} finally {
deleteFile(path)
}
assertNull(IosMicRecordings.open("/nonexistent-directory/x.mka", null))
}
}
+26 -1
View File
@@ -17,7 +17,7 @@ const font={lineHeight:12,ascent:10,descent:2,measureText:text=>text.length*4,
const menuCore=load('app/ui/menu-core.ts',{
'../graphics/image':graphics,'./menu-label': require('../.test-build/app/ui/menu-label.js'), './menu-highlight-motion': require('../.test-build/app/ui/menu-highlight-motion.js'), '../graphics/menu-scroll-list': require('../.test-build/app/graphics/menu-scroll-list.js'), './menu-scroll-motion': require('../.test-build/app/ui/menu-scroll-motion.js'), '../graphics/draw-expression': require('../.test-build/app/graphics/draw-expression.js'),
});
const {MenuLayer,TextPageLayer}=load('app/ui/menu.ts',{
const {MenuLayer,TextPageLayer,menuHeadingItem}=load('app/ui/menu.ts',{
'./menu-animation-pref': require('../.test-build/app/ui/menu-animation-pref.js'), './animation-speed': require('../.test-build/app/ui/animation-speed.js'), '../native/settings-store': { getStringSetting: (_key, fallback) => fallback }, '../graphics/screen-size': { screenWidth: () => 640, screenHeight: () => 480 },
'../graphics/image':graphics,'../graphics/textwrap':{wrapText:(_font,text)=>text.split('\n')},'../graphics/ui-fonts':{getDefaultSmallFont:()=>font},
'../util/numeric-util':{clamp:(n,a,b)=>Math.max(a,Math.min(b,n))},'./gestures':{},
@@ -81,6 +81,31 @@ test('a submenu that replaces its parent menu skips the parent plane and dims th
stack.pop();stack.pop();
assert.equal(stack.paint().length,2);
});
test('heading rows label a section and are never selected',async()=>{
const picked=[];
const row=label=>({label,onSelect(){picked.push(label);}});
const layer=new MenuLayer(null,[menuHeadingItem('First'),row('A'),row('B'),menuHeadingItem('Second'),row('C')],
{x:0,y:0,width:120,minHeight:150,maxHeight:150,squareCorners:true,showBorder:false});
const context={stack:{getBaseSize:()=>({width:120,height:150}),isFocused:()=>true}};
const paint=()=>layer.paint(context,()=>new graphics.GrayImage(120,150));
const move=async type=>{await layer.handleInput({type},context);paint();return layer.selectedIndex;};
// The highlight starts below the first heading and steps over the second, both ways and when wrapping.
paint();assert.equal(layer.selectedIndex,1);
assert.equal(await move('scroll-down'),2);
assert.equal(await move('scroll-down'),4);
assert.equal(await move('scroll-down'),1);
assert.equal(await move('scroll-up'),4);
assert.equal(await move('scroll-up'),2);
await layer.handleInput({type:'click'},context);assert.deepEqual(picked,['B']);
// A heading asked for by index lands on the row under it.
layer.selectItem(3);assert.equal(layer.selectedIndex,4);
// Headings draw dim, in capitals (4px per character in this font), in the rows' text column.
const image=paint().withDrawsBaked(false),inkAt=y=>{const xs=[];for(let x=0;x<120;x++) if(image.pixels[y*120+x]>=8) xs.push([x,image.pixels[y*120+x]]);return xs;};
const heading=inkAt(8+4+2),unselected=inkAt(8+20+4+2);
assert.equal(heading[0][0],unselected[0][0]);
assert.ok(heading.every(([,value])=>value===90));assert.ok(unselected.every(([,value])=>value===200));
assert.equal(heading.length,'FIRST'.length*2);
});
test('text page paints at the stack size, not the lens size',()=>{
// An app window's stack is its viewport; a lens-sized plane is rejected by
// the compositor as larger than the window surface.
+136
View File
@@ -0,0 +1,136 @@
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');
function load(file, modules, globals = {}) {
const source = fs.readFileSync(path.join(__dirname, '..', file), 'utf8');
const context = { exports: {}, console, ...globals, require(name) { assert.ok(name in modules, name); return modules[name]; } };
vm.runInNewContext(ts.transpileModule(source, {
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 },
}).outputText, context);
return context.exports;
}
const graphics = require('../.test-build/app/graphics/image.js');
const { bordered } = require('./helpers/viewport.cjs');
const { BdfFont } = load('app/graphics/bdffont.ts', { '@nativescript/core': {} });
const font = BdfFont.parse(fs.readFileSync(path.join(__dirname, '../app/fonts/terminus/ter-u12n.bdf'), 'utf8'));
/** The page over a fake keyboard service, shell and clock. */
function fixture({ state = 'connected', pointer = true } = {}) {
let now = 10_000, visible = true, renders = 0, nextTimer = 1;
const timers = new Map();
const service = {
listener: null, takes: 0, releases: 0,
snapshot: () => ({ state, pointer }),
takePointer(listener) {
this.takes++; this.listener = listener;
return () => { if (this.listener === listener) this.listener = null; this.releases++; };
},
};
const globals = {
Date: { now: () => now },
setInterval: (fn, ms) => { timers.set(nextTimer, { fn, ms, at: now + ms, repeat: true }); return nextTimer++; },
setTimeout: (fn, ms) => { timers.set(nextTimer, { fn, ms, at: now + ms, repeat: false }); return nextTimer++; },
clearInterval: id => timers.delete(id), clearTimeout: id => timers.delete(id),
};
const { MouseTestLayer } = load('app/apps/developer/mouse-test.ts', {
'../../graphics/ui-fonts': { getDefaultSmallFont: () => font }, '../../graphics/image': graphics,
'../../g2/keyboard/service': { keyboardService: service },
'../../ui/shell/shell': { shell: { isWindowVisible: () => visible } },
'../../util/numeric-util': { clamp: (n, lo, hi) => Math.max(lo, Math.min(hi, n)) },
'../../ui/gestures': {}, '../../ui/layers': {},
}, globals);
const layer = new MouseTestLayer('developer', () => { renders++; });
const viewport = bordered(576, 260);
const popped = [];
const ctx = { stack: { getBaseSize: () => viewport, pop: () => popped.push(1) } };
function advance(ms) {
const end = now + ms;
for (;;) {
const due = [...timers].filter(([, t]) => t.at <= end).sort((a, b) => a[1].at - b[1].at)[0];
if (!due) break;
const [id, timer] = due;
now = timer.at;
if (timer.repeat) timer.at += timer.ms; else timers.delete(id);
timer.fn();
}
now = end;
}
const sample = (buttons, extra = {}) => service.listener({ buttons, dx: 0, dy: 0, wheel: 0, pan: 0, ...extra });
return { layer, service, ctx, viewport, timers, popped, advance, sample, paint: () => layer.paint(ctx),
renders: () => renders, setVisible: value => { visible = value; } };
}
/** Whether the arrow's tip (its top-left pixel, full white) is at (x, y) and nowhere it shouldn't be. */
const tipAt = (image, x, y) => image.getPixel(x, y) === 255 && image.getPixel(x, y + 1) === 255 && image.getPixel(x + 1, y + 2) === 70;
test('the arrow follows the touchpad, stays on the page, and repaints at a rate the link can carry', () => {
const f = fixture();
assert.equal(f.service.takes, 1, 'takes the pointer while the page shows');
f.paint();
const { width, height } = f.viewport;
// The first movement starts from the middle of the page.
f.sample(0, { dx: 10, dy: -20 });
assert.ok(tipAt(f.paint(), width / 2 + 10, height / 2 - 20));
// A hundred reports in a second are one repaint per 33 ms, not a hundred.
const before = f.renders();
for (let i = 0; i < 100; i++) { f.sample(0, { dx: 1 }); f.advance(10); }
assert.ok(f.renders() - before >= 20 && f.renders() - before <= 31, String(f.renders() - before));
assert.ok(tipAt(f.paint(), width / 2 + 110, height / 2 - 20));
// Pushed past an edge it stops there.
f.sample(0, { dx: 5000, dy: 5000 });
let image = f.paint();
assert.equal(image.getPixel(width - 1, height - 1), 255);
f.sample(0, { dx: -9000, dy: -9000 });
assert.ok(tipAt(f.paint(), 0, 0));
});
test('buttons and wheel show on the page; a left click leaves a mark and a right click clears them', () => {
const f = fixture();
f.paint();
f.sample(0, { dx: 100, dy: 40 });
const { width, height } = f.viewport;
const at = { x: width / 2 + 100, y: height / 2 + 40 };
f.sample(1);
f.sample(0, { dx: -60, wheel: 2 });
let image = f.paint();
// The mark: a small cross where the click was, after the arrow has moved off it.
assert.equal(image.getPixel(at.x - 4, at.y), 140); assert.equal(image.getPixel(at.x, at.y + 4), 140);
assert.ok(tipAt(image, at.x - 60, at.y));
f.sample(2);
image = f.paint();
assert.equal(image.getPixel(at.x - 4, at.y), 0);
f.sample(0);
assert.doesNotThrow(() => f.paint());
});
test('the pointer is only held while the page can be seen, and given back when the page goes', () => {
const f = fixture();
f.paint();
f.sample(1);
// Another window comes to the front: the buttons go back to being keys.
f.setVisible(false); f.advance(400);
assert.equal(f.service.releases, 1); assert.equal(f.service.listener, null);
f.setVisible(true); f.advance(400);
assert.equal(f.service.takes, 2);
// Double tap (or Esc, which is one) leaves; leaving gives the pointer back and stops the timers.
f.layer.handleInput({ type: 'double-click' }, f.ctx);
assert.equal(f.popped.length, 1);
f.layer.onRemoved();
assert.equal(f.service.releases, 2); assert.equal(f.timers.size, 0);
const renders = f.renders();
f.advance(5000);
assert.equal(f.renders(), renders);
});
test('without a keyboard, or with one that has no pointer, the page says so', () => {
for (const options of [{ state: 'idle' }, { state: 'connected', pointer: false }]) {
const f = fixture(options);
const image = f.paint();
assert.ok(image instanceof graphics.GrayImage);
f.layer.onRemoved();
}
});
+36
View File
@@ -157,3 +157,39 @@ test('the columns keep the padding suggested for each window edge, and scroll ri
const lit = (y) => { for (let x = 180; x < 560; x++) if (image.getPixel(x, y) > 40) return true; return false; };
assert.ok([0, 1, 2, 3, 4, 5, 6, 7].some(lit), 'rows show in the top padding once scrolled');
});
test('a hidden section comes and goes while the panel stays open', async () => {
let hidden = true;
const selected = [];
const item = (label) => ({ label, onSelect: () => selected.push(label) });
const layer = new SettingsPanelLayer([
{ label: 'Display', items: [item('Brightness')] },
{ label: 'Keyboard', items: [item('Pair')], hidden: () => hidden },
{ label: 'About', items: [item('License')] },
]);
const ctx = { stack: { getBaseSize: () => bordered(576, 260), isFocused: () => true } };
const input = (type) => layer.handleInput({ type, timestampMs: 0 }, ctx);
const enter = async () => { await input('click'); await input('click'); await input('double-click'); };
// Hidden: the row below Display is About.
await input('scroll-down'); await enter();
assert.deepEqual(selected, ['License']);
layer.focusSection('Keyboard');
await enter();
assert.deepEqual(selected, ['License', 'License'], 'a hidden section is no deep-link target either');
// Shown, with no new panel: the highlight stays on About, and Keyboard is the row above it.
hidden = false;
layer.paint(ctx);
await enter();
assert.deepEqual(selected.slice(2), ['License']);
await input('scroll-up'); await enter();
assert.deepEqual(selected.slice(3), ['Pair']);
// Hidden again from inside it: focus comes back out to the top of the list.
await input('click');
hidden = true;
layer.paint(ctx);
await enter();
assert.deepEqual(selected.slice(4), ['Brightness']);
});
+56
View File
@@ -0,0 +1,56 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { loader } = require('./helpers/load-typescript.cjs');
const settings = new Map();
const connections = loader({}, {
'../../native/settings-store': {
getStringSetting: (key, fallback) => (settings.has(key) ? settings.get(key) : fallback),
setStringSetting: (key, value) => settings.set(key, value),
},
})('app/apps/terminal/connections.ts');
// vm-realm objects: compare as plain JSON.
const plain = (value) => JSON.parse(JSON.stringify(value));
test('ssh:// strings select tmux by default', () => {
assert.deepEqual(plain(connections.parseSshConnectionString('ssh://jim@laptop')), { user: 'jim', host: 'laptop', port: 22, mode: 'tmux' });
assert.deepEqual(plain(connections.parseSshConnectionString('ssh://jim@laptop:2222/')), { user: 'jim', host: 'laptop', port: 2222, mode: 'tmux' });
assert.deepEqual(plain(connections.parseSshConnectionString('ssh://jim@[fd7a::1]:22')), { user: 'jim', host: 'fd7a::1', port: 22, mode: 'tmux' });
assert.equal(connections.parseSshConnectionString('ssh://jim%40corp@laptop').user, 'jim@corp');
});
test('?g2mirror= tunnels a g2mirror server over ssh', () => {
assert.deepEqual(plain(connections.parseSshConnectionString('ssh://jim@laptop?g2mirror=tok')), {
user: 'jim', host: 'laptop', port: 22, mode: 'g2mirror', authToken: 'tok', targetHost: '127.0.0.1', targetPort: 8737,
});
const custom = connections.parseSshConnectionString('ssh://jim@laptop?g2mirror=tok@en@localhost:9000');
assert.equal(custom.authToken, 'tok@en');
assert.equal(custom.targetHost, 'localhost');
assert.equal(custom.targetPort, 9000);
});
test('malformed ssh:// strings are rejected', () => {
for (const url of ['ssh://laptop', 'ssh://jim@', 'ssh://jim@laptop:0', 'ssh://jim@laptop?mode=tmux', 'ssh://jim@laptop?g2mirror=', 'ssh://jim@laptop?g2mirror=tok@host:99999', 'ssh://jim@laptop?g2mirror']) {
assert.equal(connections.parseSshConnectionString(url), null, url);
}
});
test('any-kind parsing and host labels', () => {
assert.equal(connections.parseAnyConnectionString('g2mirror://tok@laptop').kind, 'g2mirror');
assert.equal(connections.parseAnyConnectionString('ssh://jim@laptop').kind, 'ssh');
assert.equal(connections.parseAnyConnectionString('http://laptop'), null);
assert.equal(connections.connectionStringHostLabel('ssh://jim@laptop?g2mirror=secret'), 'jim@laptop');
assert.equal(connections.connectionStringHostLabel('ssh://jim@laptop:2222'), 'jim@laptop:2222');
assert.equal(connections.connectionStringHostLabel('ssh://jim@[fd7a::1]'), 'jim@[fd7a::1]');
assert.equal(connections.connectionStringHostLabel('g2mirror://tok@laptop'), 'laptop');
});
test('stored connections keep pinned host keys', () => {
connections.saveConnections([{ id: 'c1', url: 'ssh://jim@laptop', serverName: null, enabled: true, hostKeys: ['AAAA'] }]);
connections.updateConnection('c1', { hostKeys: ['AAAA', 'BBBB'] });
assert.deepEqual(plain(connections.loadConnections()[0].hostKeys), ['AAAA', 'BBBB']);
// Entries saved before SSH support have no hostKeys.
settings.set(connections.TERMINAL_CONNECTIONS_KEY, JSON.stringify([{ id: 'c2', url: 'g2mirror://t@h', enabled: true }]));
assert.deepEqual(plain(connections.loadConnections()[0].hostKeys), []);
});
+9
View File
@@ -118,6 +118,12 @@ function terminal({ sessions = ['vim', 'top'], presets = '', bellWake = true } =
},
'../../ui/gestures': { GESTURE_DOUBLE_CLICK: 'double-tap' },
'../../native/g2mirror-client': { G2MirrorClient: FakeClient },
'../../native/ssh': {
copyToPhoneClipboard() {}, describeSshFailure: (failure) => failure.message, getSshSecret: () => '',
openSshChannel() { throw new Error('no SSH in this test'); }, removeSshSecret() {}, setSshSecret() {},
sshClientKey: () => ({ authorizedKey: 'ecdsa-sha2-nistp256 AAAA test', fingerprint: 'SHA256:test' }),
},
'./tmux-client': { TmuxClient: class {} },
'../../native/settings-store': { onSettingsStoreChanged() {} },
'../../ui/dashboard-settings': {
enumSettingMenuItem() {}, textSettingMenuItem() {}, toggleSettingMenuItem() {},
@@ -125,6 +131,7 @@ function terminal({ sessions = ['vim', 'top'], presets = '', bellWake = true } =
terminalDisplayLayoutSetting: setting('default'),
terminalLaunchPresetsSetting: setting(presets),
terminalNewConnectionSetting: setting(''),
terminalSshPasswordSetting: setting(''),
terminalWakeOnBellSetting: setting(bellWake),
},
'../../ui/font-picker': { terminalFontPickerMenuItem() {} },
@@ -133,6 +140,8 @@ function terminal({ sessions = ['vim', 'top'], presets = '', bellWake = true } =
connectionDisplayName: (config) => config.id,
loadConnections: () => [{ id: 'c1', url: 'ok', serverName: 'host', enabled: true }],
parseConnectionString: () => ({ host: 'host' }),
parseAnyConnectionString: () => ({ kind: 'g2mirror', secure: false, host: 'host', port: 8737, authToken: 'token' }),
connectionStringHostLabel: () => 'host',
saveConnections() {}, updateConnection() {},
},
'./terminal-emulator': { TerminalEmulator: FakeEmulator },
+202
View File
@@ -0,0 +1,202 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { spawn, spawnSync } = require('node:child_process');
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const tmux = require('../.test-build/app/apps/terminal/tmux-client.js');
// ---- pure helpers ----
test('tmux version gate', () => {
assert.deepEqual(tmux.parseTmuxVersion('tmux 3.2a'), [3, 2]);
assert.equal(tmux.tmuxVersionSupported('tmux 3.2a'), true);
assert.equal(tmux.tmuxVersionSupported('tmux 3.8'), true);
assert.equal(tmux.tmuxVersionSupported('tmux 3.1c'), false);
assert.equal(tmux.tmuxVersionSupported('tmux 2.7'), false);
assert.equal(tmux.tmuxVersionSupported('tmux next-3.9'), true);
assert.equal(tmux.tmuxVersionSupported('tmux master'), true);
});
test('decodeTmuxOutput undoes tmux and transport escaping', () => {
const bytes = (text) => Array.from(tmux.decodeTmuxOutput(text));
assert.deepEqual(bytes('a\\015\\012b'), [0x61, 0x0d, 0x0a, 0x62]);
assert.deepEqual(bytes('back\\134slash'), [...Buffer.from('back\\slash')]);
assert.deepEqual(bytes('héllo ✓'), [...Buffer.from('héllo ✓')]);
// A UTF-8 character split across two %output lines arrives escaped.
assert.deepEqual([...bytes('caf\\303'), ...bytes('\\251')], [...Buffer.from('café')]);
// A lone backslash without three octal digits is literal.
assert.deepEqual(bytes('\\9'), [0x5c, 0x39]);
});
test('hostCommand keeps scripts portable across login shells', () => {
assert.equal(tmux.hostCommand('echo hi'), "exec /bin/sh -c 'echo hi'");
assert.throws(() => tmux.hostCommand("echo 'hi'"));
assert.throws(() => tmux.hostCommand('echo \\\\'));
for (const command of [tmux.pollerCommand(), tmux.viewerCommand('$3'), tmux.launchCommand('shell'), tmux.launchCommand('htop -d 5')]) {
assert.match(command, /^exec \/bin\/sh -c '[^']*'$/);
}
assert.throws(() => tmux.viewerCommand('work'));
assert.throws(() => tmux.launchCommand('rm -rf ~; echo $HOME'));
});
test('pane rows and labels', () => {
const fields = ['%3', '$1', 'work', '@2', '1', 'zsh', '2', '0', '4242', '0', '1700000000', '3', 'zsh', '/home/jim/src', 'build\twatcher'];
const row = tmux.parsePaneRow(fields.join('\t'));
assert.equal(row.title, 'build\twatcher');
assert.equal(row.bells, 3);
assert.equal(row.windowPanes, 2);
assert.equal(tmux.paneLabel(row, 'laptop.local', '/home/jim'), 'work:1.0 build\twatcher');
assert.equal(tmux.paneLabel({ ...row, title: 'laptop.local' }, 'laptop.local', '/home/jim'), 'work:1.0 zsh ~/src');
assert.equal(tmux.paneLabel({ ...row, title: 'laptop', windowPanes: 1 }, 'laptop.local', '/home/jim'), 'work:1 zsh ~/src');
assert.equal(tmux.parsePaneRow('garbage'), null);
});
test('snapshot bytes restore cursor and key modes', () => {
const state = tmux.parsePaneState('%1|4|1|1|1|0|1|0|0|0|1|80|2|10|57|5|0');
assert.equal(state.historyNext, 57);
assert.equal(state.historyOldest, 5);
const text = Buffer.from(tmux.snapshotBytes(state, ['$ ls', 'a b'], [])).toString();
assert.equal(text, '$ ls\x1b[0m\r\na b\x1b[0m\x1b[2;5H\x1b[?1h');
// Without history_added (tmux < 3.8), history_size stands in.
assert.equal(tmux.parsePaneState('%1|0|0|1|0|0|1|0|0|0|23|80|24|10||||').historyNext, 10);
});
test('send-keys commands are hex and chunked', () => {
const commands = tmux.sendKeysCommands('%4', new Uint8Array(300).fill(0x41));
assert.equal(commands.length, 2);
assert.match(commands[0], /^send-keys -t %4 -H 41 41 /);
});
// ---- against a real tmux server ----
const tmuxVersion = spawnSync('tmux', ['-V'], { encoding: 'utf8' });
const haveTmux = tmuxVersion.status === 0 && tmux.tmuxVersionSupported(tmuxVersion.stdout);
/** Mimic the SSH transport: run through a shell, split stdout/stderr into lines, escape non-UTF-8 lines. */
function localChannel(env) {
return (command, listener) => {
const child = spawn('/bin/sh', ['-c', command], { env, stdio: ['pipe', 'pipe', 'pipe'] });
let closed = false;
// Like the SSH transport, a write to a channel whose command just exited is dropped, not thrown.
child.stdin.on('error', () => {});
const lines = (stream, emit) => {
let buffered = Buffer.alloc(0);
stream.on('data', (chunk) => {
buffered = Buffer.concat([buffered, chunk]);
let index;
while ((index = buffered.indexOf(0x0a)) >= 0) {
emit(escapeLine(buffered.subarray(0, index)));
buffered = buffered.subarray(index + 1);
}
});
};
lines(child.stdout, (line) => !closed && listener.onLine(line));
lines(child.stderr, (line) => !closed && listener.onStderr(line));
child.on('spawn', () => listener.onOpen());
child.on('exit', (status) => { if (!closed) { closed = true; listener.onClosed(status); } });
return {
send: (line) => { if (closed) return false; child.stdin.write(`${line}\n`); return true; },
close: () => { if (closed) return; closed = true; child.stdin.end(); setTimeout(() => child.kill(), 500); },
};
};
}
function escapeLine(bytes) {
const text = bytes.toString('utf8');
if (Buffer.from(text, 'utf8').equals(bytes)) return text;
return Array.from(bytes, (byte) => (byte >= 0x80 ? `\\${byte.toString(8).padStart(3, '0')}` : String.fromCharCode(byte))).join('');
}
function waitFor(predicate, what, timeoutMs = 10_000) {
return new Promise((resolve, reject) => {
const started = Date.now();
const timer = setInterval(() => {
let value;
try { value = predicate(); } catch (error) { clearInterval(timer); reject(error); return; }
if (value) { clearInterval(timer); resolve(value); }
else if (Date.now() - started > timeoutMs) { clearInterval(timer); reject(new Error(`timed out waiting for ${what}`)); }
}, 50);
});
}
test('TmuxClient against a local tmux server', { skip: !haveTmux && 'tmux >= 3.2 not installed' }, async () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'fc-tmux-'));
const env = { ...process.env, TMUX_TMPDIR: dir, SHELL: '/bin/sh', PS1: '$ ' };
delete env.TMUX;
const tm = (...args) => spawnSync('tmux', args, { env, encoding: 'utf8' });
const openChannel = localChannel(env);
const clients = [];
try {
tm('new-session', '-d', '-s', 'work', '-x', '120', '-y', '40', '/bin/sh');
const control = new tmux.TmuxClient({ openChannel, label: 'test', cols: 80, rows: 20, stateKey: 'test-connection' });
clients.push(control);
const bells = [];
control.onBell((socket) => bells.push(socket));
control.start();
await waitFor(() => control.state().phase === 'connected' && control.state().sessions.length === 1, 'listing');
assert.equal(control.state().sessions[0].socket, '%0');
assert.match(control.state().sessions[0].title, /^work:0 /);
// A bell in the session's current window (tmux's own bell flag misses these).
tm('send-keys', '-t', '%0', '-l', "printf '\\a'");
tm('send-keys', '-t', '%0', 'Enter');
await waitFor(() => bells.includes('%0'), 'bell');
const launched = await control.launchSession('shell');
assert.match(launched, /^%\d+$/);
await waitFor(() => control.state().sessions.some((s) => s.socket === launched), 'launched pane in listing');
// View the original pane.
const view = new tmux.TmuxClient({ openChannel, label: 'test', cols: 80, rows: 20, stateKey: '' });
clients.push(view);
let screen = '';
const snapshots = [];
const histories = [];
view.onTerminalData((data, kind) => {
if (kind === 'snapshot') { screen = ''; snapshots.push(Buffer.from(data).toString()); }
screen += Buffer.from(data).toString();
});
view.onSnapshot((next, oldest) => snapshots.push({ next, oldest }));
view.onHistoryLines((reply) => histories.push(reply));
view.onStateChange((state) => {
if (state.phase === 'connected' && !view.requested) { view.requested = true; view.connectSession('%0'); }
});
view.onSessionAttached(() => view.view());
view.start();
await waitFor(() => snapshots.length >= 2, 'snapshot');
assert.equal(view.state().phase, 'attached');
assert.equal(tm('display', '-p', '-t', '%0', '#{window_width}x#{window_height}').stdout.trim(), '80x20');
view.sendInput('for i in $(seq 1 60); do echo row$i; done\r');
await waitFor(() => screen.includes('row60'), 'typed command output');
// Re-snapshot: rows have scrolled into the history; page some back.
view.unview();
view.view();
await waitFor(() => snapshots.length >= 4, 'second snapshot');
const { next } = snapshots[snapshots.length - 1];
assert.ok(next > 20, `history should have grown, next=${next}`);
view.requestHistory(next, 5);
await waitFor(() => histories.length === 1, 'history page');
assert.equal(histories[0].lines.length, 5);
assert.equal(histories[0].start, next - 5);
assert.ok(histories[0].lines.some((line) => /^row\d+$/.test(line)), JSON.stringify(histories[0].lines));
// Unviewing gives the window back its own size.
view.unview();
await waitFor(() => tm('display', '-p', '-t', '%0', '#{window_width}').stdout.trim() !== '80', 'size restored', 3000).catch(() => {});
// The pane going away ends the view.
let detached = null;
view.onSessionDetached((reason) => { detached = reason; });
view.view();
tm('kill-pane', '-t', '%0');
await waitFor(() => detached !== null, 'detach on pane exit');
} finally {
for (const client of clients) client.stop();
tm('kill-server');
fs.rmSync(dir, { recursive: true, force: true });
}
});
+31
View File
@@ -4,6 +4,7 @@ const { loader } = require('./helpers/load-typescript.cjs');
const load = loader();
const { TranscriptModel, TranscriptScroll, transcriptLayout, wrapTranscribeText } = load('app/apps/transcribe/transcript-model.ts');
const { SpeechPauseDetector } = load('app/native/speech-pause.ts');
const { formatReplayTime, stepReplaySeconds } = load('app/apps/transcribe/replay-time.ts');
const { TimedTranscript } = load('app/native/transcript-format.ts');
const { bordered, unbordered, edged } = require('./helpers/viewport.cjs');
@@ -128,3 +129,33 @@ test('pause detector uses 1.5 seconds of PCM, fires once per pause, and ignores
d.accept(pcm(3000));
assert.equal(d.accept(pcm(0, 1500)), true);
});
test('a replay jump leaves its time on a line of its own and starts a new paragraph', () => {
const m = new TranscriptModel();
m.mark('[0:30]');
assert.equal(m.text, '[0:30]');
m.accept({ text: 'After the jump', isFinal: false });
assert.equal(m.text, '[0:30]\nAfter the jump');
// What was still being spoken when the replay jumped is kept, ahead of the mark.
m.mark('[1:15]');
m.accept({ text: 'Later.', isFinal: true });
m.accept({ text: 'More.', isFinal: true });
assert.equal(m.text, '[0:30]\nAfter the jump\n[1:15]\nLater. More.');
});
test('replay times read as m:ss, with hours from an hour on', () => {
assert.equal(formatReplayTime(0), '0:00');
assert.equal(formatReplayTime(83_999), '1:23');
assert.equal(formatReplayTime(3_600_000), '1:00:00');
assert.equal(formatReplayTime(83_000, true), '0:01:23');
assert.equal(formatReplayTime(-5), '0:00');
});
test('the time picker steps carry between fields and stay inside the recording', () => {
assert.equal(stepReplaySeconds(59, 'seconds', 1, 600), 60);
assert.equal(stepReplaySeconds(60, 'seconds', -1, 600), 59);
assert.equal(stepReplaySeconds(30, 'minutes', 2, 600), 150);
assert.equal(stepReplaySeconds(590, 'minutes', 1, 600), 600);
assert.equal(stepReplaySeconds(30, 'minutes', -1, 600), 0);
assert.equal(stepReplaySeconds(100, 'hours', 1, 7300), 3700);
});
+13
View File
@@ -16,6 +16,18 @@
"../app/remote/protocol.ts",
"../app/remote/listeners.ts",
"../app/g2/ancs-client.ts",
"../app/g2/keyboard/protocol.ts",
"../app/g2/keyboard/hid.ts",
"../app/g2/keyboard/client.ts",
"../app/g2/keyboard/bridge.ts",
"../app/g2/keyboard/keys.ts",
"../app/g2/keyboard/bindings.ts",
"../app/g2/keyboard/key-router.ts",
"../app/g2/keyboard/pointer.ts",
"../app/apps/terminal/terminal-keys.ts",
"../app/native/terminal-session-types.ts",
"../app/apps/terminal/terminal-client.ts",
"../app/apps/terminal/tmux-client.ts",
"../app/phone-ui/ble-bandwidth-meter.ts",
"../app/apps/terminal/hub-header.ts",
"../app/g2/ios-peripheral-identity.ts",
@@ -39,6 +51,7 @@
"../app/g2/glasses-artwork.ts",
"../app/g2/glasses-display-state.ts",
"../app/g2/glance-state.ts",
"../app/native/imu-types.ts",
"../app/ui/shell/notification-modal-queue.ts",
"../app/ui/shell/notification-popup-delay.ts",
"../app/ui/shell/notification-popup-timeout.ts",
+22 -17
View File
@@ -166,6 +166,8 @@ function androidBridge(modules = {}) {
setNoiseSuppression() {}, setBeamFilter() {}, clearSpeakerVerification() {},
setFrenchWhisper(enabled) { this.frenchWhisper = enabled; },
setOnboardModelKind(kind) { this.modelKind = kind; }, setOnboardLanguage(language) { this.language = language; },
setMicArray() {}, setReplay() {}, setRecordingEnabled(enabled) { this.recordingEnabled = enabled; },
noteRecordingEvent(json) { (this.recordingEvents ??= []).push(JSON.parse(json)); },
start(_mode, id) { this.captureId = id; this.active = true; this.starts++; },
stop() { this.active = false; },
isCapturing() { return this.active; },
@@ -253,24 +255,23 @@ test('dedicated French Whisper selects its own files and switches back without r
bridge.stop();
});
test('Android continuous captures record to the requested WAV, and report one that cannot be created', () => {
test('Android continuous captures record to the requested file, and report one that cannot be created', () => {
const recordings = [];
let fail = false, attempts = 0;
class FakeRecorder {
static open(name) {
attempts++;
if (fail) return null;
const r = new FakeRecorder(); r.name = name; r.path = `/Download/${name}.wav`; r.pcm = []; r.closed = false; recordings.push(r); return r;
}
append(pcm) { this.pcm.push(...pcm); }
close() { this.closed = true; }
}
const { bridge, controller } = androidBridge({
'./wav-recorder': { WavRecorder: FakeRecorder },
'./mic-recording': { newRecordingPath: name => `/Download/${name}.mka` },
'./speech-pause': { SpeechPauseDetector: class { reset() {} accept() { return false; } } },
'../util/array-util': { toUint8Array: pcm => Uint8Array.from(pcm) },
'../util/audio-level': { pcmLevel: () => 0 },
});
// The controller writes the file itself, from the capture's own thread.
controller.startRecording = (path, json) => {
attempts++;
if (fail) return false;
recordings.push({ path, info: JSON.parse(json), closed: false });
return true;
};
controller.stopRecording = () => { recordings.at(-1).closed = true; return 0; };
const options = { provider: 'onboard' };
let changes = 0; bridge.onRecordingChange(() => changes++);
bridge.recordTo('transcribe-1');
@@ -278,20 +279,24 @@ test('Android continuous captures record to the requested WAV, and report one th
// Push-to-talk on a capture of its own is not recorded, and the file waits for audio to save.
bridge.startPushToTalk(options);
controller.listener.onPcm([9, 9]);
assert.equal(attempts, 0);
assert.deepEqual([attempts, controller.recordingEnabled], [0, false]);
bridge.startContinuousCapture(options);
controller.listener.onPcm([1, 2]);
controller.listener.onPcm([3, 4]);
assert.deepEqual(recordings.map(r => [r.name, r.pcm, r.closed]), [['transcribe-1', [1, 2, 3, 4], false]]);
// The file outlives a capture: one restarted after the glasses reconnect keeps appending to it.
assert.deepEqual(recordings.map(r => [r.path, r.info.recording, r.closed]), [['/Download/transcribe-1.mka', 'transcribe-1', false]]);
assert.deepEqual([attempts, controller.recordingEnabled], [1, true]);
// The file outlives a capture: one restarted after the glasses reconnect goes on in it,
// with the restart noted, and nothing is recorded while no capture holds the mic.
bridge.handleSessionEnded();
assert.equal(controller.recordingEnabled, false);
bridge.resumeCapture(options);
controller.listener.onPcm([5]);
assert.deepEqual(recordings.map(r => [r.pcm, r.closed]), [[[1, 2, 3, 4, 5], false]]);
assert.deepEqual([attempts, recordings[0].closed, controller.recordingEnabled], [1, false, true]);
assert.deepEqual(controller.recordingEvents.map(event => [event.event, event.recording]), [['capture', 'transcribe-1']]);
bridge.recordTo('');
assert.deepEqual([recordings[0].closed, bridge.recording, changes], [true, false, 2]);
controller.listener.onPcm([6]);
assert.equal(recordings[0].pcm.length, 5);
assert.equal(attempts, 1);
// One attempt per recording, not one per packet; another recording is a fresh attempt.
fail = true; attempts = 0;
@@ -305,5 +310,5 @@ test('Android continuous captures record to the requested WAV, and report one th
bridge.recordTo('');
bridge.recordTo('transcribe-2');
controller.listener.onPcm([10]);
assert.deepEqual([bridge.recording, bridge.recordingFailed, recordings.at(-1).pcm], [true, false, [10]]);
assert.deepEqual([bridge.recording, bridge.recordingFailed, recordings.at(-1).path], [true, false, '/Download/transcribe-2.mka']);
});
+209
View File
@@ -0,0 +1,209 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { loader } = require('./helpers/load-typescript.cjs');
/** The Android voice bridge over a fake native controller that records what it is told. */
function harness({ recordingOk = true, downloads = '/downloads' } = {}) {
const controllers = [];
class FakeController {
constructor() {
this.calls = []; this.events = []; this.capturing = false; this.recording = null;
this.micArray = null; this.replay = null; this.recordingEnabled = null;
controllers.push(this);
// Setters the tests don't look at are only logged.
return new Proxy(this, { get: (target, name) => name in target ? target[name] : (...args) => { target.calls.push([name, ...args]); } });
}
setListener(listener) { this.listener = listener; }
setMicArray(record) { this.micArray = record; }
setReplay(path, startMs) { this.replay = path === null ? null : { path, startMs }; }
start(mode) { this.calls.push(['start', mode]); this.capturing = true; }
stop() { this.calls.push(['stop']); this.capturing = false; }
isCapturing() { return this.capturing; }
startRecording(path, json) { if (recordingOk) this.recording = { path, info: JSON.parse(json) }; return recordingOk; }
setRecordingEnabled(enabled) { this.recordingEnabled = enabled; }
noteRecordingEvent(json) { this.events.push(JSON.parse(json)); }
stopRecording() { this.calls.push(['stopRecording']); this.recording = null; return 0; }
seekReplay(ms) { this.calls.push(['seekReplay', ms]); }
setReplayPaused(paused) { this.calls.push(['setReplayPaused', paused]); }
}
const module = loader({
global: { isAndroid: true },
com: { faceclaw: { app: { FaceclawVoiceController: FakeController, FaceclawVoiceControllerListener: function (impl) { return impl; } } } },
}, {
'@nativescript/core': { Utils: { android: { getApplicationContext: () => ({}) } } },
'./cloud-stt': { toJavaBytes: bytes => Array.from(bytes) },
'./cloud-stt-provider': {
createCloudSttClient: () => null,
transcriptionKey: options => `${options.provider}:${options.language ?? ''}`,
},
'./mic-recording': { newRecordingPath: name => downloads === null ? null : `${downloads}/${name}.mka` },
'../util/array-util': { toUint8Array: value => value },
})('app/native/voice-control.ts');
const options = (extra = {}) => ({
communicator: {}, provider: 'onboard', elevenLabsApiKey: '', openAiApiKey: '', sonioxApiKey: '', ...extra,
});
return { ...module, controllers, options, pcm: controller => controller.listener.onPcm(new Uint8Array(1600)) };
}
const CONFIGURE_2 = Uint8Array.from([0x4d, 0x43, 1, 1, 1, 1, 1, 0, 160, 0, 0, 0, 2]);
const CONFIGURE_4 = Uint8Array.from([0x4d, 0x43, 1, 1, 0, 3, 1, 0, 160, 0, 0, 0, 2]);
test('a capture of the temples\' microphones arms them, and another microphone count restarts the stream', () => {
const h = harness();
const bridge = h.voiceControlBridge;
bridge.startContinuousCapture(h.options({ micArray: { configure: CONFIGURE_2 } }));
const controller = h.controllers[0];
assert.deepEqual(controller.micArray, Array.from(CONFIGURE_2));
assert.equal(controller.replay, null);
assert.deepEqual(controller.calls.filter(call => call[0] === 'start'), [['start', 'onboard']]);
// Push-to-talk asks for the same thing while the window is open: it shares the stream.
bridge.startPushToTalk(h.options({ micArray: { configure: CONFIGURE_2 } }));
assert.equal(controller.calls.filter(call => call[0] === 'start').length, 1);
bridge.stopPushToTalk();
// The Microphones setting changed.
bridge.retargetContinuousCapture(h.options({ micArray: { configure: CONFIGURE_4 } }));
assert.deepEqual(controller.micArray, Array.from(CONFIGURE_4));
assert.deepEqual(controller.calls.filter(call => call[0] === 'start' || call[0] === 'stop').map(call => call[0]), ['start', 'stop', 'start']);
// Back to the mixed stream; the phone's microphone never gets an array.
bridge.retargetContinuousCapture(h.options());
assert.equal(controller.micArray, null);
bridge.retargetContinuousCapture(h.options({ communicator: null, usePhoneMic: true, micArray: { configure: CONFIGURE_4 } }));
assert.equal(controller.micArray, null);
});
test('a recording opens with the first audio, describes its capture, and follows the capture across restarts', () => {
const h = harness();
const bridge = h.voiceControlBridge;
const changes = [];
bridge.onRecordingChange(() => changes.push(bridge.recording));
bridge.recordTo('transcribe-1');
assert.equal(bridge.recording, true);
bridge.startContinuousCapture(h.options({ micArray: { configure: CONFIGURE_4 }, noiseSuppression: true, captureInfo: { mics: 4, glasses: 'G2' } }));
const controller = h.controllers[0];
// Nothing on disk until audio arrives: a capture that never starts leaves no file.
assert.equal(controller.recording, null);
assert.equal(controller.recordingEnabled, true);
h.pcm(controller);
assert.equal(controller.recording.path, '/downloads/transcribe-1.mka');
const info = controller.recording.info;
assert.equal(info.recording, 'transcribe-1');
assert.equal(info.source, 'glasses');
assert.equal(info.micArray, true);
assert.equal(info.mics, 4);
assert.equal(info.glasses, 'G2');
assert.deepEqual(info.transcription, { provider: 'onboard', language: '' });
assert.deepEqual(info.processing, { noiseSuppression: true, beamFilter: null });
h.pcm(controller);
assert.equal(controller.calls.filter(call => call[0] === 'stopRecording').length, 0);
// The capture restarts with other microphones: the same file goes on, with the change noted in it.
bridge.retargetContinuousCapture(h.options({ captureInfo: { mics: 1, glasses: 'G2' } }));
assert.equal(controller.events.length, 1);
assert.equal(controller.events[0].event, 'capture');
assert.equal(controller.events[0].mics, 1);
assert.equal(controller.events[0].micArray, false);
assert.equal(controller.recording.path, '/downloads/transcribe-1.mka');
// Save Recording was turned off.
bridge.recordTo('');
assert.equal(bridge.recording, false);
assert.equal(controller.recording, null);
assert.deepEqual(changes, [true, false]);
});
test('push-to-talk on a capture of its own is not recorded', () => {
const h = harness();
const bridge = h.voiceControlBridge;
bridge.recordTo('transcribe-1');
bridge.startPushToTalk(h.options());
const controller = h.controllers[0];
assert.equal(controller.recordingEnabled, false);
h.pcm(controller);
assert.equal(controller.recording, null);
// The Transcribe window joins the running stream: from here it is recorded.
bridge.startContinuousCapture(h.options());
assert.equal(controller.recordingEnabled, true);
h.pcm(controller);
assert.equal(controller.recording.path, '/downloads/transcribe-1.mka');
bridge.stopContinuousCapture();
assert.equal(controller.recordingEnabled, false);
});
test('a recording that cannot be created is reported once', () => {
for (const h of [harness({ recordingOk: false }), harness({ downloads: null })]) {
const bridge = h.voiceControlBridge;
bridge.recordTo('transcribe-1');
bridge.startContinuousCapture(h.options());
h.pcm(h.controllers[0]);
assert.equal(bridge.recording, false);
assert.equal(bridge.recordingFailed, true);
}
});
test('a replay runs on its own controller, follows its events, jumps while playing and restarts after the end', () => {
const h = harness();
const replay = new h.VoiceReplay('/downloads/talk.mka', () => h.options({ communicator: {}, micArray: { configure: CONFIGURE_4 } }));
const seen = [];
replay.onChange(jumpedToMs => seen.push([replay.state, replay.positionMs, jumpedToMs]));
assert.equal(h.controllers.length, 0);
replay.start();
// The microphone's bridge has not been touched.
assert.equal(h.controllers.length, 1);
const controller = h.controllers[0];
assert.deepEqual(controller.replay, { path: '/downloads/talk.mka', startMs: 0 });
assert.equal(controller.micArray, null);
assert.equal(replay.state, 'opening');
const statuses = [];
replay.events.onStatus(state => statuses.push(state.status));
assert.equal(statuses.at(-1), 'Opening the recording...');
controller.listener.onReplay('start', 0, 60_000);
assert.equal(replay.state, 'playing');
assert.equal(replay.durationMs, 60_000);
controller.listener.onReplay('position', 1000, 60_000);
assert.equal(replay.positionMs, 1000);
// A plain start and the ticking position are not jumps.
assert.deepEqual(seen.map(entry => entry[2]), [undefined, undefined, undefined]);
replay.setPaused(true);
assert.equal(replay.state, 'paused');
replay.setPaused(false);
replay.jumpTo(90_000); // past the end: held to the recording's length
assert.deepEqual(controller.calls.filter(call => call[0] === 'seekReplay' || call[0] === 'setReplayPaused'),
[['setReplayPaused', true], ['setReplayPaused', false], ['seekReplay', 60_000]]);
replay.jumpTo(30_000);
controller.listener.onReplay('seek', 30_000, 60_000);
assert.deepEqual(seen.at(-1), ['playing', 30_000, 30_000]);
controller.listener.onReplay('end', 60_000, 60_000);
controller.capturing = false;
assert.equal(replay.state, 'ended');
assert.equal(replay.positionMs, 60_000);
replay.setPaused(true); // nothing to pause
assert.equal(replay.state, 'ended');
// A jump once it has ended plays again from there.
replay.jumpTo(45_000);
assert.deepEqual(controller.replay, { path: '/downloads/talk.mka', startMs: 45_000 });
assert.equal(replay.state, 'opening');
controller.listener.onReplay('start', 45_000, 60_000);
assert.deepEqual(seen.at(-1), ['playing', 45_000, 45_000]);
replay.close();
assert.equal(controller.calls.at(-1)[0], 'stop');
replay.start();
assert.equal(controller.calls.at(-1)[0], 'stop');
});
test('a recording that cannot be opened fails the replay', () => {
const h = harness();
const replay = new h.VoiceReplay('/downloads/missing.mka', () => h.options());
replay.start();
const controller = h.controllers[0];
controller.listener.onStatus('Could not open the recording: no such file');
controller.listener.onReplay('error', 0, 0);
assert.equal(replay.state, 'failed');
const statuses = [];
replay.events.onStatus(state => statuses.push(state.status));
assert.deepEqual(statuses, ['Could not open the recording: no such file']);
});
-19
View File
@@ -1,19 +0,0 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { loader } = require('./helpers/load-typescript.cjs');
test('the WAV header describes 16 kHz mono PCM16 and its data size', () => {
const { wavHeader } = loader({ DataView }, {})('app/native/wav-header.ts');
const header = Buffer.from(wavHeader(32000));
assert.equal(header.length, 44);
assert.equal(header.toString('ascii', 0, 4), 'RIFF');
assert.equal(header.readUInt32LE(4), 36 + 32000);
assert.equal(header.toString('ascii', 8, 16), 'WAVEfmt ');
assert.equal(header.readUInt16LE(20), 1);
assert.equal(header.readUInt16LE(22), 1);
assert.equal(header.readUInt32LE(24), 16000);
assert.equal(header.readUInt32LE(28), 32000);
assert.equal(header.readUInt16LE(34), 16);
assert.equal(header.toString('ascii', 36, 40), 'data');
assert.equal(header.readUInt32LE(40), 32000);
});