Files
livekit-agents/python/tests/test_plugin_soniox_tts.py
T

328 lines
11 KiB
Python

"""Unit tests for Soniox TTS chunk forwarding and stream rotation.
The plugin feeds each prepared chunk, space-separated, into one shared Soniox stream, and
rotates to a fresh ``stream_id`` when input goes idle for ``stream_idle_timeout``
or after a transient failure (batch replay).
These tests drive the real ``SynthesizeStream`` against a fake ``_Connection``.
"""
from __future__ import annotations
import asyncio
import json
from dataclasses import dataclass
from typing import Any
from unittest.mock import AsyncMock, MagicMock
import aiohttp
import pytest
from aiohttp import web
from aiohttp.test_utils import TestServer
from livekit.agents import (
DEFAULT_API_CONNECT_OPTIONS,
APIConnectOptions,
APIStatusError,
ProviderAttempts,
http,
tts as tts_module,
)
from livekit.agents.tts import AudioEmitter
from livekit.plugins import soniox
from livekit.plugins.soniox.tts import _Connection
from tests.utils.attempts import observed_attempts
pytestmark = [
pytest.mark.plugin("soniox"),
]
# 10 ms of mono s16le silence at 24 kHz
_SILENCE_PCM = b"\x00\x00" * 240
SENTENCES = [
"Hello there, this is the first sentence.",
"And here comes a second sentence!",
"Finally a third one?",
]
async def test_websocket_authenticates_once_for_multiple_streams() -> None:
sent: asyncio.Queue[str] = asyncio.Queue()
auth_headers: list[str | None] = []
async def handle(request: web.Request) -> web.WebSocketResponse:
auth_headers.append(request.headers.get("Authorization"))
socket = web.WebSocketResponse()
await socket.prepare(request)
async for message in socket:
if message.type == aiohttp.WSMsgType.TEXT:
await sent.put(message.data)
return socket
app = web.Application()
app.router.add_get("/tts", handle)
async with TestServer(app) as server, http.open() as session:
tts = soniox.TTS(
api_key="test-key",
http_session=session,
websocket_url=str(server.make_url("/tts")),
)
try:
for stream_id in ("first", "second"):
connection = await tts._current_connection(
conn_options=APIConnectOptions(max_retry=0)
)
waiter = asyncio.get_running_loop().create_future()
connection.register_stream(
stream_id,
MagicMock(),
waiter,
opts=tts._opts,
read_deadline=observed_attempts("tts_stream", APIConnectOptions()),
)
try:
connection.send_text(stream_id, "Hello!", text_end=True)
config = json.loads(await asyncio.wait_for(sent.get(), timeout=1.0))
text = json.loads(await asyncio.wait_for(sent.get(), timeout=1.0))
assert "api_key" not in config
assert config["model"] == tts.model
assert config["stream_id"] == stream_id
assert text == {"stream_id": stream_id, "text": "Hello!", "text_end": True}
finally:
connection.unregister_stream(stream_id)
waiter.cancel()
assert auth_headers == ["Bearer test-key"]
finally:
await tts.aclose()
@pytest.mark.parametrize("stream_id", [None, ""])
@pytest.mark.parametrize("status_code", [401, 429])
@pytest.mark.parametrize("register_before_error", [False, True])
async def test_connection_error_reaches_all_streams(
stream_id: str | None, status_code: int, register_before_error: bool
) -> None:
tts = soniox.TTS(api_key="test-key")
connection = _Connection(tts._opts, MagicMock())
payload: dict[str, Any] = {
"error_code": status_code,
"error_message": "Connection rejected",
"request_id": "test-request",
}
if stream_id is not None:
payload["stream_id"] = stream_id
ws = MagicMock(spec=http.WebSocket)
ws.closed = False
ws.close_code = 1008
ws.receive = AsyncMock(
side_effect=[
http.WebSocketMessage(http.WebSocketMessageType.TEXT, json.dumps(payload)),
http.WebSocketMessage(http.WebSocketMessageType.CLOSE, ""),
]
)
connection._ws = ws
waiters: list[asyncio.Future[None]] = []
def register_stream(name: str) -> None:
waiter = asyncio.get_running_loop().create_future()
waiters.append(waiter)
connection.register_stream(
name,
MagicMock(),
waiter,
opts=tts._opts,
read_deadline=observed_attempts("tts_stream", APIConnectOptions()),
)
try:
if register_before_error:
register_stream("first")
register_stream("second")
await connection._recv_loop()
register_stream("closing")
assert connection._close_task is not None
await connection._close_task
register_stream("closed")
for waiter in waiters:
with pytest.raises(APIStatusError) as exc_info:
await asyncio.wait_for(waiter, timeout=1.0)
assert exc_info.value.status_code == status_code
assert exc_info.value.message == "Connection rejected"
assert exc_info.value.retryable is (status_code == 429)
assert exc_info.value.request_id == "test-request"
assert not connection.is_current
assert connection.closed
assert connection.num_active_streams == 0
ws.receive.assert_awaited_once()
finally:
await connection.aclose()
if connection._close_task is not None:
await connection._close_task
for waiter in waiters:
if waiter.done() and not waiter.cancelled():
waiter.exception()
else:
waiter.cancel()
await tts.aclose()
@dataclass
class _StreamSlot:
emitter: AudioEmitter
waiter: asyncio.Future[None]
class _FakeConnection:
"""Stand-in for ``_Connection``: text produces audio, ``text_end`` terminates."""
def __init__(self, *, fail_on_send: int | None = None) -> None:
self.is_current = True
self.closed = False
self.registered_ids: list[str] = []
self.send_calls: list[tuple[str, str, bool]] = []
self.max_open_streams = 0
self._streams: dict[str, _StreamSlot] = {}
self._sends = 0
self._fail_on_send = fail_on_send
def register_stream(
self,
stream_id: str,
emitter: AudioEmitter,
waiter: asyncio.Future[None],
*,
opts: Any,
read_deadline: ProviderAttempts,
) -> None:
self.registered_ids.append(stream_id)
self._streams[stream_id] = _StreamSlot(emitter, waiter)
self.max_open_streams = max(self.max_open_streams, len(self._streams))
def unregister_stream(self, stream_id: str) -> None:
self._streams.pop(stream_id, None)
def send_text(self, stream_id: str, text: str, *, text_end: bool = False) -> None:
self.send_calls.append((stream_id, text, text_end))
slot = self._streams.get(stream_id)
if slot is None:
return
if text_end and not text:
# server: final audio + audio_end + terminated
if not slot.waiter.done():
slot.waiter.set_result(None)
return
self._sends += 1
if self._sends == self._fail_on_send:
self._fail_on_send = None
if not slot.waiter.done():
slot.waiter.set_exception(
APIStatusError("transient", status_code=429, retryable=True)
)
return
slot.emitter.push(_SILENCE_PCM)
def cancel_stream(self, stream_id: str) -> None:
slot = self._streams.get(stream_id)
if slot is not None and not slot.waiter.done():
slot.waiter.set_result(None)
async def _synthesize(
fake: _FakeConnection,
*,
chunk_delays: list[float] | None = None,
**tts_kwargs: Any,
) -> int:
tts = soniox.TTS(api_key="fake-key", **tts_kwargs)
async def _fake_current_connection(conn_options: APIConnectOptions) -> _FakeConnection:
return fake
tts._current_connection = _fake_current_connection # type: ignore[method-assign]
stream = tts._stream(conn_options=DEFAULT_API_CONNECT_OPTIONS)
delays = chunk_delays or [0.01] * len(SENTENCES)
async def _push() -> None:
for chunk, delay in zip(SENTENCES, delays, strict=True):
stream._push_chunk(tts_module.SpeechChunk(text=chunk, said=chunk))
await asyncio.sleep(delay)
stream.end_input()
push_t = asyncio.create_task(_push())
frames = 0
async for _ in stream:
frames += 1
await push_t
await stream.aclose()
return frames
async def test_steady_flow_uses_single_stream() -> None:
fake = _FakeConnection()
frames = await _synthesize(fake)
assert frames > 0
assert len(fake.registered_ids) == 1
text_sends = [text for _, text, end in fake.send_calls if not end]
end_sends = [c for c in fake.send_calls if c[2]]
assert text_sends == [sentence + " " for sentence in SENTENCES]
assert len(end_sends) == 1
async def test_idle_stall_rotates_stream() -> None:
fake = _FakeConnection()
frames = await _synthesize(
fake,
chunk_delays=[3.0, 0.01, 0.01],
stream_idle_timeout=1.0,
)
assert frames > 0
# sentence 1 on the first stream, finalized during the stall; 2 and 3 on the next
assert len(fake.registered_ids) == 2
assert sum(1 for c in fake.send_calls if c[2]) == 2
assert fake.max_open_streams == 1
async def test_transient_failure_replays_batch() -> None:
fake = _FakeConnection(fail_on_send=1)
frames = await _synthesize(fake)
assert frames > 0
assert len(fake.registered_ids) == 2
failed_id, replacement_id = fake.registered_ids
replayed = [text for sid, text, end in fake.send_calls if sid == replacement_id and not end]
assert replayed[0] == SENTENCES[0] + " "
assert fake.max_open_streams == 1
async def test_invalid_stream_idle_timeout_rejected() -> None:
with pytest.raises(ValueError):
soniox.TTS(api_key="fake-key", stream_idle_timeout=0)
with pytest.raises(ValueError):
soniox.TTS(api_key="fake-key", stream_idle_timeout=-1.0)
async def test_reduce_silence_rejected_on_unsupported_model() -> None:
# default model is tts-rt-v1-preview, which lacks supports_silence_reduction
with pytest.raises(ValueError, match="reduce_silence"):
soniox.TTS(api_key="fake-key", reduce_silence=True)
with pytest.raises(ValueError, match="reduce_silence"):
soniox.TTS(api_key="fake-key", model="tts-rt-v1", reduce_silence=True)
tts = soniox.TTS(api_key="fake-key")
with pytest.raises(ValueError, match="reduce_silence"):
tts.update_options(reduce_silence=True)
assert tts._opts.reduce_silence is False
tts = soniox.TTS(api_key="fake-key", model="tts-rt-v2", reduce_silence=True)
with pytest.raises(ValueError, match="reduce_silence"):
tts.update_options(model="tts-rt-v1")
assert tts._opts.model == "tts-rt-v2"