// Real-device context menus (NO fake-media flags, no server, no capture): right-click and Shift+F10 on the // footer microphone/headphones buttons open the device menu without toggling mute/deafen; choosing an // entry updates Settings and persists; Escape restores focus. // node scripts/smoke-devices-menu.mjs --out docs/evidence/runtime/ [--exe packaged.exe] import { launchApp } from './lib/electron-app.mjs'; import { mkdirSync, writeFileSync, mkdtempSync, rmSync } from 'node:fs'; import { tmpdir } from 'node:os'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'); const argv = process.argv.slice(2); const opt = (n, d) => (argv.indexOf(`--${n}`) >= 0 ? argv[argv.indexOf(`--${n}`) + 1] : d); const out = path.resolve(root, opt('out', 'smoke-out')); const exe = opt('exe'); mkdirSync(out, { recursive: true }); const profile = mkdtempSync(path.join(tmpdir(), 'teamspeach-menu-')); const result = { steps: [], console: [] }; const step = (name, data = {}) => { result.steps.push({ name, ...data }); console.log(`[menu] ${name}`, JSON.stringify(data)); }; async function launch(label) { const app = await launchApp({ exe, profile }); const page = await app.firstWindow(); page.on('console', (m) => result.console.push(`${label} ${m.type()}: ${m.text()}`)); page.on('pageerror', (e) => result.console.push(`${label} pageerror: ${e.message}`)); await page.setViewportSize({ width: 1280, height: 800 }).catch(() => {}); return { app, page }; } const state = (page) => page.evaluate(() => ({ audio: window.teamspeachDiagnostics.snapshot().audio, capturing: window.teamspeachDiagnostics.snapshot().voice.capturing, framesForwarded: window.teamspeachDiagnostics.snapshot().voice.framesForwarded, menu: Boolean(document.querySelector('.device-menu')), items: Array.from(document.querySelectorAll('.device-menu [role="menuitemradio"]')).map((i) => ({ label: i.textContent, checked: i.getAttribute('aria-checked') })), focused: document.activeElement?.id || document.activeElement?.className || '', inside: (() => { const m = document.querySelector('.device-menu'); if (!m) return null; const r = m.getBoundingClientRect(); return r.left >= 0 && r.top >= 0 && r.right <= innerWidth && r.bottom <= innerHeight; })(), })); let app; let page; let failed = null; try { ({ app, page } = await launch('run1')); await page.waitForSelector('#view-onboarding', { state: 'visible', timeout: 20000 }); await page.fill('#onboarding-nick', 'MenuSmoke'); await page.click('#onboarding-submit'); await page.waitForSelector('#view-app', { state: 'visible', timeout: 20000 }); await page.click('#btn-mute', { button: 'right' }); await page.waitForSelector('.device-menu [role="menuitemradio"]'); let s = await state(page); step('mic right-click', { menu: s.menu, inputMuted: s.audio.input_muted, capturing: s.capturing, framesForwarded: s.framesForwarded, items: s.items, insideViewport: s.inside, focused: s.focused }); if (s.audio.input_muted) throw new Error('right-click toggled mute'); await page.screenshot({ path: path.join(out, '62-mic-device-menu.png') }); const pick = s.items[Math.min(2, s.items.length - 1)].label; await page.click(`.device-menu [role="menuitemradio"]:has-text("${pick.replace(/"/g, '\\"')}")`); await page.waitForTimeout(700); await page.click('#btn-settings'); await page.click('#settings-nav .nav-item[data-panel="audio"]'); const settingsInput = await page.$eval('#input-device', (el) => el.selectedOptions[0].textContent); step('menu choice reflected in settings', { pick, settingsInput, same: pick === settingsInput }); if (pick !== settingsInput) throw new Error('settings not updated from menu'); await page.click('#settings-back'); // Keyboard: Shift+F10 on the headphones button, arrows, Escape restores focus; deafen untouched. await page.focus('#btn-deafen'); await page.keyboard.press('Shift+F10'); await page.waitForSelector('.device-menu [role="menuitemradio"]'); await page.keyboard.press('ArrowDown'); s = await state(page); step('headphones Shift+F10', { menu: s.menu, outputMuted: s.audio.output_muted, items: s.items.map((i) => i.label), insideViewport: s.inside }); await page.screenshot({ path: path.join(out, '63-output-device-menu.png') }); await page.keyboard.press('Escape'); s = await state(page); step('escape', { menuClosed: !s.menu, focusRestored: s.focused === 'btn-deafen', outputMuted: s.audio.output_muted }); if (s.menu || s.focused !== 'btn-deafen' || s.audio.output_muted) throw new Error('escape/focus/deafen behaviour wrong'); await page.click('#btn-deafen', { button: 'right' }); await page.waitForSelector('.device-menu [role="menuitemradio"]'); const outPick = (await state(page)).items[Math.min(2, (await state(page)).items.length - 1)].label; await page.keyboard.press('Escape'); await page.click('#btn-deafen', { button: 'right' }); await page.waitForSelector('.device-menu [role="menuitemradio"]'); await page.click(`.device-menu [role="menuitemradio"]:has-text("${outPick.replace(/"/g, '\\"')}")`); await page.waitForTimeout(700); // Left click still toggles as before. await page.click('#btn-mute'); s = await state(page); step('left click still toggles mute', { inputMuted: s.audio.input_muted }); await page.click('#btn-mute'); // Races: enumeration is slowed to 600 ms. A second menu request or a dismissal while the first is // pending must leave at most one current menu and never reopen or steal focus later. await page.evaluate(() => { const md = navigator.mediaDevices; const original = md.enumerateDevices.bind(md); md.enumerateDevices = () => new Promise((resolve, reject) => setTimeout(() => original().then(resolve, reject), 600)); }); await page.click('#btn-mute', { button: 'right' }); await page.click('#btn-deafen', { button: 'right' }); await page.waitForTimeout(1500); const afterSwitch = await page.evaluate(() => ({ menus: document.querySelectorAll('.device-menu').length, outputExpanded: document.querySelector('#btn-deafen').getAttribute('aria-expanded'), inputExpanded: document.querySelector('#btn-mute').getAttribute('aria-expanded'), items: document.querySelectorAll('.device-menu [role="menuitemradio"]').length, })); step('rapid mic → headphones while enumeration pending', afterSwitch); if (afterSwitch.menus !== 1 || afterSwitch.outputExpanded !== 'true' || afterSwitch.inputExpanded !== 'false' || afterSwitch.items < 2) throw new Error('stale or duplicate menu'); await page.keyboard.press('Escape'); await page.click('#btn-mute', { button: 'right' }); await page.click('#connect-address'); await page.waitForTimeout(1500); const afterDismiss = await page.evaluate(() => ({ menus: document.querySelectorAll('.device-menu').length, focused: document.activeElement?.id ?? '', expanded: document.querySelector('#btn-mute').getAttribute('aria-expanded'), })); step('dismissed before enumeration finished', afterDismiss); if (afterDismiss.menus !== 0 || afterDismiss.focused !== 'connect-address' || afterDismiss.expanded !== 'false') throw new Error('menu reopened or focus stolen'); await app.close(); ({ app, page } = await launch('run2')); await page.waitForSelector('#view-app', { state: 'visible', timeout: 20000 }); await page.click('#btn-mute', { button: 'right' }); await page.waitForSelector('.device-menu [role="menuitemradio"]'); const checkedIn = (await state(page)).items.find((i) => i.checked === 'true')?.label; await page.keyboard.press('Escape'); await page.click('#btn-deafen', { button: 'right' }); await page.waitForSelector('.device-menu [role="menuitemradio"]'); const checkedOut = (await state(page)).items.find((i) => i.checked === 'true')?.label; step('after restart', { checkedIn, checkedOut, persisted: checkedIn === pick && checkedOut === outPick }); if (checkedIn !== pick || checkedOut !== outPick) throw new Error('menu choices not persisted'); } catch (err) { failed = err; step('FAILED', { error: String(err.stack || err) }); if (page) await page.screenshot({ path: path.join(out, 'fail.png') }).catch(() => {}); } finally { if (app) await app.close().catch(() => {}); rmSync(profile, { recursive: true, force: true }); } result.ok = !failed; writeFileSync(path.join(out, 'result.json'), JSON.stringify(result, null, 2)); console.log(`[menu] ${result.ok ? 'OK' : 'FAILED'} → ${out}`); process.exit(result.ok ? 0 : 1);