mirror of
https://github.com/livekit/agents.git
synced 2026-12-06 04:38:33 +00:00
194 lines
6.0 KiB
Python
194 lines
6.0 KiB
Python
from __future__ import annotations
|
|
|
|
from collections.abc import Coroutine
|
|
from typing import Any
|
|
from unittest.mock import AsyncMock, MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
from livekit.agents import DEFAULT_API_CONNECT_OPTIONS, APIStatusError, http, stt
|
|
from livekit.plugins.nabrah.stt import STT, SpeechStream, _Message
|
|
|
|
pytestmark = pytest.mark.unit
|
|
|
|
|
|
@pytest.fixture
|
|
async def stream() -> SpeechStream:
|
|
def _discard_task(
|
|
coroutine: Coroutine[Any, Any, Any], *args: object, **kwargs: object
|
|
) -> MagicMock:
|
|
coroutine.close()
|
|
return MagicMock()
|
|
|
|
provider = STT(api_key="test-key", http_session=MagicMock(spec=http.HttpSession))
|
|
with patch("livekit.agents._generated.stt.asyncio.create_task", side_effect=_discard_task):
|
|
return provider.stream(conn_options=DEFAULT_API_CONNECT_OPTIONS)
|
|
|
|
|
|
async def test_closed_utterance_with_repeated_prefix_starts_a_new_utterance(
|
|
stream: SpeechStream,
|
|
) -> None:
|
|
stream._process_message(
|
|
_Message.model_validate(
|
|
{"type": "transcript", "text": "مرحبا", "is_final": True, "audio_processed": 1.0}
|
|
)
|
|
)
|
|
stream._process_message(
|
|
_Message.model_validate(
|
|
{
|
|
"type": "transcript",
|
|
"text": "مرحبا بكم",
|
|
"is_final": False,
|
|
"audio_processed": 2.0,
|
|
}
|
|
)
|
|
)
|
|
|
|
assert stream._current_text() == "مرحبا مرحبا بكم"
|
|
|
|
|
|
async def test_open_utterance_correction_replaces_previous_hypothesis(stream: SpeechStream) -> None:
|
|
stream._process_message(
|
|
_Message.model_validate({"type": "transcript", "text": "مرحبا بكم", "is_final": False})
|
|
)
|
|
stream._process_message(
|
|
_Message.model_validate({"type": "transcript", "text": "مرحبا بكن", "is_final": False})
|
|
)
|
|
|
|
assert stream._current_text() == "مرحبا بكن"
|
|
|
|
|
|
async def test_flushed_prefix_preserves_provider_punctuation(stream: SpeechStream) -> None:
|
|
stream._opts.end_of_turn_confirm_delay_seconds = None
|
|
|
|
stream._process_message(
|
|
_Message.model_validate(
|
|
{
|
|
"type": "transcript",
|
|
"text": "مرحبا <eot>",
|
|
"is_final": False,
|
|
"audio_processed": 1.0,
|
|
}
|
|
)
|
|
)
|
|
stream._process_message(
|
|
_Message.model_validate(
|
|
{
|
|
"type": "transcript",
|
|
"text": "مرحبا. كيف <eot>",
|
|
"is_final": False,
|
|
"audio_processed": 2.0,
|
|
}
|
|
)
|
|
)
|
|
|
|
assert stream._utt_flushed_clean == "مرحبا. كيف"
|
|
|
|
stream._process_message(
|
|
_Message.model_validate(
|
|
{
|
|
"type": "transcript",
|
|
"text": "مرحبا. كيف حالك",
|
|
"is_final": False,
|
|
"audio_processed": 3.0,
|
|
}
|
|
)
|
|
)
|
|
|
|
assert stream._current_text() == "حالك"
|
|
|
|
|
|
async def test_filtered_word_does_not_move_raw_word_cursor_backwards(stream: SpeechStream) -> None:
|
|
stream._opts.end_of_turn_confirm_delay_seconds = None
|
|
stream._process_message(
|
|
_Message.model_validate(
|
|
{
|
|
"type": "transcript",
|
|
"text": "مرحبا <eot>",
|
|
"words": [{"word": "مرحبا"}, {"word": "<eot>"}],
|
|
}
|
|
)
|
|
)
|
|
|
|
assert stream._utt_flushed_words == 2
|
|
|
|
stream._process_message(
|
|
_Message.model_validate(
|
|
{
|
|
"type": "transcript",
|
|
"text": "مرحبا <eot> كيف",
|
|
"words": [{"word": "مرحبا"}, {"word": "<eot>"}, {"word": "كيف"}],
|
|
}
|
|
)
|
|
)
|
|
|
|
assert [word.text for word in stream._utt_words] == ["كيف"]
|
|
|
|
|
|
async def test_provider_error_does_not_expose_provider_message(stream: SpeechStream) -> None:
|
|
transcript = "private customer transcript"
|
|
|
|
with pytest.raises(APIStatusError) as exc_info:
|
|
stream._process_message(_Message.model_validate({"type": "error", "message": transcript}))
|
|
|
|
assert transcript not in str(exc_info.value)
|
|
|
|
|
|
async def test_malformed_message_does_not_expose_provider_content(
|
|
stream: SpeechStream, caplog: pytest.LogCaptureFixture
|
|
) -> None:
|
|
transcript = "private customer transcript"
|
|
ws = MagicMock(spec=http.WebSocket)
|
|
ws.receive = AsyncMock(
|
|
side_effect=[
|
|
http.WebSocketMessage(http.WebSocketMessageType.TEXT, '{"type": "transcript"}'),
|
|
http.WebSocketMessage(http.WebSocketMessageType.CLOSE, ""),
|
|
]
|
|
)
|
|
stream._input_done = True
|
|
|
|
with (
|
|
caplog.at_level("WARNING", logger="livekit.plugins.nabrah"),
|
|
patch.object(stream, "_process_message", side_effect=ValueError(transcript)),
|
|
):
|
|
await stream._recv_task(ws)
|
|
|
|
assert "nabrah STT returned malformed data" in caplog.text
|
|
assert transcript not in caplog.text
|
|
|
|
|
|
async def test_stream_failure_is_not_retried_after_audio_is_consumed(stream: SpeechStream) -> None:
|
|
assert stream._stream_failure("failed").retryable is True
|
|
|
|
stream._audio_position = 0.1
|
|
|
|
assert stream._stream_failure("failed").retryable is False
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("field", "value"),
|
|
[("is_final", "false"), ("audio_processed", float("nan"))],
|
|
)
|
|
async def test_invalid_transcript_fields_are_rejected(
|
|
stream: SpeechStream, field: str, value: object
|
|
) -> None:
|
|
message: dict[str, object] = {"type": "transcript", "text": "مرحبا", field: value}
|
|
|
|
with pytest.raises(ValueError):
|
|
stream._process_message(_Message.model_validate(message))
|
|
|
|
|
|
async def test_flush_reports_usage_without_transcript(stream: SpeechStream) -> None:
|
|
emitted: list[stt.SpeechEvent] = []
|
|
stream._audio_position = 1.25
|
|
|
|
with patch.object(stream, "_emit", side_effect=emitted.append):
|
|
stream._flush_eos()
|
|
|
|
usage_events = [
|
|
event for event in emitted if event.type == stt.SpeechEventType.RECOGNITION_USAGE
|
|
]
|
|
assert len(usage_events) == 1
|
|
assert usage_events[0].recognition_usage is not None
|
|
assert usage_events[0].recognition_usage.audio_duration == pytest.approx(1.25)
|