forked from RichardAtCT/claude-code-openai-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_basic.py
More file actions
executable file
·187 lines (160 loc) · 5.66 KB
/
test_basic.py
File metadata and controls
executable file
·187 lines (160 loc) · 5.66 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
#!/usr/bin/env python3
"""
Basic test to verify the Claude Code OpenAI wrapper works.
Run this after starting the server to ensure everything is set up correctly.
"""
import sys
import os
import requests
from openai import OpenAI
def get_api_key():
"""Get the appropriate API key for testing."""
# Check if user provided API key via environment
if os.getenv("TEST_API_KEY"):
return os.getenv("TEST_API_KEY")
# Check server auth status
try:
response = requests.get("http://localhost:8000/v1/auth/status")
if response.status_code == 200:
auth_data = response.json()
server_info = auth_data.get("server_info", {})
if not server_info.get("api_key_required", False):
# No auth required, use a dummy key
return "no-auth-required"
else:
# Auth required but no key provided
print("⚠️ Server requires API key but none provided.")
print(" Set TEST_API_KEY environment variable with your server's API key")
print(" Example: TEST_API_KEY=your-server-key python test_basic.py")
return None
except Exception as e:
print(f"⚠️ Could not check server auth status: {e}")
print(" Assuming no authentication required")
return "fallback-dummy-key"
def test_health_check():
"""Test the health endpoint."""
print("Testing health check...")
try:
response = requests.get("http://localhost:8000/health")
if response.status_code == 200:
print("✓ Health check passed")
return True
else:
print(f"✗ Health check failed: {response.status_code}")
return False
except Exception as e:
print(f"✗ Cannot connect to server: {e}")
return False
def test_models_endpoint():
"""Test the models endpoint."""
print("\nTesting models endpoint...")
try:
response = requests.get("http://localhost:8000/v1/models")
if response.status_code == 200:
data = response.json()
print(f"✓ Models endpoint works. Found {len(data['data'])} models")
return True
else:
print(f"✗ Models endpoint failed: {response.status_code}")
return False
except Exception as e:
print(f"✗ Models endpoint error: {e}")
return False
def test_openai_sdk():
"""Test with OpenAI SDK."""
print("\nTesting OpenAI SDK integration...")
api_key = get_api_key()
if api_key is None:
print("✗ Cannot run test - API key required but not provided")
return False
try:
client = OpenAI(
base_url="http://localhost:8000/v1",
api_key=api_key
)
# Simple test
response = client.chat.completions.create(
model="claude-3-5-sonnet-20241022",
messages=[
{"role": "user", "content": "Say 'Hello, World!' and nothing else."}
],
max_tokens=50
)
content = response.choices[0].message.content
print(f"✓ OpenAI SDK test passed")
print(f" Response: {content}")
return True
except Exception as e:
print(f"✗ OpenAI SDK test failed: {e}")
return False
def test_streaming():
"""Test streaming functionality."""
print("\nTesting streaming...")
api_key = get_api_key()
if api_key is None:
print("✗ Cannot run test - API key required but not provided")
return False
try:
client = OpenAI(
base_url="http://localhost:8000/v1",
api_key=api_key
)
stream = client.chat.completions.create(
model="claude-3-5-sonnet-20241022",
messages=[
{"role": "user", "content": "Count from 1 to 3."}
],
stream=True
)
chunks_received = 0
content = ""
for chunk in stream:
chunks_received += 1
if chunk.choices[0].delta.content:
content += chunk.choices[0].delta.content
if chunks_received > 0:
print(f"✓ Streaming test passed ({chunks_received} chunks)")
print(f" Response: {content[:50]}...")
return True
else:
print("✗ No streaming chunks received")
return False
except Exception as e:
print(f"✗ Streaming test failed: {e}")
return False
def main():
"""Run all tests."""
print("Claude Code OpenAI Wrapper - Basic Tests")
print("="*50)
print("Make sure the server is running: python main.py")
print("="*50)
# Show API key status
api_key = get_api_key()
if api_key:
if api_key == "no-auth-required":
print("🔓 Server authentication: Not required")
else:
print("🔑 Server authentication: Required (using provided key)")
else:
print("❌ Server authentication: Required but no key available")
print("="*50)
tests = [
test_health_check,
test_models_endpoint,
test_openai_sdk,
test_streaming
]
passed = 0
for test in tests:
if test():
passed += 1
print("\n" + "="*50)
print(f"Tests completed: {passed}/{len(tests)} passed")
if passed == len(tests):
print("✓ All tests passed! The wrapper is working correctly.")
return 0
else:
print("✗ Some tests failed. Check the server logs for details.")
return 1
if __name__ == "__main__":
sys.exit(main())