1+ import unittest
2+ import tempfile
3+ import os
4+ from unittest .mock import patch , MagicMock
5+ from src .core .board import generate_board , toggle_tile , reset_board , clicked_tiles
6+
7+ class TestBoard (unittest .TestCase ):
8+ def setUp (self ):
9+ """Set up mock phrases for testing."""
10+ # Create a temporary phrases.txt file
11+ self .temp_file = tempfile .NamedTemporaryFile (delete = False )
12+ with open (self .temp_file .name , 'w' ) as f :
13+ f .write ("PHRASE 1\n PHRASE 2\n PHRASE 3\n PHRASE 4\n PHRASE 5\n " )
14+ f .write ("PHRASE 6\n PHRASE 7\n PHRASE 8\n PHRASE 9\n PHRASE 10\n " )
15+ f .write ("PHRASE 11\n PHRASE 12\n PHRASE 13\n PHRASE 14\n PHRASE 15\n " )
16+ f .write ("PHRASE 16\n PHRASE 17\n PHRASE 18\n PHRASE 19\n PHRASE 20\n " )
17+ f .write ("PHRASE 21\n PHRASE 22\n PHRASE 23\n PHRASE 24\n PHRASE 25\n " )
18+
19+ # Mock the phrases module
20+ self .phrases_patcher = patch ('src.core.phrases.get_phrases' )
21+ self .mock_get_phrases = self .phrases_patcher .start ()
22+ self .mock_get_phrases .return_value = [
23+ f"PHRASE { i } " for i in range (1 , 26 )
24+ ]
25+
26+ # Reset clicked tiles before each test
27+ clicked_tiles .clear ()
28+
29+ def tearDown (self ):
30+ """Clean up after tests."""
31+ self .phrases_patcher .stop ()
32+ os .unlink (self .temp_file .name )
33+
34+ def test_generate_board (self ):
35+ """Test that a board is generated with the correct structure."""
36+ # Directly using the returned board rather than the global variable
37+ # This makes the test more reliable
38+ board = generate_board (42 )
39+
40+ # Board should be a 5x5 grid
41+ self .assertEqual (len (board ), 5 )
42+ for row in board :
43+ self .assertEqual (len (row ), 5 )
44+
45+ # The middle cell should be FREE MEAT
46+ from src .config .constants import FREE_SPACE_TEXT
47+ self .assertEqual (board [2 ][2 ].upper (), FREE_SPACE_TEXT )
48+
49+ # FREE SPACE should be the only clicked tile initially
50+ self .assertEqual (len (clicked_tiles ), 1 )
51+ self .assertIn ((2 , 2 ), clicked_tiles )
52+
53+ def test_toggle_tile (self ):
54+ """Test that toggling a tile works correctly."""
55+ # Initially, only FREE SPACE should be clicked
56+ from src .core .board import board
57+ generate_board (42 )
58+ initial_count = len (clicked_tiles )
59+
60+ # Toggle a tile that isn't FREE SPACE
61+ toggle_tile (0 , 0 )
62+ self .assertEqual (len (clicked_tiles ), initial_count + 1 )
63+ self .assertIn ((0 , 0 ), clicked_tiles )
64+
65+ # Toggle the same tile again (should remove it)
66+ toggle_tile (0 , 0 )
67+ self .assertEqual (len (clicked_tiles ), initial_count )
68+ self .assertNotIn ((0 , 0 ), clicked_tiles )
69+
70+ # Toggle FREE SPACE (should do nothing)
71+ toggle_tile (2 , 2 )
72+ self .assertEqual (len (clicked_tiles ), initial_count )
73+ self .assertIn ((2 , 2 ), clicked_tiles )
74+
75+ def test_reset_board (self ):
76+ """Test that resetting the board works correctly."""
77+ # Set up a board with some clicked tiles
78+ from src .core .board import board
79+ generate_board (42 )
80+ toggle_tile (0 , 0 )
81+ toggle_tile (1 , 1 )
82+ self .assertEqual (len (clicked_tiles ), 3 ) # FREE SPACE + 2 others
83+
84+ # Reset the board
85+ reset_board ()
86+
87+ # Only FREE SPACE should remain clicked
88+ self .assertEqual (len (clicked_tiles ), 1 )
89+ self .assertIn ((2 , 2 ), clicked_tiles )
90+ self .assertNotIn ((0 , 0 ), clicked_tiles )
91+ self .assertNotIn ((1 , 1 ), clicked_tiles )
92+
93+ if __name__ == '__main__' :
94+ unittest .main ()
0 commit comments