Debian Codegraph @

tests/unit/test_local_resources.py

raw
1 from unittest.mock import Mock
2
3 import pytest
4
5 from fenrirscreenreader.core.commandManager import CommandManager
6 from fenrirscreenreader.core.settingsManager import SettingsManager
7
8
9 def write_script(path):
10 path.write_text("#!/bin/sh\n", encoding="utf-8")
11 path.chmod(0o755)
12
13
14 def build_command_environment(local_path, system_path):
15 settings_manager = Mock()
16 settings_manager.get_user_script_path.return_value = str(local_path)
17 settings_manager.get_setting.return_value = str(system_path)
18 valid_keys = {
19 "KEY_A",
20 "KEY_B",
21 "KEY_C",
22 "KEY_D",
23 "KEY_E",
24 "KEY_SHIFT",
25 "KEY_SCRIPT",
26 "KEY_U",
27 "KEY_X",
28 }
29
30 return {
31 "commands": {"commands": {}},
32 "commandsIgnore": {"commands": {}},
33 "bindings": {
34 str([1, sorted(["KEY_C", "KEY_SCRIPT"])]): "EXISTING"
35 },
36 "rawBindings": {},
37 "general": {"curr_user": "Username"},
38 "runtime": {
39 "SettingsManager": settings_manager,
40 "DebugManager": Mock(write_debug_out=Mock()),
41 "InputManager": Mock(
42 is_valid_key=Mock(side_effect=lambda key: key in valid_keys)
43 ),
44 },
45 }
46
47
48 @pytest.mark.unit
49 def test_local_scripts_load_before_non_conflicting_system_scripts(tmp_path):
50 local_path = tmp_path / "local"
51 system_path = tmp_path / "system"
52 local_path.mkdir()
53 system_path.mkdir()
54 write_script(local_path / "local_only__-__KEY_A")
55 write_script(local_path / "same_name__-__KEY_B")
56 write_script(system_path / "same_name__-__KEY_D")
57 write_script(system_path / "system_only__-__KEY_E")
58
59 env = build_command_environment(local_path, system_path)
60 command_manager = CommandManager()
61 command_manager.initialize = lambda environment: None
62 command_manager.env = env
63
64 command_manager.load_script_commands()
65
66 commands = env["commands"]["commands"]
67 assert "LOCAL_ONLY__-__KEY_A" in commands
68 assert "SYSTEM_ONLY__-__KEY_E" in commands
69 assert commands["SAME_NAME__-__KEY_B"].script_path == str(
70 local_path / "same_name__-__KEY_B"
71 )
72 assert "SAME_NAME__-__KEY_D" not in commands
73
74
75 @pytest.mark.unit
76 def test_system_scripts_skip_existing_shortcut_bindings(tmp_path):
77 local_path = tmp_path / "local"
78 system_path = tmp_path / "system"
79 local_path.mkdir()
80 system_path.mkdir()
81 write_script(system_path / "conflicting_key__-__KEY_C")
82 write_script(system_path / "system_only__-__KEY_E")
83
84 env = build_command_environment(local_path, system_path)
85 command_manager = CommandManager()
86 command_manager.env = env
87
88 command_manager.load_script_commands()
89
90 commands = env["commands"]["commands"]
91 assert "CONFLICTING_KEY__-__KEY_C" not in commands
92 assert "SYSTEM_ONLY__-__KEY_E" in commands
93
94
95 @pytest.mark.unit
96 def test_same_directory_script_variants_load_multiple_shortcuts(tmp_path):
97 local_path = tmp_path / "local"
98 system_path = tmp_path / "system"
99 local_path.mkdir()
100 system_path.mkdir()
101 write_script(system_path / "musicplayer__-__KEY_SHIFT__+__KEY_U.sh")
102 write_script(system_path / "musicplayer__-__KEY_SHIFT__+__KEY_X.sh")
103
104 env = build_command_environment(local_path, system_path)
105 command_manager = CommandManager()
106 command_manager.env = env
107
108 command_manager.load_script_commands()
109
110 commands = env["commands"]["commands"]
111 assert "MUSICPLAYER__-__KEY_SHIFT__+__KEY_U" in commands
112 assert "MUSICPLAYER__-__KEY_SHIFT__+__KEY_X" in commands
113 assert (
114 env["bindings"][str([1, sorted(["KEY_SCRIPT", "KEY_SHIFT", "KEY_U"])])]
115 == "MUSICPLAYER__-__KEY_SHIFT__+__KEY_U"
116 )
117 assert (
118 env["bindings"][str([1, sorted(["KEY_SCRIPT", "KEY_SHIFT", "KEY_X"])])]
119 == "MUSICPLAYER__-__KEY_SHIFT__+__KEY_X"
120 )
121
122
123 @pytest.mark.unit
124 def test_sound_theme_resolution_prefers_local_soundpacks(tmp_path):
125 manager = SettingsManager()
126 local_root = tmp_path / "local" / "fenrir"
127 system_root = tmp_path / "system" / "sounds" / "fenrir"
128 local_theme = local_root / "sounds" / "default"
129 system_theme = system_root / "default"
130 local_theme.mkdir(parents=True)
131 system_theme.mkdir(parents=True)
132 (local_theme / "soundicons.conf").write_text(
133 "Accept=Accept.wav\n", encoding="utf-8"
134 )
135 (system_theme / "soundicons.conf").write_text(
136 "Accept=SystemAccept.wav\n", encoding="utf-8"
137 )
138 manager.user_resource_root = str(local_root)
139 manager.system_sound_roots = [str(system_root)]
140
141 assert manager.resolve_sound_theme_path("default") == str(local_theme)
142
143
144 @pytest.mark.unit
145 def test_sound_theme_resolution_accepts_absolute_theme_path(tmp_path):
146 manager = SettingsManager()
147 theme_path = tmp_path / "custom"
148 theme_path.mkdir()
149 (theme_path / "soundicons.conf").write_text(
150 "Accept=Accept.wav\n", encoding="utf-8"
151 )
152
153 assert manager.resolve_sound_theme_path(str(theme_path)) == str(theme_path)