Debian Codegraph @

tests/unit/test_output_manager.py

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 import importlib.util
2 import threading
3 import time
4 from pathlib import Path
5 from unittest.mock import Mock
6
7 import pytest
8
9 from fenrirscreenreader.core.outputManager import OutputManager
10
11
12 def build_output_manager():
13 settings_manager = Mock()
14 settings_manager.get_setting_as_bool.return_value = True
15 settings_manager.get_setting_as_float.return_value = 1.0
16 sound_driver = Mock()
17 speech_driver = Mock()
18 output_manager = OutputManager()
19 output_manager.env = {
20 "soundIcons": {
21 "ACCEPT": "/tmp/Accept.wav",
22 "ERRORSCREEN": "/tmp/ErrorScreen.wav",
23 },
24 "runtime": {
25 "SettingsManager": settings_manager,
26 "SoundDriver": sound_driver,
27 "SpeechDriver": speech_driver,
28 "DebugManager": Mock(write_debug_out=Mock()),
29 "TextManager": Mock(
30 replace_head_lines=Mock(side_effect=lambda text: text)
31 ),
32 "PunctuationManager": Mock(
33 proceed_punctuation=Mock(
34 side_effect=lambda text, _ignore_punctuation: text
35 )
36 ),
37 },
38 }
39 return output_manager, sound_driver, speech_driver
40
41
42 def load_key_interrupt_module():
43 module_path = (
44 Path(__file__).resolve().parents[2]
45 / "src"
46 / "fenrirscreenreader"
47 / "commands"
48 / "onKeyInput"
49 / "10000-shut_up.py"
50 )
51 spec = importlib.util.spec_from_file_location(
52 "fenrir_key_interrupt", module_path
53 )
54 module = importlib.util.module_from_spec(spec)
55 spec.loader.exec_module(module)
56 return module
57
58
59 @pytest.mark.unit
60 def test_present_text_allows_sound_only_feedback():
61 output_manager, sound_driver, _speech_driver = build_output_manager()
62
63 output_manager.present_text("", sound_icon="Accept", interrupt=False)
64
65 sound_driver.play_sound_file.assert_called_once_with(
66 "/tmp/Accept.wav", False
67 )
68
69
70 @pytest.mark.unit
71 def test_play_sound_supports_error_alias():
72 output_manager, sound_driver, _speech_driver = build_output_manager()
73
74 assert output_manager.play_sound("Error") is True
75
76 sound_driver.play_sound_file.assert_called_once_with(
77 "/tmp/ErrorScreen.wav", True
78 )
79
80
81 @pytest.mark.unit
82 def test_interrupt_output_async_does_not_block_on_slow_cancel():
83 output_manager, _sound_driver, speech_driver = build_output_manager()
84 interrupt_started = threading.Event()
85 release_interrupt = threading.Event()
86
87 def slow_cancel():
88 interrupt_started.set()
89 release_interrupt.wait(timeout=1.0)
90
91 speech_driver.cancel.side_effect = slow_cancel
92
93 start_time = time.monotonic()
94 output_manager.interrupt_output_async()
95 elapsed = time.monotonic() - start_time
96
97 try:
98 assert interrupt_started.wait(timeout=0.2)
99 assert elapsed < 0.2
100 output_manager.interrupt_output_async()
101 assert speech_driver.cancel.call_count == 1
102 finally:
103 release_interrupt.set()
104 output_manager.interrupt_thread.join(timeout=1.0)
105
106
107 @pytest.mark.unit
108 def test_interrupt_output_waits_only_briefly_for_slow_cancel():
109 output_manager, _sound_driver, speech_driver = build_output_manager()
110 interrupt_started = threading.Event()
111 release_interrupt = threading.Event()
112
113 def slow_cancel():
114 interrupt_started.set()
115 release_interrupt.wait(timeout=1.0)
116
117 speech_driver.cancel.side_effect = slow_cancel
118
119 start_time = time.monotonic()
120 output_manager.interrupt_output()
121 elapsed = time.monotonic() - start_time
122
123 try:
124 assert interrupt_started.wait(timeout=0.2)
125 assert elapsed < 0.2
126 output_manager.interrupt_output()
127 assert speech_driver.cancel.call_count == 1
128 finally:
129 release_interrupt.set()
130 output_manager.interrupt_thread.join(timeout=1.0)
131
132
133 @pytest.mark.unit
134 def test_cancel_speech_skips_when_speech_driver_is_busy():
135 output_manager, _sound_driver, speech_driver = build_output_manager()
136 output_manager.speech_driver_lock_timeout = 0.01
137 output_manager.speech_driver_lock.acquire()
138
139 try:
140 start_time = time.monotonic()
141 output_manager.cancel_speech()
142 elapsed = time.monotonic() - start_time
143 finally:
144 output_manager.speech_driver_lock.release()
145
146 assert elapsed < 0.2
147 speech_driver.cancel.assert_not_called()
148
149
150 @pytest.mark.unit
151 def test_speak_text_drops_speech_when_cancel_holds_driver_lock():
152 output_manager, _sound_driver, speech_driver = build_output_manager()
153 output_manager.speech_driver_lock_timeout = 0.01
154 output_manager.speech_driver_lock.acquire()
155
156 try:
157 start_time = time.monotonic()
158 output_manager.speak_text("hello", interrupt=False, flush=False)
159 elapsed = time.monotonic() - start_time
160 finally:
161 output_manager.speech_driver_lock.release()
162
163 assert elapsed < 0.2
164 speech_driver.speak.assert_not_called()
165
166
167 @pytest.mark.unit
168 def test_present_text_records_speech_history_when_enabled():
169 output_manager, _sound_driver, speech_driver = build_output_manager()
170 speech_history_manager = Mock(add_text=Mock())
171 output_manager.env["runtime"]["SpeechHistoryManager"] = (
172 speech_history_manager
173 )
174
175 output_manager.present_text("hello history", interrupt=False)
176
177 speech_history_manager.add_text.assert_called_once_with("hello history")
178 speech_driver.speak.assert_called_once()
179
180
181 @pytest.mark.unit
182 def test_process_mid_word_punctuation_handles_chained_dotted_words():
183 output_manager, _sound_driver, _speech_driver = build_output_manager()
184
185 result = output_manager.process_mid_word_punctuation(
186 ".config archive.tar.gz settings.conf."
187 )
188
189 assert result == (
190 "dot config archive dot tar dot gz settings dot conf."
191 )
192
193
194 @pytest.mark.unit
195 def test_present_text_does_not_record_when_speech_disabled():
196 output_manager, _sound_driver, speech_driver = build_output_manager()
197 speech_history_manager = Mock(add_text=Mock())
198 output_manager.env["runtime"]["SpeechHistoryManager"] = (
199 speech_history_manager
200 )
201
202 def _get_setting_as_bool(section, setting):
203 if (section, setting) == ("speech", "enabled"):
204 return False
205 return True
206
207 output_manager.env["runtime"][
208 "SettingsManager"
209 ].get_setting_as_bool.side_effect = _get_setting_as_bool
210
211 output_manager.present_text("hello history", interrupt=False)
212
213 speech_history_manager.add_text.assert_not_called()
214 speech_driver.speak.assert_not_called()
215
216
217 @pytest.mark.unit
218 def test_key_interrupt_command_uses_nonblocking_interrupt():
219 module = load_key_interrupt_module()
220 settings_manager = Mock()
221 settings_manager.get_setting_as_bool.return_value = True
222 settings_manager.get_setting.return_value = ""
223 output_manager = Mock()
224 env = {
225 "input": {
226 "curr_input": ["KEY_A"],
227 "prev_input": [],
228 },
229 "runtime": {
230 "InputManager": Mock(no_key_pressed=Mock(return_value=False)),
231 "OutputManager": output_manager,
232 "ScreenManager": Mock(is_screen_change=Mock(return_value=False)),
233 "SettingsManager": settings_manager,
234 },
235 }
236 command = module.command()
237 command.initialize(env)
238
239 command.run()
240
241 output_manager.interrupt_output_async.assert_called_once_with()
242 output_manager.interrupt_output.assert_not_called()