File size: 4,589 Bytes
9410704
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
"""Tests for SpeechEngineServer — mirrors SpeechEngineServer.test.ts."""

import asyncio
import json
import typing

import pytest
import websockets

from elevenlabs.speech_engine import SpeechEngineServer


# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------


async def _connect_and_send(
    port: int,
    messages: typing.List[typing.Dict[str, typing.Any]],
) -> typing.List[typing.Dict[str, typing.Any]]:
    """Open a client WS, send *messages*, and collect the first response."""
    uri = "ws://127.0.0.1:{}".format(port)
    received = []  # type: typing.List[typing.Dict[str, typing.Any]]
    async with websockets.connect(uri) as ws:  # type: ignore[attr-defined]
        for msg in messages:
            await ws.send(json.dumps(msg))
        # Give the server a moment to process and respond.
        try:
            while True:
                raw = await asyncio.wait_for(ws.recv(), timeout=0.2)
                received.append(json.loads(raw))
        except (asyncio.TimeoutError, websockets.exceptions.ConnectionClosed):
            pass
    return received


# ---------------------------------------------------------------------------
# handleConnection
# ---------------------------------------------------------------------------


@pytest.mark.asyncio
async def test_handle_connection_wraps_ws_and_calls_on_init() -> None:
    init_ids = []  # type: typing.List[str]

    async def on_init(conversation_id: str, session: typing.Any) -> None:
        init_ids.append(conversation_id)

    server = SpeechEngineServer(port=0, on_init=on_init)

    # Use port 0 to get an ephemeral port; start via websockets directly.
    started = asyncio.Event()
    actual_port = 0

    async def _handler(ws: typing.Any, *_args: typing.Any) -> None:
        session = server.handle_connection(ws)
        await session.run()

    ws_server = await websockets.serve(_handler, "127.0.0.1", 0)  # type: ignore[attr-defined]
    for sock in ws_server.sockets:
        actual_port = sock.getsockname()[1]
        break

    try:
        uri = "ws://127.0.0.1:{}".format(actual_port)
        async with websockets.connect(uri) as client:  # type: ignore[attr-defined]
            await client.send(
                json.dumps({"type": "init", "conversation_id": "conv_1"})
            )
            await asyncio.sleep(0.1)
    finally:
        ws_server.close()
        await ws_server.wait_closed()

    assert init_ids == ["conv_1"]


# ---------------------------------------------------------------------------
# Session responses are received by the client
# ---------------------------------------------------------------------------


@pytest.mark.asyncio
async def test_session_responses_received_by_client() -> None:
    async def on_transcript(
        transcript: typing.Any, session: typing.Any
    ) -> None:
        last = transcript[-1]
        await session.send_response("echo: {}".format(last.content))

    server = SpeechEngineServer(port=0, on_transcript=on_transcript)

    async def _handler(ws: typing.Any, *_args: typing.Any) -> None:
        session = server.handle_connection(ws)
        await session.run()

    ws_server = await websockets.serve(_handler, "127.0.0.1", 0)  # type: ignore[attr-defined]
    actual_port = 0
    for sock in ws_server.sockets:
        actual_port = sock.getsockname()[1]
        break

    try:
        uri = "ws://127.0.0.1:{}".format(actual_port)
        async with websockets.connect(uri) as client:  # type: ignore[attr-defined]
            await client.send(
                json.dumps(
                    {
                        "type": "user_transcript",
                        "user_transcript": [{"role": "user", "content": "ping"}],
                        "event_id": 1,
                    }
                )
            )
            raw = await asyncio.wait_for(client.recv(), timeout=1.0)
            response = json.loads(raw)
    finally:
        ws_server.close()
        await ws_server.wait_closed()

    assert response == {
        "type": "agent_response",
        "content": "echo: ping",
        "event_id": 1,
        "is_final": False,
    }


# ---------------------------------------------------------------------------
# Lifecycle
# ---------------------------------------------------------------------------


@pytest.mark.asyncio
async def test_stop_resolves_when_no_server_running() -> None:
    server = SpeechEngineServer()
    await server.stop()  # Should not raise.