// Capture lifecycle of the AudioEngine with stubbed Web Audio / getUserMedia: a stop or a newer start // while the microphone request is pending must never leave a microphone open. import test from 'node:test'; import assert from 'node:assert/strict'; import { AudioEngine } from '../app/renderer/js/audio/engine.js'; function fakeStream(name) { const track = { label: name, muted: false, stopped: false, stop() { this.stopped = true; }, addEventListener() {}, getSettings: () => ({ sampleRate: 48000, channelCount: 1 }), }; return { name, track, getTracks: () => [track], getAudioTracks: () => [track] }; } const node = () => ({ connect(next) { return next ?? this; }, disconnect() {}, port: {}, gain: { value: 1 } }); /** Engine whose getUserMedia calls stay pending until the test resolves them in order. */ function setup() { const pending = []; Object.defineProperty(globalThis, 'navigator', { configurable: true, value: { mediaDevices: { getUserMedia: () => new Promise((resolve, reject) => pending.push({ resolve, reject })) } }, }); globalThis.AudioWorkletNode = class { constructor() { Object.assign(this, node()); } }; const engine = new AudioEngine({ onFrame() {}, log() {} }); engine.init = async () => {}; engine.ctx = { createMediaStreamSource: () => node(), createGain: node, destination: {} }; return { engine, pending, flush: () => new Promise((r) => setTimeout(r, 0)) }; } test('stop while the microphone request is pending closes the late stream', async () => { const { engine, pending, flush } = setup(); const started = engine.startCapture({}); await flush(); assert.equal(engine.captureStarting, true); engine.stopCapture(); assert.equal(engine.captureStarting, false); const stream = fakeStream('late'); pending[0].resolve(stream); assert.equal(await started, null); assert.equal(stream.track.stopped, true); assert.equal(engine.capturing, false); }); test('a newer start supersedes a pending one; only the newest stream stays open', async () => { const { engine, pending, flush } = setup(); const first = engine.startCapture({}); await flush(); const second = engine.startCapture({}); await flush(); const a = fakeStream('first'); const b = fakeStream('second'); pending[1].resolve(b); assert.equal(await second, 'second'); pending[0].resolve(a); assert.equal(await first, null); assert.equal(a.track.stopped, true); assert.equal(b.track.stopped, false); assert.equal(engine.capture.stream, b); engine.stopCapture(); assert.equal(b.track.stopped, true); assert.equal(engine.capturing, false); }); test('a failure of a superseded start is ignored; a current failure is reported', async () => { const { engine, pending, flush } = setup(); const stale = engine.startCapture({}); await flush(); engine.stopCapture(); pending[0].reject(Object.assign(new Error('denied'), { name: 'NotAllowedError' })); assert.equal(await stale, null); const current = engine.startCapture({}); await flush(); pending[1].reject(Object.assign(new Error('denied'), { name: 'NotAllowedError' })); await assert.rejects(current, { name: 'NotAllowedError' }); assert.equal(engine.captureStarting, false); }); test('a missing explicit device is an error with no second request for another microphone', async () => { const { engine, pending, flush } = setup(); const start = engine.startCapture({ deviceId: 'gone' }); await flush(); pending[0].reject(Object.assign(new Error('not found'), { name: 'OverconstrainedError' })); await assert.rejects(start, { code: 'device_unavailable' }); await flush(); assert.equal(pending.length, 1); assert.equal(engine.capturing, false); }); test('a failure while building the capture graph releases the opened microphone', async () => { const { engine, pending, flush } = setup(); const disconnected = []; engine.ctx.createMediaStreamSource = () => ({ connect: (n) => n, disconnect: () => disconnected.push('source') }); globalThis.AudioWorkletNode = class { constructor() { throw new Error('worklet not registered'); } }; const start = engine.startCapture({}); await flush(); const stream = fakeStream('mic'); pending[0].resolve(stream); await assert.rejects(start, /worklet not registered/); assert.equal(stream.track.stopped, true); assert.deepEqual(disconnected, ['source']); assert.equal(engine.capturing, false); assert.equal(engine.captureStarting, false); });