| 1 | import random |
| 2 | import unittest |
| 3 | |
| 4 | import chess |
| 5 | import chess.engine |
| 6 | |
| 7 | from schach import engines, lichess, wdl |
| 8 | |
| 9 | |
| 10 | class FromWhitePctTest(unittest.TestCase): |
| 11 | def test_stockfish_bar_has_no_draw(self): |
| 12 | w, d, l = wdl.from_white_pct(70.0) |
| 13 | self.assertAlmostEqual(w, 0.7) |
| 14 | self.assertEqual(d, 0.0) |
| 15 | self.assertAlmostEqual(l, 0.3) |
| 16 | self.assertEqual(wdl.from_white_pct(100.0), (1.0, 0.0, 0.0)) |
| 17 | self.assertIsNone(wdl.from_white_pct(None)) |
| 18 | |
| 19 | |
| 20 | class GameRatingTest(unittest.TestCase): |
| 21 | def test_most_played_game_perf_wins_puzzles_ignored(self): |
| 22 | account = {"perfs": {"blitz": {"games": 40, "rating": 1310}, "rapid": {"games": 120, "rating": 1405}, |
| 23 | "puzzle": {"games": 900, "rating": 1800}}} |
| 24 | self.assertEqual(lichess.game_rating(account), 1405) |
| 25 | |
| 26 | def test_no_games(self): |
| 27 | self.assertIsNone(lichess.game_rating({"perfs": {"blitz": {"games": 0, "rating": 1500}}})) |
| 28 | self.assertIsNone(lichess.game_rating(None)) |
| 29 | |
| 30 | |
| 31 | class FakeMaia: |
| 32 | def __init__(self): |
| 33 | self.options = None |
| 34 | |
| 35 | def analyse(self, board, limit, multipv, options): |
| 36 | self.options = options |
| 37 | return [{"wdl": chess.engine.PovWdl(chess.engine.Wdl(500, 200, 300), board.turn)}] |
| 38 | |
| 39 | |
| 40 | class MaiaWdlJobTest(unittest.TestCase): |
| 41 | def test_self_elo_is_side_to_move(self): |
| 42 | board = chess.Board() |
| 43 | board.push_uci("e2e4") # Schwarz am Zug |
| 44 | engine = FakeMaia() |
| 45 | result = engines.maia_wdl(board, 1400, 800)(engine) |
| 46 | self.assertEqual((engine.options["SelfElo"], engine.options["OppoElo"]), (800, 1400)) |
| 47 | self.assertEqual(result, (0.3, 0.2, 0.5)) # aus Sicht von Weiß |
| 48 | |
| 49 | def test_other_jobs_set_both_elos(self): |
| 50 | engine = FakeMaia() |
| 51 | engines.maia_candidates(chess.Board(), 1100)(engine) |
| 52 | self.assertEqual((engine.options["SelfElo"], engine.options["OppoElo"]), (1100, 1100)) |
| 53 | self.assertNotIn("Elo", engine.options) |
| 54 | |
| 55 | |
| 56 | class PovTest(unittest.TestCase): |
| 57 | def test_black_to_move_is_flipped(self): |
| 58 | self.assertEqual(wdl.from_pov((700, 200, 100), chess.BLACK), (0.1, 0.2, 0.7)) |
| 59 | |
| 60 | def test_pov_wdl(self): |
| 61 | pov = chess.engine.PovWdl(chess.engine.Wdl(600, 300, 100), chess.BLACK) |
| 62 | self.assertEqual(wdl.from_pov(pov, chess.BLACK), (0.1, 0.3, 0.6)) |
| 63 | |
| 64 | |
| 65 | class OutcomeTest(unittest.TestCase): |
| 66 | def test_mate_and_stalemate(self): |
| 67 | mate = chess.Board("7k/6Q1/6K1/8/8/8/8/8 b - - 0 1") |
| 68 | self.assertEqual(wdl.outcome(mate), (1.0, 0.0, 0.0)) |
| 69 | stalemate = chess.Board("7k/5Q2/6K1/8/8/8/8/8 b - - 0 1") |
| 70 | self.assertEqual(wdl.outcome(stalemate), (0.0, 1.0, 0.0)) |
| 71 | self.assertIsNone(wdl.outcome(chess.Board())) |
| 72 | |
| 73 | |
| 74 | class DrawOfferTest(unittest.TestCase): |
| 75 | def test_worse_side_accepts_better_side_declines(self): |
| 76 | rng = random.Random(1) |
| 77 | black_better = (0.1, 0.2, 0.7) |
| 78 | self.assertTrue(all(wdl.maia_accepts_draw(black_better, chess.WHITE, rng) for _ in range(50))) |
| 79 | self.assertFalse(any(wdl.maia_accepts_draw(black_better, chess.BLACK, rng) for _ in range(50))) |
| 80 | |
| 81 | def test_balanced_position_is_a_coin_flip(self): |
| 82 | rng = random.Random(2) |
| 83 | answers = [wdl.maia_accepts_draw((0.3, 0.4, 0.3), chess.BLACK, rng) for _ in range(400)] |
| 84 | self.assertTrue(100 < sum(answers) < 300) |
| 85 | |
| 86 | |
| 87 | if __name__ == "__main__": |
| 88 | unittest.main() |