testsAndMisc/PYTHON/lichess_bot/tests/test_puzzles.py
Krzysztof kuhy Rudnicki 3ac56e541e Enable D100-D107 docstring rules: add docstrings to all modules, classes, methods, and functions
- Added module docstrings to 19 Python files
- Added class docstrings to 5 classes (ScreenLocker, PokerModifierApp, etc.)
- Added method docstrings to 22 methods
- Added function docstrings to 25 functions
- Added __init__ docstrings to 5 classes
- Removed D100-D107 from ruff ignore list (docstrings now enforced)
- Removed deprecated ANN101, ANN102, UP038 rules from ignore list
- Fixed UP038: use union types in isinstance() calls
- All ruff checks now pass with full docstring enforcement
2025-11-30 14:45:55 +01:00

78 lines
2.5 KiB
Python

"""Test the engine against Lichess puzzles."""
import csv
import os
import chess
import pytest
from PYTHON.lichess_bot.engine import RandomEngine
def _load_top_puzzles(csv_path: str, limit: int = 8) -> list[tuple[str, str]]:
"""Return a list of (FEN, solution_moves_str) for the first `limit` rows in the CSV.
CSV columns: PuzzleId,FEN,Moves,...
"""
puzzles: list[tuple[str, str]] = []
with open(csv_path, newline="", encoding="utf-8") as f:
reader = csv.DictReader(f)
for row in reader:
fen = row["FEN"].strip()
moves = row["Moves"].strip()
if fen and moves:
puzzles.append((fen, moves))
if len(puzzles) >= limit:
break
return puzzles
@pytest.mark.parametrize(
("fen", "moves_str"),
_load_top_puzzles(
os.path.join(os.path.dirname(__file__), "lichess_db_puzzle.csv"), limit=8
),
)
def test_puzzle_engine_follow_solution(fen: str, moves_str: str):
"""Verify the engine follows puzzle solutions correctly."""
board = chess.Board(fen)
eng = RandomEngine(max_time_sec=1.0)
# Moves are space-separated UCIs alternating sides
# starting from side-to-move in the FEN
solution_moves = moves_str.split()
step = 0
for uci in solution_moves:
step += 1
# Engine move on this ply
mv, expl = eng.choose_move_with_explanation(board, time_budget_sec=0.5)
assert mv is not None, f"No move returned at step {step}.\nExplanation: {expl}"
# If engine move differs from solution, fail immediately
# but provide analysis of the correct move
if mv.uci() != uci:
# Ask the engine to analyze the correct move for debug
score_cp, proposed_expl, best_mv, best_expl = (
eng.evaluate_proposed_move_with_suggestion(
board, uci, time_budget_sec=0.5
)
)
details = [
f"Puzzle failed at step {step}.",
f"FEN: {fen}",
f"Expected: {uci}",
f"Engine played: {mv.uci()}",
"--- engine explanation ---",
expl,
"--- analysis of expected move ---",
f"score_cp: {score_cp}",
proposed_expl,
]
if best_mv is not None:
details.append("--- engine best move analysis ---")
details.append(best_expl)
pytest.fail("\n".join(details))
# Apply the move and continue
board.push(mv)