2026-05-09 22:31:28 +02:00
|
|
|
import pytest
|
|
|
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
|
|
2026-08-02 21:50:00 +02:00
|
|
|
import styles.blink
|
|
|
|
|
import styles.simple
|
|
|
|
|
import wecker
|
2026-05-09 22:31:28 +02:00
|
|
|
|
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-08-02 21:50:00 +02:00
|
|
|
"""One fresh pygame mock shared by wecker and both styles.
|
|
|
|
|
|
|
|
|
|
Under Pattern C the styles use pygame directly, so a single mock must be
|
|
|
|
|
patched into wecker (setup) and the style modules (start) for assertions
|
|
|
|
|
to see the same calls.
|
|
|
|
|
"""
|
|
|
|
|
mock = MagicMock()
|
|
|
|
|
with (
|
|
|
|
|
patch.object(wecker, "pygame", mock),
|
|
|
|
|
patch.object(styles.blink, "pygame", mock),
|
|
|
|
|
patch.object(styles.simple, "pygame", 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
|
|
|
|
2026-08-02 16:29:40 +02:00
|
|
|
def test_setup_inits_mixer_and_gpio_not_music(mock_gpio, mock_pygame):
|
|
|
|
|
wecker.setup()
|
|
|
|
|
assert mock_pygame.mixer.pre_init.called
|
|
|
|
|
assert mock_pygame.mixer.init.called
|
|
|
|
|
assert mock_gpio.setmode.called
|
|
|
|
|
assert mock_gpio.setup.called
|
|
|
|
|
# setup() no longer loads music — that is now the style's job.
|
|
|
|
|
assert not mock_pygame.mixer.music.load.called
|
2026-05-09 22:31:28 +02:00
|
|
|
|
2026-05-11 15:05:29 +02:00
|
|
|
|
2026-07-31 16:19:14 +02:00
|
|
|
def test_run_alarm_unknown_style_raises(mock_gpio, mock_pygame):
|
|
|
|
|
"""An unknown style is rejected before any hardware is touched."""
|
|
|
|
|
with pytest.raises(ValueError, match="Unknown alarm style"):
|
|
|
|
|
wecker.run_alarm(style_name="nope", test_mode=True)
|
|
|
|
|
mock_pygame.mixer.init.assert_not_called()
|
2026-05-09 22:31:28 +02:00
|
|
|
|
2026-05-11 15:05:29 +02:00
|
|
|
|
2026-07-31 16:19:14 +02:00
|
|
|
def test_run_alarm_uses_selected_style(mock_gpio, mock_pygame):
|
2026-08-02 21:50:00 +02:00
|
|
|
"""run_alarm looks up the style, injects set_led + music_file, calls start()."""
|
2026-07-31 16:19:14 +02:00
|
|
|
fake = MagicMock()
|
|
|
|
|
fake.update.return_value = True # keep ringing
|
|
|
|
|
fake_style_cls = MagicMock(return_value=fake)
|
2026-05-11 15:05:29 +02:00
|
|
|
|
2026-07-31 16:19:14 +02:00
|
|
|
with patch("wecker.get_style", return_value=fake_style_cls) as get_style:
|
|
|
|
|
wecker.run_alarm(style_name="whatever", test_mode=True)
|
2026-05-11 15:05:29 +02:00
|
|
|
|
2026-07-31 16:19:14 +02:00
|
|
|
get_style.assert_called_once_with("whatever")
|
2026-08-02 22:09:46 +02:00
|
|
|
assert fake_style_cls.call_args.args[0] is wecker.set_led
|
|
|
|
|
assert isinstance(fake_style_cls.call_args.kwargs["music_file"], str)
|
2026-08-02 16:29:40 +02:00
|
|
|
fake.start.assert_called_once()
|
2026-07-31 16:19:14 +02:00
|
|
|
fake.update.assert_called()
|
2026-06-18 17:01:26 +02:00
|
|
|
|
|
|
|
|
|
2026-08-02 16:29:40 +02:00
|
|
|
def test_run_alarm_default_music_file(mock_gpio, mock_pygame, monkeypatch):
|
|
|
|
|
monkeypatch.delenv("MUSIC_FILE", raising=False)
|
|
|
|
|
fake = MagicMock()
|
|
|
|
|
fake.update.return_value = True
|
|
|
|
|
fake_style_cls = MagicMock(return_value=fake)
|
|
|
|
|
with patch("wecker.get_style", return_value=fake_style_cls):
|
|
|
|
|
wecker.run_alarm(test_mode=True)
|
2026-08-02 22:09:46 +02:00
|
|
|
assert fake_style_cls.call_args.kwargs["music_file"] == wecker.DEFAULT_MUSIC_FILE
|
2026-06-18 17:01:26 +02:00
|
|
|
|
|
|
|
|
|
2026-08-02 16:29:40 +02:00
|
|
|
def test_run_alarm_env_music_file(mock_gpio, mock_pygame, monkeypatch):
|
2026-06-18 17:01:26 +02:00
|
|
|
monkeypatch.setenv("MUSIC_FILE", "/env/track.mp3")
|
2026-08-02 16:29:40 +02:00
|
|
|
fake = MagicMock()
|
|
|
|
|
fake.update.return_value = True
|
|
|
|
|
fake_style_cls = MagicMock(return_value=fake)
|
|
|
|
|
with patch("wecker.get_style", return_value=fake_style_cls):
|
|
|
|
|
wecker.run_alarm(test_mode=True)
|
2026-08-02 22:09:46 +02:00
|
|
|
assert fake_style_cls.call_args.kwargs["music_file"] == "/env/track.mp3"
|
2026-08-02 16:29:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_run_alarm_music_file_arg_overrides_env(mock_gpio, mock_pygame, monkeypatch):
|
|
|
|
|
monkeypatch.setenv("MUSIC_FILE", "/env/track.mp3")
|
|
|
|
|
fake = MagicMock()
|
|
|
|
|
fake.update.return_value = True
|
|
|
|
|
fake_style_cls = MagicMock(return_value=fake)
|
|
|
|
|
with patch("wecker.get_style", return_value=fake_style_cls):
|
|
|
|
|
wecker.run_alarm(music_file="/arg/track.mp3", test_mode=True)
|
2026-08-02 22:09:46 +02:00
|
|
|
assert fake_style_cls.call_args.kwargs["music_file"] == "/arg/track.mp3"
|
2026-08-02 16:29:40 +02:00
|
|
|
|
|
|
|
|
|
2026-08-02 21:50:00 +02:00
|
|
|
def test_run_alarm_blink_plays_music(mock_gpio, mock_pygame):
|
2026-08-02 16:29:40 +02:00
|
|
|
"""The default (blink) style plays music on an endless loop."""
|
|
|
|
|
wecker.run_alarm(test_mode=True)
|
|
|
|
|
assert mock_pygame.mixer.music.play.called
|
|
|
|
|
assert mock_pygame.mixer.music.play.call_args.args[0] == -1 # endless loop
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_run_alarm_simple_beeps_and_plays_no_music(mock_gpio, mock_pygame):
|
|
|
|
|
"""The simple style beeps and never touches the music stream."""
|
|
|
|
|
wecker.run_alarm(style_name="simple", test_mode=True)
|
|
|
|
|
assert mock_pygame.mixer.Sound.return_value.play.called # beep played
|
|
|
|
|
assert not mock_pygame.mixer.music.play.called # no music
|