Public repository: timo / schach

Reader

Ein aufgeräumtes Schach-Programm für GNOME

schach / tests / test_warum_variante.py · 5.6 KB · at 3c63b1d52560 · raw

Outline: 18 definitions
1
"""„Warum nicht?“ und die dritte Hinweisstufe als Nebenvarianten im Partiebaum (Analysebrett,
2
Stufe 5): mit den echten Mixins, ohne Engine (die Erklärungsbäume sind von Hand gebaut)."""
3
4
import unittest
5
6
import chess
7
import chess.pgn
8
9
from schach import explain, savegames, zustand
10
from tests.test_zustand_regression import FakeWindow, played
11
12
13
def m(uci):
14
    return chess.Move.from_uci(uci)
15
16
17
def why_not(board, ply, best, refutation=()):
18
    """Baum wie explain.why_not_job(variation=True): gespielter Zug mit Widerlegung, bester Zug daneben."""
19
    before = board.root()
20
    for move in board.move_stack[:ply]:
21
        before.push(move)
22
    tree = explain.line(before, [board.move_stack[ply], *refutation])
23
    tree.info(tree.first())["label"] = "Gespielt"
24
    top = tree.game.add_variation(best, comment="Besser als … Damit gewinnst du.")
25
    tree.info(top)["label"] = "Bester Zug"
26
    return tree
27
28
29
class Window(FakeWindow):
30
    def __init__(self, *sans):
31
        super().__init__(played(*sans), mode="maia")
32
        self.saved = 0
33
34
    def _schedule_autosave(self):
35
        self.saved += 1
36
37
38
class WhyNotTest(unittest.TestCase):
39
    def setUp(self):
40
        self.win = Window("e4", "e5", "Ke2", "d6")
41
        self.tree = why_not(self.win.board, 2, m("g1f3"), refutation=[m("d8g5")])
42
43
    def test_best_move_hangs_before_the_played_move(self):
44
        win = self.win
45
        win._why_not_variation(2, self.tree)
46
        fork = win._game_node(1)
47
        self.assertEqual([(c.move, c.starting_comment) for c in fork.variations],
48
                         [(m("e1e2"), ""), (m("g1f3"), "Bester Zug")])
49
        self.assertEqual(win.game_tree.label(fork.variations[1]), "Bester Zug")
50
        self.assertTrue(win._why_not_known(2))
51
52
    def test_board_jumps_to_the_fork(self):
53
        win = self.win
54
        win._why_not_variation(2, self.tree)
55
        self.assertTrue(win.demo["game"])
56
        self.assertIs(win.demo["node"], win._game_node(1))
57
        self.assertIsNotNone(win.paused)  # das Analysebrett hält Maia an
58
        self.assertEqual(zustand.violations(win), [])
59
60
    def test_refutation_hangs_after_the_played_move(self):
61
        win = self.win
62
        win._why_not_variation(2, self.tree)
63
        after = win._game_node(2)
64
        self.assertEqual([(c.move, c.starting_comment) for c in after.variations],
65
                         [(m("d7d6"), ""), (m("d8g5"), "Warum nicht 2. Ke2?")])
66
67
    def test_widerlegung_die_der_partie_folgt_traegt_den_erklaerten_zug(self):
68
        """Nutzerbericht 24.11.2026: Folgt die Widerlegung der Partie zwei Züge und zweigt erst dann
69
        ab, steht sie unter einem Zug des Gegners. Ohne Zug im Namen sah „Warum nicht?“ dort aus,
70
        als ginge es um den Zug des Gegners."""
71
        win = Window("e4", "e5", "Ke2", "d6", "d3", "Nc6")
72
        tree = why_not(win.board, 2, m("g1f3"), refutation=[m("d7d6"), m("d2d3"), m("d8g5")])
73
        win._why_not_variation(2, tree)
74
        deeper = win._game_node(4).variations  # nach 3. d3: Partie 3… Sc6, Widerlegung 3… Dg5
75
        self.assertEqual([(c.move, c.starting_comment) for c in deeper],
76
                         [(m("b8c6"), ""), (m("d8g5"), "Warum nicht 2. Ke2?")])
77
78
    def test_twice_adds_nothing(self):
79
        win = self.win
80
        win._why_not_variation(2, self.tree)
81
        win._why_not_variation(2, self.tree)
82
        self.assertEqual(len(win._game_node(1).variations), 2)
83
        self.assertEqual(len(win._game_node(2).variations), 2)
84
85
    def test_saved_with_the_game(self):
86
        win = self.win
87
        win._why_not_variation(2, self.tree)
88
        self.assertGreater(win.saved, 0)
89
        state = savegames.GameState(board=win.board, human=win.human, elo=1500,
90
                                    variations=win._game_variations())
91
        loaded = savegames.from_pgn(savegames.to_pgn(state))
92
        self.assertEqual([v[0] for v in loaded.variations[1]], [m("g1f3")])
93
        self.assertEqual(loaded.variations[1][0][4], "Bester Zug")
94
95
96
class HintTest(unittest.TestCase):
97
    def setUp(self):
98
        self.win = Window("e4", "e5", "Nf3", "Nc6")
99
        board = self.win.board.copy()
100
        self.tree = explain.line(board, [m("f1c4"), m("g8f6")])
101
        self.tree.first().comment = "Du entwickelst eine Figur."
102
103
    def test_hint_hangs_at_the_present_and_board_shows_it(self):
104
        win = self.win
105
        win._hint_variation(self.tree)
106
        present = win._game_node(3)
107
        hint = present.variations[0]
108
        self.assertEqual((hint.move, hint.starting_comment), (m("f1c4"), "Hinweis 3. Lc4"))
109
        self.assertEqual(win.game_tree.label(hint), "Hinweis")  # Schild am Pfeil bleibt kurz
110
        self.assertIs(win.demo["node"], hint)
111
        self.assertEqual(zustand.violations(win), [])
112
113
    def test_playing_the_hint_makes_it_the_played_move(self):
114
        win = self.win
115
        win._hint_variation(self.tree)
116
        hint = win._game_node(3).variations[0]
117
        win.demo = None
118
        win.paused = None
119
        win.board.push(m("f1c4"))
120
        win._game_tree()
121
        self.assertIs(win._game_node(4), hint)  # derselbe Knoten, jetzt gespielt
122
        self.assertTrue(win.game_tree.meta[hint]["played"])
123
        self.assertEqual([c.move for c in hint.variations], [m("g8f6")])  # Rest bleibt Nebenvariante
124
125
    def test_another_move_keeps_the_hint_as_side_line(self):
126
        win = self.win
127
        win._hint_variation(self.tree)
128
        win.demo = None
129
        win.paused = None
130
        win.board.push(m("f1b5"))
131
        win._game_tree()
132
        present = win._game_node(3)
133
        self.assertEqual([c.move for c in present.variations], [m("f1b5"), m("f1c4")])
134
        self.assertEqual(present.variations[1].starting_comment, "Hinweis 3. Lc4")
135
136
137
if __name__ == "__main__":
138
    unittest.main()