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

991 lines
33 KiB
Python

"""Unit tests for ElevenLabs TTS plugin configuration and websocket behavior."""
import asyncio
import base64
import json
import logging
from types import SimpleNamespace
import pytest
from livekit.agents import DEFAULT_API_CONNECT_OPTIONS, http, tts as agents_tts, utils
from livekit.plugins.elevenlabs import tts as elevenlabs_tts
pytestmark = pytest.mark.plugin("elevenlabs")
class _FakeWebSocket:
def __init__(self, messages: list[object]) -> None:
self._messages = messages
self.closed = False
self.close_code = 1000
async def receive(self) -> object:
if self._messages:
return self._messages.pop(0)
return SimpleNamespace(type=http.WebSocketMessageType.CLOSE, data="")
async def close(self) -> None:
self.closed = True
class _FakeEmitter:
def __init__(self) -> None:
self.audio_chunks: list[bytes] = []
self.timed_transcript_pushes = 0
def push(self, audio: bytes) -> None:
self.audio_chunks.append(audio)
def push_timed_transcript(self, _timed_words: object) -> None:
self.timed_transcript_pushes += 1
class _FakeConnection:
def __init__(self, context_id: str, messages: list[object]) -> None:
self._closed = False
self._ws = _FakeWebSocket(messages)
self._is_current = True
self._active_contexts = {context_id}
self._input_queue = utils.aio.Chan[object]()
self.emitter = _FakeEmitter()
self.waiter: asyncio.Future[None] = asyncio.get_event_loop().create_future()
self._context_data = {
context_id: elevenlabs_tts._StreamData(
emitter=self.emitter,
alignment=elevenlabs_tts._AlignmentBuffer(),
waiter=self.waiter,
)
}
self.preferred_alignment = "normalized"
def unregister_stream(self, context_id: str) -> None:
elevenlabs_tts._Connection.unregister_stream(self, context_id) # pyright: ignore[reportArgumentType]
def _cleanup_context(self, context_id: str) -> None:
elevenlabs_tts._Connection._cleanup_context(self, context_id) # pyright: ignore[reportArgumentType]
async def aclose(self) -> None:
self._closed = True
await self._ws.close()
def _websocket_text_message(payload: dict[str, object]) -> object:
return SimpleNamespace(type=http.WebSocketMessageType.TEXT, data=json.dumps(payload))
def test_synthesize_url_carries_no_streaming_latency_optimization() -> None:
tts = elevenlabs_tts.TTS(api_key="test-key", voice_id="voice-1")
url = elevenlabs_tts._synthesize_url(tts._opts) # pyright: ignore[reportPrivateUsage]
assert url.startswith(f"{tts._opts.base_url}/text-to-speech/voice-1/stream?")
assert "optimize_streaming_latency" not in url
with pytest.raises(TypeError):
elevenlabs_tts.TTS(api_key="test-key", streaming_latency=3) # type: ignore[call-arg]
def test_auto_mode_defaults_to_false_with_chunk_length_schedule() -> None:
tts = elevenlabs_tts.TTS(api_key="test-key", chunk_length_schedule=[120, 160, 250, 290])
assert tts._opts.auto_mode is False
@pytest.mark.asyncio
async def test_prewarm_opens_connection(monkeypatch: pytest.MonkeyPatch) -> None:
opened = asyncio.Event()
async def _current_connection(
self: object,
) -> tuple[SimpleNamespace, float, bool]:
opened.set()
return SimpleNamespace()
monkeypatch.setattr(elevenlabs_tts.TTS, "_current_connection", _current_connection)
tts = elevenlabs_tts.TTS(api_key="test-key")
tts.prewarm()
await asyncio.wait_for(opened.wait(), timeout=1)
await tts.aclose()
@pytest.mark.asyncio
async def test_option_update_during_connect_discards_stale_socket(
monkeypatch: pytest.MonkeyPatch,
) -> None:
connect_started = asyncio.Event()
resume_connect = asyncio.Event()
connections: list[object] = []
class _StubConnection:
def __init__(self, opts: object, session: object, conn_options: object) -> None:
self._opts = opts
self._conn_options = conn_options
self._closed = False
self.has_active_contexts = False
self.is_current = True
self._recv_task = None
connections.append(self)
async def connect(self) -> None:
if len(connections) == 1:
connect_started.set()
await resume_connect.wait()
async def aclose(self) -> None:
self._closed = True
def mark_non_current(self) -> None:
self.is_current = False
monkeypatch.setattr(elevenlabs_tts, "_Connection", _StubConnection)
async with http.open() as session:
tts = elevenlabs_tts.TTS(api_key="test-key", voice_id="old", http_session=session)
connection_task = asyncio.create_task(tts._current_connection())
await asyncio.wait_for(connect_started.wait(), timeout=1)
tts.update_options(voice_id="new")
resume_connect.set()
connection = await asyncio.wait_for(connection_task, timeout=1)
assert len(connections) == 2
assert connections[0]._closed # pyright: ignore[reportAttributeAccessIssue]
assert connection is connections[1]
assert connection._opts.voice_id == "new"
await tts.aclose()
@pytest.mark.asyncio
@pytest.mark.asyncio
async def test_prewarm_stops_on_non_retryable_api_error(monkeypatch: pytest.MonkeyPatch) -> None:
attempts = 0
finished = asyncio.Event()
async def _current_connection(self: object) -> tuple[SimpleNamespace, float, bool]:
nonlocal attempts
attempts += 1
raise elevenlabs_tts.APIStatusError("Unauthorized", status_code=401)
monkeypatch.setattr(elevenlabs_tts.TTS, "_current_connection", _current_connection)
async def unexpected_retry(_delay: float) -> None:
finished.set()
original_sleep = asyncio.sleep
monkeypatch.setattr(elevenlabs_tts.asyncio, "sleep", unexpected_retry)
tts = elevenlabs_tts.TTS(api_key="test-key")
tts.prewarm()
await original_sleep(0)
await original_sleep(0)
assert attempts == 1
assert not finished.is_set()
await tts.aclose()
async def test_prewarm_retries_with_exponential_backoff(
monkeypatch: pytest.MonkeyPatch,
) -> None:
attempts = 0
delays: list[float] = []
ready = asyncio.Event()
async def _current_connection(
self: object,
) -> tuple[SimpleNamespace, float, bool]:
nonlocal attempts
attempts += 1
if attempts <= 3:
raise ConnectionError("temporary failure")
ready.set()
return SimpleNamespace()
async def _sleep(delay: float) -> None:
delays.append(delay)
monkeypatch.setattr(elevenlabs_tts.TTS, "_current_connection", _current_connection)
monkeypatch.setattr(elevenlabs_tts.asyncio, "sleep", _sleep)
tts = elevenlabs_tts.TTS(api_key="test-key")
tts.prewarm()
await asyncio.wait_for(ready.wait(), timeout=1)
await tts.aclose()
assert attempts == 4
assert delays == [1.0, 2.0, 4.0]
async def test_dialogue_prewarm_reconnects_when_idle_socket_closes(
monkeypatch: pytest.MonkeyPatch,
) -> None:
connections: list[asyncio.Future[None]] = []
reconnected = asyncio.Event()
async def _current_connection(
self: object,
) -> tuple[SimpleNamespace, float, bool]:
recv_task = asyncio.get_running_loop().create_future()
connections.append(recv_task)
if len(connections) == 2:
reconnected.set()
async def wait_closed() -> None:
await asyncio.wait({recv_task})
return SimpleNamespace(wait_closed=wait_closed)
monkeypatch.setattr(elevenlabs_tts.TTS, "_current_connection", _current_connection)
tts = elevenlabs_tts.TTS(api_key="test-key", model="eleven_v4")
tts.prewarm()
while not connections:
await asyncio.sleep(0)
connections[0].cancel()
await asyncio.wait_for(reconnected.wait(), timeout=1)
await tts.aclose()
assert len(connections) == 2
assert not connections[1].cancelled()
def test_build_context_init_packet_includes_generation_config() -> None:
tts = elevenlabs_tts.TTS(api_key="test-key", chunk_length_schedule=[80, 120], auto_mode=False)
packet = elevenlabs_tts._build_context_init_packet( # pyright: ignore[reportPrivateUsage]
tts._opts, context_id="ctx-1"
)
assert packet["text"] == " "
assert packet["context_id"] == "ctx-1"
assert packet["generation_config"] == {"chunk_length_schedule": [80, 120]}
def test_build_context_init_packet_omits_generation_config_when_not_set() -> None:
tts = elevenlabs_tts.TTS(api_key="test-key")
packet = elevenlabs_tts._build_context_init_packet( # pyright: ignore[reportPrivateUsage]
tts._opts, context_id="ctx-2"
)
assert "generation_config" not in packet
def test_build_context_init_packet_includes_pronunciation_dictionaries() -> None:
tts = elevenlabs_tts.TTS(
api_key="test-key",
pronunciation_dictionary_locators=[
elevenlabs_tts.PronunciationDictionaryLocator(
pronunciation_dictionary_id="dict-1",
version_id="v1",
)
],
)
packet = elevenlabs_tts._build_context_init_packet( # pyright: ignore[reportPrivateUsage]
tts._opts, context_id="ctx-3"
)
assert packet["pronunciation_dictionary_locators"] == [
{
"pronunciation_dictionary_id": "dict-1",
"version_id": "v1",
}
]
@pytest.mark.asyncio
async def test_recv_loop_accepts_snake_case_context_id() -> None:
context_id = "ctx_123"
audio_chunk = b"hello-audio"
connection = _FakeConnection(
context_id,
[
_websocket_text_message(
{
"context_id": context_id,
"audio": base64.b64encode(audio_chunk).decode("ascii"),
"isFinal": True,
}
),
],
)
await elevenlabs_tts._Connection._recv_loop(connection)
assert connection.emitter.audio_chunks == [audio_chunk]
assert connection.waiter.done()
assert connection.waiter.result() is None
assert connection._context_data == {}
@pytest.mark.asyncio
async def test_recv_loop_still_accepts_camel_case_context_id() -> None:
context_id = "ctx_123"
audio_chunk = b"hello-audio"
connection = _FakeConnection(
context_id,
[
_websocket_text_message(
{
"contextId": context_id,
"audio": base64.b64encode(audio_chunk).decode("ascii"),
"isFinal": True,
}
),
],
)
await elevenlabs_tts._Connection._recv_loop(connection)
assert connection.emitter.audio_chunks == [audio_chunk]
assert connection.waiter.done()
assert connection.waiter.result() is None
assert connection._context_data == {}
@pytest.mark.asyncio
async def test_recv_loop_ignores_flush_done_for_active_context() -> None:
context_id = "ctx_123"
audio_chunk = b"hello-audio"
connection = _FakeConnection(
context_id,
[
_websocket_text_message(
{
"type": "flush_done",
"context_id": context_id,
"status_code": 206,
"done": False,
"data": "",
"flush_done": True,
}
),
_websocket_text_message(
{
"context_id": context_id,
"audio": base64.b64encode(audio_chunk).decode("ascii"),
"isFinal": True,
}
),
],
)
await elevenlabs_tts._Connection._recv_loop(connection)
assert connection.emitter.audio_chunks == [audio_chunk]
assert connection.waiter.done()
assert connection.waiter.result() is None
@pytest.mark.asyncio
async def test_recv_loop_ignores_flush_done_for_inactive_context() -> None:
context_id = "ctx_123"
audio_chunk = b"hello-audio"
connection = _FakeConnection(
context_id,
[
_websocket_text_message(
{
"type": "flush_done",
"context_id": "already_closed_context",
"status_code": 206,
"done": False,
"data": "",
"flush_done": True,
}
),
_websocket_text_message(
{
"context_id": context_id,
"audio": base64.b64encode(audio_chunk).decode("ascii"),
"isFinal": True,
}
),
],
)
await elevenlabs_tts._Connection._recv_loop(connection)
assert connection.emitter.audio_chunks == [audio_chunk]
assert connection.waiter.done()
assert connection.waiter.result() is None
@pytest.mark.asyncio
async def test_recv_loop_drops_audio_for_unregistered_context() -> None:
"""Audio flushed after the stream ended must not reach its emitter."""
context_id = "ctx_123"
connection = _FakeConnection(
context_id,
[
_websocket_text_message(
{
"context_id": context_id,
"audio": base64.b64encode(b"late-audio").decode("ascii"),
"isFinal": True,
}
),
],
)
connection.unregister_stream(context_id)
await elevenlabs_tts._Connection._recv_loop(connection)
assert connection.emitter.audio_chunks == []
# the server released the context, so the connection can drain
assert connection._active_contexts == set()
@pytest.mark.asyncio
@pytest.mark.parametrize(
"connection_cls", [elevenlabs_tts._Connection, elevenlabs_tts._DialogueConnection]
)
@pytest.mark.parametrize("input_closed", [False, True])
async def test_recv_loop_resets_timeout_timer_on_audio(
connection_cls: type, input_closed: bool
) -> None:
context_id = "ctx_123"
connection = _FakeConnection(
context_id,
[
_websocket_text_message(
{
"context_id": context_id,
"audio": base64.b64encode(b"hello-audio").decode("ascii"),
}
),
],
)
ctx = connection._context_data[context_id]
ctx.input_closed = input_closed
timer = asyncio.get_event_loop().call_later(60, lambda: None)
ctx.timeout_timer = timer
restarted: list[str] = []
connection._start_timeout_timer = restarted.append # type: ignore[attr-defined]
final_key = "isFinal" if connection_cls is elevenlabs_tts._Connection else "is_final"
connection._ws._messages.append(
_websocket_text_message({"context_id": context_id, final_key: True})
)
await connection_cls._recv_loop(connection)
assert connection.waiter.result() is None
# cleared so _start_timeout_timer can arm a new timer on the next send
assert timer.cancelled()
assert ctx.timeout_timer is None
# after close_context, no send will re-arm it: the audio handler must
assert restarted == ([context_id] if input_closed else [])
@pytest.mark.asyncio
@pytest.mark.parametrize(
("connection_cls", "model"),
[
(elevenlabs_tts._Connection, "eleven_flash_v2_5"),
(elevenlabs_tts._DialogueConnection, "eleven_v3_conversational"),
],
)
async def test_send_loop_arms_timeout_on_close_context(connection_cls: type, model: str) -> None:
tts = elevenlabs_tts.TTS(api_key="test-key", model=model, voice_id="voice-1")
async with http.open() as session:
connection = connection_cls(tts._opts, session)
connection._ws = _RecordingWs()
ctx = elevenlabs_tts._StreamData(
emitter=_FakeEmitter(), # type: ignore[arg-type]
alignment=elevenlabs_tts._AlignmentBuffer(),
read_timeout=60,
waiter=asyncio.get_event_loop().create_future(),
)
connection._context_data["ctx-1"] = ctx
connection._active_contexts.add("ctx-1")
connection.close_context("ctx-1")
connection._input_queue.close()
await asyncio.wait_for(connection._send_loop(), timeout=1.0)
# no more sends will arm it, so close_context must
assert ctx.input_closed
assert ctx.timeout_timer is not None
ctx.timeout_timer.cancel()
def test_unregister_stream_keeps_the_context_closable() -> None:
"""close_context() must still reach the server, otherwise contexts leak (#5844)."""
context_id = "ctx_123"
connection = _FakeConnection(context_id, [])
connection.unregister_stream(context_id)
assert context_id not in connection._context_data
assert context_id in connection._active_contexts
elevenlabs_tts._Connection.close_context(connection, context_id) # pyright: ignore[reportArgumentType]
assert connection._input_queue.recv_nowait() == elevenlabs_tts._CloseContext(context_id)
@pytest.mark.asyncio
async def test_interrupted_stream_unregisters_before_ending_the_segment(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Regression for #6929: a cancelled run must stop routing audio to its emitter."""
calls: list[str] = []
registered = asyncio.Event()
class _StubConnection:
def register_stream(self, **kwargs: object) -> None:
registered.set()
def send_content(self, content: object) -> None:
calls.append("send_content")
def unregister_stream(self, context_id: str) -> None:
calls.append("unregister_stream")
def close_context(self, context_id: str) -> None:
calls.append("close_context")
connection = _StubConnection()
async def _current_connection(self: object, **kwargs: object) -> object:
return connection
original_end_segment = agents_tts.AudioEmitter.end_segment
def _end_segment(self: agents_tts.AudioEmitter) -> None:
calls.append("end_segment")
original_end_segment(self)
monkeypatch.setattr(elevenlabs_tts.TTS, "_current_connection", _current_connection)
monkeypatch.setattr(agents_tts.AudioEmitter, "end_segment", _end_segment)
tts = elevenlabs_tts.TTS(api_key="test-key", encoding="pcm_24000")
stream = tts.stream(conn_options=DEFAULT_API_CONNECT_OPTIONS)
stream.push_text("hello world. ")
consumer = asyncio.create_task(anext(stream))
await asyncio.wait_for(registered.wait(), timeout=1)
await stream.aclose()
try:
await consumer
except (StopAsyncIteration, asyncio.CancelledError):
pass
assert calls.index("unregister_stream") < calls.index("end_segment")
assert "close_context" in calls
@pytest.mark.asyncio
@pytest.mark.parametrize("auto_mode", [True, False])
async def test_stream_sends_each_chunk_as_is(
monkeypatch: pytest.MonkeyPatch, auto_mode: bool
) -> None:
sent: list[tuple[str, bool]] = []
closed = asyncio.Event()
class _StubConnection:
def register_stream(self, **kwargs: object) -> None:
pass
def send_content(self, content: elevenlabs_tts._SynthesizeContent) -> None:
sent.append((content.text, content.flush))
def unregister_stream(self, context_id: str) -> None:
pass
def close_context(self, context_id: str) -> None:
closed.set()
connection = _StubConnection()
async def _current_connection(self: object, **kwargs: object) -> object:
return connection
monkeypatch.setattr(elevenlabs_tts.TTS, "_current_connection", _current_connection)
tts = elevenlabs_tts.TTS(api_key="test-key", encoding="pcm_24000", auto_mode=auto_mode)
assert tts._core_text_unit() == ("sentences" if auto_mode else "words")
stream = tts._stream(conn_options=DEFAULT_API_CONNECT_OPTIONS)
stream._push_chunk(agents_tts.SpeechChunk(text="Hello there.", said="Hello there."))
stream._push_chunk(agents_tts.SpeechChunk(text='<break time="1s"/>', said=""))
stream.flush()
stream._push_chunk(agents_tts.SpeechChunk(text="Bye.", said="Bye."))
stream.end_input()
await asyncio.wait_for(closed.wait(), timeout=1)
await stream.aclose()
assert sent == [
("Hello there. ", auto_mode),
('<break time="1s"/> ', auto_mode),
("Bye. ", auto_mode),
("", True),
]
# -- eleven_v3 / eleven_v4 (text-to-dialogue) ----------------------------------------
def test_dialogue_synthesize_url_targets_text_to_dialogue_endpoint() -> None:
tts = elevenlabs_tts.TTS(api_key="test-key", model="eleven_v3_conversational")
url = elevenlabs_tts._dialogue_synthesize_url(tts._opts) # pyright: ignore[reportPrivateUsage]
assert url.startswith(f"{elevenlabs_tts.API_BASE_URL_V1}/text-to-dialogue/stream?")
assert "voice_id" not in url
def test_dialogue_multi_stream_url_omits_regular_tts_only_params() -> None:
tts = elevenlabs_tts.TTS(
api_key="test-key",
model="eleven_v3_conversational",
voice_id="voice-1",
enable_ssml_parsing=True,
chunk_length_schedule=[80, 120],
)
url = elevenlabs_tts._dialogue_multi_stream_url( # pyright: ignore[reportPrivateUsage]
tts._opts
)
assert url.startswith("wss://")
assert "/text-to-dialogue/multi-stream-input?" in url
assert "voice-1" not in url
assert "model_id=eleven_v3_conversational" in url
assert "enable_ssml_parsing" not in url
assert "inactivity_timeout" not in url
assert "auto_mode" not in url
def test_build_dialogue_synthesize_body_single_turn() -> None:
tts = elevenlabs_tts.TTS(
api_key="test-key",
model="eleven_v3_conversational",
voice_id="voice-1",
pronunciation_dictionary_locators=[
elevenlabs_tts.PronunciationDictionaryLocator(
pronunciation_dictionary_id="dict-1",
version_id="v1",
)
],
)
body = elevenlabs_tts._build_dialogue_synthesize_body( # pyright: ignore[reportPrivateUsage]
tts._opts, "hello there", voice_settings=None
)
assert body["inputs"] == [{"text": "hello there", "voice_id": "voice-1"}]
assert body["model_id"] == "eleven_v3_conversational"
assert "settings" not in body
assert body["pronunciation_dictionary_locators"] == [
{"pronunciation_dictionary_id": "dict-1", "version_id": "v1"}
]
def test_build_dialogue_synthesize_body_keeps_only_supported_settings() -> None:
tts = elevenlabs_tts.TTS(
api_key="test-key", model="eleven_v3_conversational", voice_id="voice-1"
)
body = elevenlabs_tts._build_dialogue_synthesize_body( # pyright: ignore[reportPrivateUsage]
tts._opts, "hello there", voice_settings={"stability": 0.5, "similarity_boost": 0.75}
)
assert body["settings"] == {"stability": 0.5}
def test_build_dialogue_context_init_packet_keeps_only_supported_voice_settings() -> None:
tts = elevenlabs_tts.TTS(
api_key="test-key",
model="eleven_v3_conversational",
voice_id="voice-1",
voice_settings=elevenlabs_tts.VoiceSettings(stability=0.5, similarity_boost=0.75),
)
packet = elevenlabs_tts._build_dialogue_context_init_packet( # pyright: ignore[reportPrivateUsage]
tts._opts, context_id="ctx-1"
)
assert packet["voice_settings"] == {"stability": 0.5}
def test_dialogue_model_warns_on_unsupported_voice_settings(
caplog: pytest.LogCaptureFixture,
) -> None:
with caplog.at_level(logging.WARNING):
elevenlabs_tts.TTS(
api_key="test-key",
model="eleven_v3_conversational",
voice_settings=elevenlabs_tts.VoiceSettings(stability=0.5, similarity_boost=0.75),
)
assert any("voice_settings.similarity_boost" in r.getMessage() for r in caplog.records)
@pytest.mark.asyncio
async def test_dialogue_recv_loop_parses_audio_and_alignment() -> None:
context_id = "ctx_123"
audio_chunk = b"hello-audio"
connection = _FakeConnection(
context_id,
[
_websocket_text_message(
{
"context_id": context_id,
"audio": base64.b64encode(audio_chunk).decode("ascii"),
"alignment": {
"chars": ["h", "i"],
"char_start_times_ms": [0, 100],
"char_durations_ms": [100, 100],
},
"is_final": True,
}
),
],
)
await elevenlabs_tts._DialogueConnection._recv_loop( # pyright: ignore[reportPrivateUsage]
connection
)
assert connection.emitter.audio_chunks == [audio_chunk]
assert connection.emitter.timed_transcript_pushes >= 1
assert connection.waiter.done()
assert connection.waiter.result() is None
assert connection._context_data == {}
@pytest.mark.asyncio
async def test_dialogue_recv_loop_turn_boundary_does_not_resolve_waiter() -> None:
context_id = "ctx_123"
audio_chunk = b"hello-audio"
connection = _FakeConnection(
context_id,
[
_websocket_text_message(
{
"context_id": context_id,
"audio": base64.b64encode(audio_chunk).decode("ascii"),
"is_final_audio_for_turn": True,
}
),
_websocket_text_message(
{
"context_id": context_id,
"is_final": True,
}
),
],
)
await elevenlabs_tts._DialogueConnection._recv_loop( # pyright: ignore[reportPrivateUsage]
connection
)
assert connection.emitter.audio_chunks == [audio_chunk]
assert connection.waiter.done()
assert connection.waiter.result() is None
@pytest.mark.asyncio
async def test_dialogue_recv_loop_reports_error() -> None:
context_id = "ctx_123"
connection = _FakeConnection(
context_id,
[
_websocket_text_message(
{
"context_id": context_id,
"error": "something went wrong",
}
),
],
)
await elevenlabs_tts._DialogueConnection._recv_loop( # pyright: ignore[reportPrivateUsage]
connection
)
assert connection.waiter.done()
exc = connection.waiter.exception()
assert isinstance(exc, elevenlabs_tts.APIError)
assert connection._context_data == {}
@pytest.mark.asyncio
async def test_dialogue_recv_loop_drops_audio_for_unregistered_context() -> None:
context_id = "ctx_123"
connection = _FakeConnection(
context_id,
[
_websocket_text_message(
{
"context_id": context_id,
"audio": base64.b64encode(b"late-audio").decode("ascii"),
"is_final": True,
}
),
],
)
connection.unregister_stream(context_id)
await elevenlabs_tts._DialogueConnection._recv_loop( # pyright: ignore[reportPrivateUsage]
connection
)
assert connection.emitter.audio_chunks == []
assert connection._active_contexts == set()
class _RecordingWs:
def __init__(self) -> None:
self.sent: list[dict[str, object]] = []
self.closed = False
async def send_json(self, data: dict[str, object]) -> None:
self.sent.append(data)
async def close(self) -> None:
self.closed = True
@pytest.mark.asyncio
async def test_dialogue_send_loop_sends_close_context_without_waiting() -> None:
tts = elevenlabs_tts.TTS(
api_key="test-key", model="eleven_v3_conversational", voice_id="voice-1"
)
async with http.open() as session:
connection = elevenlabs_tts._DialogueConnection( # pyright: ignore[reportPrivateUsage]
tts._opts, session
)
ws = _RecordingWs()
connection._ws = ws # type: ignore[assignment]
connection.send_content(
elevenlabs_tts._SynthesizeContent("ctx-1", "hello ", flush=True) # pyright: ignore[reportPrivateUsage]
)
connection.close_context("ctx-1")
connection._input_queue.close()
await asyncio.wait_for(connection._send_loop(), timeout=1.0)
assert ws.sent == [
{"context_id": "ctx-1", "voices": ["voice-1"]},
{
"context_id": "ctx-1",
"inputs": [{"text": "hello ", "voice_id": "voice-1"}],
"flush": True,
},
{"context_id": "ctx-1", "close_context": True},
]
@pytest.mark.asyncio
async def test_dialogue_send_loop_sends_keep_alive_for_idle_context(
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setattr(elevenlabs_tts, "_DIALOGUE_KEEP_ALIVE_INTERVAL", 0.05)
tts = elevenlabs_tts.TTS(
api_key="test-key", model="eleven_v3_conversational", voice_id="voice-1"
)
async with http.open() as session:
connection = elevenlabs_tts._DialogueConnection( # pyright: ignore[reportPrivateUsage]
tts._opts, session
)
ws = _RecordingWs()
connection._ws = ws # type: ignore[assignment]
connection.send_content(
elevenlabs_tts._SynthesizeContent("ctx-1", "hello ", flush=True) # pyright: ignore[reportPrivateUsage]
)
send_task = asyncio.create_task(connection._send_loop())
await asyncio.sleep(0.2)
assert {"context_id": "ctx-1", "keep_alive": True} in ws.sent
connection._input_queue.close()
await asyncio.wait_for(send_task, timeout=1.0)
@pytest.mark.asyncio
async def test_dialogue_send_loop_keeps_idle_context_alive_during_other_traffic(
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setattr(elevenlabs_tts, "_DIALOGUE_KEEP_ALIVE_INTERVAL", 0.05)
tts = elevenlabs_tts.TTS(
api_key="test-key", model="eleven_v3_conversational", voice_id="voice-1"
)
async with http.open() as session:
connection = elevenlabs_tts._DialogueConnection( # pyright: ignore[reportPrivateUsage]
tts._opts, session
)
ws = _RecordingWs()
connection._ws = ws # type: ignore[assignment]
connection.send_content(
elevenlabs_tts._SynthesizeContent("ctx-idle", "hello ") # pyright: ignore[reportPrivateUsage]
)
send_task = asyncio.create_task(connection._send_loop())
for _ in range(20):
connection.send_content(
elevenlabs_tts._SynthesizeContent("ctx-busy", "hello ") # pyright: ignore[reportPrivateUsage]
)
await asyncio.sleep(0.01)
assert {"context_id": "ctx-idle", "keep_alive": True} in ws.sent
connection._input_queue.close()
await asyncio.wait_for(send_task, timeout=1.0)
@pytest.mark.asyncio
async def test_dialogue_send_loop_stops_keep_alive_once_context_closes(
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setattr(elevenlabs_tts, "_DIALOGUE_KEEP_ALIVE_INTERVAL", 0.05)
tts = elevenlabs_tts.TTS(
api_key="test-key", model="eleven_v3_conversational", voice_id="voice-1"
)
async with http.open() as session:
connection = elevenlabs_tts._DialogueConnection( # pyright: ignore[reportPrivateUsage]
tts._opts, session
)
ws = _RecordingWs()
connection._ws = ws # type: ignore[assignment]
connection.send_content(
elevenlabs_tts._SynthesizeContent("ctx-1", "hello ", flush=True) # pyright: ignore[reportPrivateUsage]
)
connection.close_context("ctx-1")
send_task = asyncio.create_task(connection._send_loop())
await asyncio.sleep(0.2)
assert {"context_id": "ctx-1", "keep_alive": True} not in ws.sent
assert {"context_id": "ctx-1", "close_context": True} in ws.sent
connection._input_queue.close()
await asyncio.wait_for(send_task, timeout=1.0)
@pytest.mark.asyncio
async def test_disabled_response_timeout_keeps_pending_context() -> None:
connection = object.__new__(elevenlabs_tts._Connection)
waiter = asyncio.get_running_loop().create_future()
context_id = "pending-context"
ctx = elevenlabs_tts._StreamData(
emitter=_FakeEmitter(), # type: ignore[arg-type]
alignment=elevenlabs_tts._AlignmentBuffer(),
read_timeout=None,
waiter=waiter,
)
connection._context_data = {context_id: ctx}
connection._active_contexts = {context_id}
try:
connection._start_timeout_timer(context_id)
await asyncio.sleep(0)
await asyncio.sleep(0)
assert not waiter.done()
assert context_id in connection._context_data
finally:
if ctx.timeout_timer is not None:
ctx.timeout_timer.cancel()
if waiter.done() and not waiter.cancelled():
waiter.exception()
waiter.cancel()
# Catches: the strict frame model refusing ElevenLabs' `"isFinal": null`, which it sends on every
# audio frame but the last, so every streamed synthesis failed with "Could not synthesize".
def test_a_frame_with_a_null_final_flag_is_not_final() -> None:
from livekit.plugins.elevenlabs.models import TTSFrame
frame = TTSFrame.model_validate_json('{"audio": "AAAA", "isFinal": null, "contextId": "c"}')
assert frame.audio == "AAAA" and frame.is_final is False and frame.context_id == "c"
assert TTSFrame.model_validate_json('{"isFinal": true}').is_final is True
assert TTSFrame.model_validate_json('{"is_final": true, "context_id": "d"}').context_id == "d"