Backend:
- prompts.py: 5 personality presets (уличный_гопник default, профессор,
поэт, молчун, шут). Each provides voice + label + description.
build_system_prompt accepts `preset` parameter; the MCP-tools section
+ must-call-move rule is shared common tail.
- coordinator: `start_new_game` now accepts `overrides` ({white: {agent,
model, prompt}, black: {...}}) — missing fields filled randomly.
MODEL_OPTIONS published per agent.
- AppState.prompts tracks the preset id per side; persisted into game's
agents dict and PGN.
- AppState.eval_cache cleared on new_game (was per-life-of-process before).
- persistence.save_game now stores full history (with comments) and the
per-position eval cache in the game JSON.
- ChessGame.abort() — manual game termination with result='*' and
Termination='aborted'.
- New endpoints:
• GET /api/options — agents, models per agent, prompt presets.
• GET /api/stats — leaderboard aggregated from games/*.json:
per (agent, model) and per persona, with score = wins + 0.5*draws.
• POST /api/abort-game — force-end current game.
- POST /api/new-game now returns 409 when a game is in progress.
- GET /api/games[/{id}[/pgn]] also 409 while in progress — gallery is
hidden behind the one-game-at-a-time rule.
Frontend:
- New setup dialog: per-side agent / model / persona selects, swap +
random shortcuts, persona description preview, abort button inline
when current game blocks the start.
- 'games & stats' link in the board footer; greyed out while a game is
active.
- /games page reworked: leaderboard (player × model + by persona),
games table with persona labels, replay panel showing eval per ply
with prev/next/first/last + arrow-key nav.
- chess.js loadPgn → load_pgn fix consolidated; replay prefers saved
history (comments + side) over PGN parse when available.
164 lines
5.5 KiB
Python
164 lines
5.5 KiB
Python
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"] == []
|