import urllib.request import urllib.parse import http.cookiejar import json BASE_URL = "http://127.0.0.1:8001" def test_flow(): print("--- 1. Test Game Start ---") req = urllib.request.Request(f"{BASE_URL}/api/game/start", data=b"{}", headers={"Content-Type": "application/json"}) res = urllib.request.urlopen(req) start_data = json.loads(res.read().decode()) print("Start Response:", start_data) session_id = start_data["session_id"] assert start_data["question_number"] == 1 assert start_data["score"] == 0 assert "canonical_answer" not in start_data print("\n--- 2. Test Correct Answer (James) ---") payload = json.dumps({"session_id": session_id, "answer": "James"}).encode() req = urllib.request.Request(f"{BASE_URL}/api/game/check", data=payload, headers={"Content-Type": "application/json"}) res = urllib.request.urlopen(req) check_data = json.loads(res.read().decode()) print("Check Response:", check_data) assert check_data["correct"] is True assert check_data["score"] == 1 assert check_data["next_question"]["question_number"] == 2 print("\n--- 3. Test Wrong Answer ---") payload = json.dumps({"session_id": session_id, "answer": "Vollkommen falsch"}).encode() req = urllib.request.Request(f"{BASE_URL}/api/game/check", data=payload, headers={"Content-Type": "application/json"}) res = urllib.request.urlopen(req) wrong_data = json.loads(res.read().decode()) print("Wrong Response:", wrong_data) assert wrong_data["correct"] is False assert wrong_data["attempts_used"] == 1 assert wrong_data["attempts_left"] == 2 print("\n--- 4. Test Question Suggestion (Community) ---") sugg_payload = json.dumps({ "frage": "Wie lautet der Tarnname von Sirius Black als Hund?", "antwort": "Tatze", "tipp": "Einer der Rumtreiber", "name": "Remus Lupin" }).encode() req = urllib.request.Request(f"{BASE_URL}/api/suggestions", data=sugg_payload, headers={"Content-Type": "application/json"}) res = urllib.request.urlopen(req) sugg_data = json.loads(res.read().decode()) print("Suggestion Response:", sugg_data) assert sugg_data["success"] is True print("\n--- 5. Test Admin Login & Moderation Flow ---") cj = http.cookiejar.CookieJar() opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj)) login_data = urllib.parse.urlencode({"username": "admin", "password": "Alohomora2026!"}).encode() login_req = urllib.request.Request(f"{BASE_URL}/admin/login", data=login_data) login_res = opener.open(login_req) assert login_res.getcode() == 200 print("Admin logged in successfully.") admin_dashboard_res = opener.open(f"{BASE_URL}/admin") content = admin_dashboard_res.read().decode() assert "Großinquisitor-Cockpit" in content assert "Tatze" in content print("Dashboard rendered with pending suggestions.") # Test Matcher API test_match_data = urllib.parse.urlencode({"test_input": "Taze", "canonical": "Tatze"}).encode() tm_res = opener.open(f"{BASE_URL}/admin/questions/test-match", data=test_match_data) tm_json = json.loads(tm_res.read().decode()) print("Matcher Test ('Taze' vs 'Tatze'):", tm_json) assert tm_json["matched"] is True print("\n✅ All Game, Suggestion & Admin Tests Passed Successfully!") if __name__ == "__main__": test_flow()