83 lines
2.5 KiB
Python
83 lines
2.5 KiB
Python
|
|
import pytest
|
||
|
|
from fastapi.testclient import TestClient
|
||
|
|
from app.main import app
|
||
|
|
from app.game import game_engine
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.fixture(autouse=True)
|
||
|
|
def reset_board_state():
|
||
|
|
import asyncio
|
||
|
|
asyncio.run(game_engine.reset())
|
||
|
|
yield
|
||
|
|
asyncio.run(game_engine.reset())
|
||
|
|
|
||
|
|
|
||
|
|
def test_health():
|
||
|
|
client = TestClient(app)
|
||
|
|
response = client.get("/api/health")
|
||
|
|
assert response.status_code == 200
|
||
|
|
data = response.json()
|
||
|
|
assert data["success"] is True
|
||
|
|
assert "FastAPI" in data["message"] or data["data"]["service"] == "FastAPI"
|
||
|
|
|
||
|
|
|
||
|
|
def test_register_player():
|
||
|
|
client = TestClient(app)
|
||
|
|
res = client.post("/api/players", json={"name": "CyberBot", "color": "#00FFAA"})
|
||
|
|
assert res.status_code == 201
|
||
|
|
player = res.json()
|
||
|
|
assert player["name"] == "CyberBot"
|
||
|
|
assert player["color"] == "#00FFAA"
|
||
|
|
assert 0 <= player["x"] <= 64
|
||
|
|
assert 0 <= player["y"] <= 64
|
||
|
|
assert "id" in player
|
||
|
|
|
||
|
|
|
||
|
|
def test_register_alias_endpoint():
|
||
|
|
client = TestClient(app)
|
||
|
|
res = client.post("/api/register", json={"name": "AliasBot", "color": "#FF00FF"})
|
||
|
|
assert res.status_code == 201
|
||
|
|
player = res.json()
|
||
|
|
assert player["name"] == "AliasBot"
|
||
|
|
assert 0 <= player["x"] <= 64
|
||
|
|
assert 0 <= player["y"] <= 64
|
||
|
|
|
||
|
|
|
||
|
|
def test_board_state_after_registrations():
|
||
|
|
client = TestClient(app)
|
||
|
|
client.post("/api/players", json={"name": "Bot1", "color": "#FF0000"})
|
||
|
|
client.post("/api/players", json={"name": "Bot2", "color": "#0000FF"})
|
||
|
|
|
||
|
|
res = client.get("/api/board")
|
||
|
|
assert res.status_code == 200
|
||
|
|
board = res.json()
|
||
|
|
assert board["player_count"] == 2
|
||
|
|
assert len(board["players"]) == 2
|
||
|
|
assert board["config"]["min_x"] == 0
|
||
|
|
assert board["config"]["max_x"] == 64
|
||
|
|
assert board["config"]["min_y"] == 0
|
||
|
|
assert board["config"]["max_y"] == 64
|
||
|
|
|
||
|
|
|
||
|
|
def test_player_deletion_and_reset():
|
||
|
|
client = TestClient(app)
|
||
|
|
reg_res = client.post("/api/players", json={"name": "TempBot", "color": "#123456"})
|
||
|
|
player_id = reg_res.json()["id"]
|
||
|
|
|
||
|
|
del_res = client.delete(f"/api/players/{player_id}")
|
||
|
|
assert del_res.status_code == 200
|
||
|
|
|
||
|
|
board_res = client.get("/api/board")
|
||
|
|
assert board_res.json()["player_count"] == 0
|
||
|
|
|
||
|
|
|
||
|
|
def test_validation_errors():
|
||
|
|
client = TestClient(app)
|
||
|
|
# Empty name
|
||
|
|
res = client.post("/api/players", json={"name": " ", "color": "#FF0000"})
|
||
|
|
assert res.status_code == 422
|
||
|
|
|
||
|
|
# Invalid color
|
||
|
|
res = client.post("/api/players", json={"name": "Bot", "color": "not-a-color-123456789"})
|
||
|
|
assert res.status_code == 422
|