File size: 11,431 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
"""
Tests for ClientTools custom event loop functionality.
"""

import asyncio
import pytest
import threading
import time
from elevenlabs.conversational_ai.conversation import ClientTools


class TestClientTools:
    """Test suite for ClientTools functionality."""
    
    def test_default_initialization(self):
        """Test that ClientTools can be initialized without parameters (backwards compatibility)."""
        client_tools = ClientTools()
        assert client_tools._custom_loop is None
        assert client_tools._loop is None
        assert not client_tools._running.is_set()
        
    def test_custom_loop_initialization(self):
        """Test that ClientTools can be initialized with a custom event loop."""
        loop = asyncio.new_event_loop()
        try:
            client_tools = ClientTools(loop=loop)
            assert client_tools._custom_loop is loop
            assert client_tools._loop is None
            assert not client_tools._running.is_set()
        finally:
            loop.close()
    
    def test_start_with_default_loop(self):
        """Test starting ClientTools with default behavior (creates own loop)."""
        client_tools = ClientTools()
        
        try:
            client_tools.start()
            assert client_tools._running.is_set()
            assert client_tools._loop is not None
            assert client_tools._thread is not None
            assert client_tools._thread.is_alive()
        finally:
            client_tools.stop()
    
    @pytest.mark.asyncio
    async def test_start_with_custom_loop(self):
        """Test starting ClientTools with a custom event loop."""
        custom_loop = asyncio.get_running_loop()
        client_tools = ClientTools(loop=custom_loop)
        
        try:
            client_tools.start()
            assert client_tools._running.is_set()
            assert client_tools._loop is custom_loop
            assert client_tools._thread is None  # No thread created for custom loop
        finally:
            client_tools.stop()
    
    def test_tool_registration(self):
        """Test that tools can be registered correctly."""
        client_tools = ClientTools()
        
        def sync_tool(params):
            return "sync result"
        
        async def async_tool(params):
            return "async result"
        
        client_tools.register("sync_tool", sync_tool, is_async=False)
        client_tools.register("async_tool", async_tool, is_async=True)
        
        assert "sync_tool" in client_tools.tools
        assert "async_tool" in client_tools.tools
        
        sync_handler, sync_is_async = client_tools.tools["sync_tool"]
        async_handler, async_is_async = client_tools.tools["async_tool"]
        
        assert sync_handler is sync_tool
        assert not sync_is_async
        assert async_handler is async_tool
        assert async_is_async
    
    def test_duplicate_tool_registration(self):
        """Test that registering a tool with the same name raises an error."""
        client_tools = ClientTools()
        
        def tool(params):
            return "result"
        
        client_tools.register("test_tool", tool)
        
        with pytest.raises(ValueError, match="Tool 'test_tool' is already registered"):
            client_tools.register("test_tool", tool)
    
    def test_invalid_handler_registration(self):
        """Test that registering a non-callable handler raises an error."""
        client_tools = ClientTools()
        
        with pytest.raises(ValueError, match="Handler must be callable"):
            client_tools.register("invalid_tool", "not_callable")
    
    @pytest.mark.asyncio
    async def test_sync_tool_execution_with_custom_loop(self):
        """Test executing a sync tool with a custom event loop."""
        custom_loop = asyncio.get_running_loop()
        client_tools = ClientTools(loop=custom_loop)
        
        def sync_tool(params):
            return f"sync result: {params.get('input', 'default')}"
        
        client_tools.register("sync_tool", sync_tool, is_async=False)
        client_tools.start()
        
        try:
            result = await client_tools.handle("sync_tool", {"input": "test"})
            assert result == "sync result: test"
        finally:
            client_tools.stop()
    
    @pytest.mark.asyncio
    async def test_async_tool_execution_with_custom_loop(self):
        """Test executing an async tool with a custom event loop."""
        custom_loop = asyncio.get_running_loop()
        client_tools = ClientTools(loop=custom_loop)
        
        async def async_tool(params):
            await asyncio.sleep(0.01)  # Simulate async work
            return f"async result: {params.get('input', 'default')}"
        
        client_tools.register("async_tool", async_tool, is_async=True)
        client_tools.start()
        
        try:
            result = await client_tools.handle("async_tool", {"input": "test"})
            assert result == "async result: test"
        finally:
            client_tools.stop()
    
    @pytest.mark.asyncio
    async def test_tool_execution_with_callback_custom_loop(self):
        """Test executing a tool via callback mechanism with custom event loop."""
        custom_loop = asyncio.get_running_loop()
        client_tools = ClientTools(loop=custom_loop)
        
        def sync_tool(params):
            return f"callback result: {params.get('data', 'none')}"
        
        client_tools.register("callback_tool", sync_tool, is_async=False)
        client_tools.start()
        
        callback_results = []
        
        def test_callback(response):
            callback_results.append(response)
        
        try:
            client_tools.execute_tool(
                "callback_tool", 
                {"tool_call_id": "test123", "data": "callback_test"}, 
                test_callback
            )
            
            # Wait for callback to be executed
            await asyncio.sleep(0.1)
            
            assert len(callback_results) == 1
            response = callback_results[0]
            assert response["type"] == "client_tool_result"
            assert response["tool_call_id"] == "test123"
            assert "callback result: callback_test" in response["result"]
            assert response["is_error"] is False
        finally:
            client_tools.stop()
    
    def test_sync_tool_execution_with_default_loop(self):
        """Test executing a sync tool with default loop behavior."""
        client_tools = ClientTools()
        
        def sync_tool(params):
            return f"default sync: {params.get('value', 'empty')}"
        
        client_tools.register("sync_tool", sync_tool, is_async=False)
        client_tools.start()
        
        # Use asyncio.run to test from a fresh event loop
        async def test():
            result = await client_tools.handle("sync_tool", {"value": "default_test"})
            return result
        
        try:
            result = asyncio.run(test())
            assert result == "default sync: default_test"
        finally:
            client_tools.stop()
    
    @pytest.mark.asyncio
    async def test_unregistered_tool_error(self):
        """Test that calling an unregistered tool raises an error."""
        custom_loop = asyncio.get_running_loop()
        client_tools = ClientTools(loop=custom_loop)
        client_tools.start()
        
        try:
            with pytest.raises(ValueError, match="Tool 'nonexistent' is not registered"):
                await client_tools.handle("nonexistent", {})
        finally:
            client_tools.stop()
    
    def test_execute_tool_without_start(self):
        """Test that execute_tool raises error when not started."""
        client_tools = ClientTools()
        
        def callback(response):
            pass
        
        with pytest.raises(RuntimeError, match="ClientTools event loop is not running"):
            client_tools.execute_tool("any_tool", {}, callback)
    
    @pytest.mark.asyncio
    async def test_tool_exception_handling(self):
        """Test that tool exceptions are handled correctly."""
        custom_loop = asyncio.get_running_loop()
        client_tools = ClientTools(loop=custom_loop)
        
        def failing_tool(params):
            raise ValueError("Tool failed intentionally")
        
        client_tools.register("failing_tool", failing_tool, is_async=False)
        client_tools.start()
        
        callback_results = []
        
        def error_callback(response):
            callback_results.append(response)
        
        try:
            client_tools.execute_tool(
                "failing_tool",
                {"tool_call_id": "error_test"},
                error_callback
            )
            
            # Wait for callback
            await asyncio.sleep(0.1)
            
            assert len(callback_results) == 1
            response = callback_results[0]
            assert response["type"] == "client_tool_result"
            assert response["tool_call_id"] == "error_test"
            assert response["is_error"] is True
            assert "Tool failed intentionally" in response["result"]
        finally:
            client_tools.stop()
    
    @pytest.mark.asyncio
    async def test_event_loop_isolation(self):
        """Test that custom loop prevents 'different event loop' errors."""
        # This test simulates the scenario mentioned in the GitHub issue
        main_loop = asyncio.get_running_loop()
        
        # Create ClientTools with the current loop
        client_tools = ClientTools(loop=main_loop)
        
        async def loop_aware_tool(params):
            # This tool checks which loop it's running on
            current_loop = asyncio.get_running_loop()
            return f"Running on loop: {id(current_loop)}"
        
        client_tools.register("loop_tool", loop_aware_tool, is_async=True)
        client_tools.start()
        
        try:
            result = await client_tools.handle("loop_tool", {})
            # The result should indicate it's running on the same loop
            expected_loop_id = str(id(main_loop))
            assert expected_loop_id in result
        finally:
            client_tools.stop()
    
    def test_stop_custom_loop_behavior(self):
        """Test that stop() doesn't close custom loops."""
        custom_loop = asyncio.new_event_loop()
        client_tools = ClientTools(loop=custom_loop)
        
        try:
            client_tools.start()
            assert client_tools._running.is_set()
            
            client_tools.stop()
            assert not client_tools._running.is_set()
            
            # Custom loop should still be open and usable
            assert not custom_loop.is_closed()
        finally:
            custom_loop.close()
    
    def test_stop_default_loop_behavior(self):
        """Test that stop() properly cleans up default loops."""
        client_tools = ClientTools()
        
        client_tools.start()
        assert client_tools._running.is_set()
        assert client_tools._thread is not None
        thread = client_tools._thread
        
        client_tools.stop()
        assert not client_tools._running.is_set()
        
        # Wait for thread to finish
        thread.join(timeout=1.0)
        assert not thread.is_alive()