rewrite: TS monorepo — pi SDK stateless agents, Postgres+pg-boss, SvelteKit themed UI; legacy moved to legacy/
This commit is contained in:
@@ -0,0 +1,163 @@
|
||||
import asyncio
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
import ai_chess.state as state_mod
|
||||
from ai_chess.app import build_app
|
||||
from ai_chess.chess_game import ChessGame
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client(tmp_path, monkeypatch):
|
||||
# Use a fresh AppState per test
|
||||
monkeypatch.setattr(state_mod, "GAMES_DIR", tmp_path / "games")
|
||||
monkeypatch.setattr(state_mod, "CONFIG_DIR", tmp_path / "config")
|
||||
monkeypatch.setattr(state_mod, "LOGS_DIR", tmp_path / "logs")
|
||||
s = state_mod.AppState()
|
||||
s.game = ChessGame(game_id="t1")
|
||||
monkeypatch.setattr(state_mod, "state", s)
|
||||
app = build_app(s)
|
||||
return TestClient(app)
|
||||
|
||||
|
||||
def test_state_endpoint(client):
|
||||
r = client.get("/api/state")
|
||||
assert r.status_code == 200
|
||||
body = r.json()
|
||||
assert body["game"]["fen"].startswith("rnbqkbnr")
|
||||
assert body["game"]["side_to_move"] == "white"
|
||||
|
||||
|
||||
def test_settings_get_post(client):
|
||||
r = client.get("/api/settings")
|
||||
assert r.status_code == 200
|
||||
assert r.json()["allow_draw_resign"] is True
|
||||
|
||||
r = client.post("/api/settings", json={"allow_illegal_moves": True})
|
||||
assert r.status_code == 200
|
||||
assert r.json()["allow_illegal_moves"] is True
|
||||
|
||||
|
||||
def test_mcp_initialize(client):
|
||||
r = client.post("/mcp/white", json={
|
||||
"jsonrpc": "2.0", "id": 1, "method": "initialize",
|
||||
"params": {"protocolVersion": "2025-06-18", "capabilities": {}, "clientInfo": {"name": "test", "version": "0"}},
|
||||
})
|
||||
assert r.status_code == 200
|
||||
body = r.json()
|
||||
assert body["result"]["serverInfo"]["name"].startswith("ai-chess")
|
||||
|
||||
|
||||
def test_mcp_tools_list_and_call(client):
|
||||
r = client.post("/mcp/white", json={
|
||||
"jsonrpc": "2.0", "id": 2, "method": "tools/list", "params": {},
|
||||
})
|
||||
names = [t["name"] for t in r.json()["result"]["tools"]]
|
||||
assert "move" in names and "resign" in names
|
||||
|
||||
r = client.post("/mcp/white", json={
|
||||
"jsonrpc": "2.0", "id": 3, "method": "tools/call",
|
||||
"params": {"name": "move", "arguments": {"move": "e4", "comment": "го"}},
|
||||
})
|
||||
body = r.json()
|
||||
content = body["result"]["content"][0]["text"]
|
||||
assert '"ok": true' in content or '"ok":true' in content
|
||||
assert "e4" in content
|
||||
|
||||
|
||||
def test_mcp_wrong_side_blocked(client):
|
||||
r = client.post("/mcp/black", json={
|
||||
"jsonrpc": "2.0", "id": 4, "method": "tools/call",
|
||||
"params": {"name": "move", "arguments": {"move": "e5"}},
|
||||
})
|
||||
text = r.json()["result"]["content"][0]["text"]
|
||||
assert "not_your_turn" in text
|
||||
|
||||
|
||||
def test_mcp_notification_returns_202_no_body(client):
|
||||
# JSON-RPC notification (no "id" key) MUST return 202 with empty body —
|
||||
# otherwise rmcp (codex's MCP client) treats 200+body as an unexpected
|
||||
# response and closes the transport.
|
||||
r = client.post("/mcp/white", json={
|
||||
"jsonrpc": "2.0", "method": "notifications/initialized", "params": {},
|
||||
})
|
||||
assert r.status_code == 202
|
||||
assert r.content == b""
|
||||
|
||||
|
||||
def test_mcp_batch_with_notification(client):
|
||||
# Batch with one request + one notification — response should contain
|
||||
# only the request's reply.
|
||||
r = client.post("/mcp/white", json=[
|
||||
{"jsonrpc": "2.0", "id": 5, "method": "tools/list", "params": {}},
|
||||
{"jsonrpc": "2.0", "method": "notifications/initialized", "params": {}},
|
||||
])
|
||||
assert r.status_code == 200
|
||||
body = r.json()
|
||||
assert isinstance(body, list)
|
||||
assert len(body) == 1
|
||||
assert body[0]["id"] == 5
|
||||
|
||||
|
||||
def test_options_lists_models_and_prompts(client):
|
||||
r = client.get("/api/options")
|
||||
assert r.status_code == 200
|
||||
b = r.json()
|
||||
assert "claude" in b["agents"] and "codex" in b["agents"]
|
||||
assert "opus" in b["models"]["claude"]
|
||||
assert "gpt-5.5" in b["models"]["codex"]
|
||||
prompt_ids = [p["id"] for p in b["prompts"]]
|
||||
assert "уличный_гопник" in prompt_ids
|
||||
assert "профессор" in prompt_ids
|
||||
|
||||
|
||||
def test_new_game_accepts_overrides(client):
|
||||
# Abort the fixture's t1 game first — one game at a time.
|
||||
client.post("/api/abort-game")
|
||||
r = client.post("/api/new-game", json={"overrides": {
|
||||
"white": {"agent": "claude", "model": "sonnet", "prompt": "профессор"},
|
||||
"black": {"agent": "codex", "model": "gpt-5.4", "prompt": "шут"},
|
||||
}})
|
||||
assert r.status_code == 200
|
||||
g = r.json()
|
||||
a = g["agents"]
|
||||
assert a["white"]["agent"] == "claude"
|
||||
assert a["white"]["model"] == "sonnet"
|
||||
assert a["white"]["prompt"] == "профессор"
|
||||
assert a["black"]["agent"] == "codex"
|
||||
assert a["black"]["model"] == "gpt-5.4"
|
||||
assert a["black"]["prompt"] == "шут"
|
||||
|
||||
|
||||
def test_new_game_409_when_current_in_progress(client):
|
||||
# First call: t1 game is active (no result), need to finish it first.
|
||||
r = client.post("/api/new-game")
|
||||
assert r.status_code == 409
|
||||
|
||||
|
||||
def test_abort_game_then_new(client):
|
||||
r = client.post("/api/abort-game")
|
||||
assert r.status_code == 200
|
||||
assert r.json()["ok"] is True
|
||||
r2 = client.post("/api/new-game")
|
||||
assert r2.status_code == 200
|
||||
|
||||
|
||||
def test_games_endpoints_blocked_when_in_progress(client):
|
||||
# t1 is in progress per fixture
|
||||
assert client.get("/api/games").status_code == 409
|
||||
assert client.get("/api/games/anything").status_code == 409
|
||||
assert client.get("/api/games/anything/pgn").status_code == 409
|
||||
# Abort and they become accessible
|
||||
client.post("/api/abort-game")
|
||||
assert client.get("/api/games").status_code == 200
|
||||
|
||||
|
||||
def test_stats_empty_when_no_games(client):
|
||||
r = client.get("/api/stats")
|
||||
assert r.status_code == 200
|
||||
b = r.json()
|
||||
assert b["total_games"] == 0
|
||||
assert b["players"] == []
|
||||
assert b["prompts"] == []
|
||||
Reference in New Issue
Block a user