Skip to content

Commit f8b077d

Browse files
ochafikclaude
andcommitted
Add tests for full coverage of tool validation branches
- Add test cases for valid create_message calls that pass validation (covering branches 261->266, 266->272, 269->272 in session.py) - Add test for CreateMessageResult.content_as_list with single content (covering line 1245 else branch in types.py) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent b1d944a commit f8b077d

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

tests/server/test_session.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,29 @@ async def test_create_message_tool_result_validation():
360360
tools=[tool],
361361
)
362362

363+
# Case 5: text-only message with tools (no tool_results) - passes validation
364+
# This covers branch 261->266 (has_tool_results=False) and 266->272
365+
# We use move_on_after since send_request will block waiting for response
366+
with anyio.move_on_after(0.01):
367+
await session.create_message(
368+
messages=[types.SamplingMessage(role="user", content=text)],
369+
max_tokens=100,
370+
tools=[tool],
371+
)
372+
373+
# Case 6: valid matching tool_result/tool_use IDs - passes validation
374+
# This covers branch 269->272 (IDs match, no error raised)
375+
with anyio.move_on_after(0.01):
376+
await session.create_message(
377+
messages=[
378+
types.SamplingMessage(role="user", content=text),
379+
types.SamplingMessage(role="assistant", content=tool_use),
380+
types.SamplingMessage(role="user", content=tool_result),
381+
],
382+
max_tokens=100,
383+
tools=[tool],
384+
)
385+
363386

364387
@pytest.mark.anyio
365388
async def test_other_requests_blocked_before_initialization():

tests/test_types.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,11 @@ async def test_create_message_result_with_tool_use():
253253
assert result.stopReason == "toolUse"
254254
assert result.model == "claude-3"
255255

256+
# Test content_as_list with single content (covers else branch)
257+
content_list = result.content_as_list
258+
assert len(content_list) == 1
259+
assert content_list[0] == result.content
260+
256261

257262
@pytest.mark.anyio
258263
async def test_client_capabilities_with_sampling_tools():

0 commit comments

Comments
 (0)