Debian Codegraph @

tests/unit/test_time_command.py

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
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 / "src"
15 / "fenrirscreenreader"
16 / "commands"
17 / "onHeartBeat"
18 / "76000-time.py"
19 )
20 spec = importlib.util.spec_from_file_location("fenrir_time_command", module_path)
21 module = importlib.util.module_from_spec(spec)
22 spec.loader.exec_module(module)
23 return module
24
25
26 def _create_environment(output_manager):
27 now = datetime.datetime.now()
28 settings_manager = Mock()
29 settings_manager.get_setting.side_effect = lambda section, setting: {
30 ("time", "on_minutes"): str(now.minute).zfill(2),
31 ("general", "date_format"): "%A, %B %d, %Y",
32 ("general", "time_format"): "%I:%M%P",
33 }[(section, setting)]
34 settings_manager.get_setting_as_int.side_effect = lambda section, setting: {
35 ("time", "delay_sec"): 0,
36 }[(section, setting)]
37 settings_manager.get_setting_as_bool.side_effect = lambda section, setting: {
38 ("time", "enabled"): True,
39 ("time", "present_date"): True,
40 ("time", "present_time"): True,
41 ("time", "interrupt"): False,
42 ("time", "announce"): True,
43 }[(section, setting)]
44
45 return {
46 "runtime": {
47 "OutputManager": output_manager,
48 "SettingsManager": settings_manager,
49 }
50 }
51
52
53 def test_time_announcement_lock_suppresses_second_instance(tmp_path):
54 time_module = _load_time_module()
55 lock_path = tmp_path / "time-announcement.lock"
56 first_output = Mock(
57 interrupt_output=Mock(),
58 play_sound_icon=Mock(),
59 present_text=Mock(),
60 )
61 second_output = Mock(
62 interrupt_output=Mock(),
63 play_sound_icon=Mock(),
64 present_text=Mock(),
65 )
66 first_command = time_module.command()
67 second_command = time_module.command()
68
69 first_command.initialize(_create_environment(first_output))
70 second_command.initialize(_create_environment(second_output))
71 first_command._get_announcement_lock_path = lambda: str(lock_path)
72 second_command._get_announcement_lock_path = lambda: str(lock_path)
73 first_command.last_time -= datetime.timedelta(hours=1)
74 second_command.last_time -= datetime.timedelta(hours=1)
75
76 first_command.run()
77 second_command.run()
78
79 first_output.play_sound_icon.assert_called_once_with("announce")
80 first_output.present_text.assert_called()
81 second_output.interrupt_output.assert_not_called()
82 second_output.play_sound_icon.assert_not_called()
83 second_output.present_text.assert_not_called()