const test = require('node:test'); const assert = require('node:assert/strict'); const fs = require('node:fs'); const vm = require('node:vm'); const ts = require('typescript'); function fixture(swap = false, app = {}) { const source = ts.createSourceFile('shell.ts', fs.readFileSync('app/ui/shell/shell.ts', 'utf8'), ts.ScriptTarget.Latest, true); const cls = source.statements.find(n => ts.isClassDeclaration(n) && n.name.text === 'Shell'); const route = cls.members.find(n => n.name?.getText(source) === 'routeInput'); class Overlay {} const context = { exports: {}, ShellOverlayMenuLayer: Overlay, swapMenuGesturesSetting: { get: () => swap }, isWatchInput: () => false }; vm.runInNewContext(ts.transpileModule(`export class Shell { ${route.getText(source)} }`, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 }, }).outputText, context); const s = new context.exports.Shell(); const delivered = []; const window = { handleInput: async e => delivered.push(e), hasAppMenu: () => true, ...app }; let overlay = false, escapes = 0; Object.assign(s, { focus: 'window', screenOn: true, lastInput: null, foregroundWindow: () => window, cancelEscapeMenuTimer() {}, startEscapeMenuTimer() { escapes++; }, config: { requestShellRender() {} }, openEscapeMenu() { overlay = true; }, stack: { isAtBase: () => !overlay, popIfTop(fn) { if (overlay && fn(new Overlay())) { overlay = false; return true; } return false; } }, }); return { s, delivered, setSwap: value => { swap = value; }, overlay: () => overlay, escapes: () => escapes, input: type => s.routeInput({ type, source: 'ring', timestampMs: 42 }, 17) }; } for (const swap of [false, true]) { test(`menu routing, overlay switching and metadata with inversion ${swap}`, async () => { const h = fixture(swap); const system = swap ? 'short-then-long-press' : 'long-press'; const app = swap ? 'long-press' : 'short-then-long-press'; await h.input(app); assert.equal(h.delivered.at(-1).type, 'short-then-long-press'); assert.equal(h.delivered.at(-1).source, 'ring'); assert.equal(h.delivered.at(-1).timestampMs, 42); await h.input(system); assert.equal(h.overlay(), true); await h.input(app); assert.equal(h.overlay(), false); assert.equal(h.delivered.length, 2); }); for (const app of [{ claimsLongPress: () => true }, { holdToTalk: true }]) { test(`owned long press remains intact with inversion ${swap}: ${JSON.stringify(Object.keys(app))}`, async () => { const h = fixture(swap, app); await h.input('long-press'); assert.equal(h.delivered[0].type, 'long-press'); assert.equal(h.escapes(), app.holdToTalk ? 0 : 1); await h.input('short-then-long-press'); if (app.holdToTalk) assert.equal(h.overlay(), true); else assert.equal(h.delivered[1].type, 'short-then-long-press'); }); } } test('gesture inversion never wakes a sleeping screen', async () => { const h = fixture(true); h.s.screenOn = false; await h.input('long-press'); await h.input('short-then-long-press'); assert.equal(h.delivered.length, 0); assert.equal(h.overlay(), false); }); test('the switcher retains its existing gestures when inversion is enabled', async () => { for (const type of ['long-press', 'short-then-long-press']) { const snapshots = []; for (const swap of [false, true]) { const h = fixture(swap); h.s.focus = 'sidebar'; await h.input(type); snapshots.push({ overlay: h.overlay(), delivered: h.delivered.map(e => e.type) }); } assert.deepEqual(snapshots[0], snapshots[1]); } }); test('the setting applies immediately and follows changing app hold ownership', async () => { let claims = false; const h = fixture(false, { claimsLongPress: () => claims }); h.setSwap(true); await h.input('long-press'); assert.equal(h.delivered.at(-1).type, 'short-then-long-press'); claims = true; await h.input('long-press'); assert.equal(h.delivered.at(-1).type, 'long-press'); claims = false; await h.input('long-press'); assert.equal(h.delivered.at(-1).type, 'short-then-long-press'); }); test('app menu hints reflect inversion, voice capture and owned holds', () => { const source = ts.createSourceFile('window-menu.ts', fs.readFileSync('app/ui/window-menu.ts', 'utf8'), ts.ScriptTarget.Latest, true); const cls = source.statements.find(n => ts.isClassDeclaration(n) && n.name.text === 'WindowMenuLayer'); let swap = false; const context = { exports: {}, MenuLayer: class { constructor(_title, _items, layout) { this.layout = layout; } }, WINDOW_MENU_LAYOUT: { footer: 'long-press' }, GESTURE_SHORT_THEN_LONG_PRESS: 'tap-hold', swapMenuGesturesSetting: { get: () => swap }, gestureHints: hints => hints[0][0] }; vm.runInNewContext(ts.transpileModule(cls.getText(source), { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 }, }).outputText, context); const hint = (voice = false, claims = false) => new context.exports.WindowMenuLayer('App', [], voice, claims).layout.footer; assert.equal(hint(), 'long-press'); swap = true; assert.equal(hint(), 'tap-hold'); assert.equal(hint(false, true), 'long-press'); assert.equal(hint(true), 'tap-hold'); });