-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_server.py
More file actions
174 lines (139 loc) · 5.3 KB
/
Copy pathtest_server.py
File metadata and controls
174 lines (139 loc) · 5.3 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
"""
Quick server test script.
Tests the API with sample data from the FastAPI repo.
Usage:
1. Start server: python main.py (in another terminal)
2. Run this: python test_server.py
"""
import requests
import time
import json
BASE_URL = "http://localhost:8000"
def test_health():
"""Test health endpoint."""
print("Testing /health...")
response = requests.get(f"{BASE_URL}/health")
print(f" Status: {response.status_code}")
print(f" Response: {json.dumps(response.json(), indent=2)}")
return response.status_code == 200
def test_status():
"""Test status endpoint."""
print("\nTesting /status...")
response = requests.get(f"{BASE_URL}/status")
print(f" Status: {response.status_code}")
data = response.json()
print(f" Response: {json.dumps(data, indent=2)}")
return response.status_code == 200, data
def test_ingest():
"""Test ingest endpoint with FastAPI repo."""
print("\nTesting /ingest...")
print(" (This may take 10-30 seconds...)")
payload = {
"repo_path": "data/fastapi/fastapi",
"window": 80,
"overlap": 15,
}
start = time.time()
response = requests.post(f"{BASE_URL}/ingest", json=payload, timeout=120)
duration = time.time() - start
print(f" Status: {response.status_code}")
if response.status_code == 200:
data = response.json()
print(f" Success: {data['success']}")
print(f" Files scanned: {data['files_scanned']}")
print(f" Files read: {data['files_read']}")
print(f" Chunks created: {data['chunks_total']}")
print(f" Avg lines per chunk: {data['avg_lines_per_chunk']}")
print(f" Duration: {data['duration_seconds']}s (API) / {duration:.2f}s (total)")
return True, data
else:
print(f" Error: {response.text}")
return False, None
def test_search(query: str, k: int = 5, **filters):
"""Test search endpoint."""
print(f"\nTesting /search with query: '{query}'")
params = {"q": query, "k": k}
params.update(filters)
start = time.time()
response = requests.get(f"{BASE_URL}/search", params=params)
duration = time.time() - start
print(f" Status: {response.status_code}")
if response.status_code == 200:
data = response.json()
print(f" Query: {data['query']}")
print(f" Results: {data['total_results']}")
print(f" Latency: {data['latency_ms']}ms (API) / {duration*1000:.2f}ms (total)")
if data['results']:
print(f"\n Top 3 results:")
for i, result in enumerate(data['results'][:3], 1):
print(f" {i}. {result['path']} (lines {result['start_line']}-{result['end_line']})")
print(f" Score: {result['score']:.3f} | Lang: {result['lang']}")
preview = result['preview'].strip().split('\n')[0][:60]
print(f" Preview: {preview}...")
return True, data
else:
print(f" Error: {response.text}")
return False, None
def run_full_test():
"""Run complete test suite."""
print("=" * 70)
print("CodePilot API - Full Integration Test")
print("=" * 70)
# Test 1: Health
if not test_health():
print("\n❌ Health check failed!")
return False
# Test 2: Status
success, status_data = test_status()
if not success:
print("\n❌ Status check failed!")
return False
# Test 3: Ingest (if not already indexed)
if not status_data.get('indexed'):
print("\n📥 Repository not indexed. Running ingestion...")
success, ingest_data = test_ingest()
if not success:
print("\n❌ Ingestion failed!")
return False
else:
print(f"\n✓ Repository already indexed ({status_data['chunks']} chunks)")
# Test 4: Multiple searches
test_queries = [
("How do I validate JWT tokens?", {}),
("WebSocket connection handling", {}),
("dependency injection", {"lang": "python"}),
("middleware", {"path_contains": "fastapi"}),
]
print("\n" + "=" * 70)
print("Running Search Tests")
print("=" * 70)
for query, filters in test_queries:
success, _ = test_search(query, k=5, **filters)
if not success:
print(f"\n❌ Search failed for: {query}")
return False
time.sleep(0.5) # Brief pause between searches
# Summary
print("\n" + "=" * 70)
print("✅ All tests passed!")
print("=" * 70)
# Final status
_, final_status = test_status()
print("\nFinal system status:")
print(f" Indexed: {final_status['indexed']}")
print(f" Total chunks: {final_status['chunks']}")
print(f" Model loaded: {final_status['model_loaded']}")
print(f" Index loaded: {final_status['index_loaded']}")
return True
if __name__ == "__main__":
try:
success = run_full_test()
exit(0 if success else 1)
except requests.exceptions.ConnectionError:
print("\n❌ Cannot connect to server!")
print(" Make sure the server is running:")
print(" cd /Users/aliasgarmomin/codepilot && python api/main.py")
exit(1)
except KeyboardInterrupt:
print("\n\n⚠️ Test interrupted by user")
exit(1)