| 1 | """Kursmodus: Was sitzt, spielt das Programm vor; gefragt wird, was heute dran ist.""" |
| 2 | |
| 3 | import datetime |
| 4 | import unittest |
| 5 | from unittest import mock |
| 6 | |
| 7 | import chess |
| 8 | |
| 9 | from schach import opening, repetition, training |
| 10 | |
| 11 | COURSE = """[Event "Mini"] |
| 12 | [SchachColor "white"] |
| 13 | |
| 14 | 1. e4 e5 2. Nf3 Nc6 3. Bc4 Bc5 4. c3 Nf6 * |
| 15 | """ |
| 16 | |
| 17 | |
| 18 | class _View: |
| 19 | flipped = False |
| 20 | |
| 21 | def animate_move(self, _board, _move): |
| 22 | pass |
| 23 | |
| 24 | |
| 25 | class _Toggle: |
| 26 | def set_active(self, _value): |
| 27 | pass |
| 28 | |
| 29 | |
| 30 | class _NoNames: |
| 31 | def name(self, _board): |
| 32 | return "" |
| 33 | |
| 34 | |
| 35 | class FakeWindow(training.OpeningMixin): |
| 36 | def __init__(self): |
| 37 | self._opening_init() |
| 38 | self.mode = "opening" |
| 39 | self.gen = 0 |
| 40 | self.view = _View() |
| 41 | self.list_btn = _Toggle() |
| 42 | self.messages = [] |
| 43 | self.symbols = {} |
| 44 | self.takebacks = {} |
| 45 | self.final_text = None |
| 46 | self.badge_ply = None |
| 47 | self.browse_ply = None |
| 48 | |
| 49 | def _leave_special(self): |
| 50 | pass |
| 51 | |
| 52 | def _reset_game_tables(self): |
| 53 | pass |
| 54 | |
| 55 | def _notation(self): |
| 56 | return "de" |
| 57 | |
| 58 | def _coach(self, markup, ply=None, replace=False, shapes=(), demos=()): |
| 59 | self.messages.append(markup) |
| 60 | |
| 61 | def _changed(self): |
| 62 | self.gen += 1 |
| 63 | |
| 64 | def _refresh(self): |
| 65 | pass |
| 66 | |
| 67 | def _refresh_chrome(self): |
| 68 | pass |
| 69 | |
| 70 | def _coach_show(self): |
| 71 | pass |
| 72 | |
| 73 | |
| 74 | class RetryTest(unittest.TestCase): |
| 75 | def setUp(self): |
| 76 | self.course = opening.parse_course(COURSE) |
| 77 | self.progress = repetition.Progress(fuzz=False) |
| 78 | patches = [ |
| 79 | mock.patch.object(training, "progress", lambda: self.progress), |
| 80 | # Zeitgeber sofort ausführen |
| 81 | mock.patch.object(training.GLib, "timeout_add", lambda _ms, fn, *a: fn(*a)), |
| 82 | mock.patch.object(training, "_book", lambda: None), # Kurszüge braucht kein Buch |
| 83 | mock.patch.object(training, "_names", lambda: _NoNames()), |
| 84 | ] |
| 85 | for patch in patches: |
| 86 | patch.start() |
| 87 | self.addCleanup(patch.stop) |
| 88 | self.window = FakeWindow() |
| 89 | |
| 90 | def board_after(self, ucis=""): |
| 91 | board = self.course.start() |
| 92 | for uci in ucis.split(): |
| 93 | board.push_uci(uci) |
| 94 | return board |
| 95 | |
| 96 | def test_fresh_line_starts_at_the_beginning(self): |
| 97 | self.window.start_opening(self.course) |
| 98 | self.assertEqual(len(self.window.board.move_stack), 0) |
| 99 | self.assertIsNotNone(self.window.decision) # wartet auf den eigenen Zug |
| 100 | |
| 101 | def test_known_moves_are_played_up_to_the_due_one(self): |
| 102 | # sitzt: 1. e4, 2. Sf3, 3. Lc4 – heute „Nochmal“ bei 4. c3 (Halbzug 6) |
| 103 | for ucis in ("", "e2e4 e7e5", "e2e4 e7e5 g1f3 b8c6"): |
| 104 | self.progress.record(self.course, self.board_after(ucis), repetition.GOOD) |
| 105 | self.progress.record(self.course, self.board_after("e2e4 e7e5 g1f3 b8c6 f1c4 f8c5"), repetition.AGAIN) |
| 106 | self.window.start_opening(self.course) |
| 107 | board = self.window.board |
| 108 | self.assertEqual(len(board.move_stack), 6) # direkt vor dem Fehler von eben |
| 109 | self.assertEqual(board.turn, chess.WHITE) |
| 110 | self.assertFalse(self.window.opening_busy) |
| 111 | self.assertIsNotNone(self.window.decision) |
| 112 | self.assertIn("Zug 4", self.window.messages[-1]) |
| 113 | |
| 114 | def test_later_known_move_is_played_for_you_and_due_one_is_recorded(self): |
| 115 | long_ago = datetime.datetime.now() - datetime.timedelta(days=90) |
| 116 | for ucis in ("", "e2e4 e7e5", "e2e4 e7e5 g1f3 b8c6 f1c4 f8c5"): |
| 117 | self.progress.record(self.course, self.board_after(ucis), repetition.GOOD) |
| 118 | bc4 = self.board_after("e2e4 e7e5 g1f3 b8c6") |
| 119 | self.progress.record(self.course, bc4, repetition.GOOD, now=long_ago) # längst fällig |
| 120 | self.window.start_opening(self.course) |
| 121 | self.assertEqual(len(self.window.board.move_stack), 4) |
| 122 | self.window._opening_play(chess.Move.from_uci("f1c4")) |
| 123 | # Gegner 3… Lc5, dann 4. c3 vorgespielt (sitzt), 4… Sf6 – Linie zu Ende |
| 124 | self.assertEqual(len(self.window.board.move_stack), 8) |
| 125 | self.assertEqual([grade for grade, _days in self.window.course_records], [repetition.GOOD]) |
| 126 | self.assertIn("nächste Wiederholung", self.window.final_text) |
| 127 | self.assertGreater(self.progress.card(self.course, bc4)["last_review"], long_ago.isoformat()) |
| 128 | |
| 129 | def test_course_takeback_is_kept_for_the_pgn(self): |
| 130 | self.window.start_opening(self.course) |
| 131 | self.window.board.push_san("d4") # kein Kurszug |
| 132 | self.window.symbols[0] = "?!" |
| 133 | self.window._opening_take_back(self.window.gen, 0) |
| 134 | self.assertEqual(len(self.window.board.move_stack), 0) |
| 135 | (variation,) = self.window.takebacks[-1] |
| 136 | self.assertEqual(variation[0], chess.Move.from_uci("d2d4")) |
| 137 | self.assertTrue(variation[1].startswith("[%zurueck kurs] ?!")) |
| 138 | |
| 139 | |
| 140 | if __name__ == "__main__": |
| 141 | unittest.main() |