Fail fast when configured recording input cannot be resolved

This commit is contained in:
Thales Maciel 2026-02-26 16:39:36 -03:00
parent 386ba4af92
commit aa77dbc395
2 changed files with 107 additions and 2 deletions

View file

@ -3,12 +3,14 @@ import unittest
from pathlib import Path
import numpy as np
from unittest.mock import patch
ROOT = Path(__file__).resolve().parents[1]
SRC = ROOT / "src"
if str(SRC) not in sys.path:
sys.path.insert(0, str(SRC))
import recorder
from recorder import RecordResult, stop_recording
@ -30,6 +32,40 @@ class _Stream:
raise self.close_exc
class _InputStream:
def __init__(self, **kwargs):
self.kwargs = kwargs
self.started = False
def start(self) -> None:
self.started = True
def stop(self) -> None:
return
def close(self) -> None:
return
class _FakeSoundDevice:
def __init__(self):
self.default = type("Default", (), {"device": (0, None)})()
self.created_streams = []
self._devices = [
{"name": "Built-in Output", "max_input_channels": 0},
{"name": "Laptop Mic", "max_input_channels": 1},
{"name": "USB Interface", "max_input_channels": 2},
]
def query_devices(self):
return self._devices
def InputStream(self, **kwargs):
stream = _InputStream(**kwargs)
self.created_streams.append(stream)
return stream
class RecorderTests(unittest.TestCase):
def test_stop_recording_closes_stream_when_stop_raises(self):
stream = _Stream(stop_exc=RuntimeError("stop boom"))
@ -75,6 +111,40 @@ class RecorderTests(unittest.TestCase):
np.testing.assert_allclose(audio, np.array([0.2, 0.4], dtype=np.float32))
def test_start_recording_accepts_explicit_device_name(self):
sd = _FakeSoundDevice()
with patch("recorder._sounddevice", return_value=sd):
stream, record = recorder.start_recording("Laptop")
self.assertTrue(stream.started)
self.assertEqual(stream.kwargs["device"], 1)
self.assertEqual(record.channels, 1)
def test_start_recording_accepts_explicit_device_index(self):
sd = _FakeSoundDevice()
with patch("recorder._sounddevice", return_value=sd):
stream, _record = recorder.start_recording(2)
self.assertTrue(stream.started)
self.assertEqual(stream.kwargs["device"], 2)
def test_start_recording_invalid_explicit_name_raises(self):
sd = _FakeSoundDevice()
with patch("recorder._sounddevice", return_value=sd):
with self.assertRaisesRegex(
ValueError,
"did not match any input device; available inputs: 1:Laptop Mic, 2:USB Interface",
):
recorder.start_recording("NotADevice")
def test_start_recording_empty_input_uses_default(self):
sd = _FakeSoundDevice()
with patch("recorder._sounddevice", return_value=sd):
stream, _record = recorder.start_recording("")
self.assertTrue(stream.started)
self.assertIsNone(stream.kwargs["device"])
if __name__ == "__main__":
unittest.main()