| 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 |
/ "fenrirscreenreader" |
| 46 |
/ "commands" |
| 47 |
/ "onKeyInput" |
| 48 |
/ "10000-shut_up.py" |
| 49 |
) |
| 50 |
spec = importlib.util.spec_from_file_location( |
| 51 |
"fenrir_key_interrupt", module_path |
| 52 |
) |
| 53 |
module = importlib.util.module_from_spec(spec) |
| 54 |
spec.loader.exec_module(module) |
| 55 |
return module |
| 56 |
|
| 57 |
|
| 58 |
@pytest.mark.unit |
| 59 |
def test_present_text_allows_sound_only_feedback(): |
| 60 |
output_manager, sound_driver, _speech_driver = build_output_manager() |
| 61 |
|
| 62 |
output_manager.present_text("", sound_icon="Accept", interrupt=False) |
| 63 |
|
| 64 |
sound_driver.play_sound_file.assert_called_once_with( |
| 65 |
"/tmp/Accept.wav", False |
| 66 |
) |
| 67 |
|
| 68 |
|
| 69 |
@pytest.mark.unit |
| 70 |
def test_play_sound_supports_error_alias(): |
| 71 |
output_manager, sound_driver, _speech_driver = build_output_manager() |
| 72 |
|
| 73 |
assert output_manager.play_sound("Error") is True |
| 74 |
|
| 75 |
sound_driver.play_sound_file.assert_called_once_with( |
| 76 |
"/tmp/ErrorScreen.wav", True |
| 77 |
) |
| 78 |
|
| 79 |
|
| 80 |
@pytest.mark.unit |
| 81 |
def test_interrupt_output_async_does_not_block_on_slow_cancel(): |
| 82 |
output_manager, _sound_driver, speech_driver = build_output_manager() |
| 83 |
interrupt_started = threading.Event() |
| 84 |
release_interrupt = threading.Event() |
| 85 |
|
| 86 |
def slow_cancel(): |
| 87 |
interrupt_started.set() |
| 88 |
release_interrupt.wait(timeout=1.0) |
| 89 |
|
| 90 |
speech_driver.cancel.side_effect = slow_cancel |
| 91 |
|
| 92 |
start_time = time.monotonic() |
| 93 |
output_manager.interrupt_output_async() |
| 94 |
elapsed = time.monotonic() - start_time |
| 95 |
|
| 96 |
try: |
| 97 |
assert interrupt_started.wait(timeout=0.2) |
| 98 |
assert elapsed < 0.2 |
| 99 |
output_manager.interrupt_output_async() |
| 100 |
assert speech_driver.cancel.call_count == 1 |
| 101 |
finally: |
| 102 |
release_interrupt.set() |
| 103 |
output_manager.interrupt_thread.join(timeout=1.0) |
| 104 |
|
| 105 |
|
| 106 |
@pytest.mark.unit |
| 107 |
def test_interrupt_output_waits_only_briefly_for_slow_cancel(): |
| 108 |
output_manager, _sound_driver, speech_driver = build_output_manager() |
| 109 |
interrupt_started = threading.Event() |
| 110 |
release_interrupt = threading.Event() |
| 111 |
|
| 112 |
def slow_cancel(): |
| 113 |
interrupt_started.set() |
| 114 |
release_interrupt.wait(timeout=1.0) |
| 115 |
|
| 116 |
speech_driver.cancel.side_effect = slow_cancel |
| 117 |
|
| 118 |
start_time = time.monotonic() |
| 119 |
output_manager.interrupt_output() |
| 120 |
elapsed = time.monotonic() - start_time |
| 121 |
|
| 122 |
try: |
| 123 |
assert interrupt_started.wait(timeout=0.2) |
| 124 |
assert elapsed < 0.2 |
| 125 |
output_manager.interrupt_output() |
| 126 |
assert speech_driver.cancel.call_count == 1 |
| 127 |
finally: |
| 128 |
release_interrupt.set() |
| 129 |
output_manager.interrupt_thread.join(timeout=1.0) |
| 130 |
|
| 131 |
|
| 132 |
@pytest.mark.unit |
| 133 |
def test_cancel_speech_skips_when_speech_driver_is_busy(): |
| 134 |
output_manager, _sound_driver, speech_driver = build_output_manager() |
| 135 |
output_manager.speech_driver_lock_timeout = 0.01 |
| 136 |
output_manager.speech_driver_lock.acquire() |
| 137 |
|
| 138 |
try: |
| 139 |
start_time = time.monotonic() |
| 140 |
output_manager.cancel_speech() |
| 141 |
elapsed = time.monotonic() - start_time |
| 142 |
finally: |
| 143 |
output_manager.speech_driver_lock.release() |
| 144 |
|
| 145 |
assert elapsed < 0.2 |
| 146 |
speech_driver.cancel.assert_not_called() |
| 147 |
|
| 148 |
|
| 149 |
@pytest.mark.unit |
| 150 |
def test_speak_text_drops_speech_when_cancel_holds_driver_lock(): |
| 151 |
output_manager, _sound_driver, speech_driver = build_output_manager() |
| 152 |
output_manager.speech_driver_lock_timeout = 0.01 |
| 153 |
output_manager.speech_driver_lock.acquire() |
| 154 |
|
| 155 |
try: |
| 156 |
start_time = time.monotonic() |
| 157 |
output_manager.speak_text("hello", interrupt=False, flush=False) |
| 158 |
elapsed = time.monotonic() - start_time |
| 159 |
finally: |
| 160 |
output_manager.speech_driver_lock.release() |
| 161 |
|
| 162 |
assert elapsed < 0.2 |
| 163 |
speech_driver.speak.assert_not_called() |
| 164 |
|
| 165 |
|
| 166 |
@pytest.mark.unit |
| 167 |
def test_present_text_records_speech_history_when_enabled(): |
| 168 |
output_manager, _sound_driver, speech_driver = build_output_manager() |
| 169 |
speech_history_manager = Mock(add_text=Mock()) |
| 170 |
output_manager.env["runtime"]["SpeechHistoryManager"] = ( |
| 171 |
speech_history_manager |
| 172 |
) |
| 173 |
|
| 174 |
output_manager.present_text("hello history", interrupt=False) |
| 175 |
|
| 176 |
speech_history_manager.add_text.assert_called_once_with("hello history") |
| 177 |
speech_driver.speak.assert_called_once() |
| 178 |
|
| 179 |
|
| 180 |
@pytest.mark.unit |
| 181 |
def test_process_mid_word_punctuation_handles_chained_dotted_words(): |
| 182 |
output_manager, _sound_driver, _speech_driver = build_output_manager() |
| 183 |
|
| 184 |
result = output_manager.process_mid_word_punctuation( |
| 185 |
".config archive.tar.gz settings.conf." |
| 186 |
) |
| 187 |
|
| 188 |
assert result == ( |
| 189 |
"dot config archive dot tar dot gz settings dot conf." |
| 190 |
) |
| 191 |
|
| 192 |
|
| 193 |
@pytest.mark.unit |
| 194 |
def test_present_text_does_not_record_when_speech_disabled(): |
| 195 |
output_manager, _sound_driver, speech_driver = build_output_manager() |
| 196 |
speech_history_manager = Mock(add_text=Mock()) |
| 197 |
output_manager.env["runtime"]["SpeechHistoryManager"] = ( |
| 198 |
speech_history_manager |
| 199 |
) |
| 200 |
|
| 201 |
def _get_setting_as_bool(section, setting): |
| 202 |
if (section, setting) == ("speech", "enabled"): |
| 203 |
return False |
| 204 |
return True |
| 205 |
|
| 206 |
output_manager.env["runtime"][ |
| 207 |
"SettingsManager" |
| 208 |
].get_setting_as_bool.side_effect = _get_setting_as_bool |
| 209 |
|
| 210 |
output_manager.present_text("hello history", interrupt=False) |
| 211 |
|
| 212 |
speech_history_manager.add_text.assert_not_called() |
| 213 |
speech_driver.speak.assert_not_called() |
| 214 |
|
| 215 |
|
| 216 |
@pytest.mark.unit |
| 217 |
def test_key_interrupt_command_uses_nonblocking_interrupt(): |
| 218 |
module = load_key_interrupt_module() |
| 219 |
settings_manager = Mock() |
| 220 |
settings_manager.get_setting_as_bool.return_value = True |
| 221 |
settings_manager.get_setting.return_value = "" |
| 222 |
output_manager = Mock() |
| 223 |
env = { |
| 224 |
"input": { |
| 225 |
"curr_input": ["KEY_A"], |
| 226 |
"prev_input": [], |
| 227 |
}, |
| 228 |
"runtime": { |
| 229 |
"InputManager": Mock(no_key_pressed=Mock(return_value=False)), |
| 230 |
"OutputManager": output_manager, |
| 231 |
"ScreenManager": Mock(is_screen_change=Mock(return_value=False)), |
| 232 |
"SettingsManager": settings_manager, |
| 233 |
}, |
| 234 |
} |
| 235 |
command = module.command() |
| 236 |
command.initialize(env) |
| 237 |
|
| 238 |
command.run() |
| 239 |
|
| 240 |
output_manager.interrupt_output_async.assert_called_once_with() |
| 241 |
output_manager.interrupt_output.assert_not_called() |