""" ContextFlow Test Suite Tests all API endpoints and core functionality. """ import requests import json import time BASE_URL = "http://localhost:5001/api" def test_health(): """Test health endpoint""" print("\n=== Testing Health Endpoint ===") try: response = requests.get(f"{BASE_URL}/health") print(f"Status: {response.status_code}") print(f"Response: {response.json()}") return response.status_code == 200 except Exception as e: print(f"Error: {e}") return False def test_session(): """Test session management""" print("\n=== Testing Session Management ===") try: response = requests.post(f"{BASE_URL}/session/start", json={ "user_id": "test_user", "topic": "Machine Learning" }) print(f"Status: {response.status_code}") data = response.json() print(f"Session ID: {data.get('session_id')}") print(f"Predictions: {len(data.get('predictions', []))} doubts predicted") update_response = requests.post(f"{BASE_URL}/session/update", json={ "user_id": "test_user", "behavioral_data": { "mouse_hesitation": 0.3, "scroll_reversals": 5 } }) print(f"Update Status: {update_response.status_code}") return response.status_code == 200 except Exception as e: print(f"Error: {e}") return False def test_gestures(): """Test gesture management""" print("\n=== Testing Gesture Management ===") try: response = requests.get(f"{BASE_URL}/gesture/list?user_id=test_user") print(f"List Status: {response.status_code}") data = response.json() print(f"Gestures: {data.get('count')} available") response = requests.post(f"{BASE_URL}/gesture/add", json={ "user_id": "test_user", "name": "Test Gesture", "description": "A test gesture" }) print(f"Add Status: {response.status_code}") response = requests.post(f"{BASE_URL}/gesture/training/start", json={ "user_id": "test_user", "gesture_id": "thinking" }) print(f"Training Start Status: {response.status_code}") print(f"Instructions: {response.json().get('instructions')}") sample_landmarks = [[0.1, 0.2, 0.0]] * 21 response = requests.post(f"{BASE_URL}/gesture/training/sample", json={ "user_id": "test_user", "landmarks": sample_landmarks }) print(f"Training Sample Status: {response.status_code}") return True except Exception as e: print(f"Error: {e}") return False def test_doubt_prediction(): """Test doubt prediction""" print("\n=== Testing Doubt Prediction ===") try: response = requests.post(f"{BASE_URL}/predict/doubts", json={ "user_id": "test_user", "context": { "topic": "Neural Networks", "progress": 0.5, "confusion_signals": 0.7 } }) print(f"Status: {response.status_code}") data = response.json() print(f"Predictions: {len(data.get('predictions', []))} doubts") for pred in data.get('predictions', [])[:3]: print(f" - {pred.get('doubt')} ({pred.get('confidence')*100:.0f}%)") return True except Exception as e: print(f"Error: {e}") return False def test_knowledge_graph(): """Test knowledge graph""" print("\n=== Testing Knowledge Graph ===") try: response = requests.post(f"{BASE_URL}/graph/add", json={ "user_id": "test_user", "doubt": { "topic": "Deep Learning", "concept": "Backpropagation", "content": "How does gradient descent work?" } }) print(f"Add Node Status: {response.status_code}") response = requests.post(f"{BASE_URL}/graph/query", json={ "user_id": "test_user", "query": "gradient descent", "top_k": 3 }) print(f"Query Status: {response.status_code}") return True except Exception as e: print(f"Error: {e}") return False def test_recall(): """Test spaced repetition recall""" print("\n=== Testing Spaced Repetition ===") try: response = requests.get(f"{BASE_URL}/review/due?user_id=test_user") print(f"Due Reviews Status: {response.status_code}") data = response.json() print(f"Due Count: {data.get('due_count')}") if data.get('cards'): card = data['cards'][0] response = requests.post(f"{BASE_URL}/review/complete", json={ "user_id": "test_user", "card_id": card['card_id'], "quality": 4 }) print(f"Complete Review Status: {response.status_code}") print(f"XP Earned: {response.json().get('xp_earned')}") return True except Exception as e: print(f"Error: {e}") return False def test_peer_learning(): """Test peer learning network""" print("\n=== Testing Peer Learning ===") try: response = requests.get(f"{BASE_URL}/peer/insights?topic=ML") print(f"Peer Insights Status: {response.status_code}") data = response.json() print(f"Insights: {len(data.get('insights', []))}") response = requests.get(f"{BASE_URL}/peer/doubts?topic=ML&limit=5") print(f"Peer Doubts Status: {response.status_code}") response = requests.get(f"{BASE_URL}/peer/trending") print(f"Trending Status: {response.status_code}") print(f"Trending: {response.json().get('trending', [])[:3]}") return True except Exception as e: print(f"Error: {e}") return False def test_llm_flow(): """Test LLM Flow (browser launch simulation)""" print("\n=== Testing LLM Flow ===") try: response = requests.get(f"{BASE_URL}/llm/gesture-actions?user_id=test_user") print(f"Gesture Actions Status: {response.status_code}") data = response.json() print(f"Available Actions: {len(data.get('actions', []))}") for action in data.get('actions', [])[:5]: print(f" - {action.get('action')}: {action.get('gesture')}") response = requests.post(f"{BASE_URL}/llm/rl/start", json={ "user_id": "test_user", "context": {"topic": "Learning"} }) print(f"RL Loop Start Status: {response.status_code}") response = requests.get(f"{BASE_URL}/llm/rl/status?user_id=test_user") print(f"RL Status: {response.json()}") response = requests.post(f"{BASE_URL}/llm/rl/feedback", json={ "user_id": "test_user", "quality": 4, "comment": "Great explanation!" }) print(f"RL Feedback Status: {response.status_code}") return True except Exception as e: print(f"Error: {e}") return False def run_all_tests(): """Run all tests""" print("=" * 50) print("ContextFlow Test Suite") print("=" * 50) tests = [ ("Health Check", test_health), ("Session Management", test_session), ("Gesture Management", test_gestures), ("Doubt Prediction", test_doubt_prediction), ("Knowledge Graph", test_knowledge_graph), ("Spaced Repetition", test_recall), ("Peer Learning", test_peer_learning), ("LLM Flow", test_llm_flow), ] results = [] for name, test_func in tests: try: result = test_func() results.append((name, result)) status = "PASS" if result else "FAIL" print(f"\n{'='*40}") print(f"Result: {status}") except Exception as e: print(f"\nException: {e}") results.append((name, False)) print("\n" + "=" * 50) print("SUMMARY") print("=" * 50) passed = sum(1 for _, r in results if r) total = len(results) for name, result in results: status = "PASS ✓" if result else "FAIL ✗" print(f"{status}: {name}") print(f"\nTotal: {passed}/{total} passed") return passed == total if __name__ == "__main__": import sys print("Starting ContextFlow Test Suite...") print("Make sure the backend server is running on http://localhost:5001") print("Press Ctrl+C to cancel, or Enter to continue...") try: input() except: pass success = run_all_tests() sys.exit(0 if success else 1)