test: increased cb test coverage
This commit is contained in:
committed by
Luijkx,S.O.H. (Storm)
parent
de2e56ffce
commit
7f7c658901
@@ -120,3 +120,83 @@ def test_mlx_recognizer():
|
||||
mlx_mock.transcribe.return_value = {"text": "Hi"}
|
||||
res = rec.recognize_speech(np.zeros(10))
|
||||
assert res == "Hi"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_transcription_loop_continues_after_error(mock_zmq_context):
|
||||
mock_sub = MagicMock()
|
||||
mock_sub.recv = AsyncMock()
|
||||
mock_zmq_context.instance.return_value.socket.return_value = mock_sub
|
||||
|
||||
fake_audio = np.zeros(16000, dtype=np.float32).tobytes()
|
||||
|
||||
mock_sub.recv.side_effect = [
|
||||
fake_audio, # first iteration → recognizer fails
|
||||
asyncio.CancelledError(), # second iteration → stop loop
|
||||
]
|
||||
|
||||
with patch.object(SpeechRecognizer, "best_type") as mock_best:
|
||||
mock_recognizer = MagicMock()
|
||||
mock_recognizer.recognize_speech.side_effect = RuntimeError("fail")
|
||||
mock_best.return_value = mock_recognizer
|
||||
|
||||
agent = TranscriptionAgent("tcp://in")
|
||||
agent._running = True # ← REQUIRED to enter the loop
|
||||
agent.send = AsyncMock() # should never be called
|
||||
agent.add_behavior = AsyncMock() # match other tests
|
||||
|
||||
await agent.setup()
|
||||
|
||||
try:
|
||||
await agent._transcribing_loop()
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
|
||||
# recognizer failed, so we should never send anything
|
||||
agent.send.assert_not_called()
|
||||
|
||||
# recv must have been called twice (audio then CancelledError)
|
||||
assert mock_sub.recv.call_count == 2
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_transcription_continue_branch_when_empty(mock_zmq_context):
|
||||
mock_sub = MagicMock()
|
||||
mock_sub.recv = AsyncMock()
|
||||
mock_zmq_context.instance.return_value.socket.return_value = mock_sub
|
||||
|
||||
# First recv → audio chunk
|
||||
# Second recv → Cancel loop → stop iteration
|
||||
fake_audio = np.zeros(16000, dtype=np.float32).tobytes()
|
||||
mock_sub.recv.side_effect = [fake_audio, asyncio.CancelledError()]
|
||||
|
||||
with patch.object(SpeechRecognizer, "best_type") as mock_best:
|
||||
mock_recognizer = MagicMock()
|
||||
mock_recognizer.recognize_speech.return_value = "" # <— triggers the continue branch
|
||||
mock_best.return_value = mock_recognizer
|
||||
|
||||
agent = TranscriptionAgent("tcp://in")
|
||||
|
||||
# Make loop runnable
|
||||
agent._running = True
|
||||
agent.send = AsyncMock()
|
||||
agent.add_behavior = AsyncMock()
|
||||
|
||||
await agent.setup()
|
||||
|
||||
# Execute loop manually
|
||||
try:
|
||||
await agent._transcribing_loop()
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
|
||||
# → Because of "continue", NO sending should occur
|
||||
agent.send.assert_not_called()
|
||||
|
||||
# → Continue was hit, so we must have read exactly 2 times:
|
||||
# - first audio
|
||||
# - second CancelledError
|
||||
assert mock_sub.recv.call_count == 2
|
||||
|
||||
# → recognizer was called once (first iteration)
|
||||
assert mock_recognizer.recognize_speech.call_count == 1
|
||||
|
||||
Reference in New Issue
Block a user