Debian Codegraph @

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