-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_backend.py
More file actions
87 lines (78 loc) · 2.59 KB
/
test_backend.py
File metadata and controls
87 lines (78 loc) · 2.59 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
#!/usr/bin/env python3
"""
Quick smoke-test for the Seva backend.
Run: python test_backend.py [BASE_URL]
Defaults to http://localhost:8000 if no URL provided.
"""
import sys
import json
import urllib.request
import urllib.error
def main():
base = sys.argv[1] if len(sys.argv) > 1 else "http://localhost:8000"
base = base.rstrip("/")
passed = 0
failed = 0
# --- Test 1: Health check ---
print(f"\n[1/3] GET {base}/health")
try:
req = urllib.request.Request(f"{base}/health")
with urllib.request.urlopen(req, timeout=10) as resp:
data = json.loads(resp.read())
assert data.get("status") == "ok", f"Unexpected body: {data}"
print(f" ✅ status=ok environment={data.get('environment')}")
passed += 1
except Exception as e:
print(f" ❌ {e}")
failed += 1
# --- Test 2: Chat endpoint (streaming) ---
print(f"\n[2/3] POST {base}/chat (streaming response)")
try:
payload = json.dumps({
"messages": [{"role": "user", "content": "Hi, what can you help with?"}]
}).encode()
req = urllib.request.Request(
f"{base}/chat",
data=payload,
headers={"Content-Type": "application/json"},
)
with urllib.request.urlopen(req, timeout=30) as resp:
body = resp.read().decode()
assert len(body) > 0, "Empty response"
preview = body[:120].replace("\n", " ")
print(f" ✅ Got {len(body)} chars: \"{preview}...\"")
passed += 1
except Exception as e:
print(f" ❌ {e}")
failed += 1
# --- Test 3: Invalid request ---
print(f"\n[3/3] POST {base}/chat (empty body → expect 422)")
try:
req = urllib.request.Request(
f"{base}/chat",
data=b"{}",
headers={"Content-Type": "application/json"},
)
urllib.request.urlopen(req, timeout=10)
print(" ❌ Expected 422 but got 200")
failed += 1
except urllib.error.HTTPError as e:
if e.code == 422:
print(f" ✅ Got 422 as expected")
passed += 1
else:
print(f" ❌ Expected 422, got {e.code}")
failed += 1
except Exception as e:
print(f" ❌ {e}")
failed += 1
# --- Summary ---
print(f"\n{'='*40}")
print(f"Results: {passed} passed, {failed} failed")
if failed:
print("Some tests failed – check the output above.")
sys.exit(1)
else:
print("All tests passed! 🎉")
if __name__ == "__main__":
main()