BlueGit

ict rusty-teams

Files

rusty-teams / scripts / call-echo-e2e.mjs

call-echo-e2e.mjs

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