| 1 |
""" |
| 2 |
Integration tests for remote control functionality. |
| 3 |
|
| 4 |
Tests the remote control system including Unix socket and TCP communication, |
| 5 |
command parsing, and settings management. |
| 6 |
""" |
| 7 |
|
| 8 |
import pytest |
| 9 |
import socket |
| 10 |
import time |
| 11 |
from unittest.mock import Mock, patch |
| 12 |
|
| 13 |
from fenrirscreenreader.core.remoteManager import RemoteManager |
| 14 |
|
| 15 |
|
| 16 |
@pytest.mark.integration |
| 17 |
@pytest.mark.remote |
| 18 |
class TestRemoteCommandParsing: |
| 19 |
"""Test remote control command parsing and execution.""" |
| 20 |
|
| 21 |
def setup_method(self): |
| 22 |
"""Create RemoteManager instance for each test.""" |
| 23 |
self.manager = RemoteManager() |
| 24 |
|
| 25 |
def test_say_command_parsing(self, mock_environment): |
| 26 |
"""Test parsing of 'command say' messages.""" |
| 27 |
self.manager.initialize(mock_environment) |
| 28 |
|
| 29 |
result = self.manager.handle_command_execution_with_response( |
| 30 |
"say Hello World" |
| 31 |
) |
| 32 |
|
| 33 |
assert result["success"] is True |
| 34 |
assert "Speaking" in result["message"] |
| 35 |
mock_environment["runtime"]["OutputManager"].speak_text.assert_called_once_with( |
| 36 |
"Hello World" |
| 37 |
) |
| 38 |
|
| 39 |
def test_interrupt_command(self, mock_environment): |
| 40 |
"""Test speech interruption command.""" |
| 41 |
self.manager.initialize(mock_environment) |
| 42 |
|
| 43 |
result = self.manager.handle_command_execution_with_response("interrupt") |
| 44 |
|
| 45 |
assert result["success"] is True |
| 46 |
mock_environment["runtime"][ |
| 47 |
"OutputManager" |
| 48 |
].interrupt_output.assert_called_once() |
| 49 |
|
| 50 |
def test_setting_change_parsing(self, mock_environment): |
| 51 |
"""Test parsing of 'setting set' commands.""" |
| 52 |
self.manager.initialize(mock_environment) |
| 53 |
|
| 54 |
# Mock parse_setting_args to verify it gets called |
| 55 |
with patch.object( |
| 56 |
mock_environment["runtime"]["SettingsManager"], "parse_setting_args" |
| 57 |
) as mock_parse: |
| 58 |
result = self.manager.handle_settings_change_with_response( |
| 59 |
"set speech#rate=0.8" |
| 60 |
) |
| 61 |
|
| 62 |
assert result["success"] is True |
| 63 |
mock_parse.assert_called_once_with("speech#rate=0.8") |
| 64 |
|
| 65 |
def test_clipboard_command(self, mock_environment): |
| 66 |
"""Test clipboard setting via remote control.""" |
| 67 |
self.manager.initialize(mock_environment) |
| 68 |
|
| 69 |
result = self.manager.handle_command_execution_with_response( |
| 70 |
"clipboard Test clipboard content" |
| 71 |
) |
| 72 |
|
| 73 |
assert result["success"] is True |
| 74 |
mock_environment["runtime"][ |
| 75 |
"MemoryManager" |
| 76 |
].add_value_to_first_index.assert_called_once_with( |
| 77 |
"clipboardHistory", "Test clipboard content" |
| 78 |
) |
| 79 |
|
| 80 |
def test_quit_command(self, mock_environment): |
| 81 |
"""Test Fenrir shutdown command.""" |
| 82 |
self.manager.initialize(mock_environment) |
| 83 |
|
| 84 |
result = self.manager.handle_command_execution_with_response( |
| 85 |
"quitapplication" |
| 86 |
) |
| 87 |
|
| 88 |
assert result["success"] is True |
| 89 |
mock_environment["runtime"][ |
| 90 |
"EventManager" |
| 91 |
].stop_main_event_loop.assert_called_once() |
| 92 |
|
| 93 |
def test_unknown_command_rejection(self, mock_environment): |
| 94 |
"""Test that unknown commands are rejected.""" |
| 95 |
self.manager.initialize(mock_environment) |
| 96 |
|
| 97 |
result = self.manager.handle_command_execution_with_response( |
| 98 |
"unknown_command" |
| 99 |
) |
| 100 |
|
| 101 |
assert result["success"] is False |
| 102 |
assert "Unknown command" in result["message"] |
| 103 |
|
| 104 |
|
| 105 |
@pytest.mark.integration |
| 106 |
@pytest.mark.remote |
| 107 |
class TestRemoteSettingsControl: |
| 108 |
"""Test remote control of settings.""" |
| 109 |
|
| 110 |
def setup_method(self): |
| 111 |
"""Create RemoteManager instance for each test.""" |
| 112 |
self.manager = RemoteManager() |
| 113 |
|
| 114 |
def test_setting_reset(self, mock_environment): |
| 115 |
"""Test resetting settings to defaults.""" |
| 116 |
self.manager.initialize(mock_environment) |
| 117 |
|
| 118 |
with patch.object( |
| 119 |
mock_environment["runtime"]["SettingsManager"], "reset_setting_arg_dict" |
| 120 |
) as mock_reset: |
| 121 |
result = self.manager.handle_settings_change_with_response("reset") |
| 122 |
|
| 123 |
assert result["success"] is True |
| 124 |
mock_reset.assert_called_once() |
| 125 |
|
| 126 |
def test_setting_save(self, mock_environment): |
| 127 |
"""Test saving settings to file.""" |
| 128 |
self.manager.initialize(mock_environment) |
| 129 |
|
| 130 |
mock_environment["runtime"]["SettingsManager"].get_settings_file = Mock( |
| 131 |
return_value="/tmp/test.conf" |
| 132 |
) |
| 133 |
|
| 134 |
with patch.object( |
| 135 |
mock_environment["runtime"]["SettingsManager"], "save_settings" |
| 136 |
) as mock_save: |
| 137 |
result = self.manager.handle_settings_change_with_response("save") |
| 138 |
|
| 139 |
assert result["success"] is True |
| 140 |
mock_save.assert_called_once() |
| 141 |
|
| 142 |
def test_settings_remote_disabled(self, mock_environment): |
| 143 |
"""Test that settings commands are blocked when disabled.""" |
| 144 |
mock_environment["runtime"]["SettingsManager"].get_setting_as_bool = Mock( |
| 145 |
return_value=False |
| 146 |
) |
| 147 |
self.manager.initialize(mock_environment) |
| 148 |
|
| 149 |
result = self.manager.handle_settings_change_with_response( |
| 150 |
"set speech#rate=0.5" |
| 151 |
) |
| 152 |
|
| 153 |
assert result["success"] is False |
| 154 |
assert "disabled" in result["message"].lower() |
| 155 |
|
| 156 |
|
| 157 |
@pytest.mark.integration |
| 158 |
@pytest.mark.remote |
| 159 |
class TestRemoteDataFormat: |
| 160 |
"""Test remote control data format handling.""" |
| 161 |
|
| 162 |
def setup_method(self): |
| 163 |
"""Create RemoteManager instance for each test.""" |
| 164 |
self.manager = RemoteManager() |
| 165 |
|
| 166 |
def test_command_prefix_case_insensitive(self, mock_environment): |
| 167 |
"""Test that command prefixes are case-insensitive.""" |
| 168 |
self.manager.initialize(mock_environment) |
| 169 |
|
| 170 |
# All of these should work |
| 171 |
result1 = self.manager.handle_remote_incomming_with_response( |
| 172 |
"COMMAND say test" |
| 173 |
) |
| 174 |
result2 = self.manager.handle_remote_incomming_with_response( |
| 175 |
"command say test" |
| 176 |
) |
| 177 |
result3 = self.manager.handle_remote_incomming_with_response( |
| 178 |
"CoMmAnD say test" |
| 179 |
) |
| 180 |
|
| 181 |
assert all(r["success"] for r in [result1, result2, result3]) |
| 182 |
|
| 183 |
def test_setting_prefix_case_insensitive(self, mock_environment): |
| 184 |
"""Test that setting prefixes are case-insensitive.""" |
| 185 |
self.manager.initialize(mock_environment) |
| 186 |
|
| 187 |
with patch.object( |
| 188 |
mock_environment["runtime"]["SettingsManager"], "parse_setting_args" |
| 189 |
): |
| 190 |
result1 = self.manager.handle_remote_incomming_with_response( |
| 191 |
"SETTING set speech#rate=0.5" |
| 192 |
) |
| 193 |
result2 = self.manager.handle_remote_incomming_with_response( |
| 194 |
"setting set speech#rate=0.5" |
| 195 |
) |
| 196 |
|
| 197 |
assert all(r["success"] for r in [result1, result2]) |
| 198 |
|
| 199 |
def test_empty_data_handling(self, mock_environment): |
| 200 |
"""Test handling of empty remote data.""" |
| 201 |
self.manager.initialize(mock_environment) |
| 202 |
|
| 203 |
result = self.manager.handle_remote_incomming_with_response("") |
| 204 |
|
| 205 |
assert result["success"] is False |
| 206 |
assert "No data" in result["message"] |
| 207 |
|
| 208 |
def test_invalid_format_rejection(self, mock_environment): |
| 209 |
"""Test rejection of invalid command format.""" |
| 210 |
self.manager.initialize(mock_environment) |
| 211 |
|
| 212 |
result = self.manager.handle_remote_incomming_with_response( |
| 213 |
"invalid format without prefix" |
| 214 |
) |
| 215 |
|
| 216 |
assert result["success"] is False |
| 217 |
assert "Unknown command format" in result["message"] |
| 218 |
|
| 219 |
def test_list_instances_top_level_command(self, mock_environment): |
| 220 |
"""Test listing registered Fenrir instances.""" |
| 221 |
self.manager.initialize(mock_environment) |
| 222 |
|
| 223 |
with patch( |
| 224 |
"fenrirscreenreader.core.remoteManager.remoteInstanceRegistry.list_instances", |
| 225 |
return_value=[ |
| 226 |
{ |
| 227 |
"pid": 123, |
| 228 |
"ppid": 100, |
| 229 |
"screen_driver": "ptyDriver", |
| 230 |
"keyboard_driver": "x11Driver", |
| 231 |
"main_socket": True, |
| 232 |
"x11_window_id": "0x123", |
| 233 |
"socket_files": [ |
| 234 |
"/tmp/fenrirscreenreader-123.sock", |
| 235 |
"/tmp/fenrirscreenreader-daemon.sock", |
| 236 |
], |
| 237 |
} |
| 238 |
], |
| 239 |
): |
| 240 |
result = self.manager.handle_remote_incomming_with_response("ls") |
| 241 |
|
| 242 |
assert result["success"] is True |
| 243 |
assert "pid=123" in result["message"] |
| 244 |
assert "x11_window_id=0x123" in result["message"] |
| 245 |
assert "/tmp/fenrirscreenreader-123.sock" in result["message"] |
| 246 |
|
| 247 |
def test_remote_incoming_suppresses_command_claimed_by_other_instance( |
| 248 |
self, mock_environment, tmp_path |
| 249 |
): |
| 250 |
"""Test untargeted duplicate remote commands only run in one process.""" |
| 251 |
self.manager.initialize(mock_environment) |
| 252 |
lock_path = tmp_path / "remote-command.lock" |
| 253 |
event_data = "command say duplicated" |
| 254 |
event_hash = self.manager._get_remote_command_hash(event_data) |
| 255 |
lock_path.write_text(f"999999 {event_hash} {time.time()}\n") |
| 256 |
|
| 257 |
with patch.object( |
| 258 |
self.manager, |
| 259 |
"_get_remote_command_lock_path", |
| 260 |
return_value=str(lock_path), |
| 261 |
): |
| 262 |
self.manager.handle_remote_incomming(event_data) |
| 263 |
|
| 264 |
mock_environment["runtime"]["OutputManager"].speak_text.assert_not_called() |
| 265 |
|
| 266 |
def test_remote_incoming_allows_same_instance_repeat( |
| 267 |
self, mock_environment, tmp_path |
| 268 |
): |
| 269 |
"""Test repeated direct commands to one instance are not suppressed.""" |
| 270 |
self.manager.initialize(mock_environment) |
| 271 |
lock_path = tmp_path / "remote-command.lock" |
| 272 |
|
| 273 |
with patch.object( |
| 274 |
self.manager, |
| 275 |
"_get_remote_command_lock_path", |
| 276 |
return_value=str(lock_path), |
| 277 |
): |
| 278 |
self.manager.handle_remote_incomming("command say repeat") |
| 279 |
self.manager.handle_remote_incomming("command say repeat") |
| 280 |
|
| 281 |
assert mock_environment["runtime"]["OutputManager"].speak_text.call_count == 2 |
| 282 |
|
| 283 |
def test_remote_incoming_allows_different_command_after_claim( |
| 284 |
self, mock_environment, tmp_path |
| 285 |
): |
| 286 |
"""Test a fresh different command is not suppressed by the shared lock.""" |
| 287 |
self.manager.initialize(mock_environment) |
| 288 |
lock_path = tmp_path / "remote-command.lock" |
| 289 |
previous_hash = self.manager._get_remote_command_hash("command say first") |
| 290 |
lock_path.write_text(f"999999 {previous_hash} {time.time()}\n") |
| 291 |
|
| 292 |
with patch.object( |
| 293 |
self.manager, |
| 294 |
"_get_remote_command_lock_path", |
| 295 |
return_value=str(lock_path), |
| 296 |
): |
| 297 |
self.manager.handle_remote_incomming("command say second") |
| 298 |
|
| 299 |
mock_environment["runtime"]["OutputManager"].speak_text.assert_called_once() |
| 300 |
|
| 301 |
|
| 302 |
@pytest.mark.integration |
| 303 |
@pytest.mark.remote |
| 304 |
class TestWindowDefinition: |
| 305 |
"""Test window definition via remote control.""" |
| 306 |
|
| 307 |
def setup_method(self): |
| 308 |
"""Create RemoteManager instance for each test.""" |
| 309 |
self.manager = RemoteManager() |
| 310 |
|
| 311 |
def test_define_window_valid_coordinates(self, mock_environment): |
| 312 |
"""Test defining a window with valid coordinates.""" |
| 313 |
self.manager.initialize(mock_environment) |
| 314 |
|
| 315 |
result = self.manager.handle_command_execution_with_response( |
| 316 |
"window 10 5 70 20" |
| 317 |
) |
| 318 |
|
| 319 |
assert result["success"] is True |
| 320 |
mock_environment["runtime"][ |
| 321 |
"CursorManager" |
| 322 |
].set_window_for_application.assert_called_once() |
| 323 |
|
| 324 |
# Verify the coordinates were parsed correctly |
| 325 |
call_args = mock_environment["runtime"][ |
| 326 |
"CursorManager" |
| 327 |
].set_window_for_application.call_args |
| 328 |
start, end = call_args[0] |
| 329 |
assert start == {"x": 10, "y": 5} |
| 330 |
assert end == {"x": 70, "y": 20} |
| 331 |
|
| 332 |
def test_define_window_insufficient_coordinates(self, mock_environment): |
| 333 |
"""Test that window definition with < 4 coordinates is ignored.""" |
| 334 |
self.manager.initialize(mock_environment) |
| 335 |
|
| 336 |
# Should succeed but not call set_window_for_application |
| 337 |
result = self.manager.handle_command_execution_with_response("window 10 20") |
| 338 |
|
| 339 |
assert result["success"] is True |
| 340 |
mock_environment["runtime"][ |
| 341 |
"CursorManager" |
| 342 |
].set_window_for_application.assert_not_called() |
| 343 |
|
| 344 |
def test_reset_window(self, mock_environment): |
| 345 |
"""Test resetting window to full screen.""" |
| 346 |
self.manager.initialize(mock_environment) |
| 347 |
|
| 348 |
result = self.manager.handle_command_execution_with_response("resetwindow") |
| 349 |
|
| 350 |
assert result["success"] is True |
| 351 |
mock_environment["runtime"][ |
| 352 |
"CursorManager" |
| 353 |
].clear_window_for_application.assert_called_once() |
| 354 |
|
| 355 |
|
| 356 |
@pytest.mark.integration |
| 357 |
@pytest.mark.remote |
| 358 |
class TestVMenuControl: |
| 359 |
"""Test VMenu control via remote.""" |
| 360 |
|
| 361 |
def setup_method(self): |
| 362 |
"""Create RemoteManager instance for each test.""" |
| 363 |
self.manager = RemoteManager() |
| 364 |
|
| 365 |
def test_set_vmenu(self, mock_environment): |
| 366 |
"""Test setting VMenu to specific menu.""" |
| 367 |
self.manager.initialize(mock_environment) |
| 368 |
|
| 369 |
result = self.manager.handle_command_execution_with_response( |
| 370 |
"vmenu /vim/file/save" |
| 371 |
) |
| 372 |
|
| 373 |
assert result["success"] is True |
| 374 |
mock_environment["runtime"]["VmenuManager"].set_curr_menu.assert_called_once_with( |
| 375 |
"/vim/file/save" |
| 376 |
) |
| 377 |
|
| 378 |
def test_reset_vmenu(self, mock_environment): |
| 379 |
"""Test resetting VMenu to default.""" |
| 380 |
self.manager.initialize(mock_environment) |
| 381 |
|
| 382 |
result = self.manager.handle_command_execution_with_response("resetvmenu") |
| 383 |
|
| 384 |
assert result["success"] is True |
| 385 |
mock_environment["runtime"]["VmenuManager"].set_curr_menu.assert_called_once_with() |
| 386 |
|
| 387 |
|
| 388 |
@pytest.mark.integration |
| 389 |
@pytest.mark.remote |
| 390 |
@pytest.mark.slow |
| 391 |
class TestRemoteControlThroughput: |
| 392 |
"""Test remote control performance characteristics.""" |
| 393 |
|
| 394 |
def test_rapid_say_commands(self, mock_environment): |
| 395 |
"""Test handling of rapid successive say commands.""" |
| 396 |
manager = RemoteManager() |
| 397 |
manager.initialize(mock_environment) |
| 398 |
|
| 399 |
# Send 100 rapid commands |
| 400 |
for i in range(100): |
| 401 |
result = manager.handle_command_execution_with_response(f"say test {i}") |
| 402 |
assert result["success"] is True |
| 403 |
|
| 404 |
# Verify all were queued |
| 405 |
assert ( |
| 406 |
mock_environment["runtime"]["OutputManager"].speak_text.call_count == 100 |
| 407 |
) |
| 408 |
|
| 409 |
def test_rapid_setting_changes(self, mock_environment): |
| 410 |
"""Test handling of rapid setting changes.""" |
| 411 |
manager = RemoteManager() |
| 412 |
manager.initialize(mock_environment) |
| 413 |
|
| 414 |
# Rapidly change speech rate |
| 415 |
with patch.object( |
| 416 |
mock_environment["runtime"]["SettingsManager"], "parse_setting_args" |
| 417 |
) as mock_parse: |
| 418 |
for rate in [0.5, 0.6, 0.7, 0.8, 0.9, 1.0]: |
| 419 |
result = manager.handle_settings_change_with_response( |
| 420 |
f"set speech#rate={rate}" |
| 421 |
) |
| 422 |
assert result["success"] is True |
| 423 |
|
| 424 |
assert mock_parse.call_count == 6 |