import test from 'node:test'; import assert from 'node:assert/strict'; import { acquireCapture, cancelCapture, listCaptureCameras, getCaptureSessionId } from '../app/renderer/js/stream/capture.js'; function fixture({ camera = false, audio = false, settings = {}, deviceId = 'test-camera' } = {}) { const tracks = [{ kind: 'video', readyState: 'live', getSettings: () => ({ deviceId }), stop() { this.readyState = 'ended'; }, addEventListener(name, callback) { if (name === 'ended') this.onEnd = callback; } }]; if (audio) tracks.push({ kind: 'audio', readyState: 'live', getSettings: () => settings, stop() { this.readyState = 'ended'; } }); const stream = { getTracks: () => tracks, getVideoTracks: () => tracks.filter(t => t.kind === 'video'), getAudioTracks: () => tracks.filter(t => t.kind === 'audio') }; const calls = []; const bridge = { async selectCaptureSource(options) { calls.push(['select', options]); return { ok: true, camera, audio, excludeSelf: audio, appAudioSessionId: 'test-session' }; }, async cancelCaptureSelection() { calls.push(['cancel']); return { ok: true }; }, async stopAppAudio(options) { calls.push(['audio-stop', options]); return { ok: true }; }, }; const mediaDevices = { enumerateDevices: async () => [{ kind: 'videoinput', deviceId, label: 'Generated camera' }], getSupportedConstraints: () => ({ restrictOwnAudio: true }), getDisplayMedia: async options => { calls.push(['display', options]); return stream; }, getUserMedia: async options => { calls.push(['camera', options]); return stream; }, }; return { stream, tracks, calls, bridge, mediaDevices }; } test('explicit display selection returns live video and independent app-audio session handle', async () => { const f = fixture(); assert.equal(await acquireCapture({ sourceId: 'window:42:0' }, f), f.stream); assert.equal(getCaptureSessionId(f.stream), 'test-session'); assert.equal(f.calls.find(([kind]) => kind === 'display')[1].audio, false); assert.equal(f.calls.at(-1)[0], 'cancel'); }); test('unconfirmed self-audio exclusion stops every track instead of silently broadcasting feedback', async () => { const f = fixture({ audio: true }); await assert.rejects(acquireCapture({ sourceId: 'window:42:0', audioMode: 'system' }, f), { code: 'audio_exclusion_unverified' }); assert.ok(f.tracks.every(track => track.readyState === 'ended')); }); test('confirmed constraint is passed explicitly; this is not native isolation proof', async () => { const f = fixture({ audio: true, settings: { restrictOwnAudio: true } }); await acquireCapture({ sourceId: 'window:42:0', audioMode: 'system' }, f); assert.equal(f.calls.find(([kind]) => kind === 'display')[1].audio.restrictOwnAudio, true); }); test('missing requested audio never falls back to microphone or video-only', async () => { const f = fixture(); f.bridge.selectCaptureSource = async () => ({ ok: true, audio: true, excludeSelf: false }); await assert.rejects(acquireCapture({ sourceId: 'window:42:0', audioMode: 'system' }, f), { code: 'system_audio_unavailable' }); assert.ok(f.tracks.every(track => track.readyState === 'ended')); assert.equal(f.calls.some(([kind]) => kind === 'camera'), false); }); test('camera exact device is observed and returned device identity is checked', async () => { const f = fixture({ camera: true }); await acquireCapture({ sourceId: 'camera:test-camera' }, f); assert.deepEqual(f.calls.find(([kind]) => kind === 'camera')[1], { audio: false, video: { deviceId: { exact: 'test-camera' } } }); const gone = fixture({ camera: true }); await assert.rejects(acquireCapture({ sourceId: 'camera:missing-device' }, gone), { code: 'capture_source_expired' }); assert.equal(gone.calls.some(([kind]) => kind === 'camera' || kind === 'select'), false); }); test('first-use default camera exists only for a real permission-hidden device and is explicit', async () => { const f = fixture({ camera: true, deviceId: '' }); const cameras = await listCaptureCameras(f); assert.equal(cameras[0].id, 'camera:default-device'); await acquireCapture({ sourceId: cameras[0].id }, f); assert.deepEqual(f.calls.find(([kind]) => kind === 'camera')[1], { audio: false, video: true }); }); test('camera device mismatch stops stream without retrying another device', async () => { const f = fixture({ camera: true }); f.tracks[0].getSettings = () => ({ deviceId: 'wrong-camera' }); await assert.rejects(acquireCapture({ sourceId: 'camera:test-camera' }, f), { code: 'camera_selection_mismatch' }); assert.equal(f.tracks[0].readyState, 'ended'); assert.equal(f.calls.filter(([kind]) => kind === 'camera').length, 1); }); test('OS cancellation and device failures clear grant and do not retry capture', async () => { for (const name of ['NotAllowedError', 'AbortError', 'NotReadableError']) { const f = fixture(); let calls = 0; f.mediaDevices.getDisplayMedia = async () => { calls++; throw Object.assign(new Error('fixture failure'), { name }); }; await assert.rejects(acquireCapture({ sourceId: 'window:42:0' }, f)); assert.equal(calls, 1); assert.equal(f.calls.at(-1)[0], 'cancel'); } }); test('failed permission cleanup stops otherwise successful stream', async () => { const f = fixture(); f.bridge.cancelCaptureSelection = async () => { throw new Error('renderer bridge closed'); }; await assert.rejects(acquireCapture({ sourceId: 'window:42:0' }, f), { code: 'capture_cleanup_failed' }); assert.ok(f.tracks.every(track => track.readyState === 'ended')); }); test('late native chooser result after cancellation stops all tracks and concurrent requests cannot steal grants', async () => { const f = fixture(); let finish; let started; const ready = new Promise(resolve => { started = resolve; }); f.mediaDevices.getDisplayMedia = () => { started(); return new Promise(resolve => { finish = resolve; }); }; const pending = acquireCapture({ sourceId: 'window:42:0' }, f); await ready; await assert.rejects(acquireCapture({ sourceId: 'window:43:0' }, f), { code: 'capture_busy' }); await cancelCapture(f); finish(f.stream); await assert.rejects(pending, { code: 'capture_cancelled' }); assert.ok(f.tracks.every(track => track.readyState === 'ended')); }); test('native picker rejection revokes its already eligible application-audio token', async () => { const f = fixture(); f.mediaDevices.getDisplayMedia = async () => { throw Object.assign(new Error('native picker cancelled'), { name: 'AbortError' }); }; await assert.rejects(acquireCapture({ sourceId: 'native-picker' }, f), { code: 'capture_denied_or_cancelled' }); assert.deepEqual(f.calls.filter(([kind]) => kind === 'audio-stop'), [['audio-stop', { sessionId: 'test-session' }]]); }); test('dialog cancellation revokes a preview audio grant before any mixer starts', async () => { const f = fixture(); await acquireCapture({ sourceId: 'window:42:0' }, f); await cancelCapture(f); assert.equal(getCaptureSessionId(f.stream), null); assert.deepEqual(f.calls.filter(([kind]) => kind === 'audio-stop'), [['audio-stop', { sessionId: 'test-session' }]]); }); test('source ended revokes only its token and repeated cancellation is idempotent', async () => { const f = fixture(); await acquireCapture({ sourceId: 'window:42:0' }, f); f.tracks[0].onEnd(); await cancelCapture(f); assert.equal(getCaptureSessionId(f.stream), null); assert.equal(f.calls.filter(([kind]) => kind === 'audio-stop').length, 1); });