| 1 |
""" |
| 2 |
Shared pytest fixtures for Fenrir tests. |
| 3 |
|
| 4 |
This file contains fixtures and configuration used across all test modules. |
| 5 |
""" |
| 6 |
|
| 7 |
import os |
| 8 |
import sys |
| 9 |
import tempfile |
| 10 |
from pathlib import Path |
| 11 |
from unittest.mock import MagicMock, Mock |
| 12 |
|
| 13 |
import pytest |
| 14 |
|
| 15 |
# Add src directory to Python path for imports |
| 16 |
fenrir_root = Path(__file__).parent.parent |
| 17 |
sys.path.insert(0, str(fenrir_root / "src")) |
| 18 |
|
| 19 |
|
| 20 |
@pytest.fixture |
| 21 |
def mock_environment(): |
| 22 |
"""Create a minimal mock environment for testing. |
| 23 |
|
| 24 |
Returns a mock environment dict with required runtime managers mocked. |
| 25 |
This allows testing components without initializing the full Fenrir stack. |
| 26 |
""" |
| 27 |
env = { |
| 28 |
"runtime": { |
| 29 |
"DebugManager": Mock(write_debug_out=Mock()), |
| 30 |
"OutputManager": Mock( |
| 31 |
present_text=Mock(), |
| 32 |
speak_text=Mock(), |
| 33 |
interrupt_output=Mock(), |
| 34 |
), |
| 35 |
"SettingsManager": Mock( |
| 36 |
get_setting=Mock(return_value="default"), |
| 37 |
get_setting_as_int=Mock(return_value=0), |
| 38 |
get_setting_as_float=Mock(return_value=0.0), |
| 39 |
get_setting_as_bool=Mock(return_value=True), |
| 40 |
), |
| 41 |
"InputManager": Mock( |
| 42 |
sendKeys=Mock(), |
| 43 |
handle_device_grab=Mock(), |
| 44 |
), |
| 45 |
"ScreenManager": Mock(update_screen_ignored=Mock()), |
| 46 |
"EventManager": Mock(stop_main_event_loop=Mock()), |
| 47 |
"MemoryManager": Mock( |
| 48 |
add_value_to_first_index=Mock(), |
| 49 |
get_index_list_element=Mock(return_value="test clipboard"), |
| 50 |
is_index_list_empty=Mock(return_value=False), |
| 51 |
), |
| 52 |
"VmenuManager": Mock( |
| 53 |
set_curr_menu=Mock(), |
| 54 |
), |
| 55 |
"CursorManager": Mock( |
| 56 |
set_window_for_application=Mock(), |
| 57 |
clear_window_for_application=Mock(), |
| 58 |
), |
| 59 |
}, |
| 60 |
"settings": Mock(), |
| 61 |
"general": { |
| 62 |
"curr_user": "testuser", |
| 63 |
}, |
| 64 |
} |
| 65 |
return env |
| 66 |
|
| 67 |
|
| 68 |
@pytest.fixture |
| 69 |
def temp_config_file(tmp_path): |
| 70 |
"""Create a temporary configuration file for testing. |
| 71 |
|
| 72 |
Returns path to a valid test configuration file. |
| 73 |
""" |
| 74 |
config_path = tmp_path / "test_settings.conf" |
| 75 |
config_content = """[sound] |
| 76 |
enabled=True |
| 77 |
driver=gstreamerDriver |
| 78 |
theme=default |
| 79 |
volume=0.7 |
| 80 |
|
| 81 |
[speech] |
| 82 |
enabled=True |
| 83 |
driver=speechdDriver |
| 84 |
rate=0.5 |
| 85 |
pitch=0.5 |
| 86 |
volume=1.0 |
| 87 |
autoReadIncoming=True |
| 88 |
|
| 89 |
[screen] |
| 90 |
driver=vcsaDriver |
| 91 |
encoding=auto |
| 92 |
screenUpdateDelay=0.05 |
| 93 |
|
| 94 |
[keyboard] |
| 95 |
driver=evdevDriver |
| 96 |
device=ALL |
| 97 |
grabDevices=True |
| 98 |
keyboardLayout=desktop |
| 99 |
|
| 100 |
[general] |
| 101 |
debugLevel=2 |
| 102 |
debugMode=File |
| 103 |
|
| 104 |
[remote] |
| 105 |
enable=True |
| 106 |
driver=unixDriver |
| 107 |
port=22447 |
| 108 |
enableSettingsRemote=True |
| 109 |
enableCommandRemote=True |
| 110 |
""" |
| 111 |
config_path.write_text(config_content) |
| 112 |
return config_path |
| 113 |
|
| 114 |
|
| 115 |
@pytest.fixture |
| 116 |
def temp_socket_path(tmp_path): |
| 117 |
"""Create a temporary Unix socket path for testing. |
| 118 |
|
| 119 |
Returns path that can be used for Unix socket testing. |
| 120 |
""" |
| 121 |
return tmp_path / "test_fenrir.sock" |
| 122 |
|
| 123 |
|
| 124 |
@pytest.fixture |
| 125 |
def temp_clipboard_file(tmp_path): |
| 126 |
"""Create a temporary clipboard file for testing. |
| 127 |
|
| 128 |
Returns path to a temporary file for clipboard operations. |
| 129 |
""" |
| 130 |
clipboard_path = tmp_path / "fenrirClipboard" |
| 131 |
clipboard_path.write_text("") |
| 132 |
return clipboard_path |
| 133 |
|
| 134 |
|
| 135 |
@pytest.fixture |
| 136 |
def sample_screen_data(): |
| 137 |
"""Return sample screen data for testing screen-related functionality. |
| 138 |
|
| 139 |
Returns dict with screen dimensions and content. |
| 140 |
""" |
| 141 |
return { |
| 142 |
"columns": 80, |
| 143 |
"lines": 24, |
| 144 |
"delta": "Hello World", |
| 145 |
"cursor": {"x": 0, "y": 0}, |
| 146 |
"content": "Sample screen content\nSecond line\nThird line", |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
@pytest.fixture |
| 151 |
def sample_remote_commands(): |
| 152 |
"""Return sample remote control commands for testing. |
| 153 |
|
| 154 |
Returns list of valid remote commands. |
| 155 |
""" |
| 156 |
return [ |
| 157 |
"command say Hello World", |
| 158 |
"command interrupt", |
| 159 |
"setting set speech#rate=0.8", |
| 160 |
"setting set speech#pitch=0.6", |
| 161 |
"setting set sound#volume=0.5", |
| 162 |
"setting reset", |
| 163 |
] |
| 164 |
|
| 165 |
|
| 166 |
@pytest.fixture |
| 167 |
def invalid_remote_commands(): |
| 168 |
"""Return invalid remote control commands for testing validation. |
| 169 |
|
| 170 |
Returns list of commands that should be rejected. |
| 171 |
""" |
| 172 |
return [ |
| 173 |
"setting set speech#rate=999", # Out of range |
| 174 |
"setting set speech#rate=-1", # Negative value |
| 175 |
"setting set speech#pitch=10", # Out of range |
| 176 |
"setting set speech#volume=-0.5", # Negative volume |
| 177 |
"setting set invalid#setting=value", # Invalid section |
| 178 |
"command unknown_command", # Unknown command |
| 179 |
] |
| 180 |
|
| 181 |
|
| 182 |
# Pytest hooks for test session customization |
| 183 |
|
| 184 |
|
| 185 |
def pytest_configure(config): |
| 186 |
"""Configure pytest with custom settings.""" |
| 187 |
# Add custom markers |
| 188 |
config.addinivalue_line( |
| 189 |
"markers", "unit: Unit tests (fast, no mocking required)" |
| 190 |
) |
| 191 |
config.addinivalue_line( |
| 192 |
"markers", "integration: Integration tests (require mocking)" |
| 193 |
) |
| 194 |
config.addinivalue_line( |
| 195 |
"markers", "driver: Driver tests (require root access)" |
| 196 |
) |
| 197 |
config.addinivalue_line( |
| 198 |
"markers", "slow: Tests that take more than 1 second" |
| 199 |
) |
| 200 |
|
| 201 |
|
| 202 |
def pytest_collection_modifyitems(config, items): |
| 203 |
"""Modify test collection to skip driver tests unless explicitly run. |
| 204 |
|
| 205 |
Driver tests require root access and hardware, so skip by default. |
| 206 |
Run with: pytest --run-driver-tests |
| 207 |
""" |
| 208 |
skip_driver = pytest.mark.skip( |
| 209 |
reason="Driver tests require root access (use --run-driver-tests)" |
| 210 |
) |
| 211 |
run_driver_tests = config.getoption("--run-driver-tests", default=False) |
| 212 |
|
| 213 |
for item in items: |
| 214 |
if "driver" in item.keywords and not run_driver_tests: |
| 215 |
item.add_marker(skip_driver) |
| 216 |
|
| 217 |
|
| 218 |
def pytest_addoption(parser): |
| 219 |
"""Add custom command line options.""" |
| 220 |
parser.addoption( |
| 221 |
"--run-driver-tests", |
| 222 |
action="store_true", |
| 223 |
default=False, |
| 224 |
help="Run driver tests that require root access", |
| 225 |
) |