From 5b5b845e41794365ae93b0f53d2332259e11d596 Mon Sep 17 00:00:00 2001 From: Markus Graf Date: Sun, 10 May 2026 07:41:00 +0200 Subject: [PATCH] Fix premature evaluation after blinking completes When transitioning from STATE_BLINKING to STATE_WAIT_FOR_INPUT, the script used a stale timestamp `now` from before the blocking `blink_led` function. This caused the puzzle to immediately evaluate as failed. Updated to use `time.time()` after blinking finishes to ensure the user gets the full 3 seconds to enter their answer. --- tests/test_wecker.py | 14 ++++++++++++++ wecker.py | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/test_wecker.py b/tests/test_wecker.py index fa0ae4c..4b73d63 100644 --- a/tests/test_wecker.py +++ b/tests/test_wecker.py @@ -103,3 +103,17 @@ def test_state_machine_incorrect(mock_gpio, mock_pygame): assert keep_running assert clock.state == wecker.STATE_WAIT_BEFORE_RETRY + +@patch('wecker.time.time') +def test_blinking_updates_time_correctly(mock_time, mock_gpio, mock_pygame): + clock = wecker.AlarmClock() + clock.state = wecker.STATE_BLINKING + clock.target_blinks = 4 + + mock_time.return_value = 200.0 + + with patch('wecker.blink_led'): + clock.update(100.0, False) + + assert clock.state == wecker.STATE_WAIT_FOR_INPUT + assert clock.last_interaction_time == 200.0 diff --git a/wecker.py b/wecker.py index b462367..9ff750b 100644 --- a/wecker.py +++ b/wecker.py @@ -101,7 +101,7 @@ class AlarmClock: logging.info("Blinking finished. Waiting for input...") self.state = STATE_WAIT_FOR_INPUT self.user_presses = 0 - self.last_interaction_time = now # Use the current update time! + self.last_interaction_time = time.time() # Use time.time() to account for blocking blink_led if 'GPIO' in globals() and hasattr(GPIO, 'input'): self.button_was_pressed = (GPIO.input(BUTTON_PIN) == GPIO.LOW)