Drop the injected AlarmSound Protocol and the wecker.Sound class. Each style now owns its tone by using pygame directly; the runner owns only the audio engine lifecycle (mixer init/quit), button sampling, and cleanup. This is the seam for future styles that handle their own tone — a 'talk' style would just import pygame and play speech, with no shared interface to extend. - styles/base.py: AlarmStyle.__init__(set_led, music_file) + start() + update() - styles/blink.py: start() loads+plays music via pygame.mixer.music - styles/simple.py: owns its beep — synthesises a square-wave buffer in module (stdlib array+math) and plays it via pygame.mixer.Sound - wecker.py: setup() brings up the mixer only; run_alarm constructs the style and guards start() with a clean log+exit on failure Tests: a shared tests/conftest.py stubs RPi.GPIO/pygame in sys.modules before any SUT import (order-independent, removes duplicated inline mocking); the wecker mock_pygame fixture patches one fresh pygame mock into wecker + both style modules so assertions see the same calls. README: tone-ownership and plugin-contract updated.
56 lines
1.7 KiB
Python
56 lines
1.7 KiB
Python
from unittest.mock import MagicMock, patch
|
|
|
|
from styles.simple import SimpleStyle
|
|
|
|
|
|
def _make_style():
|
|
calls = []
|
|
return SimpleStyle(lambda on: calls.append(on)), calls
|
|
|
|
|
|
def test_simple_keeps_ringing_when_not_pressed():
|
|
style, _ = _make_style()
|
|
with patch("styles.simple.pygame", MagicMock()):
|
|
style.start()
|
|
assert style.update(0.0, False) is True
|
|
|
|
|
|
def test_simple_stops_on_first_press():
|
|
style, _ = _make_style()
|
|
with patch("styles.simple.pygame", MagicMock()):
|
|
style.start()
|
|
assert style.update(0.0, True) is False # press -> stop
|
|
|
|
|
|
def test_simple_led_solid_on():
|
|
style, calls = _make_style()
|
|
with patch("styles.simple.pygame", MagicMock()):
|
|
style.start()
|
|
style.update(0.0, False)
|
|
assert calls == [True, True] # start sets LED on; update keeps it on
|
|
|
|
|
|
def test_simple_beeps_on_interval():
|
|
style, _ = _make_style()
|
|
with patch("styles.simple.pygame", MagicMock()) as pg:
|
|
style.start()
|
|
style.update(100.0, False) # first tick -> beep
|
|
assert pg.mixer.Sound.return_value.play.called
|
|
pg.mixer.Sound.return_value.play.reset_mock()
|
|
|
|
style.update(100.4, False) # before interval -> no beep
|
|
assert not pg.mixer.Sound.return_value.play.called
|
|
|
|
style.update(100.5, False) # at interval -> beep
|
|
assert pg.mixer.Sound.return_value.play.called
|
|
|
|
|
|
def test_simple_never_plays_music():
|
|
style, _ = _make_style()
|
|
with patch("styles.simple.pygame", MagicMock()) as pg:
|
|
style.start()
|
|
style.update(0.0, False)
|
|
style.update(0.0, True) # press -> stop
|
|
assert not pg.mixer.music.play.called
|
|
assert not pg.mixer.music.load.called
|