Public repository: timo / schach

Reader

Ein aufgeräumtes Schach-Programm für GNOME

schach / tests / test_zustand.py · 6.1 KB · at 3c63b1d52560 · raw

Outline: 19 definitions
1
"""Invarianten des Fensterzustands (schach/zustand.py) an einfachen Ersatzfenstern."""
2
3
import os
4
import types
5
import unittest
6
from unittest import mock
7
8
import chess
9
10
from schach import zustand
11
12
13
def window(**changes):
14
    """Ersatzfenster in einem gültigen Grundzustand: Partie gegen Maia, Gegenwart, nichts offen."""
15
    win = types.SimpleNamespace(
16
        mode="maia", demo=None, playback=None, recorder=None, replaying=False, browse_ply=None,
17
        board=chess.Board(), hint_level=0, hint_move=None, explain_pending=False, paused=None,
18
        clock=None, _over=lambda: False,
19
    )
20
    for name, value in changes.items():
21
        setattr(win, name, value)
22
    return win
23
24
25
ANALYSIS = {"label": "Nachspielen", "game": True}
26
SHOW = {"label": "Warum Lc4?"}
27
28
29
def playback(playing=True):
30
    return {"media": types.SimpleNamespace(get_playing=lambda: playing)}
31
32
33
def clock(running):
34
    return types.SimpleNamespace(running=running)
35
36
37
class ViolationsTest(unittest.TestCase):
38
    def assertValid(self, win):
39
        self.assertEqual(zustand.violations(win), [])
40
41
    def assertViolates(self, win, text):
42
        found = zustand.violations(win)
43
        self.assertTrue(any(text in f for f in found), found)
44
45
    def test_grundzustand(self):
46
        self.assertValid(window())
47
48
    def test_wiedergabe_braucht_analysebrett(self):
49
        self.assertValid(window(demo=ANALYSIS, replaying=True, playback=playback(), paused={}))
50
        self.assertViolates(window(playback=playback(False)), "Wiedergabe ohne Analysebrett")
51
        # der gemeldete Fehler: eine Vorführung geht auf, der Ton der Wiedergabe läuft weiter
52
        self.assertViolates(window(demo=SHOW, replaying=True, playback=playback(), paused={}),
53
                            "Ton der Wiedergabe läuft unter einer Vorführung")
54
        # so soll es sein: der Ton wartet, bis die Vorführung zu ist
55
        self.assertValid(window(demo=SHOW, replaying=True, playback=playback(False), paused={}))
56
57
    def test_aufnahme(self):
58
        self.assertValid(window(demo=ANALYSIS, recorder=object(), paused={}))
59
        self.assertViolates(window(recorder=object()), "Aufnahme ohne Analysebrett")
60
        self.assertViolates(window(demo=ANALYSIS, recorder=object(), playback=playback()), "zugleich")
61
62
    def test_nachspielen(self):
63
        self.assertViolates(window(replaying=True), "Nachspielen ohne Analysebrett")
64
        self.assertValid(window(replaying=True, mode="setup"))
65
        self.assertValid(window(replaying=True, demo=SHOW, paused={}))  # „Warum nicht?“ beim Nachspielen
66
67
    def test_analysebrett_nur_gegen_maia_und_beim_ansehen(self):
68
        self.assertValid(window(mode="review", demo=ANALYSIS))
69
        self.assertViolates(window(mode="duo", demo=ANALYSIS), "Analysebrett in Aktivität")
70
        self.assertValid(window(mode="puzzle", demo=ANALYSIS))  # Stufe 6: Rätsel jederzeit ansehen
71
        self.assertViolates(window(mode="opening", demo=ANALYSIS), "Analysebrett in Aktivität")
72
        self.assertValid(window(mode="opening", demo=ANALYSIS, _over=lambda: True))  # Ende der Linie
73
74
    def test_hinweis(self):
75
        move = chess.Move.from_uci("e2e4")
76
        self.assertValid(window(hint_level=2, hint_move=move))
77
        self.assertViolates(window(hint_level=3, hint_move=move), "Hinweisstufe 3")
78
        self.assertViolates(window(hint_level=1), "ohne Hinweiszug")
79
        self.assertViolates(window(mode="online", hint_level=1, hint_move=move), "in Aktivität")
80
        self.assertViolates(window(explain_pending=True, hint_level=1, hint_move=move), "„Warum?“")
81
82
    def test_pause(self):
83
        self.assertValid(window(paused={}))
84
        self.assertViolates(window(mode="duo", paused={}), "Pause in Aktivität")
85
        self.assertViolates(window(demo=SHOW), "gegen Maia ohne Pause")
86
        self.assertViolates(window(demo=ANALYSIS), "gegen Maia ohne Pause")  # seit 24.11.2026
87
        # jede laufende Rechnung hält die Pause fest (Topic pause)
88
        self.assertViolates(window(hint_pending=True), "Hinweis wird gerechnet, gegen Maia ohne Pause")
89
        self.assertViolates(window(why_not_entries={1: {"busy": True}}), "„Warum nicht?“ wird gerechnet")
90
        self.assertValid(window(hint_pending=True, paused={}))
91
        self.assertValid(window(mode="duo", hint_pending=True))  # Pause gibt es nur gegen Maia
92
        self.assertValid(window(mode="review", demo=ANALYSIS))
93
        self.assertValid(window(demo=SHOW, _over=lambda: True))  # nach dem Ende gibt es keine Pause
94
        self.assertValid(window(mode="puzzle", demo=SHOW))
95
96
    def test_uhr(self):
97
        self.assertValid(window(clock=clock(chess.WHITE)))
98
        self.assertValid(window(clock=clock(None), paused={}))
99
        self.assertViolates(window(clock=clock(chess.WHITE), paused={}), "Uhr läuft in der Pause")
100
        self.assertViolates(window(clock=clock(chess.BLACK), _over=lambda: True), "nach dem Ende")
101
        self.assertViolates(window(mode="puzzle", clock=clock(chess.WHITE)), "eigene Uhr")
102
103
    def test_alte_stellung(self):
104
        board = chess.Board()
105
        for san in ("e4", "e5", "Nf3"):
106
            board.push_san(san)
107
        self.assertValid(window(board=board, browse_ply=0))
108
        self.assertValid(window(board=board, browse_ply=2, demo=ANALYSIS, paused={}))  # = Gegenwart, nicht alt
109
        self.assertViolates(window(board=board, browse_ply=0, demo=ANALYSIS), "alte Stellung")
110
111
112
class CheckTest(unittest.TestCase):
113
    def setUp(self):
114
        zustand._reported.clear()
115
116
    def test_streng_bricht_ab(self):
117
        with mock.patch.dict(os.environ, {"SCHACH_STRENG": "1"}):
118
            with self.assertRaises(AssertionError):
119
                zustand.check(window(playback=playback(False)))
120
121
    def test_sonst_einmal_geloggt(self):
122
        with mock.patch.dict(os.environ, {}, clear=False):
123
            os.environ.pop("SCHACH_STRENG", None)
124
            with self.assertLogs("schach.zustand", "WARNING") as logs:
125
                zustand.check(window(playback=playback(False)))
126
                zustand.check(window(playback=playback(False)))
127
                zustand.log.warning("Ende")
128
        self.assertEqual(len(logs.output), 2)  # die Meldung einmal, dazu „Ende“
129
130
131
if __name__ == "__main__":
132
    unittest.main()