File size: 7,800 Bytes
571f3d0
 
 
 
 
d10d3ce
571f3d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d10d3ce
 
 
 
571f3d0
 
 
 
 
 
 
 
d10d3ce
 
 
 
571f3d0
 
 
 
 
 
 
 
 
d10d3ce
 
 
 
571f3d0
 
 
 
 
 
 
 
 
 
d10d3ce
 
 
 
571f3d0
 
 
 
 
 
 
 
 
d10d3ce
571f3d0
 
d10d3ce
571f3d0
 
d10d3ce
 
 
 
571f3d0
 
 
 
 
 
 
 
d10d3ce
571f3d0
 
 
d10d3ce
571f3d0
 
 
d10d3ce
571f3d0
 
d10d3ce
 
 
 
571f3d0
 
 
 
d10d3ce
571f3d0
 
 
 
 
d10d3ce
571f3d0
 
 
 
 
 
 
d10d3ce
571f3d0
 
d10d3ce
571f3d0
 
 
d10d3ce
571f3d0
 
d10d3ce
571f3d0
 
 
 
 
 
d10d3ce
571f3d0
 
 
 
 
 
 
d10d3ce
571f3d0
d10d3ce
 
 
571f3d0
 
 
 
d10d3ce
571f3d0
 
 
 
 
 
 
d10d3ce
571f3d0
 
 
d10d3ce
571f3d0
d10d3ce
 
571f3d0
 
 
d10d3ce
571f3d0
 
 
 
 
 
d10d3ce
571f3d0
 
 
d10d3ce
571f3d0
 
 
d10d3ce
571f3d0
 
d10d3ce
 
 
 
571f3d0
d10d3ce
571f3d0
d10d3ce
571f3d0
 
d10d3ce
571f3d0
 
 
 
 
 
d10d3ce
571f3d0
 
 
 
 
d10d3ce
571f3d0
 
d10d3ce
 
571f3d0
 
 
d10d3ce
 
571f3d0
d10d3ce
571f3d0
d10d3ce
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
"""
Tests for the Gradio UI interface to ensure proper error handling and memory management.
"""

import matplotlib.pyplot as plt

from ui.gradio_interface import GradioInterface


