| 1 | """Aus Fehlern lernen: eigene ? und ?? aus Partien gegen Maia werden Übungen unter „Rätsel“.""" |
| 2 | |
| 3 | import unittest |
| 4 | from unittest import mock |
| 5 | |
| 6 | import chess |
| 7 | import chess.engine |
| 8 | |
| 9 | from schach import mistakes, puzzles, repetition, scan, training |
| 10 | |
| 11 | # 1. e4 e5 2. Sf3 Sc6 3. Lc4 Sd4 4. Sxe5? (Halbzug 6) Dg5 |
| 12 | GAME = ["e2e4", "e7e5", "g1f3", "b8c6", "f1c4", "c6d4", "f3e5", "d8g5"] |
| 13 | CP = {"f3d4": 80, "e1g1": 75, "c2c3": 10, "f3e5": -150} # Bewertung nach Weiß, Stellung nach 3… Sd4 |
| 14 | |
| 15 | |
| 16 | def _game(): |
| 17 | board = chess.Board() |
| 18 | for uci in GAME: |
| 19 | board.push_uci(uci) |
| 20 | return board |
| 21 | |
| 22 | |
| 23 | class _Engine: |
| 24 | """Stockfish-Ersatz: kennt nur die Stellung nach 3… Sd4.""" |
| 25 | |
| 26 | def analyse(self, board, _limit, multipv=1): |
| 27 | infos = [] |
| 28 | for uci, cp in sorted(CP.items(), key=lambda item: -item[1]): |
| 29 | score = chess.engine.PovScore(chess.engine.Cp(cp), chess.WHITE) |
| 30 | infos.append({"pv": [chess.Move.from_uci(uci)], "score": score, "depth": 16}) |
| 31 | return infos[:multipv] |
| 32 | |
| 33 | |
| 34 | class _Worker: |
| 35 | """Engine-Warteschlange, die sofort rechnet und zurückruft.""" |
| 36 | |
| 37 | def submit(self, fn, callback): |
| 38 | callback(fn(_Engine()), None) |
| 39 | |
| 40 | |
| 41 | class LogicTest(unittest.TestCase): |
| 42 | def test_candidates_only_own_and_worst_first(self): |
| 43 | board = _game() |
| 44 | symbols = {0: "?", 2: "??", 4: "?!", 6: "?", 1: "??", 5: "?"} # ungerade: Schwarz |
| 45 | self.assertEqual(mistakes.candidates(board, chess.WHITE, symbols), [0, 2, 6]) |
| 46 | symbols[3] = "??" # jetzt hat Weiß vier, die ?? gehen vor |
| 47 | symbols[4] = "??" |
| 48 | self.assertEqual(mistakes.candidates(board, chess.WHITE, symbols), [0, 2, 4]) # 0 ist das frühere ? |
| 49 | self.assertEqual(mistakes.candidates(board, chess.BLACK, symbols), [1, 3, 5]) |
| 50 | |
| 51 | def test_position_before_the_mistake(self): |
| 52 | (item,) = mistakes.positions(_game(), [6], {6: "?"}) |
| 53 | self.assertEqual(item["last"], chess.Move.from_uci("c6d4")) |
| 54 | self.assertEqual(item["played"], chess.Move.from_uci("f3e5")) |
| 55 | self.assertEqual(item["before"].turn, chess.WHITE) |
| 56 | self.assertEqual(item["start"].turn, chess.BLACK) |
| 57 | |
| 58 | def test_almost_as_good_counts(self): |
| 59 | scores = {chess.Move.from_uci(u): v for u, v in {"a2a3": 60.0, "b2b3": 58.5, "c2c3": 57.0}.items()} |
| 60 | best, good = mistakes.accepted(scores, chess.Move.from_uci("c2c3")) |
| 61 | self.assertEqual(best.uci(), "a2a3") |
| 62 | self.assertEqual([m.uci() for m in good], ["a2a3", "b2b3"]) |
| 63 | |
| 64 | def test_cards_job(self): |
| 65 | found = mistakes.positions(_game(), [6], {6: "?"}) |
| 66 | ((key, content),) = mistakes.cards_job(found)(_Engine()) |
| 67 | self.assertEqual(content["solution"], ["f3d4"]) |
| 68 | self.assertEqual(content["accepted"], ["f3d4", "e1g1"]) # Rochade fast genauso gut, c3 nicht |
| 69 | self.assertEqual((content["last"], content["played"]), ("c6d4", "f3e5")) |
| 70 | self.assertEqual(key, repetition.position_key(found[0]["before"])) |
| 71 | |
| 72 | |
| 73 | class _Toasts: |
| 74 | def __init__(self): |
| 75 | self.titles = [] |
| 76 | |
| 77 | def add_toast(self, toast): |
| 78 | self.titles.append(toast.get_title()) |
| 79 | |
| 80 | |
| 81 | class _View: |
| 82 | flipped = False |
| 83 | |
| 84 | def animate_move(self, _board, _move): |
| 85 | pass |
| 86 | |
| 87 | |
| 88 | class _App: |
| 89 | lichess_client = None |
| 90 | |
| 91 | def side_engine(self): |
| 92 | return _Worker() |
| 93 | |
| 94 | |
| 95 | class FakeWindow(scan.ScanMixin, puzzles.PuzzleMixin): |
| 96 | def __init__(self): |
| 97 | self.mode = "maia" |
| 98 | self.board = _game() |
| 99 | self.human = chess.WHITE |
| 100 | self.symbols = {6: "?", 7: "??"} # 7: Maias Fehler zählt nicht |
| 101 | self.settings = {"scan_puzzles": False} # verpasste Chancen: tests/test_chances.py |
| 102 | self.analyses = {} |
| 103 | self.names = [] |
| 104 | self.stockfish = _Worker() |
| 105 | self.toasts = _Toasts() |
| 106 | self.view = _View() |
| 107 | self.gen = 0 |
| 108 | self.puzzle = None |
| 109 | self.badge_ply = self.browse_ply = self.hint_move = self.final_text = None |
| 110 | |
| 111 | def _foreign(self): |
| 112 | return False |
| 113 | |
| 114 | def _notation(self): |
| 115 | return "de" |
| 116 | |
| 117 | def get_application(self): |
| 118 | return _App() |
| 119 | |
| 120 | def _leave_special(self): |
| 121 | self._puzzle_leave() |
| 122 | |
| 123 | def _reset_game_tables(self): |
| 124 | self.symbols = {} |
| 125 | |
| 126 | def _changed(self): |
| 127 | self.gen += 1 |
| 128 | |
| 129 | def _refresh(self): |
| 130 | pass |
| 131 | |
| 132 | |
| 133 | class ScanTest(unittest.TestCase): |
| 134 | def setUp(self): |
| 135 | self.progress = repetition.Progress(fuzz=False) |
| 136 | patches = [ |
| 137 | mock.patch.object(training, "progress", lambda: self.progress), |
| 138 | mock.patch.object(puzzles.GLib, "timeout_add", lambda _ms, fn, *a: fn(*a)), |
| 139 | mock.patch.object(puzzles.lichess, "_background", mock.Mock()), |
| 140 | ] |
| 141 | for patch in patches: |
| 142 | patch.start() |
| 143 | self.addCleanup(patch.stop) |
| 144 | self.window = FakeWindow() |
| 145 | |
| 146 | def test_game_end_keeps_own_mistake_silently(self): |
| 147 | self.window._scan_game() |
| 148 | self.assertEqual(self.progress.puzzles_due(), 1) # gleich zu üben |
| 149 | self.assertEqual(self.window.toasts.titles, ["Aus dieser Partie: 1 Stellung zum Üben – unter „Rätsel“"]) |
| 150 | self.window._scan_game() # dieselbe Partie noch einmal: nichts doppelt |
| 151 | self.assertEqual(self.progress.puzzles_due(), 1) |
| 152 | self.assertEqual(len(self.window.toasts.titles), 1) |
| 153 | |
| 154 | def test_switched_off(self): |
| 155 | self.window.settings["scan_mistakes"] = False |
| 156 | self.window._scan_game() |
| 157 | self.assertEqual(self.progress.puzzles_due(), 0) |
| 158 | |
| 159 | def test_only_games_against_maia(self): |
| 160 | self.window.mode = "duo" |
| 161 | self.window._scan_game() |
| 162 | self.assertEqual(self.progress.puzzles_due(), 0) |
| 163 | |
| 164 | def _practise(self): |
| 165 | self.window._scan_game() |
| 166 | self.window.start_puzzle() |
| 167 | self.assertEqual(self.window.board.peek(), chess.Move.from_uci("c6d4")) # Maias Zug vorgespielt |
| 168 | self.assertEqual(self.window._puzzle_subtitle(), "Aus deiner Partie · Weiß am Zug") |
| 169 | |
| 170 | def test_good_alternative_solves_it(self): |
| 171 | self._practise() |
| 172 | self.window._puzzle_play(chess.Move.from_uci("e1g1")) # nicht der beste, aber fast so gut |
| 173 | self.assertTrue(self.window.final_text.startswith("Gelöst! In deiner Partie kam hier 4. Sxe5?.")) |
| 174 | self.assertEqual(self.progress.puzzles_due(), 0) |
| 175 | |
| 176 | def test_same_mistake_again_is_again(self): |
| 177 | self._practise() |
| 178 | self.window._puzzle_play(chess.Move.from_uci("f3e5")) # wie in der Partie |
| 179 | self.window._puzzle_take_back(self.window.gen, 6) |
| 180 | self.window._puzzle_play(chess.Move.from_uci("f3d4")) |
| 181 | self.assertIn("Das Rätsel wiederholen wir gleich", self.window.final_text) |
| 182 | |
| 183 | |
| 184 | if __name__ == "__main__": |
| 185 | unittest.main() |