"""Identity is the load-bearing part: a memory slot must follow the same piece. If `advance_ids` drifts, every arm still trains and every number still prints -- the memory just follows the wrong piece. That failure is silent, so it gets the test. """ import chess import chess.pgn import pytest import chess_domain def _game(fen: str | None, sans: list[str]) -> chess.pgn.Game: board = chess.Board(fen) if fen else chess.Board() for san in sans: board.push_san(san) return chess.pgn.Game.from_board(board) CASTLE_EP = ["e4", "e5", "Nf3", "Nf6", "Bc4", "Bc5", "O-O", "O-O", "d4", "exd4", "e5", "d5", "exd6"] def test_rook_keeps_its_id_through_castling(): plies = chess_domain.plies(_game(None, CASTLE_EP)) h1 = chess.H1 after = next(p for p in plies if h1 not in # h1 empty: rook has moved [chess.square(int(c[0] * 7), int(c[1] * 7)) for c in p.coords]) f1_row = [k for k, c in enumerate(after.coords) if chess.square(int(c[0] * 7), int(c[1] * 7)) == chess.F1][0] assert after.ids[f1_row] == h1, "the castled rook was handed a new identity" def test_promoted_pawn_keeps_its_id_and_changes_type(): plies = chess_domain.plies(_game("1r4k1/P7/8/8/8/8/8/6K1 w - - 0 1", ["axb8=Q", "Kf7"])) row = plies[-1].ids.index(chess.A7) # the pawn that started on a7 coords = plies[-1].coords[row] assert chess.square(int(coords[0] * 7), int(coords[1] * 7)) == chess.B8 assert plies[-1].attrs[row][chess.QUEEN - 1] == 1.0, "promotion did not change the type" def test_the_identity_assertion_can_actually_fire(monkeypatch): """Sabotage: stop tracking captures, and the board must disagree with the map.""" monkeypatch.setattr(chess_domain, "advance_ids", lambda board, move, piece, id_at: id_at.update( {move.to_square: id_at.pop(move.from_square, move.from_square)})) with pytest.raises(AssertionError, match="drifted"): chess_domain.plies(_game(None, CASTLE_EP))