949eb679e1
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
66 lines
2.3 KiB
Python
66 lines
2.3 KiB
Python
import numpy as np
|
|
import pytest
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
from whisper_local.recorder import Recorder
|
|
|
|
|
|
@pytest.fixture
|
|
def recorder():
|
|
return Recorder(sample_rate=16000, channels=1, min_duration=0.5)
|
|
|
|
|
|
class TestRecorder:
|
|
def test_init(self, recorder):
|
|
assert recorder.sample_rate == 16000
|
|
assert recorder.channels == 1
|
|
assert not recorder.is_recording
|
|
|
|
def test_start_recording(self, recorder):
|
|
with patch("sounddevice.InputStream") as mock_stream_cls:
|
|
mock_stream = MagicMock()
|
|
mock_stream_cls.return_value = mock_stream
|
|
recorder.start()
|
|
assert recorder.is_recording
|
|
assert len(recorder._chunks) == 0
|
|
recorder.stop() # Cleanup
|
|
|
|
def test_stop_without_start_returns_none(self, recorder):
|
|
result = recorder.stop()
|
|
assert result is None
|
|
|
|
def test_add_chunk_and_stop(self, recorder):
|
|
with patch("sounddevice.InputStream") as mock_stream_cls:
|
|
mock_stream_cls.return_value = MagicMock()
|
|
recorder.start()
|
|
# Simuliere 1 Sekunde Audio (16000 Samples)
|
|
chunk = np.zeros((16000, 1), dtype=np.float32)
|
|
recorder._chunks.append(chunk)
|
|
result = recorder.stop()
|
|
assert result is not None
|
|
assert result.shape == (16000,)
|
|
assert result.dtype == np.float32
|
|
|
|
def test_short_audio_returns_none(self, recorder):
|
|
with patch("sounddevice.InputStream") as mock_stream_cls:
|
|
mock_stream_cls.return_value = MagicMock()
|
|
recorder.start()
|
|
# Nur 0.1s Audio — unter min_duration
|
|
chunk = np.zeros((1600, 1), dtype=np.float32)
|
|
recorder._chunks.append(chunk)
|
|
result = recorder.stop()
|
|
assert result is None
|
|
|
|
def test_multiple_chunks_concatenated(self, recorder):
|
|
with patch("sounddevice.InputStream") as mock_stream_cls:
|
|
mock_stream_cls.return_value = MagicMock()
|
|
recorder.start()
|
|
chunk1 = np.ones((8000, 1), dtype=np.float32)
|
|
chunk2 = np.ones((8000, 1), dtype=np.float32) * 0.5
|
|
recorder._chunks.extend([chunk1, chunk2])
|
|
result = recorder.stop()
|
|
assert result is not None
|
|
assert result.shape == (16000,)
|
|
assert result[0] == 1.0
|
|
assert result[8000] == 0.5
|