import { describe, expect, test } from "bun:test"; import { DEFAULT_AUDIO_INPUT_DEVICE_ID, buildVoiceOverAudioConstraints, normalizeSelectedDeviceId, type VoiceOverInputDevice, } from "@/voiceover/devices"; const devices: VoiceOverInputDevice[] = [ { deviceId: DEFAULT_AUDIO_INPUT_DEVICE_ID, label: "System default microphone", isDefault: true, }, { deviceId: "blue-yeti-snowball", label: "Blue Snowball", isDefault: false, }, ]; describe("buildVoiceOverAudioConstraints", () => { test("uses default processing constraints without an exact device", () => { const constraints = buildVoiceOverAudioConstraints({ deviceId: null }); expect(constraints.audio).toEqual({ echoCancellation: true, noiseSuppression: true, autoGainControl: true, }); }); test("adds an exact device constraint for a selected microphone", () => { const constraints = buildVoiceOverAudioConstraints({ deviceId: "blue-yeti-snowball", }); expect(constraints.audio).toEqual({ deviceId: { exact: "blue-yeti-snowball" }, echoCancellation: true, noiseSuppression: true, autoGainControl: true, }); }); }); describe("normalizeSelectedDeviceId", () => { test("keeps the selected device when it is still present", () => { expect( normalizeSelectedDeviceId({ deviceId: "blue-yeti-snowball", devices, }), ).toBe("blue-yeti-snowball"); }); test("clears a selected device when it is no longer present", () => { expect( normalizeSelectedDeviceId({ deviceId: "missing", devices, }), ).toBeNull(); }); test("keeps the browser default device id", () => { expect( normalizeSelectedDeviceId({ deviceId: DEFAULT_AUDIO_INPUT_DEVICE_ID, devices: [], }), ).toBe(DEFAULT_AUDIO_INPUT_DEVICE_ID); }); });