'use strict'; const test = require('node:test'); const assert = require('node:assert/strict'); const { EventEmitter } = require('node:events'); const { PassThrough } = require('node:stream'); const { AppAudioController, readLines, validPcm, MAX_IN_FLIGHT } = require('../app/main/appaudio-main'); const app = { id: 'wasapi:42:1234', label: 'Owned test tone', pid: 42, ancestors: [1] }; const pcm = sequence => ({ event: 'pcm', sequence, pcm: Buffer.alloc(3840).toString('base64') }); const format = { event: 'format', sampleRate: 48000, channels: 2, format: 's16le', frames: 960 }; const tick = () => new Promise(resolve => setImmediate(resolve)); function fixture({ apps = [app], listAutomatic = true, scope = 'process-tree' } = {}) { const frame = {}; const events = []; const children = []; const contents = new EventEmitter(); contents.send = (_channel, data) => events.push(data); const controller = new AppAudioController({ resolveBinary: () => 'fixed-core.exe', getWindow: () => ({ isDestroyed: () => false, webContents: contents }), trustedFrame: candidate => candidate === frame, ownPid: 99, spawnProcess(binary, args) { assert.equal(binary, 'fixed-core.exe'); assert.equal(args[0], 'app-audio'); const child = new EventEmitter(); Object.assign(child, { stdin: new PassThrough(), stdout: new PassThrough(), stderr: new PassThrough(), exitCode: null, args }); child.kill = () => { child.exitCode = 1; child.emit('close', 1); }; child.stdin.on('finish', () => { child.exitCode = 0; child.emit('close', 0); }); child.packet = value => child.stdout.write(`${JSON.stringify(value)}\n`); children.push(child); if (args[1] === 'list' && listAutomatic) queueMicrotask(() => { child.packet({ supported: true, scope, hostAncestors: [99, 98], platform: scope === 'process-tree' ? 'win32' : 'linux', applications: apps }); child.exitCode = 0; child.emit('close', 0); }); return child; }, }); const authorize = () => { const { appAudioSessionId } = controller.authorizeSelection(frame, 'window:owned'); controller.confirmSelection(frame); return appAudioSessionId; }; return { controller, frame, events, children, authorize }; } test('PCM parser rejects malformed, oversized and replay frame shapes', () => { assert.equal(validPcm(pcm(0)), true); for (const value of [null, pcm(-1), pcm(1.5), { ...pcm(0), pcm: 'A'.repeat(5121) }, { ...pcm(0), pcm: '='.repeat(5120) }]) assert.equal(Boolean(validPcm(value)), false); const stream = new PassThrough(); const errors = []; readLines(stream, 10, () => assert.fail('No oversized line should parse'), cause => errors.push(cause)); stream.write('x'.repeat(11)); stream.write('x'.repeat(11)); assert.equal(errors.length, 1); }); test('fresh lists exclude TeamsPeach and descendants; selection is one-use and observed', async () => { const f = fixture({ apps: [app, { ...app, id: 'self', pid: 99 }, { ...app, id: 'child', pid: 100, ancestors: [99] }] }); assert.deepEqual((await f.controller.list()).applications, [app]); const sessionId = f.authorize(); assert.equal((await f.controller.start({}, { sessionId, ids: [app.id] })).ok, false); assert.equal((await f.controller.start(f.frame, { sessionId, ids: ['arbitrary:pid'] })).error.code, 'appaudio_not_observed'); const started = f.controller.start(f.frame, { sessionId, ids: [app.id] }); await tick(); const capture = f.children.find(child => child.args[1] === 'capture'); capture.packet(format); assert.equal((await started).ok, true); assert.equal((await f.controller.start(f.frame, { sessionId, ids: [app.id] })).ok, false); f.controller.stopAll(); }); test('bounded IPC window requires owning session acknowledgements and rejects replay', async () => { const f = fixture(); await f.controller.list(); const sessionId = f.authorize(); const started = f.controller.start(f.frame, { sessionId, ids: [app.id] }); await tick(); const child = f.children.at(-1); child.packet(format); await started; for (let i = 0; i < 12; i++) child.packet(pcm(i)); assert.equal(f.events.length, MAX_IN_FLIGHT); f.controller.ack({}, { sessionId, token: 1 }); child.packet(pcm(12)); assert.equal(f.events.length, MAX_IN_FLIGHT); f.controller.ack(f.frame, { sessionId, token: 1 }); child.packet(pcm(13)); assert.equal(f.events.length, MAX_IN_FLIGHT + 1); child.packet(pcm(13)); assert.equal(f.controller.active, null); assert.equal(f.events.some(value => value.event === 'error'), true); }); test('stop during fresh enumeration cannot launch a late capture', async () => { const f = fixture({ listAutomatic: false }); f.controller.observed.set(app.id, app); const sessionId = f.authorize(); const started = f.controller.start(f.frame, { sessionId, ids: [app.id] }); f.controller.stop(f.frame, { sessionId }); f.children[0].packet({ supported: true, scope: 'process-tree', applications: [app] }); f.children[0].emit('close', 0); assert.equal((await started).ok, false); assert.equal(f.children.some(child => child.args[1] === 'capture'), false); }); test('overlapping process trees and stale observed identities never capture', async () => { const nested = { id: 'wasapi:43:55', label: 'Owned child', pid: 43, ancestors: [42, 1] }; const f = fixture({ apps: [app, nested] }); await f.controller.list(); const sessionId = f.authorize(); const response = await f.controller.start(f.frame, { sessionId, ids: [app.id, nested.id] }); assert.equal(response.error.code, 'appaudio_overlap'); assert.equal(f.children.some(child => child.args[1] === 'capture'), false); f.controller.observed.set('stale', { ...app, id: 'stale' }); assert.equal((await f.controller.start(f.frame, { sessionId: f.authorize(), ids: ['stale'] })).error.code, 'appaudio_selection_expired'); }); test('cancel before native format rejects pending start and terminates the helper', async () => { const f = fixture(); await f.controller.list(); const sessionId = f.authorize(); const started = f.controller.start(f.frame, { sessionId, ids: [app.id] }); await tick(); const child = f.children.at(-1); f.controller.stop(f.frame, { sessionId }); assert.equal((await started).ok, false); assert.equal(child.stdin.writableEnded, true); child.packet(format); child.packet(pcm(0)); assert.equal(f.events.some(value => value.event === 'pcm'), false); }); test('audio-stream scope permits separately observed streams belonging to one PID', async () => { const f = fixture({ scope: 'audio-stream', apps: [app, { ...app, id: 'pulse:second' }] }); const listed = await f.controller.list(); assert.equal(listed.scope, 'audio-stream'); const sessionId = f.authorize(); const started = f.controller.start(f.frame, { sessionId, ids: [app.id, 'pulse:second'] }); await tick(); const captures = f.children.filter(child => child.args[1] === 'capture'); assert.equal(captures.length, 2); for (const child of captures) child.packet(format); assert.equal((await started).ok, true); f.controller.stopAll(); }); test('all scope captures observed safe roots rather than selected ids', async () => { const child = { id: 'child', label: 'Child', pid: 43, ancestors: [42, 1] }; const other = { id: 'other', label: 'Other', pid: 44, ancestors: [1] }; const f = fixture({ apps: [app, child, other, { ...app, id: 'self', pid: 99 }] }); await f.controller.list(); const sessionId = f.authorize(); const start = f.controller.start(f.frame, { sessionId, ids: [], scope: 'all' }); await tick(); const captures = f.children.filter(child => child.args[1] === 'capture'); assert.deepEqual(captures.map(child => child.args[2]).sort(), [app.id, other.id].sort()); captures.forEach(child => child.packet(format)); assert.equal((await start).ok, true); assert.equal(f.events.find(event => event.event === 'applications').applications.length, 2); f.controller.stopAll(); }); test('exclude scope excludes selected processes; overlapping child exclusion fails closed', async () => { const other = { id: 'other', label: 'Other', pid: 44, ancestors: [1] }; const f = fixture({ apps: [app, other] }); await f.controller.list(); const start = f.controller.start(f.frame, { sessionId: f.authorize(), ids: [app.id], scope: 'exclude' }); await tick(); const captures = f.children.filter(child => child.args[1] === 'capture'); assert.deepEqual(captures.map(child => child.args[2]), [other.id]); captures[0].packet(format); assert.equal((await start).ok, true); f.controller.stopAll(); const g = fixture({ apps: [app, { id: 'nested', label: 'Nested', pid: 43, ancestors: [42] }] }); await g.controller.list(); const result = await g.controller.start(g.frame, { sessionId: g.authorize(), ids: ['nested'], scope: 'exclude' }); assert.equal(result.ok, false); assert.match(result.error.message, /ancestor/); assert.equal(g.children.some(child => child.args[1] === 'capture'), false); }); test('all scope rejects resource overflow without truncating applications', async () => { const f = fixture({ apps: Array.from({ length: 9 }, (_, i) => ({ id: `app${i}`, label: 'Generated', pid: i + 200, ancestors: [] })) }); await f.controller.list(); const result = await f.controller.start(f.frame, { sessionId: f.authorize(), ids: [], scope: 'all' }); assert.equal(result.ok, false); assert.match(result.error.message, /eight/); assert.equal(f.children.some(child => child.args[1] === 'capture'), false); });