refactor(keyboard_coop): remove dead/unreachable code

- Remove unreachable force-submit check (line 351): available_letters
  always contains the clicked letter after add(letter)
- Remove unreachable hover color branch (lines 398-404): the available
  check at line 396 catches all available letters first
- Remove unused KEY_HOVER_COLOR constant
- Remove unused mouse_pos variable in _draw_keyboard
- Update test from hover to unavailable key color test

Coverage: 97% -> 100%
This commit is contained in:
Krzysztof kuhy Rudnicki 2025-12-01 20:15:40 +01:00
parent 4e64c3deef
commit 57233aa8d5
2 changed files with 9 additions and 22 deletions

View File

@ -26,7 +26,6 @@ SCREEN_HEIGHT = 768
BACKGROUND_COLOR = (30, 30, 40)
KEYBOARD_COLOR = (60, 60, 70)
KEY_COLOR = (80, 80, 90)
KEY_HOVER_COLOR = (100, 100, 110)
KEY_SELECTED_COLOR = (150, 150, 200)
KEY_AVAILABLE_COLOR = (100, 150, 100)
TEXT_COLOR = (255, 255, 255)
@ -346,10 +345,6 @@ class KeyboardCoopGame:
f"Player {self.state.current_player + 1}: Choose an adjacent letter!"
)
# If no valid moves available, force word submission
if not self.keyboard.available_letters:
self._submit_word()
def _submit_word(self) -> None:
"""Submit the current word and check if it's valid."""
if len(self.state.current_word) >= MIN_WORD_LENGTH and self._is_valid_word(
@ -387,19 +382,12 @@ class KeyboardCoopGame:
def _draw_keyboard(self) -> None:
"""Draw the virtual keyboard."""
mouse_pos = pygame.mouse.get_pos()
for letter, rect in self.keyboard.positions.items():
# Determine key color
if letter in self.state.selected_letters:
color = KEY_SELECTED_COLOR
elif letter in self.keyboard.available_letters:
color = KEY_AVAILABLE_COLOR
elif (
rect.collidepoint(mouse_pos)
and letter in self.keyboard.available_letters
):
color = KEY_HOVER_COLOR
else:
color = KEY_COLOR

View File

@ -760,15 +760,14 @@ class TestDrawKeyboard:
colors_used = [call[0][1] for call in calls]
assert KEY_SELECTED_COLOR in colors_used
def test_draw_keyboard_hover_color(self) -> None:
"""Test hover color when mouse is over available key."""
def test_draw_keyboard_unavailable_key_color(self) -> None:
"""Test unavailable keys get default key color."""
mock_pg = MagicMock()
mock_pg.draw = MagicMock()
# Mouse is at position that collides with the key
mock_pg.mouse.get_pos.return_value = (100, 100)
with patch.dict("sys.modules", {"pygame": mock_pg}):
from python_pkg.keyboard_coop.main import (
KEY_COLOR,
FontSet,
GameState,
KeyboardCoopGame,
@ -785,18 +784,18 @@ class TestDrawKeyboard:
)
mock_rect_a = MagicMock()
# Mouse collides with this rect
mock_rect_a.collidepoint.return_value = True
mock_rect_a.center = (100, 100)
game.keyboard.positions = {"a": mock_rect_a}
# Key is available (required for hover color)
game.keyboard.available_letters = {"a"}
# Key is NOT available - should get KEY_COLOR
game.keyboard.available_letters = set()
game._draw_keyboard()
# draw.rect should have been called
assert mock_pg.draw.rect.call_count >= 2
# Check that KEY_COLOR was used for unavailable key
calls = mock_pg.draw.rect.call_args_list
colors_used = [call[0][1] for call in calls]
assert KEY_COLOR in colors_used
class TestDrawUI: