Files
ai-chess/tests/test_app.py
T
ai-chess 83e2fb8765 feat(app): FastAPI REST + WS + MCP HTTP transport
Implements build_app() with REST endpoints (/api/state, /api/games,
/api/settings, /api/new-game, /api/agent-binding), WebSocket /ws for
realtime events, and custom JSON-RPC MCP transport at /mcp/{side}
supporting initialize, tools/list, and tools/call with proper
content-wrapping and isError flag. Static UI mount guarded by
PUBLIC_DIR existence check. All 5 e2e tests pass.
2026-05-14 23:18:57 +03:00

76 lines
2.4 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