Skip to content

Commit

Permalink
Fixes to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paulovcmedeiros committed Feb 4, 2024
1 parent 2c2562d commit 22053e7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 22 deletions.
5 changes: 5 additions & 0 deletions pyrobbot/voice_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,9 @@ def handle_listening(
while True:
try:
audio = self.listen()
if audio is None:
raise EOFError

if audio.duration_seconds < minimum_prompt_duration_seconds:
continue

Expand Down Expand Up @@ -310,6 +313,8 @@ def handle_listening(
questions_queue.put(None)
elif question:
questions_queue.put(question)
except EOFError:
questions_queue.put(None)
except Exception as error: # noqa: BLE001
logger.opt(exception=True).debug(error)
logger.error(error)
Expand Down
15 changes: 0 additions & 15 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import io
from unittest.mock import MagicMock

import lorem
import numpy as np
import pydub
import pygame
import pytest
from _pytest.logging import LogCaptureFixture
from loguru import logger
Expand Down Expand Up @@ -233,19 +231,6 @@ def _mock_iter_bytes(*args, **kwargs): # noqa: ARG001
return_value=mock_transcription.text,
)

# orig_func = VoiceChat._wav_buffer_to_sound

# def mock_wav_buffer_to_sound(self: VoiceChat, *args, **kwargs):
# try:
# return orig_func(self, *args, **kwargs)
# except pygame.error:
# return MagicMock()

# mocker.patch(
# "pyrobbot.voice_chat.VoiceChat._wav_buffer_to_sound",
# mock_wav_buffer_to_sound,
# )

mocker.patch("webrtcvad.Vad.is_speech", return_value=False)
mocker.patch("pygame.mixer.init")
mocker.patch("chime.play_wav")
8 changes: 5 additions & 3 deletions tests/smoke/test_commands.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import io
import subprocess

import pytest
from pydub import AudioSegment

from pyrobbot.__main__ import main
from pyrobbot.argparse_wrapper import get_parsed_args
Expand Down Expand Up @@ -40,7 +42,7 @@ def new_run(*args, **kwargs):

@pytest.mark.parametrize("stt", ["google", "openai"])
@pytest.mark.parametrize("tts", ["google", "openai"])
def test_voice_chat(mocker, tts, stt):
def test_voice_chat(mocker, mock_wav_bytes_string, tts, stt):
# We allow even number of calls in order to let the function be tested first and
# then terminate the chat
def _mock_listen(*args, **kwargs): # noqa: ARG001
Expand All @@ -49,8 +51,8 @@ def _mock_listen(*args, **kwargs): # noqa: ARG001
except AttributeError:
_mock_listen.execution_counter = 0
if _mock_listen.execution_counter % 2:
raise KeyboardInterrupt
return "foobar"
return None
return AudioSegment.from_wav(io.BytesIO(mock_wav_bytes_string))

mocker.patch("pyrobbot.voice_chat.VoiceChat.listen", _mock_listen)
main(["voice", "--tts", tts, "--stt", stt])
14 changes: 10 additions & 4 deletions tests/unit/test_text_to_speech.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

import pytest
from pydantic import ValidationError
from pydub import AudioSegment
from sounddevice import PortAudioError

from pyrobbot.chat_configs import VoiceChatConfigs
from pyrobbot.sst_and_tts import SpeechToText, TextToSpeech
from pyrobbot.voice_chat import VoiceChat


Expand All @@ -30,9 +32,13 @@ def test_listen(default_voice_chat):


@pytest.mark.parametrize("stt_engine", ["google", "openai"])
def test_stt(default_voice_chat, stt_engine, mock_wav_bytes_string):
def test_stt(default_voice_chat, stt_engine):
"""Test the speech-to-text method."""
default_voice_chat.stt_engine = stt_engine
rtn = default_voice_chat._wav_buffer_to_text(io.BytesIO(mock_wav_bytes_string))

assert rtn == "patched"
stt = SpeechToText(
speech=AudioSegment.silent(duration=100),
engine=stt_engine,
general_token_usage_db=default_voice_chat.general_token_usage_db,
token_usage_db=default_voice_chat.token_usage_db,
)
assert stt.text == "patched"

0 comments on commit 22053e7

Please sign in to comment.