| 1 | // 1:1 call end-to-end test against the Echo bot with the deployed Windows app: no second person needed. |
| 2 | // The app's mic is VB-CABLE fed with a speech file; the bot plays its prompt, records us and plays it back. |
| 3 | // Checks that the app hears the bot, that our speech comes back, and that hold and resume stop and restart media. |
| 4 | // Usage: node scripts/call-echo-e2e.mjs <work dir> |
| 5 | import { spawn, execFileSync } from 'node:child_process'; |
| 6 | import { readFileSync, writeFileSync } from 'node:fs'; |
| 7 | |
| 8 | const [workDir] = process.argv.slice(2); |
| 9 | const PROBE_DIR = process.env.PROBE_DIR ?? '/mnt/c/Users/dd/AppData/Local/Temp/call-e2e'; |
| 10 | const TEAMS_EXE = process.env.TEAMS_EXE ?? '/mnt/c/Users/dd/AppData/Local/Programs/ms-teams-linux/teams.exe'; |
| 11 | const POWERSHELL = '/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe'; |
| 12 | const TASKLIST = '/mnt/c/Windows/System32/tasklist.exe'; |
| 13 | const SCRIPT_DIR = new URL('.', import.meta.url).pathname; |
| 14 | const CONNECT_WAIT_MS = 12_000; |
| 15 | const LISTEN_SECONDS = 40; |
| 16 | const HOLD_SETTLE_MS = 6_000; |
| 17 | const HOLD_MEASURE_MS = 6_000; |
| 18 | const PLAY_SECONDS = 150; |
| 19 | const APP_THRESHOLD = { speech: 200, floor: 20 }; |
| 20 | const SPEECH_TO_PAUSE = 4; |
| 21 | const RESUMED_PACKETS = 100; |
| 22 | const HELD_PACKETS = 20; |
| 23 | |
| 24 | const windowsPath = (path) => execFileSync('wslpath', ['-w', path]).toString().trim(); |
| 25 | const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); |
| 26 | const log = (message) => console.error(`[${new Date().toISOString().slice(11, 19)}] ${message}`); |
| 27 | |
| 28 | function defaultAudio(...args) { |
| 29 | const script = windowsPath(SCRIPT_DIR + 'default-audio.ps1'); |
| 30 | return execFileSync(POWERSHELL, ['-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', script, ...args]).toString().trim(); |
| 31 | } |
| 32 | |
| 33 | function teamsProcessId() { |
| 34 | const rows = execFileSync(TASKLIST, ['/FI', 'IMAGENAME eq teams.exe', '/FO', 'CSV', '/NH']).toString().trim().split(/\r?\n/); |
| 35 | const processes = rows |
| 36 | .map((row) => row.split('","').map((cell) => cell.replace(/"/g, ''))) |
| 37 | .filter((cells) => cells[0]?.toLowerCase() === 'teams.exe') |
| 38 | .map((cells) => ({ id: Number(cells[1]), memory: Number(cells[4].replace(/\D/g, '')) })); |
| 39 | processes.sort((left, right) => right.memory - left.memory); |
| 40 | return processes[0]?.id; |
| 41 | } |
| 42 | |
| 43 | const runTeams = (...args) => spawn(TEAMS_EXE, args, { detached: true, stdio: 'ignore' }).unref(); |
| 44 | |
| 45 | function runProbe(...args) { |
| 46 | return new Promise((resolve, reject) => { |
| 47 | const child = spawn(PROBE_DIR + '/audio_probe.exe', args, { stdio: ['ignore', 'pipe', 'pipe'] }); |
| 48 | let output = ''; |
| 49 | let errors = ''; |
| 50 | child.stdout.on('data', (chunk) => (output += chunk)); |
| 51 | child.stderr.on('data', (chunk) => (errors += chunk)); |
| 52 | child.on('exit', (code) => (code === 0 ? resolve(output) : reject(new Error(`audio_probe ${args[0]}: ${errors.trim()}`)))); |
| 53 | }); |
| 54 | } |
| 55 | |
| 56 | async function appStats() { |
| 57 | const path = workDir + '/app-stats.json'; |
| 58 | writeFileSync(path, ''); |
| 59 | runTeams('--call', `stats ${windowsPath(path)}`); |
| 60 | for (let attempt = 0; attempt < 6; attempt++) { |
| 61 | await delay(1_500); |
| 62 | const text = readFileSync(path, 'utf8'); |
| 63 | if (text) return JSON.parse(text); |
| 64 | } |
| 65 | throw new Error('the app wrote no stats'); |
| 66 | } |
| 67 | |
| 68 | function judge(levels) { |
| 69 | const sorted = [...levels].sort((left, right) => left - right); |
| 70 | const percentile = (share) => sorted[Math.min(sorted.length - 1, Math.floor(share * sorted.length))] ?? 0; |
| 71 | const [p10, p90] = [percentile(0.1), percentile(0.9)]; |
| 72 | return { pass: p90 >= APP_THRESHOLD.speech && p90 >= SPEECH_TO_PAUSE * Math.max(p10, APP_THRESHOLD.floor), p10, p90 }; |
| 73 | } |
| 74 | |
| 75 | async function mediaDelta(ms) { |
| 76 | const before = await appStats(); |
| 77 | await delay(ms); |
| 78 | const after = await appStats(); |
| 79 | return { app: after, sent: after.audioOutPackets - before.audioOutPackets, received: after.audioInPackets - before.audioInPackets }; |
| 80 | } |
| 81 | |
| 82 | const report = { started: new Date().toISOString(), checks: {} }; |
| 83 | const savedCapture = defaultAudio('get', 'capture'); |
| 84 | const restoreCapture = () => defaultAudio('set', 'capture', savedCapture); |
| 85 | for (const signal of ['SIGINT', 'SIGTERM']) process.on(signal, () => { restoreCapture(); process.exit(130); }); |
| 86 | try { |
| 87 | if (!teamsProcessId()) throw new Error('teams.exe is not running'); |
| 88 | defaultAudio('set', 'capture', 'CABLE Output'); |
| 89 | const playing = runProbe('play', 'CABLE Input', windowsPath(PROBE_DIR + '/app.wav'), String(PLAY_SECONDS)); |
| 90 | runTeams('--call', 'echo'); |
| 91 | log(`echo call placed, waiting ${CONNECT_WAIT_MS / 1000}s`); |
| 92 | await delay(CONNECT_WAIT_MS); |
| 93 | runTeams('--unmute'); |
| 94 | const connected = await appStats(); |
| 95 | report.checks.connected = { pass: connected.inCall && /Connected/.test(connected.state), state: connected.state }; |
| 96 | if (!report.checks.connected.pass) throw new Error(`echo call not connected: ${connected.state}`); |
| 97 | log(`listening ${LISTEN_SECONDS}s for the prompt and our echo`); |
| 98 | const recorded = await runProbe('record-process', String(teamsProcessId()), String(LISTEN_SECONDS), windowsPath(workDir + '/echo-heard.wav')); |
| 99 | report.checks.heard = judge(JSON.parse(recorded).rms); |
| 100 | runTeams('--call', 'hold'); |
| 101 | await delay(HOLD_SETTLE_MS); |
| 102 | const held = await mediaDelta(HOLD_MEASURE_MS); |
| 103 | report.checks.hold = { pass: held.sent <= HELD_PACKETS && /Local/.test(held.app.hold), sent: held.sent, received: held.received, hold: held.app.hold }; |
| 104 | runTeams('--call', 'resume'); |
| 105 | await delay(HOLD_SETTLE_MS); |
| 106 | const resumed = await mediaDelta(HOLD_MEASURE_MS); |
| 107 | report.checks.resume = { pass: resumed.sent >= RESUMED_PACKETS && resumed.received >= RESUMED_PACKETS && /Active/.test(resumed.app.hold), sent: resumed.sent, received: resumed.received, hold: resumed.app.hold }; |
| 108 | await playing.catch((error) => (report.playError = error.message)); |
| 109 | } catch (error) { |
| 110 | report.error = error.message; |
| 111 | } finally { |
| 112 | if (teamsProcessId()) runTeams('--hang-up'); |
| 113 | restoreCapture(); |
| 114 | } |
| 115 | report.pass = !report.error && Object.values(report.checks).every((check) => check.pass); |
| 116 | writeFileSync(workDir + '/echo-report.json', JSON.stringify(report, null, 2)); |
| 117 | console.log(JSON.stringify({ pass: report.pass, checks: report.checks, error: report.error })); |
| 118 | process.exit(report.pass ? 0 : 1); |