Merge branch 'main' into glanceboard-tilt-angle-and-glance-down

This commit is contained in:
James Babcock committed 2026-11-15 22:38:39 -08:00
commit 37d81d0308
416 files changed
+34629 -3885

No files matched your search

+41 -2
View File
@@ -4,6 +4,7 @@ const fs = require('node:fs');
const path = require('node:path');
const vm = require('node:vm');
const ts = require('typescript');
const { bordered, unbordered, edged } = require('./helpers/viewport.cjs');
function load(file, deps, globals = {}) {
const exports = {};
@@ -256,8 +257,10 @@ function shellEnv({ wakeAction = 'voice-input', skipConfirmation = false } = {})
const timings = { spanCurrent: (_, fn) => fn(), span: (_, __, fn) => fn(), spanAsync: (_, __, fn) => fn(),
startFrame: () => 0, annotateFrame() {}, finishFrame() {}, runWithFrame: (_, fn) => fn() };
const layers = load('app/ui/layers.ts', {
'./viewport': require('../.test-build/app/ui/viewport.js'),
'../graphics/image': images, '../graphics/plane': { dimPlanes: (planes) => planes },
'../native/frame-timings': timings, './gestures': gestures,
'../graphics/screen-size': { screenWidth: () => 640, screenHeight: () => 480 },
});
class Menu {
constructor(title, items) { this.title = title; this.items = items; this.selectedIndex = 0; }
@@ -282,9 +285,11 @@ function shellEnv({ wakeAction = 'voice-input', skipConfirmation = false } = {})
'../notifications': {}, '../dashboard-settings': settings, './ambient-cards': {},
'./chrome-layer': { ShellChromeLayer: class {} }, './modal-layer': {}, './tool-debug-layer': {},
'./brightness-picker-layer': {}, './notification-modal-queue': load('app/ui/shell/notification-modal-queue.ts', {}),
'./notification-popup-timeout': {},
'../../native/notification-icons': {}, '../../native/notification-sources': {},
'../../assistant/tool-registry': {}, './geometry': {
sidebarWidth: () => 64, minWindowTop: () => 96, switcherPosition: () => 'left', uiDepth: () => 0,
screenWidth: () => 640, screenHeight: () => 480,
appViewportRect: () => ({ x: 64, y: 124, width: 576, height: 260 }),
},
});
@@ -292,7 +297,7 @@ function shellEnv({ wakeAction = 'voice-input', skipConfirmation = false } = {})
'../../graphics/image': images, '../../native/frame-timings': timings,
'../../util/render-freshness': { beginRenderPass() {}, endRenderPass: () => false },
'../layers': layers, '../window-menu': { WindowMenuLayer: Menu },
'./chrome-layer': { windowIcon: () => () => null }, './geometry': { appViewportSize: () => ({ width: 576, height: 260 }), setAppImmersive: () => false },
'./chrome-layer': { windowIcon: () => () => null }, './geometry': { appViewport: () => ({ width: 576, height: 260 }), setAppImmersive: () => false },
'./shell': { shell },
});
const input = [], menus = [];
@@ -396,7 +401,9 @@ function chatLayerEnv() {
});
createAiChatWindow({ actions: {}, setSurfaceVisible() {}, onClosed() {} });
const layer = windowOptions.baseLayer;
return { ...env, layer, paint: (width = 576, height = 260) => layer.paint({ stack: { getBaseSize: () => ({ width, height }) } }) };
// A framed window (8px of padding all round) unless a viewport says otherwise.
return { ...env, layer, paint: (width = 576, height = 260, viewport = bordered(width, height)) =>
layer.paint({ stack: { getBaseSize: () => viewport } }) };
}
test('scrollback stays anchored during streaming, follows at the bottom and resets on session switch', () => {
@@ -438,3 +445,35 @@ test('chat paints at both window heights with pending speech pinned beneath hist
}
}
});
test('chat keeps inside the padding of the edges a border runs along, and uses the room where there is none', () => {
const env = chatLayerEnv();
const session = env.store.ensureSession();
for (let i = 0; i < 30; i++) session.transcript.push({ role: i % 2 ? 'assistant' : 'user', text: `Message ${i}: sample conversation text.` });
// The rows and columns holding any ink, text included.
const inkBounds = (image) => {
const baked = image.withDrawsBaked();
const bounds = { left: Infinity, top: Infinity, right: -1, bottom: -1 };
for (let y = 0; y < baked.height; y++) for (let x = 0; x < baked.width; x++) {
if (!baked.getPixel(x, y)) continue;
bounds.left = Math.min(bounds.left, x); bounds.right = Math.max(bounds.right, x);
bounds.top = Math.min(bounds.top, y); bounds.bottom = Math.max(bounds.bottom, y);
}
return bounds;
};
for (const viewport of [
bordered(576, 260), unbordered(576, 260), edged(576, 260, 'top', 'left'), edged(576, 452, 'bottom', 'right'),
bordered(476, 155), unbordered(476, 155), edged(640, 480, 'bottom'),
]) {
const { width, height, padding } = viewport;
const where = JSON.stringify(viewport);
const ink = inkBounds(env.paint(width, height, viewport));
assert.ok(env.layer.maxFirstLine > 0, where);
// The footer's rule starts right at the left padding and the scrollbar hugs the right.
assert.equal(ink.left, padding.left, where);
assert.equal(ink.right, width - padding.right - 1, where);
// The title's line starts at the top padding and the footer's last line ends at the bottom.
assert.ok(ink.top >= padding.top && ink.top < padding.top + 12, where);
assert.ok(ink.bottom < height - padding.bottom && ink.bottom >= height - padding.bottom - 12, where);
}
});
+11
View File
@@ -32,6 +32,17 @@ test('receives fragmented UTF-8 notifications; pre-existing and modified items d
x.source(8,0,4);x.feed(3,[]);x.details(8);x.feed(3,[]);x.metadata(8);assert.equal(x.changed.at(-1)[1],false)
assert.ok(x.sent.every(p=>p.length<=20))
})
test('a phone that is not sharing ANCS says so and is asked again, and an unknown status restarts',t=>{
const x=setup(t);x.client.stop()
const sent=[],c=new AncsClient(async b=>sent.push([...b]),()=>{})
t.after(()=>c.stop());c.start(5,20)
c.receive(new Uint8Array([65,78,1,0,...le(5),0,0,3,0]))
c.receive(new Uint8Array([65,78,1,0,...le(5),1,0,3,3]))
assert.equal(c.state,'unavailable');assert.match(c.statusMessage,/Share System Notifications/)
const d=new AncsClient(async()=>{},()=>{});t.after(()=>d.stop());d.start(6,20)
d.receive(new Uint8Array([65,78,1,0,...le(6),0,0,3,4]))
assert.equal(d.state,'unavailable');assert.doesNotMatch(d.statusMessage,/Share System Notifications/)
})
test('removal during retrieval cannot resurrect notification',t=>{
const x=setup(t);x.source(7);x.source(7,2);x.details(7);x.feed(3,[])
assert.deepEqual(x.client.read(),[])
+321 -37
View File
@@ -4,25 +4,28 @@ const fs = require('node:fs');
const path = require('node:path');
const vm = require('node:vm');
const ts = require('typescript');
const { bordered, unbordered, edged, sameViewport } = require('./helpers/viewport.cjs');
const read = (file) => fs.readFileSync(path.join(__dirname, '..', file), 'utf8');
const js = (source) => ts.transpileModule(source, {
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 },
}).outputText;
function geometry() {
// The shell geometry over a G2's 640x480 screen, or `screen` (a RayNeo iO is 540x220).
function geometry(screen = { width: 640, height: 480 }) {
const settings = Object.fromEntries(Object.entries({
displayLayoutSetting: 'band-middle', navigateDisplayLayoutSetting: 'global', terminalDisplayLayoutSetting: 'default',
appSwitcherPositionSetting: 'left', uiDepthSetting: '0', statusBarPositionSetting: 'top',
statusBarVisibilitySetting: 'always', windowBorderSetting: true,
statusBarVisibilitySetting: 'always', windowBorderSetting: 'all',
}).map(([name, value]) => [name, { value, get() { return this.value; } }]));
const context = { exports: {}, require: (name) => {
if (name === '../../graphics/image') return { G2_LENS_WIDTH: 640, G2_LENS_HEIGHT: 480 };
if (name === '../../graphics/screen-size') return { screenWidth: () => screen.width, screenHeight: () => screen.height };
if (name === '../viewport') return require('../.test-build/app/ui/viewport.js');
assert.equal(name, '../dashboard-settings');
return settings;
} };
vm.runInNewContext(js(read('app/ui/shell/geometry.ts')), context);
const rect = (mode, appId) => ({ ...context.exports.appViewportRect(mode, appId) });
return { ...context.exports, settings, rect };
return { ...context.exports, settings, screen, rect };
}
const LAYOUTS = ['band-top', 'band-high', 'band-middle', 'band-low', 'band-bottom', 'tall', 'fullscreen'];
@@ -98,18 +101,22 @@ test('Full Screen widens Tall windows to the panel and drops depth, beside a bot
test('all display modes and layouts stay within the display and align content below the bar', () => {
const g = geometry();
g.settings.uiDepthSetting.value = '48';
const combos = [['left', 'top'], ['right', 'top'], ['bottom', 'top'], ['bottom', 'bottom'], ['left', 'bottom']];
// A side strip has no choice of border: the setting doesn't reach it.
const combos = [['left', 'top'], ['right', 'top'], ['left', 'bottom'], ['right', 'top', 'always', 'none']];
for (const statusBar of ['top', 'bottom']) {
for (const visibility of ['always', 'switcher']) {
for (const border of [true, false]) combos.push(['popup', statusBar, visibility, border]);
for (const border of ['all', 'status', 'none']) {
combos.push(['bottom', statusBar, 'always', border]);
for (const visibility of ['always', 'switcher']) combos.push(['popup', statusBar, visibility, border]);
}
}
for (const [switcher, statusBar, visibility = 'always', border = true] of combos) {
for (const [switcher, statusBar, visibility = 'always', border = 'all'] of combos) {
g.settings.appSwitcherPositionSetting.value = switcher;
g.settings.statusBarPositionSetting.value = statusBar;
g.settings.statusBarVisibilitySetting.value = visibility;
g.settings.windowBorderSetting.value = border;
const side = switcher === 'left' || switcher === 'right';
// Only no border at all drops the line between a status bar and the window.
assert.equal(g.statusBarDivider(), side || border !== 'none');
for (const globalMode of LAYOUTS) {
g.settings.displayLayoutSetting.value = globalMode;
for (const appId of ['navigate', 'terminal']) {
@@ -126,15 +133,18 @@ test('all display modes and layouts stay within the display and align content be
if (switcher === 'popup') {
// Each edge has the status bar's rows (unless it only
// overlays), else the frame's side, else nothing.
assert.equal(g.windowFramed(appId), border && !wide);
assert.equal(g.windowFramed(appId), border === 'all' && !wide);
const edge = (side) => visibility === 'always' && statusBar === side ? g.TOP_BAR_HEIGHT : g.windowFramed(appId) ? 1 : 0;
assert.deepEqual([header, footer], [edge('top'), edge('bottom')]);
// The status bar's rows lie within the band, at its edge.
const top = g.windowTop(heightMode, appId), band = g.windowBandHeight(heightMode, appId);
assert.equal(g.statusBarTop(heightMode, appId), statusBar === 'top' ? top : top + band - g.TOP_BAR_HEIGHT);
} else {
assert.equal(g.windowFramed(appId), switcher === 'bottom' && !wide);
assert.equal(g.windowFramed(appId), switcher === 'bottom' && border === 'all' && !wide);
assert.deepEqual([header, footer], [!g.statusInSwitcherRow(appId) ? g.TOP_BAR_HEIGHT : g.windowFramed(appId) ? 1 : 0, 0]);
// A bottom row's top edge is a line with every border, or as
// the status line where the status bar is in the row.
if (switcher === 'bottom') assert.equal(g.switcherRowDivider(appId), border === 'all' || (border === 'status' && statusBar === 'bottom'));
}
assert.equal(rect.y, g.windowTop(heightMode, appId) + header);
assert.equal(rect.height + header + footer, g.windowBandHeight(heightMode, appId));
@@ -195,13 +205,16 @@ test('a popup app switcher centres 576-wide windows, whose edges the status bar
assert.deepEqual(g.rect('min'), { x: 32, y: 97, width: 576, height: 286 });
assert.deepEqual(g.rect('max', 'terminal'), { x: 32, y: 1, width: 576, height: 478 });
assert.equal(g.statusBarTop('min'), 356);
// Without the frame too, windows have no chrome rows at all.
g.settings.windowBorderSetting.value = false;
assert.equal(g.windowFramed(), false);
assert.deepEqual(g.rect('max', 'terminal'), { x: 32, y: 0, width: 576, height: 480 });
assert.deepEqual(g.rect('medium'), { x: 32, y: 96, width: 576, height: 288 });
// Without the frame too (just the status line, or no border), windows
// have no chrome rows at all.
for (const border of ['status', 'none']) {
g.settings.windowBorderSetting.value = border;
assert.equal(g.windowFramed(), false);
assert.deepEqual(g.rect('max', 'terminal'), { x: 32, y: 0, width: 576, height: 480 });
assert.deepEqual(g.rect('medium'), { x: 32, y: 96, width: 576, height: 288 });
}
// Full Screen: the status bar keeps its edge, but there's no room for a frame.
g.settings.windowBorderSetting.value = true;
g.settings.windowBorderSetting.value = 'all';
g.settings.statusBarVisibilitySetting.value = 'always';
g.settings.displayLayoutSetting.value = 'fullscreen';
assert.equal(g.windowFramed(), false);
@@ -263,6 +276,168 @@ test('an immersive app fills the panel without chrome rows, frame or depth, in a
}
});
test('on a RayNeo iO\'s 540x220 screen windows take every row under the bar, beside the strip or over the row', () => {
const g = geometry({ width: 540, height: 220 });
const panel = { x: 0, y: 0, width: 540, height: 220 };
// A band is the whole screen's height, so every band position is the same.
for (const layout of LAYOUTS) {
g.settings.displayLayoutSetting.value = layout;
g.settings.appSwitcherPositionSetting.value = 'left';
assert.equal(g.minWindowHeight(), 220);
assert.equal(g.minWindowTop(), 0);
for (const mode of ['min', 'medium', 'max']) assert.deepEqual(g.rect(mode), { x: 64, y: 28, width: 476, height: 192 }, layout);
assert.deepEqual({ ...g.switcherRect('min') }, { x: 0, y: 0, width: 64, height: 220 });
g.settings.appSwitcherPositionSetting.value = 'right';
assert.deepEqual(g.rect('min'), { x: 0, y: 28, width: 476, height: 192 });
assert.deepEqual({ ...g.switcherRect('min') }, { x: 476, y: 0, width: 64, height: 220 });
}
assert.equal(g.isOnSwitcherEdge(500, 10, 'min'), true);
assert.equal(g.screenCenterInViewportX(), 270);
// A bottom row: the window centred above it, its sides room for the deepest shift.
g.settings.appSwitcherPositionSetting.value = 'bottom';
g.settings.displayLayoutSetting.value = 'band-middle';
assert.deepEqual(g.rect('min'), { x: 32, y: 28, width: 476, height: 156 });
assert.deepEqual({ ...g.switcherRect('min') }, { x: 32, y: 184, width: 476, height: 36 });
g.settings.displayLayoutSetting.value = 'fullscreen';
assert.deepEqual(g.rect('min'), { x: 0, y: 28, width: 540, height: 156 });
// The phone mirror shows the whole screen, at its 27:11.
g.settings.displayLayoutSetting.value = 'band-middle';
for (const position of ['left', 'bottom', 'popup']) {
g.settings.appSwitcherPositionSetting.value = position;
assert.deepEqual({ ...g.phonePreviewRect({ ...g.usedScreenRect('min') }) }, panel, position);
}
// Every layout stays on the screen.
g.settings.uiDepthSetting.value = '62';
for (const switcher of ['left', 'right', 'bottom', 'popup']) {
g.settings.appSwitcherPositionSetting.value = switcher;
for (const statusBar of ['top', 'bottom']) {
g.settings.statusBarPositionSetting.value = statusBar;
for (const layout of LAYOUTS) {
g.settings.displayLayoutSetting.value = layout;
for (const mode of ['min', 'medium', 'max']) {
const rect = g.rect(mode), row = g.switcherRect(mode);
const where = `${switcher} ${statusBar} ${layout} ${mode}`;
assert.ok(rect.x >= 0 && rect.y >= 0 && rect.x + rect.width <= 540 && rect.y + rect.height <= 220, where);
assert.ok(row.y >= 0 && row.y + row.height <= 220, where);
if (g.windowFramed()) assert.ok(rect.x - 1 - 31 >= 0 && rect.x + rect.width + 31 < 540, where);
}
}
}
}
// The G2's band is unchanged.
assert.equal(geometry().minWindowHeight(), 288);
});
test('on a RayNeo iO the chrome paints the screen\'s size, its strip and row within it', () => {
for (const position of ['left', 'right', 'bottom', 'popup']) {
const g = geometry({ width: 540, height: 220 });
g.settings.appSwitcherPositionSetting.value = position;
const { chrome, state } = chromeLayer(g, { windows: 3, notifications: 2, phoneBattery: 80 });
state.focus = 'sidebar';
const image = chrome.paint();
assert.deepEqual([image.width, image.height], [540, 220], position);
const parts = chrome.paintParts();
assert.ok(parts.length > 0, position);
for (const part of parts) {
assert.ok(part.x >= 0 && part.y >= 0 && part.x + part.image.width <= 540 && part.y + part.image.height <= 220,
`${position} part ${part.shellKey} at ${[part.x, part.y, part.image.width, part.image.height]}`);
}
// And it draws something there.
const lit = (x0, y0, w, h) => { for (let y = y0; y < y0 + h; y++) for (let x = x0; x < x0 + w; x++) if (image.pixels[y * 540 + x]) return true; return false; };
assert.ok(lit(0, 0, 540, 220), position);
}
});
test('each window edge is labelled by whether a border line runs along it, with padding only there', () => {
const g = geometry();
// top, right, bottom, left: B = a border line right outside it, o = nothing.
const edges = (appId) => {
const e = g.windowEdges(appId);
return [e.top, e.right, e.bottom, e.left].map((kind) => (kind === 'border' ? 'B' : 'o')).join('');
};
const set = (switcher, statusBar = 'top', border = 'all', visibility = 'always') => {
g.settings.appSwitcherPositionSetting.value = switcher;
g.settings.statusBarPositionSetting.value = statusBar;
g.settings.windowBorderSetting.value = border;
g.settings.statusBarVisibilitySetting.value = visibility;
};
// A side strip: the top bar's divider and the strip's separator; the
// other side and the bottom are the screen's edge or blank. The Border
// setting doesn't reach it.
set('left');
assert.equal(edges(), 'BooB');
set('right', 'top', 'none');
assert.equal(edges(), 'BBoo');
// A bottom row: the frame all round; with only the status line, the edge
// the status bar is past; with no border, nothing.
set('bottom');
assert.equal(edges(), 'BBBB');
set('bottom', 'bottom');
assert.equal(edges(), 'BBBB');
set('bottom', 'top', 'status');
assert.equal(edges(), 'Booo');
set('bottom', 'bottom', 'status');
assert.equal(edges(), 'ooBo');
set('bottom', 'top', 'none');
assert.equal(edges(), 'oooo');
set('bottom', 'bottom', 'none');
assert.equal(edges(), 'oooo');
// A popup: the frame, or the line by a status bar with rows of its own
// (one that only overlays has none).
set('popup');
assert.equal(edges(), 'BBBB');
set('popup', 'bottom', 'status');
assert.equal(edges(), 'ooBo');
set('popup', 'top', 'status');
assert.equal(edges(), 'Booo');
set('popup', 'top', 'status', 'switcher');
assert.equal(edges(), 'oooo');
set('popup', 'top', 'all', 'switcher');
assert.equal(edges(), 'BBBB');
set('popup', 'top', 'none');
assert.equal(edges(), 'oooo');
// Full Screen has no room for the frame's sides: the status bar's divider
// and the switcher row's separator are the lines left.
g.settings.displayLayoutSetting.value = 'fullscreen';
set('bottom');
assert.equal(edges(), 'BoBo');
set('bottom', 'bottom');
assert.equal(edges(), 'ooBo');
set('bottom', 'top', 'status');
assert.equal(edges(), 'Booo');
set('popup');
assert.equal(edges(), 'Booo');
set('popup', 'bottom');
assert.equal(edges(), 'ooBo');
// An immersive window has the whole panel.
g.settings.displayLayoutSetting.value = 'band-middle';
set('bottom');
g.setAppImmersive('onboarding', true);
assert.equal(edges('onboarding'), 'oooo');
assert.equal(edges('launcher'), 'BBBB');
set('left');
assert.equal(edges('onboarding'), 'oooo');
g.setAppImmersive('onboarding', false);
// The viewport a window lays out in carries the labels and the padding
// suggested for them: 8px beside a line, none beside blank space.
const plain = (value) => JSON.parse(JSON.stringify(value));
set('left');
assert.deepEqual(plain(g.appViewport('min')), {
width: 576, height: 260,
edges: { top: 'border', right: 'offscreen', bottom: 'offscreen', left: 'border' },
padding: { top: 8, right: 0, bottom: 0, left: 8 },
});
set('bottom', 'bottom', 'none');
assert.deepEqual(plain(g.appViewport('min').padding), { top: 0, right: 0, bottom: 0, left: 0 });
set('bottom', 'bottom');
assert.deepEqual(plain(g.appViewport('min').padding), { top: 8, right: 8, bottom: 8, left: 8 });
for (const mode of ['min', 'medium', 'max']) {
const { width, height } = g.appViewport(mode, 'terminal');
assert.deepEqual({ width, height }, { ...g.appViewportSize(mode, 'terminal') });
}
});
test('the phone mirror zooms to the area the shell uses, centred at the panel\'s 4:3', () => {
const g = geometry();
const preview = (mode, appId) => {
@@ -356,8 +531,9 @@ const shellScene = require('../.test-build/app/graphics/shell-scene.js');
const displayList = require('../.test-build/app/graphics/display-list.js');
const notificationTypes = require('../.test-build/app/native/notification-types.js');
const ICON = notificationTypes.NOTIFICATION_ICON_SIZE;
function chromeLayer(g, { notifications = 0, phoneBattery = null, windows = 1, trayIcons = [] } = {}) {
const font = { lineHeight: 12, ascent: 10, measureText: (text) => text.length * 4,
function chromeLayer(g, { notifications = 0, phoneBattery = null, windows = 1, trayIcons = [], glassesModel = 'G2' } = {}) {
const measuredText = [];
const font = { lineHeight: 12, ascent: 10, measureText: (text) => { measuredText.push(text); return text.length * 4; },
drawText: (image, x, y, text, value) => { for (let i = 0; i < text.length; i++) image.fillRect(x + i * 4, y + 2, 2, 6, value); } };
let nextKey = 100;
const requested = [];
@@ -378,7 +554,7 @@ function chromeLayer(g, { notifications = 0, phoneBattery = null, windows = 1, t
'../../native/phone-battery': { readPhoneBatteryState: () => ({ battery: phoneBattery, charging: false }) },
'../../util/render-freshness': { renderPassAllowsStaleData: () => false },
'../dashboard-settings': { batteryDisplayModeSetting: { get: () => 'icon' }, phoneBatteryVisibilitySetting: settings,
batteryIndicatorVisible: () => true },
glassesBatteryVisibilitySetting: settings, glassesModelName: () => glassesModel, batteryIndicatorVisible: () => true },
'../clock-format': { formatClockDate: () => 'Thu Oct 2', formatClockTime: () => '9:41' },
'../layers': { LayerStack: { allocateShellKey: () => nextKey++ } },
// As in menu.ts.
@@ -396,11 +572,19 @@ function chromeLayer(g, { notifications = 0, phoneBattery = null, windows = 1, t
};
let heightChanges = 0;
const chrome = new context.exports.ShellChromeLayer(() => state, () => heightChanges++);
return { chrome, state, requested, planBottomBar: context.exports.planBottomBar, heightChanges: () => heightChanges };
return { chrome, state, requested, measuredText, planBottomBar: context.exports.planBottomBar, heightChanges: () => heightChanges };
}
const notification = (key) => ({ kind: 'notification', key });
const keysOf = (items) => Array.from(items, (item) => item.key);
test('the top bar labels the glasses battery with the paired model', () => {
const { chrome, state, measuredText } = chromeLayer(geometry(), { glassesModel: 'G1' });
state.battery.headset = 72;
chrome.paintParts();
assert.ok(measuredText.includes('G1'));
assert.ok(!measuredText.includes('G2'));
});
test('a bottom app switcher frames the foreground window\'s content in a rounded border', () => {
const g = geometry();
g.settings.appSwitcherPositionSetting.value = 'bottom';
@@ -762,9 +946,9 @@ test('a popup switcher\'s status bar can sit at the bottom, show only in the swi
g.settings.statusBarPositionSetting.value = 'bottom';
[bar] = chrome.paintParts();
assert.deepEqual([bar.y, at(bar, 300, 356), at(bar, 300, 383)], [356, 40, 1]);
// Always shown without a frame, a divider still separates bar and window.
// Always shown with only the status line, a divider still separates bar and window.
g.settings.statusBarVisibilitySetting.value = 'always';
g.settings.windowBorderSetting.value = false;
g.settings.windowBorderSetting.value = 'status';
state.focus = 'window';
const parts = chrome.paintParts();
assert.deepEqual(Array.from(parts, (p) => p.shellKey), [2]);
@@ -772,6 +956,93 @@ test('a popup switcher\'s status bar can sit at the bottom, show only in the swi
g.settings.statusBarPositionSetting.value = 'top';
[bar] = chrome.paintParts();
assert.deepEqual([bar.y, at(bar, 300, 123)], [96, 40]);
// With no border the bar is all there is, at either edge.
g.settings.windowBorderSetting.value = 'none';
[bar] = chrome.paintParts();
assert.deepEqual([bar.y, at(bar, 300, 123)], [96, 1]);
g.settings.statusBarPositionSetting.value = 'bottom';
assert.deepEqual(Array.from(chrome.paintParts(), (p) => [p.shellKey, p.y, at(p, 300, 356)]), [[2, 356, 1]]);
});
test('a bottom app switcher\'s border can be just the line by the status bar, or nothing', () => {
const g = geometry();
g.settings.appSwitcherPositionSetting.value = 'bottom';
g.settings.statusBarPositionSetting.value = 'bottom';
g.settings.windowBorderSetting.value = 'status';
const { chrome, state } = chromeLayer(g, { windows: 2 });
state.selectedIndex = 1;
const frames = (parts) => parts.flatMap((p) => p.image.draws.filter((d) => d.presentation?.displayList));
const at = (p, x, y) => p.image.pixels[(y - p.y) * p.image.width + x - p.x];
// With the status bar in the row, the line is the row's top edge, straight
// across the window's width; no frame, so the content takes the header's
// row and the row's surface is no wider than the window.
assert.deepEqual(g.rect('min'), { x: 32, y: 78, width: 576, height: 288 });
let parts = chrome.paintParts();
let [row] = parts;
assert.equal(frames(parts).length, 0);
assert.deepEqual([row.x, row.y, row.image.width], [32, 366, 576]);
assert.deepEqual([at(row, 32, 366), at(row, 300, 366), at(row, 607, 366)], [40, 40, 40]);
// The first cell (the launcher's) starts at the row's end, in line with
// the window's edge, and the selection is still a tab diverting the line.
assert.deepEqual([chrome.windowIndexAt(31, 380, 2), chrome.windowIndexAt(32, 380, 2), chrome.windowIndexAt(72, 380, 2)], [null, 0, 1]);
assert.deepEqual([at(row, 71, 366), at(row, 72, 366), at(row, 90, 366), at(row, 72, 380)], [40, 150, 1, 150]);
// The launcher's own tab runs down the window's edge from the line's start.
state.selectedIndex = 0;
[row] = chrome.paintParts();
assert.deepEqual([at(row, 32, 366), at(row, 32, 380), at(row, 50, 366), at(row, 68, 366)], [150, 150, 1, 40]);
state.selectedIndex = 1;
// The flat paint has no frame either.
assert.equal(chrome.paint().draws.filter((d) => d.presentation?.displayList).length, 0);
// With the status bar on top, the line is the top bar's divider, and the
// row has none: its selection is a box, as in a second row.
g.settings.statusBarPositionSetting.value = 'top';
assert.deepEqual(g.rect('min'), { x: 32, y: 106, width: 576, height: 260 });
parts = chrome.paintParts();
let bar;
[bar, row] = parts;
assert.deepEqual([Array.from(parts, (p) => p.shellKey), frames(parts).length], [[2, 1], 0]);
assert.deepEqual([at(bar, 32, 105), at(bar, 607, 105)], [40, 40]);
assert.deepEqual([at(row, 32, 366), at(row, 300, 366)], [1, 1]);
assert.deepEqual([at(row, 90, 366), at(row, 72, 380), at(row, 90, 401)], [150, 150, 150]);
// Focused, the box fills round the icon.
state.focus = 'sidebar';
[, row] = chrome.paintParts();
assert.deepEqual([at(row, 73, 380), at(row, 90, 380)], [255, 120]);
state.focus = 'window';
// No border: neither line, wherever the status bar is.
g.settings.windowBorderSetting.value = 'none';
[bar, row] = chrome.paintParts();
assert.deepEqual([at(bar, 300, 105), at(row, 300, 366), at(row, 90, 366)], [1, 1, 150]);
g.settings.statusBarPositionSetting.value = 'bottom';
parts = chrome.paintParts();
[row] = parts;
assert.deepEqual([frames(parts).length, at(row, 32, 366), at(row, 300, 366), at(row, 90, 366)], [0, 1, 1, 150]);
assert.deepEqual(g.rect('min'), { x: 32, y: 78, width: 576, height: 288 });
assert.deepEqual([chrome.windowIndexAt(31, 380, 2), chrome.windowIndexAt(32, 380, 2)], [null, 0]);
// A Full Screen window's row starts its cells at the screen's edge.
g.settings.displayLayoutSetting.value = 'fullscreen';
chrome.paintParts();
assert.deepEqual([chrome.windowIndexAt(0, 460, 2), chrome.windowIndexAt(40, 460, 2)], [0, 1]);
g.settings.displayLayoutSetting.value = 'band-middle';
// All around, a Full Screen window has no room for a frame and keeps its
// straight lines, as beside a side strip.
g.settings.windowBorderSetting.value = 'all';
g.settings.statusBarPositionSetting.value = 'top';
g.settings.displayLayoutSetting.value = 'fullscreen';
[bar, row] = chrome.paintParts();
assert.deepEqual([at(bar, 300, 27), at(row, 0, 444), at(row, 639, 444)], [40, 40, 40]);
// Its cells keep their margin from the row's end.
assert.deepEqual([chrome.windowIndexAt(7, 460, 2), chrome.windowIndexAt(8, 460, 2)], [null, 0]);
// Scrolled past the launcher, a row without the frame keeps the left
// chevron's margin too.
const narrow = geometry();
narrow.settings.appSwitcherPositionSetting.value = 'bottom';
narrow.settings.windowBorderSetting.value = 'none';
const scrolled = chromeLayer(narrow, { windows: 40 });
scrolled.state.selectedIndex = 39;
scrolled.chrome.paintParts();
assert.equal(scrolled.chrome.windowIndexAt(35, 380, 40), null);
assert.notEqual(scrolled.chrome.windowIndexAt(41, 380, 40), null);
});
test('a notification the switcher selects sits in a box, filled with its icon inverted while the switcher has focus', () => {
@@ -874,33 +1145,42 @@ test('with the selection out on a notification, the windows stay scrolled as the
});
// Exercise the real worker message handler with platform work stubbed out.
function resizeHandler(file, globals) {
// `own` names the worker's own functions and constants the handler runs with.
function resizeHandler(file, globals, own = []) {
const source = ts.createSourceFile(file, read(file), ts.ScriptTarget.Latest, true);
const handler = source.statements.find((s) => ts.isExpressionStatement(s)
&& ts.isBinaryExpression(s.expression) && s.expression.left.getText(source) === 'global.onmessage');
assert.ok(handler);
const named = source.statements.filter((s) => ts.isFunctionDeclaration(s) && own.includes(s.name?.text)
|| ts.isVariableStatement(s) && s.declarationList.declarations.some((d) => own.includes(d.name.getText(source))));
assert.equal(named.length, own.length);
const context = { global: {}, ...globals };
vm.runInNewContext(js(handler.getText(source)), context);
vm.runInNewContext(js([...named, handler].map((s) => s.getText(source)).join('\n')), context);
return (viewport) => context.global.onmessage({ data: { type: 'resize-window', windowId: 'test', viewport } });
}
test('Navigate resize preserves its active route and window, and remeasures an open menu', () => {
let paints = 0;
let menuSize;
const window = { windowId: 'test', viewportWidth: 640, viewportHeight: 452,
const window = { windowId: 'test', viewportWidth: 640, viewportHeight: 452, viewport: bordered(640, 452),
menu: { resize: (size) => { menuSize = size; } }, lastSubmittedFingerprint: 'old' };
const route = { destination: 'Home' };
const resize = resizeHandler('app/apps/navigate/navigate-app.worker.ts', {
window, route, phase: 'navigating', render: () => { paints++; },
window, route, phase: 'navigating', render: () => { paints++; }, sameViewport,
});
resize({ width: 576, height: 260 });
resize(bordered(576, 260));
assert.equal(window.viewportHeight, 260);
assert.equal(window.viewportWidth, 576);
assert.deepEqual(window.viewport, bordered(576, 260));
assert.equal(window.lastSubmittedFingerprint, '');
assert.equal(route.destination, 'Home');
assert.deepEqual(menuSize, { width: 576, height: 260 });
resize({ width: 576, height: 260 });
assert.deepEqual(menuSize, bordered(576, 260));
resize(bordered(576, 260));
assert.equal(paints, 1);
// What the edges touch changing is a new layout too, at the same size.
resize(edged(576, 260, 'top'));
assert.equal(paints, 2);
assert.deepEqual(menuSize, edged(576, 260, 'top'));
});
test('Terminal resize keeps the view identity, negotiates its grid and skips position-only changes', () => {
@@ -911,7 +1191,7 @@ test('Terminal resize keeps the view identity, negotiates its grid and skips pos
client: { stop: () => calls.push('stop'), setViewport: (...size) => calls.push(size) },
};
const resize = resizeHandler('app/apps/terminal/terminal-app.worker.ts', {
windows: new Map([['test', window]]), clearTimeout: (timer) => calls.push(timer),
windows: new Map([['test', window]]), pendingHeightChanges: new Map(), clearTimeout: (timer) => calls.push(timer),
TerminalEmulator: class { constructor(cols, rows) { this.cols = cols; this.rows = rows; } },
reconnectView: (view) => { assert.equal(view, window); calls.push('reconnect'); },
scheduleRender: () => calls.push('paint'),
@@ -944,13 +1224,17 @@ test('the games and Roam resize in place, keeping their state, and repaint when
}
let paints = 0, viewSize;
const window = { windowId: 'test', viewportWidth: 576, viewportHeight: 443, menu: null, lastSubmittedFingerprint: 'old',
view: { resize: (...size) => { viewSize = size; } } };
const resize = resizeHandler('app/apps/roam/roam-app.worker.ts', {
window, render: () => paints++, DOC_MARGIN: 6, HEADER_HEIGHT: 30, BOTTOM_MARGIN: 6,
});
resize({ width: 576, height: 415 });
// The document re-wraps for the region under its header.
assert.deepEqual([window.viewportHeight, viewSize, window.lastSubmittedFingerprint, paints], [415, [564, 379], '', 1]);
view: { resize: (size) => { viewSize = { ...size }; } } };
const resize = resizeHandler('app/apps/roam/roam-app.worker.ts', { window, render: () => paints++ },
['HEADER_HEIGHT', 'headerBottom', 'documentBox']);
resize(bordered(576, 415));
// The document re-wraps for the region under its header: between the side
// padding and down to the window's bottom edge, whose padding goes inside it.
assert.deepEqual([window.viewportHeight, viewSize, window.lastSubmittedFingerprint, paints],
[415, { x: 8, y: 32, width: 560, height: 383, padBottom: 8 }, '', 1]);
assert.deepEqual(window.viewport, bordered(576, 415));
resize(unbordered(576, 415));
assert.deepEqual(viewSize, { x: 0, y: 24, width: 576, height: 391, padBottom: 0 });
});
test('resizing a terminal reconnects with the new grid and ignores callbacks from the old socket', () => {
+10
View File
@@ -0,0 +1,10 @@
// Compile the watch app's platform-independent Swift for the Mac and run its checks.
const { execFileSync } = require('node:child_process');
const { mkdtempSync } = require('node:fs');
const { tmpdir } = require('node:os');
const { join, resolve } = require('node:path');
const root = resolve(__dirname, '..'), dir = mkdtempSync(join(tmpdir(), 'faceclaw-apple-watch-'));
const src = 'apple-watch/Faceclaw';
execFileSync('xcrun', ['swiftc', '-O', `${src}/Protocol.swift`, `${src}/PadStatus.swift`, `${src}/PadGestureRecognizer.swift`,
'tests/native/apple-watch/main.swift', '-o', join(dir, 'apple-watch-test')], { cwd: root, stdio: 'inherit' });
execFileSync(join(dir, 'apple-watch-test'), [], { stdio: 'inherit' });
+24 -2
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,
@@ -60,7 +61,6 @@ function settingsEnv(initialUrl = '', initialModel = 'openai-compatible') {
deleteAsrModel() {}, startAsrModelDownload() {}, onAsrModelStateChanged: () => () => {},
},
'../../apps/files/text-viewer': { TextViewerLayer: class {} },
'../../analytics/privacy-disclosure': { IOS_PRIVACY_NOTICE: '' },
'../menu': {
drawRightValueMenuItem() {}, openModalMenu() {},
submenuItem: label => ({ label, onSelect() {} }),
@@ -73,6 +73,9 @@ function settingsEnv(initialUrl = '', initialModel = 'openai-compatible') {
openSettingsSubMenu() {},
},
'../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' }),
},
@@ -83,6 +86,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; },
@@ -148,3 +152,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);
});
-2
View File
@@ -12,7 +12,6 @@ const {
MINIMUM_METERS,
MAXIMUM_METERS,
zoneFromMeters,
zoneLabel,
sortedByProximity,
} = require("../.test-build/app/g2/ble-proximity.js");
@@ -59,7 +58,6 @@ test("zones and their boundaries", () => {
assert.equal(zoneFromMeters(3), "far");
assert.equal(zoneFromMeters(9.9), "far");
assert.equal(zoneFromMeters(10), "distant");
assert.equal(zoneLabel("immediate"), "In your hand");
});
test("weak signals carry lower confidence", () => {
+16 -1
View File
@@ -1,6 +1,6 @@
const { test } = require('node:test');
const assert = require('node:assert/strict');
const { normalizeBrightnessCurve, brightnessCurveError, DEFAULT_BRIGHTNESS_CURVE } = require('../.test-build/app/g2/brightness-curve.js');
const { normalizeBrightnessCurve, brightnessCurveError, DEFAULT_BRIGHTNESS_CURVE, G1_BRIGHTNESS_CURVE, RAYNEO_IO_BRIGHTNESS_CURVE } = require('../.test-build/app/g2/brightness-curve.js');
test('brightness curve accepts monotone knots and canonicalizes spacing', () => {
assert.equal(normalizeBrightnessCurve(' 0:0, 2.5:20,1000:100 '), '0:0,2.5:20,1000:100');
});
@@ -43,6 +43,21 @@ test('the screen fade speed scales the 280 ms on/off fade, or makes it instant',
const fadeAt = (speed) => { api.screenFadeSetting.set(speed); return api.getBrightnessPreferences().fadeMs; };
assert.deepEqual(['very-fast', 'fast', 'normal', 'slow', 'disabled'].map(fadeAt), [56, 140, 280, 560, 0]);
});
test('an unset curve follows the paired glasses; a chosen one stays', () => {
const store = new Map();
const api = settingsFixture(store);
assert.equal(api.getBrightnessPreferences().curve, DEFAULT_BRIGHTNESS_CURVE);
store.set('deviceType', 'rayneo-io');
assert.equal(api.getBrightnessPreferences().curve, RAYNEO_IO_BRIGHTNESS_CURVE);
assert.equal(brightnessCurveError(RAYNEO_IO_BRIGHTNESS_CURVE), null);
store.set('deviceType', 'g1');
assert.equal(api.getBrightnessPreferences().curve, G1_BRIGHTNESS_CURVE);
assert.equal(brightnessCurveError(G1_BRIGHTNESS_CURVE), null);
api.autoBrightnessCurveSetting.set('0:0,2:50,8:100');
assert.equal(api.getBrightnessPreferences().curve, '0:0,2:50,8:100');
store.set('deviceType', 'g2');
assert.equal(api.getBrightnessPreferences().curve, '0:0,2:50,8:100');
});
test('curve drafts preview exactly while brightness keeps the previous valid saved curve', () => {
const store = new Map([['display.autoBrightnessCurve', '0:0,2:50,8:100']]);
let api = settingsFixture(store), curve = api.autoBrightnessCurveSetting;
+19 -1
View File
@@ -23,11 +23,13 @@ const { BdfFont } = load('app/graphics/bdffont.ts');
const font = BdfFont.parse(fs.readFileSync(path.join(__dirname, '../app/fonts/terminus/ter-u18n.bdf'), 'utf8'));
const fonts = { getDefaultSmallFont: () => font };
const layers = load('app/ui/layers.ts', {
'./viewport': require('../.test-build/app/ui/viewport.js'),
'../graphics/image': graphics, '../graphics/plane': plane, './gestures': gestures,
'../native/frame-timings': { spanCurrent: (_name, paint) => paint() },
'../graphics/screen-size': { screenWidth: () => 640, screenHeight: () => 480 },
});
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 },
'./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 },
'./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'),
'../graphics/image': graphics, '../graphics/ui-fonts': fonts, '../graphics/textwrap': textwrap,
'../util/numeric-util': numeric, './gestures': gestures, './metrics': load('app/ui/metrics.ts'),
@@ -108,6 +110,22 @@ test('the thin bar fills upward in proportion to brightness in each viewport', (
}
});
test('in a short window (a RayNeo iO\'s) the bar shrinks so the dialog stays inside it', () => {
for (const viewport of [{ x: 64, y: 28, width: 476, height: 192 }, { x: 32, y: 1, width: 476, height: 155 }]) {
const f = fixture('100', viewport);
const image = f.stack.paint().at(-1).image;
// The dialog's border (110) and the full bar (255), all within the window.
let top = Infinity, bottom = -1, fill = 0;
for (let y = 0; y < image.height; y++) for (let x = 0; x < image.width; x++) {
const value = image.getPixel(x, y);
if (value === 110) { top = Math.min(top, y); bottom = Math.max(bottom, y); }
if (value === 255) fill++;
}
assert.ok(top >= viewport.y && bottom < viewport.y + viewport.height, `dialog ${top}..${bottom} in ${JSON.stringify(viewport)}`);
assert.ok(fill > 0 && fill < 12 * 100, 'a shorter bar, still drawn');
}
});
test('system menu hides brightness in Auto and opens its picker between Voice input and Debug', async () => {
const f = fixture('auto');
const { shell } = load('app/ui/shell/shell.ts', {
+1 -1
View File
@@ -9,7 +9,7 @@ test('iOS game worker forwards an immutable byte payload through its host and ig
const load = loader({ global: { isIOS: true, postMessage: data => worker.onmessage({ data }) } }, {
'../../graphics/image': {}, './chrome-layer': { windowIcon() {} },
'../../assistant/tool-registry': { toolRegistry: { removeAppTools() {} } },
'./geometry': { appViewportSize: () => ({ width: 576, height: 260 }) },
'./geometry': { appViewport: () => ({ width: 576, height: 260 }) },
'../../native/frame-timings': {}, './worker-state': {},
'./shell': { shell: { registerWindow() {} } },
});
+45 -15
View File
@@ -4,6 +4,7 @@ const fs = require('node:fs');
const path = require('node:path');
const vm = require('node:vm');
const ts = require('typescript');
const { bordered, unbordered, edged } = require('./helpers/viewport.cjs');
function load(file, modules = {}, globals = {}) {
const source = fs.readFileSync(path.join(__dirname, '..', file), 'utf8');
@@ -23,7 +24,7 @@ const font = (size) => BdfFont.parse(fs.readFileSync(path.join(__dirname, `../ap
const small = font(18), large = font(32);
const diagnostics = { magneticAccuracy: 3, magneticAnomalies: 2, orientationSource: 3, flags: 0x8c, sampleTimeMs: 1234 };
function compass(width = 576, height = 260) {
function compass(width = 576, height = 260, glasses = 'g2', viewport = bordered(width, height)) {
let listener, window;
class RecordingImage extends graphics.GrayImage {
texts = [];
@@ -55,14 +56,16 @@ function compass(width = 576, height = 260) {
},
'../../ui/shell/shell': { shell: { isWindowVisible: () => true } },
'../../native/location-permissions': { hasLocationPermission: () => false },
'../../ui/dashboard-settings': { glassesHaveCompass: () => glasses === 'g2' },
'./calibration': calibration,
'./calibration-layer': {}, './declination': { onDeclinationChanged: () => () => {} },
'./heading': { getNorthReference: () => 'magnetic', resolveHeading: (v) => ({ displayDegrees: v }) },
}, { setInterval: () => 1, clearInterval() {} });
const ctx = { stack: { getBaseSize: () => ({ width, height }), pop() {} } };
const ctx = { stack: { getBaseSize: () => viewport, pop() {} } };
app.createCompassAppWindow({ onClosed() {} });
return { menu: () => window.menuItems(),
paint: () => window.baseLayer.paint(ctx),
tap: () => window.baseLayer.handleInput({ type: 'click' }, ctx),
heading: (info) => listener({ command: 15, headingDegrees: 359, diagnostics: info }) };
}
@@ -94,19 +97,46 @@ test('calibration dots track firmware magnetic accuracy and never go stale', ()
});
test('calibration readout fits the viewport without overlapping the heading or status', () => {
for (const [width, height] of [[240, 260], [320, 260], [576, 260], [640, 260], [576, 288], [576, 140]]) {
const app = compass(width, height); app.heading({ ...diagnostics, magneticAccuracy: 2 });
const image = app.paint();
const label = calibrationText(image);
assert.ok(label.x >= 0 && label.y >= 0, `${width}x${height}`);
assert.ok(label.x + label.width <= width && label.y + label.height <= image.height, `${width}x${height}`);
for (const other of image.texts.filter((t) => t !== label)) {
assert.ok(!overlaps(label, other), `${width}x${height}: overlaps "${other.text}"`);
}
if (process.env.COMPASS_PREVIEW && width === 576 && height === 260) {
fs.writeFileSync(process.env.COMPASS_PREVIEW, Buffer.concat([
Buffer.from(`P5\n${image.width} ${image.height}\n255\n`), Buffer.from(image.withDrawsBaked().pixels),
]));
for (const [width, height] of [[240, 260], [320, 260], [576, 260], [640, 260], [576, 288], [576, 140], [476, 155]]) {
for (const viewport of [bordered(width, height), unbordered(width, height), edged(width, height, 'top', 'left'), edged(width, height, 'bottom')]) {
const where = `${width}x${height} ${JSON.stringify(viewport.padding)}`;
const app = compass(width, height, 'g2', viewport); app.heading({ ...diagnostics, magneticAccuracy: 2 });
const image = app.paint();
const label = calibrationText(image);
const { padding } = viewport;
// Every line of text keeps inside the padding of the window's edges.
for (const text of image.texts) {
assert.ok(text.x >= padding.left && text.y >= padding.top, `${where}: "${text.text}"`);
assert.ok(text.x + text.width <= width - padding.right && text.y + text.height <= height - padding.bottom, `${where}: "${text.text}"`);
}
for (const other of image.texts.filter((t) => t !== label)) {
assert.ok(!overlaps(label, other), `${where}: overlaps "${other.text}"`);
}
if (process.env.COMPASS_PREVIEW && width === 576 && height === 260 && viewport.padding.top && viewport.padding.right) {
fs.writeFileSync(process.env.COMPASS_PREVIEW, Buffer.concat([
Buffer.from(`P5\n${image.width} ${image.height}\n255\n`), Buffer.from(image.withDrawsBaked().pixels),
]));
}
}
}
});
test('the calibration readout sits in the top-right corner, at the padding of those edges', () => {
const corner = (viewport) => {
const label = calibrationText(compass(576, 260, 'g2', viewport).paint());
return [label.x + label.width, label.y];
};
assert.deepEqual(corner(bordered(576, 260)), [568, 8]);
assert.deepEqual(corner(unbordered(576, 260)), [576, 0]);
assert.deepEqual(corner(edged(576, 260, 'top')), [576, 8]);
});
test('glasses without a magnetometer say so and offer no calibration', () => {
const app = compass(576, 260, 'g1');
const image = app.paint();
assert.ok(image.texts.some((t) => t.text === 'These glasses have no compass sensor'));
assert.equal(calibrationText(image), undefined);
assert.equal(app.menu().length, 0);
// ctx.stack has no push or topMatches here, so opening calibration would throw.
app.tap();
});
+9 -2
View File
@@ -4,6 +4,7 @@ const fs = require('node:fs');
const path = require('node:path');
const vm = require('node:vm');
const ts = require('typescript');
const { bordered, unbordered } = require('./helpers/viewport.cjs');
function load(file, modules = {}, extra = '', globals = {}) {
const source = fs.readFileSync(path.join(__dirname, '..', file), 'utf8') + extra;
@@ -44,6 +45,7 @@ const compass = load('app/apps/compass/compass-app.ts', {
'../../ui/shell/in-process-window': {},
'../../ui/shell/shell': {},
'../../native/location-permissions': {},
'../../ui/dashboard-settings': { glassesHaveCompass: () => true },
'./calibration': calibration,
'./calibration-layer': {},
'./compass-rose': rose,
@@ -123,7 +125,8 @@ test('heading updates reuse the texture; brightness, layout and clipping changes
const layer = new compass.CompassLayer(() => {});
layer.enabled = true;
let height = 260;
const ctx = { stack: { getBaseSize: () => ({ width: viewportWidth, height }) } };
let viewport = bordered;
const ctx = { stack: { getBaseSize: () => viewport(viewportWidth, height) } };
const texture = () => layer.paint(ctx).draws.find((draw) => draw.kind === 'image').source;
const waiting = texture();
layer.rawHeading = 0;
@@ -142,7 +145,11 @@ test('heading updates reuse the texture; brightness, layout and clipping changes
const wide = texture();
assert.notEqual(wide, tall);
layer.firmwareStatus = 'Calibration status '.repeat(12);
assert.notEqual(texture(), wide);
const wrapped = texture();
assert.notEqual(wrapped, wide);
// Losing the borders moves the rose toward the edges, at the same size.
viewport = unbordered;
assert.notEqual(texture(), wrapped);
viewportWidth = 576;
});
+7
View File
@@ -41,6 +41,13 @@ with tempfile.TemporaryDirectory() as d:
try: m.read_document(p)
except (ValueError, m.ET.ParseError): pass
else: raise AssertionError(f'accepted invalid config {invalid!r}')
pulled = {'schema': 2, 'platform': 'ios', 'settings': {'onboarding.lookalike': True, 'display.timeFormat': '24h'}, 'platformSettings': {
'onboarding.complete': True, 'onboarding.previewOnly': False, 'onboarding.glassesFinishedVersion': '0.8.6', 'onboarding.step': 3, 'deviceAddress.left': 'AA'}}
reset = json.loads(ios.onboarding_reset_document(json.dumps(pulled)))
assert reset == {'schema': 2, 'platform': 'ios', 'settings': {}, 'platformSettings': {
'onboarding.complete': False, 'onboarding.glassesFinishedVersion': '', 'onboarding.step': 0}}, reset
assert m.check_document(json.dumps(reset)) == (2, 'ios', 3)
assert ios.onboarding_reset_document(json.dumps({**pulled, 'platformSettings': {'onboarding.complete': False, 'onboarding.glassesFinishedVersion': '', 'deviceAddress.left': 'AA'}})) is None
`], { stdio: 'inherit' });
});
+1
View File
@@ -22,6 +22,7 @@ const { BdfFont } = load('app/graphics/bdffont.ts');
const font = (name) => BdfFont.parse(fs.readFileSync(path.join(__dirname, `../app/fonts/terminus/${name}.bdf`), 'utf8'));
const fonts = { getDefaultSmallFont: () => font('ter-u12n'), getDefaultMediumFont: () => font('ter-u16n') };
const layers = load('app/ui/layers.ts', {
'./viewport': require('../.test-build/app/ui/viewport.js'),
'../graphics/image': graphics, '../graphics/plane': plane, './gestures': gestures,
'../native/frame-timings': { spanCurrent: (_name, paint) => paint() },
});
+72
View File
@@ -35,6 +35,33 @@ test('the display mode starts as the middle band, or carries over the old mode a
assert.equal(api.displayLayoutSetting.get(), 'band-bottom');
});
test('the border starts all around, or as the status line a popup switcher\'s old Window border toggle left', () => {
const border = (entries) => settingsFixture(new Map(entries)).windowBorderSetting.get();
assert.equal(border([]), 'all');
assert.equal(border([['display.appSwitcherPosition', 'popup']]), 'all');
assert.equal(border([['display.appSwitcherPosition', 'popup'], ['display.windowBorder', true]]), 'all');
assert.equal(border([['display.appSwitcherPosition', 'popup'], ['display.windowBorder', false]]), 'status');
// A bottom row's windows had the border whatever the toggle said.
assert.equal(border([['display.windowBorder', false]]), 'all');
// Once set, the setting is its own, for either switcher.
const store = new Map([['display.appSwitcherPosition', 'popup'], ['display.windowBorder', false]]);
const api = settingsFixture(store);
assert.equal(api.windowBorderSetting.displayValue(), 'Status line');
api.windowBorderSetting.set('none');
assert.equal(store.get('display.border'), 'none');
api.appSwitcherPositionSetting.set('bottom');
assert.equal(api.windowBorderSetting.get(), 'none');
assert.deepEqual(Array.from(api.windowBorderSetting.values, (value) => api.windowBorderSetting.displayValue(value)), ['All around', 'Status line', 'None']);
});
test('the glasses model name follows the paired glasses type', () => {
const model = (entries) => settingsFixture(new Map(entries)).glassesModelName();
assert.equal(model([]), 'G2');
assert.equal(model([['deviceType', 'g2']]), 'G2');
assert.equal(model([['deviceType', 'g1']]), 'G1');
assert.equal(model([['deviceType', 'rayneo-io']]), 'iO');
});
test('per-app display modes carry over their old mode and position where the merged values can say it', () => {
const app = (appId, entries) => settingsFixture(new Map(entries))[`${appId}DisplayLayoutSetting`].get();
assert.equal(app('navigate', []), 'global');
@@ -64,3 +91,48 @@ test('Full Screen is offered only with a bottom or popup app switcher', () => {
assert.deepEqual(Array.from(api.DISPLAY_LAYOUT_VALUES, (value) => api.displayLayoutLabel(value)),
['Band · Top', 'Band · High', 'Band · Middle', 'Band · Low', 'Band · Bottom', 'Tall', 'Full Screen']);
});
test('a RayNeo iO defaults the app switcher to the left, and keeps a chosen position and the band', () => {
const store = new Map();
const api = settingsFixture(store);
assert.equal(api.appSwitcherPositionSetting.get(), 'bottom');
api.glassesTypeSetting.set('rayneo-io');
assert.equal(api.appSwitcherPositionSetting.get(), 'left');
assert.equal(api.displayLayoutSetting.get(), 'band-middle');
assert.equal(store.has('display.appSwitcherPosition'), false);
// A stored position is the user's, on either glasses.
api.appSwitcherPositionSetting.set('right');
assert.equal(api.appSwitcherPositionSetting.get(), 'right');
api.glassesTypeSetting.set('g2');
assert.equal(api.appSwitcherPositionSetting.get(), 'right');
store.delete('display.appSwitcherPosition');
assert.equal(api.appSwitcherPositionSetting.get(), 'bottom');
// An unknown stored value falls back to the glasses' default.
store.set('display.appSwitcherPosition', 'sideways');
api.glassesTypeSetting.set('rayneo-io');
assert.equal(api.appSwitcherPositionSetting.get(), 'left');
});
test('a RayNeo iO always takes the R1 directly, leaving the G2\'s ring connection setting alone', () => {
const store = new Map();
const api = settingsFixture(store);
assert.equal(api.effectiveRingConnectionMode(), 'glasses');
api.ringConnectionModeSetting.set('health');
assert.equal(api.effectiveRingConnectionMode(), 'health');
api.glassesTypeSetting.set('rayneo-io');
assert.equal(api.effectiveRingConnectionMode(), 'direct');
assert.equal(api.ringConnectionModeSetting.get(), 'health');
api.glassesTypeSetting.set('g2');
assert.equal(api.effectiveRingConnectionMode(), 'health');
});
test('only a G2 has a compass', () => {
const api = settingsFixture();
assert.equal(api.glassesHaveCompass(), true);
api.glassesTypeSetting.set('g1');
assert.equal(api.glassesHaveCompass(), false);
api.glassesTypeSetting.set('rayneo-io');
assert.equal(api.glassesHaveCompass(), false);
api.glassesTypeSetting.set('g2');
assert.equal(api.glassesHaveCompass(), true);
});
+113
View File
@@ -162,3 +162,116 @@ test("a bare R1 substring is not a ring — only the stock EVEN R1 prefix is", (
const ad = classifyAdvertisement(raw({ address: "DA:7F:D2:B5:6E:E2", name: "EVEN R1_B56EE2", manufacturerData: "" }));
assert.equal(ad.role, "ring");
});
// RayNeo iO-2229 as captured 2026-11-01: company 0x5716 + [07][MAC]; pairing mode omits the data.
test("RayNeo iO advertisements classify as one device with its MAC", () => {
const io = classifyAdvertisement(raw({
address: "00:6A:5E:5F:33:C2",
name: "RayNeo iO-2229",
manufacturerData: "1657" + "07006a5e5f33c2130000000000000001",
}));
assert.equal(io.role, "rayneo-io");
assert.equal(io.embeddedMac, "00:6A:5E:5F:33:C2");
assert.equal(io.embeddedMacMismatch, false);
assert.equal(io.serial, null);
const pairing = classifyAdvertisement(raw({ address: "00:6A:5E:5F:33:C2", name: "RayNeo iO-2229", manufacturerData: "" }));
assert.equal(pairing.role, "rayneo-io");
assert.equal(pairing.embeddedMac, null);
// G2 advertisements are untouched
assert.equal(classifyAdvertisement(raw({})).role, "left");
});
// ---------------------------------------------------------------------------
// Even G1
// ---------------------------------------------------------------------------
const g1Ascii = (text) => Array.from(text).map((c) => c.charCodeAt(0).toString(16).padStart(2, "0")).join("");
const g1Raw = (name, manufacturerData, address = "F4:27:E9:CA:E8:3F") => ({
address,
name,
manufacturerData,
rssi: -50,
txPower: null,
connectable: true,
bonded: false,
source: "scan",
seenAtMs: 1000,
});
test("a G1 arm on Even's firmware: side from the name, serial after the side byte", () => {
const ad = classifyAdvertisement(g1Raw("Even G1_70_L_EF50E5", "02" + g1Ascii("S110LABC030139") + "0000000000"));
assert.equal(ad.role, "left");
assert.equal(ad.g1Firmware, "stock");
assert.equal(ad.serial, "S110LABC030139");
assert.equal(ad.embeddedMac, null);
});
test("a G1 arm on g1v0: the name says all, the serial is in the 0xFFFF record", () => {
const ad = classifyAdvertisement(g1Raw("G1v0_R", "ffff" + "01" + "04" + g1Ascii("S110LABC030139")));
assert.equal(ad.role, "right");
assert.equal(ad.g1Firmware, "g1v0");
assert.equal(ad.serial, "S110LABC030139");
assert.equal(classifyAdvertisement(g1Raw("G1v0S_L", "")).g1Firmware, "g1v0-safe");
assert.equal(classifyAdvertisement(g1Raw("G1v0S_L", "")).serial, null);
});
const g1Record = (flags, version, serial = "S110LABC030139") => "ffff" + "02" + flags + version + g1Ascii(serial);
test("a G1 arm on g1v0 0.5.66+: its name tells pairs apart, its record says what it runs", () => {
const ad = classifyAdvertisement(g1Raw("G1CFW_L_EF50E5", g1Record("05", "000542")));
assert.equal(ad.role, "left");
assert.equal(ad.g1Firmware, "g1v0");
assert.equal(ad.g1Version, "0.5.66");
assert.equal(ad.serial, "S110LABC030139");
assert.equal(classifyAdvertisement(g1Raw("G1CFW_R_CBE689", g1Record("04", "000542"))).role, "right");
});
test("a G1 arm in safe mode says so in its record, not its name", () => {
const ad = classifyAdvertisement(g1Raw("G1CFW_L_EF50E5", g1Record("0d", "000542")));
assert.equal(ad.g1Firmware, "g1v0-safe");
assert.equal(ad.g1Version, "0.5.66");
});
test("a G1 arm whose record hasn't arrived yet still shows, without a version or serial", () => {
const ad = classifyAdvertisement(g1Raw("G1CFW_L_EF50E5", ""));
assert.equal(ad.g1Firmware, "g1v0");
assert.equal(ad.g1Version, undefined);
assert.equal(ad.serial, null);
});
test("g1v0 before 0.5.66 and Even's firmware advertise no version", () => {
assert.equal(classifyAdvertisement(g1Raw("G1v0_R", "ffff" + "01" + "04" + g1Ascii("S110LABC030139"))).g1Version, undefined);
assert.equal(classifyAdvertisement(g1Raw("Even G1_70_L_EF50E5", "02" + g1Ascii("S110LABC030139"))).g1Version, undefined);
});
test("a G1 pair joins on its serial across firmware, so an install in progress stays one pair", () => {
const stock = classifyAdvertisement(g1Raw("Even G1_70_L_EF50E5", "02" + g1Ascii("S110LABC030139") + "0000000000"));
const custom = classifyAdvertisement(g1Raw("G1CFW_R_CBE689", g1Record("04", "000542")));
assert.equal(stock.serial, custom.serial);
});
test("on iOS a G1 arm gets an address from its peripheral identifier, and is found at all", () => {
const { identifyIosPeripheral, g1IosAddress } = require("../.test-build/app/g2/ios-peripheral-identity.js");
const identifier = "14b99254-0798-d1a8-3eec-b141bdabfae8";
assert.equal(g1IosAddress(identifier), "14:B9:92:54:07:98");
for (const [name, manufacturerData, firmware] of [
["G1CFW_L_EF50E5", g1Record("05", "000542"), "g1v0"],
["Even G1_70_L_EF50E5", "02" + g1Ascii("S110LABC030139"), "stock"],
["G1v0_L", "ffff" + "01" + "05" + g1Ascii("S110LABC030139"), "g1v0"],
]) {
const device = identifyIosPeripheral({ identifier, name, manufacturerData, rssi: -50, connectable: true });
assert.ok(device, name);
assert.equal(device.address, "14:B9:92:54:07:98", name);
assert.equal(device.identifier, identifier.toUpperCase(), name);
assert.equal(device.role, "left", name);
assert.equal(device.g1Firmware, firmware, name);
assert.equal(device.serial, "S110LABC030139", name);
}
assert.equal(identifyIosPeripheral({ identifier: "not-a-uuid", name: "G1CFW_L_EF50E5", manufacturerData: "", rssi: 0, connectable: true }), null);
});
test("names that only look like a G1 are not admitted", () => {
for (const name of ["G1v0_X", "G1v0", "G1 Tracker", "Even G1", "Pixel G1v0_L", "G1CFW_L", "G1CFW_X_EF50E5", "G1CFW_L_EF50E", "Pixel G1CFW_L_EF50E5"]) {
assert.equal(classifyAdvertisement(g1Raw(name, "")), null, name);
}
});
+1 -1
View File
@@ -130,9 +130,9 @@ test("a layout font is the default for text and list containers, which override
page.containers.map((c) => [c.name, c.font]),
[
["l", { family: "Inter", size: 24, weight: 700 }],
["i", undefined],
["t", { family: "Inter", size: 24 }],
["u", { family: "Terminus", size: 24 }],
["i", undefined],
],
);
});
+126
View File
@@ -0,0 +1,126 @@
// updateImageRawData conversion (app/apps/evenhub/image-pipeline.ts), checked
// against what the Even phone app's decompiled _prepare / _prepareLegacyRaw do.
const test = require("node:test");
const assert = require("node:assert/strict");
const UPNG = require("upng-js");
const {
IMAGE_EXCEPTION,
IMAGE_SEND_FAILED,
IMAGE_SIZE_INVALID,
IMAGE_SUCCESS,
detectImageFormat,
grayLevel,
prepareImageUpdate,
resizeRgbaCover,
} = require("../.test-build/app/apps/evenhub/image-pipeline.js");
const noNative = () => null;
const request = (over) => ({
imageData: [],
compressMode: 2,
requestWidth: 0,
requestHeight: 0,
geometry: { width: 20, height: 20 },
decodeNative: noNative,
stockLimits: true,
...over,
});
/** A PNG of the given RGBA pixels. */
function png(width, height, rgba) {
return Array.from(new Uint8Array(UPNG.encode([Uint8Array.from(rgba).buffer], width, height, 0)));
}
test("imageData must be an array of integers", () => {
assert.equal(prepareImageUpdate(request({ imageData: "AAAA" })).result, IMAGE_SEND_FAILED);
assert.equal(prepareImageUpdate(request({ imageData: { 0: 1, 1: 2 } })).result, IMAGE_SEND_FAILED);
assert.equal(prepareImageUpdate(request({ imageData: [1, 2.5] })).result, IMAGE_SEND_FAILED);
});
test("formats are recognized by their magic bytes", () => {
assert.equal(detectImageFormat(Uint8Array.from(png(1, 1, [0, 0, 0, 255]))), "png");
assert.equal(detectImageFormat(Uint8Array.from([0xff, 0xd8, 0xff, 0xe0])), "jpeg");
assert.equal(detectImageFormat(Uint8Array.from([0x52, 0x49, 0x46, 0x46, 0, 0, 0, 0, 0x57, 0x45, 0x42, 0x50])), "webp");
assert.equal(detectImageFormat(Uint8Array.from([0x47, 0x49, 0x46, 0x38, 0x39, 0x61])), "gif");
assert.equal(detectImageFormat(Uint8Array.from([0x42, 0x4d, 0, 0])), "bmp");
assert.equal(detectImageFormat(Uint8Array.from([1, 2, 3])), "raw");
});
test("grey uses 299/587/114 weights rounded to 16 levels, ignoring alpha", () => {
assert.equal(grayLevel(255, 0, 0), 4);
assert.equal(grayLevel(255, 255, 255), 15);
assert.equal(grayLevel(0, 0, 0), 0);
// A transparent white pixel is white: the phone app never looks at alpha.
const result = prepareImageUpdate(request({ imageData: png(20, 20, new Array(20 * 20).fill([255, 255, 255, 0]).flat()) }));
assert.equal(result.result, IMAGE_SUCCESS);
assert.ok(result.levels.every((level) => level === 15));
});
test("an encoded image is scaled to cover the container and centre-cropped", () => {
// 4x2: columns 0..3 lit 0, 85, 170, 255. Covering 20x20 scales by 10 to
// 40x20 and crops 10px a side, so only columns 1 and 2 survive.
const row = [0, 85, 170, 255].flatMap((v) => [v, v, v, 255]);
const result = prepareImageUpdate(request({ imageData: png(4, 2, [...row, ...row]) }));
assert.equal(result.result, IMAGE_SUCCESS);
assert.deepEqual([result.width, result.height], [20, 20]);
assert.deepEqual(Array.from(result.levels.slice(0, 20)), [...new Array(10).fill(grayLevel(85, 85, 85)), ...new Array(10).fill(grayLevel(170, 170, 170))]);
});
test("the cover resize samples source pixel centres", () => {
const source = { width: 3, height: 1, rgba: Uint8Array.from([10, 0, 0, 0, 20, 0, 0, 0, 30, 0, 0, 0]) };
const wide = resizeRgbaCover(source, 6, 2);
assert.deepEqual(Array.from(wide.rgba.filter((_, i) => i % 4 === 0)), [10, 10, 20, 20, 30, 30, 10, 10, 20, 20, 30, 30]);
});
test("width and height sent with the update replace the container's", () => {
const black = new Array(30 * 30).fill([0, 0, 0, 255]).flat();
const result = prepareImageUpdate(request({ imageData: png(30, 30, black), requestWidth: 40, requestHeight: 25 }));
assert.deepEqual([result.width, result.height], [40, 25]);
});
test("without a usable container size, the decoded size must itself be in range", () => {
const tiny = new Array(10 * 10).fill([0, 0, 0, 255]).flat();
assert.equal(prepareImageUpdate(request({ imageData: png(10, 10, tiny), geometry: null })).result, IMAGE_SIZE_INVALID);
});
test("JPEG, WebP and GIF go to the native decoder; a failed decode is an exception", () => {
const jpeg = [0xff, 0xd8, 0xff, 0xe0];
const decoded = prepareImageUpdate(request({
imageData: jpeg,
decodeNative: () => ({ width: 20, height: 20, rgba: new Uint8Array(20 * 20 * 4).fill(255) }),
}));
assert.equal(decoded.result, IMAGE_SUCCESS);
assert.equal(decoded.format, "jpeg");
assert.equal(prepareImageUpdate(request({ imageData: jpeg })).result, IMAGE_EXCEPTION);
});
test("raw pixels need compressMode 2 and exactly the container's size", () => {
const raw = new Array(400).fill(0);
assert.equal(prepareImageUpdate(request({ imageData: raw })).result, IMAGE_SUCCESS);
assert.equal(prepareImageUpdate(request({ imageData: raw, compressMode: 0 })).result, IMAGE_EXCEPTION);
assert.equal(prepareImageUpdate(request({ imageData: raw.slice(1) })).result, IMAGE_EXCEPTION);
assert.equal(prepareImageUpdate(request({ imageData: raw, geometry: { width: 400, height: 1 } })).result, IMAGE_SIZE_INVALID);
});
test("raw bytes up to 15 are already 4-bit levels; larger ones are 8-bit grey", () => {
const raw = new Array(400).fill(0);
raw.splice(0, 4, 15, 16, 128, 255);
const { levels } = prepareImageUpdate(request({ imageData: raw }));
assert.deepEqual(Array.from(levels.slice(0, 4)), [15, 1, 8, 15]);
});
test("4-bit raw rows are padded to whole bytes", () => {
// 21 wide: 11 bytes a row, high nibble first, the last low nibble unused.
const rows = [];
for (let y = 0; y < 20; y++) rows.push(0xf0, ...new Array(9).fill(0), 0x0f << 4);
const { result, levels } = prepareImageUpdate(request({ imageData: rows, geometry: { width: 21, height: 20 } }));
assert.equal(result, IMAGE_SUCCESS);
assert.deepEqual([levels[0], levels[1], levels[20], levels[21]], [15, 0, 15, 15]);
});
test("extended layouts skip the stock size and compressMode rules", () => {
const big = new Array(400 * 200).fill(0);
const result = prepareImageUpdate(request({ imageData: big, compressMode: 0, geometry: { width: 400, height: 200 }, stockLimits: false }));
assert.equal(result.result, IMAGE_SUCCESS);
});
+63
View File
@@ -0,0 +1,63 @@
// The EvenHub layout debug log (app/apps/evenhub/layout-debug.ts): page notes
// and the per-update lines it writes to logcat.
const test = require("node:test");
const assert = require("node:assert/strict");
const { parsePage } = require("../.test-build/app/apps/evenhub/containers.js");
const { describeImageUpdate, describePage, pageNotes } = require("../.test-build/app/apps/evenhub/layout-debug.js");
const iconOverBody = (extra = {}) =>
parsePage({
containerTotalNum: 2,
textObject: [{ containerID: 1, containerName: "body", xPosition: 0, yPosition: 0, width: 576, height: 288, isEventCapture: 1, ...extra.text }],
imageObject: [{ containerID: 2, containerName: "icon", xPosition: 8, yPosition: 8, width: 24, height: 24, ...extra.image }],
});
test("describePage lists containers bottom to top and ends with the raw payload", () => {
const lines = describePage("rebuildPageContainer", iconOverBody(), { containerTotalNum: 2 });
assert.match(lines[0], /^rebuildPageContainer: 2 containers \(0 list, 1 text, 1 image\), containerTotalNum=2/);
assert.match(lines[1], /^ {2}0: text#1 "body" @0,0 576x288 capture len=0 ""$/);
assert.match(lines[2], /^ {2}1: image#2 "icon" @8,8 24x24 no pixels yet$/);
assert.equal(lines.at(-1), ' raw: {"containerTotalNum":2}');
});
test("a shared container id is noted with the container it resolves to", () => {
assert.ok(pageNotes(iconOverBody({ image: { containerID: 1 } })).includes(
'id 1 is shared by text#1 "body", image#1 "icon"; updates and events resolve to text#1 "body" only',
));
});
test("capture-container problems and a wrong containerTotalNum are noted", () => {
assert.ok(pageNotes(iconOverBody({ text: { isEventCapture: 0 } })).some((n) => n.startsWith("no event-capture container")));
assert.ok(pageNotes(iconOverBody(), 3).some((n) => n.startsWith("containerTotalNum is 3 but 2")));
assert.deepEqual(pageNotes(iconOverBody(), 2), []);
});
const prepared = (over = {}) => ({ result: 0, levels: null, width: 0, height: 0, format: "raw", reason: "", ...over });
test("an update lists payload, size, lit pixels and outcome", () => {
const page = iconOverBody();
const levels = new Uint8Array(24 * 24);
levels[24 * 3 + 5] = 9;
levels[24 * 10 + 7] = 15;
const [line] = describeImageUpdate({
sentId: 2, sentName: "icon", raw: Array.from(levels), target: page.containers[1],
prepared: prepared({ levels, width: 24, height: 24 }), shown: true, result: 0,
});
assert.equal(line, 'updateImageRawData id=2 "icon" -> image#2 "icon" payload=number[576] format=raw 24x24 lit=2px max=15/15 bbox=5,3-7,10 result=0 shown');
});
test("an update says why it wasn't shown", () => {
const page = iconOverBody({ image: { containerID: 1 } });
const toText = describeImageUpdate({
sentId: 1, sentName: "", raw: [], target: page.containers[0],
prepared: prepared({ levels: new Uint8Array(1), width: 1, height: 1 }), shown: false, result: 4,
});
assert.ok(toText.some((line) => line.includes('resolves to text#1 "body", so the glasses refuse')));
const failed = describeImageUpdate({
sentId: 2, sentName: "", raw: "AAAA", target: undefined,
prepared: prepared({ result: 4, format: null, reason: "imageData is not an array of integers" }), shown: false, result: 4,
});
assert.match(failed[0], /payload=string\(4 chars\) result=4 not shown$/);
assert.equal(failed[1], " imageData is not an array of integers");
});
+61
View File
@@ -0,0 +1,61 @@
// The Even phone app's checks before it sends a page or text upgrade to the
// glasses (app/apps/evenhub/payload-validation.ts).
const test = require("node:test");
const assert = require("node:assert/strict");
const { stockPayloadError } = require("../.test-build/app/apps/evenhub/payload-validation.js");
// What SDK 0.0.16 sends for a small page (captured with a stub bridge).
const sdkPage = () => ({
containerTotalNum: 2,
textObject: [{ containerID: 1, containerName: "t", xPosition: 0, yPosition: 0, width: 200, height: 100, content: "hi", isEventCapture: 1, borderWidth: 2 }],
imageObject: [{ containerID: 2, containerName: "i", xPosition: 0, yPosition: 0, width: 40, height: 40 }],
listObject: [{ containerID: 3, containerName: "l", itemContainer: { itemCount: 2, itemName: ["a", "b"], isItemSelectBorderEn: 1 } }],
menuObject: { menuItems: [{ itemName: "Pause", itemID: 1 }] },
});
test("SDK-shaped payloads pass", () => {
assert.equal(stockPayloadError("create", sdkPage()), null);
assert.equal(stockPayloadError("rebuild", sdkPage()), null);
assert.equal(stockPayloadError("textUpgrade", { containerID: 1, containerName: "t", contentOffset: 2, contentLength: 3, content: "abc" }), null);
});
test("unknown keys fail, numeric tag keys pass", () => {
const page = sdkPage();
page.textObject[0].Container_ID = 1;
assert.match(stockPayloadError("create", page), /unknown key textObject\[0\]\.Container_ID/);
assert.match(stockPayloadError("rebuild", { ...sdkPage(), widgetId: 1 }), /unknown key widgetId/);
assert.equal(stockPayloadError("create", { ...sdkPage(), 7: 1 }), null);
});
test("numeric fields take integers or numeric strings only", () => {
const withField = (value) => {
const page = sdkPage();
page.textObject[0].xPosition = value;
return stockPayloadError("create", page);
};
assert.equal(withField(-10), null, "negative values wrap rather than fail");
assert.equal(withField("12"), null);
assert.match(withField(10.5), /must be an integer/);
assert.match(withField(true), /must be an integer/);
assert.match(withField(null), /is null/);
assert.match(withField("left"), /must be an integer/);
});
test("string and repeated fields are type-checked", () => {
const page = sdkPage();
page.textObject[0].content = 5;
assert.match(stockPayloadError("create", page), /content must be a string/);
const list = sdkPage();
list.listObject[0].itemContainer.itemName = "a";
assert.match(stockPayloadError("create", list), /itemName must be an array/);
});
test("textColor must be an integer 0..4 wherever it appears", () => {
const page = sdkPage();
page.textObject[0].textColor = 4;
assert.equal(stockPayloadError("create", page), null);
page.textObject[0].textColor = 15;
assert.match(stockPayloadError("create", page), /textColor must be an integer from 0 to 4/);
assert.match(stockPayloadError("textUpgrade", { containerID: 1, content: "x", textColor: "3" }), /textColor/);
});
+300
View File
@@ -0,0 +1,300 @@
// EvenHub with a RayNeo iO paired: the store shows only a notice (and loads
// nothing), and each sideloaded app warns once that it won't display
// correctly. The real layers run with their dependencies mocked; what they
// paint is collected as text.
const test = require('node:test');
const assert = require('node:assert/strict');
const { loader } = require('./helpers/load-typescript.cjs');
const { bordered } = require('./helpers/viewport.cjs');
const gestures = loader({}, {})('app/ui/gestures.ts');
const font = { lineHeight: 10, measureText: (text) => text.length * 6 };
/** Records drawn text; every other drawing call is a no-op. */
const makeImageClass = (drawn) => class {
drawText(_font, _x, _y, text) { drawn.push(text); }
fillRect() {}
fillRoundedRect() {}
drawRoundedRect() {}
};
const settle = () => new Promise((resolve) => setImmediate(resolve));
// ---- the store -----------------------------------------------------------
const emptyPage = { apps: [], total: 0, page: 1, pageSize: 20 };
class FakeMenu {
constructor(options) { this.options = options; this.items = options.items; this.selectedIndex = 0; }
setItems(items) { this.items = items; }
get selectedItem() { return this.items[this.selectedIndex] ?? null; }
handleInput() {}
paint(image, box) { this.items.forEach((item, index) => this.options.draw({ image, item, index, x: box.x, y: box.y, width: box.width })); }
drawScrollbar() {}
}
const loadStore = () => {
const drawn = [];
const glasses = { available: false };
const calls = { listApps: 0, yields: 0 };
const setting = { get: () => '', set() {} };
const load = loader({}, {
'../../graphics/ui-fonts': { getDefaultSmallFont: () => font },
'../../graphics/image': { GrayImage: makeImageClass(drawn) },
'../../graphics/textwrap': { truncateText: (_f, text) => text, wrapText: (_f, text) => [text] },
'../../ui/gestures': { ...gestures, GESTURE_CLICK: 'click' },
'../../ui/menu': { drawSelectionHighlight() {} },
'../../ui/menu-core': { Menu: FakeMenu },
'../../ui/shell/shell': { shell: { yieldFocusToSidebar() { calls.yields++; } } },
'../../ui/metrics': { lineStep: () => 12 },
'../../ui/dashboard-settings': {
ConfigSettingString: class { constructor(options) { this.value = options.defaultValue ?? ''; }
get() { return this.value; } set(next) { this.value = next; return next; } },
},
'./even-api': {
evenHubApi: {
listApps: async () => { calls.listApps++; return emptyPage; },
listNewApps: async () => emptyPage,
searchApps: async () => emptyPage,
},
EvenHubAuthenticationError: class extends Error {},
isEvenHubStoreConfigured: () => true,
},
'./credentials': {
evenHubLoginEmailSetting: setting, evenHubLoginPasswordSetting: setting, evenHubRememberMeSetting: setting,
clearEvenHubLoginForm() {}, resetEvenHubLoginForm() {}, clearEvenHubSession() {}, clearTransientEvenHubSession() {},
},
'./installed-apps': { getInstalledEvenHubApps: () => [], uninstallEvenHubPackage: () => true },
'./store-detail-layer': { EvenHubStoreDetailLayer: class {} },
'./store-install': { cleanError: (error) => String(error), installStoreApp: async () => ({}) },
'./updates': { checkForEvenHubUpdates: async () => [], describeUpdate: () => ({}) },
'./glasses-compat': { isEvenHubStoreAvailable: () => glasses.available },
});
const { EvenHubStoreLayer } = load('app/apps/evenhub/store-layer.ts');
const layer = new EvenHubStoreLayer({ launchApp: async () => {}, appendLog: () => {} });
const stackCalls = { push: 0, clearToBase: 0 };
const ctx = {
stack: {
getBaseSize: () => bordered(476, 180),
isFocused: () => true,
clearToBase() { stackCalls.clearToBase++; },
push() { stackCalls.push++; },
pop() {},
},
actions: { requestRender() {}, endTextSettingEdit() {}, startTextSettingsEdit() {} },
};
const repaint = () => {
drawn.length = 0;
layer.paint(ctx);
return [...drawn];
};
return { layer, ctx, glasses, calls, stackCalls, repaint };
};
const NOTICE = 'Using the Even Realities app store requires Even Realities glasses.';
test('with a RayNeo iO the store shows the notice and loads nothing', async () => {
const { layer, ctx, calls, repaint } = loadStore();
const painted = repaint();
assert.ok(painted.includes(NOTICE), JSON.stringify(painted));
assert.ok(!painted.includes('Top'), 'no tabs');
await settle();
assert.equal(calls.listApps, 0, 'no request to the store');
assert.equal(layer.buildMenuItems().length, 0, 'no store actions in the window menu');
await layer.handleInput({ type: 'click', source: 'ring' }, ctx);
await layer.handleInput({ type: 'scroll-down' }, ctx);
layer.receiveTextInput('chess', ctx);
await settle();
assert.equal(calls.listApps, 0, 'input and dictation do nothing');
assert.ok(repaint().includes(NOTICE));
await layer.handleInput({ type: 'double-click', source: 'ring' }, ctx);
await layer.handleInput({ type: 'swipe-left', source: 'watch', timestampMs: 0 }, ctx);
assert.equal(calls.yields, 2, 'double-click and a back swipe leave the window');
});
test('an installed app\'s store page opens to the notice, naming the app', () => {
const { layer, ctx, stackCalls, repaint } = loadStore();
layer.showInstalledPackage(ctx.stack, { packageId: 'com.example.alpha', name: 'Alpha', version: '1' });
assert.equal(stackCalls.push, 0, 'no store page is opened');
assert.equal(stackCalls.clearToBase, 1, 'pages above the notice are closed');
const painted = repaint();
assert.ok(painted.includes("Alpha's package is missing."), JSON.stringify(painted));
assert.ok(painted.includes(NOTICE));
});
test('the store comes back once Even Realities glasses are paired', async () => {
const { glasses, calls, repaint } = loadStore();
repaint();
glasses.available = true;
repaint();
await settle();
assert.equal(calls.listApps, 1, 'the Top tab loads on the first paint it gets');
const painted = repaint();
assert.ok(!painted.includes(NOTICE));
assert.ok(painted.includes('Public apps by popularity'), JSON.stringify(painted));
});
// ---- the display warning ---------------------------------------------------
const loadWarning = () => {
const drawn = [];
const load = loader({}, {
'../../graphics/ui-fonts': { getDefaultSmallFont: () => font },
'../../graphics/image': { GrayImage: makeImageClass(drawn) },
'../../graphics/textwrap': { wrapText: (_f, text) => [text] },
'../../ui/gestures': gestures,
'../../ui/layers': {},
'../../ui/menu': { drawListScrollbar() {}, drawSelectionHighlight() {} },
'../../ui/metrics': { lineStep: () => 12, listRowHeight: () => 20 },
});
const { EvenHubDisplayWarningLayer } = load('app/apps/evenhub/display-warning-dialog.ts');
const ImageClass = makeImageClass(drawn);
const outcome = { answered: 0, closed: 0, popped: 0 };
const dialog = new EvenHubDisplayWarningLayer(
'Demo',
() => { outcome.answered++; },
() => { outcome.closed++; },
);
// The real stack calls onRemoved as it pops.
const ctx = {
stack: {
getBaseSize: () => bordered(476, 180),
isFocused: () => true,
pop() { outcome.popped++; dialog.onRemoved(); },
},
};
const paint = () => {
drawn.length = 0;
dialog.paint(ctx, () => new ImageClass());
return [...drawn];
};
return { dialog, ctx, paint, outcome };
};
test('the display warning names the app and the reason', () => {
const { paint } = loadWarning();
const painted = paint();
assert.ok(painted.includes('Compatibility warning'));
const message = painted.find((text) => text.includes('Demo'));
assert.ok(message, JSON.stringify(painted));
assert.match(message, /will not display correctly on the RayNeo iO/);
assert.match(message, /different resolution/);
assert.ok(painted.includes('Continue') && painted.includes('Close app'));
});
test('Continue dismisses the warning and leaves the app running', () => {
const { dialog, ctx, paint, outcome } = loadWarning();
paint();
dialog.handleInput({ type: 'click', source: 'ring' }, ctx);
assert.deepEqual(outcome, { answered: 1, closed: 0, popped: 1 });
});
test('Close app dismisses the warning and closes the app', () => {
const { dialog, ctx, paint, outcome } = loadWarning();
paint();
dialog.handleInput({ type: 'scroll-down' }, ctx);
dialog.handleInput({ type: 'click', source: 'ring' }, ctx);
assert.deepEqual(outcome, { answered: 1, closed: 1, popped: 1 });
});
test('double-click dismisses the warning; closing the window under it does not count', () => {
const backedOut = loadWarning();
backedOut.paint();
backedOut.dialog.handleInput({ type: 'double-click', source: 'ring' }, backedOut.ctx);
assert.deepEqual(backedOut.outcome, { answered: 1, closed: 0, popped: 1 });
const abandoned = loadWarning();
abandoned.paint();
abandoned.dialog.onRemoved();
assert.deepEqual(abandoned.outcome, { answered: 0, closed: 0, popped: 0 }, 'shown again next launch');
});
// ---- which launches warn -------------------------------------------------
const loadCompat = (glassesType) => {
const store = new Map();
const glasses = { type: glassesType };
const compat = loader({}, {
'../../ui/dashboard-settings': { glassesTypeSetting: { get: () => glasses.type } },
'../../native/settings-store': {
getStringSetting: (key, fallback) => (store.has(key) ? store.get(key) : fallback),
setStringSetting: (key, value) => { store.set(key, value); },
},
})('app/apps/evenhub/glasses-compat.ts');
return { compat, glasses };
};
test('each app warns once with a RayNeo iO paired, and never with Even glasses', () => {
const { compat, glasses } = loadCompat('rayneo-io');
assert.equal(compat.isEvenHubStoreAvailable(), false);
assert.equal(compat.needsDisplayWarning('com.example.a'), true);
compat.markDisplayWarningShown('com.example.a');
compat.markDisplayWarningShown('com.example.a');
assert.equal(compat.needsDisplayWarning('com.example.a'), false, 'answered once');
assert.equal(compat.needsDisplayWarning('com.example.b'), true, 'another app still warns');
glasses.type = 'g2';
assert.equal(compat.isEvenHubStoreAvailable(), true);
assert.equal(compat.needsDisplayWarning('com.example.b'), false);
glasses.type = 'g1';
assert.equal(compat.isEvenHubStoreAvailable(), true);
assert.equal(compat.needsDisplayWarning('com.example.b'), false);
});
// ---- the app window ------------------------------------------------------
const loadWindow = (needsWarning) => {
const pushed = [];
const closed = [];
const marked = [];
class FakeWarning { constructor(appName, onAnswered, closeApp) { Object.assign(this, { appName, onAnswered, closeApp }); } }
const load = loader({}, {
'../../graphics/image': { GrayImage: class {} },
'./compositor': { EVENHUB_SCREEN_WIDTH: 576 },
'../../ui/gestures': gestures,
'../../ui/layers': {},
'../../ui/shell/in-process-window': {
createInProcessWindow: () => ({
stack: { push: (layer) => pushed.push(layer) },
window: { handleInput: async () => {} },
requestRender() {},
setHeightMode() {},
}),
},
'../../ui/shell/shell': { shell: { closeWindow: (id) => closed.push(id), foregroundWindow: () => null } },
'../../ui/shell/chrome-layer': { makeImageWindowIcon: () => () => {}, windowIcon: () => () => {} },
'./session': { EvenHubSession: class {} },
'./installed-apps': { renderInstalledEvenHubIcon: () => null },
'./glasses-compat': {
needsDisplayWarning: () => needsWarning,
markDisplayWarningShown: (id) => marked.push(id),
},
'./display-warning-dialog': { EvenHubDisplayWarningLayer: FakeWarning },
});
const { createEvenHubWindow } = load('app/apps/evenhub/evenhub-window.ts');
const session = {
manifest: { packageId: 'com.example.demo', name: 'Demo' },
attachWindow() {},
};
const options = { actions: {}, submitFrame() {}, setSurfaceVisible() {}, removeSurface() {}, onClosed() {} };
createEvenHubWindow('evenhub:app:7', 'evenhub', session, options, () => {}, () => {});
return { pushed, closed, marked, FakeWarning };
};
test('an EvenHub window opens under the warning when the app needs one', () => {
const { pushed, closed, marked, FakeWarning } = loadWindow(true);
assert.equal(pushed.length, 1);
const warning = pushed[0];
assert.ok(warning instanceof FakeWarning);
assert.equal(warning.appName, 'Demo');
warning.onAnswered();
assert.deepEqual(marked, ['com.example.demo']);
warning.closeApp();
assert.deepEqual(closed, ['evenhub:app:7']);
});
test('an EvenHub window opens straight to the app otherwise', () => {
assert.equal(loadWindow(false).pushed.length, 0);
});
+161
View File
@@ -0,0 +1,161 @@
// EvenHubSession behaviors that follow stock (the Even phone app plus
// firmware 2.2.9.22, as decompiled and run in the firmware emulator): text
// scrolling and its boundary events, text upgrades, image routing, and what a
// page the firmware can't build does.
const test = require('node:test');
const assert = require('node:assert/strict');
const vm = require('node:vm');
const { loader } = require('./helpers/load-typescript.cjs');
// A face with 10px characters and 27px lines, wrapping only at '\n'.
const face = {
lineHeight: 27,
measureLine: text => text.length * 10,
wrap: text => text.split('\n'),
drawText() {},
drawTextWrapped() {},
};
function harness() {
const timers = [];
const load = loader({ global: { isIOS: true }, setTimeout: fn => { timers.push(fn); return timers.length; }, clearTimeout() {}, Promise }, {
'@nativescript/core': { ApplicationSettings: { getString: (_k, d) => d, setString() {} } },
'upng-js': require('upng-js'),
'../../graphics/evenhub-font': { EvenHubFont: { get: () => face } },
'./fonts': { containerFace: () => face, availableFontFamilies: () => [], measureContainerText: () => ({ width: 0, lineHeight: 27 }) },
'../../ui/dashboard-settings': {}, '../../ui/sound-effects': {}, './api-key-dialog': {},
'./mic-router': { evenHubMicRouter: {} }, './imu-router': { evenHubImuRouter: {} },
'./compass-router': { evenHubCompassRouter: {} }, '../../assistant/tool-registry': {},
'../../native/location': {}, '../../native/location-tracker': {}, '../../native/location-permissions': {},
'../../native/image-rgba': { decodeImageRgba: () => null },
});
const api = load('app/apps/evenhub/session.ts');
const session = new api.EvenHubSession({ name: 'Probe', packageId: 'test.stock', permissions: [] }, '/test/dist', () => {}, '');
let windowClosed = false;
session.attachWindow({ requestRender() {}, windowId: 'probe', closeWindow: () => { windowClosed = true; } });
const web = vm.createContext({ Promise, Map, Date, console });
web.window = web;
web.__faceclawEvenHub = { postMessage: (name, args, id) => session.handleBridgeCall(name, args, id) };
session.attachWebView({ evaluateJs: script => vm.runInContext(script, web), destroy() {} });
vm.runInContext(api.EVENHUB_BRIDGE_INJECT_SCRIPT, web);
const events = [];
web._listenEvenAppMessage = value => {
const message = typeof value === 'string' ? JSON.parse(value) : value;
if (message.method === 'evenHubEvent') events.push(JSON.parse(JSON.stringify(message.data)));
};
const call = (method, data = {}) => web.flutter_inappwebview.callHandler('evenAppMessage', JSON.stringify({ method, data }));
const runTimers = () => { for (const fn of timers.splice(0)) fn(); };
return { session, call, events, runTimers, windowClosed: () => windowClosed };
}
const lines = n => Array.from({ length: n }, (_, i) => `line ${i}`).join('\n');
const textPage = (content, extra = {}) => ({
containerTotalNum: 1,
textObject: [{ containerID: 1, containerName: 'body', xPosition: 0, yPosition: 0, width: 300, height: 100, content, isEventCapture: 1, ...extra }],
});
const swipe = (h, direction) => h.session.handleGesture({ type: direction === 'down' ? 'scroll-down' : 'scroll-up' });
test('an overflowing capture text container scrolls itself and reports only at its ends', async () => {
const h = harness();
// 6 lines = 162px in a 100px window: 62px to scroll, three 28px swipes.
assert.equal(await h.call('createStartUpPageContainer', textPage(lines(6))), 0);
h.events.length = 0;
swipe(h, 'down'); swipe(h, 'down'); swipe(h, 'down');
assert.deepEqual(h.events, [], 'no events while scrolling, nor for the swipe that reaches the end');
swipe(h, 'down');
assert.deepEqual(h.events, [{ type: 'textEvent', jsonData: { containerID: 1, containerName: 'body', eventType: 2 } }]);
h.events.length = 0;
swipe(h, 'up'); swipe(h, 'up'); swipe(h, 'up');
assert.deepEqual(h.events, []);
swipe(h, 'up');
assert.deepEqual(h.events, [{ type: 'textEvent', jsonData: { containerID: 1, containerName: 'body', eventType: 1 } }]);
});
test('text that fits reports every swipe as reaching an end', async () => {
const h = harness();
await h.call('createStartUpPageContainer', textPage('short'));
h.events.length = 0;
swipe(h, 'down'); swipe(h, 'down');
assert.equal(h.events.filter(e => e.type === 'textEvent' && e.jsonData.eventType === 2).length, 2);
});
test('text upgrades replace the whole text by id, ignore offsets, and keep old text when emptied', async () => {
const h = harness();
await h.call('createStartUpPageContainer', textPage('Hello world'));
const body = () => h.session.page.containers[0];
assert.equal(await h.call('textContainerUpgrade', { containerID: 1, containerName: 'other name', contentOffset: 3, contentLength: 2, content: 'XY' }), true);
assert.equal(body().content, 'XY');
assert.equal(await h.call('textContainerUpgrade', { containerID: 1, content: '' }), true);
assert.equal(body().content, 'XY', 'an empty upgrade leaves the old text showing');
assert.equal(await h.call('textContainerUpgrade', { containerID: 9, content: 'nope' }), false);
assert.equal(await h.call('textContainerUpgrade', { containerID: 1, content: 'x', junk: 1 }), false, 'unknown keys are refused');
});
test('an upgrade scrolls the text back to the top', async () => {
const h = harness();
await h.call('createStartUpPageContainer', textPage(lines(6)));
swipe(h, 'down');
assert.equal(h.session.page.containers[0].scrollY, 28);
await h.call('textContainerUpgrade', { containerID: 1, content: lines(7) });
assert.equal(h.session.page.containers[0].scrollY, 0);
});
test('image updates need a page, an image at that id, and a usable payload', async () => {
const h = harness();
const raw = new Array(400).fill(0);
assert.equal(await h.call('updateImageRawData', { containerID: 2, imageData: raw, compressMode: 2 }), 4, 'no page yet');
await h.call('createStartUpPageContainer', {
containerTotalNum: 3,
textObject: [{ containerID: 1, containerName: 'body', width: 300, height: 100, content: 'x', isEventCapture: 1 },
{ containerID: 3, containerName: 'label', width: 50, height: 30, content: 'y' }],
imageObject: [{ containerID: 2, containerName: 'icon', width: 20, height: 20 }, { containerID: 3, containerName: 'shadowed', width: 20, height: 20 }],
});
assert.equal(await h.call('updateImageRawData', { containerID: 2, imageData: raw, compressMode: 2 }), 0);
assert.equal(await h.call('updateImageRawData', { containerID: 2, imageData: 'AAAA', compressMode: 2 }), 4, 'base64 is refused');
assert.equal(await h.call('updateImageRawData', { containerID: 3, imageData: raw, compressMode: 2 }), 4, 'id 3 resolves to the text container');
// With no image at that id the Even app has no size for raw pixels: 0x0 is out of range.
assert.equal(await h.call('updateImageRawData', { containerID: 7, imageData: raw, compressMode: 2 }), 2);
});
test('a page the firmware cannot build fails create, and a failed rebuild closes the app', async () => {
const h = harness();
const tinyImage = { ...textPage('x'), imageObject: [{ containerID: 2, containerName: 'dot', width: 10, height: 10 }] };
assert.equal(await h.call('createStartUpPageContainer', tinyImage), 1);
assert.equal(await h.call('rebuildPageContainer', textPage('x')), false, 'nothing to rebuild before a page exists');
assert.equal(await h.call('createStartUpPageContainer', textPage('x')), 0);
h.events.length = 0;
assert.equal(await h.call('rebuildPageContainer', tinyImage), false);
assert.equal(h.session.page.containers.length, 0);
assert.ok(h.events.some(e => e.type === 'sysEvent' && e.jsonData.eventType === 6), 'ABNORMAL_EXIT');
h.runTimers();
assert.equal(h.windowClosed(), true);
});
test('the Even app refuses malformed pages before they reach the glasses', async () => {
const h = harness();
assert.equal(await h.call('createStartUpPageContainer', textPage('x', { isEventCapture: true })), 1);
assert.equal(await h.call('createStartUpPageContainer', textPage('x', { textColor: 7 })), 1);
assert.equal(await h.call('createStartUpPageContainer', textPage('x')), 0);
assert.equal(await h.call('rebuildPageContainer', { ...textPage('y'), Container_Total_Num: 1 }), false);
assert.equal(h.session.page.containers[0].content, 'x', 'a refused rebuild leaves the page alone');
});
test('borders use the colour index, overflowing text gets a scrollbar, and images cover text', async () => {
const h = harness();
// 300x100 with a 2px border of colour 3; 6 lines overflow the 96px window by 66px.
await h.call('createStartUpPageContainer', {
containerTotalNum: 2,
textObject: [{ containerID: 1, containerName: 'body', xPosition: 0, yPosition: 0, width: 300, height: 100, content: lines(6), isEventCapture: 1, borderWidth: 2, borderColor: 3 }],
imageObject: [{ containerID: 2, containerName: 'icon', xPosition: 0, yPosition: 40, width: 20, height: 20 }],
});
await h.call('updateImageRawData', { containerID: 2, imageData: new Array(400).fill(0), compressMode: 2 });
const image = h.session.paint({ width: 576, height: 288 }, true);
const at = (x, y) => image.pixels[y * 576 + x];
assert.equal(at(150, 0), 3 * 17, 'border grey');
assert.equal(at(0, 50), 0, 'the black image covers the border under it');
// Track 6..94 (88px); length floor(88 * 100 / 166) = 53; x 290..293.
assert.deepEqual([at(290, 7), at(291, 7), at(293, 7), at(289, 7), at(294, 7)], [187, 187, 187, 0, 0]);
assert.deepEqual([at(290, 6), at(291, 6), at(292, 6), at(293, 6)], [0, 187, 187, 0], 'chamfered top row');
assert.equal(at(291, 58), 187);
assert.equal(at(291, 59), 0, 'bar ends after 53 rows');
});
+6 -2
View File
@@ -9,6 +9,7 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { loader } = require('./helpers/load-typescript.cjs');
const { bordered } = require('./helpers/viewport.cjs');
const setting = (initial = '') => {
let value = initial;
@@ -17,6 +18,8 @@ const setting = (initial = '') => {
class AuthError extends Error {}
const gestures = loader({}, {})('app/ui/gestures.ts');
/**
* Load the store layer, by default with a signed-out account and no stored
* credentials. `api` overrides evenHubApi methods; text the layer paints is
@@ -34,7 +37,7 @@ const loadStoreLayer = ({ configured = false, api = {} } = {}) => {
'../../graphics/image': { GrayImage: class { drawText(_f, _x, _y, text) { drawn.push(text); } fillRect() {} } },
'../../graphics/textwrap': { truncateText: (_f, text) => text, wrapText: (_f, text) => [text] },
'../../util/numeric-util': { clamp: (v, lo, hi) => Math.min(hi, Math.max(lo, v)) },
'../../ui/gestures': { GESTURE_CLICK: 'click' },
'../../ui/gestures': { ...gestures, GESTURE_CLICK: 'click' },
'../../ui/menu': { drawSelectionHighlight() {} },
'../../ui/menu-core': require('../.test-build/app/ui/menu-core.js'),
'../../ui/shell/shell': { shell: { yieldFocusToSidebar() {} } },
@@ -60,6 +63,7 @@ const loadStoreLayer = ({ configured = false, api = {} } = {}) => {
'./store-detail-layer': { EvenHubStoreDetailLayer: class { constructor(_app, options) { detailOptions.push(options); } } },
'./store-install': { cleanError: (error) => String(error?.message ?? error), installStoreApp: async () => {} },
'./updates': { checkForEvenHubUpdates: async () => [], describeUpdate: () => '' },
'./glasses-compat': { isEvenHubStoreAvailable: () => true },
});
const { EvenHubStoreLayer } = load('app/apps/evenhub/store-layer.ts');
return { EvenHubStoreLayer, email, password, drawn, detailOptions };
@@ -71,7 +75,7 @@ const makeCtx = () => {
const stack = {
cleared: 0,
pushed: [],
getBaseSize: () => ({ width: 576, height: 136 }),
getBaseSize: () => bordered(576, 136),
isFocused: () => true,
clearToBase() { stack.cleared++; stack.pushed.length = 0; },
push(layer) { stack.pushed.push(layer); },
+81
View File
@@ -0,0 +1,81 @@
// Stock container-model rules the EvenHub emulation follows: stacking order,
// which container captures input, id resolution, firmware clamps and border
// greys. Source: the evenRealities-openCFW reconstruction of firmware 2.2.6.10
// (evenhub_ui_containers.c, evenhub_ui_page.c, common_text/list_container.c).
const test = require("node:test");
const assert = require("node:assert/strict");
const {
borderGrey,
clampToStockLimits,
containerById,
eventCaptureContainer,
paintOrder,
parsePage,
} = require("../.test-build/app/apps/evenhub/containers.js");
const names = (containers) => containers.map((c) => c.name);
test("without zOrderIndex containers stack lists, then texts, then images", () => {
const page = parsePage({
imageObject: [{ containerID: 1, containerName: "img" }],
textObject: [{ containerID: 2, containerName: "txt" }],
listObject: [{ containerID: 3, containerName: "lst" }],
});
assert.deepEqual(names(paintOrder(page.containers)), ["lst", "txt", "img"]);
});
test("zOrderIndex sorts only when every container has one", () => {
const all = parsePage({
textObject: [{ containerID: 1, containerName: "body", zOrderIndex: 2 }],
imageObject: [{ containerID: 2, containerName: "icon", zOrderIndex: 1 }],
});
assert.deepEqual(names(paintOrder(all.containers)), ["icon", "body"]);
const some = parsePage({
textObject: [{ containerID: 1, containerName: "body", zOrderIndex: 2 }],
imageObject: [{ containerID: 2, containerName: "icon" }],
});
assert.deepEqual(names(paintOrder(some.containers)), ["body", "icon"]);
});
test("the first capture container in stacking order owns input", () => {
const page = parsePage({
textObject: [
{ containerID: 1, containerName: "a", isEventCapture: 1, zOrderIndex: 5 },
{ containerID: 2, containerName: "b", isEventCapture: 1, zOrderIndex: 3 },
],
});
assert.equal(eventCaptureContainer(page).name, "b");
});
test("an id resolves to its first container in stacking order, whatever the kind", () => {
const page = parsePage({
textObject: [{ containerID: 7, containerName: "label" }],
imageObject: [{ containerID: 7, containerName: "icon" }],
});
assert.equal(containerById(page, 7).kind, "text");
assert.equal(containerById(page, 8), undefined);
});
test("stock clamps text and list geometry but leaves image sizes alone", () => {
const page = parsePage({
textObject: [{ containerID: 1, containerName: "t", xPosition: 900, yPosition: 400, width: 800, height: 400, borderColor: 40, borderRadius: 30, paddingLength: 50 }],
listObject: [{ containerID: 2, containerName: "l", itemContainer: { itemName: Array.from({ length: 25 }, (_, i) => `item ${i}`) } }],
imageObject: [{ containerID: 3, containerName: "i", xPosition: 600, width: 400, height: 200 }],
});
clampToStockLimits(page);
const [list, text, image] = page.containers;
assert.deepEqual([text.x, text.y, text.width, text.height], [576, 288, 576, 288]);
assert.deepEqual([text.borderColor, text.borderRadius, text.paddingLength], [16, 10, 32]);
assert.equal(list.itemNames.length, 20);
assert.deepEqual([image.x, image.width, image.height], [576, 400, 200]);
});
test("border color indices are 16 grey steps, with 16 meaning white", () => {
assert.equal(borderGrey(0), 0);
assert.equal(borderGrey(3), 0x33);
assert.equal(borderGrey(15), 0xff);
assert.equal(borderGrey(16), 0xff);
const page = parsePage({ textObject: [{ containerID: 1, containerName: "t", borderWidth: 2 }] });
assert.equal(page.containers[0].borderColor, 0, "an omitted borderColor is the protobuf default, black");
});
+308
View File
@@ -0,0 +1,308 @@
// EvenHub store navigation: watch swipes on the tab strip, the Updates tab's
// Update All row, and page-at-a-time scrolling in the permission dialog.
//
// The real layers run with their dependencies mocked; what they paint is
// collected as text, so these assert what the user would see.
const test = require('node:test');
const assert = require('node:assert/strict');
const { loader } = require('./helpers/load-typescript.cjs');
const { bordered, unbordered, edged } = require('./helpers/viewport.cjs');
const gestures = loader({}, {})('app/ui/gestures.ts');
const font = { lineHeight: 10, measureText: (text) => text.length * 6 };
/** Records drawn text (and where, in `drawn.positions`); every other drawing call is a no-op. */
const makeImageClass = (drawn) => class {
drawText(_font, x, y, text) { drawn.push(text); (drawn.positions ??= new Map()).set(text, { x, y }); }
fillRect() {}
fillRoundedRect() {}
drawRoundedRect() {}
};
/** Stand-in for ui/menu-core's Menu: draws every row in order, scrolls the selection by one. */
class FakeMenu {
/** The box and scrollbar track of the latest paint, whichever tab's menu it was. */
static painted = { box: null, scrollbar: null };
constructor(options) {
this.options = options;
this.items = options.items;
this.selectedIndex = 0;
this.scrollTop = 0;
}
setItems(items, selectedIndex) {
this.items = items;
if (selectedIndex !== undefined) this.selectedIndex = selectedIndex;
this.selectedIndex = Math.max(0, Math.min(this.selectedIndex, items.length - 1));
}
get selectedItem() { return this.items[this.selectedIndex] ?? null; }
handleInput(event) {
if (event.type === 'scroll-down') this.selectedIndex = Math.min(this.items.length - 1, this.selectedIndex + 1);
if (event.type === 'scroll-up') this.selectedIndex = Math.max(0, this.selectedIndex - 1);
}
paint(image, box) {
FakeMenu.painted.box = box;
this.items.forEach((item, index) => this.options.draw({ image, item, index, x: box.x, y: box.y, width: box.width }));
}
drawScrollbar(_image, x, y, height) { FakeMenu.painted.scrollbar = { x, y, height }; }
}
const storeApp = (packageId, name, version) => ({
id: 1, packageId, name, version, creatorName: '', tagline: '', description: '', categories: [],
installCount: 0, likeCount: 0, firstPublishedAt: '', iconPath: '', changelog: '', fileSize: 0,
});
const emptyPage = { apps: [], total: 0, page: 1, pageSize: 20 };
/** A signed-in store layer; `updates` is what the update check reports. */
const loadStore = ({ updates = [] } = {}) => {
const drawn = [];
const installs = [];
const setting = { get: () => '', set() {} };
const load = loader({}, {
'../../graphics/ui-fonts': { getDefaultSmallFont: () => font },
'../../graphics/image': { GrayImage: makeImageClass(drawn) },
'../../graphics/textwrap': { truncateText: (_f, text) => text, wrapText: (_f, text) => [text] },
'../../ui/gestures': { ...gestures, GESTURE_CLICK: 'click' },
'../../ui/menu': { drawSelectionHighlight() {} },
'../../ui/menu-core': { Menu: FakeMenu },
'../../ui/shell/shell': { shell: { yieldFocusToSidebar() {} } },
'../../ui/metrics': { lineStep: () => 12 },
'../../ui/dashboard-settings': {
ConfigSettingString: class { constructor(options) { this.value = options.defaultValue ?? ''; }
get() { return this.value; } set(next) { this.value = next; return next; } },
},
'./even-api': {
evenHubApi: { listApps: async () => emptyPage, listNewApps: async () => emptyPage, searchApps: async () => emptyPage },
EvenHubAuthenticationError: class extends Error {},
isEvenHubStoreConfigured: () => true,
},
'./credentials': {
evenHubLoginEmailSetting: setting, evenHubLoginPasswordSetting: setting, evenHubRememberMeSetting: setting,
clearEvenHubLoginForm() {}, resetEvenHubLoginForm() {}, clearEvenHubSession() {}, clearTransientEvenHubSession() {},
},
'./installed-apps': { getInstalledEvenHubApps: () => updates.map((update) => update.installed), uninstallEvenHubPackage: () => true },
'./store-detail-layer': { EvenHubStoreDetailLayer: class {} },
'./store-install': {
cleanError: (error) => String(error?.message ?? error),
installStoreApp: async (_ctx, app) => { installs.push(app.packageId); return { packageId: app.packageId }; },
},
'./updates': {
checkForEvenHubUpdates: async () => updates,
describeUpdate: () => ({ updateAvailable: false, packageMissing: false }),
},
'./glasses-compat': { isEvenHubStoreAvailable: () => true },
});
const { EvenHubStoreLayer } = load('app/apps/evenhub/store-layer.ts');
const layer = new EvenHubStoreLayer({ launchApp: async () => {}, appendLog: () => {} });
return { layer, drawn, installs };
};
const makeCtx = (viewport = bordered(576, 288)) => ({
stack: {
getBaseSize: () => viewport,
isFocused: () => true,
clearToBase() {},
push() {},
pop() {},
},
actions: { requestRender() {}, endTextSettingEdit() {}, startTextSettingsEdit() {} },
});
const settle = () => new Promise((resolve) => setImmediate(resolve));
/** Repaint and return the drawn text. */
const repaint = (layer, drawn, ctx) => {
drawn.length = 0;
layer.paint(ctx);
return [...drawn];
};
const watch = (type) => ({ type, source: 'watch', timestampMs: 0 });
test('watch swipes left and right switch tabs while the tab strip is focused', async () => {
const { layer, drawn } = loadStore();
const ctx = makeCtx();
layer.paint(ctx);
await settle();
assert.ok(repaint(layer, drawn, ctx).includes('Public apps by popularity'));
// In the list, a right swipe is "select" and must not change tabs.
await layer.handleInput(watch('swipe-right'), ctx);
await settle();
assert.ok(repaint(layer, drawn, ctx).includes('Public apps by popularity'), 'still on Top');
// A left swipe in the list is "back", which focuses the tab strip.
await layer.handleInput(watch('swipe-left'), ctx);
await layer.handleInput(watch('swipe-right'), ctx);
await settle();
assert.ok(repaint(layer, drawn, ctx).includes('Recently published apps'), 'right moved to New');
await layer.handleInput(watch('swipe-left'), ctx);
await settle();
assert.ok(repaint(layer, drawn, ctx).includes('Public apps by popularity'), 'left moved back to Top');
// Up/down still switch tabs too.
await layer.handleInput(watch('swipe-down'), ctx);
await settle();
assert.ok(repaint(layer, drawn, ctx).includes('Recently published apps'), 'down moved to New');
});
/** Open the store and move to the Updates tab, with focus back in its list. */
const openUpdatesTab = async (layer, ctx) => {
layer.paint(ctx);
await settle();
await layer.handleInput({ type: 'double-click', source: 'ring' }, ctx);
for (let i = 0; i < 3; i++) await layer.handleInput({ type: 'scroll-down' }, ctx);
await settle();
await layer.handleInput({ type: 'click', source: 'ring' }, ctx);
};
test('the Updates tab lists Update All first and it installs every update', async () => {
const updates = [
{ installed: { packageId: 'a', name: 'Alpha', version: '1' }, latest: storeApp('a', 'Alpha', '2'), packageMissing: false },
{ installed: { packageId: 'b', name: 'Beta', version: '1' }, latest: storeApp('b', 'Beta', '3'), packageMissing: false },
{ installed: { packageId: 'c', name: 'Gamma', version: '1' } },
];
const { layer, drawn, installs } = loadStore({ updates });
const ctx = makeCtx();
await openUpdatesTab(layer, ctx);
const painted = repaint(layer, drawn, ctx);
assert.ok(painted.includes('Updates (3)'), `the tab count is apps, not rows: ${JSON.stringify(painted)}`);
assert.ok(painted.includes('3 apps need attention'));
const updateAll = painted.indexOf('Update All');
assert.ok(updateAll >= 0, `painted: ${JSON.stringify(painted)}`);
assert.ok(painted.includes('Install all 2 available updates'), 'the orphan is not counted');
assert.ok(updateAll < painted.indexOf('Alpha') && updateAll < painted.indexOf('Gamma'), 'Update All is the first row');
// The list opens on Update All; clicking it installs both updatable apps.
await layer.handleInput({ type: 'click', source: 'ring' }, ctx);
await settle();
assert.deepEqual(installs, ['a', 'b']);
const after = repaint(layer, drawn, ctx);
assert.ok(!after.includes('Update All'), 'nothing left to update, so the row goes');
assert.ok(after.includes('Gamma'), 'the missing package stays listed');
assert.ok(after.includes('Updated 2 apps.'));
});
test('the Updates tab has no Update All row when nothing can be installed', async () => {
const updates = [{ installed: { packageId: 'c', name: 'Gamma', version: '1' } }];
const { layer, drawn } = loadStore({ updates });
const ctx = makeCtx();
await openUpdatesTab(layer, ctx);
const painted = repaint(layer, drawn, ctx);
assert.ok(painted.includes('Gamma'));
assert.ok(!painted.includes('Update All'));
});
test('the list scrolls to the window\'s bottom edge under the fixed header, inside the side padding', async () => {
const updates = [{ installed: { packageId: 'c', name: 'Gamma', version: '1' } }];
const layout = async (viewport) => {
const { layer, drawn } = loadStore({ updates });
const ctx = makeCtx(viewport);
await openUpdatesTab(layer, ctx);
repaint(layer, drawn, ctx);
return { ...FakeMenu.painted, title: drawn.positions.get('EvenHub'), row: drawn.positions.get('Gamma') };
};
// Framed: rows start at the left padding and stop short of the scrollbar,
// which hugs the right padding. The header keeps the top padding; the list
// box runs to the bottom edge with that edge's padding inside it.
const framed = await layout(bordered(576, 288));
assert.equal(framed.box.x, 8);
assert.equal(framed.box.x + framed.box.width, 576 - 8 - 6);
assert.equal(framed.box.y + framed.box.height, 288, 'the box reaches the bottom edge');
assert.equal(framed.box.padTop, 0, 'no padding under the fixed header');
assert.equal(framed.box.padBottom, 8);
assert.deepEqual(framed.title, { x: 14, y: 11 }, 'a focused tab\'s highlight, 3px above the title line, sits at the padding');
assert.equal(framed.row.x, framed.title.x, 'the title lines up with the row text');
assert.deepEqual(framed.scrollbar, { x: 576 - 8 - 3, y: framed.box.y, height: 288 - 8 - framed.box.y });
// Nothing bordering the window: content runs to every edge.
const bare = await layout(unbordered(576, 288));
assert.equal(bare.box.x, 0);
assert.equal(bare.box.padBottom, 0);
assert.equal(bare.box.y + bare.box.height, 288);
assert.deepEqual(bare.title, { x: 6, y: 3 });
assert.equal(bare.box.y, framed.box.y - 8, 'the header moves up by the top padding');
assert.equal(bare.scrollbar.x, 576 - 3, 'the scrollbar stays clear of the rows');
assert.ok(bare.box.x + bare.box.width <= bare.scrollbar.x);
// Each edge follows its own padding.
const mixed = await layout(edged(576, 288, 'top', 'left'));
assert.equal(mixed.box.x, 8);
assert.equal(mixed.box.x + mixed.box.width, 576 - 6);
assert.equal(mixed.box.y, framed.box.y);
assert.equal(mixed.box.padBottom, 0);
});
// ---- permission dialog ---------------------------------------------------
const loadDialog = (detailLines) => {
const drawn = [];
const load = loader({}, {
'../../graphics/ui-fonts': { getDefaultSmallFont: () => font },
'../../graphics/image': { GrayImage: makeImageClass(drawn) },
'../../graphics/textwrap': { truncateText: (_f, text) => text, wrapText: (_f, text) => text.split('|') },
'../../ui/gestures': gestures,
'../../ui/layers': {},
'../../ui/menu': { drawListScrollbar() {}, drawSelectionHighlight() {} },
'./permissions': { permissionDetail: (permission) => permission.detail, permissionLabel: (name) => name },
'./privacy-policy': { openPrivacyPolicyOnPhone() {} },
'../../ui/metrics': { lineStep: () => 12, listRowHeight: () => 20 },
});
const { EvenHubPermissionDialogLayer } = load('app/apps/evenhub/permission-dialog.ts');
const ImageClass = makeImageClass(drawn);
const outcome = { confirmed: null };
const dialog = new EvenHubPermissionDialogLayer(
'Demo',
[{ name: 'network', detail: detailLines.join('|') }],
'',
() => { outcome.confirmed = true; },
() => { outcome.confirmed = false; },
);
const ctx = { stack: { getBaseSize: () => bordered(576, 136), isFocused: () => true, pop() {} } };
const paint = () => {
drawn.length = 0;
dialog.paint(ctx, () => new ImageClass());
return [...drawn];
};
return { dialog, ctx, paint, outcome };
};
test('the permission dialog scrolls a page at a time with one line of overlap', () => {
const lines = Array.from({ length: 30 }, (_, i) => `line ${i + 1}`);
const { dialog, ctx, paint, outcome } = loadDialog(lines);
const pages = [paint()];
while (!pages[pages.length - 1].includes('Allow')) {
assert.ok(pages.length < 20, 'paging reaches the actions');
dialog.handleInput({ type: 'scroll-down' }, ctx);
pages.push(paint());
}
assert.ok(pages.length >= 3, `the list is long enough to need several pages (${pages.length})`);
// Every page but the last (which stops at the bottom) starts on the line the previous one ended on.
for (let page = 1; page < pages.length - 1; page++) {
const previous = pages[page - 1];
assert.equal(pages[page][0], previous[previous.length - 1], `page ${page + 1} overlaps page ${page} by one line`);
assert.ok(pages[page].length > 2, 'a page moves by more than one line');
}
// Every line was on screen on some page.
for (const line of lines) assert.ok(pages.some((page) => page.includes(line)), `${line} was shown`);
// Paging up from the bottom: the old first line becomes the new last one.
const bottom = pages[pages.length - 1];
dialog.handleInput({ type: 'scroll-up' }, ctx);
const up = paint();
assert.equal(up[up.length - 1], bottom[0], 'one line of overlap going up');
assert.ok(up.length > 2);
for (let i = 0; i < pages.length; i++) dialog.handleInput({ type: 'scroll-up' }, ctx);
assert.deepEqual(paint(), pages[0], 'back at the top');
// A click before the bottom pages down rather than allowing; scrolling up returns.
dialog.handleInput({ type: 'click', source: 'ring' }, ctx);
assert.deepEqual(paint(), pages[1]);
assert.equal(outcome.confirmed, null);
dialog.handleInput({ type: 'scroll-up' }, ctx);
assert.deepEqual(paint(), pages[0]);
});
+14
View File
@@ -74,3 +74,17 @@ test("reportedFirmwareVersion picks the higher arm", () => {
assert.equal(compat.reportedFirmwareVersion(info("", "", "2.2.9.22")), "2.2.9.22");
assert.equal(compat.reportedFirmwareVersion(info("", "", "")), "");
});
test("a G1's report names what the arms run and offers the install, stock or too old", () => {
const stock = compat.firmwareIncompatibilityMessage(info("", "Even's firmware", "Even's firmware"));
assert.match(stock, /G1/);
assert.match(stock, /Even's firmware/);
assert.match(stock, /install/);
assert.doesNotMatch(stock, /revision/);
const older = compat.firmwareIncompatibilityMessage(info("", "g1v0 0.5.62", "g1v0 0.5.63"));
assert.match(older, /L=g1v0 0\.5\.62 R=g1v0 0\.5\.63/);
assert.match(older, /install/);
// A compatible G1 reports the extension, and a G2's stock report is unchanged.
assert.equal(compat.firmwareIncompatibilityMessage(info(`Faceclaw/${REQUIRED}`, "g1v0 0.5.63", "g1v0 0.5.63")), null);
assert.match(compat.firmwareIncompatibilityMessage(info("")), /stock firmware/);
});
+93
View File
@@ -0,0 +1,93 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { bordered, unbordered, edged } = require('./helpers/viewport.cjs');
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, `${file}: ${name}`); return modules[name]; } };
vm.runInNewContext(ts.transpileModule(source, {
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 },
}).outputText, context);
return context.exports;
}
const build = (name) => require(`../.test-build/app/${name}.js`);
const graphics = build('graphics/image');
const metrics = build('ui/metrics');
const { BdfFont } = load('app/graphics/bdffont.ts', { '@nativescript/core': {} });
const bdf = (size) => BdfFont.parse(fs.readFileSync(path.join(__dirname, `../app/fonts/terminus/ter-u${size}n.bdf`), 'utf8'));
const font = bdf(14);
const textwrap = load('app/graphics/textwrap.ts', {});
const coreDeps = {
'../graphics/image': graphics, './menu-highlight-motion': build('ui/menu-highlight-motion'),
'../graphics/menu-scroll-list': build('graphics/menu-scroll-list'), './menu-scroll-motion': build('ui/menu-scroll-motion'),
'../graphics/draw-expression': build('graphics/draw-expression'),
};
const menuCore = load('app/ui/menu-core.ts', coreDeps);
const menu = load('app/ui/menu.ts', {
...coreDeps, './menu-animation-pref': build('ui/menu-animation-pref'), './animation-speed': build('ui/animation-speed'),
'../native/settings-store': { getStringSetting: (_key, fallback) => fallback },
'../graphics/screen-size': { screenWidth: () => 540, screenHeight: () => 220 },
'../graphics/textwrap': textwrap, '../graphics/ui-fonts': { getDefaultSmallFont: () => font },
'../util/numeric-util': { clamp: (n, lo, hi) => Math.max(lo, Math.min(hi, n)) }, './gestures': {}, './metrics': metrics, './menu-core': menuCore,
});
// The UI font picker on Roboto Light 20, drawn in Terminus (TTF rendering is native).
const preview = bdf(20);
const picker = load('app/ui/font-picker.ts', {
'../graphics/bdffont': { getFont: () => font }, '../graphics/image': graphics, '../graphics/textwrap': textwrap,
'../graphics/ttf-font': { TtfFont: { load: () => preview } },
'../graphics/installed-fonts': { listInstalledFonts: () => [{ path: '/fonts/Roboto-Light.ttf', fileName: 'Roboto-Light.ttf',
family: 'Roboto', style: 'Light', weightOrder: 300, monospace: false }] },
'../graphics/ui-fonts': { fontSelectionLabel: () => 'Roboto Light 20', getDefaultSmallFont: () => font, uiFontSizeAllowed: () => true,
getTerminalFontSelection: () => null, getUiFontSelection: () => ({ kind: 'ttf', file: 'Roboto-Light.ttf', size: 20 }),
setTerminalFontSelection() {}, setUiFontSelection() {} },
'./gestures': {}, './menu': menu, './menu-core': menuCore, './metrics': metrics,
});
function stackOf(size) {
const layers = [];
return { layers, ctx: { stack: { getBaseSize: () => size, isFocused: () => true, push: (layer) => layers.push(layer) } } };
}
const glyphRuns = (image, f) => image.draws.filter((d) => d.kind === 'glyph' && d.font === f);
test('the font picker previews under its rows where they fit, else beside them, on the window either way', () => {
// At 192 rows either fits, by the font's metrics: only the bounds are checked.
// Framed windows, and ones nothing borders (so no padding: the rows reach the edges).
for (const [size, beside] of [[bordered(576, 260), false], [bordered(476, 192), null], [bordered(476, 155), true],
[unbordered(576, 260), false], [unbordered(476, 155), true], [edged(576, 260, 'top', 'left'), false]]) {
const { layers, ctx } = stackOf(size);
picker.uiFontPickerMenuItem().onSelect(ctx);
const image = layers[0].paint(ctx);
const sample = glyphRuns(image, preview);
// The rows' labels and values (255 selected, 200 not; the title is 220, the info line 110).
const rows = glyphRuns(image, font).filter((d) => d.value === 255 || d.value === 200);
assert.ok(sample.length > 20, `the sample is drawn at ${size.height}`);
for (const d of image.draws) {
if (d.kind !== 'glyph') continue;
assert.ok(d.x >= 0 && d.y >= 0 && d.x + d.glyph.bbxWidth <= size.width && d.y + d.font.lineHeight <= size.height,
`glyph at ${d.x},${d.y} outside ${size.width}x${size.height}`);
}
const rowsRight = Math.max(...rows.map((d) => d.x)), rowsBottom = Math.max(...rows.map((d) => d.y));
if (beside === true) assert.ok(Math.min(...sample.map((d) => d.x)) > rowsRight, 'the sample right of the rows');
if (beside === false) assert.ok(Math.min(...sample.map((d) => d.y)) > rowsBottom, 'the sample under the rows');
// The rows' text keeps the same inset from wherever the padding puts their boxes.
assert.equal(Math.min(...rows.map((d) => d.x)), size.padding.left + 10);
}
});
test('a modal menu in a short window (a RayNeo iO\'s) keeps narrow margins, so four choices all show', () => {
const items = ['Left', 'Right', 'Bottom', 'Popup'].map((label) => ({ label, onSelect() {} }));
for (const [size, margin] of [[{ width: 576, height: 260 }, 20], [{ width: 476, height: 155 }, 8]]) {
const { layers, ctx } = stackOf(size);
menu.openModalMenu(ctx, 'App switcher position', items, 0);
const { layout } = layers[0];
assert.ok(layout.y >= margin && layout.y + layout.maxHeight <= size.height - margin, `${size.height}: ${layout.y}+${layout.maxHeight}`);
// Title, body padding (8 above and below) and the four rows.
const natural = metrics.menuTitleHeight(font) + 2 * 8 + items.length * metrics.listRowHeight(font);
assert.equal(layout.maxHeight, natural, `${size.height}: every row fits`);
}
});
+220
View File
@@ -0,0 +1,220 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const crypto = require('node:crypto');
const zlib = require('node:zlib');
const { readZipEntry, assembleG1Image, decodeG1OwnBytes, extractEvenG1Images, G1FirmwareError } = require('../.test-build/app/g1/firmware-image.js');
const { EVEN_G1_RESTORE } = require('../.test-build/app/g1/even-firmware.js');
const plan = require('../.test-build/app/g1/flash-plan.js');
const { G1V0_IMAGE } = require('../.test-build/app/g1/firmware/g1v0-image.js');
const { G1V0_OWN_BYTES_BASE64 } = require('../.test-build/app/g1/firmware/g1v0-own-bytes.js');
const sha = (bytes) => crypto.createHash('sha256').update(bytes).digest('hex');
const noise = (length, seed) => Uint8Array.from({ length }, (_, i) => (i * 31 + seed * 17 + (i >> 3)) & 0xff);
const fromBase64 = (text) => new Uint8Array(Buffer.from(text, 'base64'));
/** A zip the way Even's are made: one entry stored, the others deflated, under a folder. */
function zipOf(entries) {
const locals = [];
const directory = [];
let offset = 0;
for (const { name, data, deflate } of entries) {
const packed = deflate ? zlib.deflateRawSync(data) : Buffer.from(data);
const nameBytes = Buffer.from(name);
const local = Buffer.alloc(30);
local.writeUInt32LE(0x04034b50, 0);
local.writeUInt16LE(deflate ? 8 : 0, 8);
local.writeUInt32LE(packed.length, 18);
local.writeUInt32LE(data.length, 22);
local.writeUInt16LE(nameBytes.length, 26);
const central = Buffer.alloc(46);
central.writeUInt32LE(0x02014b50, 0);
central.writeUInt16LE(deflate ? 8 : 0, 10);
central.writeUInt32LE(packed.length, 20);
central.writeUInt32LE(data.length, 24);
central.writeUInt16LE(nameBytes.length, 28);
central.writeUInt32LE(offset, 42);
locals.push(local, nameBytes, packed);
directory.push(central, nameBytes);
offset += local.length + nameBytes.length + packed.length;
}
const centralBytes = Buffer.concat(directory);
const end = Buffer.alloc(22);
end.writeUInt32LE(0x06054b50, 0);
end.writeUInt16LE(entries.length, 8);
end.writeUInt16LE(entries.length, 10);
end.writeUInt32LE(centralBytes.length, 12);
end.writeUInt32LE(offset, 16);
return new Uint8Array(Buffer.concat([...locals, centralBytes, end]));
}
function fixture() {
const net = noise(5000, 1);
const app = noise(9000, 2);
const image = noise(20000, 3);
const table = app.subarray(4000, 4352);
image.set(net, 3000);
image.set(table, 12000);
const parts = [
{ offset: 3000, length: net.length, file: 'net.bin', fileOffset: 0, sha256: sha(net) },
{ offset: 12000, length: table.length, file: 'app.bin', fileOffset: 4000, sha256: sha(table) },
];
const own = Buffer.concat([image.subarray(0, 3000), image.subarray(8000, 12000), image.subarray(12352)]);
const manifest = { version: '0.0.1', sha256: sha(image), size: image.length, source: 't', stockVersion: 's', stockZipUrl: 'u', stockZipSha256: 'z', parts };
return { net, app, image, own: new Uint8Array(own), manifest };
}
test('a zip entry is found, deflated or stored, also under a folder', () => {
const stored = noise(300, 4);
const packed = noise(70000, 5);
const zip = zipOf([{ name: 'manifest.json', data: stored }, { name: 'firmware/app.bin', data: packed, deflate: true }]);
assert.deepEqual(readZipEntry(zip, 'manifest.json'), stored);
assert.deepEqual(Buffer.from(readZipEntry(zip, 'app.bin')), Buffer.from(packed));
assert.throws(() => readZipEntry(zip, 'missing.bin'), /no missing\.bin/);
assert.throws(() => readZipEntry(noise(100, 6), 'x'), /not a zip/);
});
test('the parts go back where they were, byte for byte', () => {
const { net, app, image, own, manifest } = fixture();
const files = { 'net.bin': net, 'app.bin': app };
const out = assembleG1Image(manifest, own, (name) => files[name], sha);
assert.deepEqual(Buffer.from(out), Buffer.from(image));
});
test('a part that is not the one it was built with is refused', () => {
const { net, app, own, manifest } = fixture();
const bad = net.slice();
bad[100] ^= 1;
const files = { 'net.bin': bad, 'app.bin': app };
assert.throws(() => assembleG1Image(manifest, own, (name) => files[name], sha), G1FirmwareError);
const short = { 'net.bin': net.subarray(0, 100), 'app.bin': app };
assert.throws(() => assembleG1Image(manifest, own, (name) => short[name], sha), /not the one/);
});
test('damaged bundled bytes never pass as an image', () => {
const { net, app, own, manifest } = fixture();
const files = { 'net.bin': net, 'app.bin': app };
const flipped = own.slice();
flipped[10] ^= 1;
assert.throws(() => assembleG1Image(manifest, flipped, (name) => files[name], sha), /failed verification/);
assert.throws(() => assembleG1Image(manifest, own.subarray(0, own.length - 1), (name) => files[name], sha), G1FirmwareError);
});
test('the bundled firmware is whole and carries none of Even\'s bytes in its manifest ranges', () => {
const own = decodeG1OwnBytes(G1V0_OWN_BYTES_BASE64, fromBase64);
const taken = G1V0_IMAGE.parts.reduce((sum, part) => sum + part.length, 0);
assert.equal(own.length + taken, G1V0_IMAGE.size);
// MCUboot's magic and the version in its header, ahead of the first part.
assert.equal(Buffer.from(own.subarray(0, 4)).toString('hex'), '3db8f396');
assert.equal(`${own[20]}.${own[21]}.${own[22] | (own[23] << 8)}`, G1V0_IMAGE.version);
assert.match(G1V0_IMAGE.stockZipUrl, /^https:\/\/cdn\.evenreal\.co\//);
assert.match(G1V0_IMAGE.sha256, /^[0-9a-f]{64}$/);
const sorted = [...G1V0_IMAGE.parts].sort((a, b) => a.offset - b.offset);
assert.ok(sorted[0].offset > 512 && sorted.at(-1).offset + sorted.at(-1).length < G1V0_IMAGE.size);
assert.doesNotMatch(G1V0_IMAGE.source, /uncommitted/, 'ship only a firmware that was committed');
});
test('versions compare by number, not by text', () => {
assert.ok(plan.compareG1Versions('0.5.100', '0.5.63') > 0);
assert.ok(plan.compareG1Versions('0.5.9', '0.5.63') < 0);
assert.equal(plan.compareG1Versions('0.5.63', '0.5.63'), 0);
assert.equal(plan.compareG1Versions('0.5', '0.5.0'), 0);
assert.ok(plan.compareG1Versions('', '0.5.63') < 0);
});
test('a pair is chosen by serial, then address, and never guessed between two', () => {
const g1 = (serial, left, right) => ({ g1Firmware: 'g1v0', serial, left: left && { address: left }, right: right && { address: right } });
const ours = g1('S1', 'L1', 'R1');
const theirs = g1('S2', 'L2', 'R2');
assert.equal(plan.pickG1Pair([theirs, ours], { serial: 's1' }), ours);
assert.equal(plan.pickG1Pair([theirs, ours], { left: 'L1' }), ours);
assert.equal(plan.pickG1Pair([theirs, ours], { right: 'R2' }), theirs);
assert.equal(plan.pickG1Pair([theirs, ours], {}), null);
assert.equal(plan.pickG1Pair([ours], {}), ours);
// A flash changed the addresses: the serial still finds it.
assert.equal(plan.pickG1Pair([g1('S1', 'NEW', 'NEW2'), theirs], { serial: 'S1', left: 'OLD', right: 'OLD2' }).left.address, 'NEW');
// Our serial with one arm heard so far: wait, even if another pair is complete.
assert.equal(plan.pickG1Pair([g1('S1', 'L1', null), theirs], { serial: 'S1' }), null);
// The wanted glasses weren't heard: another pair is never a stand-in, even the only one around.
assert.equal(plan.pickG1Pair([theirs], { serial: 'S1' }), null);
assert.equal(plan.pickG1Pair([theirs], { left: 'L1', right: 'R1' }), null);
// A wanted address on one side doesn't match a pair for lacking the other.
assert.equal(plan.pickG1Pair([g1('S2', 'L2', null)], { left: 'L1' }), null);
// G2 pairs don't count.
assert.equal(plan.pickG1Pair([{ serial: 'S9', left: { address: 'a' }, right: { address: 'b' } }], {}), null);
});
test('the state of the two arms is that of the arm that needs the most', () => {
const on = (version, extra = {}) => ({ firmware: 'g1v0', version, safeMode: false, ...extra });
const state = (...arms) => plan.g1FirmwareState(arms, '0.5.63');
assert.equal(state(on('0.5.63'), on('0.5.63')), 'current');
assert.equal(state(on('0.5.64'), on('0.5.63')), 'current');
assert.equal(state(on('0.5.62'), on('0.5.63')), 'older');
assert.equal(state(on('0.5.63', { safeMode: true }), on('0.5.62')), 'safe-mode');
assert.equal(state({ firmware: 'stock' }, on('0.5.63', { safeMode: true })), 'stock');
});
test('what the advertising alone says about the arms', () => {
const arm = (g1Firmware, g1Version) => ({ g1Firmware, g1Version });
const state = (left, right) => plan.g1StateFromAdvertising({ left, right }, '0.5.66');
assert.deepEqual(state(arm('g1v0', '0.5.66'), arm('g1v0', '0.5.66')), { state: 'current', version: '0.5.66' });
assert.deepEqual(state(arm('g1v0', '0.5.67'), arm('g1v0', '0.5.66')), { state: 'current', version: '0.5.66' });
assert.deepEqual(state(arm('g1v0', '0.5.66'), arm('g1v0', '0.5.100')), { state: 'current', version: '0.5.66' });
assert.deepEqual(state(arm('stock'), arm('g1v0', '0.5.66')), { state: 'stock', version: '' });
assert.deepEqual(state(arm('g1v0-safe', '0.5.66'), arm('g1v0', '0.5.66')), { state: 'safe-mode', version: '' });
// Firmware before 0.5.66, or a record that hasn't arrived: an arm has to be asked.
assert.equal(state(arm('g1v0'), arm('g1v0', '0.5.66')), null);
assert.equal(state(arm('g1v0'), arm('g1v0')), null);
});
test('an older arm wins over a newer one', () => {
const arm = (g1Version) => ({ g1Firmware: 'g1v0', g1Version });
assert.deepEqual(plan.g1StateFromAdvertising({ left: arm('0.5.67'), right: arm('0.5.65') }, '0.5.66'), { state: 'older', version: '0.5.65' });
});
test('the native flow\'s answer is read, and nonsense is no answer', () => {
assert.deepEqual(plan.parseG1FlashResult('{"flashed":true,"version":"0.5.64"}'), { flashed: true, version: '0.5.64' });
assert.deepEqual(plan.parseG1FlashResult('nonsense'), {});
assert.deepEqual(plan.parseG1FlashResult(''), {});
assert.deepEqual(plan.parseG1FlashResult('null'), {});
});
test('an uninstall goes right first, and its bar follows that order', () => {
assert.deepEqual([...plan.G1_UNINSTALL_SIDES], ['right', 'left']);
assert.equal(plan.g1FlashPercent('right', 0, 100, plan.G1_UNINSTALL_SIDES), 0);
assert.equal(plan.g1FlashPercent('right', 100, 100, plan.G1_UNINSTALL_SIDES), 50);
assert.equal(plan.g1FlashPercent('left', 50, 100, plan.G1_UNINSTALL_SIDES), 75);
});
test('Even\'s two images come out of its download only when each is the one published', () => {
const app = noise(9000, 7);
const netCore = noise(4000, 8);
const zip = zipOf([
{ name: 'app_update.bin', data: app },
{ name: 'net_core_app_update.bin', data: netCore, deflate: true },
{ name: 'manifest.json', data: noise(100, 9) },
]);
const release = { version: '9.9.9', zipUrl: 'u', zipSha256: sha(zip), appSha256: sha(app), netCoreSha256: sha(netCore) };
const out = extractEvenG1Images(zip, release, sha);
assert.deepEqual(Buffer.from(out.app), Buffer.from(app));
assert.deepEqual(Buffer.from(out.netCore), Buffer.from(netCore));
assert.throws(() => extractEvenG1Images(zip, { ...release, zipSha256: sha(app) }, sha), (e) => e instanceof G1FirmwareError && /download failed/.test(e.message));
assert.throws(() => extractEvenG1Images(zip, { ...release, netCoreSha256: sha(app) }, sha), /net_core_app_update\.bin in Even's download/);
});
test('the release an uninstall puts back is the one the bundled firmware was built from', () => {
// g1v0 embeds Even's network core from this same download, which the bundle's manifest checks by hash.
assert.equal(EVEN_G1_RESTORE.version, G1V0_IMAGE.stockVersion);
assert.equal(EVEN_G1_RESTORE.zipUrl, G1V0_IMAGE.stockZipUrl);
assert.equal(EVEN_G1_RESTORE.zipSha256, G1V0_IMAGE.stockZipSha256);
const net = G1V0_IMAGE.parts.find((part) => part.file === 'net_core_app_update.bin' && part.fileOffset === 0);
assert.equal(net?.sha256, EVEN_G1_RESTORE.netCoreSha256);
});
test('each arm counts for half of the bar', () => {
assert.equal(plan.g1FlashPercent('left', 0, 100), 0);
assert.equal(plan.g1FlashPercent('left', 100, 100), 50);
assert.equal(plan.g1FlashPercent('right', 0, 100), 50);
assert.equal(plan.g1FlashPercent('right', 100, 100), 100);
assert.equal(plan.g1FlashPercent('right', 5, 0), 50);
});
+46 -3
View File
@@ -20,18 +20,26 @@ function load(file, dependencies) {
function harness() {
const values = new Map();
const listeners = new Set();
const live = new Set();
const glasses = { haveCompass: true };
class Setting {
constructor(options) { Object.assign(this, options); }
get() { return values.get(this.storageKey) ?? this.defaultValue; }
get() {
const value = values.get(this.storageKey) ?? this.defaultValue;
return this.normalize ? this.normalize(value) : value;
}
set(value) { values.set(this.storageKey, value); }
}
const config = {
ConfigSettingBoolean: Setting, ConfigSettingEnum: Setting,
glassesHaveCompass: () => glasses.haveCompass,
onAnySettingChanged: (listener) => { listeners.add(listener); return () => listeners.delete(listener); },
};
const widgets = {
glanceWidgetSpans: (choice) => choice === 'calendar' || choice === 'terminal',
findGlanceWidget: () => ({ create: () => ({ start() {}, stop() {}, paint() {} }) }),
findGlanceWidget: (choice) => ({ create: () => ({
start() { live.add(choice); }, stop() { live.delete(choice); }, paint() {},
}) }),
};
const settings = load('app/apps/glanceboard/glanceboard-settings.ts', {
'../../ui/dashboard-settings': config, './layout': layout, './widgets': widgets,
@@ -45,7 +53,7 @@ function harness() {
'../../graphics/image': { GrayImage }, '../../ui/dashboard-settings': config,
'./glanceboard-settings': settings, './layout': layout, './widgets': widgets,
});
return { settings, GlanceBoard, values, listeners, notify: () => [...listeners].forEach((listener) => listener()) };
return { settings, GlanceBoard, values, listeners, live, glasses, notify: () => [...listeners].forEach((listener) => listener()) };
}
test('layout defaults to 2x2; expanding preserves stored slots and remembers the extra row', () => {
@@ -97,3 +105,38 @@ test('an active empty board resizes and requests a frame when the layout changes
board.stop();
assert.equal(h.listeners.size, 0);
});
test('glasses without a compass: no compass in the picker, and a stored one reads as Empty until a G2 returns', () => {
const h = harness();
h.values.set('glanceboard.quadrants.slot.0', 'compass');
assert.ok(h.settings.glanceSlotSettings()[0].values.includes('compass'));
assert.equal(h.settings.glanceSlotSettings()[0].get(), 'compass');
h.glasses.haveCompass = false;
const [slot] = h.settings.glanceSlotSettings();
assert.ok(!slot.values.includes('compass'));
// Empty, not the slot's default: System card may already be elsewhere on the board.
assert.equal(slot.get(), 'none');
assert.equal(h.values.get('glanceboard.quadrants.slot.0'), 'compass');
h.glasses.haveCompass = true;
assert.equal(h.settings.glanceSlotSettings()[0].get(), 'compass');
});
test('an unknown stored slot value still falls back to the slot default', () => {
const { settings: s, values } = harness();
values.set('glanceboard.quadrants.slot.0', 'removed-widget');
assert.equal(s.glanceSlotSettings()[0].get(), 'system-card');
});
test('a live board drops the compass widget when the paired glasses have no compass', () => {
const h = harness();
h.values.set('glanceboard.quadrants.slot.0', 'compass');
const board = new h.GlanceBoard(() => {});
board.start();
assert.ok(h.live.has('compass'));
h.glasses.haveCompass = false;
h.notify();
assert.ok(!h.live.has('compass'));
board.stop();
});
+19 -12
View File
@@ -15,6 +15,7 @@ const connected = {
silentMode: false,
screenOn: true,
battery: 72,
glassesModel: "G2",
caseBattery: null,
caseCharging: null,
foregroundTitle: "Music",
@@ -37,22 +38,23 @@ test("only the Display off state counts as asleep (the watch face's wake hint)",
assert.equal(glassesDisplayAsleep({ ...connected, phase: "disconnected", screenOn: false }), false);
});
test("charging takes precedence and includes the last-known G2 battery", () => {
test("charging takes precedence and includes the glasses' model and last-known battery", () => {
assert.equal(
glassesDisplayLabel({ ...connected, phase: "charging", silentMode: true, screenOn: false }),
"Charging · G2 72%",
"Charging: G2 72%",
);
assert.equal(chargingDisplayLabel(null), "Charging");
assert.equal(chargingDisplayLabel(-1), "Charging");
assert.equal(chargingDisplayLabel(101), "Charging");
assert.equal(glassesDisplayLabel({ ...connected, phase: "charging", glassesModel: "G1" }), "Charging: G1 72%");
assert.equal(chargingDisplayLabel(null, "G2"), "Charging");
assert.equal(chargingDisplayLabel(-1, "G2"), "Charging");
assert.equal(chargingDisplayLabel(101, "G2"), "Charging");
});
test("while charging, the case level is the watch face's second line and joins the mirror's message", () => {
const charging = { ...connected, phase: "charging", caseBattery: 64, caseCharging: false };
assert.equal(glassesDisplayLabel(charging), "Charging · G2 72%");
assert.equal(glassesDisplayLabel(charging), "Charging: G2 72%");
assert.equal(glassesDisplayDetail(charging), "Case 64%");
assert.equal(glassesDisplayDetail({ ...charging, caseCharging: true }), "Case 64% ⚡");
assert.equal(chargingPreviewMessage(charging), "Charging · G2 72% · Case 64%");
assert.equal(chargingPreviewMessage(charging), "Charging: G2 72% · Case 64%");
// No case level yet: the label alone.
assert.equal(glassesDisplayDetail({ ...charging, caseBattery: null }), "");
assert.equal(chargingPreviewMessage({ ...charging, caseBattery: null, battery: null }), "Charging");
@@ -65,14 +67,19 @@ const levels = {
};
test("the Settings battery line lists each known level, with ⚡ only while the reading is live", () => {
assert.equal(formatBatteryLine(levels, true), "G2 87% ⚡ · Case 64% ⚡ · R1 72%");
assert.equal(formatBatteryLine(levels, true, "G2"), "G2 87% ⚡ · Case 64% ⚡ · R1 72%");
// Without a link to the glasses their charging state and the case's are stale.
assert.equal(formatBatteryLine({ ...levels, ringCharging: true }, false), "G2 87% · Case 64% · R1 72% ⚡");
assert.equal(formatBatteryLine({ ...levels, ringCharging: true }, false, "G2"), "G2 87% · Case 64% · R1 72% ⚡");
// caseCharging is null while the glasses are out of the case.
assert.equal(formatBatteryLine({ ...levels, caseCharging: null }, true), "G2 87% ⚡ · Case 64% · R1 72%");
assert.equal(formatBatteryLine({ ...levels, case: null, ring: null }, true), "G2 87% ⚡");
assert.equal(formatBatteryLine({ ...levels, caseCharging: null }, true, "G2"), "G2 87% ⚡ · Case 64% · R1 72%");
assert.equal(formatBatteryLine({ ...levels, case: null, ring: null }, true, "G2"), "G2 87% ⚡");
assert.equal(
formatBatteryLine({ headset: null, headsetCharging: null, case: null, caseCharging: null, ring: null, ringCharging: null }, true),
formatBatteryLine({ headset: null, headsetCharging: null, case: null, caseCharging: null, ring: null, ringCharging: null }, true, "G2"),
"",
);
});
test("the Settings battery line names the paired glasses", () => {
assert.equal(formatBatteryLine({ ...levels, case: null, ring: null }, true, "G1"), "G1 87% ⚡");
assert.equal(formatBatteryLine({ ...levels, case: null, ring: null }, true, "iO"), "iO 87% ⚡");
});
+23
View File
@@ -0,0 +1,23 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { GrayImage } = require('../.test-build/app/graphics/image.js');
test('a non-integer size rounds up, warns, and leaves every pixel addressable', t => {
const warn = t.mock.method(console, 'warn', () => {});
const image = new GrayImage(78.625, 23.5, 7);
assert.deepEqual([image.width, image.height, image.pixels.length], [79, 24, 79 * 24]);
assert.ok(image.pixels.every(value => value === 7));
for (let y = 0; y < image.height; y++) {
image.setPixel(78, y, 200);
assert.equal(image.getPixel(78, y), 200, `row ${y}`);
}
assert.equal(warn.mock.callCount(), 1);
assert.match(warn.mock.calls[0].arguments[0], /78\.625x23\.5/);
});
test('a whole-number size is kept as given, without a warning', t => {
const warn = t.mock.method(console, 'warn', () => {});
const image = new GrayImage(79, 24);
assert.deepEqual([image.width, image.height], [79, 24]);
assert.equal(warn.mock.callCount(), 0);
});
+17
View File
@@ -0,0 +1,17 @@
// Viewports for layout tests: what ctx.stack.getBaseSize() (or a worker's
// open-window message) hands a layout. Needs `tsc -p tests/tsconfig.json`.
const viewport = require('../../.test-build/app/ui/viewport.js');
/** A window with a border line along every edge (a framed window): 8px of padding all round. */
const bordered = (width, height) => viewport.viewportWith({ width, height }, viewport.uniformEdges('border'));
/** A window nothing borders (Border: None, or immersive): no padding. */
const unbordered = (width, height) => viewport.viewportWith({ width, height }, viewport.uniformEdges('offscreen'));
/** A window with border lines along just the named edges, e.g. edged(576, 260, 'top', 'left'). */
const edged = (width, height, ...borders) => viewport.viewportWith({ width, height }, {
top: 'offscreen', right: 'offscreen', bottom: 'offscreen', left: 'offscreen',
...Object.fromEntries(borders.map((edge) => [edge, 'border'])),
});
module.exports = { ...viewport, bordered, unbordered, edged };
+45 -8
View File
@@ -65,6 +65,7 @@ function loadIconGrid({ lastInputWasWatch = false } = {}) {
'./menu-animation-pref': build('ui/menu-animation-pref'),
'./animation-speed': build('ui/animation-speed'),
'../native/settings-store': { getStringSetting: (_key, fallback) => fallback },
'../graphics/screen-size': { screenWidth: () => 640, screenHeight: () => 480 },
});
const grid = load('app/ui/icon-grid.ts', {
...common,
@@ -123,18 +124,53 @@ function shown(image, at) {
return withClock(at, () => Array.from(image.withDrawsBaked().pixels, (v) => Math.min(15, (v + 8) >> 4)));
}
test('a box topped at the band inset less the highlight inset pads the first row band evenly', () => {
// The launcher's root grid (launcher-app.ts ROOT_GRID_TOP) relies on this.
const { grid } = makeGrid();
const { ICON_GRID_BAND_INSET_X: insetX, ICON_GRID_HIGHLIGHT_INSET_Y: insetY } = loadIconGrid();
const image = paint(grid, 1000, { x: 0, y: insetX - insetY, width: 500, height: 190 });
/** Bounds of the selection highlight's stroke in a painted image: [top, left, right margin]. */
function bandBounds(image) {
const stroke = build('ui/menu-core').MENU_HIGHLIGHT_STROKE;
let left = 500, right = -1, top = 200;
image.pixels.forEach((v, i) => {
if (v !== stroke) return;
left = Math.min(left, i % 500); right = Math.max(right, i % 500); top = Math.min(top, (i / 500) | 0);
});
assert.deepEqual([top, left, 500 - 1 - right], [insetX, insetX, insetX]);
return [top, left, 500 - 1 - right];
}
test('a box inset at the sides, with as much padTop, pads the first row band evenly', () => {
// The launcher's grid (launcher-app.ts) relies on this: its box runs to the
// window's top edge, with the edge's suggested padding as padTop.
const pad = 8;
const image = paint(makeGrid().grid, 1000, { x: pad, y: 0, width: 500 - 2 * pad, height: 190, padTop: pad });
assert.deepEqual(bandBounds(image), [pad, pad, pad]);
// Beside blank space there is no padding: the band spans the box, its own
// 2px inside its row.
assert.deepEqual(bandBounds(paint(makeGrid().grid, 1000, { x: 0, y: 0, width: 500, height: 190 })), [2, 0, 0]);
});
test('padTop scrolls away with the first row, and the scroll still animates to the static paint', async () => {
const box = { x: 0, y: 0, width: 500, height: 180, padTop: 8, padBottom: 8 };
const { grid } = makeGrid({ items: Array.from({ length: 20 }, (_, i) => `item${i}`) });
const iconYs = (image) => [...new Set(image.draws.filter((d) => d.kind === 'image' && !d.presentation).map((d) => d.y))];
// The first row starts below the padding (less the band's own 2px inset).
assert.deepEqual(iconYs(paint(grid, 500, box)), [6 + 4, 6 + 4 + ROW_H]);
await grid.handleInput(ring('scroll-down'), null);
paint(grid, 1000, box);
// Once the band's slide onto the second row is over.
const settledBefore = paint(grid, 2000, box);
await grid.handleInput(ring('scroll-down'), null);
const moving = paint(grid, 3000, box);
assert.ok(animation(moving), 'the scroll is retained as a display list');
assert.deepEqual(shown(moving, 3000), shown(settledBefore, 2000));
const end = 3000 + MENU_HIGHLIGHT_DURATION_MS;
const settled = paint(grid, end, box);
// Scrolled, the rows run from the box's top edge.
assert.deepEqual(iconYs(settled), [4, 4 + ROW_H]);
assert.deepEqual(shown(moving, end), shown(settled, end));
// A touch lands on the row under it either way.
const { grid: touched, log } = makeGrid();
paint(touched, 1000, box);
assert.equal(await touched.hitTest(50, 3, null), false, 'the padding is no row');
assert.equal(await touched.hitTest(50, 6 + ROW_H + 10, null), true);
assert.deepEqual(log.activated.map((a) => a.index), [5]);
});
test('rows keep their content height instead of stretching, and the next row peeks clipped', () => {
@@ -202,8 +238,9 @@ test('item selection slides the highlight between cells without redrawing past t
assert.equal(list.calls[0].clip.width, 495, 'the scrollbar gutter is left to the static paint');
const rect = list.calls[1];
const { evaluate } = build('graphics/draw-expression');
assert.equal(evaluate(rect.x, 0).value, 200 + 6, 'starts on the middle cell');
assert.equal(evaluate(rect.x, MENU_HIGHLIGHT_DURATION_MS).value, 300 + 6, 'rests on the next cell');
// The rows keep clear of the gutter: five 99px columns in the 495 left of it.
assert.equal(evaluate(rect.x, 0).value, 198 + 6, 'starts on the middle cell');
assert.equal(evaluate(rect.x, MENU_HIGHLIGHT_DURATION_MS).value, 297 + 6, 'rests on the next cell');
assert.deepEqual(shown(moving, 2000), shown(before, 1000));
const end = 2000 + MENU_HIGHLIGHT_DURATION_MS;
assert.deepEqual(shown(moving, end), shown(paint(grid, end), end));
+23 -5
View File
@@ -4,6 +4,7 @@ const fs = require('node:fs');
const path = require('node:path');
const vm = require('node:vm');
const ts = require('typescript');
const { bordered, unbordered } = require('./helpers/viewport.cjs');
const read = (file) => fs.readFileSync(path.join(__dirname, '..', file), 'utf8');
const js = (source) => ts.transpileModule(source, {
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 },
@@ -103,8 +104,16 @@ test('Input events captures while visible, pauses for history, and unsubscribes
});
let visible = true, renders = 0, removed = 0, pops = 0;
const layer = new InputEventsLayer(() => renders++, () => visible, () => removed++);
const ctx = { stack: { getBaseSize: () => ({ width: 576, height: 260 }),
let viewport = bordered(576, 260);
const ctx = { stack: { getBaseSize: () => viewport,
pop: () => pops++, popThrough: (page) => { assert.equal(page, layer); page.onRemoved(); } } };
const inBounds = (image) => {
for (const text of image.texts) {
assert.ok(text.x >= 0 && text.x + text.width <= image.width, text.text);
assert.ok(text.y >= 0 && text.y + text.height <= image.height, text.text);
}
};
const at = (image, start) => image.texts.find(t => t.text.startsWith(start));
try {
for (const type of ['ring-press','click','ring-press','ring-press','double-click','long-press','long-press-release','short-then-long-press']) {
const e = { ...event(type, 1000 + renders * 75), ringInput: { tick: 4294967200 + renders, type: 1, aux:0, speed:0 } };
@@ -117,10 +126,19 @@ test('Input events captures while visible, pauses for history, and unsubscribes
assert.ok(image.texts.some(t => t.text.startsWith("*")));
layer.menuItems()[2].onSelect(ctx);
assert.ok(layer.paint(ctx).texts.some(t => t.text.startsWith("Phone receive time")));
for (const text of image.texts) {
assert.ok(text.x >= 0 && text.x + text.width <= image.width, text.text);
assert.ok(text.y >= 0 && text.y + text.height <= image.height, text.text);
}
inBounds(image);
// Framed: the title and the hint line sit at the padding; the log is between them.
assert.deepEqual([at(image, 'Input events').x, at(image, 'Input events').y], [8, 8]);
assert.equal(at(image, 'Unfiltered').y + font.lineHeight, 260 - 8);
assert.ok(Math.max(...image.texts.map(t => t.x + t.width)) <= 576 - 8, 'nothing runs into the right padding');
// Nothing bordering the window: the same lines run to its edges.
viewport = unbordered(576, 260);
const bare = layer.paint(ctx);
inBounds(bare);
assert.deepEqual([at(bare, 'Input events').x, at(bare, 'Input events').y], [0, 0]);
assert.equal(at(bare, 'Unfiltered').y + font.lineHeight, 260);
assert.ok(bare.texts.length >= image.texts.length, 'the log has no less room');
viewport = bordered(576, 260);
if (process.env.INPUT_EVENTS_PREVIEW) fs.writeFileSync(process.env.INPUT_EVENTS_PREVIEW, Buffer.concat([
Buffer.from(`P5\n${image.width} ${image.height}\n255\n`), Buffer.from(image.withDrawsBaked().pixels),
]));
+11 -5
View File
@@ -212,15 +212,12 @@ test('iOS bandwidth footer toggles live, polls only in foreground and resets its
'./phone-gestures': { PhoneGestureRecognizer: class { cancel() {} } },
'./ble-bandwidth-meter': require('../.test-build/app/phone-ui/ble-bandwidth-meter.js'),
'../native/ble-traffic': { sampleBleTraffic: () => totals },
'../ui/dashboard-settings': { brightnessSetting: { get: () => 'auto' }, displayLayoutSetting: { displayValue: () => 'Tall' }, showBleBandwidthSetting: { get: () => enabled },
'../ui/dashboard-settings': { phoneInputStyleSetting: { get: () => 'watch' }, showBleBandwidthSetting: { get: () => enabled },
onAnySettingChanged: fn => { settingListeners.add(fn); return () => settingListeners.delete(fn); } },
'../g2/device-addresses': { loadDeviceAddresses: () => ({}) },
'../g2/ios-peripheral-identity': { deviceAddressError: () => 'no test devices' },
'./onboarding-state': { isPreviewOnlyMode: () => false },
'../g2/reconnect-policy': { isAutoReconnectSuppressed: () => false },
'../g2/glasses-display-state': require('../.test-build/app/g2/glasses-display-state.js'),
'../ui/shell/shell': { shell: { onBatteryLevelsChanged: () => () => {}, getBatteryLevels: () => ({
headset: null, headsetCharging: null, case: null, caseCharging: null, ring: null, ringCharging: null }) } },
};
modules['./keyboard-input-view-model'] = load('app/phone-ui/keyboard-input-view-model.ts', { require: id => modules[id] });
modules['./remote-controls-view-model'] = load('app/phone-ui/remote-controls-view-model.ts', { require: id => modules[id] });
@@ -368,6 +365,7 @@ test('iOS preview voice uses the phone microphone, while connected capture stays
test('iOS continuous capture is shared by windows and restarts after a disconnect, a lock or backgrounding', async () => {
const calls = [], claims = [], providers = [];
let saveRecording = false;
// Models the bridge's continuous state: listening on a source, or draining after a stop.
const bridge = { source: null, draining: false, recording: '', rawSession: undefined,
supportsContinuous: voice => voice.provider !== 'onboard',
@@ -385,7 +383,7 @@ test('iOS continuous capture is shared by windows and restarts after a disconnec
setRawSession: session => { bridge.rawSession = session; } };
const settings = { ...voiceSettings, voiceProviderSetting: { get: () => 'whisper-server' },
whisperServerUrlSetting: { get: () => 'http://whisper.local:8000' }, whisperServerLanguageSetting: { get: () => 'en' },
saveTranscribeAudioSetting: { get: () => true } };
saveRecordingSetting: { get: () => saveRecording } };
const { IosPreviewController } = load('app/g2/ios-preview-controller.ts', {
require: id => id === '../native/ios-voice-input' ? { iosVoiceInput: bridge } : id === '../ui/dashboard-settings' ? settings
: id === '../ui/shell/voice-activity' ? { voiceActivity: { setActive: (active, holder) => claims.push([active, holder]) } }
@@ -404,7 +402,15 @@ test('iOS continuous capture is shared by windows and restarts after a disconnec
host.startContinuousVoiceCapture('transcribe', { recordAs: 'transcribe-1' }); await settle();
host.startContinuousVoiceCapture('calculator'); await settle();
assert.deepEqual(calls, [['glasses', true]], 'the second window shares the capture');
// Save Recording (in the window's menu) starts and stops the recording while it is open.
assert.equal(bridge.recording, '');
saveRecording = true; host.syncContinuousRecording();
assert.equal(bridge.recording, 'transcribe-1');
saveRecording = false; host.syncContinuousRecording();
assert.equal(bridge.recording, '');
saveRecording = true; host.syncContinuousRecording();
assert.equal(bridge.recording, 'transcribe-1');
assert.deepEqual(calls, [['glasses', true]], 'without touching the capture');
assert.deepEqual(claims.at(-1), [true, 'continuous-capture']);
host.stopContinuousVoiceCapture('calculator');
assert.deepEqual(calls, [['glasses', true]], 'closing one window keeps the other listening');
+16 -4
View File
@@ -4,6 +4,7 @@ const fs = require('node:fs');
const path = require('node:path');
const vm = require('node:vm');
const ts = require('typescript');
const { bordered, unbordered } = require('./helpers/viewport.cjs');
function load(file, modules = {}, globals = {}) {
const sandbox = { exports: {}, require: id => {
@@ -150,9 +151,9 @@ function screenFixture() {
const f = provider(), timers = new Map(); let nextTimer = 0;
const font = { lineHeight: 12, measureText: text => text.length * 6 };
class Image {
constructor(width = 300, height = 288) { this.width = width; this.height = height; this.text = []; }
drawText(_font, _x, _y, text) { this.text.push(text); }
fillRoundedRect() {} drawRoundedRect() {}
constructor(width = 300, height = 288) { this.width = width; this.height = height; this.text = []; this.at = {}; this.boxes = []; }
drawText(_font, x, y, text) { this.text.push(text); this.at[text] = [x, y]; }
fillRoundedRect(x, y, width, height) { this.boxes.push([x, y, width, height]); } drawRoundedRect() {}
}
const common = {
'graphics/image': { GrayImage: Image }, 'graphics/ui-fonts': { getDefaultSmallFont: () => font, getDefaultMediumFont: () => font },
@@ -173,7 +174,8 @@ function screenFixture() {
test('Calendar and Glanceboard show permission, loading, error and event states with widget cleanup', () => {
const f = screenFixture(); let renders = 0, requests = 0;
const widget = new f.CalendarWidget(), layer = new f.CalendarLayer(() => requests++);
const ctx = { stack: { getBaseSize: () => ({ width: 300, height: 288 }) } };
let viewport = bordered(300, 288);
const ctx = { stack: { getBaseSize: () => viewport } };
const paintWidget = () => { const image = new f.Image(); widget.paint(image); return image.text.join(' '); };
widget.start(() => renders++); widget.start(() => renders++); assert.equal(f.timers.size, 1);
f.permission(false); assert.match(layer.paint(ctx).text.join(' '), /Grant calendar permission/);
@@ -187,6 +189,16 @@ test('Calendar and Glanceboard show permission, loading, error and event states
f.reads[1].done(JSON.stringify([f.event('event')]), null);
assert.match(layer.paint(ctx).text.join(' '), /Meeting/);
assert.match(paintWidget(), /Meeting/);
// Beside border lines the selection box keeps the suggested padding and the
// title lines up with row text; with nothing past the edges they run to them.
const framed = layer.paint(ctx);
assert.deepEqual(framed.at.Calendar, [18, 10]);
assert.deepEqual(framed.boxes[0], [8, 54, 284, 32]);
assert.equal(framed.at.Office[0], 18);
viewport = unbordered(300, 288);
const bare = layer.paint(ctx);
assert.deepEqual(bare.at.Calendar, [10, 2]);
assert.deepEqual(bare.boxes[0], [0, 46, 300, 32]);
widget.stop(); const before = renders; f.bridge.changeHandler();
assert.equal(renders, before); assert.equal(f.timers.size, 0);
});
+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 },
+7 -4
View File
@@ -87,7 +87,8 @@ function sessionHarness(overrides = {}, playBuzzer = () => {}, { packageId = 'te
'../../ui/dashboard-settings': {}, '../../ui/sound-effects': {}, './api-key-dialog': {},
'./mic-router': { evenHubMicRouter: {} }, './imu-router': { evenHubImuRouter: {} },
'./compass-router': { evenHubCompassRouter: {} }, '../../assistant/tool-registry': {},
'../../native/location': {}, '../../native/location-tracker': {}, '../../native/location-permissions': {}, ...overrides,
'../../native/location': {}, '../../native/location-tracker': {}, '../../native/location-permissions': {},
'../../native/image-rgba': { decodeImageRgba: () => null }, ...overrides,
});
const api = load('app/apps/evenhub/session.ts');
const session = new api.EvenHubSession({ name: 'Probe', packageId, permissions: [] }, '/test/dist', line => logs.push(line), '', playBuzzer);
@@ -105,10 +106,12 @@ function sessionHarness(overrides = {}, playBuzzer = () => {}, { packageId = 'te
test('injected EvenHub RPC creates, updates and composites an image page with no Android globals', async () => {
const h = sessionHarness();
assert.equal(await h.call('createStartUpPageContainer', { containerTotalNum: 1,
imageObject: [{ containerID: 1, containerName: 'board', xPosition: 0, yPosition: 0, width: 2, height: 2 }] }), 0);
assert.equal(await h.call('updateImageRawData', { containerID: 1, containerName: 'board', imageData: [0, 80, 160, 255] }), 0);
imageObject: [{ containerID: 1, containerName: 'board', xPosition: 0, yPosition: 0, width: 20, height: 20 }] }), 0);
// Raw 8-bit grey as the SDK sends it (compressMode 2), quantized to 4-bit levels as the Even app does.
const raw = new Array(400).fill(0); raw.splice(0, 4, 0, 80, 160, 255);
assert.equal(await h.call('updateImageRawData', { containerID: 1, containerName: 'board', imageData: raw, compressMode: 2 }), 0);
const painted = h.session.paint({ width: 576, height: 288 }, true);
assert.deepEqual([painted.pixels[0], painted.pixels[1], painted.pixels[576], painted.pixels[577]], [0, 80, 160, 255]);
assert.deepEqual(Array.from(painted.pixels.slice(0, 4)), [0, 5 * 17, 9 * 17, 255]);
assert.equal(await h.call('setLocalStorage', { key: 'score', value: '42' }), true);
assert.equal(await h.call('getLocalStorage', { key: 'score' }), '42');
const events = [];
+102 -7
View File
@@ -21,12 +21,18 @@ const planes = load('app/graphics/plane.ts', { './image': images });
const timings = load('app/native/frame-timings.ts', {});
const LAUNCHER_GRAY = 75;
let glassesType = 'g2';
function fixture() {
const relay = { available: true, starts: 0, client: null, message: undefined };
const observers = new Map(), phoneState = { protectedDataAvailable: true };
let settingsChanged, remoteHost, controller, bridge = null;
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 };
@@ -104,12 +110,12 @@ function fixture() {
const noop = () => () => {};
class FaceclawCommunicatorBridge extends Display {
phase = 'disconnected'; listeners = {};
constructor() { super(); bridge = this; }
constructor(addresses) { super(); bridge = this; this.addresses = addresses; }
on(kind, fn) { this.listeners[kind] = fn; return () => { if (this.listeners[kind] === fn) delete this.listeners[kind]; }; }
onStateChange(fn) { return this.on('state', fn); } onRingEvent(fn) { return this.on('ring', fn); }
onWearState(fn) { return this.on('wear', fn); } onBatteryState(fn) { return this.on('battery', fn); } onCaseState(fn) { return this.on('case', fn); } onFirmwareInfo() { return noop(); }
onWearState(fn) { return this.on('wear', fn); } onBatteryState(fn) { return this.on('battery', fn); } onCaseState(fn) { return this.on('case', fn); } onFirmwareInfo(fn) { return this.on('firmware', fn); }
onFrameMetrics() { return noop(); } onPreviewAnimationFrame() { return noop(); } addCompassListener() { return noop(); } onAncsRelayFrame() { return noop(); }
onAncsAuthorization() { return noop(); } setRequiresAncs() {} rightWriteLimit() { return 20; } onPhoneLockSignal() {}
onAncsAuthorization() { return noop(); } setRequiresAncs() {} ancsRelayAvailable() { return relay.available; } rightWriteLimit() { return 20; } onPhoneLockSignal() {}
async writeRawToRight() {} async configureBrightness() {} async setBrightness() {} async enableWearDetectionAndRequestState() {} async playBuzzerSequence() {} async close() {}
emitState(phase, status = phase) { if (phase === 'connected' || phase === 'disconnected') this.phase = phase; this.listeners.state?.({ phase, status }); }
emitRing(input) { this.listeners.ring?.(input); }
@@ -148,7 +154,10 @@ function fixture() {
'../ui/shell/voice-activity': { voiceActivity: { setActive() {}, isActive: () => false, subscribe: () => () => {} } },
'../native/faceclaw-communicator.ios': { FaceclawCommunicatorBridge, resolveIosPeripherals: async addresses => addresses },
'../native/preview-display.ios': { PreviewDisplayTarget },
'./ancs-client': { ANCS_FIRMWARE_VERSION: 16, AncsClient: class { state = 'disconnected'; start() {} stop() {} stopCommand() { return new Uint8Array(); } receive() { return false; } } },
'../g1/firmware/g1v0-image': { G1V0_IMAGE: { version: '0.5.65' } },
'./ancs-client': { ANCS_FIRMWARE_VERSION: 16, AncsClient: class { state = 'disconnected'; constructor() { relay.client = this; }
start() { this.state = 'starting'; relay.starts++; } stop(message) { this.state = 'disconnected'; relay.message = message; }
stopCommand() { return new Uint8Array(); } receive() { return false; } } },
'../native/nightscout-bridge': { nightscoutBridge: { async start() {}, async stop() {} } },
'../native/weather': { weatherBridge: { setSessionActive() {} } },
'./glance-host': { GlanceHost }, './events': events,
@@ -157,7 +166,9 @@ function fixture() {
'./lock-screen': { LOCK_SCREEN_SURFACE_ID: 'lock-screen', createLockScreenImage: () => new images.GrayImage(640, 480, 123) },
'./glasses-presence': load('app/g2/glasses-presence.ts', {}),
'./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': {},
@@ -171,7 +182,7 @@ function fixture() {
'../apps/all-apps': { ALL_APPS: [{ appId: 'glanceboard', glanceboard: provider }] },
'../apps/onboarding/onboarding-progress': { claimGlassesOnboardingAutoLaunch: () => false },
'../ui/shell/worker-window': {}, '../ui/shell/in-process-window': {},
'../ui/dashboard-settings': { brightnessSetting: { get: () => 'auto' }, brightnessSettingToLevel: () => null, getBrightnessPreferences: () => ({ auto: true, level: 50, minimum: 2, maximum: 100, curve: '0:0,1000:100', fadeMs: 280 }), lockScreenEnabledSetting: { get: () => settings.lock }, ringConnectionModeSetting: { get: () => 'glasses' }, onAnySettingChanged: fn => { settingsChanged = fn; return () => {}; }, previewColorSetting: { get: () => 'white' } },
'../ui/dashboard-settings': { glassesTypeSetting: { get: () => glassesType }, brightnessSetting: { get: () => 'auto' }, brightnessSettingToLevel: () => null, getBrightnessPreferences: () => ({ auto: true, level: 50, minimum: 2, maximum: 100, curve: '0:0,1000:100', fadeMs: 280 }), lockScreenEnabledSetting: { get: () => settings.lock }, ringConnectionModeSetting: { get: () => 'glasses' }, onAnySettingChanged: fn => { settingsChanged = fn; return () => {}; }, previewColorSetting: { get: () => 'white' } },
'../native/phone-battery': { readPhoneBatteryState: () => ({ battery: 80, charging: false }) },
'../apps/ios-availability': { iosAppUnavailableReason: () => null },
'../graphics/glyph-wire': { prepareFrameDraws: () => null },
@@ -184,7 +195,10 @@ function fixture() {
: ({ 0: 'click', 1: 'scroll-up', 2: 'scroll-down', 3: 'double-click', 9: 'long-press',
10: 'long-press-release', 11: 'short-then-long-press' })[event.eventType] ?? 'unknown', source: 'ring',
}) },
'../ui/shell/geometry': { appViewportRect: () => ({ x: 0, y: 0, width: 640, height: 480 }) },
'../ui/shell/geometry': {
appViewportRect: () => ({ x: 0, y: 0, width: 640, height: 480 }),
windowEdges: () => ({ top: 'offscreen', right: 'offscreen', bottom: 'offscreen', left: 'offscreen' }),
},
};
const { IosPreviewController } = load('app/g2/ios-preview-controller.ts', modules, {
...clock, console: { log() {}, warn() {}, error: text => errors.push(text) },
@@ -213,10 +227,41 @@ function fixture() {
}
async function phone(type, origin = 'ring') { controller.gesture(type, origin); await render(); }
async function connect() { await controller.connect(); await render(); }
return { 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 };
}
test('the session is told which glasses it drives, as a G2 session gets nowhere on a G1', async () => {
for (const type of ['g1', 'g2']) {
glassesType = type;
const f = fixture();
await f.connect();
assert.equal(f.bridge.addresses.glassesType, type);
assert.ok('right' in f.bridge.addresses && 'left' in f.bridge.addresses, 'the arms still come with it');
}
glassesType = 'g2';
});
test('a link that comes back asks for the notification relay again; a G1 without one is told to update', async () => {
const f = fixture();
await f.connect();
const firmware = { leftVersion: 'g1v0 0.5.65', rightVersion: 'g1v0 0.5.65', extension: 'Faceclaw/38' };
f.bridge.listeners.firmware(firmware);
assert.equal(f.relay.starts, 1);
// Connected and running: nothing to do.
f.bridge.emitState('connected');
assert.equal(f.relay.starts, 1);
// The firmware's relay ended with the link, so the client must not go on thinking it's ready.
f.bridge.emitState('retrying', 'Reconnecting');
assert.equal(f.relay.client.state, 'disconnected');
f.bridge.emitState('connected');
assert.equal(f.relay.starts, 2);
f.bridge.emitState('retrying', 'Reconnecting');
f.relay.available = false;
f.bridge.emitState('connected');
assert.equal(f.relay.starts, 2);
assert.match(f.relay.message, /firmware 0\.5\.65/);
});
const assertBlank = pixels => assert.ok(pixels.every(p => p === 0));
const assertBoard = pixels => {
assert.equal(pixels[48 * 640 + 270], 200);
@@ -307,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/38' });
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.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/39' });
assert.ok(!f.keyboard.calls.includes('ready'), f.keyboard.calls.join());
} finally { glassesType = 'g2'; }
});
+131
View File
@@ -0,0 +1,131 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { loader } = require('./helpers/load-typescript.cjs');
const NOT_DETERMINED = 0, DENIED = 2, ALLOWED = 3;
const STATE_UNAUTHORIZED = 3, STATE_POWERED_ON = 5;
/**
* The Permissions page and the real scan layer over a fake Kotlin central. As in
* IosBleCentral, registering the scan listener is what creates the CBCentralManager, so
* `registrations` counting up from 0 is the moment iOS would show its permission prompt.
*/
function harness({ authorization = NOT_DETERMINED, onboarding = true } = {}) {
const central = {
authorizationValue: authorization,
listener: null,
registrations: 0,
authorization() { return this.authorizationValue; },
setScanListenerListener(listener) { this.registrations++; this.listener = listener; },
// CoreBluetooth answering: the state arrives some time after the central is created.
report(state, value) {
this.authorizationValue = value;
this.listener.onBluetoothStateStateAuthorization(state, value);
},
};
const alerts = [];
const timers = [];
const appListeners = new Set();
const load = loader({
global: { isIOS: true },
setTimeout: (fn, delay) => { timers.push({ fn, delay }); return timers.length; },
clearTimeout: id => { timers[id - 1].cleared = true; },
FaceclawKitIosBleCentral: { alloc: () => ({ initWithPlatform: () => central }) },
FaceclawKitIosProtocolPlatform: { shared: {} },
FaceclawKitIosBleScanListener: {},
}, {
'@nativescript/core': {
Application: { resumeEvent: 'resume', on: (_name, fn) => appListeners.add(fn), off: (_name, fn) => appListeners.delete(fn) },
ApplicationSettings: { getString: (_key, fallback) => fallback, setString() {}, remove() {} },
Dialogs: { alert: async options => { alerts.push(options.message); }, confirm: async () => true },
Frame: { topmost: () => null },
Observable: class { notifyPropertyChange() {} },
},
'../g2/ios-peripheral-identity': {},
'../util/hex-util': { hexToBytes() {} },
'./kotlin-listener.ios': { kotlinListener: (_protocol, methods) => methods },
'../g2/android-permissions': {},
'../native/calendar-permissions': { hasCalendarPermission: () => false, ensureCalendarPermission: async () => false },
'../native/battery-optimization': {},
'../native/file-access': {},
'../native/notification-access': {},
'../native/media-controller': { mediaControllerBridge: { hasAccess: () => false, requestAccess: async () => false } },
});
const { PermissionsViewModel } = load('app/phone-ui/permissions-view-model.ts');
const model = new PermissionsViewModel({ onboarding });
const card = () => model.cards.find(item => item.id === 'nearby-devices');
return {
central, alerts, timers, appListeners, model, card,
checked: () => card().checkVisibility === 'visible',
tap: () => card().onCardTap({ object: { bindingContext: card() } }),
flush: () => new Promise(resolve => setImmediate(resolve)),
};
}
test('opening the page does not prompt; tapping the card does, and allowing checks it', async () => {
const page = harness();
page.model.onPageLoaded();
assert.equal(page.central.registrations, 0, 'rendering the card must not create the central');
assert.equal(page.checked(), false);
assert.match(page.model.primaryClass, /dimmed/);
page.tap();
assert.equal(page.central.registrations, 1, 'the tap creates the central, which shows the prompt');
page.central.report(STATE_POWERED_ON, ALLOWED);
await page.flush();
assert.equal(page.checked(), true);
assert.equal(page.model.primaryClass, '-primary');
assert.equal(page.alerts.length, 0);
});
test('Bluetooth already allowed is checked on the first render, with no central and no prompt', () => {
// A central's state arrives asynchronously, which is why a card reading it rendered unchecked
// and then had its tap ignored once the state said granted.
const page = harness({ authorization: ALLOWED });
assert.equal(page.checked(), true);
assert.equal(page.model.primaryClass, '-primary');
page.tap();
assert.equal(page.central.registrations, 0);
});
test('an answer arriving after the wait timed out still updates the page', async () => {
const page = harness();
page.model.onPageLoaded();
page.tap();
page.timers.find(timer => timer.delay === 30_000).fn();
await page.flush();
assert.match(page.alerts[0], /timed out/);
assert.equal(page.checked(), false);
page.central.report(STATE_POWERED_ON, ALLOWED);
assert.equal(page.checked(), true);
assert.equal(page.model.primaryClass, '-primary');
});
test('denied stays unchecked, and tapping again re-reads the state and explains', async () => {
const page = harness();
page.model.onPageLoaded();
page.tap();
page.central.report(STATE_UNAUTHORIZED, DENIED);
await page.flush();
assert.equal(page.checked(), false);
assert.match(page.alerts[0], /iPhone Settings/);
page.tap();
assert.equal(page.central.registrations, 2, 'an existing central re-reports its state when registered again');
page.central.report(STATE_UNAUTHORIZED, DENIED);
await page.flush();
assert.equal(page.alerts.length, 2);
});
test('leaving the page stops watching Bluetooth', () => {
const page = harness();
page.model.onPageLoaded();
page.tap();
page.model.onPageUnloaded();
assert.equal(page.appListeners.size, 0);
const before = page.model.cards;
page.central.report(STATE_POWERED_ON, ALLOWED);
assert.equal(page.model.cards, before, 'no refresh once unloaded');
});
-57
View File
@@ -1,57 +0,0 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { loader } = require('./helpers/load-typescript.cjs');
function harness(width, height) {
const font = { lineHeight: 20, measureText: text => text.length * 10, getGlyph: () => ({ dwidthX: 10 }) };
const load = loader({}, { './onboarding-fonts': { onboardingFont: () => font } });
const { IosPrivacyPage } = load('app/apps/onboarding/ios-privacy-page.ts');
const { IOS_PRIVACY_NOTICE } = load('app/analytics/privacy-disclosure.ts');
const page = new IosPrivacyPage();
let next = 0;
return {
page, notice: IOS_PRIVACY_NOTICE, get next() { return next; },
input(type) { page.handleInput({ type }, { next: () => next++ }); },
paint() {
const rows = [];
page.paint({ width, height, drawText(font, x, y, text) {
assert.ok(x + font.measureText(text) <= width, `horizontal overflow: ${text}`);
assert.ok(y >= 0 && y + font.lineHeight <= height, `vertical overflow: ${text}`);
rows.push({ y, text });
} });
return rows;
},
};
}
for (const [width, height] of [[576, 188], [576, 260], [640, 432]]) {
test(`entire iOS notice is readable before continuing at ${width}x${height}`, () => {
const h = harness(width, height);
const shown = [];
for (let i = 0; !h.next && i < 30; i++) {
const rows = h.paint();
const footer = rows.at(-1);
for (const row of rows.slice(1, -1)) {
assert.ok(row.y + 20 <= footer.y, 'body must not overlap navigation');
shown.push(row.text);
}
h.input('click');
}
assert.equal(h.next, 1);
// Line breaking may also split after punctuation (e.g. device/app).
assert.equal(shown.join('').replace(/\s+/g, ''), h.notice.replace(/\s+/g, ''));
});
}
test('scrolling stays inside the notice; revisiting starts at the beginning', () => {
const h = harness(576, 188);
const first = h.paint().map(row => row.text);
h.input('scroll-up');
assert.deepEqual(h.paint().map(row => row.text), first);
h.input('scroll-down');
assert.notDeepEqual(h.paint().map(row => row.text), first);
for (let i = 0; i < 30; i++) h.input('scroll-down');
assert.equal(h.next, 0, 'scrolling must not accept the notice');
h.page.enter();
assert.deepEqual(h.paint().map(row => row.text), first);
});
+11 -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 },
@@ -57,6 +57,16 @@ function alsRecord(alsValue) {
return body;
}
test('only a G1 session runs the G1 connect path', () => {
for (const [glassesType, expected] of [['g1', 1], ['g2', 0], [undefined, 0]]) {
const { session, FaceclawCommunicatorBridge } = harness();
let used = 0;
session.useG1 = () => { used++; };
new FaceclawCommunicatorBridge({ right: 'r', left: 'l', glassesType });
assert.equal(used, expected, String(glassesType));
}
});
test('the bridge is the active session from start() until close()', async () => {
const h = harness();
const bridge = new h.FaceclawCommunicatorBridge({ right: 'r', left: 'l' });
+35 -2
View File
@@ -31,8 +31,13 @@ function harness() {
},
};
const recordings = [];
let failRecordings = false, recordingAttempts = 0;
class FakeRecorder {
static open(name) { const r = new FakeRecorder(); r.name = name; r.path = `/Downloads/${name}.wav`; r.pcm = []; r.closed = false; recordings.push(r); return r; }
static open(name) {
recordingAttempts++;
if (failRecordings) return null;
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; }
}
@@ -44,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();
@@ -56,6 +61,7 @@ function harness() {
} };
const onboard = { provider: 'onboard', elevenLabsApiKey: '', openAiApiKey: '', sonioxApiKey: '' };
return { bridge, native, session, audio, commands, text, timers, clouds, analyzers, recordings, release: () => release(),
failRecordings: value => { failRecordings = value; }, recordingAttempts: () => recordingAttempts,
analyzerSupported: value => { analyzerSupported = value; },
onboard, cloud: { ...onboard, provider: 'soniox', sonioxApiKey: 'key' },
microphoneAuthorization: value => { microphoneAuthorization = value; }, microphoneRequests: () => microphoneRequests,
@@ -307,7 +313,10 @@ test('push-to-talk after Transcribe closed starts its own capture instead of joi
test('continuous captures record to the requested WAV, created by their first audio and kept across restarts', async () => {
const h = harness();
let changes = 0; h.bridge.onRecordingChange(() => changes++);
assert.equal(h.bridge.recording, false);
h.bridge.recordTo('transcribe-2026-10-17-120000');
assert.deepEqual([h.bridge.recording, h.bridge.recordingFailed, changes], [true, false, 1]);
// A start that fails before any audio leaves no file behind.
h.native.error = 'No phone microphone is available.';
await h.bridge.startPhoneCapture(h.cloud, () => {}, false, true);
@@ -334,6 +343,30 @@ test('continuous captures record to the requested WAV, created by their first au
assert.deepEqual(h.recordings[0].pcm, [1, 2, 3, 4, 5, 6, 7]);
h.bridge.recordTo('');
assert.equal(h.recordings[0].closed, true);
assert.deepEqual([h.bridge.recording, changes], [false, 2]);
});
test('a recording whose file cannot be created is reported, and not retried per packet', async () => {
const h = harness();
let changes = 0; h.bridge.onRecordingChange(() => changes++);
h.failRecordings(true);
h.bridge.recordTo('transcribe-1');
const start = h.bridge.startGlassesCapture(h.session, h.cloud, () => {}, false, true); h.release(); await start;
h.native.pcmHandler(new Uint8Array([1, 2]));
h.native.pcmHandler(new Uint8Array([3, 4]));
assert.deepEqual([h.bridge.recording, h.bridge.recordingFailed, changes, h.recordingAttempts()], [false, true, 2, 1]);
// Asking for the same recording again changes nothing; another one (Save Recording
// switched off and on) is a fresh attempt.
h.bridge.recordTo('transcribe-1');
h.native.pcmHandler(new Uint8Array([5]));
assert.equal(h.recordingAttempts(), 1);
h.failRecordings(false);
h.bridge.recordTo('');
assert.deepEqual([h.bridge.recording, h.bridge.recordingFailed, changes], [false, false, 3]);
h.bridge.recordTo('transcribe-1');
h.native.pcmHandler(new Uint8Array([6]));
assert.deepEqual([h.bridge.recording, h.bridge.recordingFailed, changes], [true, false, 4]);
assert.deepEqual(h.recordings[0].pcm, [6]);
});
test('a glasses session ending drains continuous capture instead of discarding what it heard', async () => {
+227
View File
@@ -0,0 +1,227 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const vm = require('node:vm');
const ts = require('typescript');
const path = require('node:path');
function load(file, context = {}) {
const sandbox = { exports: {}, console, ...context };
sandbox.global = sandbox;
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 types = () => load('app/native/wear-bridge-types.ts');
/** What FaceclawWatchBridge.m does, as far as JS can tell. */
function fakeNative({ available = true, watchApp = true } = {}) {
return {
calls: [], message: null, connection: null, watchApp,
isAvailable() { return available; },
isWatchAppAvailable() { return this.watchApp; },
setMessageHandlerConnectionHandler(message, connection) { this.message = message; this.connection = connection; },
publishStateForce(json, force) { this.calls.push(['state', JSON.parse(json), force]); },
sendEventSticky(json, sticky) { this.calls.push(['event', JSON.parse(json), sticky]); },
sendToWatchJson(pathName, json) { this.calls.push(['send', pathName, JSON.parse(json)]); },
sendAckOkJsReadyMessage(seq, ok, jsReady, message) { this.calls.push(['ack', seq, ok, jsReady, message]); },
};
}
function loadBridge(native) {
return load('app/native/wear-bridge.ios.ts', {
FaceclawWatchBridge: { shared: () => native },
require: () => types(),
}).wearBridge;
}
test('iOS bridge carries the same messages as the Android one', () => {
const native = fakeNative();
const bridge = loadBridge(native);
assert.equal(bridge.isAvailable(), true);
const received = [];
bridge.onMessage(message => received.push(message));
native.message('/faceclaw/input', '{"gesture":"click","seq":3}');
native.message('/faceclaw/press', 'not json');
assert.equal(received[0].path, '/faceclaw/input');
assert.equal(received[0].payload.gesture, 'click');
assert.equal(Object.keys(received[1].payload).length, 0, 'a malformed payload is empty, not a crash');
bridge.publishState({ phase: 'connected' }, true);
bridge.sendToWatch('/faceclaw/battery/request', {});
bridge.sendAck(received[0].nodeId, 3, false, 'refused');
assert.deepEqual(JSON.parse(JSON.stringify(native.calls)), [
['state', { phase: 'connected' }, true],
['send', '/faceclaw/battery/request', {}],
['ack', 3, false, true, 'refused'],
]);
});
test('only streaming assistant snapshots skip the sticky application context', () => {
const native = fakeNative();
const bridge = loadBridge(native);
bridge.sendEvent({ type: 'assistant', phase: 'streaming', text: 'par' });
bridge.sendEvent({ type: 'assistant', phase: 'done', text: 'partial' });
bridge.sendEvent({ type: 'alert', text: 'hi' });
assert.deepEqual(native.calls.map(call => call[2]), [false, true, true]);
});
test('availability follows the watch app, and a change reaches listeners', () => {
const native = fakeNative({ watchApp: false });
const bridge = loadBridge(native);
const seen = [];
bridge.onWatchConnection(connection => seen.push(connection.reachable));
assert.deepEqual(seen, [false], 'the current value is delivered immediately');
native.watchApp = true;
native.connection();
assert.deepEqual(seen, [false, true]);
assert.equal(bridge.getWatchConnection().watchName, 'Apple Watch');
});
test('a phone that cannot use WatchConnectivity turns the bridge into a no-op', () => {
const native = fakeNative({ available: false });
const bridge = loadBridge(native);
assert.equal(bridge.isAvailable(), false);
bridge.publishState({});
bridge.sendEvent({ type: 'alert', text: 'x' });
assert.equal(bridge.getWatchConnection().reachable, false);
assert.equal(native.calls.length, 0);
});
/** A WearRemote over a fake bridge and shell. */
function loadRemote({ phase = 'disconnected', preview = false, now = 1_800_000_000_000 } = {}) {
const bridge = {
messageListener: null, connectionListener: null, sent: [],
isAvailable: () => true,
onMessage(listener) { this.messageListener = listener; return () => {}; },
onWatchConnection(listener) { this.connectionListener = listener; listener({ reachable: true, watchName: 'Apple Watch' }); return () => {}; },
publishState() {},
sendEvent() {},
sendToWatch() {},
sendAck(_node, seq, ok, message) { this.sent.push({ seq, ok, message }); },
};
const levels = { watch: null, watchCharging: null };
const shell = {
onAssistantActivity() {}, onAlertShown() {},
getBatteryLevels: () => levels,
setBatteryLevels(update) { Object.assign(levels, update); },
foregroundWindow: () => null, getForegroundApp: () => null, getWindows: () => [],
isScreenOn: () => true, wake: () => true, isAssistantAvailable: () => false,
};
const setting = value => ({ get: () => value });
const intervals = [];
const realDate = Date;
const clock = { now };
class FakeDate extends realDate { static now() { return clock.now; } }
const inputs = [];
const state = { phase, status: '', glassesLocked: false, glassesWorn: null, silentMode: false, lastHeadsetBattery: null };
const { WearRemote } = load('app/g2/wear-remote.ts', {
Date: FakeDate,
setTimeout: () => 0, clearTimeout() {},
setInterval: fn => { intervals.push(fn); return intervals.length; },
Number, Math, JSON,
require: id => ({
'../analytics/analytics': { flagAnalyticsEvent() {} },
'../apps/evenhub/installed-apps': { getInstalledEvenHubApps: () => [], installedEvenHubAppId: id => id },
'../native/wear-bridge': { wearBridge: bridge, WEAR_PATHS: types().WEAR_PATHS },
'../ui/dashboard-settings': {
glassesModelName: () => 'G1',
onAnySettingChanged() {}, watchCanUnlockSetting: setting(true), watchCrownClockwiseNextSetting: setting(false),
watchMirrorAssistantSetting: setting(true), watchRemoteEnabledSetting: setting(true),
watchVibrateOnInputSetting: setting(true), watchSoundEffectsSetting: setting(false),
},
'../ui/shell/shell': { shell },
'../ui/shell/voice-activity': { voiceActivity: { subscribe() {}, isActive: () => false } },
'../version': { FACECLAW_VERSION: 'test' },
})[id] ?? {},
});
const remote = new WearRemote({
apps: () => [{ appId: 'timer', title: 'Timer' }],
injectInput: async kind => { inputs.push(kind); },
launchApp: async () => {}, connect: async () => {}, disconnect: async () => {},
setGlassesLocked() {}, requestShellRender() {},
previewInputActive: () => preview,
getState: () => state,
appendLog() {},
});
const deliver = (pathName, payload) => bridge.messageListener({ path: pathName, payload, nodeId: 'apple-watch' });
return { remote, bridge, shell, levels, inputs, intervals, clock, deliver };
}
const settle = () => new Promise(resolve => setImmediate(resolve));
test('gestures are refused with no display, and land on the preview without glasses', async () => {
const none = loadRemote();
none.deliver('/faceclaw/input', { gesture: 'click', seq: 1 });
none.deliver('/faceclaw/press', {});
await settle();
assert.deepEqual(none.inputs, []);
assert.equal(none.bridge.sent[0].ok, false);
assert.match(none.bridge.sent[0].message, /not connected/);
const preview = loadRemote({ preview: true });
preview.deliver('/faceclaw/press', {});
preview.deliver('/faceclaw/input', { gesture: 'click', seq: 2 });
await settle();
assert.deepEqual(preview.inputs, ['ring-press', 'click']);
assert.equal(preview.bridge.sent[0].ok, true);
const glasses = loadRemote({ phase: 'connected' });
glasses.deliver('/faceclaw/input', { gesture: 'swipe-left', seq: 3 });
await settle();
assert.deepEqual(glasses.inputs, ['swipe-left']);
});
test('a watch battery report is shown, kept while fresh, and dropped once stale', async () => {
const { levels, intervals, clock, deliver, bridge } = loadRemote({ phase: 'connected' });
deliver('/faceclaw/battery', { battery: 64, charging: true, at: clock.now });
await settle();
assert.equal(levels.watch, 64);
assert.equal(levels.watchCharging, true);
assert.equal(bridge.sent.length, 0, 'battery reports are never acked');
const tick = () => intervals.forEach(fn => fn());
clock.now += 3 * 60 * 60_000;
tick();
assert.equal(levels.watch, 64, 'a closed watch app reports rarely; three hours is still shown');
clock.now += 2 * 60 * 60_000;
tick();
assert.equal(levels.watch, null, 'five hours says the watch is gone');
});
test('a battery report found long after it was left is not shown as current', async () => {
const { levels, clock, deliver } = loadRemote({ phase: 'connected' });
deliver('/faceclaw/battery', { battery: 90, charging: false, at: clock.now - 5 * 60 * 60_000 });
await settle();
assert.equal(levels.watch, null);
// A live report without a timestamp is current by definition, as Wear OS's always are.
deliver('/faceclaw/battery', { battery: 80, charging: false });
await settle();
assert.equal(levels.watch, 80);
});
test('a watch that does not know its battery shows no level, not an empty one', async () => {
const { levels, deliver } = loadRemote({ phase: 'connected' });
deliver('/faceclaw/battery', { battery: 50, charging: false });
await settle();
assert.equal(levels.watch, 50);
deliver('/faceclaw/battery', { battery: null, charging: false });
await settle();
assert.equal(levels.watch, null);
});
test('the mirrored state carries the Apple Watch feedback settings', () => {
const state = loadRemote({ preview: true }).remote.buildState();
assert.equal(state.vibrateOnInput, true);
assert.equal(state.soundEffects, false);
});
test('the mirrored state names the glasses for the charging line', () => {
assert.equal(loadRemote({ phase: 'charging' }).remote.buildState().glassesModel, 'G1');
});
test('the mirrored state says when gestures are landing on the preview', () => {
const state = loadRemote({ preview: true }).remote.buildState();
assert.equal(state.preview, true);
assert.equal(state.connected, false);
assert.equal(loadRemote({ phase: 'connected' }).remote.buildState().connected, true);
});
+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, []);
});
+245
View File
@@ -0,0 +1,245 @@
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'))
})
+509
View File
@@ -0,0 +1,509 @@
const test = require('node:test')
const assert = require('node:assert/strict')
const { HidKeys, parseReportMap, reportKeys } = 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) }
}
})
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} still types`, async () => {
const events = [], 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), log: line => logs.push(line) })
await client.enable(); await client.connect(1, new Uint8Array(6)); const config = await client.configure()
// Keys and media keys are subscribed; the pointer and the battery are not.
assert.deepEqual(config.watched, [27,31]); assert.deepEqual(g.watches, [27,31,3]); assert.deepEqual(g.writes, [25,4,28,32])
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]])
// 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\) carries no keys/.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: [], 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),
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: 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()
})
+144
View File
@@ -0,0 +1,144 @@
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)
})
@@ -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()
}
}
}
@@ -21,4 +21,31 @@ class WavRecorderRepairTest {
file.delete()
}
}
@Test
fun keepsTheHeaderCurrentWhileRecording() {
val file = File.createTempFile("recording", ".wav")
try {
val recorder = FaceclawWavRecorder(file.path, 16000, 1)
// Under a second of audio: still the empty header from the start.
recorder.append(ByteArray(16000))
assertEquals(BinaryEncoding.wavHeader(0, 16000, 1).toList(), file.readBytes().copyOf(44).toList())
// Past a second the header covers what is on disk, without waiting for finish(), and
// later audio still lands at the end.
recorder.append(ByteArray(16000) { 1 })
recorder.append(ByteArray(100) { 2 })
var bytes = file.readBytes()
assertEquals(44 + 32100, bytes.size)
assertEquals(BinaryEncoding.wavHeader(32000, 16000, 1).toList(), bytes.copyOf(44).toList())
assertEquals(0.toByte(), bytes[44])
assertEquals(1.toByte(), bytes[44 + 16000])
assertEquals(2.toByte(), bytes.last())
assertEquals(32100L, recorder.finish())
bytes = file.readBytes()
assertEquals(44 + 32100, bytes.size)
assertEquals(BinaryEncoding.wavHeader(32100, 16000, 1).toList(), bytes.copyOf(44).toList())
} finally {
file.delete()
}
}
}
@@ -0,0 +1,108 @@
package com.faceclaw.app
import kotlin.math.pow
import kotlin.math.roundToInt
import kotlin.test.*
class ColorGlyphArtTest {
private val size = 16
private val clear = 0
private val white = argb(255, 255, 255, 255)
private val yellow = argb(255, 255, 204, 50)
private val brown = argb(255, 90, 50, 20)
private val darkGray = argb(255, 60, 60, 60)
private val green = argb(255, 120, 170, 60)
private val red = argb(255, 230, 60, 50)
private val lightRed = argb(255, 250, 140, 130)
private fun argb(a: Int, r: Int, g: Int, b: Int): Int = (a shl 24) or (r shl 16) or (g shl 8) or b
private fun glyph(pixel: (x: Int, y: Int) -> Int): IntArray = IntArray(size * size) { pixel(it % size, it / size) }
private fun inDisc(x: Int, y: Int): Boolean {
val dx = x + 0.5 - size / 2.0
val dy = y + 0.5 - size / 2.0
return dx * dx + dy * dy <= 7.0 * 7.0
}
private fun at(shade: ByteArray, x: Int, y: Int): Int = shade[y * size + x].toInt() and 0xff
private fun lit(level: Double): Int = (255 * level.pow(NotificationIconArt.GAMMA)).roundToInt()
@Test fun whiteGlyphIsItsAlpha() {
// An ordinary glyph drawn in white: antialiased edges, no color anywhere.
val pixels = glyph { x, y -> if (x in 4..11) argb(if (x == 4 || x == 11) 90 else 255, 255, 255, 255) else clear }
assertFalse(ColorGlyphArt.isColor(pixels))
assertContentEquals(ByteArray(pixels.size) { ((pixels[it] ushr 24) and 0xff).toByte() }, ColorGlyphArt.coverage(pixels))
}
@Test fun faintColorFringeIsNotColor() {
// Unpremultiplying a nearly transparent pixel can land anywhere; only real ink counts.
val pixels = glyph { x, _ -> if (x == 3) argb(8, 0, 0, 0) else if (x in 4..11) white else clear }
assertFalse(ColorGlyphArt.isColor(pixels))
}
@Test fun oneColorGlyphLightsFully() {
// 🖤: dark by lightness, but its shape is what matters.
val pixels = glyph { x, y -> if (inDisc(x, y)) darkGray else clear }
assertTrue(ColorGlyphArt.isColor(pixels))
val shade = ColorGlyphArt.coverage(pixels)
assertEquals(255, at(shade, 8, 8))
assertEquals(0, at(shade, 0, 0))
}
@Test fun darkDetailOnALightBodyStaysVisibleButDim() {
// 😀: a yellow disc with dark eyes and mouth; the body stays full, the features dim.
val pixels = glyph { x, y ->
when {
!inDisc(x, y) -> clear
(y in 5..6 && (x == 5 || x == 10)) || (y == 11 && x in 5..10) -> brown
else -> yellow
}
}
val shade = ColorGlyphArt.coverage(pixels)
assertEquals(255, at(shade, 8, 8))
assertEquals(lit(0.3), at(shade, 5, 5))
assertTrue(at(shade, 5, 5) > 0)
}
@Test fun lightDetailOnADarkerBodyDimsTheBody() {
// ✅: a green tile with a large white check; the check lights fully above a dimmer tile.
val pixels = glyph { x, y ->
when {
x !in 1..14 || y !in 1..14 -> clear
(x in 4..7 && y in 9..11) || (x in 7..12 && y in 4..8) -> white
else -> green
}
}
val shade = ColorGlyphArt.coverage(pixels)
assertEquals(255, at(shade, 9, 6))
assertEquals(lit(0.6), at(shade, 2, 2))
}
@Test fun smallHighlightLeavesTheBodyFull() {
// ❤️: a red body with a small glint; the glint is not detail worth dimming the body for.
val pixels = glyph { x, y ->
when {
!inDisc(x, y) -> clear
x in 4..5 && y in 4..5 -> lightRed
else -> red
}
}
val shade = ColorGlyphArt.coverage(pixels)
assertEquals(255, at(shade, 8, 8))
assertEquals(255, at(shade, 4, 4))
}
@Test fun edgeAlphaScalesTheShade() {
val pixels = glyph { x, y -> if (x == 2) argb(128, 60, 60, 60) else if (x in 3..12 && y in 3..12) darkGray else clear }
val shade = ColorGlyphArt.coverage(pixels)
assertEquals(128, at(shade, 2, 8))
assertEquals(0, at(shade, 1, 8))
}
@Test fun emptyGlyphIsEmpty() {
assertTrue(ColorGlyphArt.shade(IntArray(size * size)).all { it.toInt() == 0 })
}
}
@@ -0,0 +1,986 @@
package com.faceclaw.app
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertFalse
import kotlin.test.assertNotNull
import kotlin.test.assertTrue
private const val ARM = "F4:27:E9:CA:E8:3F"
private fun hex(text: String): ByteArray = ByteArray(text.length / 2) { text.substring(it * 2, it * 2 + 2).toInt(16).toByte() }
/** An image the way MCUboot lays one out: header, payload, then a TLV area with its SHA-256. */
private fun testImage(version: Triple<Int, Int, Int> = Triple(0, 5, 64), payloadSize: Int = 5000): ByteArray {
val headerSize = 512
val hash = ByteArray(32) { (it * 3 + version.third).toByte() }
val image = ByteArray(headerSize + payloadSize + 4 + 4 + 32)
fun put16(at: Int, value: Int) {
image[at] = value.toByte()
image[at + 1] = (value ushr 8).toByte()
}
fun put32(at: Int, value: Int) {
put16(at, value and 0xffff)
put16(at + 2, value ushr 16)
}
put32(0, 0x96f3b83d.toInt())
put16(8, headerSize)
put32(12, payloadSize)
image[20] = version.first.toByte()
image[21] = version.second.toByte()
put16(22, version.third)
for (i in 0 until payloadSize) image[headerSize + i] = (i * 7).toByte()
val tlv = headerSize + payloadSize
put16(tlv, 0x6907)
put16(tlv + 2, 4 + 4 + 32)
put16(tlv + 4, 0x10)
put16(tlv + 6, 32)
hash.copyInto(image, tlv + 8)
return image
}
/**
* One arm behind the StockLink port: MCUmgr's image and OS groups as flash.py meets them on real arms, and g1v0's HELLO,
* PROPS and asset store. Requests are answered synchronously, from the write. [netImage] is what a restore puts in image
* 1's slot.
*/
private class FakeArm(private val image: ByteArray, var stock: Boolean = false, private val netImage: ByteArray? = null) : StockLink {
var inbound: StockLinkListener? = null
var connected = false
var bond = BondState.BONDED
var bondRequests = 0
var bondResult = BondState.BONDED
val subscribed = ArrayList<String>()
val heartbeats = ArrayList<ByteArray>()
val calls = ArrayList<String>()
var runningVersion = "0.5.63"
var safeMode = false
var battery = 80
/** The hash in each image's second slot. */
val slotHashes = HashMap<Int, ByteArray>()
var slot1Hash: ByteArray?
get() = slotHashes[0]
set(value) {
if (value == null) slotHashes.remove(0) else slotHashes[0] = value
}
/** Images marked for the next boot, and whether permanently. */
val pending = HashMap<Int, Boolean>()
val confirms = ArrayList<Pair<Int, Boolean>>()
/** The image of the upload in progress, and what each image's last whole upload was. */
var uploadImage = -1
val uploads = HashMap<Int, ByteArray>()
var uploaded = ByteArray(0)
var uploadOffset = 0
var uploadRefusal = 0
/** After the upload the list shows this instead of the image's hash. */
var corruptAfterUpload = false
var confirmRefusal = 0
/** Only this image's confirm is refused; -1: any. */
var confirmRefusalImage = -1
/** This image's confirm is answered, and the list doesn't show it pending. */
var unmarkedImage = -1
/** No `ASSET_INFO`, as before 0.5.56. */
var storeSupported = true
var storeAvailable = true
var storeAssets = 0
var storeSeq = 0L
var storeState = 0
var storeError = 0
/** Bytes that aren't 0xFF in the store's directory sectors: Even's data, until a format. */
var directoryNonErased = 4096L
/** The format's NOTICE 8 never comes. */
var loseStoreNotice = false
/** The arm restarts during the wipe: no notice, and the job state back to idle. */
var wipeInterrupted = false
val formats = ArrayList<Boolean>()
var dropAtOffset = -1
var silentRequests = 0
/** The chunk at this offset gets no answer, once. */
var silentAtOffset = -1
var onChunk: (() -> Unit)? = null
var connects = 0
/** The OS remembers a table without g1v0's services, as after an install at the same address. */
var staleTable = false
var refreshes = 0
/** Android says bonded and the arm has lost its half. */
var staleBond = false
var removedBonds = 0
var inCase = false
/** Even's firmware's answer to its battery query; null: it says nothing. */
var stockBatteryReply: ByteArray? = null
val wantHash = G1Smp.imageHash(image)
override fun setListener(listener: StockLinkListener?) { inbound = listener }
override fun connect(address: String, timeoutMs: Int): Boolean {
connects++
connected = true
return true
}
override fun prepareLink(address: String, desiredMtu: Int, timeoutMs: Int) {}
override fun discoverServices(address: String, timeoutMs: Int): Boolean = connected
/** As FaceclawBleManager: a characteristic the table lacks throws, a refused one (no encryption) returns false. */
override fun enableNotifications(address: String, characteristicUuid: String, timeoutMs: Int): Boolean {
if (!connected) throw IllegalStateException("Not connected")
if (!hasCharacteristic(address, characteristicUuid)) throw IllegalStateException("Characteristic not found: $characteristicUuid")
if (stock && staleBond && characteristicUuid == G1Smp.NUS_TX_UUID) return false
subscribed.add(characteristicUuid)
return true
}
override fun hasCharacteristic(address: String, characteristicUuid: String): Boolean = when (characteristicUuid) {
G1Ble.EVT_CHAR_UUID -> !stock && !staleTable
G1Smp.NUS_TX_UUID -> stock
else -> true
}
override fun refreshServices(address: String, timeoutMs: Int): Boolean {
refreshes++
staleTable = false
return connected
}
override fun removeBond(address: String): Boolean {
removedBonds++
bond = BondState.NONE
staleBond = false
connected = false
return true
}
override fun isConnected(address: String): Boolean = connected
override fun bondState(address: String): BondState = bond
override fun requestBond(address: String) {
bondRequests++
bond = bondResult
}
override fun negotiatedMtu(address: String): Int = 100
override fun disconnect(address: String) { connected = false }
override fun close() { connected = false }
fun drop() {
connected = false
inbound?.onConnectionStateChange(ARM, false)
}
override fun writeFrames(address: String, characteristicUuid: String, frames: List<ByteArray>, mode: GattWriteMode, timeoutMs: Int): Boolean {
if (!connected) throw IllegalStateException("Not connected")
when (characteristicUuid) {
G1Smp.NUS_RX_UUID -> {
val frame = frames.single()
if (frame[0].toInt() == 0x2c) {
stockBatteryReply?.let { inbound?.onNotification(ARM, G1Smp.NUS_TX_UUID, it) }
} else {
heartbeats.add(frame)
}
}
G1Ble.PAIR_CHAR_UUID -> return bond == BondState.BONDED && !staleBond
G1Ble.CTRL_CHAR_UUID -> control(frames.single())
G1Smp.SMP_CHAR_UUID -> {
for (frame in frames) assertTrue(frame.size <= 97, "a write longer than the MTU allows")
smp(frames.reduce { a, b -> a + b })
}
else -> throw IllegalStateException("write to $characteristicUuid")
}
return true
}
private fun control(request: ByteArray) {
val op = request[0].toInt() and 0xff
val tag = request[1]
val args = request.copyOfRange(2, request.size)
var status = 0
var after: (() -> Unit)? = null
val body = when (op) {
G1Protocol.OP_HELLO -> {
val parts = runningVersion.split(".").map { it.toInt().toByte() }
val flags = (if (safeMode) 1 else 0) or 2 or (if (staleBond) 0 else 64) or (if (inCase) 128 else 0)
byteArrayOf(5, parts[0], parts[1], parts[2], 1, flags.toByte(), 0)
}
G1Protocol.OP_PROPS -> byteArrayOf(battery.toByte(), 0, 0, 0)
G1Protocol.OP_ASSET_INFO -> {
if (!storeSupported) {
status = 2
ByteArray(0)
} else {
ByteArray(12) + byteArrayOf(storeAssets.toByte(), storeState.toByte(), storeError.toByte(), if (storeAvailable) 1 else 0) +
le32(storeSeq.toInt())
}
}
G1Protocol.OP_FLASH_CRC -> {
calls.add("crc")
assertContentEquals(G1Protocol.flashCrcRequest(G1Protocol.STORE_ADDRESS, G1Protocol.STORE_DIRECTORY_BYTES), args)
if (safeMode) {
status = 7
} else {
after = { storeNotice(G1Protocol.STORE_JOB_CRC, 0, args + le32(0x1234) + le32(directoryNonErased.toInt())) }
}
ByteArray(0)
}
G1Protocol.OP_ASSET_FORMAT -> {
calls.add("format")
assertContentEquals(G1Protocol.assetFormatRequest(wipe = true), args)
when {
safeMode -> status = 7
storeState in 1..3 -> status = 13
else -> {
formats.add(true)
storeAssets = 0
storeSeq = 0
directoryNonErased = 0
if (wipeInterrupted) {
storeState = 0
} else {
storeState = 4
storeError = 0
if (!loseStoreNotice) after = { storeNotice(G1Protocol.STORE_JOB_FORMAT, 0, ByteArray(16)) }
}
}
}
ByteArray(0)
}
else -> ByteArray(0)
}
val event = ByteArray(9) + byteArrayOf(tag, status.toByte()) + body
event[0] = (0x80 or op).toByte()
inbound?.onNotification(ARM, G1Ble.EVT_CHAR_UUID, event)
after?.invoke()
}
private fun storeNotice(job: Int, result: Int, detail: ByteArray) {
val event = ByteArray(9) + byteArrayOf(G1Protocol.NOTICE_STORE_JOB.toByte(), job.toByte(), result.toByte()) + detail
event[0] = G1Protocol.EV_NOTICE.toByte()
inbound?.onNotification(ARM, G1Ble.EVT_CHAR_UUID, event)
}
private fun le32(value: Int): ByteArray = byteArrayOf(value.toByte(), (value ushr 8).toByte(), (value ushr 16).toByte(), (value ushr 24).toByte())
private fun imageOf(index: Int): ByteArray = when (index) {
0 -> image
1 -> netImage ?: throw AssertionError("an upload to image 1")
else -> throw AssertionError("an upload to image $index")
}
private fun smp(frame: ByteArray) {
val op = frame[0].toInt()
val group = frame[5].toInt()
val seq = frame[6].toInt() and 0xff
val cmd = frame[7].toInt()
val body = G1Cbor.decode(frame, 8) as Map<*, *>
if (silentRequests > 0) {
silentRequests--
return
}
when {
group == 1 && cmd == 1 -> {
calls.add("upload")
val off = (body["off"] as Long).toInt()
onChunk?.invoke()
if (off == silentAtOffset) {
silentAtOffset = -1
return
}
if (uploadRefusal != 0) return reply(op, group, seq, cmd, mapOf("rc" to uploadRefusal))
if (off == 0) {
// A first chunk names the image; the same file again resumes where it stopped, as MCUmgr does.
val index = (body["image"] as Long).toInt()
val expected = imageOf(index)
assertEquals(expected.size.toLong(), body["len"])
assertContentEquals(sha256(expected), body["sha"] as ByteArray)
if (index != uploadImage || uploadOffset >= expected.size) {
uploadImage = index
uploadOffset = 0
uploaded = ByteArray(0)
}
}
if (off == uploadOffset) {
val data = body["data"] as ByteArray
uploaded += data
uploadOffset += data.size
if (uploadOffset == imageOf(uploadImage).size) {
uploads[uploadImage] = uploaded
slotHashes[uploadImage] = if (corruptAfterUpload) ByteArray(32) else G1Smp.imageHash(uploaded)
}
}
if (uploadOffset >= dropAtOffset && dropAtOffset >= 0) {
dropAtOffset = -1
drop()
return
}
reply(op, group, seq, cmd, mapOf("rc" to 0, "off" to uploadOffset))
}
group == 1 && cmd == 0 && op == G1Smp.OP_READ -> {
calls.add("list")
val images = ArrayList<Map<String, Any?>>()
images.add(mapOf("image" to 0, "slot" to 0, "version" to runningVersion, "hash" to ByteArray(32), "active" to true, "confirmed" to true, "bootable" to true))
for ((index, hash) in slotHashes.toSortedMap()) {
val marked = pending[index]
val entry = mutableMapOf<String, Any?>("image" to index, "slot" to 1, "version" to "0.5.64", "hash" to hash, "bootable" to true)
if (marked != null && index != unmarkedImage) {
entry["pending"] = true
if (marked) entry["permanent"] = true
}
images.add(entry)
}
reply(op, group, seq, cmd, mapOf("images" to images))
}
group == 1 && cmd == 0 -> {
calls.add("confirm")
val hash = body["hash"] as ByteArray
val index = slotHashes.entries.firstOrNull { it.value.contentEquals(hash) }?.key
assertNotNull(index, "a confirm for a hash no slot holds")
if (index == 0 && netImage == null) {
assertEquals(true, body["confirm"])
assertContentEquals(wantHash, hash)
}
val permanent = body["confirm"] as Boolean
confirms.add(index to permanent)
val refused = if (confirmRefusalImage < 0 || confirmRefusalImage == index) confirmRefusal else 0
if (refused == 0) pending[index] = permanent
reply(op, group, seq, cmd, mapOf("rc" to refused))
}
group == 0 && cmd == 5 -> {
calls.add("reset")
reply(op, group, seq, cmd, emptyMap())
drop()
}
else -> throw IllegalStateException("SMP $group/$cmd")
}
}
/** The reply comes in pieces, as notifications do. */
private fun reply(op: Int, group: Int, seq: Int, cmd: Int, body: Map<String, Any?>) {
val frame = G1Smp.frame(op or 1, group, seq, cmd, body)
for (piece in G1Smp.split(frame, 23)) inbound?.onNotification(ARM, G1Smp.SMP_CHAR_UUID, piece)
}
}
private class Recorder : FaceclawFirmwareFlasherListener {
val states = ArrayList<String>()
val logs = ArrayList<String>()
var progress = 0L
var total = 0L
var success: Boolean? = null
var detail = ""
override fun onLog(line: String?) { logs.add(line ?: "") }
override fun onProgress(lens: String?, componentIndex: Int, componentCount: Int, blockIndex: Int, blockCount: Int, bytesSent: Long, bytesTotal: Long) {
progress = bytesSent
total = bytesTotal
}
override fun onState(state: String?, detail: String?) { states.add(state ?: "") }
override fun onComplete(success: Boolean, detail: String?) {
this.success = success
this.detail = detail ?: ""
}
}
private val FAST = G1FlashTimings(
connectTimeoutMs = 50, servicesTimeoutMs = 50, descriptorTimeoutMs = 50, writeTimeoutMs = 50, pairingMs = 200, retryDelayMs = 1,
controlReplyMs = 20, controlTries = 2, uploadReplyMs = 150, smpReplyMs = 150, resetReplyMs = 50, heartbeatMs = 30, pollMs = 5, uploadRetries = 5,
storePollMs = 10, wipeMs = 2_000, crcMs = 200,
)
class G1SmpStockTest {
@Test
fun stocksBatteryReplyIsReadOnlyWhenItIsOne() {
// What the real firmware answered in emulation (g1-fw BLE_COMMAND_SET_REPORT), first bytes.
assertEquals(100, G1Smp.parseStockBattery(byteArrayOf(0x2c, 0x66, 0x64, 0x64, 0xf9.toByte(), 0x80.toByte())))
assertEquals(null, G1Smp.parseStockBattery(byteArrayOf(0x2c, 0x66, 0)))
assertEquals(null, G1Smp.parseStockBattery(byteArrayOf(0x2c, 0x66, 101)))
assertEquals(null, G1Smp.parseStockBattery(byteArrayOf(0x2c, 0xc9.toByte(), 50)))
assertEquals(null, G1Smp.parseStockBattery(byteArrayOf(0x25, 0x66, 50)))
assertEquals(null, G1Smp.parseStockBattery(byteArrayOf(0x2c, 0x66)))
}
}
class G1SmpTest {
private val sha = ByteArray(32) { it.toByte() }
/** The expected bytes come from scouter_kit's tools/g1host.py, whose requests were proven on real arms. */
@Test
fun requestsMatchTheProvenPythonTool() {
val image = ByteArray(574408)
image[0] = 1
image[1] = 2
image[2] = 3
val first = G1Smp.uploadRequest(image, 0, sha).toMutableMap()
first["data"] = byteArrayOf(1, 2, 3)
assertEquals(
"0200004e00010001a665696d61676500636c656e1a0008c3c8636f666600637368615820000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f6464617461430102036775706772616465f4",
BinaryEncoding.bytesToHex(G1Smp.frame(G1Smp.OP_WRITE, G1Smp.GROUP_IMAGE, 0, G1Smp.CMD_IMAGE_UPLOAD, first)),
)
assertEquals(
"0200001100010101a2636f6666190800646461746143010203",
BinaryEncoding.bytesToHex(G1Smp.frame(G1Smp.OP_WRITE, G1Smp.GROUP_IMAGE, 1, G1Smp.CMD_IMAGE_UPLOAD, mapOf("off" to 2048, "data" to byteArrayOf(1, 2, 3)))),
)
assertEquals(
"0200003100010500a264686173685820000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f67636f6e6669726df5",
BinaryEncoding.bytesToHex(G1Smp.frame(G1Smp.OP_WRITE, G1Smp.GROUP_IMAGE, 5, G1Smp.CMD_IMAGE_STATE, G1Smp.confirmRequest(sha))),
)
assertEquals("0000000100010700a0", BinaryEncoding.bytesToHex(G1Smp.frame(G1Smp.OP_READ, G1Smp.GROUP_IMAGE, 7, G1Smp.CMD_IMAGE_STATE, emptyMap())))
assertEquals("020000010000ff05a0", BinaryEncoding.bytesToHex(G1Smp.frame(G1Smp.OP_WRITE, G1Smp.GROUP_OS, 255, G1Smp.CMD_OS_RESET, emptyMap())))
}
@Test
fun cborMatchesPythonAndRoundTrips() {
val value = mapOf("n" to -5, "big" to 70000, "huge" to 5000000000L, "t" to true, "s" to "ab")
val encoded = G1Cbor.encode(value)
assertEquals("a5616e24636269671a0001117064687567651b000000012a05f2006174f56173626162", BinaryEncoding.bytesToHex(encoded))
val decoded = G1Cbor.decode(encoded) as Map<*, *>
assertEquals(-5L, decoded["n"])
assertEquals(70000L, decoded["big"])
assertEquals(5000000000L, decoded["huge"])
assertEquals(true, decoded["t"])
assertEquals("ab", decoded["s"])
}
@Test
fun cborReadsTheIndefiniteLengthsOfAnImageList() {
// {"images": [{"slot": 1, "active": true}]}, each container of indefinite length.
val data = hex("bf66696d616765739fbf64736c6f74016661637469766" + "5f5ffffff")
val slots = (G1Cbor.decode(data) as Map<*, *>)["images"] as List<*>
val slot = slots.single() as Map<*, *>
assertEquals(1L, slot["slot"])
assertEquals(true, slot["active"])
}
@Test
fun malformedCborIsAnErrorNotAWrongValue() {
assertFailsWith<IllegalArgumentException> { G1Cbor.decode(hex("5820")) }
assertFailsWith<IllegalArgumentException> { G1Cbor.decode(hex("bf")) }
assertFailsWith<IllegalArgumentException> { G1Cbor.decode(ByteArray(0)) }
}
@Test
fun repliesAreJoinedAcrossNotifications() {
val assembler = G1Smp.Assembler()
val one = G1Smp.frame(3, 1, 7, 1, mapOf("off" to 2048, "rc" to 0))
val two = G1Smp.frame(3, 1, 8, 0, emptyMap())
val all = one + two
val replies = ArrayList<Pair<Int, Map<*, *>>>()
for (start in all.indices step 5) replies += assembler.feed(all.copyOfRange(start, minOf(start + 5, all.size)))
assertEquals(listOf(7, 8), replies.map { it.first })
assertEquals(2048, G1Smp.replyOffset(replies[0].second))
assertEquals(0, G1Smp.replyCode(replies[1].second))
}
@Test
fun anImageSaysWhatItIs() {
val image = testImage(Triple(0, 5, 300))
assertEquals("0.5.300", G1Smp.imageVersion(image))
assertContentEquals(ByteArray(32) { (it * 3 + 300).toByte() }, G1Smp.imageHash(image))
assertFailsWith<IllegalArgumentException> { G1Smp.imageVersion(ByteArray(100)) }
val cut = image.copyOf(image.size - 20)
assertFailsWith<IllegalArgumentException> { G1Smp.imageHash(cut) }
}
@Test
fun writesFitTheMtu() {
val pieces = G1Smp.split(ByteArray(1000), 247)
assertTrue(pieces.all { it.size <= 244 })
assertEquals(1000, pieces.sumOf { it.size })
}
}
class G1ArmFlashFlowTest {
private val image = testImage()
private fun flow(arm: FakeArm, listener: Recorder, mode: G1ArmFlashFlow.Mode, window: Int = 100) =
G1ArmFlashFlow(arm, ARM, "left", "image.bin", "", mode, window, listener, FAST, testPlatform(), { image })
private fun run(arm: FakeArm, mode: G1ArmFlashFlow.Mode, window: Int = 100): Recorder {
val listener = Recorder()
flow(arm, listener, mode, window).run()
assertNotNull(listener.success, "the flow must always finish")
return listener
}
private fun confirmIsLast(arm: FakeArm) {
assertEquals(listOf("confirm", "reset"), arm.calls.takeLast(2))
assertEquals(1, arm.calls.count { it == "confirm" })
}
@Test
fun anUpdateUploadsChecksThenConfirmsAndResets() {
val arm = FakeArm(image)
val result = run(arm, G1ArmFlashFlow.Mode.UPDATE)
assertEquals(true, result.success, result.detail)
assertEquals("""{"flashed":true,"version":"0.5.64"}""", result.detail)
assertContentEquals(image, arm.uploaded)
assertEquals(image.size.toLong(), result.progress)
assertEquals(image.size.toLong(), result.total)
confirmIsLast(arm)
// The list before the confirm is what proves the slot holds this image.
assertEquals("list", arm.calls[arm.calls.indexOf("confirm") - 1])
assertTrue(arm.heartbeats.isEmpty(), "g1v0 has no heartbeat")
assertTrue("rebooting" in result.states)
}
@Test
fun anArmAlreadyOnThisVersionIsLeftAlone() {
val arm = FakeArm(image)
arm.runningVersion = "0.5.64"
val result = run(arm, G1ArmFlashFlow.Mode.UPDATE)
assertEquals(true, result.success)
assertEquals("""{"flashed":false,"version":"0.5.64"}""", result.detail)
assertTrue(arm.calls.isEmpty(), "nothing goes to the update service")
}
@Test
fun anArmInSafeModeOnThisVersionIsInstalledAgain() {
val arm = FakeArm(image)
arm.runningVersion = "0.5.64"
arm.safeMode = true
val result = run(arm, G1ArmFlashFlow.Mode.UPDATE)
assertEquals("""{"flashed":true,"version":"0.5.64"}""", result.detail)
confirmIsLast(arm)
}
@Test
fun aSlotThatDoesNotHoldTheImageIsNeverConfirmed() {
val arm = FakeArm(image)
arm.corruptAfterUpload = true
val result = run(arm, G1ArmFlashFlow.Mode.UPDATE)
assertEquals(false, result.success)
assertFalse("confirm" in arm.calls)
assertFalse("reset" in arm.calls)
assertTrue("Nothing was installed" in result.detail, result.detail)
}
@Test
fun aRefusedUploadIsNeverConfirmed() {
val arm = FakeArm(image)
arm.uploadRefusal = 9
val result = run(arm, G1ArmFlashFlow.Mode.UPDATE)
assertEquals(false, result.success)
assertFalse("confirm" in arm.calls)
assertTrue("error 9" in result.detail, result.detail)
}
@Test
fun aRefusedConfirmIsNotAReset() {
val arm = FakeArm(image)
arm.confirmRefusal = 2
val result = run(arm, G1ArmFlashFlow.Mode.UPDATE)
assertEquals(false, result.success)
assertFalse("reset" in arm.calls)
}
@Test
fun aLowBatteryStopsAnUpdateBeforeAnyUpload() {
val arm = FakeArm(image)
arm.battery = 12
val result = run(arm, G1ArmFlashFlow.Mode.UPDATE)
assertEquals(false, result.success)
assertTrue("12%" in result.detail, result.detail)
assertTrue(arm.calls.isEmpty())
}
@Test
fun aDamagedImageFailsBeforeTheLinkIsTouched() {
val listener = Recorder()
val arm = FakeArm(image)
G1ArmFlashFlow(arm, ARM, "left", "image.bin", "", G1ArmFlashFlow.Mode.UPDATE, 100, listener, FAST, testPlatform(), { ByteArray(100) }).run()
assertEquals(false, listener.success)
assertEquals(0, arm.connects)
}
@Test
fun anUnreadableImageIsAClearFailure() {
val listener = Recorder()
val arm = FakeArm(image)
G1ArmFlashFlow(arm, ARM, "left", "image.bin", "", G1ArmFlashFlow.Mode.STOCK, 100, listener, FAST, testPlatform(), { throw IllegalStateException("gone") }).run()
assertEquals(false, listener.success)
assertTrue("could not read the firmware" in listener.detail, listener.detail)
}
@Test
fun aLostLinkResumesTheUpload() {
val arm = FakeArm(image)
arm.dropAtOffset = 2048
val result = run(arm, G1ArmFlashFlow.Mode.UPDATE)
assertEquals(true, result.success, result.detail)
assertContentEquals(image, arm.uploaded)
assertTrue(arm.connects >= 2, "it connected again")
confirmIsLast(arm)
}
@Test
fun aChunkThatGetsNoAnswerIsSentAgain() {
val arm = FakeArm(image)
arm.silentAtOffset = 2048
val result = run(arm, G1ArmFlashFlow.Mode.UPDATE)
assertEquals(true, result.success, result.detail)
assertContentEquals(image, arm.uploaded)
confirmIsLast(arm)
}
@Test
fun anUploadThatNeverGetsAnswersGivesUpWithoutConfirming() {
val arm = FakeArm(image)
arm.silentRequests = 1000
val result = run(arm, G1ArmFlashFlow.Mode.UPDATE)
assertEquals(false, result.success)
assertFalse("confirm" in arm.calls)
}
@Test
fun aSlotAlreadyHoldingTheImageSkipsTheUpload() {
val arm = FakeArm(image)
arm.slot1Hash = arm.wantHash
val result = run(arm, G1ArmFlashFlow.Mode.UPDATE)
assertEquals(true, result.success, result.detail)
assertFalse("upload" in arm.calls)
confirmIsLast(arm)
}
@Test
fun stockGetsAHeartbeatEvenWhileAChunkWaits() {
val arm = FakeArm(image, stock = true)
// A chunk can wait a long time, as the first does while the arm erases its slot: the heartbeat has to keep going.
arm.silentAtOffset = 2048
val result = run(arm, G1ArmFlashFlow.Mode.STOCK)
assertEquals(true, result.success, result.detail)
assertTrue(arm.heartbeats.size >= 3, "heartbeats: ${arm.heartbeats.size}")
assertTrue(G1Smp.NUS_TX_UUID in arm.subscribed)
assertEquals(0x25, arm.heartbeats[0][0].toInt())
assertEquals(arm.heartbeats[1][3], 1.toByte())
assertEquals(arm.heartbeats[1][5], 1.toByte())
confirmIsLast(arm)
}
@Test
fun stockIsPairedWithFirst() {
val arm = FakeArm(image, stock = true)
arm.bond = BondState.NONE
val result = run(arm, G1ArmFlashFlow.Mode.STOCK)
assertEquals(true, result.success, result.detail)
assertEquals(1, arm.bondRequests)
assertTrue("pairing" in result.states)
}
@Test
fun aRefusedBondIsRetriedThenReported() {
val arm = FakeArm(image)
arm.bond = BondState.NONE
arm.bondResult = BondState.NONE
val result = run(arm, G1ArmFlashFlow.Mode.UPDATE, window = 1)
assertEquals(false, result.success)
assertTrue(arm.calls.isEmpty())
assertTrue("Couldn't reach the left arm" in result.detail, result.detail)
}
@Test
fun aPlatformThatCannotSeeBondsReliesOnTheEncryptedWrite() {
val arm = FakeArm(image)
arm.bond = BondState.UNKNOWN
val listener = Recorder()
// The write to PAIR fails on an unknown bond in this fake, as on an arm out of the case: no encrypted link.
flow(arm, listener, G1ArmFlashFlow.Mode.PROBE, 1).run()
assertEquals(false, listener.success)
assertEquals(0, arm.bondRequests)
assertTrue("an arm pairs only in its case" in listener.detail, listener.detail)
}
@Test
fun aStaleBondInTheCaseIsRemovedAndMadeAgain() {
val arm = FakeArm(image)
arm.staleBond = true
arm.inCase = true
val result = run(arm, G1ArmFlashFlow.Mode.UPDATE)
assertEquals(true, result.success, result.detail)
assertEquals(1, arm.removedBonds)
assertEquals(1, arm.bondRequests)
confirmIsLast(arm)
}
@Test
fun aBondIsNeverGivenUpOutOfTheCase() {
val arm = FakeArm(image)
arm.staleBond = true
arm.inCase = false
val result = run(arm, G1ArmFlashFlow.Mode.UPDATE, window = 1)
assertEquals(false, result.success)
assertEquals(0, arm.removedBonds, "no new bond can be made out of the case")
}
@Test
fun aStaleBondOnStockIsRemovedAndMadeAgain() {
val arm = FakeArm(image, stock = true)
arm.staleBond = true
val result = run(arm, G1ArmFlashFlow.Mode.STOCK)
assertEquals(true, result.success, result.detail)
assertEquals(1, arm.removedBonds)
assertEquals(1, arm.bondRequests)
}
@Test
fun anOldServiceTableIsReadAgainBeforeItIsBelieved() {
val arm = FakeArm(image)
arm.staleTable = true
val result = run(arm, G1ArmFlashFlow.Mode.PROBE)
assertEquals(true, result.success, result.detail)
assertEquals(1, arm.refreshes)
assertTrue("\"firmware\":\"g1v0\"" in result.detail, result.detail)
}
@Test
fun aLowBatteryOnStockStopsBeforeAnyUpload() {
val arm = FakeArm(image, stock = true)
arm.stockBatteryReply = byteArrayOf(0x2c, 0x66, 12, 12)
val result = run(arm, G1ArmFlashFlow.Mode.STOCK)
assertEquals(false, result.success)
assertTrue("12%" in result.detail, result.detail)
assertFalse("upload" in arm.calls)
}
@Test
fun aChargedArmOnStockGoesOn() {
val arm = FakeArm(image, stock = true)
arm.stockBatteryReply = byteArrayOf(0x2c, 0x66, 100, 100)
val result = run(arm, G1ArmFlashFlow.Mode.STOCK)
assertEquals(true, result.success, result.detail)
assertTrue(result.logs.any { "battery 100%" in it }, result.logs.toString())
}
@Test
fun aStockArmThatSaysNothingAboutItsBatteryGoesOn() {
val arm = FakeArm(image, stock = true)
val result = run(arm, G1ArmFlashFlow.Mode.STOCK)
assertEquals(true, result.success, result.detail)
assertTrue(result.logs.any { "battery unknown" in it }, result.logs.toString())
}
@Test
fun aProbeSaysWhatAnArmRuns() {
val arm = FakeArm(image)
val result = run(arm, G1ArmFlashFlow.Mode.PROBE)
assertEquals(true, result.success, result.detail)
assertEquals("""{"firmware":"g1v0","version":"0.5.63","safeMode":false,"inCase":false,"battery":80}""", result.detail)
assertTrue(arm.calls.isEmpty())
}
@Test
fun aProbeOfEvensFirmwareNeedsNoPairing() {
val arm = FakeArm(image, stock = true)
arm.bond = BondState.NONE
val result = run(arm, G1ArmFlashFlow.Mode.PROBE)
assertEquals("""{"firmware":"stock"}""", result.detail)
assertEquals(0, arm.bondRequests)
}
// ---- back to Even's firmware ----
private val evenApp = testImage(Triple(0, 0, 0), payloadSize = 7000)
private val evenNet = testImage(Triple(0, 0, 1), payloadSize = 3000)
private fun restoreArm(): FakeArm = FakeArm(evenApp, netImage = evenNet).also { it.runningVersion = "0.5.66" }
private fun restore(arm: FakeArm, window: Int = 100): Recorder {
val listener = Recorder()
G1ArmFlashFlow(arm, ARM, "right", "app.bin", "net.bin", G1ArmFlashFlow.Mode.RESTORE, window, listener, FAST, testPlatform(), { path ->
when (path) {
"app.bin" -> evenApp
"net.bin" -> evenNet
else -> throw IllegalStateException("no $path")
}
}).run()
assertNotNull(listener.success, "the flow must always finish")
return listener
}
@Test
fun aRestoreUploadsBothImagesThenMarksTheNetworkCoreFirst() {
val arm = restoreArm()
val result = restore(arm)
assertEquals(true, result.success, result.detail)
assertEquals("""{"flashed":true,"firmware":"stock","wiped":false}""", result.detail)
assertContentEquals(evenNet, arm.uploads[1])
assertContentEquals(evenApp, arm.uploads[0])
// Image 1 as a test install, as Even's app and g1v0's fallback do; the application for good.
assertEquals(listOf(1 to false, 0 to true), arm.confirms)
assertEquals(listOf("confirm", "confirm", "list", "reset"), arm.calls.takeLast(4))
assertEquals((evenApp.size + evenNet.size).toLong(), result.total)
assertEquals(result.total, result.progress)
assertTrue("rebooting" in result.states)
}
@Test
fun aStoreNeverWrittenKeepsEvensData() {
val arm = restoreArm()
val result = restore(arm)
assertEquals(true, result.success, result.detail)
assertEquals(listOf("crc"), arm.calls.filter { it == "crc" || it == "format" })
assertTrue(arm.formats.isEmpty())
assertFalse("wiping" in result.states)
}
@Test
fun aStoreThatHoldsAssetsIsWipedBeforeAnyUpload() {
val arm = restoreArm()
arm.storeAssets = 2
arm.storeSeq = 5
val result = restore(arm)
assertEquals(true, result.success, result.detail)
assertEquals("""{"flashed":true,"firmware":"stock","wiped":true}""", result.detail)
assertEquals(listOf(true), arm.formats)
assertTrue(arm.calls.indexOf("format") < arm.calls.indexOf("upload"))
assertFalse("crc" in arm.calls, "a store with assets needs no fingerprint")
assertTrue("wiping" in result.states)
}
@Test
fun anEmptyStoreWhoseDirectoryWasErasedIsWiped() {
// A format resets the commit count, and erased the two directory sectors over Even's data.
val arm = restoreArm()
arm.directoryNonErased = 0
val result = restore(arm)
assertEquals(true, result.success, result.detail)
assertEquals(listOf("crc", "format"), arm.calls.filter { it == "crc" || it == "format" })
}
@Test
fun firmwareWithoutTheStoreIsNotWiped() {
val arm = restoreArm()
arm.storeSupported = false
val result = restore(arm)
assertEquals(true, result.success, result.detail)
assertFalse("crc" in arm.calls)
assertFalse("format" in arm.calls)
}
@Test
fun aLostWipeNoticeIsReadFromTheStoreState() {
val arm = restoreArm()
arm.storeAssets = 1
arm.storeSeq = 1
arm.loseStoreNotice = true
val result = restore(arm)
assertEquals(true, result.success, result.detail)
assertTrue("\"wiped\":true" in result.detail, result.detail)
}
@Test
fun anArmThatRestartsDuringTheWipeGetsNothingInstalled() {
val arm = restoreArm()
arm.storeAssets = 1
arm.storeSeq = 1
arm.wipeInterrupted = true
val result = restore(arm)
assertEquals(false, result.success)
assertTrue("restarted while erasing" in result.detail, result.detail)
assertFalse("upload" in arm.calls)
assertFalse("confirm" in arm.calls)
}
@Test
fun safeModeWithAWrittenStoreInstallsNothing() {
val arm = restoreArm()
arm.safeMode = true
arm.storeAssets = 1
arm.storeSeq = 1
val result = restore(arm)
assertEquals(false, result.success)
assertTrue("safe mode" in result.detail, result.detail)
assertFalse("format" in arm.calls)
assertFalse("upload" in arm.calls)
}
@Test
fun aLowBatteryStopsARestoreBeforeTheWipe() {
val arm = restoreArm()
arm.battery = 12
arm.storeAssets = 1
arm.storeSeq = 1
val result = restore(arm)
assertEquals(false, result.success)
assertTrue("12%" in result.detail, result.detail)
assertFalse("format" in arm.calls)
assertFalse("upload" in arm.calls)
}
@Test
fun aRefusedNetworkCoreNeverLeavesTheApplicationMarked() {
val arm = restoreArm()
arm.confirmRefusal = 3
arm.confirmRefusalImage = 1
val result = restore(arm)
assertEquals(false, result.success)
assertEquals(listOf(1 to false), arm.confirms)
assertFalse(0 in arm.pending)
assertFalse("reset" in arm.calls)
assertTrue("network core" in result.detail, result.detail)
}
@Test
fun anImageTheArmDidNotMarkIsNoReset() {
val arm = restoreArm()
arm.unmarkedImage = 0
val result = restore(arm)
assertEquals(false, result.success)
assertFalse("reset" in arm.calls)
assertTrue("didn't mark" in result.detail, result.detail)
}
@Test
fun aLostLinkResumesARestoreUpload() {
val arm = restoreArm()
arm.dropAtOffset = 2048
val result = restore(arm)
assertEquals(true, result.success, result.detail)
assertContentEquals(evenNet, arm.uploads[1])
assertContentEquals(evenApp, arm.uploads[0])
}
@Test
fun cancellingDuringTheUploadStopsBeforeTheConfirm() {
val arm = FakeArm(image)
val listener = Recorder()
val flow = flow(arm, listener, G1ArmFlashFlow.Mode.UPDATE)
arm.onChunk = { flow.cancel() }
flow.run()
assertEquals(false, listener.success)
assertEquals("Cancelled.", listener.detail)
assertFalse("confirm" in arm.calls)
}
}
class G1StoreProtocolTest {
/** The bytes of scouter_kit's tools/g1host.py: `struct.pack("<II", FORMAT_MAGIC, WIPE_MAGIC)`. */
@Test
fun requestsMatchThePythonTool() {
assertEquals("53414b5300005750", BinaryEncoding.bytesToHex(G1Protocol.assetFormatRequest(wipe = true)))
assertEquals("53414b53", BinaryEncoding.bytesToHex(G1Protocol.assetFormatRequest(wipe = false)))
assertEquals("0000140000200000", BinaryEncoding.bytesToHex(G1Protocol.flashCrcRequest(0x140000, 8192)))
}
@Test
fun assetInfoIsRead() {
// data 10, free 20, largest 30, 2 assets, state 4, error 0, flags 1, seq 0x01020304
val info = G1Protocol.parseAssetInfo(hex("0a000000140000001e00000002040001" + "04030201"))
assertNotNull(info)
assertEquals(2, info.assets)
assertEquals(4, info.state)
assertTrue(info.available)
assertFalse(info.running)
assertEquals(0x01020304L, info.seq)
assertEquals(null, G1Protocol.parseAssetInfo(ByteArray(19)))
}
@Test
fun aStoreJobsNoticeIsRead() {
val crc = hex("05" + "0000000000000000" + "08" + "0400" + "00001400" + "00200000" + "78563412" + "00100000")
val notice = G1Protocol.parseStoreNotice(crc)
assertNotNull(notice)
assertEquals(G1Protocol.STORE_JOB_CRC, notice.job)
assertEquals(0, notice.result)
assertEquals(0x140000, notice.address)
assertEquals(8192, notice.n)
assertEquals(4096L, notice.nonErased)
// Another notice kind, or a short one, is none.
assertEquals(null, G1Protocol.parseStoreNotice(hex("05" + "0000000000000000" + "07" + "2a2a")))
assertEquals(null, G1Protocol.parseStoreNotice(hex("05" + "0000000000000000" + "08" + "0300")))
}
}
File diff suppressed because it is too large. Load diff
@@ -855,4 +855,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()
}
}
}
@@ -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)
}
}
@@ -0,0 +1,167 @@
package com.faceclaw.app
import kotlin.test.*
/** Vectors are bytes captured from a RayNeo iO (Strix OS 1.0.5.12 / 1.0.4.12 + CFW) on 2026-11-01. */
class RayNeoIoProtocolTest {
private fun hex(value: String) = value.chunked(2).map { it.toInt(16).toByte() }.toByteArray()
private val phoneId = hex("fac582dfa91e")
private val glassesId = hex("006a5e5f33c2")
@Test
fun crcAndFrames() {
assertEquals(0x31c3, RayNeoIoProtocol.crc16Xmodem("123456789".encodeToByteArray()))
val payload = hex("100001")
val frame = RayNeoIoProtocol.encodeFrame(0x1234, RayNeoIoProtocol.BUSINESS_CFW, payload)
assertEquals(13, frame.size)
assertContentEquals(hex("aa55000712340" + "0f0100001"), frame.copyOfRange(0, 11))
val decoded = RayNeoIoProtocol.decodeFrame(frame)!!
assertEquals(0x1234, decoded.messageNumber)
assertEquals(0xf0, decoded.business)
assertContentEquals(payload, decoded.payload)
frame[9] = 9
assertNull(RayNeoIoProtocol.decodeFrame(frame))
}
@Test
fun reassemblerSpansNotificationsAndSkipsJunk() {
val a = RayNeoIoProtocol.encodeFrame(1, 15, ByteArray(300) { it.toByte() })
val b = RayNeoIoProtocol.encodeFrame(2, 0xf0, hex("0195000001070024cf"))
val stream = hex("0102") + a + b
val r = RayNeoIoProtocol.FrameReassembler()
val frames = RayNeoIoProtocol.writes(stream, 23).flatMap { r.ingest(it) }
assertEquals(listOf(1, 2), frames.map { it.messageNumber })
assertEquals(300, frames[0].payload.size)
assertEquals(0, r.pendingBytes)
// a corrupt head is skipped, the next good frame still decodes
val bad = a.copyOf().also { it[20] = (it[20] + 1).toByte() }
assertEquals(listOf(2), r.ingest(bad + b).map { it.messageNumber })
}
@Test
fun handshakeMatchesCapture() {
// the Mac harness's request (iOS platform byte, name "Amoli", model "Mac")
assertContentEquals(
hex("11002810000101110006fac582dfa91e120005416d6f6c69130001011600066950686f6e651700034d6163"),
RayNeoIoProtocol.versionRequest(phoneId, "Amoli", "iPhone", "Mac", RayNeoIoProtocol.PLATFORM_IOS),
)
val reply = RayNeoIoProtocol.parseVersionReply(
hex("11004a10000102110006006a5e5f33c212000e5261794e656f20694f2d323232391300010514000f3030364135453546333343323232391500010518000f303036413545354633334332323239"),
)!!
assertContentEquals(glassesId, reply.glassesId)
assertEquals(2, reply.protocolVersion)
assertEquals("006A5E5F33C2229", reply.serial)
assertFalse(reply.accountBound)
assertContentEquals(
hex("18002a100004875e2013110020efed651a6aacd1834e8023891eaf80ec4a25814db5222e42655b71fdd373ab96"),
RayNeoIoProtocol.guestAuthRequest(hex("875e2013"), phoneId, glassesId),
)
val authReply = hex("19002a1000042316e62111002082289251b37c1ddb76694d5a4bb4e911a79ad8f2867d7379bb8125f36ae6acac")
assertTrue(RayNeoIoProtocol.isGuestAuthReply(authReply))
assertTrue(RayNeoIoProtocol.verifyGuestAuthReply(authReply, phoneId, glassesId))
assertFalse(RayNeoIoProtocol.verifyGuestAuthReply(authReply, hex("fac582dfa91f"), glassesId))
}
@Test
fun envelopes() {
assertContentEquals(
hex("080110011a027b7d"),
RayNeoIoProtocol.encodeEnvelope(RayNeoIoProtocol.Envelope(RayNeoIoProtocol.OTA_VERSION, "{}")),
)
val status = RayNeoIoProtocol.decodeEnvelope(hex(GENERAL_STATUS))!!
assertEquals(1, status.type)
assertEquals(98, RayNeoIoProtocol.batteryFromLauncherJson(status.json))
assertEquals(100, RayNeoIoProtocol.batteryFromLauncherJson("""{"cmd":"battery_change","payload":{"value":100,"mode":1}}"""))
assertEquals(-1, RayNeoIoProtocol.batteryFromLauncherJson("""{"cmd":"screen_status","payload":{"value":1}}"""))
val request = RayNeoIoProtocol.Envelope(1, RayNeoIoProtocol.GENERAL_STATUS_REQUEST, sequence = 300, mode = RayNeoIoProtocol.MODE_REQUEST)
val back = RayNeoIoProtocol.decodeEnvelope(RayNeoIoProtocol.encodeEnvelope(request))!!
assertEquals(300, back.sequence)
assertEquals(RayNeoIoProtocol.MODE_REQUEST, back.mode)
assertEquals(request.json, back.json)
}
@Test
fun cfwLink() {
assertContentEquals(hex("100001"), RayNeoIoProtocol.controlPacket(RayNeoIoProtocol.CTRL_QUERY))
val record = ByteArray(500) { it.toByte() }
val packets = RayNeoIoProtocol.cfwPackets(record, 0x95, 240)
assertEquals(listOf(0x81, 0x01, 0x41), packets.map { it[0].toInt() and 255 })
assertTrue(packets.all { it[1] == 0x95.toByte() })
assertContentEquals(record, packets.flatMap { it.drop(2) }.toByteArray())
assertEquals(0xc1, RayNeoIoProtocol.cfwPackets(ByteArray(5), 1)[0][0].toInt() and 255)
val ack = RayNeoIoProtocol.parseCfwAck(hex("0195000001070024cf"))!!
assertFalse(ack.nack)
assertEquals(0x95, ack.streamId)
assertEquals(0, ack.messageId)
assertEquals(1, ack.lens)
assertEquals(7, ack.size)
assertEquals(0xcf24, ack.checksum)
assertTrue(RayNeoIoProtocol.parseCfwAck(hex("0395000002070024cf"))!!.nack)
assertNull(RayNeoIoProtocol.parseCfwAck(hex("0195000003070024cf")))
// Glassly v13 IDENT, then the Faceclaw fork's with the revision string appended
val v13 = RayNeoIoProtocol.parseIdent(hex("110d1c02dc00f00000010d007f474c4153534c594346572d494f2f31330019"))!!
assertEquals(540, v13.width)
assertEquals(220, v13.height)
assertEquals(240, v13.maxChunk)
assertEquals(13, v13.revision)
assertEquals("GLASSLYCFW-IO/13", v13.name)
assertFalse(v13.faceclaw)
val fc = RayNeoIoProtocol.parseIdent(hex("110d1c02dc00f00000010d00ff474c4153534c594346572d494f2f3133001946616365636c61772f333800"))!!
assertTrue(fc.faceclaw)
assertEquals(38, fc.faceclawRevision)
assertFalse(fc.brightnessControl)
val bright = RayNeoIoProtocol.parseIdent(hex("110d1c02dc00f00000010d00ff474c4153534c594346572d494f2f3133001946616365636c61772f33380062726967687433302061" + "6c73313600"))!!
assertEquals(listOf("bright30", "als16"), bright.features)
assertEquals(38, bright.faceclawRevision)
assertTrue(bright.brightnessControl)
// a Glassly build with no display lists has no brightness control, whatever else it says
assertFalse(RayNeoIoProtocol.Ident(540, 220, 240, 13, 0x7f, "x", -1, listOf("bright30", "als16")).brightnessControl)
val als = ByteArray(25)
als[0] = 0x1f; als[1] = 65; als[2] = 76; als[3] = 1
assertEquals(24, RayNeoIoProtocol.parseAlsReport(als)!!.size)
assertEquals(65, RayNeoIoProtocol.parseAlsReport(als)!![0].toInt())
assertNull(RayNeoIoProtocol.parseAlsReport(als.copyOf(24)))
assertNull(RayNeoIoProtocol.parseAlsReport(als.copyOf().also { it[3] = 2 }))
assertNull(RayNeoIoProtocol.parseAlsReport(als.copyOf().also { it[0] = 0x15 }))
assertEquals(1, RayNeoIoProtocol.parsePageState(hex("130100")))
val g = RayNeoIoProtocol.parseGesture(hex("0703010a000000"))!!
assertEquals(3, g.eventType)
assertEquals(1, g.source)
assertEquals(10L, g.tick)
}
@Test
fun recordsShareTheG2Encoder() {
// the G2 envelope is unchanged by the record/packet split
val g2 = CfwTransport.frame("123456789abcdef".encodeToByteArray(), 255, 3, 23)
assertContentEquals(hex("aa21ff0c0101f000830b0f0078c331323334862b"), g2[0])
// an iO record carries the same flags/length/CRC header
val t = CfwTransport(testPlatform())
val message = ByteArray(64) { (it % 7).toByte() }
val rec = t.encodeRecord(message, CfwTransport.BOTH)
assertEquals(CfwTransport.BOTH or CfwTransport.COMPRESSED or CfwTransport.RESET_CONTEXT, rec[0].toInt())
assertEquals(CfwTransport.crc(message), (rec[3].toInt() and 255) or ((rec[4].toInt() and 255) shl 8))
assertContentEquals(message, inflateRecords(listOf(rec))[0])
// the next record continues the same compression context
val second = t.encodeRecord(message, CfwTransport.BOTH)
assertEquals(CfwTransport.BOTH or CfwTransport.COMPRESSED, second[0].toInt())
t.close()
}
@Test
fun advertisement() {
assertEquals("00:6A:5E:5F:33:C2", RayNeoIoBle.macFromManufacturerData(hex("07006a5e5f33c2130000000000000001")))
assertNull(RayNeoIoBle.macFromManufacturerData(hex("0800")))
assertTrue(RayNeoIoBle.matchesName("RayNeo iO-2229"))
assertFalse(RayNeoIoBle.matchesName("Even G2_32_L_693CCB"))
}
private companion object {
const val GENERAL_STATUS = "080110011aa6037b2267656e6572616c537461747573223a7b226175746f6d617469634272696768746e657373223a66616c73652c2262617474657279223a39382c22626174745f74656d70223a33342c22626c7565746f6f746841646472657373223a2230303a36413a35453a35463a33333a4332222c226272696768746e657373223a312c226465766963654e616d65223a225261794e656f20694f2d32323239222c2269734368617267696e67223a747275652c2263686172676554797065223a302c2273637265656e537461747573223a312c2268616c6c537461747573223a302c226d6963537461747573223a302c22666f6375734d6f6465223a7b22656e61626c65223a302c22656e61626c65476c617373436c6f7365223a312c22706f6c696379223a7b226175746f223a312c22626567696e223a2230393a3030222c22656e64223a2231383a3030222c227765656b646179223a5b302c312c322c332c342c352c365d7d7d2c22707265736372697074696f6e223a7b22656e61626c65223a312c22646567726565223a222d332c302c2d322e37352c30222c227064223a223633227d7d7d"
}
}
@@ -0,0 +1,608 @@
package com.faceclaw.app
import kotlin.concurrent.Volatile
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue
private const val IO = "00:6A:5E:5F:33:C2"
private const val RING = "FF:AA:F1:F6:7E:28"
private val PHONE_ID = byteArrayOf(1, 2, 3, 4, 5, 6)
private class IoClock(private val base: ProtocolPlatform) : ProtocolPlatform by base {
@Volatile var offsetMs = 0L
override fun elapsedRealtimeMs(): Long = base.elapsedRealtimeMs() + offsetMs
}
private fun awaitTrue(timeoutMs: Long, condition: () -> Boolean): Boolean {
val platform = testPlatform()
val deadline = platform.elapsedRealtimeMs() + timeoutMs
while (platform.elapsedRealtimeMs() < deadline) {
if (condition()) return true
sleepMs(10)
}
return condition()
}
/**
* A RayNeo iO running faceclaw-rayneo-cfw, as the phone sees it over GATT: answers the
* handshake, guest auth, launcher status, OS version, QUERY and page controls, reassembles
* and inflates CFW records, and ACKs each lens of every record whose CRC checks out.
*/
private class FakeIoGlasses(platform: ProtocolPlatform) : SessionLink {
lateinit var core: GlassesSessionCore
private val lock = platform.createLock()
val glassesId = byteArrayOf(0x00, 0x6a, 0x5e, 0x5f, 0x33, 0xc2.toByte())
/** The phone id the glasses accept; null = pairing mode (they bind to the next one). */
@Volatile var boundTo: ByteArray? = null
@Volatile var faceclaw = true
/** Firmware with message 30 and the light sensor (IDENT's features string). */
@Volatile var brightnessFirmware = false
@Volatile var pageOpen = false
@Volatile var hangUps = 0
private val controlsList = ArrayList<Int>()
private val messagesList = ArrayList<ByteArray>()
private val launcherList = ArrayList<String>()
val launcher: List<String> get() = lock.withLock { launcherList.toList() }
/** Voice-channel (business 13) messages from the phone: type to JSON. */
private val voiceList = ArrayList<Pair<Int, String>>()
val voice: List<Pair<Int, String>> get() = lock.withLock { voiceList.toList() }
/** A voice-channel message from the glasses. */
fun voiceNotify(type: Int, json: String = "{}", binary: ByteArray? = null) =
notify(13, RayNeoIoProtocol.encodeEnvelope(RayNeoIoProtocol.Envelope(type, json, binary)))
private val records = ArrayList<ByteArray>()
private var current = ByteArray(0)
private val inbound = RayNeoIoProtocol.FrameReassembler()
private var number = 1
val controls: List<Int> get() = lock.withLock { controlsList.toList() }
val messages: List<ByteArray> get() = lock.withLock { messagesList.toList() }
/** Writes to the R1 on [RING] (characteristic, bytes), and the CCCDs the session enabled on it. */
private val ringWritesList = ArrayList<Pair<String, ByteArray>>()
private val ringCccdsList = ArrayList<String>()
val ringWrites: List<Pair<String, ByteArray>> get() = lock.withLock { ringWritesList.toList() }
val ringCccds: List<String> get() = lock.withLock { ringCccdsList.toList() }
/** A notification from the R1, as its link delivers it. */
fun ringNotify(characteristicUuid: String, data: ByteArray) = core.onNotification(RING, characteristicUuid, data)
fun notify(business: Int, payload: ByteArray) {
val frame = lock.withLock { RayNeoIoProtocol.encodeFrame(number++, business, payload) }
core.onNotification(IO, RayNeoIoBle.NOTIFY_CHAR_UUID, frame)
}
fun page(state: Int, reason: Int) = notify(0xf0, byteArrayOf(0x13, state.toByte(), reason.toByte()))
override fun connect(address: String, timeoutMs: Int): Boolean = true
override fun isConnected(address: String): Boolean = true
override fun requestHighPriority(address: String) {}
override fun requestMtu(address: String, mtu: Int, timeoutMs: Int): Boolean = true
override fun negotiatedMtu(address: String): Int = 185
override fun discoverServices(address: String, timeoutMs: Int): Boolean = true
override fun enableNotifications(address: String, characteristicUuid: String, enable: Boolean, timeoutMs: Int): Boolean {
if (address == RING) {
lock.withLock { ringCccdsList.add(characteristicUuid) }
return true
}
return characteristicUuid == RayNeoIoBle.NOTIFY_CHAR_UUID
}
override fun writeFrames(address: String, characteristicUuid: String, frames: List<ByteArray>, mode: GattWriteMode, timeoutMs: Int): Boolean {
if (address == RING) {
lock.withLock { for (frame in frames) ringWritesList.add(characteristicUuid to frame) }
return true
}
assertEquals(RayNeoIoBle.WRITE_CHAR_UUID, characteristicUuid)
assertTrue(frames.all { it.size <= 185 - 3 }, "writes must fit the MTU")
val decoded = lock.withLock { frames.flatMap { inbound.ingest(it) } }
for (frame in decoded) handle(frame)
return true
}
private fun handle(frame: RayNeoIoProtocol.Frame) {
val p = frame.payload
when (frame.business) {
0x10 -> {
val tlvs = RayNeoIoProtocol.decodeTlvs(p)!!
val version = tlvs.firstOrNull { it.tag == 0x11 }
val auth = tlvs.firstOrNull { it.tag == 0x18 }
if (version != null) {
val phone = RayNeoIoProtocol.decodeTlvs(version.value)!!.first { it.tag == 0x11 }.value
val bound = boundTo
if (bound != null && !bound.contentEquals(phone)) {
hangUps++
core.onConnectionStateChange(IO, false)
return
}
notify(0x10, RayNeoIoProtocol.tlv(0x11, RayNeoIoProtocol.tlv(0x10, byteArrayOf(2)) +
RayNeoIoProtocol.tlv(0x11, glassesId) + RayNeoIoProtocol.tlv(0x14, "006A5E5F33C2229".encodeToByteArray())))
} else if (auth != null) {
val fields = RayNeoIoProtocol.decodeTlvs(auth.value)!!
val phoneRandom = fields.first { it.tag == 0x10 }.value
val phone = PHONE_ID
assertTrue(RayNeoIoProtocol.authProof(phoneRandom, phone, glassesId).contentEquals(fields.first { it.tag == 0x11 }.value))
boundTo = phone
val random = byteArrayOf(9, 8, 7, 6)
notify(0x10, RayNeoIoProtocol.tlv(0x19, RayNeoIoProtocol.tlv(0x10, random) +
RayNeoIoProtocol.tlv(0x11, RayNeoIoProtocol.authProof(random, phone, glassesId))))
}
}
15 -> {
lock.withLock { launcherList.add(RayNeoIoProtocol.decodeEnvelope(p)!!.json) }
notify(15, RayNeoIoProtocol.encodeEnvelope(RayNeoIoProtocol.Envelope(1,
"""{"generalStatus":{"battery":77,"isCharging":false}}""", sequence = RayNeoIoProtocol.decodeEnvelope(p)!!.sequence, mode = 2)))
}
9 -> notify(9, RayNeoIoProtocol.encodeEnvelope(RayNeoIoProtocol.Envelope(1, """{"OsVersion":"Strix OS 1.0.4.12"}""")))
13 -> {
val envelope = RayNeoIoProtocol.decodeEnvelope(p)!!
lock.withLock { voiceList.add(envelope.type to envelope.json) }
}
0xf0 -> cfw(p)
}
}
private fun cfw(p: ByteArray) {
val opt = p[0].toInt() and 255
val stream = p[1].toInt() and 255
if (opt and RayNeoIoProtocol.OPT_CONTROL != 0) {
val ctrl = p[2].toInt() and 255
lock.withLock { controlsList.add(ctrl) }
when (ctrl) {
RayNeoIoProtocol.CTRL_QUERY -> if (faceclaw) notify(0xf0, ident())
RayNeoIoProtocol.CTRL_PAGE_OPEN, RayNeoIoProtocol.CTRL_KEEPALIVE -> { pageOpen = true; page(1, 0) }
RayNeoIoProtocol.CTRL_PAGE_CLOSE -> { pageOpen = false; page(0, 0) }
}
return
}
val complete = lock.withLock {
if (opt and RayNeoIoProtocol.OPT_RESET != 0) current = ByteArray(0)
current += p.copyOfRange(2, p.size)
val len = if (current.size >= 5) (current[1].toInt() and 255) or ((current[2].toInt() and 255) shl 8) else -1
if (len >= 0 && current.size == 5 + len) current.also { records.add(it); current = ByteArray(0) } else null
} ?: return
val message = lock.withLock { inflateRecords(records).last() }
val crc = (complete[3].toInt() and 255) or ((complete[4].toInt() and 255) shl 8)
val type = message[0].toInt() and 255
val ok = pageOpen && CfwTransport.crc(message) == crc &&
(ioSupportsCfwType(type) || (brightnessFirmware && (type == CFW_MSG_BRIGHTNESS || type == CFW_MSG_AMBIENT_LIGHT)))
if (ok) lock.withLock { messagesList.add(message) }
for (lens in 1..2) {
notify(0xf0, byteArrayOf(if (ok) 1 else 3, stream.toByte(), 0, 0, lens.toByte(),
message.size.toByte(), (message.size ushr 8).toByte(), crc.toByte(), (crc ushr 8).toByte()))
}
}
private fun ident(): ByteArray {
val name = "GLASSLYCFW-IO/13".encodeToByteArray()
return byteArrayOf(0x11, 13, 0x1c, 0x02, 0xdc.toByte(), 0x00, 0xf0.toByte(), 0x00, 0x00, 0x01, 0x0d, 0x00, 0xff.toByte()) +
name + byteArrayOf(0, 25) + "Faceclaw/38".encodeToByteArray() + byteArrayOf(0) +
(if (brightnessFirmware) "bright30 als16".encodeToByteArray() + byteArrayOf(0) else ByteArray(0))
}
/** RPL_ALS as the firmware sends it: a poll of [tenths] of a lux from a healthy sensor. */
fun alsPoll(tenths: Int) {
val report = ByteArray(25)
report[0] = 0x1f
report[1] = 65
report[2] = 76
report[3] = 1
report[4] = 2 // poll
report[5] = 0x0b // opened, passive, read ok
report[6] = 3
for (i in 0..3) report[7 + i] = (tenths ushr (8 * i)).toByte()
notify(0xf0, report)
}
override fun disconnect(address: String) {}
override fun close() {}
override fun isBonded(address: String): Boolean = false
override fun prepareBenchmarkLink(address: String, mode: Int) {}
override fun recordDisplayFrameSent() {}
}
private class IoListener : FaceclawBleCommunicatorListener {
val phases = ArrayList<String>()
@Volatile var lastStatus = ""
val events = ArrayList<String>()
val batteries = ArrayList<String>()
val ringBatteries = ArrayList<String>()
val firmware = ArrayList<String>()
override fun onStateChange(phase: String?, status: String?) {
phases.add(phase ?: "")
lastStatus = status ?: ""
}
override fun onRingEvent(kind: String?, containerName: String?, eventType: Int, eventSource: Int, systemExitReasonCode: Int, frameId: Int, ringTick: Long, ringType: Int, ringAux: Int, ringSpeed: Int) {
events.add("$kind:$eventType:$eventSource")
}
override fun onBatteryState(headsetBattery: Int, headsetCharging: Int, ringBattery: Int, ringCharging: Int) {
batteries.add("$headsetBattery/$headsetCharging")
if (ringBattery >= 0) ringBatteries.add("$ringBattery/$ringCharging")
}
override fun onSilentMode(silent: Boolean) {}
override fun onWearState(wearing: Boolean) {}
override fun onPhoneLockState(locked: Boolean) {}
override fun onCaseState(caseBattery: Int, caseCharging: Int, caseDocked: Boolean) {}
override fun onEvenAppConflict(message: String?) {}
override fun onFrameMetrics(paintMs: Int, transmitMs: Int, tileCount: Int) {}
override fun onFrameFinished(frameId: Int, outcome: String?) {}
override fun onFirmwareInfo(leftVersion: String?, rightVersion: String?, extension: String?) {
firmware.add("$leftVersion|$extension")
}
}
/** An iO session; with [ring], also the phone's own link to an R1 in the glasses role (Direct). */
private class IoSession(ring: String = "") {
val platform = IoClock(testPlatform())
val glasses = FakeIoGlasses(platform)
private val host = object : SessionHost {
private val lock = platform.createLock()
private val lines = ArrayList<String>()
private var done: Latch? = null
fun logs(): List<String> = lock.withLock { lines.toList() }
override fun postToMain(action: () -> Unit) = action()
override fun currentThreadDispatcher(): SessionDispatcher = SessionDispatcher { it() }
override fun isPhoneLocked(): Boolean = false
override fun setScreenWakeLock(on: Boolean) {}
override fun isEvenAppActive(): Boolean = false
override fun startWorker(body: () -> Unit) {
val latch = Latch(1, platform)
done = latch
startThread("io-test-session", true) {
try {
body()
} finally {
latch.countDown()
}
}
}
override fun joinWorker(timeoutMs: Long) {
done?.await(timeoutMs)
}
override fun log(level: SessionLogLevel, tag: String, message: String, error: Throwable?) {
lock.withLock { lines.add("$level $tag: $message") }
}
}
val listener = IoListener()
val core = GlassesSessionCore(glasses, host, FrameTimingsCore(platform), platform, IO, IO, ring)
init {
glasses.core = core
core.setListener(listener)
core.setRequiredFirmwareRevision(38)
core.useRayNeoIo(PHONE_ID, "Pixel", "Google", "Pixel 11")
if (ring.isNotEmpty()) core.setDirectRingRole("glasses")
}
fun pageUp(): Boolean = core.monitor.withLock { core.sessionReady && core.fixedLayoutCreated }
fun displayedMatchesDesired(): Boolean = core.monitor.withLock {
core.desiredTilesLock.locked { core.desiredFingerprint.isNotEmpty() && core.desiredFingerprint == core.displayedFingerprint }
}
fun submitFrame(value: Byte, frameId: Int) {
val pixels = ByteArray(64 * 32) { if (it % 7 == 0) value else 0 }
core.submitSurfaceFrame(ArrayByteReader(pixels), "s", 0, 0, 64, 32, "content-$value", 0, frameId, null)
}
fun tail(): String = host.logs().takeLast(25).joinToString("\n")
}
class RayNeoIoSessionTest {
@Test
fun connectsAuthenticatesOpensThePageAndPresentsFrames() {
val s = IoSession()
s.core.configureBrightness(false, 70, 2, 100, "0:0,1000:100", 280)
s.core.configureCompositorScreen(540, 220)
s.core.configureSurface("s", 0, 0, 64, 32, 0, SurfaceCompositor.TRANSPARENCY_OPAQUE)
try {
assertTrue(s.core.start())
assertTrue(awaitTrue(5_000) { s.pageUp() }, "page never opened:\n" + s.tail())
assertTrue(s.core.isRayNeoIo())
assertTrue(s.listener.firmware.contains("Strix OS 1.0.4.12|Faceclaw/38"), s.listener.firmware.toString())
assertTrue(awaitTrue(2_000) { s.listener.batteries.contains("77/0") }, s.listener.batteries.toString())
assertEquals(listOf(RayNeoIoProtocol.CTRL_QUERY, RayNeoIoProtocol.CTRL_PAGE_OPEN), s.glasses.controls.take(2))
s.submitFrame(0x7f, 1)
assertTrue(awaitTrue(5_000) { s.displayedMatchesDesired() }, "frame never displayed:\n" + s.tail())
val types = s.glasses.messages.map { it[0].toInt() and 255 }
assertTrue(types.contains(CFW_MSG_PRESENT), types.toString())
// nothing G2-only (brightness 30, ENTER_EVENHUB 31, debug flags 7, ...) reaches the iO
assertTrue(types.all { ioSupportsCfwType(it) }, types.toString())
// a visible frame wakes the screen through the launcher's brightness (70% -> level 6)
assertTrue(awaitTrue(3_000) { s.glasses.launcher.any { it.contains("brightness_change") && it.contains("\"value\":6") } },
s.glasses.launcher.toString())
// a second, different frame goes out as an update
val before = s.glasses.messages.size
s.submitFrame(0x30, 2)
assertTrue(awaitTrue(5_000) { s.displayedMatchesDesired() && s.glasses.messages.size > before })
} finally {
s.core.close()
}
// disconnect closes the page so the glasses go back to their own UI at once
assertEquals(RayNeoIoProtocol.CTRL_PAGE_CLOSE, s.glasses.controls.last())
}
@Test
fun firmwareWithBrightnessControlGetsTheG2Messages() {
val s = IoSession()
s.glasses.brightnessFirmware = true
s.core.configureBrightness(false, 70, 2, 100, "0:0,1000:100", 280)
s.core.configureCompositorScreen(540, 220)
s.core.configureSurface("s", 0, 0, 64, 32, 0, SurfaceCompositor.TRANSPARENCY_OPAQUE)
val reportLock = s.platform.createLock()
val reports = ArrayList<ByteArray>()
s.core.addAmbientLightListener(object : FaceclawAmbientLightListener {
override fun onAmbientLight(body: ByteArray?) { reportLock.withLock { reports.add(body!!) } }
})
fun ofType(type: Int) = s.glasses.messages.filter { (it[0].toInt() and 255) == type }
try {
assertTrue(s.core.start())
assertTrue(awaitTrue(5_000) { s.pageUp() }, "page never opened:\n" + s.tail())
s.submitFrame(0x7f, 1)
assertTrue(awaitTrue(5_000) { s.displayedMatchesDesired() }, "frame never displayed:\n" + s.tail())
// visible at 70%, fading over the configured 280 ms, ahead of the frame it fades in
val wake = byteArrayOf(30, 1, 70, 1, 0x18, 0x01)
assertTrue(awaitTrue(3_000) { ofType(CFW_MSG_BRIGHTNESS).isNotEmpty() }, s.tail())
assertTrue(ofType(CFW_MSG_BRIGHTNESS).first().contentEquals(wake))
val order = s.glasses.messages.map { it[0].toInt() and 255 }
assertTrue(order.indexOf(CFW_MSG_BRIGHTNESS) < order.indexOf(CFW_MSG_PRESENT), order.toString())
// the launcher's command writes preferences and loses to stock auto: not used
assertTrue(s.glasses.launcher.none { "brightness_change" in it }, s.glasses.launcher.toString())
// the light sensor, at the manual rate; the iO needs telling only once
assertTrue(awaitTrue(3_000) { ofType(CFW_MSG_AMBIENT_LIGHT).isNotEmpty() }, s.tail())
val poll = ofType(CFW_MSG_AMBIENT_LIGHT).first()
assertEquals(listOf(16, 1), poll.take(2).map { it.toInt() })
assertEquals(5000, (poll[3].toInt() and 255) or ((poll[4].toInt() and 255) shl 8))
// each 5 s the same command, as the screen keepalive
val sent = ofType(CFW_MSG_BRIGHTNESS).size
s.platform.offsetMs += IoTimings.SCREEN_REFRESH_MS + 1
assertTrue(awaitTrue(3_000) { ofType(CFW_MSG_BRIGHTNESS).size > sent }, s.tail())
assertTrue(ofType(CFW_MSG_BRIGHTNESS).last().let { it[2].toInt() == 70 && it[3].toInt() == 1 })
assertEquals(1, ofType(CFW_MSG_AMBIENT_LIGHT).size)
// Auto: reports reach the policy and listeners, the sample rate follows, a target goes out
s.core.configureBrightness(true, 70, 20, 100, "0:0,100:100", 280)
assertTrue(awaitTrue(3_000) { ofType(CFW_MSG_AMBIENT_LIGHT).size == 2 }, s.tail())
assertEquals(500, ofType(CFW_MSG_AMBIENT_LIGHT).last().let { (it[3].toInt() and 255) or ((it[4].toInt() and 255) shl 8) })
s.glasses.alsPoll(1000) // 100 lux, the curve's top
assertTrue(awaitTrue(3_000) { ofType(CFW_MSG_BRIGHTNESS).last()[2].toInt() == 100 }, s.tail())
assertTrue(awaitTrue(1_000) { reportLock.withLock { reports.isNotEmpty() } })
assertEquals(65, reportLock.withLock { reports.first()[0].toInt() })
// A request dropped with the queue before it was written goes out again
s.core.monitor.withLock { s.core.clearAllMessagesLocked("test") }
val queued = ofType(CFW_MSG_AMBIENT_LIGHT).size
s.glasses.alsPoll(1000)
assertTrue(awaitTrue(3_000) { ofType(CFW_MSG_AMBIENT_LIGHT).size > queued }, s.tail())
// A reopened page starts over: the firmware gave brightness back when it closed
val before = ofType(CFW_MSG_AMBIENT_LIGHT).size
s.glasses.pageOpen = false
s.glasses.page(0, 3)
assertTrue(awaitTrue(5_000) { s.glasses.pageOpen && s.pageUp() }, s.tail())
s.submitFrame(0x30, 2)
assertTrue(awaitTrue(5_000) { ofType(CFW_MSG_AMBIENT_LIGHT).size > before }, s.tail())
} finally {
s.core.close()
}
}
@Test
fun refusalAsksForPairingModeAndConnectsOnceItIsOn() {
val s = IoSession()
s.glasses.boundTo = byteArrayOf(9, 9, 9, 9, 9, 9)
try {
assertTrue(s.core.start())
assertTrue(awaitTrue(5_000) { s.glasses.hangUps >= 1 && s.listener.lastStatus.contains("pairing mode") },
"no pairing-mode prompt:\n" + s.tail())
assertFalse(s.pageUp())
s.glasses.boundTo = null // the wearer held the button
s.platform.offsetMs += 5_000
s.core.interruptibleSleep.interrupt()
assertTrue(awaitTrue(5_000) { s.pageUp() }, "never connected after pairing mode:\n" + s.tail())
assertTrue(s.glasses.boundTo!!.contentEquals(PHONE_ID))
} finally {
s.core.close()
}
}
@Test
fun stockFirmwareIsIncompatible() {
val s = IoSession()
s.glasses.faceclaw = false
try {
assertTrue(s.core.start())
assertTrue(awaitTrue(8_000) { s.listener.phases.contains("incompatible-firmware") }, "not halted:\n" + s.tail())
assertTrue(s.listener.firmware.contains("Strix OS 1.0.4.12|"))
assertFalse(s.glasses.controls.contains(RayNeoIoProtocol.CTRL_PAGE_OPEN))
} finally {
s.core.close()
}
}
@Test
fun gesturesPageLossAndKeepalive() {
val s = IoSession()
s.core.configureCompositorScreen(540, 220)
try {
assertTrue(s.core.start())
assertTrue(awaitTrue(5_000) { s.pageUp() }, s.tail())
// taps/crown arrive as G2 sys-events from the right temple; head up wakes; head down is dropped
s.glasses.notify(0xf0, byteArrayOf(7, 3, 1, 0, 0, 0, 0))
s.glasses.notify(0xf0, byteArrayOf(7, 1, 0, 0, 0, 0, 0))
s.glasses.notify(0xf0, byteArrayOf(7, 12, 2, 0, 0, 0, 0))
s.glasses.notify(0xf0, byteArrayOf(7, 15, 2, 0, 0, 0, 0))
assertEquals(listOf("sys-event:3:1", "sys-event:1:1", "display-wake:12:1"), s.listener.events)
// the lease ran out on the glasses: the session reopens the page
s.glasses.pageOpen = false
s.glasses.page(0, 3)
assertFalse(s.pageUp())
s.platform.offsetMs += 4_000
s.core.interruptibleSleep.interrupt()
assertTrue(awaitTrue(5_000) { s.pageUp() }, "page not reopened:\n" + s.tail())
// keepalives well inside the 120 s lease
val keepalives = s.glasses.controls.count { it == RayNeoIoProtocol.CTRL_KEEPALIVE }
s.platform.offsetMs += 31_000
s.core.interruptibleSleep.interrupt()
assertTrue(awaitTrue(5_000) { s.glasses.controls.count { it == RayNeoIoProtocol.CTRL_KEEPALIVE } > keepalives })
// the wearer closed it: stay closed until they touch the glasses again
s.glasses.pageOpen = false
s.glasses.page(0, 4)
s.platform.offsetMs += 4_000
s.core.interruptibleSleep.interrupt()
sleepMs(300)
assertFalse(s.pageUp())
s.glasses.notify(0xf0, byteArrayOf(7, 0, 1, 0, 0, 0, 0))
s.core.interruptibleSleep.interrupt()
assertTrue(awaitTrue(5_000) { s.pageUp() }, "touch did not reopen:\n" + s.tail())
} finally {
s.core.close()
}
}
@Test
fun anR1BesideTheIoIsTheSessionsOwnRingInput() {
val s = IoSession(RING)
s.core.configureCompositorScreen(540, 220)
try {
assertTrue(s.core.start())
assertTrue(awaitTrue(5_000) { s.pageUp() }, s.tail())
// The phone takes the ring's glasses role itself: legacy CCCD, the glasses
// pair-auth, then the touch source on, all on the ring's link.
assertTrue(awaitTrue(5_000) { s.glasses.ringWrites.size >= 2 }, "ring setup not sent:\n" + s.tail())
assertTrue(BleProtocol.R1_LEGACY_NOTIFY_CHAR_UUID in s.glasses.ringCccds)
val (authChar, auth) = s.glasses.ringWrites[0]
assertEquals(BleProtocol.R1_LEGACY_WRITE_CHAR_UUID, authChar)
assertTrue(auth.contentEquals(RingProtocol.LEGACY_GLASSES_PAIR_AUTH))
// Its battery and gestures reach the listener as they do beside a G2,
// not the iO's protocol parser.
s.glasses.ringNotify(BleProtocol.R1_LEGACY_NOTIFY_CHAR_UUID, byteArrayOf(0x00, 0x09, 0x8b.toByte(), 0x00, 64, 1))
assertTrue(awaitTrue(2_000) { "64/1" in s.listener.ringBatteries }, s.listener.ringBatteries.toString())
val swipe = byteArrayOf(0x00, 0x09, 0x61, 0x00, 0x04, 0x35, 0x00, 0x09, 0xcc.toByte(), 0x00, 0x00)
s.glasses.ringNotify(BleProtocol.R1_LEGACY_NOTIFY_CHAR_UUID, swipe)
assertTrue(awaitTrue(2_000) { s.listener.events.isNotEmpty() }, s.tail())
val expected = FaceclawRingEventDecoder.decode(swipe)!!.event
assertEquals("${expected.kind}:${expected.eventType}:${expected.eventSource}", s.listener.events.single())
// The wearer closed the page: a ring gesture takes it back, as a temple touch does.
s.glasses.pageOpen = false
s.glasses.page(0, 4)
s.platform.offsetMs += 4_000
s.core.interruptibleSleep.interrupt()
sleepMs(300)
assertFalse(s.pageUp())
s.glasses.ringNotify(BleProtocol.R1_LEGACY_NOTIFY_CHAR_UUID, byteArrayOf(0x00, 0x09, 0x61, 0x00, 0x01, 0x00, 0x00, 0x10, 0xcc.toByte(), 0x00, 0x00))
s.core.interruptibleSleep.interrupt()
assertTrue(awaitTrue(5_000) { s.pageUp() }, "ring input did not reopen:\n" + s.tail())
} finally {
s.core.close()
}
}
@Test
fun theWakeWordAndTheMicStream() {
val s = IoSession()
s.core.configureCompositorScreen(540, 220)
val lock = s.platform.createLock()
val packets = ArrayList<ByteArray>()
val listener = object : FaceclawAudioPacketListener {
override fun onAudioPacket(data: ByteArray?, arm: String?, arrivalMs: Long) {
lock.withLock { packets.add(data!!) }
}
}
val count = { lock.withLock { packets.size } }
fun sent(type: Int, json: String) = s.glasses.voice.count { it == type to json }
val start = """{"rc":1}"""
val stop = """{"rc":2}"""
try {
assertTrue(s.core.start())
assertTrue(awaitTrue(5_000) { s.pageUp() }, s.tail())
// "Hey RayNeo" goes to Faceclaw: the launcher's switch on, then each wake ends the
// stock session (declined, then aborted) and arrives as the G2's EVEN_AI_WAKE_UP.
s.core.setVoiceWakeEnabled(true)
// The firmware reads the switch from "mode", not "value".
assertTrue(awaitTrue(3_000) { s.glasses.launcher.any { "set_ai_voice_wakeup" in it && "\"mode\":1" in it } }, s.tail())
s.glasses.voiceNotify(RayNeoIoProtocol.VOICE_WAKE, """{"rc":2,"mode":2,"linkType":0}""")
assertTrue(awaitTrue(2_000) { "even-ai:${BleProtocol.EVEN_AI_STATUS_WAKE_UP}:0" in s.listener.events }, s.listener.events.toString())
// The same phrase's second-stage wake is not a second wake.
s.glasses.voiceNotify(RayNeoIoProtocol.VOICE_WAKE, """{"rc":2,"mode":3,"linkType":0}""")
sleepMs(200)
assertEquals(1, s.listener.events.count { it.startsWith("even-ai:") })
assertTrue(awaitTrue(2_000) {
sent(RayNeoIoProtocol.VOICE_RC, stop) >= 1 && sent(RayNeoIoProtocol.VOICE_ABORT, start) >= 1
}, s.glasses.voice.toString())
// The mic: capture opens the remote stream, its Opus packets go to the listener as
// they came, and stopping closes it.
assertTrue(s.core.startG2AudioCapture(listener))
assertTrue(awaitTrue(2_000) { sent(RayNeoIoProtocol.VOICE_STREAM_CONTROL, start) == 1 }, s.tail())
s.glasses.voiceNotify(RayNeoIoProtocol.VOICE_STREAM_AUDIO, binary = byteArrayOf(0x78, 1, 2, 3))
assertTrue(awaitTrue(2_000) { count() == 1 })
assertTrue(lock.withLock { packets[0] }.contentEquals(byteArrayOf(0x78, 1, 2, 3)))
assertTrue(s.core.isAudioCaptureActive())
// A voice session that took the recorder ended: the stream is asked for again.
s.glasses.voiceNotify(RayNeoIoProtocol.VOICE_EXITED)
assertTrue(awaitTrue(2_000) { sent(RayNeoIoProtocol.VOICE_STREAM_CONTROL, start) == 2 }, s.tail())
s.core.stopG2AudioCapture()
assertTrue(awaitTrue(2_000) { sent(RayNeoIoProtocol.VOICE_STREAM_CONTROL, stop) == 1 }, s.tail())
assertFalse(s.core.isAudioCaptureActive())
s.glasses.voiceNotify(RayNeoIoProtocol.VOICE_STREAM_AUDIO, binary = byteArrayOf(9))
sleepMs(200)
assertEquals(1, count())
// With the wake action off: the switch follows, and a wake is ended but not reported.
s.core.setVoiceWakeEnabled(false)
assertTrue(awaitTrue(3_000) { s.glasses.launcher.any { "set_ai_voice_wakeup" in it && "\"mode\":0" in it } }, s.tail())
val events = s.listener.events.size
val aborts = sent(RayNeoIoProtocol.VOICE_ABORT, start)
s.platform.offsetMs += 5_000
s.glasses.voiceNotify(RayNeoIoProtocol.VOICE_WAKE)
assertTrue(awaitTrue(2_000) { sent(RayNeoIoProtocol.VOICE_ABORT, start) > aborts }, s.glasses.voice.toString())
assertEquals(events, s.listener.events.size)
} finally {
s.core.close()
}
}
}
@@ -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). */
@@ -63,7 +65,6 @@ class VoiceCaptureSessionTest {
override val dispatcher = CallbackDispatcher { it() }
var packetListener: FaceclawAudioPacketListener? = null
var g2Stops = 0
val recordings = mutableListOf<ByteArray>()
override fun onSessionStarting() {}
override fun isG2SessionReady(): Boolean = g2Ready
override fun isG2AudioCaptureActive(): Boolean = packetListener != null
@@ -96,7 +97,6 @@ class VoiceCaptureSessionTest {
override fun embed(pcm16le: ByteArray, sampleRate: Int): FloatArray? = embedding
override fun release() {}
}
override fun recordingSink(): RecordingSink = RecordingSink { bytes, _ -> recordings.add(bytes) }
}
private val logs = mutableListOf<String>()
@@ -196,7 +196,6 @@ class VoiceCaptureSessionTest {
val session = session(host, platform, "voice-test-onboard")
session.setListener(events)
session.setUsePhoneMic(true)
session.setSaveRecordings(true)
session.start("onboard", 1)
assertTrue(events.stoppedLatch.await(10000))
assertTrue(mic.closed)
@@ -211,8 +210,6 @@ class VoiceCaptureSessionTest {
// Every segment handed to the model is at most 8 s and was peak-normalized.
assertTrue(transcriber.calls.all { it in 1..VoiceCaptureSession.TRANSCRIPT_SEGMENT_MAX_SAMPLES })
assertTrue(logs.any { it.startsWith("I Moonshine decode final=true") }, logs.toString())
assertEquals(1, host.recordings.size)
assertEquals(170 * 800 * 2, host.recordings[0].size)
assertEquals(170, events.pcmChunks.size)
assertEquals("x x", VoiceCaptureSession.joinTranscript("x", "x"))
assertEquals("x, y", VoiceCaptureSession.joinTranscript("x", ", y"))
@@ -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))
}
}
+55 -11
View File
@@ -7,7 +7,7 @@ const { pcmLevel } = loader()('app/util/audio-level.ts');
function harness() {
const levelListeners = new Set();
let tick = null;
const { MicLevelMeter, paintListeningHeader } = loader({
const { MicLevelMeter, paintListeningHeader, RECORDING_ICON, withRecordingIcon } = loader({
setInterval(fn) { tick = fn; return 1; },
clearInterval() { tick = null; },
}, {
@@ -16,14 +16,15 @@ function harness() {
} },
})('app/ui/listening-header.ts');
const level = value => { for (const listener of levelListeners) listener(value); };
return { MicLevelMeter, paintListeningHeader, level, tick: () => tick?.(), ticking: () => tick !== null, levelListeners };
return { MicLevelMeter, paintListeningHeader, RECORDING_ICON, withRecordingIcon, level, tick: () => tick?.(), ticking: () => tick !== null, levelListeners };
}
/** Records draws; text is 6px per character, like a fixed-width bitmap face. */
function fakeImage(width = 576) {
const texts = [], rects = [];
return { width, texts, rects,
const texts = [], rects = [], images = [];
return { width, texts, rects, images,
drawText(_font, x, y, text, gray) { texts.push({ x, y, text, gray }); },
drawImage(source, x, y) { images.push({ source, x, y }); },
fillRect(x, y, w, h, gray) { rects.push({ x, y, w, h, gray }); } };
}
const font = { ascent: 12, lineHeight: 16, measureText: text => text.length * 6 };
@@ -81,20 +82,63 @@ test('the header puts title and status on one line, truncated before the meter',
const h = harness();
const meter = new h.MicLevelMeter();
const image = fakeImage(300);
const bodyTop = h.paintListeningHeader(image, font, 'Translate', 'Listening (ElevenLabs)...', meter);
assert.equal(bodyTop, 20 + 18 + 2);
assert.deepEqual(image.texts.map(({ x, y, gray }) => [x, y, gray]), [[24, 20, 200], [24 + 54, 20, 110]]);
// Inside the padding suggested for the window's edges: here a border at
// the top and left, blank space at the right.
const padding = { top: 8, right: 0, bottom: 8, left: 8 };
const bodyTop = h.paintListeningHeader(image, font, 'Translate', 'Listening (ElevenLabs)...', meter, padding);
assert.equal(bodyTop, 8 + 18 + 2);
assert.deepEqual(image.texts.map(({ x, y, gray }) => [x, y, gray]), [[8, 8, 200], [8 + 54, 8, 110]]);
assert.equal(image.texts[0].text, 'Translate');
const status = image.texts[1].text;
assert.ok(status.startsWith(' · Listening') && status.endsWith('...'), status);
const meterX = Math.min(...image.rects.map(rect => rect.x));
assert.equal(meterX, 300 - 24 - meter.width);
assert.ok(24 + 54 + status.length * 6 <= meterX - 10, 'status stops short of the meter');
assert.equal(meterX, 300 - meter.width, 'the meter ends at the unpadded right edge');
assert.ok(8 + 54 + status.length * 6 <= meterX - 10, 'status stops short of the meter');
// Segments sit on the text baseline.
assert.ok(image.rects.every(rect => rect.y + rect.h === 20 + font.ascent));
assert.ok(image.rects.every(rect => rect.y + rect.h === 8 + font.ascent));
assert.equal(image.images.length, 0, 'no recording circle unless the audio is being saved');
const idle = fakeImage(300);
h.paintListeningHeader(idle, font, 'Transcribe', 'Starting microphone...', null);
h.paintListeningHeader(idle, font, 'Transcribe', 'Starting microphone...', null, padding);
assert.equal(idle.rects.length, 0);
assert.equal(idle.texts[1].text, ' · Starting microphone...');
});
test('while the audio is being saved, the recording circle sits beside the meter', () => {
const h = harness();
const meter = new h.MicLevelMeter();
const image = fakeImage(300);
const padding = { top: 8, right: 0, bottom: 8, left: 8 };
h.paintListeningHeader(image, font, 'Translate', 'Listening (ElevenLabs)...', meter, padding, true);
const meterX = Math.min(...image.rects.map(rect => rect.x));
assert.equal(meterX, 300 - meter.width, 'the meter stays where it was');
assert.equal(image.images.length, 1);
const [circle] = image.images;
assert.equal(circle.source, h.RECORDING_ICON);
assert.equal(circle.x + circle.source.width + 4, meterX);
// Centred on the meter, whose 7 rows sit on the text baseline.
const meterTop = Math.min(...image.rects.map(rect => rect.y));
assert.equal(circle.y + circle.source.height / 2, meterTop + 7 / 2);
const status = image.texts[1].text;
assert.ok(8 + 54 + status.length * 6 <= circle.x - 10, 'status stops short of the circle');
// The mic isn't live (starting, or waiting for the glasses): nothing is being saved to mark.
const idle = fakeImage(300);
h.paintListeningHeader(idle, font, 'Transcribe', 'Starting microphone...', null, padding, true);
assert.equal(idle.images.length, 0);
});
test('a tray icon gains the recording circle to its right, vertically centred', () => {
const h = harness();
const { GrayImage } = loader()('app/graphics/image.ts');
const mic = new GrayImage(14, 15, 0);
mic.setPixel(0, 0, 200); mic.setPixel(13, 14, 200);
const icon = h.withRecordingIcon(mic);
assert.deepEqual([icon.width, icon.height], [14 + 2 + 9, 15]);
assert.deepEqual([icon.getPixel(0, 0), icon.getPixel(13, 14)], [200, 200]);
assert.deepEqual([icon.getPixel(14, 7), icon.getPixel(15, 7)], [0, 0], 'a gap separates them');
// The circle's widest row is the icon's middle row; its corners are clear.
for (let x = 16; x < 25; x++) assert.equal(icon.getPixel(x, 7), 220);
assert.deepEqual([icon.getPixel(16, 3), icon.getPixel(20, 3), icon.getPixel(20, 2), icon.getPixel(20, 12)], [0, 220, 0, 0]);
});
+29
View File
@@ -0,0 +1,29 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { loader } = require('./helpers/load-typescript.cjs');
const graphics = require('../.test-build/app/graphics/image.js');
// lock-screen.ts on a screen of the given size; the text is not drawn.
function lockScreen(width, height) {
const font = { lineHeight: 20, measureText: (text) => text.length * 10, drawText() {} };
const load = loader({}, {
'../graphics/image': graphics,
'../graphics/screen-size': { screenWidth: () => width, screenHeight: () => height },
'../graphics/ui-fonts': { getDefaultMediumFont: () => font },
'../graphics/textwrap': { wrapText: (_font, text) => [text] },
});
return load('app/g2/lock-screen.ts').createLockScreenImage();
}
// The lock surface is the screen's size, so its frame must be too.
test('the lock screen fills the paired glasses\' screen, its box centred on it', () => {
for (const [width, height] of [[640, 480], [540, 220]]) {
const image = lockScreen(width, height);
assert.deepEqual([image.width, image.height], [width, height]);
const lit = [];
for (let y = 0; y < height; y++) for (let x = 0; x < width; x++) if (image.pixels[y * width + x]) lit.push([x, y]);
const xs = lit.map(([x]) => x), ys = lit.map(([, y]) => y);
const box = [Math.min(...xs), Math.min(...ys), Math.max(...xs), Math.max(...ys)];
assert.deepEqual(box, [(width - 480) / 2, (height - 150) / 2, (width + 480) / 2 - 1, (height + 150) / 2 - 1], `${width}x${height}`);
}
});
+35
View File
@@ -0,0 +1,35 @@
// EvenHub text-container wrapping (app/graphics/lvgl-wrap.ts), against what
// stock firmware 2.2.9.22 drew in the g2-firmware-emulator: spaces are kept
// everywhere, including ones that overflow onto a soft-wrapped line.
const test = require("node:test");
const assert = require("node:assert/strict");
const { wrapLvglText } = require("../.test-build/app/graphics/lvgl-wrap.js");
// The stock 20px font's advances for these: H 12px, E 11px, space 5px.
const ADVANCES = { H: 12, E: 11, " ": 5 };
const advance = (cp) => ADVANCES[String.fromCodePoint(cp)] ?? 10;
test("indentation after a newline and at the start is kept (Iris Pro's icon-clearing indent)", () => {
assert.deepEqual(wrapLvglText(" \n R E C O R D", 248, advance), [" ", " R E C O R D"]);
assert.deepEqual(wrapLvglText("AB\n CD", 248, advance), ["AB", " CD"]);
assert.deepEqual(wrapLvglText(" EF", 248, advance), [" EF"]);
});
test("spaces that overflow a line start the next one (emulator Q3)", () => {
const text = "HHHHHHHH EEEE";
assert.deepEqual(wrapLvglText(text, 100, advance), ["HHHHHHHH", " EEEE"]);
assert.deepEqual(wrapLvglText(text, 110, advance), ["HHHHHHHH ", " EEEE"]);
assert.deepEqual(wrapLvglText(text, 120, advance), ["HHHHHHHH ", "EEEE"]);
});
test("a word that overflows wraps after the last space or hyphen", () => {
assert.deepEqual(wrapLvglText("HH EEEEEEEE", 100, advance), ["HH ", "EEEEEEEE"]);
assert.deepEqual(wrapLvglText("HH-EEEEEEEE", 100, advance), ["HH-", "EEEEEEEE"]);
});
test("a run with no break opportunity breaks before the overflowing character", () => {
assert.deepEqual(wrapLvglText("HHHHHHHHHH", 50, advance), ["HHHH", "HHHH", "HH"]);
// A character wider than the line still gets one to itself.
assert.deepEqual(wrapLvglText("HH", 5, advance), ["H", "H"]);
});
+34
View File
@@ -75,6 +75,40 @@ test('scrolling keeps the selection and its neighbors visible and aligns the vie
assert.equal(menu.scrollTop, 0);
});
test('padTop and padBottom are blank rows inside the scrolled content, shown only at its ends', async () => {
// A box that runs to a window's edges: 8px of padding scrolls with the rows.
const box = { x: 10, y: 5, width: 80, height: 60, padTop: 8, padBottom: 8 };
const { menu, paint, image } = stringMenu();
const step = async (type) => { await menu.handleInput({ type }); paint(true, box); return menu.scrollTop; };
const rows = paint(true, box);
assert.equal(menu.scrollTop, 0);
assert.equal(rows[1].y, 5 + 8 + 20, 'rows start below the padding');
assert.equal(menu.indexAt(5 + 4), null, 'the padding is no row');
assert.equal(menu.indexAt(5 + 8), 0);
assert.equal(menu.overflows, true);
// Rows scrolled off are cut at the box's edge: the viewport rests on a row
// top, without the padding, until the first row comes back.
assert.equal(await step('scroll-down'), 0);
assert.equal(await step('scroll-down'), 28, 'b sits right at the top edge');
assert.equal(menu.indexAt(5), 1);
assert.equal(await step('scroll-down'), 28);
assert.equal(await step('scroll-down'), 68, 'the last row shows with the padding under it');
assert.equal(menu.selectedIndex, 4);
assert.equal(await step('scroll-up'), 68);
assert.equal(await step('scroll-up'), 28);
assert.equal(await step('scroll-up'), 28);
assert.equal(await step('scroll-up'), 0, 'back at the first row, its padding shows again');
// Content that fits just sits below the top padding.
const short = stringMenu({ items: ['a', 'b'] });
const drawn = short.paint(true, box);
assert.equal(short.menu.overflows, false);
assert.equal(drawn[1].y, 5 + 8 + 20);
// A scrollbar spans the padded content.
const before = image.pixels.slice();
menu.drawScrollbar(image, 95, 5, 60);
assert.ok(image.pixels.some((value, index) => value !== before[index]));
});
test('without wrap the ends call the exit callbacks and hold the selection', async () => {
const { menu, exits } = stringMenu();
await menu.handleInput({ type: 'scroll-up' });
+38 -4
View File
@@ -18,7 +18,7 @@ const menuCore=load('app/ui/menu-core.ts',{
'../graphics/image':graphics,'./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',{
'./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 },
'./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':{},
'./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'),
@@ -50,7 +50,8 @@ test('later opaque planes add an occlusion to a replayed selection',()=>{
});
const {LayerStack,noopLayerActions}=load('app/ui/layers.ts',{
'../graphics/image':graphics,'../graphics/plane':planes,'../native/frame-timings':{spanCurrent:(_name,fn)=>fn()},'./gestures':{},
'./viewport':require('../.test-build/app/ui/viewport.js'),
'../graphics/image':graphics,'../graphics/plane':planes,'../native/frame-timings':{spanCurrent:(_name,fn)=>fn()},'../graphics/screen-size':{screenWidth:()=>640,screenHeight:()=>480},'./gestures':{},
});
test('context menu surface and selected row both use +4 without changing the app screen',()=>{
const base=new graphics.GrayImage(100,90,96);
@@ -104,7 +105,7 @@ test('unselected sidebar icons retain -2 depth through shell cropping, including
'../../native/notification-icons':{},'../../native/notification-types':require('../.test-build/app/native/notification-types.js'),'../../native/phone-battery':{},'../../util/render-freshness':{},
'../../graphics/icons':{},'../dashboard-settings':{},'../clock-format':{},'../../graphics/bdffont':{},
'../layers':{LayerStack},'../menu':{scrollToKeepSelectionVisible:()=>0},'../metrics':{},
'./geometry':{MIN_WINDOW_HEIGHT:280,minWindowTop:()=>0,windowTop:()=>0,SHELL_OPAQUE_BLACK:1,SIDEBAR_WIDTH:64,TOP_BAR_HEIGHT:24,
'./geometry':{MIN_WINDOW_HEIGHT:280,minWindowHeight:()=>280,screenWidth:()=>640,screenHeight:()=>480,minWindowTop:()=>0,windowTop:()=>0,SHELL_OPAQUE_BLACK:1,SIDEBAR_WIDTH:64,TOP_BAR_HEIGHT:24,
SWITCHER_ROW_HEIGHT:36,switcherPosition:()=>'left',switcherRect:()=>({x:0,y:0,width:64,height:280})},
});
const state={selectedIndex:0,focus:'sidebar',windows:[0,1,2].map(n=>({attention:n===1,drawIcon:(image,x,y)=>image.fillRect(x,y,4,4,240)}))};
@@ -121,8 +122,10 @@ test('unselected sidebar icons retain -2 depth through shell cropping, including
});
test('shell modal carries its inner menu highlight animation instead of baking its first frame',async()=>{
const {ShellModalLayer}=load('app/ui/shell/modal-layer.ts',{
'../viewport':require('../.test-build/app/ui/viewport.js'),
'../../graphics/image':graphics,'../../graphics/plane':planes,'../gestures':{},'../layers':load('app/ui/layers.ts',{
'../graphics/image':graphics,'../graphics/plane':planes,'../native/frame-timings':{spanCurrent:(_name,fn)=>fn()},
'./viewport':require('../.test-build/app/ui/viewport.js'),
'../graphics/image':graphics,'../graphics/plane':planes,'../native/frame-timings':{spanCurrent:(_name,fn)=>fn()},'../graphics/screen-size':{screenWidth:()=>640,screenHeight:()=>480},
'./gestures':{isDirectionalInput:()=>false},
}),
'./geometry':{appViewportSize:()=>({width:120,height:110}),appViewportRect:()=>({x:10,y:20,width:120,height:110}),SHELL_OPAQUE_BLACK:1},
@@ -141,3 +144,34 @@ test('shell crop widens a fractional rect (TTF-measured status bar) to whole pix
assert.deepEqual([crop.x,crop.y,crop.image.width,crop.image.height],[400,250,176,15]);
assert.ok(crop.image.pixels.every(v=>v===7));
});
test('a stack without its own size follows the screen when the glasses type changes',()=>{
// The shell's stack is built once per process; re-pairing a G2 after a
// RayNeo iO must not leave its overlays painting on a 540x220 canvas.
const screen={width:540,height:220};
const {LayerStack}=load('app/ui/layers.ts',{
'./viewport':require('../.test-build/app/ui/viewport.js'),
'../graphics/image':graphics,'../graphics/plane':planes,'../native/frame-timings':{spanCurrent:(_name,fn)=>fn()},
'../graphics/screen-size':{screenWidth:()=>screen.width,screenHeight:()=>screen.height},'./gestures':{},
});
const stack=new LayerStack({paint:()=>new graphics.GrayImage(screen.width,screen.height,0)},noopLayerActions);
stack.push({paint:(_ctx,below)=>below(),handleInput:async()=>{}});
// JSON: the stack's arrays and objects come from the vm's realm.
const sizes=()=>JSON.parse(JSON.stringify(stack.paint().map(p=>[p.image.width,p.image.height])));
assert.deepEqual(sizes(),[[540,220],[540,220]]);
screen.width=640;screen.height=480;
const plain=value=>JSON.parse(JSON.stringify(value));
const unbordered={edges:{top:'offscreen',right:'offscreen',bottom:'offscreen',left:'offscreen'},padding:{top:0,right:0,bottom:0,left:0}};
assert.deepEqual(plain(stack.getBaseSize()),{width:640,height:480,...unbordered});
assert.deepEqual(sizes(),[[640,480],[640,480]]);
// An explicit size (a window's viewport, the modal's interior) stays put;
// given bare, nothing borders it, so it suggests no padding.
const sized=new LayerStack({paint:()=>new graphics.GrayImage(100,90,0)},noopLayerActions,{width:100,height:90});
screen.width=540;screen.height=220;
assert.deepEqual(plain(sized.getBaseSize()),{width:100,height:90,...unbordered});
// A host that says what its edges touch hands layers the padding for them.
const viewport=require('../.test-build/app/ui/viewport.js');
const edges={top:'border',right:'offscreen',bottom:'offscreen',left:'border'};
sized.setBaseSize(viewport.viewportWith({width:100,height:90},edges));
assert.deepEqual(plain(sized.getBaseSize()),{width:100,height:90,edges,padding:{top:8,right:0,bottom:0,left:8}});
});
+3 -2
View File
@@ -1,5 +1,6 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { bordered } = require('./helpers/viewport.cjs');
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
@@ -161,7 +162,7 @@ test('Music settings keeps toggles and selection when a new app arrives and supp
'../util/numeric-util': { clamp: (n, lo, hi) => Math.max(lo, Math.min(hi, n)) },
'./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'),
'./metrics': load('app/ui/metrics.ts'), './gestures': {},
'./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 },
'./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 },
};
deps['./menu-core'] = load('app/ui/menu-core.ts', (name) => deps[name]);
const menu = load('app/ui/menu.ts', (name) => deps[name]);
@@ -177,7 +178,7 @@ test('Music settings keeps toggles and selection when a new app arrives and supp
};
const { MusicSettingsLayer } = load('app/apps/music/music-settings.ts', (name) => uiDeps[name]);
const layer = new MusicSettingsLayer();
const ctx = { stack: { getBaseSize: () => ({ width: 540, height: 224 }), isFocused: () => true, pop: () => closes++ } };
const ctx = { stack: { getBaseSize: () => bordered(540, 224), isFocused: () => true, pop: () => closes++ } };
const paint = () => layer.paint(ctx, () => new RecordingImage(540, 224));
const input = (type) => layer.handleInput({ type }, ctx);
assert.equal(refreshes, 1);
+222
View File
@@ -0,0 +1,222 @@
// Exercises the watch app's platform-independent logic on the Mac: the gesture
// state machine, the wire format and the pad's status line.
import Foundation
var failures = 0
func check(_ condition: @autoclosure () -> Bool, _ message: String, line: Int = #line) {
if !condition() {
failures += 1
print("FAILED line \(line): \(message)")
}
}
// MARK: - Gestures
final class Harness {
let recognizer = PadGestureRecognizer()
var events: [PadEvent] = []
var time: TimeInterval = 100
init() { recognizer.onEvent = { [unowned self] in self.events.append($0) } }
func down(_ point: CGPoint = .zero) { recognizer.touchDown(at: point, time: time) }
func move(_ point: CGPoint) { recognizer.touchMoved(to: point) }
func up(_ point: CGPoint = .zero) { recognizer.touchUp(at: point, time: time) }
func wait(_ seconds: TimeInterval) { time += seconds; recognizer.advance(to: time) }
}
do {
// A lone tap is only known to be one once the double-tap window closes.
let h = Harness()
h.down(); h.up()
check(h.events == [.press], "a tap waits out the double-tap window: \(h.events)")
h.wait(0.2)
check(h.events == [.press], "still inside the window")
h.wait(0.2)
check(h.events == [.press, .tap], "tap after the window: \(h.events)")
check(h.recognizer.deadline == nil, "nothing left to wait for")
}
do {
let h = Harness()
h.down(); h.up(); h.wait(0.1); h.down(); h.wait(0.05); h.up()
check(h.events == [.press, .press, .doubleTap], "double-tap reports both presses: \(h.events)")
h.wait(1)
check(h.events.count == 3, "no late tap after a double-tap")
}
do {
// Hold: reported at the timeout and again on release, however long it lasts.
let h = Harness()
h.down(); h.wait(0.39)
check(h.events == [.press], "not yet a hold")
h.wait(0.02)
check(h.events == [.press, .longPressStart], "hold starts at the timeout: \(h.events)")
h.wait(5); h.up()
check(h.events == [.press, .longPressStart, .longPressEnd], "hold ends on release: \(h.events)")
}
do {
// Movement during a hold doesn't turn it into a swipe.
let h = Harness()
h.down(); h.wait(0.5); h.move(CGPoint(x: 50, y: 50)); h.up(CGPoint(x: 50, y: 50))
check(h.events == [.press, .longPressStart, .longPressEnd], "a hold stays a hold: \(h.events)")
}
do {
let h = Harness()
h.down(); h.up(); h.wait(0.1); h.down(); h.wait(0.5)
check(h.events == [.press, .press, .shortThenLongPress], "tap then hold: \(h.events)")
h.up(); h.wait(1)
check(h.events.count == 3, "no double-tap or release after tap-then-hold: \(h.events)")
}
do {
let h = Harness()
let start = CGPoint(x: 100, y: 180), end = CGPoint(x: 104, y: 60)
h.down(start); h.move(end); h.wait(1); h.up(end)
check(h.events == [.press, .swipe(start: start, dx: 4, dy: -120)], "a swipe, never a hold: \(h.events)")
}
do {
// A small wobble under the slop is still a tap.
let h = Harness()
h.down(.zero); h.move(CGPoint(x: 3, y: 3)); h.up(CGPoint(x: 3, y: 3)); h.wait(0.4)
check(h.events == [.press, .tap], "wobble is a tap: \(h.events)")
}
do {
// Whatever ends a hold, the phone must hear a release or it sees a stuck long-press.
let h = Harness()
h.down(); h.wait(0.5); h.recognizer.cancel()
check(h.events == [.press, .longPressStart, .longPressEnd], "a cancelled hold is released: \(h.events)")
check(!h.recognizer.isTouching, "idle after cancel")
}
do {
let h = Harness()
h.down(); h.recognizer.cancel(); h.wait(1)
check(h.events == [.press], "a cancelled touch is not a tap: \(h.events)")
}
do {
// The wrist dropped after a complete tap: it was still a tap, and there's no touch left to wait for.
let h = Harness()
h.down(); h.up(); h.recognizer.cancel()
check(h.events == [.press, .tap], "a finished tap survives cancel: \(h.events)")
h.wait(1)
check(h.events.count == 2, "and isn't reported twice")
}
do {
// A touch whose end was never seen must not wedge the next one.
let h = Harness()
h.down(); h.wait(0.5); h.down()
check(h.events == [.press, .longPressStart, .longPressEnd, .press], "a lost touch-up is recovered: \(h.events)")
}
// MARK: - Wire format
do {
let json = """
{"protocol":1,"version":"0.8.6","phase":"connected","status":"ok","connected":true,"preview":false,
"screenOn":true,"locked":false,"worn":null,"listening":false,"battery":78,"charging":false,"silentMode":false,
"caseBattery":64,"caseCharging":null,"foreground":{"appId":"music","title":"Music"},
"windows":[{"windowId":"w1","title":"Music","appId":"music","focused":true,"closeable":false,"acceptsText":true}],
"apps":[{"appId":"timer","title":"Timer"}],"remoteEnabled":true,"vibrateOnInput":false,"soundEffects":true,"canUnlock":false,
"mirrorAssistant":true,"assistantAvailable":true}
"""
let state = PhoneState.parse(json, updatedAt: 42)
check(state != nil, "state parses")
check(state?.connected == true && state?.takesInput == true, "connected")
check(state?.battery == 78 && state?.caseBattery == 64, "batteries")
check(state?.worn == nil && state?.caseCharging == nil, "JSON null stays nil")
check(state?.foreground == AppEntry(appId: "music", title: "Music"), "foreground")
check(state?.windows.first?.acceptsText == true && state?.windows.first?.closeable == false, "window flags")
check(state?.apps.map(\.appId) == ["timer"], "apps")
check(state?.vibrateOnInput == false && state?.soundEffects == true && state?.canUnlock == false, "settings")
check(state?.updatedAt == 42, "updatedAt")
}
do {
// A state from an older or newer phone app parses with defaults rather than failing.
let state = PhoneState.parse("{\"battery\":140,\"unknownField\":[1,2]}", updatedAt: 0)
check(state?.battery == nil, "out-of-range percentage is unknown")
check(state?.remoteEnabled == true && state?.canUnlock == true, "permissive defaults")
check(state?.vibrateOnInput == true && state?.soundEffects == false, "feedback defaults")
check(state?.phase == "disconnected" && state?.takesInput == false, "disconnected default")
check(PhoneState.parse("not json", updatedAt: 0) == nil, "garbage is rejected")
check(PhoneState.parse("[1]", updatedAt: 0) == nil, "a non-object is rejected")
}
do {
let ack = Ack.parse("{\"seq\":7,\"ok\":false,\"jsReady\":false,\"message\":\"nope\"}")
check(ack == Ack(seq: 7, ok: false, jsReady: false, message: "nope"), "ack: \(String(describing: ack))")
check(Ack.parse("{\"seq\":7,\"ok\":true}")?.jsReady == true, "jsReady defaults to true")
}
do {
check(PhoneEvent.parse("{\"type\":\"assistant\",\"phase\":\"done\",\"text\":\"hi\"}") == .assistant(phase: "done", text: "hi"), "assistant event")
check(PhoneEvent.parse("{\"type\":\"alert\",\"text\":\"x\"}") == .alert(text: "x"), "alert event")
check(PhoneEvent.parse("{\"type\":\"other\"}") == nil, "unknown event")
}
do {
var mirror = AssistantMirror()
mirror = mirror.applying(phase: "thinking", text: "")
check(mirror == AssistantMirror(phase: "thinking", text: ""), "thinking")
mirror = mirror.applying(phase: "streaming", text: "Hel")
mirror = mirror.applying(phase: "done", text: "")
check(mirror == AssistantMirror(phase: "done", text: "Hel"), "an empty done keeps the streamed text")
mirror = mirror.applying(phase: "closed", text: "")
check(mirror == AssistantMirror(phase: "idle", text: "Hel"), "closed keeps the text to read")
check(mirror.applying(phase: "bogus", text: "x") == mirror, "unknown phases change nothing")
}
// MARK: - Status line
do {
var up = LinkStatus(activated: true, phoneReachable: true, phoneAppInstalled: true, appRunning: true)
var state = PhoneState()
state.connected = true
state.screenOn = true
state.foreground = AppEntry(appId: "music", title: "Music")
check(padLine(state: nil, link: LinkStatus()).text == "Waiting for phone", "before activation")
check(padLine(state: state, link: LinkStatus(activated: true, phoneAppInstalled: false)).text == "Install Faceclaw on phone", "no phone app")
check(padLine(state: state, link: LinkStatus(activated: true, phoneAppInstalled: true)).text == "No phone", "unreachable")
up.appRunning = false
check(padLine(state: state, link: up).text == "Open Faceclaw on phone", "dashboard not up")
up.appRunning = true
check(padLine(state: nil, link: up).text == "Waiting for phone", "no state yet")
check(padLine(state: state, link: up) == StatusLine(text: "Music", tone: .good), "foreground app")
state.screenOn = false
check(padLine(state: state, link: up).text == "●● Wake", "display off")
state.locked = true
check(padLine(state: state, link: up).text == "Locked", "locked beats display off")
state.charging = true
state.battery = 50
state.caseBattery = 64
state.caseCharging = true
check(padLine(state: state, link: up) == StatusLine(text: "Charging: G2 50%", tone: .busy, detail: "Case 64% ⚡"), "charging")
var off = PhoneState()
check(padLine(state: off, link: up).text == "Disconnected", "disconnected")
off.phase = "retrying"
check(padLine(state: off, link: up).text == "Reconnecting…", "iOS's retrying phase")
off.phase = "disconnected"
off.preview = true
off.screenOn = true
check(padLine(state: off, link: up) == StatusLine(text: "Launcher", tone: .good, detail: "Phone preview"), "phone preview")
off.screenOn = false
check(padLine(state: off, link: up).text == "●● Wake", "preview display off")
off.remoteEnabled = false
check(padLine(state: off, link: up).text == "Watch control off", "remote disabled")
}
if failures > 0 {
print("\(failures) check(s) failed")
exit(1)
}
print("apple-watch logic: all checks passed")
+23 -8
View File
@@ -5,6 +5,7 @@ const vm = require('node:vm');
const ts = require('typescript');
const path = require('node:path');
const { loader } = require('./helpers/load-typescript.cjs');
const { bordered, unbordered, edged } = require('./helpers/viewport.cjs');
const flush = () => new Promise(resolve => setImmediate(resolve));
const route = { coordinates: [[-122.4, 37.7], [-122.4, 37.71]], distanceMeters: 1112, durationSec: 600,
@@ -13,7 +14,7 @@ const route = { coordinates: [[-122.4, 37.7], [-122.4, 37.71]], distanceMeters:
const fix = latitude => ({ latitude, longitude: -122.4, timestampMs: Date.now(), bearingDeg: 0, speedMps: 1.5, accuracyMeters: 5 });
function fixture() {
const messages = [], timers = new Set(), timeouts = new Set(), holds = [], maps = [], errors = [], requests = [];
let callbacks, compass, delayRoute = null, delayMap = null, routeError = null, distanceUnit = 'miles', timeFormat = '24h';
let callbacks, compass, delayRoute = null, delayMap = null, routeError = null, distanceUnit = 'miles', timeFormat = '24h', paints = 0;
const routeMath = loader()('app/apps/navigate/route-follower.ts');
const tracker = { running: false, isRunning() { return this.running; }, start() { this.running = true; }, stop() { this.running = false; } };
const sensor = { LocationTracker: function (value) { callbacks = value; return tracker; }, COMPASS_CHANGED: 15,
@@ -22,7 +23,8 @@ function fixture() {
handleNavigationSensorEvent: event => { if (event.kind === 'location') callbacks.onLocation(event.location); } };
const modules = {
'@nativescript/core/globals': {},
'../../graphics/bdffont': { getFont: () => ({}) }, '../../graphics/ui-fonts': { getDefaultSmallFont: () => ({}) },
// The footer hint's line height sets how much of the window the map-mode map gets.
'../../graphics/bdffont': { getFont: () => ({}) }, '../../graphics/ui-fonts': { getDefaultSmallFont: () => ({ lineHeight: 12 }) },
'../../native/settings-store': { onSettingsStoreChanged() {} },
'../../ui/dashboard-settings': { distanceUnitSetting: { get: () => distanceUnit } },
'../../ui/clock-format': loader({}, { './dashboard-settings': { timeFormatSetting: { get: () => timeFormat } } })('app/ui/clock-format.ts'),
@@ -34,7 +36,8 @@ function fixture() {
'../../native/frame-timings': { startFrame: () => 1, finishFrame() {}, logFrame() {}, span: (_id, _name, fn) => fn(), runWithFrame: (_id, fn) => fn() },
'../../native/active-display': { getActiveDisplay: () => null },
'../../ui/menu-core': require('../.test-build/app/ui/menu-core.js'),
'../../ui/window-menu': { WindowMenu: class { paint() { return []; } isOpen() { return false; } resize() {} } },
'../../ui/window-menu': { WindowMenu: class { paint() { paints++; return []; } isOpen() { return false; } resize() {} } },
'../../ui/viewport': require('../.test-build/app/ui/viewport.js'),
'../../graphics/plane': { planesFingerprint: () => 'frame' },
'../../native/mapbox': {
isMapboxConfigured: () => true,
@@ -54,9 +57,9 @@ function fixture() {
console: { log() {}, warn() {}, error: error => errors.push(error) }, require: id => modules[id] ?? {},
});
const send = data => global.onmessage({ data });
send({ type: 'open-window', windowId: 'navigate:main', surfaceId: 'window:navigate:main', title: 'Navigate', viewport: { width: 576, height: 480 } });
send({ type: 'open-window', windowId: 'navigate:main', surfaceId: 'window:navigate:main', title: 'Navigate', viewport: bordered(576, 480) });
send({ type: 'foreground', windowId: 'navigate:main', foreground: true, focused: true });
return { ...exports.probe, send, timers, holds, maps, errors, tracker, requests,
return { ...exports.probe, send, timers, holds, maps, errors, tracker, requests, paints: () => paints,
input: type => send({ type: 'input', windowId: 'navigate:main', focused: true, frameId: 1, event: { type } }),
tick: () => { for (const callback of timers) callback(); },
expireFix: () => { for (const callback of timeouts) callback(); timeouts.clear(); },
@@ -114,8 +117,9 @@ test('front page map entry follows location without routing, supports zoom and r
h.fix(37.7); await flush();
assert.equal(h.state().phase, 'map');
assert.deepEqual(h.requests, []);
// The map fills the window down to the footer hint, which rests on the bottom padding.
assert.equal(h.maps.at(-1).width, 576);
assert.equal(h.maps.at(-1).height, 450);
assert.equal(h.maps.at(-1).height, 452);
assert.equal(h.maps.at(-1).routePath, undefined);
assert.equal(h.maps.at(-1).camera.latitude, 37.7);
assert.equal(h.maps.at(-1).camera.bearingDeg, 0);
@@ -147,9 +151,20 @@ test('map suspends fetching and compass while hidden and resizes to the viewport
assert.equal(h.holds.at(-1), false);
h.send({ type: 'screen', on: true }); await flush();
assert.equal(h.maps.at(-1).camera.latitude, 37.71);
h.send({ type: 'resize-window', windowId: 'navigate:main', viewport: { width: 540, height: 260 } }); await flush();
const resize = async viewport => { h.send({ type: 'resize-window', windowId: 'navigate:main', viewport }); await flush(); };
await resize(bordered(540, 260));
assert.equal(h.maps.at(-1).width, 540);
assert.equal(h.maps.at(-1).height, 230);
assert.equal(h.maps.at(-1).height, 232);
// With no border below, the footer hint moves down to the edge and the map follows it.
await resize(edged(540, 260, 'top', 'left'));
assert.equal(h.maps.at(-1).height, 240);
// A change to the other edges lays the window out again, but keeps the map it has.
const fetched = h.maps.length, painted = h.paints();
await resize(unbordered(540, 260));
assert.equal(h.maps.length, fetched);
assert.equal(h.paints(), painted + 1);
await resize(unbordered(540, 260));
assert.equal(h.paints(), painted + 1);
h.send({ type: 'close-window' });
assert.equal(h.tracker.running, false);
assert.equal(h.timers.size, 0);
+61 -7
View File
@@ -4,6 +4,7 @@ const fs = require('node:fs');
const path = require('node:path');
const vm = require('node:vm');
const ts = require('typescript');
const { bordered, unbordered, edged } = require('./helpers/viewport.cjs');
const alerts = require('../.test-build/app/apps/nightscout/nightscout-alerts.js');
const detector = require('../.test-build/app/apps/nightscout/missed-bolus-detector.js');
const now = 1_800_000_000_000;
@@ -53,6 +54,7 @@ class Image {
drawText(font, ...args) { this.draws.push(['text', ...args]); }
drawLine(...args) { this.draws.push(['line', ...args]); }
fillRect(...args) { this.draws.push(['rect', ...args]); }
drawRect(...args) { this.draws.push(['frame', ...args]); }
}
const font = { measureText: text => text.length * 6, lineHeight: 12, hasGlyph: () => true };
@@ -125,14 +127,14 @@ test('overflow warning backgrounds cover graph pixels and deferred graph labels'
assert.equal(baked.getPixel(200, 8), 56, 'uncovered graph remains visible');
});
function fixture(always = false) {
function fixture(always = false, GrayImage = Image, uiFont = font) {
let time = now, icon, tick, changed, notify, renders = 0;
const state = { ...healthy, available: true, latest: { sgv: 100, timestampMs: now }, history: [], units: 'mg/dL', delta: 0, direction: '', iob: 1, cob: 0, pumpStatus: '0m ago', configurationMissing: false };
const bridge = { snapshot: () => state, onStateChange(fn) { notify = fn; fn(state); } };
const settings = { loadNightscoutThresholds: () => limits, nightscoutAlwaysShowInTopBarSetting: { get: () => always }, onAnySettingChanged(fn) { changed = fn; }, isNightscoutSettingsConfigured: () => true };
const deps = {
'../../graphics/image': { GrayImage: Image },
'../../graphics/ui-fonts': { getDefaultSmallFont: () => font, getDefaultLargeFont: () => font },
'../../graphics/image': { GrayImage },
'../../graphics/ui-fonts': { getDefaultSmallFont: () => uiFont, getDefaultLargeFont: () => uiFont },
'../../native/nightscout-bridge': { nightscoutBridge: bridge },
'../../ui/dashboard-settings': settings,
'./nightscout-alerts': alerts,
@@ -181,17 +183,38 @@ test('tray is hidden by default and clears recovered measurement warnings immedi
second.close(); assert.equal(f.icon(), null);
});
test('main page highlights only the breached field and removes highlights on recovery', () => {
const f = fixture();
const page = load('app/apps/nightscout/nightscout.ts', {
test('tray keeps every pixel row when a TTF font measures the reading fractionally', () => {
const { GrayImage } = require('../.test-build/app/graphics/image.js');
// 8.875 is Roboto-Light 16's digit advance under iOS Core Text.
const f = fixture(true, GrayImage, { ...font, measureText: text => text.length * 8.875, drawText() {} });
f.state.latest = { sgv: 142, timestampMs: now };
// Spans the graph's full 60..200 range, so its line crosses every row.
f.state.history = [{ timestampMs: now - 10 * 60_000, sgv: 200 }, { timestampMs: now, sgv: 60 }];
f.app.startNightscoutTrayIcon();
const icon = f.icon();
assert.ok(Number.isInteger(icon.width), `width ${icon.width}`);
for (let y = 0; y < icon.height; y++) {
const row = Array.from({ length: icon.width }, (_, x) => icon.getPixel(x, y));
assert.ok(row.some(value => value !== 0), `row ${y} of the graph is drawn`);
}
});
/** nightscout.ts (the app window's page) over a fixture's bridge and settings. */
function loadMainPage(f) {
return load('app/apps/nightscout/nightscout.ts', {
...f.deps,
'../../graphics/textwrap': { truncateText: (font, text) => text },
'../../ui/gestures': {}, '../../ui/layers': {}, '../../ui/menu': {},
'../../ui/dashboard/settings-panel': {}, '../../ui/metrics': { lineStep: () => 16 },
'~/util/date-util': { formatAgeShortFromTimestamp: () => '1m', formatTimestamp: () => '12:00' },
}, { Date: { now: () => now } });
}
test('main page highlights only the breached field and removes highlights on recovery', () => {
const f = fixture();
const page = loadMainPage(f);
const layer = new page.NightscoutLayer();
const paint = () => layer.paint({ stack: { getBaseSize: () => ({ width: 576, height: 260 }) } });
const paint = () => layer.paint({ stack: { getBaseSize: () => bordered(576, 260) } });
for (const [field, value, label] of [['reservoirUnits', 10, '10U'], ['batteryVoltage', 1, '1.00V'], ['cageTimestampMs', now - 73 * 3_600_000, 'CAGE 1m'], ['loopTimestampMs', now - 16 * 60_000, 'Loop 1m']]) {
f.state[field] = value;
const image = paint();
@@ -203,6 +226,37 @@ test('main page highlights only the breached field and removes highlights on rec
}
});
test('main page puts its title, readout and graph at the padding suggested for each edge', () => {
const f = fixture();
Object.assign(f.state, { history: [{ timestampMs: now - 60_000, sgv: 120 }], basal: [], carbs: [], boluses: [] });
const layer = new (loadMainPage(f).NightscoutLayer)();
const paint = viewport => layer.paint({ stack: { getBaseSize: () => viewport } }).draws;
const texts = draws => draws.filter(d => d[0] === 'text');
const graph = draws => draws.find(d => d[0] === 'frame').slice(1, 5);
// A border on every edge: 8px in from each. The caption is the last line above the bottom padding.
const framed = paint(bordered(576, 260));
assert.deepEqual(texts(framed)[0].slice(1, 4), [8, 8, 'Nightscout']);
assert.deepEqual(texts(framed)[1].slice(1, 4), [8, 64, '100'], 'readout, centered between the title (to y=20) and the graph (from y=120)');
assert.deepEqual(graph(framed), [8, 120, 560, 114]);
assert.deepEqual(texts(framed).at(-1).slice(1, 3), [8, 240]);
// No borders: the page runs to the window's edges and the graph takes the room.
const bare = paint(unbordered(576, 260));
assert.deepEqual(texts(bare)[0].slice(1, 3), [0, 0]);
assert.deepEqual(graph(bare), [0, 112, 576, 130]);
assert.deepEqual(texts(bare).at(-1).slice(1, 3), [0, 248]);
// Each edge is padded on its own.
assert.deepEqual(graph(paint(edged(576, 260, 'top', 'left'))), [8, 120, 568, 122]);
assert.deepEqual(graph(paint(edged(576, 260, 'bottom', 'right'))), [0, 112, 568, 122]);
// A window too short for the graph and its caption gives the graph the caption's room, inside the image.
const short = paint(bordered(476, 155));
assert.deepEqual(graph(short), [8, 120, 460, 27]);
assert.ok(!texts(short).some(d => /glucose history/.test(d[3])));
});
test('bridge exposes numeric measurements and treats absent values as unknown', async () => {
let pump = { reservoir: '10.5', battery: { voltage: '1.15' } };
const module = load('app/native/nightscout-bridge.ts', {
+54 -4
View File
@@ -1,5 +1,6 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { bordered } = require('./helpers/viewport.cjs');
const fs = require('node:fs');
const path = require('node:path');
const vm = require('node:vm');
@@ -23,6 +24,41 @@ function store(settings = new Map()) {
}
const mail = { packageName: 'com.mail', appName: 'Mail' };
/** dashboard-settings.ts over the same in-memory store, with `prefs` as its notification-sources. */
function dashboardSettings(settings, prefs) {
return evaluate(source('app/ui/dashboard-settings.ts'), (id) => {
if (id.includes('settings-store')) return {
onSettingsStoreChanged() {}, getStringSetting: (key, fallback) => settings.get(key) ?? fallback,
setStringSetting: (key, value) => settings.set(key, value),
getBooleanSetting: (key, fallback) => settings.get(key) ?? fallback, setBooleanSetting: (key, value) => settings.set(key, value),
};
if (id.includes('notification-sources')) return prefs;
if (id.includes('brightness-curve')) return require('../.test-build/app/g2/brightness-curve.js');
if (id.includes('animation-speed')) return require('../.test-build/app/ui/animation-speed.js');
return { Layer: class {}, ASSISTANT_MODEL_CHOICES: [] };
}, { global: { isIOS: false } });
}
test('"Display new notifications" defaults to 15s; Off stops every popup, the filter still mutes single apps', () => {
const settings = new Map();
const prefs = store(settings);
const api = dashboardSettings(settings, prefs);
assert.equal(api.newNotificationDisplaySetting.get(), '15s');
// Array.from: the module's arrays come from its own VM realm.
const values = Array.from(api.newNotificationDisplaySetting.values);
assert.deepEqual(values.map((value) => api.newNotificationDisplaySetting.displayValue(value)),
['Off', 'Until dismissed', '5s', '15s', '1min']);
assert.deepEqual(values.map(api.newNotificationDisplayTimeoutMs), [null, null, 5_000, 15_000, 60_000]);
assert.equal(api.shouldPopUpNewNotification(mail.packageName), true);
prefs.setNotificationSourceEnabled(mail, false);
assert.equal(api.shouldPopUpNewNotification(mail.packageName), false);
assert.equal(api.shouldPopUpNewNotification('com.other'), true);
api.newNotificationDisplaySetting.set('off');
assert.equal(api.shouldPopUpNewNotification('com.other'), false);
api.newNotificationDisplaySetting.set('until-dismissed');
assert.equal(api.shouldPopUpNewNotification('com.other'), true);
});
test('sources default on and retain per-package choices and names across restart and rediscovery', () => {
const settings = new Map();
let prefs = store(settings);
@@ -87,7 +123,7 @@ function ui(fontSize = 12) {
'../util/render-freshness': { renderPassAllowsStaleData: () => false },
'./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'),
'./metrics': load('app/ui/metrics.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 },
'./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 },
'./gestures': {},
};
const requireModule = (name) => {
@@ -98,7 +134,7 @@ function ui(fontSize = 12) {
dependencies['./menu'] = load('app/ui/menu.ts', requireModule);
const { SingleNotificationLayer } = load('app/ui/notifications.ts', requireModule);
const { NotificationFilterLayer } = load('app/ui/notification-filter.ts', requireModule);
const ctx = { stack: { getBaseSize: () => ({ width: 540, height: 224 }), isFocused: () => true, pop: () => closes++ } };
const ctx = { stack: { getBaseSize: () => bordered(540, 224), isFocused: () => true, pop: () => closes++ } };
const popup = new SingleNotificationLayer('key', { origin: 'new-notification-modal', onClose: () => closes++ });
const trayCloses = [];
const tray = new SingleNotificationLayer('key', { origin: 'notification-tray', onClose: (_ctx, gone) => trayCloses.push(gone) });
@@ -194,14 +230,16 @@ test('controller filters before waking or opening a popup, and still refreshes t
const members = ['handleAndroidNotificationPosted', 'handleAndroidNotificationRemoved', 'popUpAndroidNotification']
.map((name) => controller.members.find((node) => node.name?.getText(file) === name).getText(file));
const { NotificationPopupDelay, notificationPopupDelayMs } = require('../.test-build/app/ui/shell/notification-popup-delay.js');
const prefs = store();
const settings = new Map();
const prefs = store(settings);
const { newNotificationDisplaySetting, shouldPopUpNewNotification } = dashboardSettings(settings, prefs);
let active = [{ ...mail, key: 'key' }];
let wakes = 0, popups = 0, renders = 0;
const removed = [];
const { Harness } = evaluate(`export class Harness { ${members.join('\n')} }`, null, {
ALL_NOTIFICATIONS: 0x7fffffff,
readActiveNotifications: () => active,
shouldShowNotificationOnGlasses: prefs.shouldShowNotificationOnGlasses,
shouldPopUpNewNotification,
notificationPopupDelayMs,
shell: {
isScreenOn: () => false,
@@ -253,4 +291,16 @@ test('controller filters before waking or opening a popup, and still refreshes t
prefs.setNotificationSourceEnabled(mail, false);
elapse();
assert.equal(popups, 1);
// "Display new notifications: Off" holds back every app's popup, also once the delay is over.
prefs.setNotificationSourceEnabled(mail, true);
newNotificationDisplaySetting.set('off');
instance.handleAndroidNotificationPosted('key');
elapse();
newNotificationDisplaySetting.set('15s');
instance.handleAndroidNotificationPosted('key');
newNotificationDisplaySetting.set('off');
elapse();
assert.equal(wakes, 1);
assert.equal(popups, 1);
});
+94
View File
@@ -0,0 +1,94 @@
const test = require("node:test");
const assert = require("node:assert/strict");
const { NotificationPopupTimeout } = require("../.test-build/app/ui/shell/notification-popup-timeout.js");
/** A manual clock: schedule() queues timers that advance() fires when due. */
function makeClock() {
const clock = {
nowMs: 0,
timers: [],
schedule(run, delayMs) {
const timer = { dueAtMs: clock.nowMs + delayMs, run, cancelled: false };
clock.timers.push(timer);
return () => { timer.cancelled = true; };
},
advance(ms) {
const until = clock.nowMs + ms;
for (;;) {
const due = clock.timers.filter((timer) => !timer.cancelled && timer.dueAtMs <= until)
.sort((a, b) => a.dueAtMs - b.dueAtMs)[0];
if (!due) break;
clock.nowMs = due.dueAtMs;
due.cancelled = true;
due.run();
}
clock.nowMs = until;
},
};
return clock;
}
function makeTimeout(timeoutMs) {
const clock = makeClock();
let expiries = 0;
const timeout = new NotificationPopupTimeout(timeoutMs, () => { expiries++; }, clock.schedule);
return { clock, timeout, expiries: () => expiries };
}
test("an untouched popup expires once its timeout passes", () => {
const { clock, timeout, expiries } = makeTimeout(15_000);
timeout.restart();
clock.advance(14_999);
assert.equal(expiries(), 0);
clock.advance(1);
assert.equal(expiries(), 1);
assert.equal(timeout.isRunning(), false);
clock.advance(60_000);
assert.equal(expiries(), 1);
});
test("each interaction starts the countdown over", () => {
const { clock, timeout, expiries } = makeTimeout(5_000);
timeout.restart();
clock.advance(4_000);
timeout.restart(); // e.g. scrolling the action menu
clock.advance(4_000);
assert.equal(expiries(), 0);
clock.advance(1_000);
assert.equal(expiries(), 1);
});
test("a popup that goes away first never expires", () => {
const { clock, timeout, expiries } = makeTimeout(5_000);
timeout.restart();
clock.advance(2_000);
timeout.stop();
clock.advance(10_000);
assert.equal(expiries(), 0);
});
test("until-dismissed (no timeout) never schedules anything", () => {
const { clock, timeout, expiries } = makeTimeout(null);
timeout.restart();
assert.equal(timeout.isRunning(), false);
assert.equal(clock.timers.length, 0);
clock.advance(1_000_000);
assert.equal(expiries(), 0);
});
test("the expiry callback can restart the countdown (something was over the popup)", () => {
const clock = makeClock();
let expiries = 0;
const timeout = new NotificationPopupTimeout(5_000, () => {
expiries++;
if (expiries === 1) timeout.restart();
}, clock.schedule);
timeout.restart();
clock.advance(5_000);
assert.equal(expiries, 1);
assert.equal(timeout.isRunning(), true);
clock.advance(5_000);
assert.equal(expiries, 2);
assert.equal(timeout.isRunning(), false);
});
+195
View File
@@ -0,0 +1,195 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs'), vm = require('node:vm'), ts = require('typescript');
function load(file, modules, globals) {
const context = { exports: {}, require: name => {
if (!(name in modules)) throw new Error(`Missing mock: ${name} in ${file}`);
return modules[name];
}, setInterval, clearInterval, Date, ...globals };
vm.runInNewContext(ts.transpileModule(fs.readFileSync(file, 'utf8'), {
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 },
}).outputText, context);
return context.exports;
}
function fixture({ glasses = 'g2', ios = false, completed = false, ring = false, watch = false, previewOnly = false } = {}) {
const state = { glasses, ring, watch, navigations: [], backs: 0, previewOnly, completed };
const frame = { navigate: entry => state.navigations.push(entry), goBack: () => { state.backs++; }, canGoBack: () => true, backStack: [] };
class Observable { notifyPropertyChange() {} }
const core = {
Observable, Frame: { topmost: () => frame }, Screen: { mainScreen: { heightDIPs: 800, widthDIPs: 400 } },
Application: { on() {}, off() {}, suspendEvent: 'suspend', resumeEvent: 'resume' },
};
const onboardingState = {
hasCompletedOnboarding: () => state.completed,
setOnboardingCompleted: value => { state.completed = value; },
isPreviewOnlyMode: () => state.previewOnly, setPreviewOnlyMode: value => { state.previewOnly = value; },
ownsRing: () => state.ring, setOwnsRing: value => { state.ring = value; },
ownsWatch: () => state.watch, setOwnsWatch: value => { state.watch = value; },
};
const settings = { glassesTypeSetting: { get: () => state.glasses, set: value => { state.glasses = value; } } };
const globals = { global: { isIOS: ios, isAndroid: !ios } };
const modules = {
'@nativescript/core': core, './onboarding-state': onboardingState, '../ui/dashboard-settings': settings,
'./onboarding-navigation': { finishOnboardingNavigation: () => state.navigations.push('finish') },
'../g1/even-firmware': { EVEN_G1_RESTORE: { version: '1.6.6' } },
'../native/even-app-conflict': { openEvenAppSettings() {} },
'../native/ble-permissions': { ensureBlePermissions: async () => {} },
'../g2/device-addresses': {
loadDeviceAddresses: () => ({ right: '', left: '', ring: '' }), loadPairedGlassesIdentity: () => null,
saveDeviceAddresses() {}, savePairedGlassesIdentity() {},
},
'../util/format-error': { formatErrorMessage: String },
'../g2/pairing-candidates': {
DiscoveryAggregator: class { prune() {} pairs() { return []; } rings() { return []; } rayneos() { return []; } clear() {} },
DEFAULT_STALE_AFTER_MS: 1, nearestCandidateId: () => null,
},
'../native/device-discovery': { DeviceDiscoveryBridge: class {} },
};
// Permission checks: everything granted.
const granted = new Proxy({}, { get: () => () => true });
for (const name of ['../native/ios-bluetooth', '../g2/android-permissions', '../native/calendar-permissions', '../native/battery-optimization',
'../native/file-access', '../native/notification-access']) modules[name] = granted;
modules['../native/media-controller'] = { mediaControllerBridge: { hasAccess: () => true } };
const loadPage = file => load(`app/phone-ui/${file}.ts`, modules, globals);
return { state, loadPage };
}
const rowFor = (model, choice) => model.glassesOptions.find(option => option.choice === choice);
const selected = model => model.glassesOptions.filter(option => option.checkVisibility === 'visible').map(option => option.choice).join();
const tap = (model, type) => model.onGlassesRowTap({ object: { bindingContext: rowFor(model, type) } });
test('the devices page sits between the splash and the disclaimer', () => {
const { state, loadPage } = fixture();
const { OnboardingViewModel } = loadPage('onboarding-view-model');
new OnboardingViewModel().onPrimaryTap();
assert.equal(state.navigations.pop().moduleName, 'phone-ui/onboarding-devices-page');
const { OnboardingDevicesViewModel } = loadPage('onboarding-devices-view-model');
const devices = new OnboardingDevicesViewModel();
tap(devices, 'g2');
devices.onNextTap();
const next = state.navigations.pop();
assert.equal(next.moduleName, 'phone-ui/onboarding-page');
assert.equal(next.context.step, 2);
const disclaimer = new OnboardingViewModel(next.context.step);
assert.equal(disclaimer.headline, 'Before You Continue');
// Backing out of the disclaimer lands on the devices page, not the splash.
disclaimer.onSecondaryTap();
assert.equal(state.backs, 1);
assert.equal(state.navigations.length, 0);
});
test('iOS registers the devices page, which it cannot resolve otherwise', () => {
const source = fs.readFileSync('app/phone-ui/onboarding-register.ios.ts', 'utf8');
assert.match(source, /registerModule\('phone-ui\/onboarding-devices-page', /);
assert.match(source, /registerModule\('phone-ui\/onboarding-devices-page\.xml', /);
});
test('the disclaimer and firmware steps name only the chosen glasses', () => {
const copy = glasses => {
const { OnboardingViewModel } = fixture({ glasses }).loadPage('onboarding-view-model');
return { disclaimer: new OnboardingViewModel(2), firmware: new OnboardingViewModel(3) };
};
const g2 = copy('g2'), g1 = copy('g1'), io = copy('rayneo-io');
assert.match(g2.disclaimer.body, /for the Even Realities G2 smart glasses/);
assert.doesNotMatch(g2.disclaimer.body, /G1|RayNeo/);
assert.match(g1.disclaimer.body, /for the Even Realities G1 smart glasses/);
assert.match(io.disclaimer.body, /for the RayNeo iO smart glasses\. It is not created or supported by RayNeo\./);
assert.doesNotMatch(io.disclaimer.body, /Even/);
assert.match(g2.firmware.body, /reconnecting the official Even Realities app/);
assert.doesNotMatch(g2.firmware.body, /from Faceclaw|1\.6\.6|G1/);
assert.match(g1.firmware.body, /return to the official firmware later from Faceclaw, as version 1\.6\.6\./);
assert.doesNotMatch(g1.firmware.body, /reconnecting|G2/);
assert.equal(g2.firmware.primaryLabel, 'Flash Firmware');
assert.equal(g1.firmware.primaryLabel, 'Flash Firmware');
// The app has no flasher for an iO.
assert.equal(io.firmware.primaryLabel, 'Pair Glasses');
assert.doesNotMatch(io.firmware.body, /Flash Firmware|Even/);
// Preview Only moved to Your Devices: the firmware step only goes forward or back.
for (const { firmware } of [g2, g1, io]) {
assert.equal(firmware.secondaryLabel, 'Back');
assert.doesNotMatch(firmware.body, /two choices/);
assert.match(firmware.body, /Go back to Your Devices and choose Preview Only/);
}
});
test('Preview Only is a glasses choice that skips the firmware step', () => {
const { state, loadPage } = fixture({ glasses: 'rayneo-io' });
const { OnboardingDevicesViewModel } = loadPage('onboarding-devices-view-model');
const devices = new OnboardingDevicesViewModel();
assert.equal(rowFor(devices, 'preview').noteVisibility, 'visible');
tap(devices, 'preview');
assert.equal(state.previewOnly, true);
assert.equal(state.glasses, 'rayneo-io');
assert.equal(selected(devices), 'preview');
assert.equal(devices.nextEnabled, true);
// A later visit (say, after restarting mid-setup) asks again.
assert.equal(selected(new OnboardingDevicesViewModel()), '');
// The disclaimer can't name glasses nobody chose.
const { OnboardingViewModel } = loadPage('onboarding-view-model');
assert.match(new OnboardingViewModel(2).body, /for the Even Realities G1 and G2 and RayNeo iO smart glasses\. It is not created or supported by Even Realities or RayNeo\./);
// Permissions is the last page: onboarding completes there, in preview mode.
const { PermissionsViewModel } = loadPage('permissions-view-model');
return new PermissionsViewModel({ onboarding: true }).onPrimaryTap().then(async () => {
assert.equal(state.completed, true);
assert.deepEqual(state.navigations, ['finish']);
// Choosing glasses again turns preview off, and Permissions leads to the firmware step.
state.completed = false;
state.navigations.length = 0;
tap(devices, 'g1');
assert.equal(state.previewOnly, false);
assert.equal(state.glasses, 'g1');
await new PermissionsViewModel({ onboarding: true }).onPrimaryTap();
assert.equal(state.completed, false);
assert.equal(state.navigations[0].moduleName, 'phone-ui/onboarding-page');
assert.equal(state.navigations[0].context.step, 3);
// And that step's Back returns to Permissions.
new OnboardingViewModel(3).onSecondaryTap();
assert.equal(state.backs, 1);
});
});
test('the disconnect page drops the Even app instructions only when setting up an iO', () => {
const page = options => new (fixture(options).loadPage('onboarding-unpair-view-model').OnboardingUnpairViewModel)();
const setup = page({ glasses: 'rayneo-io' });
assert.equal(setup.evenAppVisibility, 'collapse');
assert.equal(setup.settingsButtonVisibility, 'collapse');
assert.doesNotMatch(setup.otherAppsInstructions, /Even/);
for (const options of [{ glasses: 'g2' }, { glasses: 'rayneo-io', completed: true }]) {
const model = page(options);
assert.equal(model.evenAppVisibility, 'visible');
assert.equal(model.settingsButtonVisibility, 'visible');
assert.match(model.otherAppsInstructions, /Even Realities/);
}
});
test('pairing during setup follows the choices; from the menu it offers everything', () => {
const page = (options, onboarding = true) => new (fixture(options).loadPage('pairing-view-model').PairingViewModel)({ onboarding });
const g1 = page({ glasses: 'g1' });
assert.match(g1.instructions, /^Select your G1 glasses below\. Put the glasses in their case with the lid open/);
assert.equal(g1.ringSectionVisibility, 'collapse');
const io = page({ glasses: 'rayneo-io', ring: true });
assert.match(io.instructions, /^Select your RayNeo iO and your R1 ring below\. Wear the glasses/);
assert.equal(io.ringSectionVisibility, 'visible');
// Past setup, "Pair glasses" re-enters with onboarding set, including for an
// install from before the devices page existed (nothing stored: no ring).
for (const model of [page({ glasses: 'g2', completed: true }), page({ glasses: 'g2' }, false)]) {
assert.match(model.instructions, /G2 or G1 glasses or RayNeo iO, and your R1 ring if you have one/);
assert.equal(model.ringSectionVisibility, 'visible');
}
});
+91 -9
View File
@@ -1,6 +1,7 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { loader } = require('./helpers/load-typescript.cjs');
const { bordered, unbordered } = require('./helpers/viewport.cjs');
// Roboto's line heights at the onboarding sizes, and a fixed advance.
const FONTS = { title: 31, body: 24, detail: 19, hero: 52 };
@@ -11,19 +12,22 @@ function setting(value, values) {
writes: [], displayValue: (v) => v[0].toUpperCase() + v.slice(1) };
}
function harness() {
// The onboarding pages over G2 glasses (640x480), or a RayNeo iO's 540x220 screen.
function harness(glasses = 'g2') {
const screen = glasses === 'rayneo-io' ? { width: 540, height: 220 } : { width: 640, height: 480 };
const settings = {
glassesTypeSetting: setting(glasses, ['g2', 'rayneo-io']),
appSwitcherPositionSetting: setting('bottom', ['left', 'right', 'bottom', 'popup']),
statusBarPositionSetting: setting('bottom', ['top', 'bottom']),
uiDepthSetting: setting('48', ['-62', '-48', '-32', '-16', '0', '16', '32', '48', '62']),
};
const load = loader({}, {
'./onboarding-fonts': { onboardingFont: font },
'../../graphics/image': { G2_LENS_WIDTH: 640 },
'../../ui/dashboard-settings': settings,
'../../ui/shell/geometry': { MIN_WINDOW_HEIGHT: 288, SIDEBAR_WIDTH: 64, SWITCHER_ROW_HEIGHT: 36, TOP_BAR_HEIGHT: 28 },
'../../ui/shell/geometry': { MIN_WINDOW_HEIGHT: 288, SIDEBAR_WIDTH: 64, SWITCHER_ROW_HEIGHT: 36, TOP_BAR_HEIGHT: 28,
screenWidth: () => screen.width, screenHeight: () => screen.height },
});
return { settings, load };
return { settings, screen, load };
}
/** An image that records what is drawn, checking it all lands on the image. */
@@ -50,7 +54,8 @@ test('the layout page fits four 40%-wide diagrams with captions on the immersive
const page = new LayoutPage();
assert.equal(page.immersive, true);
const image = recorder(640, 480);
page.paint(image, true);
// An immersive page's edges are the screen's: no padding.
page.paint(image, true, unbordered(640, 480).padding);
// Each diagram's outline spans the display's width at 40%.
const areas = image.calls.filter((call) => call.kind === 'outline' && call.w === 256);
assert.equal(areas.length, 4);
@@ -76,6 +81,16 @@ test('the layout page fits four 40%-wide diagrams with captions on the immersive
// Bottom, recommended, is the selected cell at first, filled while focused.
const box = image.calls.find((call) => call.kind === 'fill' && call.value === 15);
assert.ok(box.x <= areas[0].x && box.y <= areas[0].y && box.y + box.h >= bottom.y);
// With nothing bordering the page, its title sits 10px in (where list rows' text starts) and 2px down.
const title = text('Layout')[0];
assert.deepEqual([title.x, title.y], [10, 2]);
// Beside border lines the heading keeps their padding, and the grid stays centred.
const framed = recorder(640, 480);
page.paint(framed, true, bordered(640, 480).padding);
const framedTitle = framed.calls.find((call) => call.kind === 'text' && call.text === 'Layout');
assert.deepEqual([framedTitle.x, framedTitle.y], [18, 10]);
const framedAreas = framed.calls.filter((call) => call.kind === 'outline' && call.w === 256);
assert.deepEqual(framedAreas.map((area) => area.x), areas.map((area) => area.x));
});
test('the layout page starts on Bottom whatever is set, swipes in reading order, and a tap saves the choice', () => {
@@ -111,7 +126,7 @@ test('the text size page offers 12 to 18px, starts on the recommended 16px, and
'./onboarding-fonts': { onboardingFont: font },
'../../graphics/installed-fonts': { installedFontPath: (file) => `/fonts/${file}` },
'../../graphics/ttf-font': { TtfFont: { load: (path, size) => font(size === 12 ? 'title' : 'detail') } },
'../../graphics/ui-fonts': { fontSelectionLabel: () => 'Terminus', getDefaultSmallFont: () => font('detail'),
'../../graphics/ui-fonts': { defaultUiFontSize: () => 16, fontSelectionLabel: () => 'Terminus', getDefaultSmallFont: () => font('detail'),
getUiFontSelection: () => selection, setUiFontSelection: (value) => { selection = value; saved.push(value.size); },
uiFontSizeAllowed: (path, size) => size !== 13 },
'../../ui/font-picker': { FONT_SIZE_CHOICES: [8, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 24] },
@@ -134,12 +149,24 @@ test('the text size page offers 12 to 18px, starts on the recommended 16px, and
assert.deepEqual(rows.filter((texts) => texts.length > 1).length, 1);
// The size column holds the note; the preview is paragraphs of text, no menu.
const image = recorder(640, 480);
page.paint(image, true);
page.paint(image, true, unbordered(640, 480).padding);
assert.ok(menu.box.width >= 10 + '16 px'.length * 8 + 8 + 'Recommended'.length * 8);
const preview = image.calls.filter((call) => call.kind === 'text' && call.x > menu.box.x + menu.box.width);
assert.equal(preview[0].text.startsWith('This is how text will look'), true);
assert.ok(preview.length > 3);
assert.equal(image.calls.some((call) => call.kind === 'text' && call.text === 'Timers'), false);
// Unpadded, the size list starts at the window's left edge and the preview's frame reaches its right and bottom edges.
const frame = image.calls.find((call) => call.kind === 'outline');
assert.equal(menu.box.x, 0);
assert.deepEqual([frame.x + frame.w, frame.y + frame.h], [640, 480]);
// The list scrolls, so it runs to the bottom edge whatever borders it, the edge's padding inside it.
assert.deepEqual([menu.box.y + menu.box.height, menu.box.padBottom], [480, 0]);
const framed = recorder(640, 480);
page.paint(framed, true, bordered(640, 480).padding);
const paddedFrame = framed.calls.find((call) => call.kind === 'outline');
assert.equal(menu.box.x, 8);
assert.deepEqual([paddedFrame.x + paddedFrame.w, paddedFrame.y + paddedFrame.h], [632, 472]);
assert.deepEqual([menu.box.y + menu.box.height, menu.box.padBottom], [480, 8]);
// A tap saves; coming back keeps the size chosen.
page.handleInput({ type: 'scroll-up' }, { next: () => {} });
page.handleInput({ type: 'click' }, { next: () => {} });
@@ -148,6 +175,51 @@ test('the text size page offers 12 to 18px, starts on the recommended 16px, and
assert.equal(menu.selectedItem, 15);
});
test('on a RayNeo iO the text size page recommends and starts on 14px, its default', () => {
const menu = { selected: null, items: [], setItems(items, index) { this.items = items; this.selected = index; },
get selectedItem() { return this.items[this.selected] ?? null; }, paint(image, box) { this.box = box; } };
let menuOptions;
const load = loader({}, {
'./onboarding-fonts': { onboardingFont: font },
'../../graphics/installed-fonts': { installedFontPath: (file) => `/fonts/${file}` },
'../../graphics/ttf-font': { TtfFont: { load: () => font('detail') } },
'../../graphics/ui-fonts': { defaultUiFontSize: () => 14, fontSelectionLabel: () => 'Terminus', getDefaultSmallFont: () => font('detail'),
getUiFontSelection: () => ({ kind: 'ttf', file: 'Roboto-Light.ttf', size: 14 }), setUiFontSelection: () => assert.fail('nothing to save'),
uiFontSizeAllowed: () => true },
'../../ui/font-picker': { FONT_SIZE_CHOICES: [8, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 24] },
'../../ui/menu-core': { Menu: class { constructor(options) { menuOptions = options; return menu; } } },
'../../ui/metrics': { LIST_ROW_TEXT_INSET: 4, listRowHeight: (f) => f.lineHeight + 8 },
});
const { TextSizePage } = load('app/apps/onboarding/text-size-page.ts');
const page = new TextSizePage();
page.enter();
assert.equal(menu.selectedItem, 14);
const notes = menu.items.filter((size) => {
const row = recorder(200, 32);
menuOptions.draw({ image: row, item: size, x: 0, y: 0, width: 200, height: 32, selected: false });
return row.calls.some((call) => call.text === 'Recommended');
});
assert.deepEqual(notes, [14]);
// Taking the default leaves the size unset, so it keeps following the glasses.
page.handleInput({ type: 'click' }, { next: () => {} });
});
test('on a RayNeo iO the layout page recommends and starts on Left, and fits the 540x220 screen', () => {
const { load, settings } = harness('rayneo-io');
const { LayoutPage, recommendedLayout } = load('app/apps/onboarding/layout-page.ts');
assert.equal(recommendedLayout(), 'left');
const page = new LayoutPage();
const image = recorder(540, 220);
page.paint(image, true, unbordered(540, 220).padding);
const captions = image.calls.filter((call) => call.kind === 'text').map((call) => call.text);
assert.ok(captions.includes('Left (Recommended)'));
assert.ok(captions.includes('Bottom'));
page.handleInput({ type: 'click' }, { next() {} });
assert.deepEqual(settings.appSwitcherPositionSetting.writes, ['left']);
// The G2 still recommends Bottom.
assert.equal(harness().load('app/apps/onboarding/layout-page.ts').recommendedLayout(), 'bottom');
});
test('the depth page follows layouts that have depth, sits in the layout, and applies depth as it scrolls', () => {
const { load, settings } = harness();
const menu = { selected: 0, items: [], select(index) { this.selected = index; },
@@ -183,7 +255,7 @@ test('the flow switches immersive mode per page and skips pages that do not appl
const load = loader({}, {
'../../graphics/image': { GrayImage: class {} },
'../../ui/shell/in-process-window': {}, '../../ui/shell/shell': { shell }, '../../ui/sound-effects': {},
'./data-collection-page': {}, './depth-page': {}, './display-mode-page': {}, './ios-privacy-page': {}, './layout-page': {},
'./data-collection-page': {}, './depth-page': {}, './display-mode-page': {}, './layout-page': {},
'./onboarding-progress': {}, './text-size-page': {}, './welcome-page': {},
});
const { OnboardingLayer } = load('app/apps/onboarding/onboarding-app.ts');
@@ -241,9 +313,19 @@ test('the display mode page offers Full Screen only where the layout allows it,
assert.equal(menu.selectedItem, 'fullscreen');
// The selected mode's description sits beside the list, within the window (a band's).
const image = recorder(576, 260);
page.paint(image, true);
page.paint(image, true, bordered(576, 260).padding);
const description = image.calls.filter((call) => call.kind === 'text' && call.x >= menu.box.x + menu.box.width);
assert.match(description.map((call) => call.text).join(' '), /Depth is turned off/);
// Beside border lines: the title at the UI-wide (18, 10), the list's rows from the padding, the list to the bottom edge.
const title = (calls) => calls.find((call) => call.kind === 'text' && call.text === 'Display mode');
assert.deepEqual([title(image.calls).x, title(image.calls).y], [18, 10]);
assert.deepEqual([menu.box.x, menu.box.y + menu.box.height, menu.box.padBottom], [8, 260, 8]);
assert.ok(description.every((call) => call.x + call.text.length * 8 <= 576 - 18 && call.y + FONTS.detail <= 260 - 8));
// With no border on the left or bottom, the page runs to those edges.
const open = recorder(576, 260);
page.paint(open, true, { top: 8, right: 8, bottom: 0, left: 0 });
assert.deepEqual([title(open.calls).x, title(open.calls).y], [10, 10]);
assert.deepEqual([menu.box.x, menu.box.y + menu.box.height, menu.box.padBottom], [0, 260, 0]);
let next = 0;
const nav = { next: () => next++ };
page.handleInput({ type: 'scroll-up' }, nav);
+104 -1
View File
@@ -12,6 +12,7 @@ const {
isPreviouslyPaired,
glassesRow,
ringRow,
rayneoRow,
DEFAULT_STALE_AFTER_MS,
} = require("../.test-build/app/g2/pairing-candidates.js");
const { requiredArtworkFileNames, glassesImagePath, GENERIC_G2_IMAGE_PATH } = require("../.test-build/app/g2/glasses-artwork.js");
@@ -188,7 +189,7 @@ test("scan row carries the decoded variant, artwork, and proximity", () => {
assert.equal(row.colorway, "brown");
assert.equal(row.imagePath, "~/images/glasses/glasses_g2_b_brown.png");
assert.equal(row.hasVariantImage, true);
assert.match(row.proximitySummary, /In your hand|Nearby/);
assert.match(row.proximityGlyph, /^●●●[●○]$/);
assert.match(row.armsSummary, /serials match/);
assert.equal(row.canSelect, true);
assert.equal(row.isNearest, false);
@@ -289,3 +290,105 @@ test("every artwork file the resolver claims is bundled exists on disk", () => {
assert.ok(fs.existsSync(path.join(dir, name)), `missing app/images/glasses/${name}`);
}
});
test("a RayNeo iO is its own candidate, never half of a G2 pair", () => {
const aggregator = new DiscoveryAggregator();
aggregator.ingest({
address: "00:6A:5E:5F:33:C2", name: "RayNeo iO-2229", manufacturerData: "1657" + "07006a5e5f33c2130000000000000001",
rssi: -50, txPower: null, connectable: true, bonded: false, source: "scan", seenAtMs: 1000,
});
assert.equal(aggregator.pairs().length, 0);
assert.equal(aggregator.rings().length, 0);
const found = aggregator.rayneos();
assert.equal(found.length, 1);
const row = rayneoRow(found[0], { pairedAddress: "00:6a:5e:5f:33:c2" });
assert.equal(row.kind, "rayneo-io");
assert.equal(row.canSelect, true);
assert.equal(row.isPreviouslyPaired, true);
});
// ---------------------------------------------------------------------------
// Even G1
// ---------------------------------------------------------------------------
const G1_SERIAL = "S110LABC030139";
const g1Arm = (name, address, manufacturerData) => ({
address, name, manufacturerData, rssi: -50, txPower: null, connectable: true, bonded: false, source: "scan", seenAtMs: 1000,
});
const g1v0Arm = (side, address) => g1Arm(`G1v0_${side}`, address, "ffff0104" + ascii(G1_SERIAL));
test("G1 arms on g1v0 join on the frame serial and can be selected", () => {
const aggregator = new DiscoveryAggregator();
aggregator.ingest(g1v0Arm("L", "F4:27:E9:CA:E8:3F"));
aggregator.ingest(g1v0Arm("R", "CD:EB:89:BF:E9:EC"));
const [pair, ...rest] = aggregator.pairs();
assert.equal(rest.length, 0);
assert.equal(pair.completeness, "complete");
assert.equal(pair.g1Firmware, "g1v0");
assert.equal(pair.identity, null);
const row = glassesRow(pair);
assert.equal(row.modelTitle, "Even Realities G1");
assert.equal(row.canSelect, true);
assert.match(row.armsSummary, /case/);
});
test("G1 arms on current firmware join on the serial, with the arm's own name", () => {
const record = (flags) => "ffff" + "02" + flags + "000542" + ascii(G1_SERIAL);
const aggregator = new DiscoveryAggregator();
aggregator.ingest(g1Arm("G1CFW_L_EF50E5", "C0:66:16:EF:50:E5", record("05")));
aggregator.ingest(g1Arm("G1CFW_R_CBE689", "E4:46:BA:CB:E6:89", record("04")));
const [pair] = aggregator.pairs();
assert.equal(pair.g1Firmware, "g1v0");
assert.equal(pair.completeness, "complete");
assert.equal(pair.left.g1Version, "0.5.66");
assert.equal(glassesRow(pair).canSelect, true);
});
test("a G1 pair halfway through an install is one pair, shown as needing it", () => {
const aggregator = new DiscoveryAggregator();
aggregator.ingest(g1Arm("G1CFW_L_EF50E5", "C0:66:16:EF:50:E5", "ffff" + "02" + "05" + "000542" + ascii(G1_SERIAL)));
aggregator.ingest(g1Arm("Even G1_70_R_CBE689", "E4:46:BA:CB:E6:89", "01" + ascii(G1_SERIAL)));
const pairs = aggregator.pairs();
assert.equal(pairs.length, 1);
assert.equal(pairs[0].g1Firmware, "stock");
assert.equal(glassesRow(pairs[0]).canSelect, true);
});
test("G1 arms on Even's firmware can be selected, to install on", () => {
const aggregator = new DiscoveryAggregator();
aggregator.ingest(g1Arm("Even G1_70_L_EF50E5", "C0:66:16:EF:50:E5", "02" + ascii(G1_SERIAL)));
aggregator.ingest(g1Arm("Even G1_70_R_CBE689", "E4:46:BA:CB:E6:89", "01" + ascii(G1_SERIAL)));
const [pair] = aggregator.pairs();
assert.equal(pair.g1Firmware, "stock");
assert.equal(pair.completeness, "complete");
const row = glassesRow(pair);
assert.equal(row.canSelect, true);
assert.match(row.armsSummary, /install/);
});
test("a G1 pair with one arm in safe mode can be selected, to install again", () => {
const aggregator = new DiscoveryAggregator();
aggregator.ingest(g1v0Arm("L", "F4:27:E9:CA:E8:3F"));
aggregator.ingest(g1Arm("G1v0S_R", "CD:EB:89:BF:E9:EC", "ffff0104" + ascii(G1_SERIAL)));
const [pair] = aggregator.pairs();
assert.equal(pair.g1Firmware, "g1v0-safe");
assert.equal(glassesRow(pair).canSelect, true);
});
test("a G1 pair needs both arms heard, whatever it runs", () => {
const aggregator = new DiscoveryAggregator();
aggregator.ingest(g1Arm("Even G1_70_L_EF50E5", "C0:66:16:EF:50:E5", "02" + ascii(G1_SERIAL)));
const [pair] = aggregator.pairs();
assert.equal(pair.g1Firmware, "stock");
assert.equal(pair.completeness, "left-only");
assert.equal(glassesRow(pair).canSelect, false);
});
test("G2 pairs carry no G1 firmware", () => {
const aggregator = new DiscoveryAggregator();
aggregator.ingest(arm("left", "S110LABC030139", "EC:D7:82:69:3C:CB", -50));
aggregator.ingest(arm("right", "S110LABC030139", "D0:7A:47:82:09:67", -50));
const pairs = aggregator.pairs();
assert.equal(pairs.length, 1);
assert.equal(pairs[0].g1Firmware, undefined);
});
+170
View File
@@ -0,0 +1,170 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { loader } = require('./helpers/load-typescript.cjs');
const NO_LEVELS = { headset: null, headsetCharging: null, case: null, caseCharging: null, ring: null, ringCharging: null };
function fixture() {
let brightness = '40', mode = 'small', style = 'watch', picked, levels = { ...NO_LEVELS };
const changes = [], writes = [], back = [];
const settingListeners = new Set(), batteryListeners = new Set();
const subscribe = listeners => fn => { listeners.add(fn); return () => listeners.delete(fn); };
class Observable {
notifyPropertyChange(name, value) { changes.push([name, value]); }
}
const settings = {
brightnessSetting: { get: () => brightness, set: value => { brightness = value; writes.push(value); } },
displayLayoutSetting: { get: () => mode, set: value => { mode = value; }, values: ['small', 'large', 'huge'],
isDisabled: value => value === 'huge', displayValue(value = mode) { return value[0].toUpperCase() + value.slice(1); } },
phoneInputStyleSetting: { get: () => style, set: value => { style = value; } },
glassesModelName: () => 'G2',
onAnySettingChanged: subscribe(settingListeners),
};
// glasses-display-state and glasses-presence are pure, so the real ones load.
const load = loader({}, {
'@nativescript/core': { Observable, Dialogs: { action: async () => picked }, Frame: { topmost: () => ({ goBack: () => back.push(true) }) } },
'../ui/dashboard-settings': settings,
'../ui/shell/shell': { shell: { getBatteryLevels: () => levels, onBatteryLevelsChanged: subscribe(batteryListeners) } },
});
const { SettingsViewModel } = load('app/phone-ui/settings-view-model.ts');
const presence = load('app/g2/glasses-presence.ts');
return { model: new SettingsViewModel(), changes, writes, back, presence, settingListeners, batteryListeners,
brightness: () => brightness, mode: () => mode, style: () => style, pick: value => { picked = value; },
setLevels: next => { levels = { ...NO_LEVELS, ...next }; for (const fn of batteryListeners) fn(); },
changeElsewhere: fn => { fn({ setBrightness: value => { brightness = value; }, setStyle: value => { style = value; } });
for (const listener of settingListeners) listener(); } };
}
test('brightness controls snap manual levels and restore the manual value after Auto', () => {
const h = fixture();
h.model.onBrightnessChange({ value: 76 });
assert.equal(h.brightness(), '80');
h.model.onBrightnessAutoChange({ value: true });
assert.equal(h.brightness(), 'auto');
assert.equal(h.model.brightnessSliderEnabled, false);
h.model.onBrightnessChange({ value: 10 });
assert.equal(h.brightness(), 'auto');
h.model.onBrightnessAutoChange({ value: false });
assert.equal(h.brightness(), '80');
h.model.onBrightnessChange({ value: -10 });
assert.equal(h.brightness(), '2');
h.model.onBrightnessChange({ value: NaN });
assert.equal(h.brightness(), '2');
});
test('display-mode picker preserves cancellation and applies the selected mode', async () => {
const h = fixture();
h.pick('Cancel'); await h.model.onDisplayModeTap();
assert.equal(h.mode(), 'small');
// Unavailable modes (Full Screen beside a side strip) are not offered.
h.pick('Huge'); await h.model.onDisplayModeTap();
assert.equal(h.mode(), 'small');
h.pick('Large'); await h.model.onDisplayModeTap();
assert.equal(h.mode(), 'large');
assert.equal(h.model.displayModeLabel, 'Large ▾');
});
test('Phone input style picks the pad, marks the chosen button and explains it', () => {
const h = fixture();
assert.match(h.model.watchStyleClass, /segment-button-selected/);
assert.doesNotMatch(h.model.ringStyleClass, /selected/);
const watchHint = h.model.phoneInputStyleHint;
h.model.onWatchStyleTap();
assert.deepEqual(h.changes, [], 'picking the current style changes nothing');
h.model.onRingStyleTap();
assert.equal(h.style(), 'ring');
assert.match(h.model.ringStyleClass, /segment-button-selected/);
assert.doesNotMatch(h.model.watchStyleClass, /selected/);
assert.notEqual(h.model.phoneInputStyleHint, watchHint);
// Updated at once, without waiting for the store's own (next-tick) notification.
assert.deepEqual(h.changes.map(change => change[0]), ['watchStyleClass', 'ringStyleClass', 'phoneInputStyleHint']);
});
test('the page follows changes made on the glasses only while it is on screen', () => {
const h = fixture();
assert.equal(h.settingListeners.size, 0, 'nothing is subscribed before the page loads');
h.model.attach(); h.model.attach();
assert.equal(h.settingListeners.size, 1); assert.equal(h.batteryListeners.size, 1);
h.changes.length = 0;
h.changeElsewhere(remote => { remote.setBrightness('auto'); remote.setStyle('ring'); });
const changed = Object.fromEntries(h.changes);
assert.equal(changed.brightnessAuto, true);
assert.equal(changed.brightnessSliderEnabled, false);
assert.match(changed.ringStyleClass, /segment-button-selected/);
assert.ok('displayModeLabel' in changed);
h.model.detach();
assert.equal(h.settingListeners.size, 0); assert.equal(h.batteryListeners.size, 0);
// Coming back (an app resume, or returning to the page) catches up.
h.changeElsewhere(remote => remote.setBrightness('60'));
h.changes.length = 0; h.model.attach();
assert.equal(Object.fromEntries(h.changes).brightnessPercent, 60);
h.model.detach();
});
test('the battery row hides until a level is known and marks charging only over a live link', () => {
const h = fixture();
h.model.attach();
assert.equal(h.model.batteryLineVisibility, 'collapse');
h.setLevels({ headset: 90, headsetCharging: true, ring: 72 });
assert.equal(h.model.batteryLine, 'G2 90% · R1 72%');
assert.equal(h.model.batteryLineVisibility, 'visible');
h.changes.length = 0;
h.presence.updateGlassesPresence({ connected: true });
assert.equal(h.model.batteryLine, 'G2 90% ⚡ · R1 72%');
assert.deepEqual(h.changes.map(change => change[0]), ['batteryLine', 'batteryLineVisibility']);
h.model.detach();
h.presence.updateGlassesPresence({ connected: false });
assert.equal(h.model.batteryLine, 'G2 90% ⚡ · R1 72%', 'a detached page is not updated');
h.model.attach();
assert.equal(h.model.batteryLine, 'G2 90% · R1 72%');
h.model.detach();
});
test('the back arrow pops the settings page', () => {
const h = fixture();
h.model.onBackTap();
assert.deepEqual(h.back, [true]);
});
test('the settings page binds every row the view model serves and is registered for iOS', () => {
const fs = require('node:fs'), path = require('node:path');
const read = file => fs.readFileSync(path.join(__dirname, '..', file), 'utf8');
const xml = read('app/phone-ui/settings-page.xml');
for (const binding of ['displayModeLabel', 'onDisplayModeTap', 'brightnessAuto', 'onBrightnessAutoChange', 'brightnessPercent',
'onBrightnessChange', 'brightnessSliderEnabled', 'watchStyleClass', 'ringStyleClass', 'onWatchStyleTap', 'onRingStyleTap',
'phoneInputStyleHint', 'batteryLine', 'batteryLineVisibility', 'onBackTap']) {
assert.ok(xml.includes(`{{ ${binding} }}`), `settings-page.xml binds ${binding}`);
}
// iOS skips webpack's automatic page discovery, and loads app.ios.css instead of app.css.
const register = read('app/phone-ui/onboarding-register.ios.ts');
assert.match(register, /registerModule\('phone-ui\/settings-page', /);
assert.match(register, /registerModule\('phone-ui\/settings-page\.xml', /);
for (const css of ['app/app.css', 'app/app.ios.css']) assert.match(read(css), /@import '\.\/phone-ui\/settings-page\.css';/);
});
test('Phone input style is a stored setting, Watch by default, also offered in the glasses Settings app', () => {
const fs = require('node:fs'), path = require('node:path'), vm = require('node:vm'), ts = require('typescript');
const read = file => fs.readFileSync(path.join(__dirname, '..', file), 'utf8');
// The real setting object, over an in-memory settings store.
const store = new Map(), sandbox = { exports: {}, global: { isIOS: false }, setTimeout: fn => fn(),
require: id => id.includes('settings-store') ? { onSettingsStoreChanged() {},
getStringSetting: (key, fallback) => store.get(key) ?? fallback, setStringSetting: (key, value) => store.set(key, value),
getBooleanSetting: (key, fallback) => store.get(key) ?? fallback, setBooleanSetting: (key, value) => store.set(key, value) }
: { Layer: class {}, ASSISTANT_MODEL_CHOICES: [] } };
vm.runInNewContext(ts.transpileModule(read('app/ui/dashboard-settings.ts'),
{ compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 } }).outputText, sandbox);
const setting = sandbox.exports.phoneInputStyleSetting;
assert.equal(setting.label, 'Phone input style');
assert.equal(setting.get(), 'watch');
assert.deepEqual([...setting.values].map(value => setting.displayValue(value)), ['Watch', 'Ring']);
setting.set('ring');
assert.equal(store.get('phone.inputStyle'), 'ring');
assert.equal(setting.get(), 'ring');
store.set('phone.inputStyle', 'settings');
assert.equal(setting.get(), 'watch', 'an unknown stored value reads as the default');
// The on-glasses Settings app lists it with the other phone-screen settings.
const menus = read('app/ui/dashboard/settings-menus.ts');
const section = menus.slice(menus.indexOf('label: "Phone display"'), menus.indexOf('label: "Watch"'));
assert.match(section, /enumSettingMenuItem\(phoneInputStyleSetting\)/);
});
+61 -45
View File
@@ -3,62 +3,78 @@ const assert = require('node:assert/strict');
const { loader } = require('./helpers/load-typescript.cjs');
function fixture() {
let brightness = '40', mode = 'small', picked;
const changes = [], writes = [];
let style = 'watch';
const changes = [], navigations = [];
class Observable {
notifyPropertyChange(name, value) { changes.push([name, value]); }
}
const settings = {
brightnessSetting: { get: () => brightness, set: value => { brightness = value; writes.push(value); } },
displayLayoutSetting: { get: () => mode, set: value => { mode = value; }, values: ['small', 'large', 'huge'],
isDisabled: value => value === 'huge', displayValue(value = mode) { return value[0].toUpperCase() + value.slice(1); } },
};
const { RemoteControlsViewModel } = loader({}, {
'@nativescript/core': { Observable, Dialogs: { action: async () => picked } },
'../ui/dashboard-settings': settings,
'@nativescript/core': { Observable, Frame: { topmost: () => ({ navigate: entry => navigations.push(entry) }) } },
'../ui/dashboard-settings': { phoneInputStyleSetting: { get: () => style } },
})('app/phone-ui/remote-controls-view-model.ts');
return { model: new RemoteControlsViewModel(), create: () => new RemoteControlsViewModel(), changes, writes,
brightness: () => brightness, mode: () => mode, pick: value => { picked = value; } };
return { model: new RemoteControlsViewModel(), create: () => new RemoteControlsViewModel(), changes, navigations,
setStyle: value => { style = value; } };
}
test('shared controls preserve the selected tab across page visits and update all tab bindings', () => {
test('the touchpad follows the Phone input style setting and only notifies when it changes', () => {
const h = fixture();
assert.equal(h.model.watchTabVisibility, 'visible');
h.model.onRingTabTap();
assert.equal(h.model.watchTabVisibility, 'collapse');
assert.equal(h.model.ringTabVisibility, 'visible');
assert.match(h.model.ringTabClass, /tab-button-selected/);
assert.equal(h.changes.length, 6);
assert.equal(h.create().ringTabVisibility, 'visible');
h.model.onSettingsTabTap();
assert.equal(h.model.settingsTabVisibility, 'visible');
assert.equal(h.model.watchPadVisibility, 'visible');
assert.equal(h.model.ringPadVisibility, 'collapse');
// Some other setting changed: nothing to redraw, no gesture to cancel.
h.model.refreshInputStyle();
assert.deepEqual(h.changes, []);
h.setStyle('ring'); h.model.refreshInputStyle();
assert.equal(h.model.watchPadVisibility, 'collapse');
assert.equal(h.model.ringPadVisibility, 'visible');
assert.deepEqual(h.changes, [['watchPadVisibility', 'collapse'], ['ringPadVisibility', 'visible']]);
// A page built later starts on the stored style.
assert.equal(h.create().ringPadVisibility, 'visible');
});
test('shared brightness controls snap manual levels and restore the manual value after Auto', () => {
const h = fixture();
h.model.onBrightnessChange({ value: 76 });
assert.equal(h.brightness(), '80');
h.model.onBrightnessAutoChange({ value: true });
assert.equal(h.brightness(), 'auto');
assert.equal(h.model.brightnessSliderEnabled, false);
h.model.onBrightnessChange({ value: 10 });
assert.equal(h.brightness(), 'auto');
h.model.onBrightnessAutoChange({ value: false });
assert.equal(h.brightness(), '80');
h.model.onBrightnessChange({ value: -10 });
assert.equal(h.brightness(), '2');
h.model.onBrightnessChange({ value: NaN });
assert.equal(h.brightness(), '2');
test('the EvenHub phone-UI button shows only with an icon and opens that app through the host', () => {
const h = fixture(), opened = [], icon = { id: 'icon' };
h.model.showEvenHubPhoneUi = windowId => opened.push(windowId);
assert.equal(h.model.evenHubPhoneUiVisibility, 'collapse');
h.model.onEvenHubPhoneUiTap();
h.model.evenHubPhoneUi = { windowId: 'w1', title: 'Chess', icon: null };
assert.equal(h.model.evenHubPhoneUiVisibility, 'collapse');
h.model.onEvenHubPhoneUiTap();
assert.deepEqual(opened, [], 'a hidden button opens nothing');
h.changes.length = 0;
h.model.evenHubPhoneUi = { windowId: 'w1', title: 'Chess', icon };
assert.equal(h.model.evenHubPhoneUiVisibility, 'visible');
assert.equal(h.model.evenHubPhoneUiIcon, icon);
assert.equal(h.model.evenHubPhoneUiLabel, 'Open Chess on phone');
assert.deepEqual(h.changes.map(change => change[0]), ['evenHubPhoneUiIcon', 'evenHubPhoneUiVisibility', 'evenHubPhoneUiLabel']);
h.model.onEvenHubPhoneUiTap();
assert.deepEqual(opened, ['w1']);
// The host pushes a fresh object on every snapshot; an equal one is not a change.
h.changes.length = 0;
h.model.evenHubPhoneUi = { windowId: 'w1', title: 'Chess', icon };
assert.deepEqual(h.changes, []);
h.model.evenHubPhoneUi = null;
assert.equal(h.model.evenHubPhoneUiVisibility, 'collapse');
assert.equal(h.model.evenHubPhoneUiLabel, '');
});
test('shared display-mode picker preserves cancellation and applies the selected mode', async () => {
test('the gear button opens the settings page', () => {
const h = fixture();
h.pick('Cancel'); await h.model.onDisplayModeTap();
assert.equal(h.mode(), 'small');
// Unavailable modes (Full Screen beside a side strip) are not offered.
h.pick('Huge'); await h.model.onDisplayModeTap();
assert.equal(h.mode(), 'small');
h.pick('Large'); await h.model.onDisplayModeTap();
assert.equal(h.mode(), 'large');
assert.equal(h.model.displayModeLabel, 'Large ▾');
h.model.onSettingsTap();
// The entry is built in the module's own realm, so compare its field.
assert.deepEqual(h.navigations.map(entry => entry.moduleName), ['phone-ui/settings-page']);
});
test('the control bar keeps the EvenHub button leftmost and the pads bound to the input style', () => {
const xml = require('node:fs').readFileSync(require('node:path').join(__dirname, '../app/phone-ui/remote-controls.xml'), 'utf8');
const bar = xml.slice(xml.indexOf('class="control-bar"'), xml.indexOf('onControlsContentLayoutChanged'));
const order = ['onEvenHubPhoneUiTap', 'onSyntheticMicTap', 'onKeyboardTap', 'onSettingsTap'].map(name => bar.indexOf(name));
assert.ok(order.every(index => index >= 0), 'all four buttons are in the bar');
assert.deepEqual(order, [...order].sort((a, b) => a - b));
// A leading star column takes the slack, so the app button's coming and going moves no other button.
assert.match(xml, /columns="\*,auto,auto,auto,auto"[^>]*class="control-bar"/);
assert.match(xml, /visibility="\{\{ watchPadVisibility \}\}"/);
assert.match(xml, /visibility="\{\{ ringPadVisibility \}\}"/);
assert.doesNotMatch(xml, /TabVisibility|TabClass|TabTap/);
});
+87 -5
View File
@@ -15,6 +15,7 @@ function load(file, modules) {
}
const graphics = require('../.test-build/app/graphics/image.js');
const { bordered, unbordered } = require('./helpers/viewport.cjs');
const metrics = require('../.test-build/app/ui/metrics.js');
const menuCore = require('../.test-build/app/ui/menu-core.js');
const { BdfFont } = load('app/graphics/bdffont.ts', { '@nativescript/core': {} });
@@ -22,7 +23,7 @@ const font = BdfFont.parse(fs.readFileSync(path.join(__dirname, '../app/fonts/te
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 },
'./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,
@@ -34,8 +35,8 @@ const { SettingsPanelLayer } = load('app/ui/dashboard/settings-panel.ts', {
'../shell/shell': { shell: { yieldFocusToSidebar: () => yields++ } },
});
/** A panel over three sections; records which item callbacks fire. */
function panel() {
/** A panel over three sections, in a framed window unless given another; records which item callbacks fire. */
function panel(viewport = bordered(576, 260)) {
const selected = [];
const item = (label, extra = {}) => ({ label, onSelect: () => selected.push(label), ...extra });
const layer = new SettingsPanelLayer([
@@ -43,7 +44,7 @@ function panel() {
{ label: 'Voice', items: Array.from({ length: 20 }, (_, i) => item(`Voice ${i}`, { description: 'Help text for this option.' })) },
{ label: 'About', items: [], renderDetail: () => {} },
]);
const ctx = { stack: { getBaseSize: () => ({ width: 576, height: 260 }), isFocused: () => true } };
const ctx = { stack: { getBaseSize: () => viewport, isFocused: () => true } };
const input = (type) => layer.handleInput({ type, timestampMs: 0 }, ctx);
return { layer, input, paint: () => layer.paint(ctx), selected };
}
@@ -104,10 +105,91 @@ test('the selected row stays clear of its description band', async () => {
await p.input('scroll-up');
const row = selections(p.paint())[1];
const overlay = new graphics.GrayImage(576, 260);
p.layer.paintDescriptionOverlay(overlay, { stack: { getBaseSize: () => ({ width: 576, height: 260 }) } });
p.layer.paintDescriptionOverlay(overlay, { stack: { getBaseSize: () => bordered(576, 260) } });
let bandTop = -1;
for (let y = 0; y < 260 && bandTop < 0; y++) if (overlay.getPixel(300, y) !== 0) bandTop = y;
assert.ok(bandTop > 0, 'the band is drawn');
assert.ok(row.y + row.source.height <= bandTop, `row ends at ${row.y + row.source.height}, band starts at ${bandTop}`);
assert.ok(row.y + row.source.height > bandTop - 30, 'the last item scrolled down to just above the band');
});
test('a section\'s detail gives its rows the pane while focus is in them, where the two don\'t fit (a RayNeo iO\'s window)', async () => {
const docs = ['README', 'License', 'Privacy policy', 'Acknowledgements'];
const detailDraws = [];
// About's blurb under its 72px logo: about 145px.
const about = { label: 'About', items: docs.map((label) => ({ label, onSelect() {} })),
renderDetail: ({ image, x, y }) => { detailDraws.push(image); image.fillRect(x, y, 72, 72, 90); return 145; } };
for (const [size, keeps] of [[bordered(576, 260), true], [bordered(476, 192), false], [bordered(476, 155), false]]) {
const layer = new SettingsPanelLayer([about]);
const ctx = { stack: { getBaseSize: () => size, isFocused: () => true } };
const paint = () => { detailDraws.length = 0; const image = layer.paint(ctx); return { image, drawn: detailDraws.includes(image) }; };
// The preview (focus in the left column) always shows the detail.
assert.equal(paint().drawn, true, `preview at ${size.height}`);
await layer.handleInput({ type: 'click', timestampMs: 0 }, ctx);
const { image, drawn } = paint();
assert.equal(drawn, keeps, `focused at ${size.height}`);
const row = selections(image)[1];
if (keeps) assert.ok(row.y >= 8 + 145, 'the rows under the detail');
else assert.equal(row.y, 8, 'the first row at the top of the pane');
assert.ok(row.y + row.source.height <= size.height - 8);
}
});
test('the columns keep the padding suggested for each window edge, and scroll right up to the top and bottom', async () => {
// Where the left column's first-row highlight (a retained selection) starts.
const first = (viewport) => { const row = selections(panel(viewport).paint())[0]; return [row.x, row.y]; };
assert.deepEqual(first(bordered(576, 260)), [8, 8], 'beside border lines, 8px in');
assert.deepEqual(first(unbordered(576, 260)), [0, 0], 'beside blank space, right at the edges');
// The right column ends at the right edge's padding.
for (const [viewport, right] of [[bordered(576, 260), 568], [unbordered(576, 260), 576]]) {
const p = panel(viewport);
await p.input('click');
const row = selections(p.paint())[1];
assert.equal(row.x + row.source.width, right);
}
// A long list scrolled to its middle is cut off at the window's own top
// and bottom edges, not at the padding: a row starts at y 0.
const p = panel(bordered(576, 260));
await p.input('scroll-down');
await p.input('click');
for (let i = 0; i < 10; i++) await p.input('scroll-down');
const image = p.paint().withDrawsBaked();
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']);
});
+3 -3
View File
@@ -15,8 +15,8 @@ function worker() {
const messages = [];
const file = 'app/apps/terminal/terminal-app.worker.ts';
const source = ts.createSourceFile(file, read(file), ts.ScriptTarget.Latest, true);
const functions = new Set(['recencyKey', 'post', 'renderHubWindows', 'isSessionActive', 'isWindowSessionActive', 'hubAnimationShouldRun', 'syncWindowIconActivity', 'updateHubAnimation', 'noteSessionActivity', 'openWindow', 'closeWindow']);
const variables = new Set(['windows', 'pendingViews', 'activeViewId', 'sessionRecency', 'sessionActivity', 'ACTIVITY_ACTIVE_MS', 'HUB_ANIMATION_STEP_MS', 'hubAnimationPhase', 'hubAnimationTimer', 'windowIconActivity', 'screenOn']);
const functions = new Set(['recencyKey', 'post', 'stopViewConnection', 'renderHubWindows', 'isSessionActive', 'isWindowSessionActive', 'hubAnimationShouldRun', 'syncWindowIconActivity', 'updateHubAnimation', 'noteSessionActivity', 'openWindow', 'closeWindow']);
const variables = new Set(['windows', 'pendingViews', 'pendingHeightChanges', 'activeViewId', 'sessionRecency', 'sessionActivity', 'ACTIVITY_ACTIVE_MS', 'HUB_ANIMATION_STEP_MS', 'hubAnimationPhase', 'hubAnimationTimer', 'windowIconActivity', 'screenOn']);
const selected = source.statements.filter(s =>
ts.isFunctionDeclaration(s) && functions.has(s.name?.text) ||
ts.isVariableStatement(s) && s.declarationList.declarations.some(d => variables.has(d.name.getText(source))) ||
@@ -149,7 +149,7 @@ test('host applies activity only to its own open windows and cleans up on close'
'../../graphics/image': { GrayImage: Image },
'./chrome-layer': { windowIcon: (icon, letter, glyph, phase) => phase },
'../../assistant/tool-registry': { toolRegistry: { removeAppTools() {} } },
'./geometry': { appViewportSize: () => ({ width: 576, height: 260 }) },
'./geometry': { appViewport: () => ({ width: 576, height: 260 }) },
'../../native/frame-timings': {},
'./worker-state': {},
'./shell': { shell: { registerWindow: w => shellWindows.set(w.windowId, w) } },
+30 -10
View File
@@ -1,30 +1,50 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { HUB_TITLE_X, layoutHubHeader } = require('../.test-build/app/apps/terminal/hub-header.js');
const { HUB_TITLE_INSET_X, layoutHubHeader } = require('../.test-build/app/apps/terminal/hub-header.js');
const { bordered, unbordered, edged } = require('./helpers/viewport.cjs');
const font = size => ({ measureText: text => [...text].length * size, getGlyph: () => ({ dwidthX: size }) });
// Framed all round: the title 8px in plus its inset, and 2px below the top padding.
const TITLE_X = 8 + HUB_TITLE_INSET_X;
test('terminal short status stays inline; exact-fit status does not move the list', () => {
const f = font(7), title = 'Terminal', status = 'Connected.';
const width = HUB_TITLE_X + f.measureText(title) + 16 + f.measureText(status) + 12;
const header = layoutHubHeader(f, title, status, width, 18);
const width = TITLE_X + f.measureText(title) + 16 + f.measureText(status) + 8;
const header = layoutHubHeader(f, title, status, bordered(width, 260), 18);
assert.deepEqual([header.titleX, header.titleY], [TITLE_X, 10]);
assert.equal(header.y, 10); assert.equal(header.listTop, 34); assert.deepEqual(header.lines, [status]);
assert.equal(layoutHubHeader(f, title, status, width - 1, 18).y, 28);
assert.equal(layoutHubHeader(f, title, status, bordered(width - 1, 260), 18).y, 28);
});
test('terminal long error wraps in full-width block and moves menu below all lines at each font size', () => {
const status = 'Connection failed. The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.';
for (const size of [5, 7, 12, 20]) for (const width of [320, 576, 640]) {
const f = font(size), step = size * 2;
const header = layoutHubHeader(f, 'Terminal - Connections', status, width, step);
assert.equal(header.x, HUB_TITLE_X); assert.equal(header.y, 10 + step);
const header = layoutHubHeader(f, 'Terminal - Connections', status, bordered(width, 260), step);
assert.equal(header.x, TITLE_X); assert.equal(header.y, 10 + step);
assert.equal(header.lines.join(' '), status);
assert.ok(header.lines.every(line => f.measureText(line) <= width - HUB_TITLE_X - 12));
assert.ok(header.lines.every(line => f.measureText(line) <= width - TITLE_X - 8));
assert.ok(header.listTop >= header.y + header.lines.length * step);
assert.equal(header.listTop, 16 + step * (1 + header.lines.length));
}
});
test('terminal explicit newlines and long unbroken messages remain complete', () => {
const h = layoutHubHeader(font(7), 'Terminal', 'First\nSecond', 640, 18);
assert.equal(h.x, HUB_TITLE_X); assert.deepEqual(h.lines, ['First', 'Second']);
const h = layoutHubHeader(font(7), 'Terminal', 'First\nSecond', bordered(640, 260), 18);
assert.equal(h.x, TITLE_X); assert.deepEqual(h.lines, ['First', 'Second']);
const word = 'x'.repeat(200);
const wrapped = layoutHubHeader(font(7), 'Terminal', word, 320, 18);
const wrapped = layoutHubHeader(font(7), 'Terminal', word, bordered(320, 260), 18);
assert.equal(wrapped.lines.join(''), word);
});
test('terminal header keeps the padding of just the edges a border runs along', () => {
const f = font(7), title = 'Terminal', status = 'Connected.';
// Nothing bordered: the title starts its inset from the left edge, at the top edge's offset.
const open = layoutHubHeader(f, title, status, unbordered(576, 260), 18);
assert.deepEqual([open.titleX, open.titleY, open.y, open.listTop], [HUB_TITLE_INSET_X, 2, 2, 26]);
// An inline status may run right up to an unbordered right edge, but stops 8px short of a bordered one.
const fit = HUB_TITLE_INSET_X + f.measureText(title) + 16 + f.measureText(status);
assert.equal(layoutHubHeader(f, title, status, unbordered(fit, 260), 18).lines.length, 1);
assert.equal(layoutHubHeader(f, title, status, unbordered(fit, 260), 18).y, 2);
assert.equal(layoutHubHeader(f, title, status, edged(fit, 260, 'right'), 18).y, 2 + 18);
// Each edge on its own.
const topLeft = layoutHubHeader(f, title, 'First\nSecond', edged(576, 260, 'top', 'left'), 18);
assert.deepEqual([topLeft.titleX, topLeft.titleY, topLeft.x, topLeft.y], [TITLE_X, 10, TITLE_X, 28]);
const bottom = layoutHubHeader(f, title, 'First\nSecond', edged(576, 260, 'bottom'), 18);
assert.deepEqual([bottom.titleX, bottom.titleY, bottom.listTop], [HUB_TITLE_INSET_X, 2, 8 + 18 * 3]);
});
+523
View File
@@ -0,0 +1,523 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { loader } = require('./helpers/load-typescript.cjs');
const { bordered, unbordered } = require('./helpers/viewport.cjs');
const HUB = 'terminal:hub';
// Framed windows: the shell's messages carry what each edge touches and its padding.
const MIN = bordered(552, 260);
const MAX = bordered(552, 452);
const font = { lineHeight: 12, measureText: (text) => text.length * 6, getGlyph: () => null };
const tick = () => new Promise((resolve) => setTimeout(resolve, 0));
// A recording stand-in for the worker's images: enough to see what a frame shows.
function fakeImageModule() {
class GrayImage {
constructor(width, height) {
this.width = width;
this.height = height;
this.texts = [];
this.positions = [];
return new Proxy(this, { get: (target, key) => (key in target ? target[key] : () => {}) });
}
drawText(_font, x, y, text) { this.texts.push(text); this.positions.push([text, x, y]); }
drawMenuSelection(row) { this.texts.push(...row.texts.map((text) => `[${text}]`)); }
}
return { GrayImage };
}
// Runs the real worker against the shell's message protocol, stubbing only what
// sits behind it: the websocket client, the emulator, the display and the menu host.
function terminal({ sessions = ['vim', 'top'], presets = '', bellWake = true } = {}) {
const replies = [], clients = [], frames = [], menus = [];
const global = { postMessage: (message) => replies.push(message) };
class FakeClient {
constructor(options) {
this.options = options;
this.stopped = false;
this.attached = null;
this.sent = [];
this.listeners = { state: [], bell: [], activity: [] };
this.statusText = 'Connected.';
clients.push(this);
}
state() { return { phase: this.stopped ? 'idle' : 'connected', status: this.statusText, serverName: 'host', sessions: sessions.map((title) => ({ socket: title, title, cwdHint: '' })) }; }
start() { this.stopped = false; this.listeners.state.forEach((listener) => listener(this.state())); }
stop() { this.stopped = true; }
setViewport(cols, rows) { this.viewport = [cols, rows]; }
connectSession(socket) { this.attached = socket; }
sendInput(text) { this.sent.push(text); }
submitInput(text) { this.sent.push(text); }
listSessions() {}
view() {}
requestHistory() {}
onStateChange(listener) { this.listeners.state.push(listener); return () => {}; }
onBell(listener) { this.listeners.bell.push(listener); return () => {}; }
onTitle() { return () => {}; }
onActivity(listener) { this.listeners.activity.push(listener); return () => {}; }
onSnapshot() { return () => {}; }
onHistoryLines() { return () => {}; }
onSessionAttached() { return () => {}; }
onTerminalData() { return () => {}; }
onSessionDetached() { return () => {}; }
}
class FakeEmulator {
dispose() { this.disposed = true; }
write() {}
reset() {}
bufferLength() { return 0; }
lineAt() { return ''; }
cursorRow() { return 0; }
cursorCol() { return 0; }
}
// Click runs the first item, the way the real menu does at its initial selection.
class FakeWindowMenu {
constructor(options) { this.options = options; this.opened = null; menus.push(this); }
isOpen() { return this.opened !== null; }
open(items = this.options.items()) { this.opened = items; }
close() { this.opened = null; }
resize(size) { this.options.size = size; }
paint() { this.options.items(); return [this.options.paintBase()]; }
async handleInput(event) {
if (event.type === 'click') {
await this.opened[0].onSelect({ stack: { pop: () => { this.opened = null; }, push() {} } });
}
}
}
const setting = (value) => ({ id: 'setting', get: () => value, set() {} });
const image = fakeImageModule();
const load = loader({ global, setTimeout, clearTimeout, setInterval, clearInterval }, {
'@nativescript/core/globals': {},
'./background': { hasTerminalBackgroundWork: () => false },
'../../ui/shell/worker-lifecycle': { finishWorkerShutdown() {} },
'../../graphics/image': image,
'../graphics/image': image,
'../../graphics/plane': {
planesFingerprint: (planes) => JSON.stringify([planes[0].width, planes[0].height, planes[0].texts, planes[0].positions]),
flattenPlanesWithDraws: (planes) => ({ image: { ...planes[0], width: planes[0].width, height: planes[0].height, texts: planes[0].texts, to8bppBuffer: () => ({ buffer: planes[0] }) }, draws: [] }),
},
'../../graphics/glyph-wire': { prepareFrameDraws: () => null },
'../../graphics/ui-fonts': {
getDefaultSmallFont: () => font,
getTerminalFontConfig: () => ({ font, cellWidth: 6, cellHeight: 12 }),
},
'../../graphics/icons': { TERMINAL_ICON_GLYPHS: ['1', '2', '3'] },
'../../native/frame-timings': {
startFrame: () => 1, finishFrame() {}, logFrame() {},
span: (_frame, _name, run) => run(), runWithFrame: (_frame, run) => run(),
},
'../../native/active-display': {
getActiveDisplay: () => ({
submitSurfaceFrame: ({ texts, positions }, surfaceId, _x, _y, width, height) => frames.push({ surfaceId, width, height, texts, positions }),
}),
},
'../../ui/gestures': { GESTURE_DOUBLE_CLICK: 'double-tap' },
'../../native/g2mirror-client': { G2MirrorClient: FakeClient },
'../../native/settings-store': { onSettingsStoreChanged() {} },
'../../ui/dashboard-settings': {
enumSettingMenuItem() {}, textSettingMenuItem() {}, toggleSettingMenuItem() {},
terminalAutoReconnectSetting: setting(false),
terminalDisplayLayoutSetting: setting('default'),
terminalLaunchPresetsSetting: setting(presets),
terminalNewConnectionSetting: setting(''),
terminalWakeOnBellSetting: setting(bellWake),
},
'../../ui/font-picker': { terminalFontPickerMenuItem() {} },
'./connections': {
TERMINAL_CONNECTIONS_KEY: 'terminal.connections',
connectionDisplayName: (config) => config.id,
loadConnections: () => [{ id: 'c1', url: 'ok', serverName: 'host', enabled: true }],
parseConnectionString: () => ({ host: 'host' }),
saveConnections() {}, updateConnection() {},
},
'./terminal-emulator': { TerminalEmulator: FakeEmulator },
'../../ui/menu': { drawSubmenuIndicator() {}, submenuItem: (label, onSelect) => ({ label, onSelect }) },
'../../ui/window-menu': { WindowMenu: FakeWindowMenu, WindowMenuLayer: class {} },
'../../ui/shell/geometry': { appViewportSize: () => MAX },
});
load('app/apps/terminal/terminal-app.worker.ts');
const send = (message) => global.onmessage({ data: message });
const input = (type, windowId = HUB) => send({ type: 'input', windowId, event: { type }, frameId: 0, focused: true });
const openWindow = (windowId, viewport) => {
send({ type: 'open-window', windowId, surfaceId: `window:${windowId}`, title: 'Terminal', viewport });
send({ type: 'foreground', windowId, foreground: true, focused: true });
};
const repliesOf = (type) => replies.filter((reply) => reply.type === type);
return {
clients, frames, replies, menus, send, input, openWindow, repliesOf,
lastFrame: () => frames[frames.length - 1],
// Where the last frame drew a line of text, as [x, y].
textAt: (text) => frames[frames.length - 1].positions.find((entry) => entry[0] === text).slice(1),
clearReplies: () => { replies.length = 0; },
openMenu: (windowId = HUB) => input('short-then-long-press', windowId),
menuLabels: () => Array.from(menus.find((menu) => menu.isOpen()).opened, (item) => item.label),
closeMenu: () => menus.forEach((menu) => menu.close()),
// Past Manage Connections and Settings to the first session row.
selectFirstSession: () => { input('scroll-down'); input('scroll-down'); },
answerHeight: (viewport, windowId = HUB) => send({ type: 'resize-window', windowId, viewport }),
};
}
function hubShowingList() {
const t = terminal();
t.openWindow(HUB, MIN);
t.selectFirstSession();
return t;
}
function hubShowingSession() {
const t = hubShowingList();
t.input('click');
t.answerHeight(MAX);
t.clearReplies();
return t;
}
test('selecting a session shows it in the same window, at the height the shell answers with', () => {
const t = hubShowingList();
assert.equal(t.clients.length, 1, 'just the control connection so far');
assert.ok(t.lastFrame().texts.includes('[vim]') || t.lastFrame().texts.some((text) => text.includes('vim')));
t.input('click');
assert.deepEqual(t.repliesOf('open-window-request'), [], 'no new window');
assert.deepEqual(t.repliesOf('set-height-mode').map((reply) => reply.heightMode), ['max']);
assert.equal(t.clients.length, 1, 'no connection before the size is known');
t.answerHeight(MAX);
const view = t.clients[1];
assert.deepEqual([view.options.cols, view.options.rows], [92, 37], 'grid from the answered viewport');
assert.equal(view.attached, 'vim');
assert.deepEqual([t.lastFrame().surfaceId, t.lastFrame().width, t.lastFrame().height], [`window:${HUB}`, MAX.width, MAX.height]);
assert.ok(!t.lastFrame().texts.includes('Terminal'), 'the list is gone');
});
test('a repeated tap before the shell answers starts one view', () => {
const t = hubShowingList();
t.input('click');
t.input('click');
assert.equal(t.repliesOf('set-height-mode').length, 1);
t.answerHeight(MAX);
assert.equal(t.clients.length, 2);
});
test('text input reaches the session shown in place', () => {
const t = hubShowingSession();
t.send({ type: 'text-input', windowId: HUB, text: 'ls' });
assert.deepEqual(t.clients[1].sent, ['ls']);
});
test('double-tap in a session shown in place goes back to the list, not the sidebar', () => {
const t = hubShowingSession();
t.input('double-click');
assert.deepEqual(t.repliesOf('yield-focus'), []);
assert.deepEqual(t.repliesOf('set-height-mode').map((reply) => reply.heightMode), ['min']);
assert.equal(t.clients[1].stopped, false, 'the session stays up until the window has resized');
t.answerHeight(MIN);
assert.equal(t.clients[1].stopped, true);
assert.deepEqual([t.lastFrame().width, t.lastFrame().height], [MIN.width, MIN.height]);
assert.ok(t.lastFrame().texts.includes('Terminal'), 'the list is back');
assert.ok(t.lastFrame().texts.includes('[vim]'), 'with the session just viewed still selected');
t.input('double-click');
assert.deepEqual(t.repliesOf('yield-focus').map((reply) => reply.windowId), [HUB], 'then double-tap yields as usual');
});
test('coming back from a session keeps the list order despite its bumped recency, until the list next becomes visible', () => {
const t = hubShowingList();
const sessionRows = () => t.lastFrame().texts.filter((text) => /vim|top/.test(text));
t.input('scroll-down');
assert.deepEqual(sessionRows(), ['vim', '[top]']);
t.input('click');
t.answerHeight(MAX);
t.clients[0].listeners.activity.forEach((listener) => listener('top'));
t.input('double-click');
t.answerHeight(MIN);
assert.deepEqual(sessionRows(), ['vim', '[top]']);
t.send({ type: 'foreground', windowId: HUB, foreground: false, focused: false });
t.send({ type: 'foreground', windowId: HUB, foreground: true, focused: true });
assert.deepEqual(sessionRows().map((text) => text.replace(/[[\]]/g, '')), ['top', 'vim'], 're-sorted as usual');
});
test('the session just viewed stays selected when rows above it have shifted', () => {
const sessions = ['vim', 'top', 'htop'];
const t = terminal({ sessions });
t.openWindow(HUB, MIN);
t.selectFirstSession();
t.input('scroll-down');
t.input('click');
t.answerHeight(MAX);
// vim exits while top is on show, moving top's row up one (and htop into its place).
sessions.shift();
t.clients[0].listeners.state.forEach((listener) => listener(t.clients[0].state()));
t.input('double-click');
t.answerHeight(MIN);
assert.ok(t.lastFrame().texts.includes('[top]'));
});
test('the window menu leads with Open in new window only while a session is selected', () => {
const t = terminal();
t.openWindow(HUB, MIN);
t.openMenu();
assert.equal(t.menuLabels()[0], 'Settings', 'Manage Connections is selected');
t.closeMenu();
t.input('scroll-down');
t.openMenu();
assert.equal(t.menuLabels()[0], 'Settings', 'Settings is selected');
t.closeMenu();
t.input('scroll-down');
t.openMenu();
assert.deepEqual(t.menuLabels().slice(0, 2), ['Open in new window', 'Settings']);
});
test('Open in new window on a selected session opens a window and leaves the list up', async () => {
const t = hubShowingList();
t.openMenu();
t.input('click');
await tick();
assert.deepEqual(t.repliesOf('set-height-mode'), []);
const [request] = t.repliesOf('open-window-request');
assert.equal(request.title, 'vim');
assert.equal(request.heightMode, 'max');
assert.equal(request.focus, true);
assert.equal(t.repliesOf('open-window-request').length, 1);
});
test('selecting a session already in its own window focuses that window', async () => {
const t = hubShowingList();
t.openMenu();
t.input('click');
await tick();
const { windowId } = t.repliesOf('open-window-request')[0];
t.send({ type: 'foreground', windowId: HUB, foreground: false, focused: false });
t.openWindow(windowId, MAX);
t.send({ type: 'foreground', windowId: HUB, foreground: true, focused: true });
t.clearReplies();
t.input('click');
assert.deepEqual(t.repliesOf('focus-window').map((reply) => reply.windowId), [windowId]);
assert.deepEqual(t.repliesOf('set-height-mode'), []);
});
test('a session in place can move to a new window: the list comes back first, then the window opens', async () => {
const t = hubShowingSession();
t.openMenu();
assert.deepEqual(t.menuLabels().slice(0, 2), ['Open in new window', 'Settings']);
t.input('click');
await tick();
assert.deepEqual(t.repliesOf('set-height-mode').map((reply) => reply.heightMode), ['min']);
assert.deepEqual(t.repliesOf('open-window-request'), [], 'not while the old connection is up');
t.answerHeight(MIN);
assert.equal(t.clients[1].stopped, true);
const [request] = t.repliesOf('open-window-request');
assert.equal(request.title, 'vim');
assert.equal(request.iconGlyph, '1');
assert.ok(t.lastFrame().texts.includes('Terminal'));
});
test('a session in its own window keeps its plain menu and double-tap yields focus', async () => {
const t = hubShowingList();
t.openMenu();
t.input('click');
await tick();
const { windowId } = t.repliesOf('open-window-request')[0];
t.openWindow(windowId, MAX);
t.openMenu(windowId);
assert.equal(t.menuLabels()[0], 'Settings');
t.closeMenu();
t.input('double-click', windowId);
assert.deepEqual(t.repliesOf('yield-focus').map((reply) => reply.windowId), [windowId]);
});
test('a bell still finds the window while the hub is showing a session', () => {
const t = hubShowingSession();
// A session with no view of its own wakes to the hub's window.
t.clients[0].listeners.bell.forEach((listener) => listener('top', Date.now() + 1000));
assert.deepEqual(t.repliesOf('wake-window').map((reply) => reply.windowId), [HUB]);
t.clearReplies();
// The session on show: flagged only while the window is in the background.
t.send({ type: 'foreground', windowId: HUB, foreground: false, focused: false });
t.clients[0].listeners.bell.forEach((listener) => listener('vim', Date.now() + 2000));
assert.deepEqual(t.repliesOf('set-attention').map((reply) => reply.windowId), [HUB]);
});
test('a window backgrounded while waiting for the shell picks up the session without painting', () => {
const t = hubShowingList();
t.input('click');
t.send({ type: 'foreground', windowId: HUB, foreground: false, focused: false });
const painted = t.frames.length;
t.answerHeight(MAX);
assert.equal(t.frames.length, painted);
t.send({ type: 'foreground', windowId: HUB, foreground: true, focused: true });
assert.equal(t.lastFrame().height, MAX.height);
});
test('a list repaint scheduled before the swap does not paint over the session', async () => {
const sessions = ['vim', 'top'];
const t = terminal({ sessions });
t.openWindow(HUB, MIN);
t.selectFirstSession();
t.input('click');
// A new session makes the list's next paint differ, so it would be submitted.
sessions.push('htop');
t.clients[0].listeners.state.forEach((listener) => listener(t.clients[0].state()));
t.answerHeight(MAX);
await new Promise((resolve) => setTimeout(resolve, 80));
assert.equal(t.lastFrame().height, MAX.height);
assert.ok(!t.lastFrame().texts.includes('Terminal'));
});
test('a session repaint scheduled before the swap does not paint over the list', async () => {
const t = hubShowingSession();
t.input('double-click');
t.clients[1].statusText = 'Reconnecting...';
t.clients[1].listeners.state.forEach((listener) => listener(t.clients[1].state()));
t.answerHeight(MIN);
await new Promise((resolve) => setTimeout(resolve, 80));
assert.equal(t.lastFrame().height, MIN.height);
assert.ok(t.lastFrame().texts.includes('Terminal'));
});
test('closing the window while the shell has not answered drops the change', () => {
const t = hubShowingList();
t.input('click');
t.send({ type: 'close-window', windowId: HUB });
t.answerHeight(MAX);
assert.equal(t.clients.length, 1, 'no view connection started');
});
test('closing the window while it shows a session stops that session', () => {
const t = hubShowingSession();
t.send({ type: 'close-window', windowId: HUB });
assert.equal(t.clients[1].stopped, true);
});
test('a later resize of a session in place reconnects at the new grid, and the list then gets its own size', () => {
const t = hubShowingSession();
t.answerHeight(bordered(552, 400));
assert.equal(t.clients[1].stopped, false);
assert.deepEqual(t.clients[1].viewport, [92, 33]);
t.input('double-click');
t.answerHeight(bordered(552, 240));
assert.equal(t.lastFrame().height, 240);
});
test('the list lays out inside the padding of its window\'s bordered edges, and follows a change in them', async () => {
const t = terminal();
t.openWindow(HUB, MIN);
// The title lines up with the rows' text: 6px inside their boxes, which start at the left padding.
assert.deepEqual(t.textAt('Terminal'), [8 + 6, 8 + 2]);
assert.deepEqual(t.textAt('Connected.'), [8 + 6 + 'Terminal'.length * 6 + 16, 8 + 2]);
// The window menu's stack gets the whole viewport, for the pages it shows.
const plain = (value) => JSON.parse(JSON.stringify(value));
assert.deepEqual(plain(t.menus[0].options.size), plain(MIN));
// Same size, nothing bordering it any more (Border: None): a repaint with no padding.
const painted = t.frames.length;
t.send({ type: 'resize-window', windowId: HUB, viewport: unbordered(552, 260) });
await new Promise((resolve) => setTimeout(resolve, 80));
assert.equal(t.frames.length, painted + 1);
assert.deepEqual(t.textAt('Terminal'), [6, 2]);
assert.deepEqual(plain(t.menus[0].options.size), plain(unbordered(552, 260)));
});
test('a session keeps its connection and grid when only its window\'s edges change', async () => {
const t = hubShowingSession();
const painted = t.frames.length;
t.send({ type: 'resize-window', windowId: HUB, viewport: unbordered(552, 452) });
await new Promise((resolve) => setTimeout(resolve, 80));
assert.equal(t.clients[1].stopped, false);
assert.equal(t.clients[1].viewport, undefined, 'no new grid negotiated');
assert.equal(t.clients.length, 2);
assert.equal(t.frames.length, painted);
// Back in the list, the hub takes the padding the shell answers with.
t.input('double-click');
t.answerHeight(unbordered(552, 260));
assert.deepEqual(t.textAt('Terminal'), [6, 2]);
});
test('the shell host resizes the surface before answering with the new viewport', async () => {
const worker = { posted: [], postMessage(message) { this.posted.push(message); } };
const surfaces = [];
const windows = [];
let renders = 0;
let foreground;
const load = loader({ global: { isIOS: false }, setTimeout, clearTimeout, Promise }, {
'../../graphics/image': {},
'./chrome-layer': { windowIcon() {} },
'../../assistant/tool-registry': { toolRegistry: { removeAppTools() {} } },
'./geometry': { appViewport: (mode) => (mode === 'max' ? MAX : MIN) },
'../../native/frame-timings': {},
'./worker-state': {},
'./shell': { shell: {
registerWindow: (window) => windows.push(window),
getWindows: () => windows,
foregroundWindow: () => foreground,
isWindowFocused: () => true,
} },
});
const { WorkerAppHost } = load('app/ui/shell/worker-window.ts');
let finishConfigure = () => {};
const host = new WorkerAppHost({
appId: 'terminal', worker,
configureSurface: (surfaceId, visible, heightMode) => {
surfaces.push({ surfaceId, visible, heightMode });
return heightMode === 'min' ? Promise.resolve() : new Promise((resolve) => { finishConfigure = resolve; });
},
requestShellRender: () => { renders++; },
removeSurface() {},
});
worker.onmessage({ data: { type: 'worker-ready' } });
const window = host.openWindow({ windowId: HUB, title: 'Terminal', iconLetter: 'T' });
foreground = window;
await tick();
worker.posted.length = 0;
renders = 0;
worker.onmessage({ data: { type: 'set-height-mode', windowId: HUB, heightMode: 'max' } });
assert.equal(window.heightMode, 'max', 'the chrome reads the band from here');
const configured = surfaces[surfaces.length - 1];
assert.equal(configured.heightMode, 'max');
assert.equal(configured.visible(), true, 'asked late: the window may have lost the foreground');
foreground = undefined;
assert.equal(configured.visible(), false);
foreground = window;
await tick();
assert.deepEqual(worker.posted, [], 'no answer before the surface is ready');
finishConfigure();
await tick();
assert.deepEqual(worker.posted.map((message) => [message.type, message.viewport]), [['resize-window', MAX]]);
assert.equal(renders, 1);
// Any later relayout (a switcher height change) keeps the new band.
worker.posted.length = 0;
window.relayout();
assert.deepEqual(worker.posted.map((message) => message.viewport), [MAX]);
// Unchanged band: still answered, so the worker can always wait on it.
worker.posted.length = 0;
worker.onmessage({ data: { type: 'set-height-mode', windowId: HUB, heightMode: 'min' } });
await tick();
worker.onmessage({ data: { type: 'set-height-mode', windowId: HUB, heightMode: 'min' } });
await tick();
assert.deepEqual(worker.posted.map((message) => message.viewport), [MIN, MIN]);
// A closed window is not touched again.
window.close();
const count = surfaces.length;
worker.onmessage({ data: { type: 'set-height-mode', windowId: HUB, heightMode: 'max' } });
await tick();
assert.equal(surfaces.length, count);
});
+68 -1
View File
@@ -2,9 +2,13 @@ const test = require('node:test');
const assert = require('node:assert/strict');
const { loader } = require('./helpers/load-typescript.cjs');
const load = loader();
const { TranscriptModel, TranscriptScroll, wrapTranscribeText } = load('app/apps/transcribe/transcript-model.ts');
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');
const plain = value => JSON.parse(JSON.stringify(value));
function pcm(amplitude, ms = 50) {
const bytes = new Uint8Array(ms * 16 * 2);
@@ -82,6 +86,39 @@ test('short transcripts become scrollable without disabling automatic following'
assert.equal(s.layout(12, 10), 2);
});
test('the transcript lines up with the header inside the side padding and scrolls to the bottom edge', () => {
// 16px lines stepping 20, under a header that ends at y=38.
const framed = transcriptLayout(bordered(576, 260), 38, 16, 20);
assert.deepEqual(plain(framed), {
// In line with the header's title at the left padding; the right keeps the scrollbar's gutter.
textX: 8, textWidth: 576 - 8 - 8 - 8, pageLines: 10, drawnLines: 11,
scrollbar: { x: 576 - 8 - 2, y: 38, width: 2, height: 260 - 8 - 38 },
});
// Following the tail, the last line rests above the bottom padding; scrolled
// back, one more line fits above the window's edge itself.
const lineBottom = (firstLine, total) => 38 + (Math.min(total, firstLine + framed.drawnLines) - 1 - firstLine) * 20 + 16;
const s = new TranscriptScroll();
assert.equal(s.layout(40, framed.pageLines), 30);
assert.equal(lineBottom(30, 40), 234);
s.scroll(-3);
assert.equal(s.layout(40, framed.pageLines), 27);
assert.equal(lineBottom(27, 40), 254);
assert.ok(lineBottom(27, 40) > 260 - 8 && lineBottom(27, 40) <= 260);
// With nothing bordering the window, the column and the page gain the padding's room.
assert.deepEqual(plain(transcriptLayout(unbordered(576, 260), 38, 16, 20)), {
textX: 0, textWidth: 576 - 8, pageLines: 11, drawnLines: 11,
scrollbar: { x: 574, y: 38, width: 2, height: 222 },
});
const leftOnly = transcriptLayout(edged(576, 260, 'left'), 38, 16, 20);
assert.deepEqual([leftOnly.textX, leftOnly.textWidth, leftOnly.scrollbar.x], [8, 576 - 8 - 8, 574]);
// The scrollbar never stands over the text column.
for (const layout of [framed, leftOnly, transcriptLayout(bordered(476, 155), 38, 16, 20)]) {
assert.ok(layout.textX + layout.textWidth < layout.scrollbar.x);
assert.ok(layout.pageLines >= 1 && layout.drawnLines >= layout.pageLines);
}
});
test('pause detector uses 1.5 seconds of PCM, fires once per pause, and ignores leading silence', () => {
const d = new SpeechPauseDetector();
for (let i = 0; i < 60; i++) assert.equal(d.accept(pcm(0)), false);
@@ -92,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);
});
+20
View File
@@ -16,15 +16,25 @@
"../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/apps/terminal/terminal-keys.ts",
"../app/phone-ui/ble-bandwidth-meter.ts",
"../app/apps/terminal/hub-header.ts",
"../app/g2/ios-peripheral-identity.ts",
"../app/graphics/image.ts",
"../app/graphics/textwrap.ts",
"../app/graphics/lvgl-wrap.ts",
"../app/graphics/battery.ts",
"../app/graphics/plane.ts",
"../app/graphics/shell-scene.ts",
"../app/ui/metrics.ts",
"../app/ui/viewport.ts",
"../app/ui/menu-core.ts",
"../app/phone-ui/phone-gestures.ts",
"../app/native/java-direct-buffer.ts",
@@ -39,9 +49,16 @@
"../app/g2/head-tilt-math.ts",
"../app/ui/shell/notification-modal-queue.ts",
"../app/ui/shell/notification-popup-delay.ts",
"../app/ui/shell/notification-popup-timeout.ts",
"../app/apps/glanceboard/layout.ts",
"../app/apps/files/video-layout.ts",
"../app/g2/pairing-candidates.ts",
"../app/g1/pako.d.ts",
"../app/g1/firmware-image.ts",
"../app/g1/flash-plan.ts",
"../app/g1/even-firmware.ts",
"../app/g1/firmware/g1v0-image.ts",
"../app/g1/firmware/g1v0-own-bytes.ts",
"../app/apps/calculator/math/*.ts",
"../app/apps/microphones/mic-protocol.ts",
"../app/apps/calculator/calculator-commands.ts",
@@ -51,6 +68,9 @@
"../app/apps/microphones/caption-clock.ts",
"../app/apps/microphones/name-guess.ts",
"../app/apps/evenhub/containers.ts",
"../app/apps/evenhub/layout-debug.ts",
"../app/apps/evenhub/image-pipeline.ts",
"../app/apps/evenhub/payload-validation.ts",
"../app/apps/evenhub/list-menu.ts",
"../app/apps/evenhub/device-status.ts",
"../app/apps/evenhub/font-spec.ts",
+45
View File
@@ -0,0 +1,45 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { loader } = require('./helpers/load-typescript.cjs');
// ui-fonts.ts and screen-size.ts over an in-memory settings store, with each
// TTF "loaded" as its path and size.
function fixture(entries = []) {
const store = new Map(entries);
const listeners = [];
const load = loader({}, {
'../native/settings-store': {
getStringSetting: (key, fallback) => store.get(key) ?? fallback,
setStringSetting: (key, value) => { store.set(key, value); for (const listener of listeners) listener(key); },
onSettingsStoreChanged: (listener) => listeners.push(listener),
},
'./image': { G2_LENS_WIDTH: 640, G2_LENS_HEIGHT: 480, RAYNEO_IO_WIDTH: 540, RAYNEO_IO_HEIGHT: 220 },
'./bdffont': { getFont: (name) => ({ name }) },
'./ttf-font': { TtfFont: { load: (path, size) => ({ path, size, lineHeight: size + 4 }) } },
'./installed-fonts': { ensurePreinstalledFonts() {}, getInstalledFont: () => null, installedFontPath: (file) => `/fonts/${file}` },
});
return { fonts: load('app/graphics/ui-fonts.ts'), screen: load('app/graphics/screen-size.ts'), store, listeners };
}
test('the screen and the default UI font follow the paired glasses: 640x480 at 16px, or a RayNeo iO\'s 540x220 at 14px', () => {
const { fonts, screen, store, listeners } = fixture();
assert.deepEqual([screen.screenWidth(), screen.screenHeight(), screen.isRayNeoIoConfigured()], [640, 480, false]);
assert.equal(fonts.defaultUiFontSize(), 16);
assert.deepEqual({ ...fonts.getUiFontSelection() }, { kind: 'ttf', file: 'Roboto-Light.ttf', size: 16 });
assert.equal(fonts.getDefaultSmallFont().size, 16);
// Pairing a RayNeo iO: the cached fonts re-resolve at its size.
store.set(screen.GLASSES_TYPE_KEY, 'rayneo-io');
for (const listener of listeners) listener(screen.GLASSES_TYPE_KEY);
assert.deepEqual([screen.screenWidth(), screen.screenHeight(), screen.isRayNeoIoConfigured()], [540, 220, true]);
assert.equal(fonts.defaultUiFontSize(), 14);
assert.deepEqual({ ...fonts.getUiFontSelection() }, { kind: 'ttf', file: 'Roboto-Light.ttf', size: 14 });
assert.equal(fonts.getDefaultSmallFont().size, 14);
});
test('a chosen UI font keeps its size on either glasses', () => {
const { fonts, screen, store } = fixture([['deviceType', 'rayneo-io']]);
fonts.setUiFontSelection({ kind: 'ttf', file: 'Roboto-Light.ttf', size: 17 });
assert.equal(fonts.getUiFontSelection().size, 17);
store.set(screen.GLASSES_TYPE_KEY, 'g2');
assert.equal(fonts.getUiFontSelection().size, 17);
});
+71
View File
@@ -0,0 +1,71 @@
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, dependencies) {
const context = { exports: {}, console, require: (name) => {
assert.ok(name in dependencies, name);
return dependencies[name];
} };
vm.runInNewContext(ts.transpileModule(fs.readFileSync(path.join(__dirname, '..', file), 'utf8'), {
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 },
}).outputText, context);
return context.exports;
}
const installedApps = {
getInstalledEvenHubApps: () => [],
getInstalledEvenHubFingerprint: () => '',
installedEvenHubAppId: (packageId) => packageId,
renderInstalledEvenHubIcon: () => null,
};
test('the launcher grid leaves out an app the paired glasses cannot run', () => {
const { launcherEntries } = load('app/apps/launcher/index.ts', {
'../../ui/shell/shell': { shell: {} },
'./launcher-app': {},
'../evenhub/installed-apps': installedApps,
});
let supported = true;
const apps = [
{ appId: 'launcher', title: 'Apps', showInLauncher: false },
{ appId: 'timer', title: 'Timer' },
{ appId: 'compass', title: 'Compass', isSupported: () => supported },
];
const ids = () => Array.from(launcherEntries(apps), (entry) => entry.appId);
assert.deepEqual(ids(), ['timer', 'compass']);
supported = false;
assert.deepEqual(ids(), ['timer']);
});
test('apps.launch follows the paired glasses', async () => {
const listeners = [];
const { registerWindowTools } = load('app/assistant/window-tools.ts', {
'../ui/shell/shell': { shell: { wake() {} } },
'../apps/launcher/launcher-folders': {},
'../apps/evenhub/installed-apps': installedApps,
'../ui/dashboard-settings': { onAnySettingChanged: (listener) => listeners.push(listener) },
'./tool-registry': { toolRegistry: null },
});
const tools = new Map();
const registry = { registerSystemTool: (spec, handler) => tools.set(spec.name, { spec, handler }) };
const launched = [];
const timer = { appId: 'timer', title: 'Timer' };
const compass = { appId: 'compass', title: 'Compass' };
let apps = [timer, compass];
registerWindowTools({ apps: () => apps, launchApp: (appId) => launched.push(appId), requestShellRender() {} }, registry);
const launch = () => tools.get('apps.launch');
const launchIds = () => Array.from(launch().spec.inputSchema.properties.app_id.enum);
assert.deepEqual(launchIds(), ['compass', 'timer']);
apps = [timer];
listeners.forEach((listener) => listener());
assert.deepEqual(launchIds(), ['timer']);
await launch().handler({ app_id: 'compass' });
assert.deepEqual(launched, []);
await launch().handler({ app_id: 'timer' });
assert.deepEqual(launched, ['timer']);
});
+63 -2
View File
@@ -158,12 +158,14 @@ test('cloud fallback and a synchronous final still send exactly once', () => {
});
/** The Android voice bridge over a fake native controller (on-device recognition only). */
function androidBridge() {
function androidBridge(modules = {}) {
const controller = {
starts: 0,
setListener(listener) { this.listener = listener; },
setCommunicator() {}, setUsePhoneMic() {}, setSaveRecordings() {}, setEndpointing() {},
setCommunicator() {}, setUsePhoneMic() {}, setEndpointing() {},
setNoiseSuppression() {}, setBeamFilter() {}, clearSpeakerVerification() {}, setOnboardModelKind() {},
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; },
@@ -172,6 +174,7 @@ function androidBridge() {
'@nativescript/core': { Utils: { android: { getApplicationContext: () => ({}) } } },
'./speech-pause': { SpeechPauseDetector: class { reset() {} } },
'./cloud-stt-provider': { createCloudSttClient: () => null, transcriptionKey: options => options.provider },
...modules,
}, { global: { isAndroid: true }, com: { faceclaw: { app: {
FaceclawVoiceController: function () { return controller; },
FaceclawVoiceControllerListener: function (listener) { return listener; },
@@ -215,3 +218,61 @@ test('a shared capture restarts for a holder that needs another provider, and is
assert.equal(controller.active, true);
assert.equal(bridge.holdsCapture('ptt'), true);
});
test('Android continuous captures record to the requested file, and report one that cannot be created', () => {
const recordings = [];
let fail = false, attempts = 0;
const { bridge, controller } = androidBridge({
'./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');
assert.deepEqual([bridge.recording, bridge.recordingFailed, changes], [true, false, 1]);
// 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.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.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([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(attempts, 1);
// One attempt per recording, not one per packet; another recording is a fresh attempt.
fail = true; attempts = 0;
bridge.recordTo('transcribe-2');
controller.listener.onPcm([7]); controller.listener.onPcm([8]);
assert.deepEqual([bridge.recording, bridge.recordingFailed, changes, attempts], [false, true, 4, 1]);
bridge.recordTo('transcribe-2');
controller.listener.onPcm([9]);
assert.equal(attempts, 1);
fail = false;
bridge.recordTo('');
bridge.recordTo('transcribe-2');
controller.listener.onPcm([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);
});
+1 -1
View File
@@ -11,7 +11,7 @@ function fixture() {
const load = loader({ global: {}, setTimeout: (fn, ms) => { timers.set(fn, ms); return fn; }, clearTimeout: fn => timers.delete(fn) }, {
'../../graphics/image': {}, './chrome-layer': { windowIcon() {} },
'../../assistant/tool-registry': { toolRegistry: { removeAppTools() {} } },
'./geometry': { appViewportSize: () => ({ width: 576, height: 288 }) },
'./geometry': { appViewport: () => ({ width: 576, height: 288 }) },
'../../native/frame-timings': {}, './worker-state': { publishWorkerState: (...args) => published.push(args) },
'./shell': { shell: { registerWindow: w => shellWindows.set(w.windowId, w), setTrayIcon() {},
focusWindow: id => focused.push(id), isWindowFocused: () => false } },