mirror of
https://github.com/livekit/agents.git
synced 2026-12-06 04:58:42 +00:00
128 lines
4.0 KiB
Python
128 lines
4.0 KiB
Python
"""Unit tests for base STT `RecognizeStream` fields (start_time, etc.)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import time
|
|
|
|
import pytest
|
|
|
|
from livekit import rtc
|
|
from livekit.agents import APIConnectionError
|
|
from livekit.agents.stt import (
|
|
STT,
|
|
RecognizeStream,
|
|
SpeechData,
|
|
SpeechEvent,
|
|
SpeechEventType,
|
|
StreamAdapter,
|
|
STTCapabilities,
|
|
)
|
|
from livekit.agents.types import DEFAULT_API_CONNECT_OPTIONS
|
|
|
|
from .fake_stt import FakeSTT, FakeUserSpeech
|
|
from .fake_vad import FakeVAD
|
|
|
|
pytestmark = pytest.mark.unit
|
|
|
|
|
|
class _DummyStream(RecognizeStream):
|
|
"""Minimal RecognizeStream for unit tests — does not hit the network."""
|
|
|
|
def __init__(
|
|
self,
|
|
*,
|
|
stt: STT,
|
|
fail_first_run: bool = False,
|
|
) -> None:
|
|
super().__init__(stt=stt, conn_options=DEFAULT_API_CONNECT_OPTIONS)
|
|
self._fail_first_run = fail_first_run
|
|
self._run_count = 0
|
|
|
|
async def _run(self) -> None:
|
|
self._run_count += 1
|
|
if self._fail_first_run and self._run_count == 1:
|
|
raise APIConnectionError("fake failure to trigger retry")
|
|
async for _ in self.input:
|
|
pass
|
|
self.send(
|
|
SpeechEvent(
|
|
type=SpeechEventType.FINAL_TRANSCRIPT,
|
|
alternatives=[SpeechData(language="", text="hello")],
|
|
)
|
|
)
|
|
|
|
|
|
class _DummySTT(STT):
|
|
def __init__(self) -> None:
|
|
super().__init__(capabilities=STTCapabilities(streaming=True, interim_results=False))
|
|
|
|
async def _recognize_impl(
|
|
self, buffer: list[rtc.AudioFrame] | rtc.AudioFrame, *, language, conn_options
|
|
) -> SpeechEvent:
|
|
raise NotImplementedError
|
|
|
|
def stream(self, *, language=None, conn_options=DEFAULT_API_CONNECT_OPTIONS) -> _DummyStream:
|
|
return _DummyStream(stt=self)
|
|
|
|
|
|
async def test_start_time_reseeded_on_retry() -> None:
|
|
"""When _main_task retries after an APIError, start_time is re-seeded so plugin
|
|
overrides from the previous connection don't leak into the new one."""
|
|
stt = _DummySTT()
|
|
stream = _DummyStream(stt=stt, fail_first_run=True)
|
|
|
|
# Simulate a plugin overriding start_time during the first (failing) _run()
|
|
# by assigning a sentinel value before the task picks up.
|
|
sentinel = 1.0
|
|
stream.start_time = sentinel
|
|
stream.end_input()
|
|
|
|
# Let the main task run: it should retry past the first-run APIError, and
|
|
# on each attempt re-seed start_time to a fresh time.time() value before
|
|
# _run() is called.
|
|
await asyncio.wait_for(stream._task, timeout=5.0)
|
|
|
|
# After the retry, start_time must have been re-seeded (not equal to sentinel).
|
|
assert stream.start_time != sentinel
|
|
# And it should be a recent wall-clock value.
|
|
assert time.time() - stream.start_time < 5.0
|
|
|
|
await stream.aclose()
|
|
|
|
|
|
async def test_stream_adapter_keeps_vad_speech_end_on_delayed_final() -> None:
|
|
recognize_delay = 0.5
|
|
frame = rtc.AudioFrame.create(16_000, 1, int(0.1 * 16_000))
|
|
speech = FakeUserSpeech(
|
|
start_time=0.0,
|
|
end_time=0.1,
|
|
transcript="hello",
|
|
stt_delay=0.0,
|
|
)
|
|
batch_stt = FakeSTT(fake_transcript="hello", fake_timeout=recognize_delay)
|
|
batch_stt._capabilities.streaming = False
|
|
adapter = StreamAdapter(
|
|
stt=batch_stt,
|
|
vad=FakeVAD(
|
|
fake_user_speeches=[speech],
|
|
min_speech_duration=0.01,
|
|
min_silence_duration=0.3,
|
|
),
|
|
)
|
|
|
|
try:
|
|
async with adapter.stream() as stream:
|
|
stream.push_frame(frame)
|
|
stream.end_input()
|
|
events = [event async for event in stream]
|
|
finally:
|
|
await adapter.aclose()
|
|
|
|
end_event = next(event for event in events if event.type == SpeechEventType.END_OF_SPEECH)
|
|
final_event = next(event for event in events if event.type == SpeechEventType.FINAL_TRANSCRIPT)
|
|
|
|
assert end_event.speech_end_time is not None
|
|
assert final_event.speech_end_time == end_event.speech_end_time
|
|
assert final_event.created_at - end_event.created_at >= recognize_delay
|