style: format test_voice.py with black and sort imports

This commit is contained in:
Paul Gauthier (aider) 2024-11-30 15:41:19 -08:00
parent caeceb58a5
commit 97daff4a10

View file

@ -1,101 +1,113 @@
import pytest import os
from unittest.mock import Mock, patch
import numpy as np
import queue import queue
import tempfile import tempfile
import os from unittest.mock import Mock, patch
import numpy as np
import pytest
from aider.voice import SoundDeviceError, Voice
from aider.voice import Voice, SoundDeviceError
@pytest.fixture @pytest.fixture
def mock_sounddevice(): def mock_sounddevice():
with patch('sounddevice.query_devices') as mock_query: with patch("sounddevice.query_devices") as mock_query:
mock_query.return_value = [ mock_query.return_value = [
{'name': 'test_device', 'max_input_channels': 2}, {"name": "test_device", "max_input_channels": 2},
{'name': 'another_device', 'max_input_channels': 1} {"name": "another_device", "max_input_channels": 1},
] ]
yield mock_query yield mock_query
@pytest.fixture @pytest.fixture
def mock_soundfile(): def mock_soundfile():
with patch('soundfile.SoundFile') as mock_sf: with patch("soundfile.SoundFile") as mock_sf:
yield mock_sf yield mock_sf
def test_voice_init_default_device(mock_sounddevice): def test_voice_init_default_device(mock_sounddevice):
voice = Voice() voice = Voice()
assert voice.device_id is None assert voice.device_id is None
assert voice.audio_format == 'wav' assert voice.audio_format == "wav"
def test_voice_init_specific_device(mock_sounddevice): def test_voice_init_specific_device(mock_sounddevice):
voice = Voice(device_name='test_device') voice = Voice(device_name="test_device")
assert voice.device_id == 0 assert voice.device_id == 0
def test_voice_init_invalid_device(mock_sounddevice): def test_voice_init_invalid_device(mock_sounddevice):
with pytest.raises(ValueError) as exc: with pytest.raises(ValueError) as exc:
Voice(device_name='nonexistent_device') Voice(device_name="nonexistent_device")
assert 'Device' in str(exc.value) assert "Device" in str(exc.value)
assert 'not found' in str(exc.value) assert "not found" in str(exc.value)
def test_voice_init_invalid_format(): def test_voice_init_invalid_format():
with pytest.raises(ValueError) as exc: with pytest.raises(ValueError) as exc:
Voice(audio_format='invalid') Voice(audio_format="invalid")
assert 'Unsupported audio format' in str(exc.value) assert "Unsupported audio format" in str(exc.value)
def test_callback_processing(): def test_callback_processing():
voice = Voice() voice = Voice()
voice.q = queue.Queue() voice.q = queue.Queue()
# Test with silence (low amplitude) # Test with silence (low amplitude)
test_data = np.zeros((1000, 1)) test_data = np.zeros((1000, 1))
voice.callback(test_data, None, None, None) voice.callback(test_data, None, None, None)
assert voice.pct < 0.1 assert voice.pct < 0.1
# Test with loud signal (high amplitude) # Test with loud signal (high amplitude)
test_data = np.ones((1000, 1)) test_data = np.ones((1000, 1))
voice.callback(test_data, None, None, None) voice.callback(test_data, None, None, None)
assert voice.pct > 0.9 assert voice.pct > 0.9
# Verify data is queued # Verify data is queued
assert not voice.q.empty() assert not voice.q.empty()
@patch('aider.voice.litellm')
@patch("aider.voice.litellm")
def test_record_and_transcribe(mock_litellm, mock_soundfile): def test_record_and_transcribe(mock_litellm, mock_soundfile):
voice = Voice() voice = Voice()
# Mock the recording process # Mock the recording process
with patch('prompt_toolkit.shortcuts.prompt'): with patch("prompt_toolkit.shortcuts.prompt"):
with patch('sounddevice.InputStream'): with patch("sounddevice.InputStream"):
# Mock the transcription response # Mock the transcription response
mock_litellm.transcription.return_value = Mock(text="Hello, world!") mock_litellm.transcription.return_value = Mock(text="Hello, world!")
result = voice.record_and_transcribe() result = voice.record_and_transcribe()
assert result == "Hello, world!" assert result == "Hello, world!"
mock_litellm.transcription.assert_called_once() mock_litellm.transcription.assert_called_once()
def test_get_prompt(): def test_get_prompt():
voice = Voice() voice = Voice()
voice.start_time = voice.start_time = os.times().elapsed voice.start_time = voice.start_time = os.times().elapsed
voice.pct = 0.5 # 50% volume level voice.pct = 0.5 # 50% volume level
prompt = voice.get_prompt() prompt = voice.get_prompt()
assert "Recording" in prompt assert "Recording" in prompt
assert "sec" in prompt assert "sec" in prompt
assert "" in prompt # Should contain some filled blocks assert "" in prompt # Should contain some filled blocks
assert "" in prompt # Should contain some empty blocks assert "" in prompt # Should contain some empty blocks
@patch('sounddevice.InputStream')
@patch("sounddevice.InputStream")
def test_record_and_transcribe_keyboard_interrupt(mock_stream): def test_record_and_transcribe_keyboard_interrupt(mock_stream):
voice = Voice() voice = Voice()
mock_stream.side_effect = KeyboardInterrupt() mock_stream.side_effect = KeyboardInterrupt()
result = voice.record_and_transcribe() result = voice.record_and_transcribe()
assert result is None assert result is None
@patch('sounddevice.InputStream')
@patch("sounddevice.InputStream")
def test_record_and_transcribe_device_error(mock_stream): def test_record_and_transcribe_device_error(mock_stream):
voice = Voice() voice = Voice()
mock_stream.side_effect = SoundDeviceError("Test error") mock_stream.side_effect = SoundDeviceError("Test error")
result = voice.record_and_transcribe() result = voice.record_and_transcribe()
assert result is None assert result is None