refactor: styles own their ringing tone directly (Pattern C)

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.
This commit is contained in:
2026-08-02 21:50:00 +02:00
parent 92eba762ce
commit 3a7b6ac7a3
10 changed files with 167 additions and 190 deletions
+33 -27
View File
@@ -1,49 +1,55 @@
from unittest.mock import MagicMock
from unittest.mock import MagicMock, patch
from styles.simple import SimpleStyle
def _make_style():
sound = MagicMock()
style = SimpleStyle(lambda on: None, sound)
return style, sound
calls = []
return SimpleStyle(lambda on: calls.append(on)), calls
def test_simple_keeps_ringing_when_not_pressed():
style, _ = _make_style()
assert style.update(0.0, False) is True
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()
assert style.update(0.0, True) is False # press -> stop
with patch("styles.simple.pygame", MagicMock()):
style.start()
assert style.update(0.0, True) is False # press -> stop
def test_simple_led_solid_on():
calls = []
sound = MagicMock()
style = SimpleStyle(lambda on: calls.append(on), sound)
style.start()
assert calls == [True]
style.update(0.0, False)
assert calls[-1] is True # stays on every tick
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, sound = _make_style()
style.start()
style.update(100.0, False) # first tick -> beep
assert sound.play_beep.called
sound.play_beep.reset_mock()
style.update(100.4, False) # before interval -> no beep
assert not sound.play_beep.called
style.update(100.5, False) # at interval -> beep
assert sound.play_beep.called
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, sound = _make_style()
style.start()
style.update(0.0, False)
style.update(0.0, True) # press -> stop
assert not sound.play_music.called
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