diff --git a/src/voice_assistant/text/test_snowboydecoder.py b/src/voice_assistant/text/test_snowboydecoder.py new file mode 100644 index 0000000..445a3f9 --- /dev/null +++ b/src/voice_assistant/text/test_snowboydecoder.py @@ -0,0 +1,56 @@ +import unittest +from unittest.mock import Mock, patch +from snowboydecoder import HotwordDetector + +# 假设 RESOURCE_FILE 是在 snowboydecoder.py 中定义的 +RESOURCE_FILE = 'path/to/resource.res' + +class TestHotwordDetector(unittest.TestCase): + + @patch('snowboydecoder.snowboydetect.SnowboyDetect') + @patch('snowboydecoder.pyaudio.PyAudio') + def test_init(self, mock_pyaudio, mock_snowboydetect): + # Mock SnowboyDetect + mock_detector = mock_snowboydetect.return_value + mock_detector.NumChannels.return_value = 1 + mock_detector.SampleRate.return_value = 16000 + mock_detector.BitsPerSample.return_value = 16 + mock_detector.NumHotwords.return_value = 1 + + # Mock PyAudio + mock_audio = mock_pyaudio.return_value + + # Mock audio stream + mock_stream = Mock() + mock_audio.open.return_value = mock_stream + + # Initialize HotwordDetector + decoder_model = 'model_id' + sensitivity = 0.5 + audio_gain = 1.0 + detector = HotwordDetector(decoder_model, RESOURCE_FILE, sensitivity, audio_gain) + + # Verify interactions + mock_snowboydetect.assert_called_once_with( + resource_filename=RESOURCE_FILE.encode(), + model_str='model_id'.encode() + ) + mock_detector.SetAudioGain.assert_called_once_with(audio_gain) + mock_detector.SetSensitivity.assert_called_once_with('0.5'.encode()) + + # Verify PyAudio and Stream configurations + mock_pyaudio.assert_called_once() + mock_audio.open.assert_called_once_with( + input=True, output=False, + format=mock_audio.get_format_from_width(2).return_value, + channels=1, + rate=16000, + frames_per_buffer=2048, + stream_callback=detector.audio_callback + ) + + # Verify RingBuffer size + self.assertEqual(len(detector.ring_buffer), 0) + +if __name__ == '__main__': + unittest.main() \ No newline at end of file