@@ -28,7 +28,7 @@ def __init__(self, config):
2828 self .config = config
2929 self .check_connectivity = AsyncMock (return_value = True )
3030
31- def generate_speech (self , text : str , stream : bool = False ):
31+ async def generate_speech (self , text : str , stream : bool = False ):
3232 """Mock implementation that returns appropriate data based on stream parameter"""
3333 if stream :
3434 # Return an async generator for streaming
@@ -49,15 +49,17 @@ async def mock_audio_generator():
4949
5050def mock_voice_dependencies (func ):
5151 """Decorator to apply all necessary mocks for voice service tests"""
52- @patch ('nexent.core.models.tts_model .TTSModel' , MockTTSModel )
53- @patch ('nexent.core.models.stt_model .STTModel' , MockSTTModel )
52+ @patch ('services.voice_service .TTSModel' , MockTTSModel )
53+ @patch ('services.voice_service .STTModel' , MockSTTModel )
5454 @patch ('consts.const.TEST_VOICE_PATH' , '/test/path' )
5555 @patch ('consts.const.SPEED_RATIO' , 1.0 )
5656 @patch ('consts.const.VOICE_TYPE' , 'test_voice_type' )
5757 @patch ('consts.const.CLUSTER' , 'test_cluster' )
5858 @patch ('consts.const.TOKEN' , 'test_token' )
5959 @patch ('consts.const.APPID' , 'test_appid' )
6060 def wrapper (* args , ** kwargs ):
61+ # Reset the global voice service instance to ensure test isolation
62+ services .voice_service ._voice_service_instance = None
6163 return func (* args , ** kwargs )
6264 return wrapper
6365
@@ -177,6 +179,19 @@ def test_stream_tts_to_websocket_success(self):
177179 """Test successful TTS streaming to WebSocket"""
178180 service = VoiceService ()
179181
182+ # Mock the TTS model's generate_speech method directly to avoid real WebSocket connections
183+ async def mock_generate_speech (text : str , stream : bool = False ):
184+ if stream :
185+ async def mock_audio_generator ():
186+ yield b"mock_audio_chunk_1"
187+ yield b"mock_audio_chunk_2"
188+ yield b"mock_audio_chunk_3"
189+ return mock_audio_generator ()
190+ else :
191+ return b"mock_complete_audio_data"
192+
193+ service .tts_model .generate_speech = mock_generate_speech
194+
180195 # Mock WebSocket with client_state
181196 mock_websocket = Mock ()
182197 mock_websocket .send_bytes = AsyncMock ()
@@ -191,7 +206,7 @@ def test_stream_tts_to_websocket_success(self):
191206 # Test the method
192207 asyncio .run (service .stream_tts_to_websocket (mock_websocket , "Hello, world!" ))
193208
194- assert mock_websocket .send_bytes .call_count == 6
209+ assert mock_websocket .send_bytes .call_count == 3
195210 mock_websocket .send_json .assert_called_once_with ({"status" : "completed" })
196211
197212 @mock_voice_dependencies
0 commit comments