-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtest.py
More file actions
170 lines (157 loc) · 6.19 KB
/
Copy pathtest.py
File metadata and controls
170 lines (157 loc) · 6.19 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
import json
import os
import time
from urllib.request import Request, urlopen
from urllib.error import HTTPError, URLError
PROXY_URL = os.environ.get("PROXY_URL", "http://127.0.0.1:8000")
def log(msg, status="INFO"):
print(f"[{status}] {msg}")
def test_models_endpoint():
log("Testing GET /v1/models (Dynamic Model Discovery)...")
try:
req = Request(f"{PROXY_URL}/v1/models", headers={"User-Agent": "OpenCode-Tester/1.0"})
with urlopen(req, timeout=10) as resp:
if resp.status == 200:
data = json.loads(resp.read().decode("utf-8"))
models = [m.get("id") for m in data.get("data", [])]
log(f"SUCCESS: Fetched {len(models)} active model(s): {', '.join(models[:3])}...", "OK")
return models
except Exception as e:
log(f"FAILED to fetch models: {e}", "ERROR")
return []
def test_metrics_endpoint():
log("Testing GET /metrics (Observability & Location Data)...")
try:
req = Request(f"{PROXY_URL}/metrics", headers={"User-Agent": "OpenCode-Tester/1.0"})
with urlopen(req, timeout=10) as resp:
if resp.status == 200:
data = json.loads(resp.read().decode("utf-8"))
ip = data.get("verified_public_ip", "Unknown")
loc = data.get("location", {})
flag = loc.get("flag", "")
country = loc.get("country", "")
log(f"SUCCESS: Verified Public IP: {ip} {flag} ({country})", "OK")
return True
except Exception as e:
log(f"FAILED to fetch metrics: {e}", "ERROR")
return False
def test_completion_request(model_name="deepseek-v4-flash-free"):
log(f"Testing POST /v1/chat/completions (Model: {model_name})...")
payload = {
"model": model_name,
"messages": [{"role": "user", "content": "Say hello in 3 words."}],
"stream": False
}
try:
req = Request(
f"{PROXY_URL}/v1/chat/completions",
data=json.dumps(payload).encode("utf-8"),
headers={"Content-Type": "application/json", "Authorization": "Bearer public"},
method="POST"
)
with urlopen(req, timeout=30) as resp:
if resp.status == 200:
data = json.loads(resp.read().decode("utf-8"))
content = data.get("choices", [{}])[0].get("message", {}).get("content", "")
log(f"SUCCESS: Model Response -> '{content.strip()}'", "OK")
return True
except Exception as e:
log(f"FAILED completion request: {e}", "ERROR")
return False
def test_messages_endpoint(model_name="deepseek-v4-flash-free"):
log(f"Testing POST /v1/messages (Anthropic format, Model: {model_name})...")
payload = {
"model": model_name,
"messages": [{"role": "user", "content": "Say hello in 3 words."}],
"stream": False,
"max_tokens": 100,
}
try:
req = Request(
f"{PROXY_URL}/v1/messages",
data=json.dumps(payload).encode("utf-8"),
headers={
"Content-Type": "application/json",
"x-api-key": "public",
"anthropic-version": "2023-06-01",
},
method="POST",
)
with urlopen(req, timeout=30) as resp:
if resp.status == 200:
data = json.loads(resp.read().decode("utf-8"))
content = ""
if "content" in data:
blocks = data["content"]
if blocks and isinstance(blocks, list):
content = blocks[0].get("text", "")
log(f"SUCCESS: Anthropic Response -> '{content.strip()}'", "OK")
return True
except Exception as e:
log(f"FAILED messages request: {e}", "ERROR")
return False
def test_responses_endpoint(model_name="deepseek-v4-flash-free"):
log(f"Testing POST /v1/responses (Responses API, Model: {model_name})...")
payload = {
"model": model_name,
"input": "Say hello in 3 words.",
}
try:
req = Request(
f"{PROXY_URL}/v1/responses",
data=json.dumps(payload).encode("utf-8"),
headers={
"Content-Type": "application/json",
"Authorization": "Bearer public",
},
method="POST",
)
with urlopen(req, timeout=30) as resp:
if resp.status == 200:
data = json.loads(resp.read().decode("utf-8"))
log(f"SUCCESS: Responses API got status 200", "OK")
return True
except Exception as e:
log(f"FAILED responses request: {e}", "ERROR")
return False
def test_streaming_completion(model_name="deepseek-v4-flash-free"):
log(f"Testing POST /v1/chat/completions with stream=True (Model: {model_name})...")
payload = {
"model": model_name,
"messages": [{"role": "user", "content": "Count from 1 to 5."}],
"stream": True
}
try:
req = Request(
f"{PROXY_URL}/v1/chat/completions",
data=json.dumps(payload).encode("utf-8"),
headers={"Content-Type": "application/json", "Authorization": "Bearer public"},
method="POST"
)
with urlopen(req, timeout=30) as resp:
if resp.status == 200:
chunks = 0
for line in resp:
if line.strip():
chunks += 1
log(f"SUCCESS: Stream completed cleanly with {chunks} stream data lines.", "OK")
return True
except Exception as e:
log(f"FAILED streaming completion request: {e}", "ERROR")
return False
def main():
print("=" * 60)
print(" OpenCode IP Rotator & Proxy Integration Test (test.py)")
print("=" * 60)
models = test_models_endpoint()
metrics_ok = test_metrics_endpoint()
if models:
test_completion_request(models[0])
test_streaming_completion(models[0])
test_messages_endpoint(models[0])
test_responses_endpoint(models[0])
print("=" * 60)
print(" All Diagnostic Tests Completed!")
print("=" * 60)
if __name__ == "__main__":
main()