// Headless native proof: captures ONLY two owned quiet tone processes; never private/system audio. import { spawn } from 'node:child_process'; import { once } from 'node:events'; import { createInterface } from 'node:readline'; import { resolve } from 'node:path'; import assert from 'node:assert/strict'; const core = resolve(process.argv[2] || 'core/target/debug/teamspeach-core.exe'); const fixture = resolve('scripts/lib/app-audio-tone.ps1'); const isolatedDevice = process.env.TEAMSPEACH_TEST_ISOLATED_WAVEOUT_DEVICE_ID; if (process.env.TEAMSPEACH_TEST_ISOLATED_AUDIO_CONFIRMED !== '1' || !/^\d{1,5}$/.test(isolatedDevice || '') || Number(isolatedDevice) > 65535) { throw Error('Native tone proof blocked: explicitly confirmed isolated waveOut test endpoint required; default/physical audio is forbidden.'); } const owned = new Set(); function child(binary, args) { const c = spawn(binary, args, { windowsHide: true, stdio: ['pipe', 'pipe', 'pipe'] }); owned.add(c); c.once('exit', () => owned.delete(c)); c.errors = ''; c.stderr.on('data', b => { c.errors = (c.errors + b).slice(-8192); }); return c; } function deadline(promise, ms = 15000) { let timer; return Promise.race([promise, new Promise((_, reject) => { timer = setTimeout(() => reject(Error('Native proof timeout')), ms); })]).finally(() => clearTimeout(timer)); } function firstJson(c) { const lines = createInterface({ input: c.stdout }); return deadline(new Promise((resolve, reject) => { lines.once('line', line => { lines.close(); try { resolve(JSON.parse(line)); } catch (error) { reject(error); } }); c.once('exit', code => reject(Error(`Exited ${code}: ${c.errors}`))); })); } async function tone(frequency) { const c = child('C:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe', ['-NoProfile', '-NonInteractive', '-WindowStyle', 'Hidden', '-ExecutionPolicy', 'Bypass', '-File', fixture, '-Frequency', String(frequency), '-DeviceId', isolatedDevice, '-IsolatedEndpointConfirmed']); const ready = await firstJson(c); assert.equal(ready.ready, true); assert.equal(ready.pid, c.pid); return c; } async function listing() { const c = child(core, ['app-audio', 'list']); const result = await firstJson(c); await deadline(once(c, 'exit')); return result; } async function stop(c) { if (c.exitCode !== null) return; const exited = once(c, 'exit'); c.stdin.end(); const [code] = await deadline(exited, 2000); assert.equal(code, 0, c.errors); } function amplitude(samples, frequency) { let re = 0, im = 0; for (let i = 0; i < samples.length; i++) { const a = i * 2 * Math.PI * frequency / 48000; re += samples[i] * Math.cos(a); im += samples[i] * Math.sin(a); } return Math.hypot(re, im) * 2 / samples.length; } try { const a = await tone(440), b = await tone(997); const list = await listing(); assert.equal(list.supported, true); const target = list.applications.find(app => app.pid === a.pid); assert(target, 'Owned 440 Hz process must be an observed audio session'); assert(list.applications.some(app => app.pid === b.pid), 'Owned exclusion process must be an observed audio session'); const capture = child(core, ['app-audio', 'capture', target.id]); const samples = [], lines = createInterface({ input: capture.stdout }); let format; await deadline(new Promise((resolve, reject) => { capture.once('exit', code => reject(Error(`Capture exited ${code}: ${capture.errors}`))); lines.on('line', line => { try { const packet = JSON.parse(line); if (packet.event === 'format') { format = packet; return; } if (packet.event !== 'pcm') return; const bytes = Buffer.from(packet.pcm, 'base64'); assert.equal(bytes.length, 3840); for (let i = 0; i < bytes.length; i += 4) samples.push(bytes.readInt16LE(i) / 32768); if (samples.length >= 48000) { lines.removeAllListeners('line'); resolve(); } } catch (error) { reject(error); } }); })); assert.equal(format.sampleRate, 48000); assert.equal(format.channels, 2); const wanted = amplitude(samples, 440), unwanted = amplitude(samples, 997); assert(wanted > 0.00001, `Generated signal missing: ${wanted}`); assert(unwanted < wanted / 20, `Excluded generated process leaked: ${unwanted}/${wanted}`); const start = performance.now(); await stop(capture); const stopMs = performance.now() - start; await stop(a); const stale = child(core, ['app-audio', 'capture', target.id]); const [staleCode] = await deadline(once(stale, 'exit')); assert.notEqual(staleCode, 0); assert.match(stale.errors, /stale|no longer|exited/); const malformed = child(core, ['app-audio', 'capture', 'not-an-observed-identity']); const [badCode] = await deadline(once(malformed, 'exit')); assert.notEqual(badCode, 0); await stop(b); console.log(JSON.stringify({ pass: true, isolatedGeneratedPid: target.pid, wanted440Amplitude: wanted, excluded997Amplitude: unwanted, isolationDb: 20 * Math.log10(wanted / Math.max(unwanted, 1e-12)), stopMs, staleRejected: true, invalidRejected: true, privateAudioCaptured: false, windowsShown: false })); } finally { for (const c of owned) { c.stdin.end(); c.kill(); } }