| 1 |
import importlib.util |
| 2 |
from pathlib import Path |
| 3 |
from unittest.mock import Mock |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
|
| 8 |
def _load_progress_module(): |
| 9 |
module_path = ( |
| 10 |
Path(__file__).resolve().parents[2] |
| 11 |
/ "fenrirscreenreader" |
| 12 |
/ "commands" |
| 13 |
/ "onScreenUpdate" |
| 14 |
/ "65000-progress_detector.py" |
| 15 |
) |
| 16 |
spec = importlib.util.spec_from_file_location( |
| 17 |
"fenrir_progress_detector", module_path |
| 18 |
) |
| 19 |
module = importlib.util.module_from_spec(spec) |
| 20 |
spec.loader.exec_module(module) |
| 21 |
return module |
| 22 |
|
| 23 |
|
| 24 |
@pytest.mark.unit |
| 25 |
def test_progress_detector_skips_typing_delta(): |
| 26 |
progress_module = _load_progress_module() |
| 27 |
command = progress_module.command() |
| 28 |
command.env = { |
| 29 |
"commandBuffer": {"progress_monitoring": True}, |
| 30 |
"runtime": {"DebugManager": Mock(write_debug_out=Mock())}, |
| 31 |
"screen": {"new_delta": "x", "new_delta_is_typing": True}, |
| 32 |
} |
| 33 |
command.is_current_line_prompt = Mock(return_value=False) |
| 34 |
command.is_real_progress_update = Mock(return_value=True) |
| 35 |
command.detect_progress = Mock() |
| 36 |
|
| 37 |
command.run() |
| 38 |
|
| 39 |
command.is_real_progress_update.assert_not_called() |
| 40 |
command.detect_progress.assert_not_called() |
| 41 |
|
| 42 |
|
| 43 |
@pytest.mark.unit |
| 44 |
def test_progress_detector_allows_long_tqdm_transfer_delta(): |
| 45 |
progress_module = _load_progress_module() |
| 46 |
command = progress_module.command() |
| 47 |
sample = ( |
| 48 |
"88%|" |
| 49 |
"████████████████████████████████████████████████████████████████" |
| 50 |
"████████████████████████████████████████████████████████████████" |
| 51 |
"████████████████████████████████████████████████████████████████" |
| 52 |
"█████████████████████████▊ " |
| 53 |
"| 843M/954M [00:54<00:07, 15.2MB/s]" |
| 54 |
) |
| 55 |
command.env = { |
| 56 |
"commandBuffer": {"progress_monitoring": True}, |
| 57 |
"runtime": { |
| 58 |
"DebugManager": Mock(write_debug_out=Mock()), |
| 59 |
"ScreenManager": Mock(is_screen_change=Mock(return_value=False)), |
| 60 |
"CursorManager": Mock(is_cursor_vertical_move=Mock(return_value=False)), |
| 61 |
}, |
| 62 |
"screen": { |
| 63 |
"new_delta": sample, |
| 64 |
"new_content_text": sample, |
| 65 |
"old_cursor": {"x": 0, "y": 0}, |
| 66 |
"new_cursor": {"x": 0, "y": 0}, |
| 67 |
}, |
| 68 |
} |
| 69 |
|
| 70 |
assert len(sample) > 200 |
| 71 |
assert command.is_real_progress_update() |
| 72 |
|
| 73 |
|
| 74 |
@pytest.mark.unit |
| 75 |
def test_progress_detector_beeps_for_long_tqdm_transfer_delta(): |
| 76 |
progress_module = _load_progress_module() |
| 77 |
command = progress_module.command() |
| 78 |
sample = ( |
| 79 |
"90%|" |
| 80 |
"████████████████████████████████████████████████████████████████" |
| 81 |
"████████████████████████████████████████████████████████████████" |
| 82 |
"████████████████████████████████████████████████████████████████" |
| 83 |
"█████████████████████████████████████▍ " |
| 84 |
"| 856M/954M [00:56<00:14, 6.78MB/s]" |
| 85 |
) |
| 86 |
command.env = { |
| 87 |
"commandBuffer": { |
| 88 |
"progress_monitoring": True, |
| 89 |
"lastProgressValue": -1, |
| 90 |
"lastProgressTime": 0, |
| 91 |
}, |
| 92 |
"runtime": { |
| 93 |
"DebugManager": Mock(write_debug_out=Mock()), |
| 94 |
"ScreenManager": Mock(is_screen_change=Mock(return_value=False)), |
| 95 |
"CursorManager": Mock(is_cursor_vertical_move=Mock(return_value=False)), |
| 96 |
}, |
| 97 |
"screen": { |
| 98 |
"new_delta": sample, |
| 99 |
"new_delta_is_typing": False, |
| 100 |
"new_content_text": sample, |
| 101 |
"old_cursor": {"x": 0, "y": 0}, |
| 102 |
"new_cursor": {"x": 0, "y": 0}, |
| 103 |
}, |
| 104 |
} |
| 105 |
command.play_progress_tone = Mock() |
| 106 |
|
| 107 |
command.run() |
| 108 |
|
| 109 |
command.play_progress_tone.assert_called_once_with(90.0) |
| 110 |
assert command.env["commandBuffer"]["lastProgressValue"] == 90.0 |
| 111 |
|
| 112 |
|
| 113 |
@pytest.mark.unit |
| 114 |
def test_progress_detector_beeps_for_interruptible_status_without_ellipsis(): |
| 115 |
progress_module = _load_progress_module() |
| 116 |
command = progress_module.command() |
| 117 |
sample = "◦ Files: (1m 04s • esc to interrupt)" |
| 118 |
command.env = { |
| 119 |
"commandBuffer": { |
| 120 |
"progress_monitoring": True, |
| 121 |
"lastProgressValue": -1, |
| 122 |
"lastProgressTime": 0, |
| 123 |
}, |
| 124 |
"runtime": { |
| 125 |
"DebugManager": Mock(write_debug_out=Mock()), |
| 126 |
"ScreenManager": Mock(is_screen_change=Mock(return_value=False)), |
| 127 |
"CursorManager": Mock(is_cursor_vertical_move=Mock(return_value=False)), |
| 128 |
}, |
| 129 |
"screen": { |
| 130 |
"new_delta": sample, |
| 131 |
"new_delta_is_typing": False, |
| 132 |
"new_content_text": sample, |
| 133 |
"old_cursor": {"x": 0, "y": 0}, |
| 134 |
"new_cursor": {"x": 0, "y": 0}, |
| 135 |
}, |
| 136 |
} |
| 137 |
command.play_activity_beep = Mock() |
| 138 |
|
| 139 |
command.run() |
| 140 |
|
| 141 |
command.play_activity_beep.assert_called_once_with() |
| 142 |
|
| 143 |
|
| 144 |
@pytest.mark.unit |
| 145 |
def test_progress_detector_beeps_for_plain_working_elapsed_status(): |
| 146 |
progress_module = _load_progress_module() |
| 147 |
command = progress_module.command() |
| 148 |
sample = "Working (21s • esc to interrupt)" |
| 149 |
command.env = { |
| 150 |
"commandBuffer": { |
| 151 |
"progress_monitoring": True, |
| 152 |
"lastProgressValue": -1, |
| 153 |
"lastProgressTime": 0, |
| 154 |
}, |
| 155 |
"runtime": { |
| 156 |
"DebugManager": Mock(write_debug_out=Mock()), |
| 157 |
"ScreenManager": Mock(is_screen_change=Mock(return_value=False)), |
| 158 |
"CursorManager": Mock(is_cursor_vertical_move=Mock(return_value=False)), |
| 159 |
}, |
| 160 |
"screen": { |
| 161 |
"new_delta": sample, |
| 162 |
"new_delta_is_typing": False, |
| 163 |
"new_content_text": sample, |
| 164 |
"old_cursor": {"x": 0, "y": 0}, |
| 165 |
"new_cursor": {"x": 0, "y": 0}, |
| 166 |
}, |
| 167 |
} |
| 168 |
command.play_activity_beep = Mock() |
| 169 |
|
| 170 |
command.run() |
| 171 |
|
| 172 |
command.play_activity_beep.assert_called_once_with() |
| 173 |
|
| 174 |
|
| 175 |
@pytest.mark.unit |
| 176 |
def test_progress_detector_allows_long_key_value_status_delta(): |
| 177 |
progress_module = _load_progress_module() |
| 178 |
command = progress_module.command() |
| 179 |
sample = ( |
| 180 |
"frame=33480 fps=429 q=28.0 size= 195328KiB " |
| 181 |
"time=00:23:16.26 bitrate=1146.0kbits/s dup=0 drop=1 " |
| 182 |
"speed=17.9x elapsed=0:01:18.01 " |
| 183 |
"filter=scale,format output=archive-video-final-render-pass.mkv " |
| 184 |
"metadata=preserve-chapters-and-stream-layout" |
| 185 |
) |
| 186 |
command.env = { |
| 187 |
"commandBuffer": {"progress_monitoring": True}, |
| 188 |
"runtime": { |
| 189 |
"DebugManager": Mock(write_debug_out=Mock()), |
| 190 |
"ScreenManager": Mock(is_screen_change=Mock(return_value=False)), |
| 191 |
"CursorManager": Mock(is_cursor_vertical_move=Mock(return_value=False)), |
| 192 |
}, |
| 193 |
"screen": { |
| 194 |
"new_delta": sample, |
| 195 |
"new_content_text": sample, |
| 196 |
"old_cursor": {"x": 0, "y": 0}, |
| 197 |
"new_cursor": {"x": 0, "y": 0}, |
| 198 |
}, |
| 199 |
} |
| 200 |
|
| 201 |
assert len(sample) > 200 |
| 202 |
assert command.is_real_progress_update() |
| 203 |
|
| 204 |
|
| 205 |
@pytest.mark.unit |
| 206 |
def test_progress_detector_beeps_for_ffmpeg_status_line(): |
| 207 |
progress_module = _load_progress_module() |
| 208 |
command = progress_module.command() |
| 209 |
sample = ( |
| 210 |
"frame=33480 fps=429 q=28.0 size= 195328KiB " |
| 211 |
"time=00:23:16.26 bitrate=1146.0kbits/s dup=0 drop=1 " |
| 212 |
"speed=17.9x elapsed=0:01:18.01" |
| 213 |
) |
| 214 |
command.env = { |
| 215 |
"commandBuffer": { |
| 216 |
"progress_monitoring": True, |
| 217 |
"lastProgressValue": -1, |
| 218 |
"lastProgressTime": 0, |
| 219 |
}, |
| 220 |
"runtime": { |
| 221 |
"DebugManager": Mock(write_debug_out=Mock()), |
| 222 |
"ScreenManager": Mock(is_screen_change=Mock(return_value=False)), |
| 223 |
"CursorManager": Mock(is_cursor_vertical_move=Mock(return_value=False)), |
| 224 |
}, |
| 225 |
"screen": { |
| 226 |
"new_delta": sample, |
| 227 |
"new_delta_is_typing": False, |
| 228 |
"new_content_text": sample, |
| 229 |
"old_cursor": {"x": 0, "y": 0}, |
| 230 |
"new_cursor": {"x": 0, "y": 0}, |
| 231 |
}, |
| 232 |
} |
| 233 |
command.play_activity_beep = Mock() |
| 234 |
|
| 235 |
command.run() |
| 236 |
|
| 237 |
command.play_activity_beep.assert_called_once_with() |
| 238 |
|
| 239 |
|
| 240 |
@pytest.mark.unit |
| 241 |
def test_progress_detector_ignores_generic_key_value_text(): |
| 242 |
progress_module = _load_progress_module() |
| 243 |
command = progress_module.command() |
| 244 |
sample = "host=server mode=debug user=Username retry=3 status=ready" |
| 245 |
command.env = { |
| 246 |
"commandBuffer": { |
| 247 |
"progress_monitoring": True, |
| 248 |
"lastProgressValue": -1, |
| 249 |
"lastProgressTime": 0, |
| 250 |
}, |
| 251 |
"runtime": { |
| 252 |
"DebugManager": Mock(write_debug_out=Mock()), |
| 253 |
"ScreenManager": Mock(is_screen_change=Mock(return_value=False)), |
| 254 |
"CursorManager": Mock(is_cursor_vertical_move=Mock(return_value=False)), |
| 255 |
}, |
| 256 |
"screen": { |
| 257 |
"new_delta": sample, |
| 258 |
"new_delta_is_typing": False, |
| 259 |
"new_content_text": sample, |
| 260 |
"old_cursor": {"x": 0, "y": 0}, |
| 261 |
"new_cursor": {"x": 0, "y": 0}, |
| 262 |
}, |
| 263 |
} |
| 264 |
command.play_activity_beep = Mock() |
| 265 |
command.play_progress_tone = Mock() |
| 266 |
|
| 267 |
command.run() |
| 268 |
|
| 269 |
command.play_activity_beep.assert_not_called() |
| 270 |
command.play_progress_tone.assert_not_called() |
| 271 |
|
| 272 |
|
| 273 |
@pytest.mark.unit |
| 274 |
def test_progress_detector_beeps_for_pacman_total_in_multiline_delta(): |
| 275 |
progress_module = _load_progress_module() |
| 276 |
command = progress_module.command() |
| 277 |
total_line = ( |
| 278 |
"Total ( 3/749) " |
| 279 |
"830.7 MiB 4.70 MiB/s 04:05 " |
| 280 |
"[#################################################" |
| 281 |
"-----------------------------------------------------------------------] 41%" |
| 282 |
) |
| 283 |
sample = ( |
| 284 |
"package-one 125.0 MiB 2.10 MiB/s 00:12 " |
| 285 |
"[####################--------------------] 50%\n" |
| 286 |
+ total_line |
| 287 |
) |
| 288 |
command.env = { |
| 289 |
"commandBuffer": { |
| 290 |
"progress_monitoring": True, |
| 291 |
"lastProgressValue": -1, |
| 292 |
"lastProgressTime": 0, |
| 293 |
}, |
| 294 |
"runtime": { |
| 295 |
"DebugManager": Mock(write_debug_out=Mock()), |
| 296 |
"ScreenManager": Mock(is_screen_change=Mock(return_value=False)), |
| 297 |
"CursorManager": Mock(is_cursor_vertical_move=Mock(return_value=False)), |
| 298 |
}, |
| 299 |
"screen": { |
| 300 |
"new_delta": sample, |
| 301 |
"new_delta_is_typing": False, |
| 302 |
"new_content_text": sample, |
| 303 |
"old_cursor": {"x": 0, "y": 0}, |
| 304 |
"new_cursor": {"x": 0, "y": 0}, |
| 305 |
}, |
| 306 |
} |
| 307 |
command.play_progress_tone = Mock() |
| 308 |
|
| 309 |
command.run() |
| 310 |
|
| 311 |
command.play_progress_tone.assert_called_once_with(41.0) |
| 312 |
assert command.env["commandBuffer"]["lastProgressValue"] == 41.0 |
| 313 |
|
| 314 |
|
| 315 |
@pytest.mark.unit |
| 316 |
def test_explicit_progress_delta_ignores_percentage_in_bracketed_prose(): |
| 317 |
progress_module = _load_progress_module() |
| 318 |
command = progress_module.command() |
| 319 |
|
| 320 |
assert not command.is_explicit_progress_delta( |
| 321 |
"Release notes [issue #749] are 41% complete" |
| 322 |
) |
| 323 |
|
| 324 |
|
| 325 |
@pytest.mark.unit |
| 326 |
def test_progress_detector_beeps_for_curl_classic_repaint_with_trailing_text(): |
| 327 |
progress_module = _load_progress_module() |
| 328 |
command = progress_module.command() |
| 329 |
sample = ( |
| 330 |
"53 537.8M 53 285.8M 0 0 9.48M 0 00:56 00:30 00:26 9.94M " |
| 331 |
"Executing cabextract -q -d /home/Username/.local/wine64/dosdevices/" |
| 332 |
"c:/windows/syswow64 -F dx8vb.dll /home/Username/.cache/winetricks/" |
| 333 |
"dx8vb/DX81NTger.exe" |
| 334 |
) |
| 335 |
command.env = { |
| 336 |
"commandBuffer": { |
| 337 |
"progress_monitoring": True, |
| 338 |
"lastProgressValue": -1, |
| 339 |
"lastProgressTime": 0, |
| 340 |
}, |
| 341 |
"runtime": { |
| 342 |
"DebugManager": Mock(write_debug_out=Mock()), |
| 343 |
"ScreenManager": Mock(is_screen_change=Mock(return_value=False)), |
| 344 |
"CursorManager": Mock(is_cursor_vertical_move=Mock(return_value=False)), |
| 345 |
}, |
| 346 |
"screen": { |
| 347 |
"new_delta": sample, |
| 348 |
"new_delta_is_typing": False, |
| 349 |
"new_content_text": sample, |
| 350 |
"old_cursor": {"x": 0, "y": 0}, |
| 351 |
"new_cursor": {"x": 0, "y": 0}, |
| 352 |
}, |
| 353 |
} |
| 354 |
command.play_progress_tone = Mock() |
| 355 |
|
| 356 |
assert len(sample) > 200 |
| 357 |
|
| 358 |
command.run() |
| 359 |
|
| 360 |
command.play_progress_tone.assert_called_once_with(53.0) |
| 361 |
assert command.env["commandBuffer"]["lastProgressValue"] == 53.0 |
| 362 |
|
| 363 |
|
| 364 |
@pytest.mark.unit |
| 365 |
def test_progress_detector_ignores_curl_like_fields_with_mismatched_percentages(): |
| 366 |
progress_module = _load_progress_module() |
| 367 |
command = progress_module.command() |
| 368 |
sample = ( |
| 369 |
"53 537.8M 52 285.8M 0 0 9.48M 0 00:56 00:30 00:26 9.94M " |
| 370 |
"ordinary numeric report with enough trailing text to cross the long " |
| 371 |
"delta filter without representing a coherent download progress row " |
| 372 |
"or providing trustworthy percentage information" |
| 373 |
) |
| 374 |
command.env = { |
| 375 |
"commandBuffer": { |
| 376 |
"progress_monitoring": True, |
| 377 |
"lastProgressValue": -1, |
| 378 |
"lastProgressTime": 0, |
| 379 |
}, |
| 380 |
"runtime": { |
| 381 |
"DebugManager": Mock(write_debug_out=Mock()), |
| 382 |
"ScreenManager": Mock(is_screen_change=Mock(return_value=False)), |
| 383 |
"CursorManager": Mock(is_cursor_vertical_move=Mock(return_value=False)), |
| 384 |
}, |
| 385 |
"screen": { |
| 386 |
"new_delta": sample, |
| 387 |
"new_delta_is_typing": False, |
| 388 |
"new_content_text": sample, |
| 389 |
"old_cursor": {"x": 0, "y": 0}, |
| 390 |
"new_cursor": {"x": 0, "y": 0}, |
| 391 |
}, |
| 392 |
} |
| 393 |
command.play_activity_beep = Mock() |
| 394 |
command.play_progress_tone = Mock() |
| 395 |
|
| 396 |
assert len(sample) > 200 |
| 397 |
|
| 398 |
command.run() |
| 399 |
|
| 400 |
command.play_activity_beep.assert_not_called() |
| 401 |
command.play_progress_tone.assert_not_called() |