| 1 |
""" |
| 2 |
Unit tests for incoming text announcement behavior. |
| 3 |
""" |
| 4 |
|
| 5 |
import importlib.util |
| 6 |
from pathlib import Path |
| 7 |
from unittest.mock import Mock |
| 8 |
|
| 9 |
import pytest |
| 10 |
|
| 11 |
|
| 12 |
def _load_incoming_module(): |
| 13 |
module_path = ( |
| 14 |
Path(__file__).resolve().parents[2] |
| 15 |
/ "fenrirscreenreader" |
| 16 |
/ "commands" |
| 17 |
/ "onScreenUpdate" |
| 18 |
/ "70000-incoming.py" |
| 19 |
) |
| 20 |
spec = importlib.util.spec_from_file_location( |
| 21 |
"fenrir_incoming_command", module_path |
| 22 |
) |
| 23 |
module = importlib.util.module_from_spec(spec) |
| 24 |
spec.loader.exec_module(module) |
| 25 |
return module |
| 26 |
|
| 27 |
|
| 28 |
@pytest.fixture |
| 29 |
def incoming_command(): |
| 30 |
incoming_module = _load_incoming_module() |
| 31 |
output_manager = Mock(present_text=Mock()) |
| 32 |
settings_manager = Mock() |
| 33 |
settings_manager.get_setting_as_bool.side_effect = ( |
| 34 |
lambda section, setting: True |
| 35 |
if (section, setting) == ("speech", "auto_read_incoming") |
| 36 |
else False |
| 37 |
) |
| 38 |
settings_manager.get_setting_as_float.side_effect = ( |
| 39 |
lambda section, setting: { |
| 40 |
("speech", "rapid_update_window"): 0.3, |
| 41 |
("speech", "batch_flush_interval"): 0.5, |
| 42 |
}[(section, setting)] |
| 43 |
) |
| 44 |
settings_manager.get_setting_as_int.side_effect = ( |
| 45 |
lambda section, setting: { |
| 46 |
("speech", "rapid_update_threshold"): 5, |
| 47 |
("speech", "flood_line_threshold"): 500, |
| 48 |
("speech", "max_batch_lines"): 100, |
| 49 |
}[(section, setting)] |
| 50 |
) |
| 51 |
|
| 52 |
env = { |
| 53 |
"runtime": { |
| 54 |
"OutputManager": output_manager, |
| 55 |
"SettingsManager": settings_manager, |
| 56 |
"ScreenManager": Mock( |
| 57 |
is_delta=Mock(return_value=True), |
| 58 |
is_screen_change=Mock(return_value=False), |
| 59 |
), |
| 60 |
}, |
| 61 |
"screen": { |
| 62 |
"new_delta": "", |
| 63 |
"new_delta_is_typing": False, |
| 64 |
"old_content_text": "", |
| 65 |
"new_content_text": "", |
| 66 |
"old_cursor": {"x": 0, "y": 0}, |
| 67 |
"new_cursor": {"x": 0, "y": 0}, |
| 68 |
"newNegativeDelta": "", |
| 69 |
}, |
| 70 |
"commandBuffer": {}, |
| 71 |
} |
| 72 |
|
| 73 |
command = incoming_module.command() |
| 74 |
command.initialize(env) |
| 75 |
return command, env, output_manager |
| 76 |
|
| 77 |
|
| 78 |
@pytest.mark.unit |
| 79 |
class TestIncomingCommand: |
| 80 |
"""Test incoming text filtering for chat-like screen updates.""" |
| 81 |
|
| 82 |
def test_prefers_inserted_chat_line_over_changed_headers( |
| 83 |
self, incoming_command |
| 84 |
): |
| 85 |
command, env, output_manager = incoming_command |
| 86 |
env["screen"]["old_content_text"] = "\n".join( |
| 87 |
[ |
| 88 |
"Status old", |
| 89 |
"Users old", |
| 90 |
"UserA: hi", |
| 91 |
"UserB: hello", |
| 92 |
"> ", |
| 93 |
] |
| 94 |
) |
| 95 |
env["screen"]["new_content_text"] = "\n".join( |
| 96 |
[ |
| 97 |
"Status new", |
| 98 |
"Users new", |
| 99 |
"UserB: hello", |
| 100 |
"UserC: test", |
| 101 |
"> ", |
| 102 |
] |
| 103 |
) |
| 104 |
env["screen"]["new_delta"] = "\n".join( |
| 105 |
["Status new", "Users new", "UserC: test"] |
| 106 |
) |
| 107 |
|
| 108 |
command.run() |
| 109 |
|
| 110 |
output_manager.present_text.assert_called_once_with( |
| 111 |
"UserC: test", interrupt=False, flush=False |
| 112 |
) |
| 113 |
|
| 114 |
def test_keeps_header_update_when_no_lower_screen_insert_exists( |
| 115 |
self, incoming_command |
| 116 |
): |
| 117 |
command, env, output_manager = incoming_command |
| 118 |
env["screen"]["old_content_text"] = "\n".join( |
| 119 |
[ |
| 120 |
"Status old", |
| 121 |
"Users old", |
| 122 |
"UserA: hi", |
| 123 |
"UserB: hello", |
| 124 |
] |
| 125 |
) |
| 126 |
env["screen"]["new_content_text"] = "\n".join( |
| 127 |
[ |
| 128 |
"Status new", |
| 129 |
"Users new", |
| 130 |
"UserA: hi", |
| 131 |
"UserB: hello", |
| 132 |
] |
| 133 |
) |
| 134 |
env["screen"]["new_delta"] = "\n".join(["Status new", "Users new"]) |
| 135 |
|
| 136 |
command.run() |
| 137 |
|
| 138 |
output_manager.present_text.assert_called_once_with( |
| 139 |
"Status new\nUsers new", interrupt=False, flush=False |
| 140 |
) |
| 141 |
|
| 142 |
def test_skips_typing_delta(self, incoming_command): |
| 143 |
command, env, output_manager = incoming_command |
| 144 |
env["screen"]["new_delta"] = "x" |
| 145 |
env["screen"]["new_delta_is_typing"] = True |
| 146 |
|
| 147 |
command.run() |
| 148 |
|
| 149 |
output_manager.present_text.assert_not_called() |
| 150 |
|
| 151 |
def test_dialog_button_paint_is_read_after_following_message( |
| 152 |
self, incoming_command |
| 153 |
): |
| 154 |
command, env, output_manager = incoming_command |
| 155 |
env["screen"]["new_content_text"] = "\n".join( |
| 156 |
[ |
| 157 |
"".ljust(20), |
| 158 |
"< OK >".center(20), |
| 159 |
"".ljust(20), |
| 160 |
] |
| 161 |
) |
| 162 |
env["screen"]["new_delta"] = "\n".join( |
| 163 |
[ |
| 164 |
" ", |
| 165 |
" < OK >", |
| 166 |
" ", |
| 167 |
] |
| 168 |
) |
| 169 |
|
| 170 |
command.run() |
| 171 |
|
| 172 |
output_manager.present_text.assert_not_called() |
| 173 |
|
| 174 |
env["screen"]["old_content_text"] = env["screen"]["new_content_text"] |
| 175 |
env["screen"]["new_content_text"] = "\n".join( |
| 176 |
[ |
| 177 |
"This is a test".ljust(20), |
| 178 |
"< OK >".center(20), |
| 179 |
"".ljust(20), |
| 180 |
] |
| 181 |
) |
| 182 |
env["screen"]["new_delta"] = "This is a test" |
| 183 |
|
| 184 |
command.run() |
| 185 |
|
| 186 |
output_manager.present_text.assert_called_once_with( |
| 187 |
"This is a test\n< OK >", interrupt=False, flush=False |
| 188 |
) |
| 189 |
|
| 190 |
def test_single_word_output_is_not_treated_as_dialog_button( |
| 191 |
self, incoming_command |
| 192 |
): |
| 193 |
command, env, output_manager = incoming_command |
| 194 |
env["screen"]["new_content_text"] = "Ready" |
| 195 |
env["screen"]["new_delta"] = "Ready" |
| 196 |
|
| 197 |
command.run() |
| 198 |
|
| 199 |
output_manager.present_text.assert_called_once_with( |
| 200 |
"Ready", interrupt=False, flush=False |
| 201 |
) |
| 202 |
|
| 203 |
def test_dialog_button_is_not_preferred_over_message( |
| 204 |
self, incoming_command |
| 205 |
): |
| 206 |
command, env, output_manager = incoming_command |
| 207 |
env["screen"]["old_content_text"] = "\n".join( |
| 208 |
[ |
| 209 |
'bash-5.3$ dialog --msgbox "This is a test" -1 -1'.ljust(80), |
| 210 |
"".ljust(80), |
| 211 |
"".ljust(80), |
| 212 |
"".ljust(80), |
| 213 |
"".ljust(80), |
| 214 |
"".ljust(80), |
| 215 |
] |
| 216 |
) |
| 217 |
env["screen"]["new_content_text"] = "\n".join( |
| 218 |
[ |
| 219 |
"".ljust(80), |
| 220 |
" This is a test".ljust(80), |
| 221 |
"".ljust(80), |
| 222 |
"".ljust(80), |
| 223 |
" < OK >".ljust(80), |
| 224 |
"".ljust(80), |
| 225 |
] |
| 226 |
) |
| 227 |
env["screen"]["new_delta"] = "\n".join( |
| 228 |
[ |
| 229 |
" ", |
| 230 |
" This is a test ", |
| 231 |
" < OK > ", |
| 232 |
] |
| 233 |
) |
| 234 |
|
| 235 |
command.run() |
| 236 |
|
| 237 |
output_manager.present_text.assert_called_once_with( |
| 238 |
" \n This is a test \n < OK > ", |
| 239 |
interrupt=False, |
| 240 |
flush=False, |
| 241 |
) |