Debian Codegraph @

tests/unit/test_incoming_command.py

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