1+ #!/usr/bin/env python3
2+ """E2E test based on quickstart examples."""
3+
4+ import anyio
5+
6+ from claude_code_sdk import (
7+ AssistantMessage ,
8+ ClaudeCodeOptions ,
9+ ResultMessage ,
10+ TextBlock ,
11+ query ,
12+ )
13+
14+
15+ async def test_basic_example ():
16+ """Test basic example - simple question."""
17+ print ("Testing basic example..." )
18+
19+ found_response = False
20+ async for message in query (prompt = "What is 2 + 2?" ):
21+ if isinstance (message , AssistantMessage ):
22+ for block in message .content :
23+ if isinstance (block , TextBlock ):
24+ print (f" Response: { block .text } " )
25+ found_response = True
26+
27+ if not found_response :
28+ raise Exception ("No response received for basic example" )
29+ print ("✅ Basic example test passed" )
30+
31+
32+ async def test_with_options_example ():
33+ """Test example with custom options."""
34+ print ("\n Testing with options example..." )
35+
36+ options = ClaudeCodeOptions (
37+ system_prompt = "You are a helpful assistant that explains things simply." ,
38+ max_turns = 1 ,
39+ )
40+
41+ found_response = False
42+ async for message in query (
43+ prompt = "Explain what Python is in one sentence." , options = options
44+ ):
45+ if isinstance (message , AssistantMessage ):
46+ for block in message .content :
47+ if isinstance (block , TextBlock ):
48+ print (f" Response: { block .text } " )
49+ found_response = True
50+
51+ if not found_response :
52+ raise Exception ("No response received for options example" )
53+ print ("✅ Options example test passed" )
54+
55+
56+ async def test_with_tools_example ():
57+ """Test example using tools."""
58+ print ("\n Testing with tools example..." )
59+
60+ options = ClaudeCodeOptions (
61+ allowed_tools = ["Read" , "Write" ],
62+ system_prompt = "You are a helpful file assistant." ,
63+ )
64+
65+ found_response = False
66+ found_cost = False
67+ async for message in query (
68+ prompt = "Create a file called hello.txt with 'Hello, World!' in it" ,
69+ options = options ,
70+ ):
71+ if isinstance (message , AssistantMessage ):
72+ for block in message .content :
73+ if isinstance (block , TextBlock ):
74+ print (f" Response: { block .text [:100 ]} ..." )
75+ found_response = True
76+ elif isinstance (message , ResultMessage ) and message .total_cost_usd > 0 :
77+ print (f" Cost: ${ message .total_cost_usd :.4f} " )
78+ found_cost = True
79+
80+ if not found_response :
81+ raise Exception ("No response received for tools example" )
82+ if not found_cost :
83+ print (" Note: Cost information not available (might be expected)" )
84+ print ("✅ Tools example test passed" )
85+
86+
87+ async def main ():
88+ """Run all quickstart tests."""
89+ print ("=" * 50 )
90+ print ("Running Quickstart E2E Tests" )
91+ print ("=" * 50 )
92+
93+ try :
94+ await test_basic_example ()
95+ await test_with_options_example ()
96+ await test_with_tools_example ()
97+
98+ print ("\n " + "=" * 50 )
99+ print ("✅ All quickstart tests passed!" )
100+ print ("=" * 50 )
101+ except Exception as e :
102+ print (f"\n ❌ Test failed: { e } " )
103+ raise
104+
105+
106+ if __name__ == "__main__" :
107+ anyio .run (main )
0 commit comments