- mcp_post: notifications (no `id` field) → HTTP 202 with empty body, per streamable-HTTP MCP transport spec. Fixes rmcp 'transport channel closed' on codex's notifications/initialized. - start.sh: don't attach to tmux; print URL only. Backend + tmux session run in background. UI is the only viewing surface. - scripts/stop.sh: convenience teardown.
101 lines
3.4 KiB
Python
101 lines
3.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
|
|
|
|
|
|
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
|