M9: FastAPI game API — new-game, state, actions
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
61
backend/tests/test_api.py
Normal file
61
backend/tests/test_api.py
Normal file
@@ -0,0 +1,61 @@
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from tft.api import app as app_mod
|
||||
from tft.constants.loader import load_constants
|
||||
from tft.model import artifact as artifact_mod
|
||||
from tests.test_sim import make_static
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client(monkeypatch):
|
||||
static = make_static()
|
||||
static["meta"]["patch"] = "test"
|
||||
art = artifact_mod.build(static)
|
||||
cfg = load_constants(17)
|
||||
monkeypatch.setattr(app_mod, "_load_game_deps", lambda: (art, cfg))
|
||||
return TestClient(app_mod.app)
|
||||
|
||||
|
||||
def test_full_game_via_api(client):
|
||||
state = client.post("/api/game", params={"seed": 3}).json()
|
||||
game_id = state["game_id"]
|
||||
assert state["round"]["label"] == "1-1"
|
||||
assert state["player"]["hp"] == 100
|
||||
assert len(state["opponents"]) == 7
|
||||
|
||||
steps = 0
|
||||
while not state["over"]:
|
||||
if state["augment_offer"]:
|
||||
client.post(f"/api/game/{game_id}/action",
|
||||
json={"action": "pick_augment", "choice": 0})
|
||||
affordable = [i for i, s in enumerate(state["shop"])
|
||||
if s and s["cost"] <= state["player"]["gold"]]
|
||||
if affordable:
|
||||
client.post(f"/api/game/{game_id}/action",
|
||||
json={"action": "buy", "slot": affordable[0]})
|
||||
resp = client.post(f"/api/game/{game_id}/action", json={"action": "next"})
|
||||
assert resp.status_code == 200
|
||||
state = resp.json()
|
||||
steps += 1
|
||||
assert steps < 100
|
||||
assert 1 <= state["placement"] <= 8
|
||||
|
||||
|
||||
def test_invalid_action_is_400(client):
|
||||
game_id = client.post("/api/game", params={"seed": 1}).json()["game_id"]
|
||||
resp = client.post(f"/api/game/{game_id}/action",
|
||||
json={"action": "sell", "where": "bench", "idx": 99})
|
||||
assert resp.status_code == 400
|
||||
resp = client.post(f"/api/game/{game_id}/action", json={"action": "dance"})
|
||||
assert resp.status_code == 400
|
||||
assert client.post("/api/game/nope/action", json={"action": "next"}).status_code == 404
|
||||
|
||||
|
||||
def test_state_shape(client):
|
||||
state = client.post("/api/game", params={"seed": 5}).json()
|
||||
for key in ("round", "player", "board", "bench", "shop", "traits",
|
||||
"opponents", "items", "over", "log"):
|
||||
assert key in state
|
||||
assert state["player"]["score"] == 0.0
|
||||
assert all(o["board"] is not None for o in state["opponents"])
|
||||
Reference in New Issue
Block a user