import requests import sys BASE_URL = "http://127.0.0.1:7860" def validate(): print("=" * 50) print("VALIDATING EMERGENCY RESOURCE ENVIRONMENT") print("=" * 50) print() tests = 0 passed = 0 # Test 1: Server health tests += 1 try: r = requests.get(f"{BASE_URL}/", timeout=5) if r.status_code == 200: print("✅ Server is running") passed += 1 else: print("❌ Server returned error") except Exception as e: print(f"❌ Cannot connect to server: {e}") return False # Test 2: Reset endpoint tests += 1 r = requests.post(f"{BASE_URL}/reset", params={"task": "easy"}) if r.status_code == 200 and "agent_position" in r.json(): print("✅ Reset endpoint works") passed += 1 else: print("❌ Reset endpoint failed") # Test 3: All three tasks tests += 1 all_tasks_work = True for task in ["easy", "medium", "hard"]: r = requests.post(f"{BASE_URL}/reset", params={"task": task}) if r.status_code != 200: all_tasks_work = False print(f"❌ Task '{task}' failed") break if all_tasks_work: print("✅ All 3 tasks (easy, medium, hard) work") passed += 1 # Test 4: Step endpoint tests += 1 action = {"action_type": "move", "direction": "up"} r = requests.post(f"{BASE_URL}/step", json=action) if r.status_code == 200 and "reward" in r.json(): print("✅ Step endpoint works") passed += 1 else: print("❌ Step endpoint failed") # Test 5: State endpoint tests += 1 r = requests.get(f"{BASE_URL}/state") if r.status_code == 200 and "agent_position" in r.json(): print("✅ State endpoint works") passed += 1 else: print("❌ State endpoint failed") # Test 6: Grade endpoint tests += 1 r = requests.get(f"{BASE_URL}/grade") if r.status_code == 200 and "score" in r.json(): score = r.json()['score'] if 0 <= score <= 1: print(f"✅ Grade endpoint returns valid score: {score:.3f}") passed += 1 else: print(f"❌ Score {score} is not between 0 and 1") else: print("❌ Grade endpoint failed") # Test 7: Reward values tests += 1 rewards = [] for _ in range(5): action = {"action_type": "move", "direction": "right"} r = requests.post(f"{BASE_URL}/step", json=action) rewards.append(r.json()['reward']['score']) if all(0 <= r <= 1 for r in rewards): print("✅ All rewards are between 0 and 1") passed += 1 else: print("❌ Some rewards outside 0-1 range") print() print("=" * 50) print(f"RESULTS: {passed}/{tests} tests passed") print("=" * 50) return passed == tests if __name__ == "__main__": success = validate() sys.exit(0 if success else 1)