-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverify_prototype.py
More file actions
290 lines (227 loc) · 11.6 KB
/
verify_prototype.py
File metadata and controls
290 lines (227 loc) · 11.6 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
#!/usr/bin/env python3
"""
CogniFlow Prototype Verification Script
=====================================
This script tests the refactored CogniFlow services to ensure all store abstractions
are working correctly and the core learning features are functional.
Usage: python verify_prototype.py
"""
import asyncio
import httpx
import json
import time
from typing import Dict, Any
# Service endpoints
SERVICES = {
"auth": "http://localhost:8002",
"users": "http://localhost:8001",
"courses": "http://localhost:8003",
"ai_tutor": "http://localhost:8000",
"analytics": "http://localhost:8004"
}
class PrototypeVerifier:
def __init__(self):
self.results = {}
async def verify_service_health(self, service_name: str, url: str) -> bool:
"""Check if service is running and healthy"""
try:
async with httpx.AsyncClient() as client:
# Try both /health and / endpoints
endpoints = ["/health", "/"]
for endpoint in endpoints:
try:
response = await client.get(f"{url}{endpoint}", timeout=5.0)
if response.status_code == 200:
print(f"✅ {service_name.upper()} Service: Healthy")
return True
except:
continue
print(f"❌ {service_name.upper()} Service: Not responding")
return False
except Exception as e:
print(f"❌ {service_name.upper()} Service: Error - {e}")
return False
async def test_course_enrollment_flow(self) -> bool:
"""Test the complete course enrollment and progress flow"""
print("\n🎓 Testing Course Enrollment Flow...")
try:
async with httpx.AsyncClient() as client:
# 1. List available courses
response = await client.get(f"{SERVICES['courses']}/courses/")
if response.status_code != 200:
print("❌ Failed to list courses")
return False
courses = response.json()
if not courses:
print("❌ No courses available")
return False
course_id = courses[0]["id"]
print(f"✅ Found {len(courses)} courses, using course {course_id}")
# 2. Enroll user in course
enrollment_data = {"user_id": "test_user", "course_id": course_id}
response = await client.post(
f"{SERVICES['courses']}/courses/{course_id}/enroll",
json=enrollment_data
)
if response.status_code not in [200, 201]:
print(f"❌ Enrollment failed: {response.text}")
return False
print(f"✅ Successfully enrolled user in course {course_id}")
# 3. Get user enrollments
response = await client.get(f"{SERVICES['courses']}/users/test_user/enrollments")
if response.status_code != 200:
print("❌ Failed to get user enrollments")
return False
enrollments = response.json()
if not enrollments:
print("❌ No enrollments found")
return False
print(f"✅ User has {len(enrollments)} enrollments")
# 4. Complete a lesson
lesson_id = 1 # Assume first lesson
response = await client.post(
f"{SERVICES['courses']}/users/test_user/progress/{course_id}/lessons/{lesson_id}"
)
if response.status_code not in [200, 201]:
print(f"❌ Lesson completion failed: {response.text}")
return False
print(f"✅ Successfully completed lesson {lesson_id}")
# 5. Check course progress
response = await client.get(
f"{SERVICES['courses']}/users/test_user/progress/{course_id}"
)
if response.status_code != 200:
print("❌ Failed to get course progress")
return False
progress = response.json()
print(f"✅ Course progress: {progress.get('progress_percentage', 0)}%")
return True
except Exception as e:
print(f"❌ Course enrollment flow failed: {e}")
return False
async def test_ai_tutor_features(self) -> bool:
"""Test AI tutor chat, spaced repetition, and gamification"""
print("\n🤖 Testing AI Tutor Features...")
try:
async with httpx.AsyncClient() as client:
# 1. Test chat functionality
chat_data = {
"user_id": "test_user",
"message": "Hello, can you help me learn Python?",
"context": "programming"
}
response = await client.post(f"{SERVICES['ai_tutor']}/chat", json=chat_data)
if response.status_code != 200:
print(f"❌ Chat failed: {response.text}")
return False
chat_response = response.json()
print(f"✅ Chat response: {chat_response['response'][:50]}...")
# 2. Test spaced repetition schedule
response = await client.get(f"{SERVICES['ai_tutor']}/spaced-repetition/test_user")
if response.status_code != 200:
print(f"❌ Spaced repetition failed: {response.text}")
return False
schedule = response.json()
print(f"✅ Spaced repetition: {schedule.get('items_due', 0)} items due")
# 3. Test gamification stats
response = await client.get(f"{SERVICES['ai_tutor']}/gamification/test_user")
if response.status_code != 200:
print(f"❌ Gamification failed: {response.text}")
return False
stats = response.json()
print(f"✅ Gamification: {stats['stats']['total_points']} points, {len(stats['badges'])} badges")
# 4. Test quiz generation
quiz_data = {
"user_id": "test_user",
"topic": "python",
"difficulty": "beginner",
"num_questions": 3
}
response = await client.post(f"{SERVICES['ai_tutor']}/quiz/generate", json=quiz_data)
if response.status_code != 200:
print(f"❌ Quiz generation failed: {response.text}")
return False
quiz = response.json()
print(f"✅ Generated quiz with {len(quiz['questions'])} questions")
return True
except Exception as e:
print(f"❌ AI tutor features failed: {e}")
return False
async def test_analytics_events(self) -> bool:
"""Test analytics event recording and retrieval"""
print("\n📈 Testing Analytics Events...")
try:
async with httpx.AsyncClient() as client:
# 1. Record a test event
event_data = {
"user_id": "test_user",
"course_id": 1,
"lesson_id": 1,
"event_type": "lesson_complete",
"event_data": {
"duration_minutes": 15,
"completion_rate": 0.95
}
}
response = await client.post(f"{SERVICES['analytics']}/events", json=event_data)
if response.status_code not in [200, 201]:
print(f"❌ Event recording failed: {response.text}")
return False
print("✅ Successfully recorded analytics event")
# 2. Get user analytics
response = await client.get(f"{SERVICES['analytics']}/analytics/user/test_user")
if response.status_code != 200:
print(f"❌ User analytics failed: {response.text}")
return False
analytics = response.json()
print(f"✅ User analytics: {analytics['analytics']['total_events']} total events")
# 3. Get system analytics
response = await client.get(f"{SERVICES['analytics']}/analytics/system")
if response.status_code != 200:
print(f"❌ System analytics failed: {response.text}")
return False
system_analytics = response.json()
print(f"✅ System analytics: {system_analytics['analytics']['total_events']} total events")
return True
except Exception as e:
print(f"❌ Analytics events failed: {e}")
return False
async def run_verification(self):
"""Run complete verification suite"""
print("🚀 CogniFlow Prototype Verification")
print("=" * 50)
# Check service health
print("\n🏥 Checking Service Health...")
health_results = {}
for service_name, url in SERVICES.items():
health_results[service_name] = await self.verify_service_health(service_name, url)
unhealthy_services = [name for name, healthy in health_results.items() if not healthy]
if unhealthy_services:
print(f"\n⚠️ Some services are not running: {unhealthy_services}")
print("Please start all services before running verification.")
return
# Run feature tests
test_results = {}
test_results["enrollment_flow"] = await self.test_course_enrollment_flow()
test_results["ai_tutor"] = await self.test_ai_tutor_features()
test_results["analytics"] = await self.test_analytics_events()
# Summary
print("\n" + "=" * 50)
print("📊 VERIFICATION SUMMARY")
print("=" * 50)
passed_tests = sum(1 for result in test_results.values() if result)
total_tests = len(test_results)
for test_name, result in test_results.items():
status = "✅ PASSED" if result else "❌ FAILED"
print(f"{test_name.upper().replace('_', ' ')}: {status}")
print(f"\nOverall: {passed_tests}/{total_tests} tests passed")
if passed_tests == total_tests:
print("\n🎉 All tests passed! The refactored prototype is working correctly.")
print("\n🚀 Ready for frontend integration and demonstration!")
else:
print(f"\n⚠️ {total_tests - passed_tests} tests failed. Please check the service logs.")
async def main():
verifier = PrototypeVerifier()
await verifier.run_verification()
if __name__ == "__main__":
asyncio.run(main())