pre-push commit
This commit is contained in:
78
tests/test_api_basic.py
Normal file
78
tests/test_api_basic.py
Normal file
@@ -0,0 +1,78 @@
|
||||
import os, json, tempfile, shutil
|
||||
import pytest
|
||||
from battleflow import create_app
|
||||
from battleflow.state import STATE
|
||||
from battleflow.storage import ensure_default_icons, load_state
|
||||
|
||||
TOKEN = "testtoken"
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def isolated_data(tmp_path, monkeypatch):
|
||||
# fresh state for each test
|
||||
data_dir = tmp_path / "bfdata"
|
||||
data_dir.mkdir()
|
||||
monkeypatch.setenv("BATTLEFLOW_DATA_DIR", str(data_dir))
|
||||
monkeypatch.setenv("BATTLEFLOW_TOKEN", TOKEN)
|
||||
# reset STATE
|
||||
STATE.actors.clear(); STATE.turn_idx=0; STATE.round=1; STATE.visible=True; STATE.dead_mode='normal'
|
||||
app = create_app()
|
||||
with app.app_context():
|
||||
ensure_default_icons()
|
||||
load_state(STATE)
|
||||
yield app
|
||||
|
||||
@pytest.fixture
|
||||
def client(isolated_data):
|
||||
isolated_data.testing = True
|
||||
return isolated_data.test_client()
|
||||
|
||||
def url(p):
|
||||
sep = "&" if "?" in p else "?"
|
||||
return f"{p}{sep}token={TOKEN}"
|
||||
|
||||
def test_state_empty(client):
|
||||
r = client.get(url("/api/state?since=0"))
|
||||
assert r.status_code == 200
|
||||
data = r.get_json()
|
||||
assert data["actors"] == []
|
||||
assert data["round"] == 1
|
||||
|
||||
def test_add_and_cycle(client):
|
||||
# add one actor
|
||||
r = client.post(url("/api/add"), json={"name":"A","init":15,"type":"pc","hp":10,"ac":14})
|
||||
assert r.status_code == 200
|
||||
# state should include it
|
||||
r = client.get(url("/api/state?since=0"))
|
||||
s = r.get_json()
|
||||
assert len(s["actors"]) == 1
|
||||
assert s["actors"][0]["name"] == "A"
|
||||
# next/prev/visible
|
||||
assert client.post(url("/api/next")).status_code == 200
|
||||
assert client.post(url("/api/prev")).status_code == 200
|
||||
assert client.post(url("/api/toggle_visible")).status_code == 200
|
||||
|
||||
def test_effects_and_deadmode(client):
|
||||
# add
|
||||
r = client.post(url("/api/add"), json={"name":"B","init":12,"type":"npc"})
|
||||
aid = r.get_json()["id"]
|
||||
# toggle effect
|
||||
assert client.post(url("/api/toggle_effect"), json={"id":aid,"effect":"poisoned"}).status_code == 200
|
||||
# clear effects
|
||||
assert client.post(url("/api/clear_effects"), json={"id":aid}).status_code == 200
|
||||
# dead mode
|
||||
assert client.post(url("/api/dead_mode"), json={"mode":"shrink"}).status_code == 200
|
||||
assert client.post(url("/api/dead_mode"), json={"mode":"hide"}).status_code == 200
|
||||
|
||||
def test_presets_and_groups(client):
|
||||
# add presets
|
||||
p1 = client.post(url("/api/preset/add"), json={"name":"Fighter","type":"pc","hp":30,"ac":18}).get_json()["id"]
|
||||
p2 = client.post(url("/api/preset/add"), json={"name":"Goblin","type":"monster","hp":7,"ac":15}).get_json()["id"]
|
||||
# apply preset (asks for init)
|
||||
client.post(url("/api/preset/apply"), json={"id":p1,"init":14})
|
||||
# create group
|
||||
g = client.post(url("/api/preset/group/add"), json={"name":"Party","member_ids":[p1,p2]}).get_json()["id"]
|
||||
# apply group with single init
|
||||
client.post(url("/api/preset/group/apply"), json={"id":g,"init":10})
|
||||
# state should have at least 3 actors now
|
||||
s = client.get(url("/api/state?since=0")).get_json()
|
||||
assert len(s["actors"]) >= 3
|
||||
Reference in New Issue
Block a user