forked from anthropics/claude-agent-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_client.py
More file actions
366 lines (295 loc) · 14 KB
/
Copy pathtest_client.py
File metadata and controls
366 lines (295 loc) · 14 KB
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
"""Tests for Claude SDK client functionality."""
import os
from unittest.mock import AsyncMock, Mock, patch
import anyio
import pytest
from claude_agent_sdk import AssistantMessage, ClaudeAgentOptions, query
from claude_agent_sdk.types import TextBlock
class TestQueryFunction:
"""Test the main query function."""
def test_query_single_prompt(self):
"""Test query with a single prompt."""
async def _test():
with patch(
"claude_agent_sdk._internal.client.InternalClient.process_query"
) as mock_process:
# Mock the async generator
async def mock_generator():
yield AssistantMessage(
content=[TextBlock(text="4")], model="claude-opus-4-1-20250805"
)
mock_process.return_value = mock_generator()
messages = []
async for msg in query(prompt="What is 2+2?"):
messages.append(msg)
assert len(messages) == 1
assert isinstance(messages[0], AssistantMessage)
assert messages[0].content[0].text == "4"
anyio.run(_test)
def test_query_with_options(self):
"""Test query with various options."""
async def _test():
with patch(
"claude_agent_sdk._internal.client.InternalClient.process_query"
) as mock_process:
async def mock_generator():
yield AssistantMessage(
content=[TextBlock(text="Hello!")],
model="claude-opus-4-1-20250805",
)
mock_process.return_value = mock_generator()
options = ClaudeAgentOptions(
allowed_tools=["Read", "Write"],
system_prompt="You are helpful",
permission_mode="acceptEdits",
max_turns=5,
)
messages = []
async for msg in query(prompt="Hi", options=options):
messages.append(msg)
# Verify process_query was called with correct prompt and options
mock_process.assert_called_once()
call_args = mock_process.call_args
assert call_args[1]["prompt"] == "Hi"
assert call_args[1]["options"] == options
anyio.run(_test)
def test_query_with_cwd(self):
"""Test query with custom working directory."""
async def _test():
with (
patch(
"claude_agent_sdk._internal.client.SubprocessCLITransport"
) as mock_transport_class,
patch(
"claude_agent_sdk._internal.query.Query.initialize",
new_callable=AsyncMock,
),
):
mock_transport = AsyncMock()
mock_transport_class.return_value = mock_transport
# Mock the message stream
async def mock_receive():
yield {
"type": "assistant",
"message": {
"role": "assistant",
"content": [{"type": "text", "text": "Done"}],
"model": "claude-opus-4-1-20250805",
},
}
yield {
"type": "result",
"subtype": "success",
"duration_ms": 1000,
"duration_api_ms": 800,
"is_error": False,
"num_turns": 1,
"session_id": "test-session",
"total_cost_usd": 0.001,
}
mock_transport.read_messages = mock_receive
mock_transport.connect = AsyncMock()
mock_transport.close = AsyncMock()
mock_transport.end_input = AsyncMock()
mock_transport.write = AsyncMock()
mock_transport.is_ready = Mock(return_value=True)
options = ClaudeAgentOptions(cwd="/custom/path")
messages = []
async for msg in query(prompt="test", options=options):
messages.append(msg)
# Verify transport was created with correct parameters
mock_transport_class.assert_called_once()
call_kwargs = mock_transport_class.call_args.kwargs
assert call_kwargs["prompt"] == "test"
assert call_kwargs["options"].cwd == "/custom/path"
anyio.run(_test)
def _run_query_with_mocked_internals(self, env_patch, expected_timeout):
"""Helper: run query() with mocked transport/Query and verify initialize_timeout."""
async def _test():
with (
patch(
"claude_agent_sdk._internal.client.SubprocessCLITransport"
) as mock_transport_class,
patch("claude_agent_sdk._internal.client.Query") as mock_query_class,
patch.dict(os.environ, env_patch, clear=False),
):
mock_transport = AsyncMock()
mock_transport_class.return_value = mock_transport
mock_transport.connect = AsyncMock()
mock_transport.close = AsyncMock()
mock_transport.end_input = AsyncMock()
mock_transport.write = AsyncMock()
mock_transport.is_ready = Mock(return_value=True)
mock_query = AsyncMock()
mock_query_class.return_value = mock_query
mock_query.start = AsyncMock()
mock_query.initialize = AsyncMock()
mock_query.close = AsyncMock()
mock_query.close_receive_stream = Mock()
mock_query._tg = None
def _consume_coro(coro):
coro.close()
return Mock()
mock_query.spawn_task = Mock(side_effect=_consume_coro)
async def mock_receive():
yield {
"type": "result",
"subtype": "success",
"duration_ms": 100,
"duration_api_ms": 80,
"is_error": False,
"num_turns": 1,
"session_id": "test",
}
mock_query.receive_messages = mock_receive
async for _ in query(prompt="test", options=ClaudeAgentOptions()):
pass
call_kwargs = mock_query_class.call_args.kwargs
assert call_kwargs["initialize_timeout"] == expected_timeout
anyio.run(_test)
def test_query_passes_initialize_timeout_from_env(self):
"""Test that query() reads CLAUDE_CODE_STREAM_CLOSE_TIMEOUT and passes it to Query."""
self._run_query_with_mocked_internals(
env_patch={"CLAUDE_CODE_STREAM_CLOSE_TIMEOUT": "120000"},
expected_timeout=120.0,
)
def test_query_uses_default_initialize_timeout(self):
"""Test that query() defaults to 60s initialize timeout when env var is not set."""
# Ensure env var is absent for this test
with patch.dict(os.environ, {}, clear=False):
os.environ.pop("CLAUDE_CODE_STREAM_CLOSE_TIMEOUT", None)
self._run_query_with_mocked_internals(
env_patch={},
expected_timeout=60.0,
)
def test_string_prompt_spawns_wait_for_result_as_task(self):
"""Test that string prompts spawn wait_for_result_and_end_input as a background
task instead of awaiting it inline, preventing deadlock when the message
buffer fills up (e.g. >50 tool calls with hooks)."""
async def _test():
with (
patch(
"claude_agent_sdk._internal.client.SubprocessCLITransport"
) as mock_transport_class,
patch("claude_agent_sdk._internal.client.Query") as mock_query_class,
):
mock_transport = AsyncMock()
mock_transport_class.return_value = mock_transport
mock_transport.connect = AsyncMock()
mock_transport.close = AsyncMock()
mock_transport.end_input = AsyncMock()
mock_transport.write = AsyncMock()
mock_transport.is_ready = Mock(return_value=True)
mock_query = AsyncMock()
mock_query_class.return_value = mock_query
mock_query.start = AsyncMock()
mock_query.initialize = AsyncMock()
mock_query.close = AsyncMock()
mock_query.close_receive_stream = Mock()
mock_query._tg = None
def _consume_coro(coro):
coro.close()
return Mock()
mock_query.spawn_task = Mock(side_effect=_consume_coro)
async def mock_receive():
yield {
"type": "result",
"subtype": "success",
"duration_ms": 100,
"duration_api_ms": 80,
"is_error": False,
"num_turns": 1,
"session_id": "test",
}
mock_query.receive_messages = mock_receive
async for _ in query(prompt="test", options=ClaudeAgentOptions()):
pass
mock_query.spawn_task.assert_called_once()
assert not mock_query.wait_for_result_and_end_input.await_args_list, (
"wait_for_result_and_end_input should be spawned as a task, "
"not awaited directly"
)
anyio.run(_test)
class TestClaudeSDKClientTrioBackend:
"""Regression test: ClaudeSDKClient must work under trio.
``Query.start``/``spawn_task`` must not call ``asyncio.get_running_loop()``
(raises ``RuntimeError: no running event loop`` under trio). This test
drives connect()/disconnect() end-to-end on the trio backend with a mock
transport that uses only anyio primitives.
"""
def test_client_connect_under_trio(self):
import json
from claude_agent_sdk import ClaudeSDKClient
def _make_trio_safe_transport():
"""Mock transport using anyio.sleep so it runs under trio."""
mock_transport = AsyncMock()
mock_transport.connect = AsyncMock()
mock_transport.close = AsyncMock()
mock_transport.end_input = AsyncMock()
mock_transport.is_ready = Mock(return_value=True)
written: list[str] = []
async def mock_write(data):
written.append(data)
mock_transport.write = AsyncMock(side_effect=mock_write)
async def read_messages():
# Respond to the initialize control_request so connect()
# doesn't block on the 60s timeout.
for _ in range(200):
for msg_str in written:
try:
msg = json.loads(msg_str.strip())
except (json.JSONDecodeError, AttributeError):
continue
if (
msg.get("type") == "control_request"
and msg.get("request", {}).get("subtype") == "initialize"
):
yield {
"type": "control_response",
"response": {
"request_id": msg.get("request_id"),
"subtype": "success",
"response": {},
},
}
return
await anyio.sleep(0.01)
mock_transport.read_messages = read_messages
return mock_transport
async def _test():
mock_transport = _make_trio_safe_transport()
async with ClaudeSDKClient(transport=mock_transport) as client:
assert client._transport is mock_transport
mock_transport.connect.assert_called_once()
mock_transport.close.assert_called_once()
anyio.run(_test, backend="trio")
class TestClaudeSDKClientResourceCleanup:
"""Regression for #859: disconnect() must close the receive stream.
``Query.close()`` deliberately leaves ``_message_receive`` open so a
concurrently-draining consumer can finish (see
``test_buffered_messages_drain_after_close_*``). The owning consumer
is responsible for closing it once done; ``disconnect()`` is that
consumer for ``ClaudeSDKClient``. Before the fix, ``__aexit__`` left
the stream open and anyio's ``__del__`` emitted ``ResourceWarning:
Unclosed <MemoryObjectReceiveStream>`` under PYTHONDEVMODE.
"""
def test_disconnect_closes_receive_stream(self):
from anyio import ClosedResourceError
from claude_agent_sdk import ClaudeSDKClient
from claude_agent_sdk._internal.query import Query
async def _test():
mock_transport = AsyncMock()
mock_transport.is_ready = Mock(return_value=True)
async def read_messages():
return
yield
mock_transport.read_messages = read_messages
client = ClaudeSDKClient(transport=mock_transport)
client._transport = mock_transport
client._query = Query(transport=mock_transport, is_streaming_mode=True)
await client._query.start()
receive_stream = client._query._message_receive
await client.disconnect()
with pytest.raises(ClosedResourceError):
receive_stream.receive_nowait()
anyio.run(_test)