11from unittest .mock import MagicMock , patch
2- from elevenlabs .conversational_ai .conversation import Conversation , AudioInterface
2+ from elevenlabs .conversational_ai .conversation import Conversation , AudioInterface , ConversationConfig
33import json
44import time
55
@@ -81,6 +81,7 @@ def test_conversation_basic_flow():
8181 "type" : "conversation_initiation_client_data" ,
8282 "custom_llm_extra_body" : {},
8383 "conversation_config_override" : {},
84+ "dynamic_variables" : {},
8485 }
8586 mock_ws .send .assert_any_call (json .dumps (expected_init_message ))
8687 agent_response_callback .assert_called_once_with ("Hello there!" )
@@ -116,3 +117,48 @@ def test_conversation_with_auth():
116117
117118 # Assertions
118119 mock_client .conversational_ai .get_signed_url .assert_called_once_with (agent_id = TEST_AGENT_ID )
120+
121+ def test_conversation_with_dynamic_variables ():
122+ # Mock setup
123+ mock_ws = create_mock_websocket ()
124+ mock_client = MagicMock ()
125+ agent_response_callback = MagicMock ()
126+
127+ dynamic_variables = {"name" : "angelo" }
128+ config = ConversationConfig (dynamic_variables = dynamic_variables )
129+
130+ # Setup the conversation
131+ conversation = Conversation (
132+ client = mock_client ,
133+ agent_id = TEST_AGENT_ID ,
134+ requires_auth = False ,
135+ audio_interface = MockAudioInterface (),
136+ callback_agent_response = agent_response_callback ,
137+ )
138+
139+ # Run the test
140+ with patch ("elevenlabs.conversational_ai.conversation.connect" ) as mock_connect :
141+ mock_connect .return_value .__enter__ .return_value = mock_ws
142+ conversation .start_session ()
143+
144+ # Add a wait for the callback to be called
145+ timeout = 5 # 5 seconds timeout
146+ start_time = time .time ()
147+ while not agent_response_callback .called and time .time () - start_time < timeout :
148+ time .sleep (0.1 )
149+
150+ conversation .end_session ()
151+ conversation .wait_for_session_end ()
152+
153+ # Assertions
154+ expected_init_message = {
155+ "type" : "conversation_initiation_client_data" ,
156+ "custom_llm_extra_body" : {},
157+ "conversation_config_override" : {},
158+ "dynamic_variables" : {
159+ "name" : "angelo"
160+ },
161+ }
162+ mock_ws .send .assert_any_call (json .dumps (expected_init_message ))
163+ agent_response_callback .assert_called_once_with ("Hello there!" )
164+ assert conversation ._conversation_id == TEST_CONVERSATION_ID
0 commit comments