2026-05-09 22:31:28 +02:00
|
|
|
import pytest
|
|
|
|
|
import sys
|
|
|
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
|
|
|
|
|
|
# Mock RPi.GPIO and pygame before importing wecker
|
2026-05-11 15:05:29 +02:00
|
|
|
sys.modules["RPi"] = MagicMock()
|
|
|
|
|
sys.modules["RPi.GPIO"] = MagicMock()
|
|
|
|
|
sys.modules["pygame"] = MagicMock()
|
2026-05-09 22:31:28 +02:00
|
|
|
|
|
|
|
|
import wecker # noqa: E402
|
|
|
|
|
|
2026-05-11 15:05:29 +02:00
|
|
|
|
2026-05-09 22:31:28 +02:00
|
|
|
@pytest.fixture
|
|
|
|
|
def mock_gpio():
|
2026-05-11 15:05:29 +02:00
|
|
|
with patch.object(wecker, "GPIO") as mock:
|
2026-05-09 22:31:28 +02:00
|
|
|
mock.LOW = 0
|
|
|
|
|
mock.HIGH = 1
|
|
|
|
|
yield mock
|
|
|
|
|
|
2026-05-11 15:05:29 +02:00
|
|
|
|
2026-05-09 22:31:28 +02:00
|
|
|
@pytest.fixture
|
|
|
|
|
def mock_pygame():
|
2026-05-11 15:05:29 +02:00
|
|
|
with patch.object(wecker, "pygame") as mock:
|
2026-05-09 22:31:28 +02:00
|
|
|
yield mock
|
|
|
|
|
|
2026-05-11 15:05:29 +02:00
|
|
|
|
2026-05-09 22:31:28 +02:00
|
|
|
def test_set_led(mock_gpio):
|
|
|
|
|
wecker.set_led(True)
|
|
|
|
|
mock_gpio.output.assert_called_with(wecker.LED_PIN, mock_gpio.LOW)
|
2026-05-11 15:05:29 +02:00
|
|
|
|
2026-05-09 22:31:28 +02:00
|
|
|
wecker.set_led(False)
|
|
|
|
|
mock_gpio.output.assert_called_with(wecker.LED_PIN, mock_gpio.HIGH)
|
|
|
|
|
|
2026-05-11 15:05:29 +02:00
|
|
|
|
|
|
|
|
@patch("wecker.time.time")
|
2026-05-09 22:31:28 +02:00
|
|
|
def test_run_alarm_start_to_wait(mock_time, mock_gpio, mock_pygame):
|
|
|
|
|
# Mock pygame.mixer.get_init() to return True so music plays
|
|
|
|
|
mock_pygame.mixer.get_init.return_value = True
|
2026-05-11 15:05:29 +02:00
|
|
|
|
2026-05-09 22:31:28 +02:00
|
|
|
# Just run it in test mode, it should execute the loop once and exit
|
|
|
|
|
wecker.run_alarm(test_mode=True)
|
|
|
|
|
assert mock_pygame.mixer.music.play.called
|
|
|
|
|
|
2026-05-11 15:05:29 +02:00
|
|
|
|
2026-05-09 22:31:28 +02:00
|
|
|
def test_state_machine_evaluation(mock_gpio, mock_pygame):
|
|
|
|
|
clock = wecker.AlarmClock()
|
2026-05-11 15:05:29 +02:00
|
|
|
|
2026-05-09 22:31:28 +02:00
|
|
|
# Transition to ringing -> wait before blink
|
2026-05-11 15:05:29 +02:00
|
|
|
clock.update(100.0, True) # button press
|
2026-05-09 22:31:28 +02:00
|
|
|
assert clock.state == wecker.STATE_WAIT_BEFORE_BLINK
|
2026-05-11 15:05:29 +02:00
|
|
|
|
2026-05-09 22:31:28 +02:00
|
|
|
# Wait 3 seconds -> blinking
|
2026-06-18 17:09:10 +02:00
|
|
|
with patch("wecker.random.randint", return_value=3):
|
2026-05-09 22:31:28 +02:00
|
|
|
clock.update(103.1, False)
|
|
|
|
|
assert clock.state == wecker.STATE_BLINKING
|
2026-06-18 17:09:10 +02:00
|
|
|
assert clock.target_blinks == 3
|
2026-05-11 15:05:29 +02:00
|
|
|
|
2026-06-18 17:09:10 +02:00
|
|
|
# Advance time past the blink sequence (3 blinks * 2 phases * 0.3s = 1.8s)
|
|
|
|
|
clock.update(105.0, False)
|
2026-05-09 22:31:28 +02:00
|
|
|
assert clock.state == wecker.STATE_WAIT_FOR_INPUT
|
2026-05-11 15:05:29 +02:00
|
|
|
|
2026-05-09 22:31:28 +02:00
|
|
|
# Press 1
|
2026-06-18 17:09:10 +02:00
|
|
|
clock.update(105.5, True)
|
|
|
|
|
clock.update(105.6, False)
|
2026-05-09 22:31:28 +02:00
|
|
|
# Press 2
|
2026-06-18 17:09:10 +02:00
|
|
|
clock.update(106.0, True)
|
|
|
|
|
clock.update(106.1, False)
|
2026-05-09 22:31:28 +02:00
|
|
|
# Press 3
|
2026-06-18 17:09:10 +02:00
|
|
|
clock.update(106.5, True)
|
|
|
|
|
clock.update(106.6, False)
|
2026-05-11 15:05:29 +02:00
|
|
|
|
2026-05-09 22:31:28 +02:00
|
|
|
assert clock.user_presses == 3
|
2026-05-11 15:05:29 +02:00
|
|
|
|
2026-05-09 22:31:28 +02:00
|
|
|
# Wait 3 seconds to evaluate
|
2026-06-18 17:09:10 +02:00
|
|
|
clock.update(109.7, False) # Triggers state change
|
|
|
|
|
keep_running = clock.update(109.8, False) # Triggers evaluation
|
2026-05-11 15:05:29 +02:00
|
|
|
|
2026-05-09 22:31:28 +02:00
|
|
|
# It should evaluate, see it's correct, and return False (stop running)
|
|
|
|
|
assert clock.state == wecker.STATE_EVALUATING
|
|
|
|
|
assert not keep_running
|
|
|
|
|
|
2026-05-11 15:05:29 +02:00
|
|
|
|
2026-05-09 22:31:28 +02:00
|
|
|
def test_state_machine_incorrect(mock_gpio, mock_pygame):
|
|
|
|
|
clock = wecker.AlarmClock()
|
|
|
|
|
clock.state = wecker.STATE_WAIT_FOR_INPUT
|
|
|
|
|
clock.target_blinks = 3
|
2026-05-11 15:05:29 +02:00
|
|
|
|
2026-05-09 22:31:28 +02:00
|
|
|
# Only press once
|
|
|
|
|
clock.update(100.0, True)
|
|
|
|
|
clock.update(100.1, False)
|
2026-05-11 15:05:29 +02:00
|
|
|
|
2026-05-09 22:31:28 +02:00
|
|
|
# Wait to evaluate
|
|
|
|
|
clock.last_interaction_time = 100.1
|
2026-05-11 15:05:29 +02:00
|
|
|
clock.update(103.2, False) # triggers eval state
|
|
|
|
|
keep_running = clock.update(103.3, False) # evals to incorrect
|
|
|
|
|
|
2026-05-09 22:31:28 +02:00
|
|
|
# Evaluated incorrectly, should wait before retry
|
|
|
|
|
assert keep_running
|
|
|
|
|
assert clock.state == wecker.STATE_WAIT_BEFORE_RETRY
|
|
|
|
|
|
2026-05-10 07:41:00 +02:00
|
|
|
|
2026-06-18 17:09:10 +02:00
|
|
|
def test_blinking_is_non_blocking(mock_gpio, mock_pygame):
|
|
|
|
|
clock = wecker.AlarmClock()
|
|
|
|
|
clock.state = wecker.STATE_BLINKING
|
|
|
|
|
clock.target_blinks = 2
|
|
|
|
|
|
|
|
|
|
# Start of blinking: LED on
|
|
|
|
|
clock.update(100.0, False)
|
|
|
|
|
assert clock.state == wecker.STATE_BLINKING
|
|
|
|
|
mock_gpio.output.assert_called_with(wecker.LED_PIN, mock_gpio.LOW)
|
|
|
|
|
|
|
|
|
|
# Mid-blink: LED toggles based on elapsed time
|
|
|
|
|
clock.update(100.3, False)
|
|
|
|
|
mock_gpio.output.assert_called_with(wecker.LED_PIN, mock_gpio.HIGH)
|
|
|
|
|
|
|
|
|
|
# After sequence completes (2 blinks * 2 phases * 0.3s = 1.2s)
|
|
|
|
|
clock.update(101.2, False)
|
|
|
|
|
assert clock.state == wecker.STATE_WAIT_FOR_INPUT
|
|
|
|
|
mock_gpio.output.assert_called_with(wecker.LED_PIN, mock_gpio.HIGH)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_blinking_updates_time_correctly(mock_gpio, mock_pygame):
|
2026-05-10 07:41:00 +02:00
|
|
|
clock = wecker.AlarmClock()
|
|
|
|
|
clock.state = wecker.STATE_BLINKING
|
|
|
|
|
clock.target_blinks = 4
|
2026-05-11 15:05:29 +02:00
|
|
|
|
2026-06-18 17:09:10 +02:00
|
|
|
# Start blinking, then advance to the end of the sequence.
|
|
|
|
|
# 4 blinks * 2 phases * 0.3s = 2.4s
|
|
|
|
|
clock.update(100.0, False)
|
|
|
|
|
clock.update(102.4, False)
|
2026-05-11 15:05:29 +02:00
|
|
|
|
2026-05-10 07:41:00 +02:00
|
|
|
assert clock.state == wecker.STATE_WAIT_FOR_INPUT
|
2026-06-18 17:09:10 +02:00
|
|
|
assert clock.last_interaction_time == 102.4
|
2026-06-18 17:01:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_setup_uses_env_music_file(mock_gpio, mock_pygame, monkeypatch):
|
|
|
|
|
monkeypatch.setenv("MUSIC_FILE", "/custom/track.mp3")
|
|
|
|
|
wecker.setup()
|
|
|
|
|
mock_pygame.mixer.music.load.assert_called_with("/custom/track.mp3")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_setup_music_file_argument_overrides_env(mock_gpio, mock_pygame, monkeypatch):
|
|
|
|
|
monkeypatch.setenv("MUSIC_FILE", "/env/track.mp3")
|
|
|
|
|
wecker.setup(music_file="/arg/track.mp3")
|
|
|
|
|
mock_pygame.mixer.music.load.assert_called_with("/arg/track.mp3")
|