class TestGradioInterface:
    """Test the Gradio interface functionality."""

    def test_interface_initialization(self):
        """Test that interface initializes correctly."""
        interface = GradioInterface()
        assert interface.game is not None
        assert interface.game.dice_sides == 6
        assert interface.game.max_rounds == 10

    def test_reset_game_returns_proper_types(self):
        """Test that reset_game returns proper types."""
        interface = GradioInterface()
        result = interface.reset_game(dice_sides=8, max_rounds=15)

        assert len(result) == 3
        status, belief_chart, game_log = result

        assert isinstance(status, str)
        assert isinstance(belief_chart, plt.Figure)
        assert isinstance(game_log, str)

    def test_start_new_game_valid_target(self):
        """Test starting a new game with valid target."""
        interface = GradioInterface()
        result = interface.start_new_game("3")

        assert len(result) == 3
        status, belief_chart, game_log = result

        assert isinstance(status, str)
        assert isinstance(belief_chart, plt.Figure)
        assert isinstance(game_log, str)
        assert "Playing" in status

    def test_start_new_game_invalid_target(self):
        """Test starting a new game with invalid target returns proper types."""
        interface = GradioInterface()
        result = interface.start_new_game("10")  # Invalid for 6-sided die

        assert len(result) == 3
        status, belief_chart, game_log = result

        assert isinstance(status, str)
        assert isinstance(belief_chart, plt.Figure)
        assert isinstance(game_log, str)
        assert "❌" in status
        assert "between 1 and 6" in status

    def test_play_round_without_game_started(self):
        """Test playing round without starting game returns proper types."""
        interface = GradioInterface()
        result = interface.play_round()

        assert len(result) == 3
        status, belief_chart, game_log = result

        assert isinstance(status, str)
        assert isinstance(belief_chart, plt.Figure)
        assert isinstance(game_log, str)
        assert "❌" in status
        assert "not in playing phase" in status

    def test_play_round_normal_flow(self):
        """Test normal round playing flow."""
        interface = GradioInterface()

        # Start a game first
        interface.start_new_game("3")

        # Play a round
        result = interface.play_round()

        assert len(result) == 3
        status, belief_chart, game_log = result

        assert isinstance(status, str)
        assert isinstance(belief_chart, plt.Figure)
        assert isinstance(game_log, str)
        assert "Playing" in status

    def test_exceeding_max_rounds(self):
        """Test that exceeding max rounds shows graceful completion."""
        interface = GradioInterface()

        # Start a game with 2 rounds
        interface.reset_game(dice_sides=6, max_rounds=2)
        interface.start_new_game("3")

        # Play 2 rounds (should finish the game)
        interface.play_round()
        interface.play_round()

        # Try to play another round (should be prevented)
        result = interface.play_round()

        assert len(result) == 3
        status, belief_chart, game_log = result

        assert isinstance(status, str)
        assert isinstance(belief_chart, plt.Figure)
        assert isinstance(game_log, str)
        # When game is finished, we should get a graceful completion message
        assert "🏁" in status and "completed" in status

    def test_create_empty_chart(self):
        """Test that empty chart creation works properly."""
        interface = GradioInterface()
        chart = interface._create_empty_chart()

        assert isinstance(chart, plt.Figure)
        # Clean up
        plt.close(chart)

    def test_matplotlib_memory_management(self):
        """Test that matplotlib figures are properly managed."""
        interface = GradioInterface()

        # Get initial figure count
        initial_figures = len(plt.get_fignums())

        # Create multiple charts
        for _ in range(5):
            interface._create_belief_chart()

        # Should not accumulate figures due to plt.close('all')
        final_figures = len(plt.get_fignums())

        # Should have at most 1 figure open (the most recent one)
        assert final_figures <= initial_figures + 1

    def test_error_handling_preserves_types(self):
        """Test that error handling always returns consistent types."""
        interface = GradioInterface()

        # Test various error conditions
        error_results = [
            interface.start_new_game("invalid_number"),
            interface.start_new_game("0"),
            interface.start_new_game("100"),
            interface.play_round(),  # No game started
        ]

        for result in error_results:
            assert len(result) == 3
            status, belief_chart, game_log = result

            assert isinstance(status, str)
            assert isinstance(belief_chart, plt.Figure)
            assert isinstance(game_log, str)
            assert "❌" in status

            # Clean up the figure
            plt.close(belief_chart)

    def test_game_log_creation(self):
        """Test that game log is created properly."""
        interface = GradioInterface()
        interface.start_new_game("3")

        # Play a few rounds
        for _ in range(3):
            interface.play_round()

        result = interface._get_interface_state()
        status, belief_chart, game_log = result

        assert isinstance(game_log, str)
        assert "Evidence History" in game_log
        assert "Round" in game_log

        # Clean up
        plt.close(belief_chart)

    def test_graceful_game_completion(self):
        """Test that game completion shows comprehensive final results."""
        interface = GradioInterface()

        # Start and complete a game
        interface.reset_game(dice_sides=6, max_rounds=3)
        interface.start_new_game("4")

        # Play all rounds
        for _ in range(3):
            interface.play_round()

        # Get final state
        result = interface._get_interface_state()
        status, belief_chart, game_log = result

        # Should show comprehensive final results in game log
        # (round_info was removed for cleaner UI)
        assert "Game Completed" in game_log
        assert "Congratulations" in game_log or "Learning opportunity" in game_log
        assert "confidence in true target" in game_log

        # Chart should have final state title
        assert isinstance(belief_chart, plt.Figure)

        # Clean up
        plt.close(belief_chart)

    def test_completion_state_preservation(self):
        """Test that completion state preserves all information."""
        interface = GradioInterface()

        # Complete a game
        interface.reset_game(dice_sides=6, max_rounds=2)
        interface.start_new_game("3")
        interface.play_round()
        interface.play_round()

        # Try to play after completion - should preserve final state
        result = interface.play_round()
        status, belief_chart, game_log = result

        # Should still have all the final game information
        assert "🏁" in status
        assert "completed" in status
        # round_info was removed for cleaner UI
        assert len(game_log) > 50  # Should have complete evidence history
        assert isinstance(belief_chart, plt.Figure)

        # Clean up
        plt.close(belief_chart)