| 1 |
""" |
| 2 |
Unit tests for automatic time announcement behavior. |
| 3 |
""" |
| 4 |
|
| 5 |
import datetime |
| 6 |
import importlib.util |
| 7 |
from pathlib import Path |
| 8 |
from unittest.mock import Mock |
| 9 |
|
| 10 |
|
| 11 |
def _load_time_module(): |
| 12 |
module_path = ( |
| 13 |
Path(__file__).resolve().parents[2] |
| 14 |
/ "fenrirscreenreader" |
| 15 |
/ "commands" |
| 16 |
/ "onHeartBeat" |
| 17 |
/ "76000-time.py" |
| 18 |
) |
| 19 |
spec = importlib.util.spec_from_file_location("fenrir_time_command", module_path) |
| 20 |
module = importlib.util.module_from_spec(spec) |
| 21 |
spec.loader.exec_module(module) |
| 22 |
return module |
| 23 |
|
| 24 |
|
| 25 |
def _create_environment(output_manager): |
| 26 |
now = datetime.datetime.now() |
| 27 |
settings_manager = Mock() |
| 28 |
settings_manager.get_setting.side_effect = lambda section, setting: { |
| 29 |
("time", "on_minutes"): str(now.minute).zfill(2), |
| 30 |
("general", "date_format"): "%A, %B %d, %Y", |
| 31 |
("general", "time_format"): "%I:%M%P", |
| 32 |
}[(section, setting)] |
| 33 |
settings_manager.get_setting_as_int.side_effect = lambda section, setting: { |
| 34 |
("time", "delay_sec"): 0, |
| 35 |
}[(section, setting)] |
| 36 |
settings_manager.get_setting_as_bool.side_effect = lambda section, setting: { |
| 37 |
("time", "enabled"): True, |
| 38 |
("time", "present_date"): True, |
| 39 |
("time", "present_time"): True, |
| 40 |
("time", "interrupt"): False, |
| 41 |
("time", "announce"): True, |
| 42 |
}[(section, setting)] |
| 43 |
|
| 44 |
return { |
| 45 |
"runtime": { |
| 46 |
"OutputManager": output_manager, |
| 47 |
"SettingsManager": settings_manager, |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
def test_time_announcement_lock_suppresses_second_instance(tmp_path): |
| 53 |
time_module = _load_time_module() |
| 54 |
lock_path = tmp_path / "time-announcement.lock" |
| 55 |
first_output = Mock( |
| 56 |
interrupt_output=Mock(), |
| 57 |
play_sound_icon=Mock(), |
| 58 |
present_text=Mock(), |
| 59 |
) |
| 60 |
second_output = Mock( |
| 61 |
interrupt_output=Mock(), |
| 62 |
play_sound_icon=Mock(), |
| 63 |
present_text=Mock(), |
| 64 |
) |
| 65 |
first_command = time_module.command() |
| 66 |
second_command = time_module.command() |
| 67 |
|
| 68 |
first_command.initialize(_create_environment(first_output)) |
| 69 |
second_command.initialize(_create_environment(second_output)) |
| 70 |
first_command._get_announcement_lock_path = lambda: str(lock_path) |
| 71 |
second_command._get_announcement_lock_path = lambda: str(lock_path) |
| 72 |
first_command.last_time -= datetime.timedelta(hours=1) |
| 73 |
second_command.last_time -= datetime.timedelta(hours=1) |
| 74 |
|
| 75 |
first_command.run() |
| 76 |
second_command.run() |
| 77 |
|
| 78 |
first_output.play_sound_icon.assert_called_once_with("announce") |
| 79 |
first_output.present_text.assert_called() |
| 80 |
second_output.interrupt_output.assert_not_called() |
| 81 |
second_output.play_sound_icon.assert_not_called() |
| 82 |
second_output.present_text.assert_not_called() |