import urllib.request import urllib.error import json base_url = "http://127.0.0.1:8004" def post_json(url, data, token=None): req = urllib.request.Request( url, data=json.dumps(data).encode("utf-8"), headers={ "Content-Type": "application/json", **({"Authorization": f"Bearer {token}"} if token else {}) } ) with urllib.request.urlopen(req) as resp: return resp.status, json.loads(resp.read().decode("utf-8")) def get_json(url, token=None): req = urllib.request.Request( url, headers={"Authorization": f"Bearer {token}"} if token else {} ) with urllib.request.urlopen(req) as resp: return resp.status, json.loads(resp.read().decode("utf-8")) def test(): print("Testing 16x10 grid, Building Slots & Army...") status, d = post_json(f"{base_url}/api/auth/login", {"username": "Lord Malakor", "password": "pass123"}) token = d["token"] status, state = get_json(f"{base_url}/api/game/state", token) user = state["user"] print(f"User: {user['username']}, Fortress: {user['fortress_name']}") print(f"Building Slots: {user['max_building_slots']} (used: {user['used_building_slots']})") print(f"Next Slot Upgrade: {user.get('next_slot_upgrade')}") print(f"Maze Grid: {state['maze']['grid_w']}x{state['maze']['grid_h']}, Start: {state['maze']['start']}, End: {state['maze']['end']}") # Test buy building slots try: status, buy_resp = post_json(f"{base_url}/api/maze/buy-slot", {}, token) print("SUCCESS: Slot purchase response:", buy_resp) except urllib.error.HTTPError as e: print(f"Slot purchase info: HTTP {e.code} - {e.read().decode()}") # Test placing out of bounds tile (>15 or >9) try: post_json(f"{base_url}/api/maze/set-tile", {"x": 18, "y": 12, "tile_type": "rock"}, token) print("FAIL: Out of bounds tile was accepted!") except urllib.error.HTTPError as e: print(f"SUCCESS: Rejected out-of-bounds tile (18, 12): HTTP {e.code}") # Test placing tile at start/end try: post_json(f"{base_url}/api/maze/set-tile", {"x": 0, "y": 4, "tile_type": "rock"}, token) print("FAIL: Tile at start (0, 4) was accepted!") except urllib.error.HTTPError as e: print(f"SUCCESS: Rejected tile on start position (0, 4): HTTP {e.code}") # Test placing a valid rock at (3, 3) try: status, set_resp = post_json(f"{base_url}/api/maze/set-tile", {"x": 3, "y": 3, "tile_type": "rock"}, token) print("SUCCESS: Placed rock at (3, 3):", set_resp.get("valid_path")) except urllib.error.HTTPError as e: print(f"Set tile info: HTTP {e.code} - {e.read().decode()}") # Fresh user test print("\n--- Testing Fresh User with 8 Slots ---") u_fresh = "Tester_" + str(int(user["rating"])) try: status, reg_d = post_json(f"{base_url}/api/auth/register", {"username": u_fresh, "fortress_name": "Feste Eisenstein", "password": "pass"}) f_token = reg_d["token"] status, f_state = get_json(f"{base_url}/api/game/state", f_token) print("Fresh user max slots:", f_state["user"]["max_building_slots"], "Used:", f_state["user"]["used_building_slots"]) # Place 1st rock status, set_res = post_json(f"{base_url}/api/maze/set-tile", {"x": 2, "y": 2, "tile_type": "rock"}, f_token) print("Fresh user placed rock:", set_res.get("valid_path")) except Exception as e: print("Fresh user test info:", e) if __name__ == "__main__": test()