Files
faceclaw/tests/head-up-wake.test.cjs
T

161 lines
8.4 KiB
JavaScript

const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const vm = require('node:vm');
const ts = require('typescript');
const source = file => fs.readFileSync(path.join(__dirname, '..', file), 'utf8');
function evaluate(code, globals) {
const context = { exports: {}, ...globals };
vm.runInNewContext(ts.transpileModule(code, {
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 },
}).outputText, context);
return context.exports;
}
function settings(values = new Map()) {
const file = ts.createSourceFile('settings.ts', source('app/ui/dashboard-settings.ts'), ts.ScriptTarget.Latest, true);
const names = new Set(['ConfigSetting', 'ConfigSettingBoolean', 'ConfigSettingEnum', 'HeadUpWakeSetting',
'headUpWakeSetting', 'normalizeHeadUpAngle', 'headUpAngleSetting', 'getHeadUpPreferences']);
const code = file.statements.filter(node => names.has(node.name?.text) ||
(ts.isVariableStatement(node) && node.declarationList.declarations.some(d => names.has(d.name.getText(file)))))
.map(node => node.getText(file)).join('\n');
return evaluate(code, {
fixedChoiceSettings: new Map(),
getStringSetting: (key, fallback) => values.get(key) ?? fallback,
setStringSetting: (key, value) => values.set(key, value),
});
}
test('head-up preferences preserve untouched device fields and persist each explicit choice separately', () => {
const values = new Map();
let prefs = settings(values);
assert.equal(prefs.getHeadUpPreferences().enabled, -1);
assert.equal(prefs.getHeadUpPreferences().angle, -1);
prefs.headUpWakeSetting.set(false);
prefs = settings(values);
assert.equal(prefs.headUpWakeSetting.get(), false);
assert.equal(prefs.getHeadUpPreferences().enabled, 0);
assert.equal(prefs.getHeadUpPreferences().angle, -1);
prefs.headUpAngleSetting.set('75');
prefs = settings(values);
assert.equal(prefs.headUpAngleSetting.get(), '60');
assert.equal(prefs.getHeadUpPreferences().enabled, 0);
assert.equal(prefs.getHeadUpPreferences().angle, 60);
for (const [input, expected] of [['-5', '0'], ['20.8', '21'], ['NaN', '30'], ['', '30']]) {
assert.equal(prefs.headUpAngleSetting.set(input), expected);
}
assert.equal(values.has('glanceboard.showOnHeadTilt'), false);
});
test('controller skips unset preferences, deduplicates changes and forces reconnect synchronization', async () => {
const file = ts.createSourceFile('controller.ts', source('app/g2/dashboard-controller.ts'), ts.ScriptTarget.Latest, true);
const controller = file.statements.find(node => ts.isClassDeclaration(node) && node.name.text === 'DashboardController');
const method = controller.members.find(node => node.name?.getText(file) === 'pushHeadUpPreferences').getText(file);
let prefs = { enabled: -1, angle: -1 };
const { Harness } = evaluate(`export class Harness { ${method} }`, {
global: { isAndroid: true }, glassesTypeSetting: { get: () => 'g2' }, getHeadUpPreferences: () => prefs,
});
const h = new Harness(), calls = [];
h.communicator = { configureHeadUpWake: async (...args) => calls.push(args) };
h.pushHeadUpPreferences(); assert.equal(calls.length, 0);
prefs = { enabled: 0, angle: 25 };
h.pushHeadUpPreferences(); h.pushHeadUpPreferences();
assert.deepEqual(calls, [[0, 25]]);
h.pushHeadUpPreferences(true); assert.equal(calls.length, 2);
prefs = { enabled: 1, angle: 40 }; h.pushHeadUpPreferences();
assert.deepEqual(calls.at(-1), [1, 40]);
let failures = 0;
h.communicator.configureHeadUpWake = async () => { throw new Error('offline'); };
h.appendLog = () => failures++;
h.formatError = error => error.message;
h.pushHeadUpPreferences(true);
await new Promise(resolve => setImmediate(resolve));
assert.equal(h.lastPushedHeadUp, null); assert.equal(failures, 1);
const connect = controller.members.find(node => node.name?.getText(file) === 'connect').getText(file);
assert.match(connect, /pushHeadUpPreferences\(true\)/);
});
function settingsMenuHarness(isIOS, model) {
const file = ts.createSourceFile('settings-menus.ts', source('app/ui/dashboard/settings-menus.ts'), ts.ScriptTarget.Latest, true);
const actual = new Set(['settingsSections', 'ringConnectionMenuItem', 'notificationFilterMenuItem']);
const globals = { global: { isIOS }, NOTIFICATIONS_SETTINGS_SECTION: 'Notifications' };
// Isolate the real menu construction from unrelated rendering/native dependencies.
for (const node of file.statements) {
if (ts.isImportDeclaration(node)) {
for (const binding of node.importClause?.namedBindings?.elements ?? []) {
const name = binding.name.text;
globals[name] = Object.assign(() => ({}), { label: name, get: () => '' });
}
} else if (ts.isFunctionDeclaration(node) && !actual.has(node.name.text)) {
globals[node.name.text] = () => ({});
}
}
const row = setting => ({ label: setting.label });
Object.assign(globals, {
enumSettingMenuItem: row, toggleSettingMenuItem: row, textSettingMenuItem: row,
glassesTypeSetting: { get: () => model },
customizationRows: () => () => [], whisperServerMenuItems: () => [],
submenuItem: (label, onSelect, options) => ({ label, onSelect, ...options }),
});
const code = file.statements.filter(node => ts.isFunctionDeclaration(node) && actual.has(node.name.text))
.map(node => `export ${node.getText(file)}`).join('\n');
return evaluate(code, globals);
}
test('head-up controls are offered only for Android G2 model selection', () => {
for (const isIOS of [false, true]) {
for (const model of ['g2', 'g1', 'rayneo-io']) {
const display = settingsMenuHarness(isIOS, model).settingsSections().find(section => section.label === 'Display');
for (const label of ['headUpWakeSetting', 'headUpAngleSetting']) {
assert.equal(display.items.some(item => item.label === label), !isIOS && model === 'g2', `${isIOS ? 'iOS' : 'Android'} ${model} ${label}`);
}
}
}
});
test('notification filter description matches glasses-wide filtering while keeping phone notifications', () => {
const item = settingsMenuHarness(false, 'g2').notificationFilterMenuItem();
assert.equal(item.description, 'Disabled apps are hidden from glasses popups, the notification list, and the tray. Their notifications remain on your phone.');
});
test('Developer ring connection stays model-aware on iOS and Android', () => {
for (const isIOS of [false, true]) {
for (const model of ['g2', 'g1', 'rayneo-io']) {
const section = settingsMenuHarness(isIOS, model).settingsSections().find(section => section.label === 'Developer');
const ring = section.items.find(item => item.label === 'ringConnectionModeSetting');
assert.equal(typeof ring.disabled, 'function', 'use the model-aware ring helper');
assert.equal(ring.disabled(), model !== 'g2', `${isIOS ? 'iOS' : 'Android'} ${model}`);
}
}
});
function controllerForModel(getModel, isAndroid, preferences) {
const file = ts.createSourceFile('controller.ts', source('app/g2/dashboard-controller.ts'), ts.ScriptTarget.Latest, true);
const controller = file.statements.find(node => ts.isClassDeclaration(node) && node.name.text === 'DashboardController');
const method = controller.members.find(node => node.name?.getText(file) === 'pushHeadUpPreferences').getText(file);
const { Harness } = evaluate(`export class Harness { ${method} }`, {
global: { isAndroid }, glassesTypeSetting: { get: getModel }, getHeadUpPreferences: () => preferences,
});
return new Harness();
}
test('controller sends stored head-up choices only to Android G2, including forced reconnects', () => {
for (const isAndroid of [true, false]) {
let model = 'g1';
const preferences = { enabled: 1, angle: 35 };
const h = controllerForModel(() => model, isAndroid, preferences);
const calls = [];
h.communicator = { configureHeadUpWake: async (...args) => calls.push(args) };
for (model of ['g1', 'rayneo-io']) {
h.pushHeadUpPreferences();
h.pushHeadUpPreferences(true);
assert.equal(calls.length, 0, `${isAndroid ? 'Android' : 'iOS'} ${model}`);
assert.equal(h.lastPushedHeadUp, undefined, 'skipped models must not consume dedup state');
}
model = 'g2';
h.pushHeadUpPreferences();
assert.deepEqual(calls, isAndroid ? [[1, 35]] : []);
assert.deepEqual(preferences, { enabled: 1, angle: 35 }, 'model changes preserve stored choices');
}
});