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'); // The provider settings ios-preview-controller hands the voice bridge. const voiceSettings = { voiceProviderSetting: { get: () => 'onboard' }, elevenLabsApiKeySetting: { get: () => '' }, openAiApiKeySetting: { get: () => '' }, sonioxApiKeySetting: { get: () => '' }, whisperServerUrlSetting: { get: () => '' }, whisperServerModelSetting: { get: () => '' }, whisperServerLanguageSetting: { get: () => '' }, whisperServerApiKeySetting: { get: () => '' } }; function load(file, context) { const sandbox = { exports: {}, ...context }; const source = fs.readFileSync(path.join(__dirname, '..', file), 'utf8'); vm.runInNewContext(ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 } }).outputText, sandbox); return sandbox.exports; } test('iOS worker replies keep cached draw references paired with their baked pixels', () => { const replies = [], received = []; const wrap = buffer => ({ length: buffer.byteLength, buffer, base64EncodedStringWithOptions: () => Buffer.from(buffer).toString('base64') }); const NSData = { dataWithBytesLength: buffer => wrap(buffer), alloc: () => ({ initWithBase64EncodedStringOptions: text => { const bytes = Uint8Array.from(Buffer.from(text, 'base64')); return wrap(bytes.buffer); } }), }; const interop = { handleof: buffer => buffer, bufferFromData: data => data.buffer }; const display = load('app/native/active-display.ios.ts', { NSData, interop, global: { postMessage: message => replies.push(message) }, }).getActiveDisplay(); const pixels = new Uint8Array([255, 0, 0, 255]), draws = new Uint8Array([1, 7, 0, 0, 0, 0, 0, 0, 0]); display.submitSurfaceFrame(pixels.buffer, 'window:test', 0, 0, 2, 2, 'frame', 0, 1, draws.buffer); const worker = {}; const { WorkerAppHost } = load('app/ui/shell/worker-window.ts', { require: () => ({}), global: { isIOS: true }, NSData, interop, }); const host = new WorkerAppHost({ appId: 'test', worker, submitPixels: (...args) => received.push(args) }); host.openWindows.add('test'); worker.onmessage({ data: replies[0] }); assert.equal(received.length, 1); assert.deepEqual([...received[0][1]], [...pixels]); assert.deepEqual([...new Uint8Array(received[0][4])], [...draws]); display.submitSurfaceFrame(pixels.buffer, 'window:test', 0, 0, 2, 2); worker.onmessage({ data: replies[1] }); assert.equal(received[1][4], null, 'a pixel-only update must clear previous draw identities'); host.openWindows.clear(); worker.onmessage({ data: replies[0] }); assert.equal(received.length, 2, 'closed windows cannot deliver stale cached draws'); }); test('iOS installed apps launch, recover missing packages, and close before uninstall', async () => { const calls = [], app = { packageId: 'test.app', name: 'Test' }; let present = true; const modules = { "../ui/input-monitor": load("app/ui/input-monitor.ts", {}), '../apps/all-apps': { ALL_APPS: [{ appId: 'evenhub' }] }, '../apps/evenhub/installed-apps': { getInstalledEvenHubAppById: id => id === 'installed' ? app : null, installedEvenHubPackageId: id => id === 'installed' ? app.packageId : null, uninstallEvenHubPackage: id => calls.push(['uninstall', id]) }, '../apps/evenhub/updates': { isInstalledPackagePresent: () => present }, '../apps/evenhub': { openEvenHubStoreForPackage: (_ctx, value) => calls.push(['reinstall', value.packageId]) }, '../apps/evenhub/manager': { launchInstalledPackage: (ctx, value) => calls.push(['launch', ctx.appId, value.packageId]), closeRunningPackage: id => calls.push(['close', id]) }, }; const { IosPreviewController } = load('app/g2/ios-preview-controller.ts', { require: id => modules[id] ?? {} }); const host = Object.create(IosPreviewController.prototype); host.buildAppContext = app => app; host.fail = assert.fail; host.requestShellRender = () => {}; await host.launchApp('installed'); present = false; await host.launchApp('installed'); await host.uninstallApp('installed'); assert.deepEqual(calls, [['launch', 'installed', 'test.app'], ['reinstall', 'test.app'], ['close', 'test.app'], ['uninstall', 'test.app']]); }); test('iOS launch failures on a zeroed package fall back to the store page; other failures still report', async () => { const calls = [], failures = [], app = { packageId: 'test.app', name: 'Test' }; let zeroed = true; const modules = { "../ui/input-monitor": load("app/ui/input-monitor.ts", {}), '../apps/all-apps': { ALL_APPS: [{ appId: 'evenhub' }] }, '../apps/evenhub/installed-apps': { getInstalledEvenHubAppById: () => app, discardZeroedEvenHubPackage: id => { calls.push(['discard', id]); return zeroed; } }, '../apps/evenhub/updates': { isInstalledPackagePresent: () => true }, '../apps/evenhub': { openEvenHubStoreForPackage: (_ctx, value) => calls.push(['reinstall', value.packageId]) }, '../apps/evenhub/manager': { launchInstalledPackage: async () => { calls.push(['launch']); throw new Error('Could not read the EHPK manifest.'); } }, }; const { IosPreviewController } = load('app/g2/ios-preview-controller.ts', { require: id => modules[id] ?? {} }); const host = Object.create(IosPreviewController.prototype); host.buildAppContext = app => app; host.fail = error => failures.push(error.message); host.requestShellRender = () => {}; await host.launchApp('installed'); assert.deepEqual(calls, [['launch'], ['discard', 'test.app'], ['reinstall', 'test.app']]); assert.deepEqual(failures, []); calls.length = 0; zeroed = false; await host.launchApp('installed'); assert.deepEqual(calls, [['launch'], ['discard', 'test.app']]); assert.deepEqual(failures, ['Could not read the EHPK manifest.']); }); test('iOS settings prompts honor password input and report cancellation', async () => { const prompts = [], writes = []; const { IosPreviewController } = load('app/g2/ios-preview-controller.ts', { require: id => id === '@nativescript/core' ? { Dialogs: { prompt: async options => { prompts.push(options); return { result: false, text: 'discarded' }; } }, } : {} }); const host = Object.create(IosPreviewController.prototype); host.active = true; const accepted = await host.editSetting({ editorTitle: 'Password', inputKind: 'password', get: () => '', set: value => writes.push(value) }); assert.equal(prompts[0].inputType, 'password'); assert.equal(accepted, false); assert.deepEqual(writes, []); }); test('iOS host forwards hands-free capture mode to the voice bridge', async () => { const captures = []; const { IosPreviewController } = load('app/g2/ios-preview-controller.ts', { require: id => id === '../native/ios-voice-input' ? { iosVoiceInput: { startGlassesCapture: (_session, _voice, _log, endpointing) => captures.push(endpointing) } } : id === '../ui/dashboard-settings' ? voiceSettings : {} }); const host = Object.create(IosPreviewController.prototype); host.communicator = {}; host.state = { phase: 'connected' }; host.continuousClients = new Map(); await host.startVoiceCapture(true); await host.startVoiceCapture(); assert.deepEqual(captures, [true, false]); }); test('iOS phone battery distinguishes unknown, charging, full and unplugged readings', () => { const device = { batteryLevel: -1, batteryState: 0 }; const api = load('app/native/phone-battery.ts', { require: () => ({}), global: { isIOS: true }, UIDevice: { currentDevice: device }, UIDeviceBatteryState: { Unknown: 0, Unplugged: 1, Charging: 2, Full: 3 } }); assert.deepEqual({ ...api.readPhoneBatteryState() }, { battery: null, charging: null }); device.batteryLevel = 0.726; device.batteryState = 2; assert.deepEqual({ ...api.readPhoneBatteryState() }, { battery: 73, charging: true }); device.batteryLevel = 1; device.batteryState = 3; assert.deepEqual({ ...api.readPhoneBatteryState() }, { battery: 100, charging: true }); device.batteryLevel = 0; device.batteryState = 1; assert.deepEqual({ ...api.readPhoneBatteryState() }, { battery: 0, charging: false }); }); test('settings changes propagate between iOS worker isolates once, including after a getter read', () => { const store = new Map(), ticks = new Set(), tasks = []; const ApplicationSettings = { getString: (k, d) => store.get(k) ?? d, getBoolean: (k, d) => store.get(k) ?? d, setString: (k, v) => store.set(k, v), setBoolean: (k, v) => store.set(k, v), hasKey: k => store.has(k) }; const context = { require: () => ({ ApplicationSettings }), setTimeout: fn => tasks.push(fn), setInterval: fn => { ticks.add(fn); return fn; }, clearInterval: fn => ticks.delete(fn) }; const main = load('app/native/settings-store.ios.ts', context), worker = load('app/native/settings-store.ios.ts', context); const seenMain = [], seenWorker = []; const offMain = main.onSettingsStoreChanged(k => seenMain.push(k)); const offWorker = worker.onSettingsStoreChanged(k => seenWorker.push(k)); main.getStringSetting('terminal.connections', '[]'); worker.getStringSetting('terminal.connections', '[]'); main.setStringSetting('terminal.connections', '["test"]'); assert.equal(worker.getStringSetting('terminal.connections', '[]'), '["test"]'); const flush = () => { for (const tick of ticks) tick(); while (tasks.length) tasks.shift()(); }; flush(); flush(); assert.deepEqual(seenMain, ['terminal.connections']); assert.deepEqual(seenWorker, ['terminal.connections']); main.getBooleanSetting('sound', true); worker.getBooleanSetting('sound', true); worker.setBooleanSetting('sound', false); flush(); assert.equal(main.getBooleanSetting('sound', true), false); assert.deepEqual(seenMain, ['terminal.connections', 'sound']); offWorker(); offMain(); assert.equal(ticks.size, 0); }); test('iOS worker frames preserve baked grayscale bytes through the message boundary', () => { const messages = []; const api = load('app/native/active-display.ios.ts', { global: { postMessage: m => messages.push(m) }, interop: { handleof: b => b }, NSData: { dataWithBytesLength: (b, n) => ({ base64EncodedStringWithOptions: () => Buffer.from(b, 0, n).toString('base64') }) } }); const pixels = new Uint8Array([0, 1, 15, 127, 254, 255]); api.getActiveDisplay().submitSurfaceFrame(pixels.buffer, 'window:blocks:main', 0, 0, 3, 2); assert.equal(messages[0].surfaceId, 'window:blocks:main'); assert.equal(messages[0].width, 3); assert.equal(messages[0].height, 2); assert.deepEqual(Buffer.from(messages[0].pixels, 'base64'), Buffer.from(pixels)); }); test('settings-driven repaint runs after font cache invalidation, regardless of subscription order', () => { const listeners = [], tasks = [], painted = []; let cachedFont = 'Light'; const api = load('app/ui/dashboard-settings.ts', { global: { isIOS: true }, require: id => id.includes('settings-store') ? { onSettingsStoreChanged: fn => listeners.push(fn) } : { Layer: class {}, ASSISTANT_MODEL_CHOICES: [] }, setTimeout: fn => tasks.push(fn), }); api.onAnySettingChanged(() => painted.push(cachedFont)); listeners.push(() => { cachedFont = 'Bold'; }); for (const listener of listeners) listener('display.uiFont2'); while (tasks.length) tasks.shift()(); assert.deepEqual(painted, ['Bold']); }); test('iOS bandwidth footer toggles live, polls only in foreground and resets its rate window on resume', () => { const views = [], intervals = new Map(), appEvents = new Map(), settingListeners = new Set(); let enabled = false, atMs = 0, totals = { messages: 0, bytes: 0, frames: 0 }, nextId = 0; class View { events = new Map(); children = []; constructor() { views.push(this); } on(event, fn) { this.events.set(event, fn); } addChild(view) { this.children.push(view); } set(key, value) { this[key] = value; } notifyPropertyChange() {} getActualSize() { return { width: 0, height: 0 }; } getViewById() { return null; } static setRow() {} static setColumn() {} } const core = { Application: { suspendEvent: 'suspend', resumeEvent: 'resume', on: (key, fn) => appEvents.set(key, fn), off: key => appEvents.delete(key) }, Builder: { load: () => new View() }, Observable: View, Button: View, Color: class {}, Dialogs: {}, GridLayout: View, Image: View, Label: View, Page: View, StackLayout: View }; const modules = { "../ui/input-monitor": load("app/ui/input-monitor.ts", {}), '../assistant/system-tools': { registerSystemTools() {} }, '../assistant/window-tools': { registerWindowTools() {} }, '../assistant/navigate-tools': { registerNavigateTools() {} }, '../assistant/roam-tools': { registerRoamTools() {} }, "../native/ios-navigation-sensors": {}, '../native/notification-icons.ios': { bindIosNotifications() {}, iosNotificationsChanged() {}, onIosNotificationPopup: () => () => {}, readActiveNotifications: () => [] }, '../native/notification-sources': { shouldShowNotificationOnGlasses: () => true }, '../native/compass.ios': { bindCompassSession() {}, receiveCompassEvent() {} }, '@nativescript/core': core, '../apps/launcher': { launcherEntries: () => [] }, '../apps/all-apps': { ALL_APPS: [] }, '../g2/ios-preview-controller': { IosPreviewController: class { resume() {} pause() {} } }, './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': { 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 }, }; 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] }); const { createMainPage } = load('app/phone-ui/main-page.ios.ts', { require: id => modules[id] ?? {}, Date: { now: () => atMs }, NSNotificationCenter: { defaultCenter: { addObserverForNameObjectQueueUsingBlock: () => ({}), removeObserver() {} } }, UIKeyboardWillChangeFrameNotification: 'keyboard', NSOperationQueue: { mainQueue: {} }, setInterval: fn => { const id = ++nextId; intervals.set(id, fn); return id; }, clearInterval: id => intervals.delete(id), setTimeout: () => 1, clearTimeout() {}, }); const page = createMainPage(), footer = views.find(v => v.accessibilityIdentifier === 'ble-bandwidth-indicator'); page.events.get('loaded')(); assert.equal(intervals.size, 0); assert.equal(footer.visibility, 'collapse'); enabled = true; for (const fn of settingListeners) fn(); assert.equal(intervals.size, 1); assert.equal(footer.visibility, 'visible'); totals = { messages: 4, bytes: 2000, frames: 2 }; atMs = 1000; for (const fn of intervals.values()) fn(); assert.match(footer.text, /2.0 fps, 1,000 B\/frame/); appEvents.get('suspend')(); assert.equal(intervals.size, 0); const previousText = footer.text; totals = { messages: 40, bytes: 20000, frames: 20 }; atMs = 20000; for (const fn of settingListeners) fn(); assert.equal(footer.text, previousText); assert.equal(intervals.size, 0); appEvents.get('resume')(); assert.equal(intervals.size, 1); assert.doesNotMatch(footer.text, /fps/); enabled = false; for (const fn of settingListeners) fn(); assert.equal(footer.visibility, 'collapse'); assert.equal(intervals.size, 0); page.events.get('unloaded')(); assert.equal(appEvents.size, 0); assert.equal(settingListeners.size, 0); }); test('iOS configures an in-process surface before submitting constructor-triggered frames', async () => { const submitted = [], surfaces = new Set(); const shell = { getWindows: () => [], configure() {}, registerWindow() {}, focusWindow() {}, foregroundWindow: () => null }; const api = load('app/g2/ios-preview-controller.ts', { require: name => ({ '../ui/shell/shell': { shell }, '../ui/shell/geometry': { appViewportRect: () => ({ x: 0, y: 0, width: 2, height: 1 }) }, '../graphics/plane': { flattenPlanesWithDraws: planes => ({ image: planes[0], draws: [] }), planesFingerprint: () => 'fp' }, '../graphics/glyph-wire': { prepareFrameDraws: () => null }, })[name] ?? {}, console, }); // Exercise the real launch method with a strict display boundary. App // construction paints synchronously, as Nightscout's tray subscription does. const controller = Object.create(api.IosPreviewController.prototype); controller.inProcessApps = new Map(); controller.requestShellRender = () => {}; controller.schedulePreviewUpdate = () => {}; controller.communicator = null; controller.previewTarget = { async configureSurface(id) { surfaces.add(id); }, async setSurfaceVisible() {}, async removeSurface(id) { surfaces.delete(id); }, async submitSurfaceFrame(id, pixels) { assert.ok(surfaces.has(id), `unconfigured ${id}`); submitted.push([...pixels]); }, }; let plumbing, early; const fresh = [{ pixels: new Uint8Array([30, 40]), width: 2, height: 1 }]; await controller.launchInProcessApp('nightscout', 'window:nightscout', options => { plumbing = options; early = options.submitFrame([{ pixels: new Uint8Array([10, 20]), width: 2, height: 1 }]); return { window: { windowId: 'nightscout', surfaceId: 'window:nightscout', appId: 'nightscout' }, requestRender: () => options.submitFrame(fresh) }; }); await early; assert.deepEqual(submitted, [[30, 40]]); plumbing.removeSurface(); await plumbing.submitFrame(fresh); assert.deepEqual(submitted, [[30, 40]], 'late callbacks cannot submit to a removed surface'); }); test('iOS compositor rejects missing and removed surfaces before crossing into Kotlin', () => { let nativeCalls = 0; const native = { configureIdXYWidthHeightZOrderTransparent() {}, removeId() {}, submitDrawsIdDataXYWidthHeightDraws() { nativeCalls++; }, visibleIdVisible() { nativeCalls++; }, depthIdDepth() { nativeCalls++; } }; const api = load('app/graphics/surface-compositor.ios.ts', { require: () => ({ toData: data => data }), FaceclawKitIosSurfaceCompositor: { alloc: () => ({ initWithWidthHeight: () => native }) }, }); const c = new api.SurfaceCompositor(2, 1), pixels = new Uint8Array([1, 2]); const rect = { x: 0, y: 0, width: 2, height: 1 }; const rejectsAll = () => { assert.throws(() => c.submitSurfaceFrame('nightscout', pixels, rect), /Unknown surface/); assert.throws(() => c.setSurfaceVisible('nightscout', true), /Unknown surface/); assert.throws(() => c.setSurfaceDepth('nightscout', 1), /Unknown surface/); }; rejectsAll(); c.configureSurface('nightscout', { ...rect, zOrder: 0, transparency: 'opaque' }); c.submitSurfaceFrame('nightscout', pixels, rect); c.setSurfaceVisible('nightscout', true); c.setSurfaceDepth('nightscout', 1); assert.throws(() => c.setSurfaceDepth('nightscout', 200), /Invalid surface depth/); c.removeSurface('nightscout'); rejectsAll(); assert.equal(nativeCalls, 3); }); test('iOS opens the on-glasses onboarding on a new acknowledged frame, once per run while due', () => { const settings = new Map(); const progress = load('app/apps/onboarding/onboarding-progress.ts', { require: id => ({ '../../native/settings-store': { getStringSetting: (key, fallback) => settings.get(key) ?? fallback, setStringSetting: (key, value) => settings.set(key, value), }, '../../version': { FACECLAW_VERSION: '9.9.9' }, })[id], }); const launched = []; const { IosPreviewController } = load('app/g2/ios-preview-controller.ts', { require: id => ({ '../apps/onboarding/onboarding-progress': progress })[id] ?? {}, }); const host = Object.create(IosPreviewController.prototype); host.acknowledgedFrames = 0; host.launchApp = id => { launched.push(id); return Promise.resolve(); }; host.maybeLaunchOnboarding({ phase: 'connecting', frames: 0 }); host.maybeLaunchOnboarding({ phase: 'connected', frames: 0 }); assert.deepEqual(launched, []); host.maybeLaunchOnboarding({ phase: 'connected', frames: 1 }); assert.deepEqual(launched, ['onboarding']); // Still unfinished, but a reconnect in the same run doesn't reopen it. host.maybeLaunchOnboarding({ phase: 'connected', frames: 2 }); assert.deepEqual(launched, ['onboarding']); assert.equal(progress.isGlassesOnboardingDue(), true); progress.markGlassesOnboardingFinished(); assert.equal(progress.isGlassesOnboardingDue(), false); settings.set('onboarding.glassesFinishedVersion', '9.9.8'); assert.equal(progress.isGlassesOnboardingDue(), true, 'an upgrade makes onboarding due again'); }); test('iOS preview voice uses the phone microphone, while connected capture stays on glasses', async () => { const calls = []; const bridge = { prepare: async (foreground, phone, voice) => { calls.push(['prepare', foreground, phone, voice.provider]); return true; }, startPhoneCapture: async (voice, _log, endpointing) => calls.push(['phone', voice.provider, endpointing]), startGlassesCapture: async (_session, voice, _log, endpointing) => calls.push(['glasses', voice.provider, endpointing]) }; const { IosPreviewController } = load('app/g2/ios-preview-controller.ts', { require: id => id === '../native/ios-voice-input' ? { iosVoiceInput: bridge } : id === '../ui/dashboard-settings' ? voiceSettings : {}, }); const host = Object.create(IosPreviewController.prototype); host.active = true; host.glassesLocked = false; host.communicator = null; host.state = { phase: 'disconnected' }; host.continuousClients = new Map(); assert.equal(await host.prepareVoiceCapture(), true); await host.startVoiceCapture(true); assert.deepEqual(calls, [['prepare', true, true, 'onboard'], ['prepare', true, true, 'onboard'], ['phone', 'onboard', true]]); calls.length = 0; host.communicator = {}; host.state = { phase: 'connected' }; assert.equal(await host.prepareVoiceCapture(), true); await host.startVoiceCapture(false); assert.deepEqual(calls, [['prepare', true, false, 'onboard'], ['glasses', 'onboard', false]]); calls.length = 0; host.active = false; host.communicator = null; host.state = { phase: 'disconnected' }; assert.equal(await host.prepareVoiceCapture(), false); await host.startVoiceCapture(); assert.deepEqual(calls, []); }); 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', get continuousSource() { return this.source; }, get continuousActive() { return !!this.source || this.draining; }, prepare: async () => true, startGlassesCapture: async (_session, voice, _log, _endpointing, continuous) => { calls.push(['glasses', continuous]); providers.push(`${voice.provider}:${voice.language}`); bridge.source = 'glasses'; }, startPhoneCapture: async (_voice, _log, _endpointing, continuous) => { calls.push(['phone', continuous]); bridge.source = 'phone'; }, drain() { if (this.source) { this.source = null; this.draining = true; } }, stopContinuousCapture: async () => { calls.push(['stop']); bridge.drain(); }, handleSessionEnded() { if (bridge.source === 'glasses') bridge.drain(); }, stopPhoneCapture() { if (bridge.source === 'phone') bridge.drain(); }, recordTo: name => { bridge.recording = name; }, setRawSession: session => { bridge.rawSession = session; } }; const settings = { ...voiceSettings, voiceProviderSetting: { get: () => 'whisper-server' }, whisperServerUrlSetting: { get: () => 'http://whisper.local:8000' }, whisperServerLanguageSetting: { get: () => 'en' }, 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]) } } : id === '../native/cloud-stt-provider' ? { transcriptionKey: voice => `${voice.provider}:${voice.language}` } : {}, }); const host = Object.create(IosPreviewController.prototype); Object.assign(host, { active: true, glassesLocked: false, communicator: {}, state: { phase: 'connected' }, sessionConnected: true, continuousClients: new Map(), continuousPending: false, continuousCaptureToken: 0, displayReady: Promise.resolve(), glance: { dismiss() {} }, onError: message => calls.push(['error', message]), logBluetooth() {}, fail() {}, requestShellRender() {}, syncRuntime() {}, flushBluetoothLog() {} }); const settle = () => new Promise(resolve => setImmediate(resolve)); // The bridge's end-of-capture callback, once a drain has finished. const drained = () => { bridge.draining = false; host.syncContinuousCapture(); }; const session = phase => { host.state = { phase }; if (phase !== 'connected') bridge.handleSessionEnded(); host.syncContinuousSession(); }; 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'); // Translate needs its own provider and language: the shared capture drains // and restarts on them, again when only the language changes, and moves // back to the main provider (and Whisper's own language) once Translate closes. calls.length = 0; host.startContinuousVoiceCapture('translate', { transcription: { provider: 'soniox', language: '' } }); await settle(); assert.deepEqual(calls, [['stop']]); drained(); await settle(); assert.deepEqual(calls, [['stop'], ['glasses', true]]); assert.deepEqual(providers.slice(-2), ['whisper-server:en', 'soniox:']); calls.length = 0; host.startContinuousVoiceCapture('translate', { transcription: { provider: 'soniox', language: 'ja' } }); drained(); await settle(); assert.deepEqual(calls, [['stop'], ['glasses', true]]); assert.equal(providers.at(-1), 'soniox:ja'); calls.length = 0; host.stopContinuousVoiceCapture('translate'); drained(); await settle(); assert.deepEqual(calls, [['stop'], ['glasses', true]]); assert.equal(providers.at(-1), 'whisper-server:en'); // A reconnect drains the capture, waits for the glasses, and resumes on them. calls.length = 0; session('retrying'); drained(); await settle(); assert.deepEqual(calls, []); assert.deepEqual(claims.at(-1), [false, 'continuous-capture'], 'other mic users may run meanwhile'); session('connected'); await settle(); assert.deepEqual(calls, [['glasses', true]]); // Locking drains; unlocking resumes. EvenHub apps' raw mic tap follows the lock too. calls.length = 0; host.setGlassesLocked(true); drained(); await settle(); assert.deepEqual(calls, []); assert.equal(bridge.rawSession, null, 'locking unbinds the raw mic session'); host.setGlassesLocked(false); await settle(); assert.deepEqual(calls, [['glasses', true]]); assert.equal(bridge.rawSession, host.communicator, 'unlocking rebinds it to the connected session'); // A failed capture is not retried in a loop, only on the next session. calls.length = 0; bridge.source = null; drained(); await settle(); assert.deepEqual(calls, []); // Without glasses the phone mic stands in while in the foreground, and moves to the glasses when they connect. host.communicator = null; session('disconnected'); await settle(); assert.deepEqual(calls, [['phone', true]]); calls.length = 0; host.pause(); drained(); await settle(); assert.deepEqual(calls, [], 'no phone capture in the background'); host.active = true; host.syncContinuousCapture(); await settle(); assert.deepEqual(calls, [['phone', true]]); calls.length = 0; host.communicator = {}; session('connected'); await settle(); assert.deepEqual(calls, [['stop']]); drained(); await settle(); assert.deepEqual(calls, [['stop'], ['glasses', true]]); // The last window closing stops it for good. calls.length = 0; host.stopContinuousVoiceCapture('transcribe'); drained(); session('retrying'); session('connected'); await settle(); assert.deepEqual(calls, [['stop']]); assert.equal(bridge.recording, ''); assert.deepEqual(claims.at(-1), [false, 'continuous-capture']); }); test('iOS keyboard action opens the shared destination session and remains blocked while locked', () => { let opened = 0; const { IosPreviewController } = load('app/g2/ios-preview-controller.ts', { require: id => id === '../ui/shell/shell' ? { shell: { startKeyboardInput: () => opened++ } } : {}, }); const host = Object.create(IosPreviewController.prototype); host.active = true; host.glassesLocked = false; host.typeIntoApp(); assert.equal(opened, 1); host.glassesLocked = true; host.typeIntoApp(); assert.equal(opened, 1); host.glassesLocked = false; host.active = false; host.typeIntoApp(); assert.equal(opened, 1); }); test('iOS curve editor retains invalid draft and retries Save with a validation message', async () => { const prompts = [], errors = [], saved = []; const answers = [{ result: true, text: '0:0,5:' }, { result: true, text: '0:0,5:100' }]; const { IosPreviewController } = load('app/g2/ios-preview-controller.ts', { require: id => id === '@nativescript/core' ? { Dialogs: { prompt: async options => { prompts.push(options.defaultText); return answers.shift(); }, alert: async options => errors.push(options.message) } } : {} }); const host = Object.create(IosPreviewController.prototype); host.active = true; const curve = require('../.test-build/app/g2/brightness-curve.js'); assert.equal(await host.editSetting({ editorTitle: 'Auto light curve', get: () => '0:0,8:100', set: value => saved.push(value), validationError: curve.brightnessCurveError }), true); assert.deepEqual(prompts, ['0:0,8:100', '0:0,5:']); assert.equal(errors.length, 1); assert.deepEqual(saved, ['0:0,5:100']); });