-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_system.py
More file actions
367 lines (274 loc) · 11 KB
/
test_system.py
File metadata and controls
367 lines (274 loc) · 11 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
359
360
361
362
363
364
365
366
367
"""Comprehensive test script for CodeReview-AI-Agent system.
This script tests the complete end-to-end functionality including:
- All three agents
- Custom tools
- Session management
- Memory bank
- Observability
- Evaluation framework
"""
import os
import sys
from pathlib import Path
# Add parent directory to path
sys.path.insert(0, str(Path(__file__).parent))
from main import CodeReviewOrchestrator
from utils.observability import get_observability
from utils.evaluation import AgentEvaluator, create_default_test_cases
def test_basic_functionality():
"""Test basic code review functionality."""
print("\n" + "="*70)
print("Test 1: Basic Code Review")
print("="*70)
# Initialize observability
obs = get_observability(log_level="INFO")
# Simple test code
test_code = '''def hello(name):
"""Say hello."""
return f"Hello, {name}!"
'''
try:
orchestrator = CodeReviewOrchestrator()
with obs.trace_operation("basic_review_test"):
results = orchestrator.review_code(test_code, language="python")
# Validate results
assert 'agents' in results, "Results should contain agents key"
assert 'code_analyzer' in results['agents'], "Code analyzer should run"
assert 'security_checker' in results['agents'], "Security checker should run"
assert 'quality_reviewer' in results['agents'], "Quality reviewer should run"
print("✅ Basic functionality test PASSED")
return True
except Exception as e:
print(f"❌ Basic functionality test FAILED: {e}")
return False
def test_security_detection():
"""Test security vulnerability detection."""
print("\n" + "="*70)
print("Test 2: Security Vulnerability Detection")
print("="*70)
# Code with security issues
test_code = '''def unsafe_query(user_id):
password = "hardcoded123"
query = "SELECT * FROM users WHERE id = %s" % user_id
return execute(query)
'''
try:
orchestrator = CodeReviewOrchestrator()
results = orchestrator.review_code(test_code, language="python")
# Check security findings
security_results = results['agents']['security_checker']
vulnerabilities = security_results.get('vulnerabilities', [])
assert len(vulnerabilities) > 0, "Should detect security vulnerabilities"
# Check for specific vulnerabilities
vuln_types = [v['type'] for v in vulnerabilities]
assert 'sql_injection' in vuln_types or 'hardcoded_secret' in vuln_types, \
"Should detect SQL injection or hardcoded secrets"
print(f" Detected {len(vulnerabilities)} vulnerabilities: {vuln_types}")
print("✅ Security detection test PASSED")
return True
except Exception as e:
print(f"❌ Security detection test FAILED: {e}")
return False
def test_complexity_analysis():
"""Test complexity analysis."""
print("\n" + "="*70)
print("Test 3: Complexity Analysis")
print("="*70)
# Complex function
test_code = '''def complex_calc(x, y, z):
if x > 0:
if y > 0:
if z > 0:
return x + y + z
else:
return x + y - z
else:
return x - y
else:
return 0
'''
try:
orchestrator = CodeReviewOrchestrator()
results = orchestrator.review_code(test_code, language="python")
# Check analysis results
analysis_results = results['agents']['code_analyzer']
metrics = analysis_results.get('metrics', {})
assert 'total_complexity' in metrics, "Should calculate complexity"
assert metrics['total_complexity'] > 0, "Complexity should be greater than 0"
print(f" Total complexity: {metrics['total_complexity']}")
print(f" Max function complexity: {metrics['max_function_complexity']}")
print("✅ Complexity analysis test PASSED")
return True
except Exception as e:
print(f"❌ Complexity analysis test FAILED: {e}")
return False
def test_session_management():
"""Test session management and memory."""
print("\n" + "="*70)
print("Test 4: Session Management")
print("="*70)
test_code = 'def test(): pass'
try:
orchestrator = CodeReviewOrchestrator()
# First review
results1 = orchestrator.review_code(test_code, language="python")
session_id = results1['session_id']
# Retrieve session history
history = orchestrator.get_session_history(session_id)
assert history is not None, "Should retrieve session history"
assert len(history['history']) > 0, "Session should have history"
print(f" Session ID: {session_id}")
print(f" History items: {len(history['history'])}")
print("✅ Session management test PASSED")
return True
except Exception as e:
print(f"❌ Session management test FAILED: {e}")
return False
def test_memory_bank():
"""Test memory bank functionality."""
print("\n" + "="*70)
print("Test 5: Memory Bank")
print("="*70)
try:
orchestrator = CodeReviewOrchestrator()
memory = orchestrator.memory_bank
# Store data
memory.store("test_key", {"data": "test_value"})
# Retrieve data
retrieved = memory.get("test_key")
assert retrieved is not None, "Should retrieve stored data"
assert retrieved['data'] == "test_value", "Data should match"
# Check exists
assert memory.exists("test_key"), "Key should exist"
# Get summary
summary = memory.get_context_summary()
assert summary['total_items'] > 0, "Should have items in memory"
print(f" Memory items: {summary['total_items']}")
print("✅ Memory bank test PASSED")
return True
except Exception as e:
print(f"❌ Memory bank test FAILED: {e}")
return False
def test_observability():
"""Test observability features."""
print("\n" + "="*70)
print("Test 6: Observability")
print("="*70)
try:
obs = get_observability()
# Test tracing
with obs.trace_operation("test_operation", test_param="value"):
import time
time.sleep(0.1)
# Test metrics
obs.record_metric("test_metric", 42.5)
# Get traces and metrics
traces = obs.get_traces()
metrics = obs.get_all_metrics()
assert len(traces) > 0, "Should have recorded traces"
assert "test_metric" in metrics, "Should have recorded metrics"
print(f" Total traces: {len(traces)}")
print(f" Total metrics: {len(metrics)}")
print("✅ Observability test PASSED")
return True
except Exception as e:
print(f"❌ Observability test FAILED: {e}")
return False
def test_evaluation_framework():
"""Test evaluation framework."""
print("\n" + "="*70)
print("Test 7: Evaluation Framework")
print("="*70)
try:
orchestrator = CodeReviewOrchestrator()
evaluator = AgentEvaluator()
# Create test cases
test_cases = create_default_test_cases()
# Add a subset for quick testing
for tc in test_cases[:2]:
evaluator.add_test_case(tc)
# Run evaluation
summary = evaluator.evaluate_all(orchestrator)
assert 'total_tests' in summary, "Should have evaluation summary"
assert summary['total_tests'] == 2, "Should run 2 tests"
print(f" Tests run: {summary['total_tests']}")
print(f" Pass rate: {summary['pass_rate']}%")
print("✅ Evaluation framework test PASSED")
return True
except Exception as e:
print(f"❌ Evaluation framework test FAILED: {e}")
import traceback
traceback.print_exc()
return False
def run_full_evaluation():
"""Run full evaluation with all test cases."""
print("\n" + "="*70)
print("Full Agent Evaluation")
print("="*70)
try:
orchestrator = CodeReviewOrchestrator()
evaluator = AgentEvaluator()
# Load all default test cases
test_cases = create_default_test_cases()
for tc in test_cases:
evaluator.add_test_case(tc)
print(f"\nRunning evaluation on {len(test_cases)} test cases...")
# Run evaluation
summary = evaluator.evaluate_all(orchestrator)
# Print results
evaluator.print_summary()
# Export results
evaluator.export_results("evaluation_results.json")
print("\n💾 Detailed results saved to: evaluation_results.json")
return True
except Exception as e:
print(f"❌ Full evaluation FAILED: {e}")
import traceback
traceback.print_exc()
return False
def main():
"""Run all tests."""
print("\n" + "="*70)
print("CodeReview-AI-Agent System Tests")
print("="*70)
# Check for API key
if not os.getenv('GOOGLE_AI_API_KEY'):
print("\n❌ ERROR: GOOGLE_AI_API_KEY environment variable not set")
print(" Please set your API key:")
print(" export GOOGLE_AI_API_KEY='your-key-here'")
sys.exit(1)
results = []
# Run tests
results.append(("Basic Functionality", test_basic_functionality()))
results.append(("Security Detection", test_security_detection()))
results.append(("Complexity Analysis", test_complexity_analysis()))
results.append(("Session Management", test_session_management()))
results.append(("Memory Bank", test_memory_bank()))
results.append(("Observability", test_observability()))
results.append(("Evaluation Framework", test_evaluation_framework()))
# Print summary
print("\n" + "="*70)
print("📊 Test Summary")
print("="*70)
passed = sum(1 for _, result in results if result)
total = len(results)
for name, result in results:
status = "✅ PASSED" if result else "❌ FAILED"
print(f" {status}: {name}")
print(f"\nTotal: {passed}/{total} tests passed ({int(passed/total*100)}%)")
# Run full evaluation if all tests passed
if passed == total:
print("\n🎉 All unit tests passed! Running full evaluation...")
run_full_evaluation()
# Print observability summary
obs = get_observability()
obs.print_summary()
# Export observability data
obs.export_traces("test_traces.json")
obs.export_metrics("test_metrics.json")
print("\n💾 Observability data exported to test_traces.json and test_metrics.json")
print("\n" + "="*70)
return passed == total
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1)