Spaces:
Sleeping
Sleeping
| 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) |