Files
kondiplo_api/test_api.py
2026-02-15 14:53:03 +09:00

84 lines
2.7 KiB
Python

import requests
import json
from diplomacy import Game
# API URL
BASE_URL = "http://127.0.0.1:8000"
def test_api():
# 1. Test GET /maps
print("Testing /maps...")
resp = requests.get(f"{BASE_URL}/maps")
print(resp.json())
# 2. Test POST /calculate/process (Spring 1901 -> Fall 1901)
print("\nTesting /calculate/process...")
game = Game()
initial_state = game.to_dict()
payload = {
"game_state": initial_state,
"orders": {
"FRANCE": ["A MAR H", "A PAR - BUR", "F BRE - MAO"],
"ENGLAND": ["F EDI - NTH", "F LON - ENG", "A LVP - WAL"]
}
}
resp = requests.post(f"{BASE_URL}/calculate/process", json=payload)
result = resp.json()
new_phase = result["game_state"]["phase"]
print(f"New Phase: {new_phase}")
assert "FALL 1901" in new_phase
# 3. Test POST /render
print("\nTesting /render...")
payload = {
"game_state": initial_state,
"orders": {
"FRANCE": ["A MAR - SPA", "A PAR - BUR", "F BRE - MAO"]
},
"incl_orders": True
}
resp = requests.post(f"{BASE_URL}/render", json=payload)
print(f"Render Status: {resp.status_code}")
print(f"Content Type: {resp.headers.get('Content-Type')}")
assert resp.status_code == 200
assert "image/svg+xml" in resp.headers.get('Content-Type')
# Save SVG to check manually if possible
with open("test_render.svg", "wb") as f:
f.write(resp.content)
print("Saved test_render.svg")
# 4. Test POST /calculate/possible-orders
print("\nTesting /calculate/possible-orders...")
payload = {
"game_state": initial_state
}
resp = requests.post(f"{BASE_URL}/calculate/possible-orders", json=payload)
result = resp.json()
print(f"Possible Orders (Keys): {list(result['possible_orders'].keys())[:5]}")
assert "BUD" in result['possible_orders']
# 5. Test initial state
print("\nTesting /game/initial-state...")
resp = requests.get(f"{BASE_URL}/game/initial-state")
result = resp.json()
assert "game_state" in result
print(f"Initial Phase: {result['game_state']['phase']}")
# 6. Test auto orders
print("\nTesting /calculate/auto-orders...")
payload = {
"game_state": initial_state
}
resp = requests.post(f"{BASE_URL}/calculate/auto-orders?power_name=FRANCE", json=payload)
assert resp.status_code == 200
result = resp.json()
print(f"Auto Orders for FRANCE: {result['orders']}")
assert len(result['orders']) > 0
# Basic check to see if orders look valid (simple heuristic)
assert any("A PAR" in o or "A MAR" in o or "F BRE" in o for o in result['orders'])
if __name__ == "__main__":
test_api()