-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvalidate-system.py
More file actions
executable file
·358 lines (309 loc) · 12.2 KB
/
Copy pathvalidate-system.py
File metadata and controls
executable file
·358 lines (309 loc) · 12.2 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/usr/bin/env python3
import requests
import json
import time
import sys
from typing import Dict, Any, List
from dataclasses import dataclass
@dataclass
class TestResult:
name: str
passed: bool
message: str
response: Any = None
class SystemValidator:
def __init__(self):
self.base_url = "http://localhost"
self.router_url = f"{self.base_url}:8000"
self.math_url = f"{self.base_url}:8001"
self.weather_url = f"{self.base_url}:8002"
self.qna_url = f"{self.base_url}:8003"
self.session = requests.Session()
self.session.timeout = 10
def wait_for_services(self, max_wait=30):
print("⏳ Waiting for services to be ready...")
services = [
("Router", self.router_url),
("Math Agent", self.math_url),
("Weather Agent", self.weather_url),
("QnA Agent", self.qna_url)
]
for name, url in services:
ready = False
for _ in range(max_wait):
try:
response = self.session.get(f"{url}/health")
if response.status_code == 200:
ready = True
break
except:
pass
time.sleep(1)
if not ready:
print(f"❌ {name} service not ready at {url}")
return False
return True
def run_all_tests(self) -> List[TestResult]:
results = []
results.extend(self.test_health_endpoints())
results.extend(self.test_direct_agents())
results.extend(self.test_router_routing())
results.extend(self.test_edge_cases())
return results
def test_health_endpoints(self) -> List[TestResult]:
results = []
print("\n🏥 Testing Health Endpoints...")
services = [
("Router", self.router_url),
("Math Agent", self.math_url),
("Weather Agent", self.weather_url),
("QnA Agent", self.qna_url)
]
for name, url in services:
try:
response = self.session.get(f"{url}/health")
if response.status_code == 200:
data = response.json()
results.append(TestResult(
name=f"{name} Health Check",
passed=True,
message=f"Service healthy: {data.get('status', 'unknown')}",
response=data
))
else:
results.append(TestResult(
name=f"{name} Health Check",
passed=False,
message=f"HTTP {response.status_code}: {response.text}"
))
except Exception as e:
results.append(TestResult(
name=f"{name} Health Check",
passed=False,
message=f"Connection failed: {str(e)}"
))
return results
def test_direct_agents(self) -> List[TestResult]:
results = []
print("\n🎯 Testing Direct Agent APIs...")
# Test Math Agent
try:
response = self.session.post(f"{self.math_url}/solve", json={"question": "2 + 2"})
if response.status_code == 200:
data = response.json()
results.append(TestResult(
name="Math Agent Direct API",
passed=data.get("result") == 4.0,
message=f"Math calculation: {data.get('expression')} = {data.get('result')}",
response=data
))
else:
results.append(TestResult(
name="Math Agent Direct API",
passed=False,
message=f"HTTP {response.status_code}: {response.text}"
))
except Exception as e:
results.append(TestResult(
name="Math Agent Direct API",
passed=False,
message=f"Error: {str(e)}"
))
# Test Weather Agent
try:
response = self.session.post(f"{self.weather_url}/get", json={"question": "weather in Paris"})
if response.status_code == 200:
data = response.json()
results.append(TestResult(
name="Weather Agent Direct API",
passed="weather" in data and "city" in data,
message=f"Weather for {data.get('city')}: {data.get('weather', {}).get('condition')}",
response=data
))
else:
results.append(TestResult(
name="Weather Agent Direct API",
passed=False,
message=f"HTTP {response.status_code}: {response.text}"
))
except Exception as e:
results.append(TestResult(
name="Weather Agent Direct API",
passed=False,
message=f"Error: {str(e)}"
))
# Test QnA Agent
try:
response = self.session.post(f"{self.qna_url}/ask", json={"question": "Hello"})
if response.status_code == 200:
data = response.json()
results.append(TestResult(
name="QnA Agent Direct API",
passed="response" in data,
message=f"QnA response: {data.get('response', 'No response')}",
response=data
))
else:
results.append(TestResult(
name="QnA Agent Direct API",
passed=False,
message=f"HTTP {response.status_code}: {response.text}"
))
except Exception as e:
results.append(TestResult(
name="QnA Agent Direct API",
passed=False,
message=f"Error: {str(e)}"
))
return results
def test_router_routing(self) -> List[TestResult]:
results = []
print("\n🔀 Testing Router Intelligence...")
test_cases = [
{
"question": "What is 15 * 8 + 7?",
"expected_agent": "math",
"description": "Complex math expression"
},
{
"question": "Calculate the square root of 144",
"expected_agent": "math",
"description": "Math function"
},
{
"question": "What's the weather like in Tokyo?",
"expected_agent": "weather",
"description": "Weather query with city"
},
{
"question": "Is it raining in London?",
"expected_agent": "weather",
"description": "Weather condition query"
},
{
"question": "How hot is it in Dubai?",
"expected_agent": "weather",
"description": "Temperature query"
},
{
"question": "What is the capital of France?",
"expected_agent": "qna",
"description": "General knowledge question"
},
{
"question": "Hello, how are you?",
"expected_agent": "qna",
"description": "Conversational query"
},
{
"question": "Tell me about artificial intelligence",
"expected_agent": "qna",
"description": "General information request"
}
]
for test_case in test_cases:
try:
response = self.session.post(
f"{self.router_url}/ask",
json={"question": test_case["question"]}
)
if response.status_code == 200:
data = response.json()
routed_agent = data.get("agent", "").lower()
expected = test_case["expected_agent"]
passed = expected in routed_agent
results.append(TestResult(
name=f"Routing: {test_case['description']}",
passed=passed,
message=f"Expected: {expected}, Got: {routed_agent}",
response=data
))
else:
results.append(TestResult(
name=f"Routing: {test_case['description']}",
passed=False,
message=f"HTTP {response.status_code}: {response.text}"
))
except Exception as e:
results.append(TestResult(
name=f"Routing: {test_case['description']}",
passed=False,
message=f"Error: {str(e)}"
))
return results
def test_edge_cases(self) -> List[TestResult]:
results = []
print("\n⚠️ Testing Edge Cases...")
edge_cases = [
{
"question": "",
"description": "Empty question",
"expect_error": True
},
{
"question": " ",
"description": "Whitespace only question",
"expect_error": True
},
{
"question": "a" * 1000,
"description": "Very long question",
"expect_error": False
}
]
for case in edge_cases:
try:
response = self.session.post(
f"{self.router_url}/ask",
json={"question": case["question"]}
)
if case["expect_error"]:
passed = response.status_code >= 400
message = f"Expected error, got HTTP {response.status_code}"
else:
passed = response.status_code == 200
message = f"Got HTTP {response.status_code}"
results.append(TestResult(
name=f"Edge Case: {case['description']}",
passed=passed,
message=message,
response=response.json() if response.status_code == 200 else response.text
))
except Exception as e:
results.append(TestResult(
name=f"Edge Case: {case['description']}",
passed=False,
message=f"Error: {str(e)}"
))
return results
def print_results(self, results: List[TestResult]):
print("\n" + "="*50)
print("📊 VALIDATION RESULTS")
print("="*50)
passed_count = 0
total_count = len(results)
for result in results:
status = "✅" if result.passed else "❌"
print(f"{status} {result.name}: {result.message}")
if result.passed:
passed_count += 1
print("\n" + "-"*50)
print(f"🎯 SUMMARY: {passed_count}/{total_count} tests passed ({passed_count/total_count*100:.1f}%)")
if passed_count == total_count:
print("🎉 All tests passed! System is fully functional.")
return True
else:
print("⚠️ Some tests failed. Please check the issues above.")
return False
def main():
validator = SystemValidator()
print("🧪 AI AnswerBot System Validation")
print("="*50)
if not validator.wait_for_services():
print("❌ Services are not ready. Please ensure all containers are running.")
sys.exit(1)
results = validator.run_all_tests()
success = validator.print_results(results)
sys.exit(0 if success else 1)
if __name__ == "__main__":
main()