import pytest from ai_chess.chess_game import ChessGame from ai_chess.mcp_server import build_tools_for_side from ai_chess.settings import Settings from ai_chess.sides import Side from ai_chess.state import AppState def fresh_state() -> AppState: s = AppState() s.game = ChessGame(game_id="t1") return s @pytest.mark.asyncio async def test_get_board(): s = fresh_state() tools = build_tools_for_side(Side.WHITE, s) res = await tools["get_board"]({}) assert "ascii" in res and "fen" in res assert res["side_to_move"] == "white" @pytest.mark.asyncio async def test_move_legal(): s = fresh_state() tools = build_tools_for_side(Side.WHITE, s) res = await tools["move"]({"move": "e4", "comment": "го"}) assert res["ok"] is True assert res["san"] == "e4" @pytest.mark.asyncio async def test_move_not_your_turn(): s = fresh_state() tools = build_tools_for_side(Side.BLACK, s) res = await tools["move"]({"move": "e5"}) assert res["ok"] is False assert res["reason"] == "not_your_turn" @pytest.mark.asyncio async def test_move_illegal_default_off(): s = fresh_state() tools = build_tools_for_side(Side.WHITE, s) res = await tools["move"]({"move": "e2e7"}) assert res["ok"] is False assert res["reason"] == "illegal_move" @pytest.mark.asyncio async def test_move_illegal_allowed_when_flag_on(): s = fresh_state() s.settings = Settings(allow_illegal_moves=True) tools = build_tools_for_side(Side.WHITE, s) res = await tools["move"]({"move": "e2e7"}) assert res["ok"] is True assert res["illegal"] is True @pytest.mark.asyncio async def test_resign_disabled(): s = fresh_state() s.settings = Settings(allow_draw_resign=False) tools = build_tools_for_side(Side.WHITE, s) res = await tools["resign"]({}) assert res["ok"] is False assert res["reason"] == "resign_disabled" @pytest.mark.asyncio async def test_resign_ok_then_game_over(): s = fresh_state() tools = build_tools_for_side(Side.WHITE, s) res = await tools["resign"]({"comment": "сдаюсь"}) assert res["ok"] is True assert s.game.result == "0-1" @pytest.mark.asyncio async def test_draw_offer_and_accept(): s = fresh_state() s.game.try_move(Side.WHITE, "e4") tools_b = build_tools_for_side(Side.BLACK, s) r1 = await tools_b["offer_draw"]({"comment": "ничья?"}) assert r1["ok"] is True tools_w = build_tools_for_side(Side.WHITE, s) r2 = await tools_w["accept_draw"]({}) assert r2["ok"] is True assert s.game.result == "1/2-1/2"