Skip to content

Commit 97d77ed

Browse files
committed
chore: add basic mock
1 parent 95bd48e commit 97d77ed

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ dist/
33
__pycache__/
44
poetry.toml
55
.ruff_cache/
6+
.DS_Store

tests/e2e_test_convai.py

Whitespace-only changes.

tests/test_convai.py

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
from unittest.mock import MagicMock, patch
2+
from elevenlabs.conversational_ai.conversation import Conversation, AudioInterface
3+
import json
4+
import time
5+
6+
7+
class MockAudioInterface(AudioInterface):
8+
def start(self, input_callback):
9+
print("Audio interface started")
10+
self.input_callback = input_callback
11+
12+
def stop(self):
13+
print("Audio interface stopped")
14+
15+
def output(self, audio):
16+
print(f"Would play audio of length: {len(audio)} bytes")
17+
18+
def interrupt(self):
19+
print("Audio interrupted")
20+
21+
22+
# Add test constants and helpers at module level
23+
TEST_CONVERSATION_ID = "test123"
24+
TEST_AGENT_ID = "test_agent"
25+
26+
27+
def create_mock_websocket(messages=None):
28+
"""Helper to create a mock websocket with predefined responses"""
29+
mock_ws = MagicMock()
30+
31+
if messages is None:
32+
messages = [
33+
{
34+
"type": "conversation_initiation_metadata",
35+
"conversation_initiation_metadata_event": {"conversation_id": TEST_CONVERSATION_ID},
36+
},
37+
{"type": "agent_response", "agent_response_event": {"agent_response": "Hello there!"}},
38+
]
39+
40+
def response_generator():
41+
for msg in messages:
42+
yield json.dumps(msg)
43+
while True:
44+
yield '{"type": "keep_alive"}'
45+
46+
mock_ws.recv = MagicMock(side_effect=response_generator())
47+
return mock_ws
48+
49+
50+
def test_conversation_basic_flow():
51+
# Mock setup
52+
mock_ws = create_mock_websocket()
53+
mock_client = MagicMock()
54+
agent_response_callback = MagicMock()
55+
56+
# Setup the conversation
57+
conversation = Conversation(
58+
client=mock_client,
59+
agent_id=TEST_AGENT_ID,
60+
requires_auth=False,
61+
audio_interface=MockAudioInterface(),
62+
callback_agent_response=agent_response_callback,
63+
)
64+
65+
# Run the test
66+
with patch("elevenlabs.conversational_ai.conversation.connect") as mock_connect:
67+
mock_connect.return_value.__enter__.return_value = mock_ws
68+
conversation.start_session()
69+
70+
# Add a wait for the callback to be called
71+
timeout = 5 # 5 seconds timeout
72+
start_time = time.time()
73+
while not agent_response_callback.called and time.time() - start_time < timeout:
74+
time.sleep(0.1)
75+
76+
conversation.end_session()
77+
conversation.wait_for_session_end()
78+
79+
# Assertions
80+
expected_init_message = {
81+
"type": "conversation_initiation_client_data",
82+
"custom_llm_extra_body": {},
83+
"conversation_config_override": {},
84+
}
85+
mock_ws.send.assert_any_call(json.dumps(expected_init_message))
86+
agent_response_callback.assert_called_once_with("Hello there!")
87+
assert conversation._conversation_id == TEST_CONVERSATION_ID
88+
89+
90+
def test_conversation_with_auth():
91+
# Mock setup
92+
mock_client = MagicMock()
93+
mock_client.conversational_ai.get_signed_url.return_value.signed_url = "wss://signed.url"
94+
mock_ws = create_mock_websocket(
95+
[
96+
{
97+
"type": "conversation_initiation_metadata",
98+
"conversation_initiation_metadata_event": {"conversation_id": TEST_CONVERSATION_ID},
99+
}
100+
]
101+
)
102+
103+
conversation = Conversation(
104+
client=mock_client,
105+
agent_id=TEST_AGENT_ID,
106+
requires_auth=True,
107+
audio_interface=MockAudioInterface(),
108+
)
109+
110+
# Run the test
111+
with patch("elevenlabs.conversational_ai.conversation.connect") as mock_connect:
112+
mock_connect.return_value.__enter__.return_value = mock_ws
113+
conversation.start_session()
114+
conversation.end_session()
115+
conversation.wait_for_session_end()
116+
117+
# Assertions
118+
mock_client.conversational_ai.get_signed_url.assert_called_once_with(agent_id=TEST_AGENT_ID)

0 commit comments

Comments
 (0)