about summary refs log tree commit diff stats
path: root/tests/test_utils_pure.py
blob: d7f79f19a499e983f3b4bc7ccc3276591a9d7e87 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
"""Tests for pure utility functions not covered in test_utils.py.

Covers:
  - is_valid_prefix_path
  - is_safe_prefix_path
  - _resolve_runner_to_directory
  - has_proton_installed
"""

from unittest.mock import patch


# ── is_valid_prefix_path ──────────────────────────────────────────────────


class TestIsValidPrefixPath:
    """Tests for fungus.utils_pure.is_valid_prefix_path."""

    def test_inside_prefixes_dir(self, tmp_path):
        """Path inside the configured prefixes directory returns True."""
        from fungus.utils_pure import is_valid_prefix_path

        prefixes = tmp_path / "fungus" / "prefixes"
        prefixes.mkdir(parents=True)
        result = is_valid_prefix_path(str(prefixes / "umu-1234"), str(prefixes))
        assert result is True

    def test_is_prefixes_dir_itself(self, tmp_path):
        """The prefixes directory itself is valid."""
        from fungus.utils_pure import is_valid_prefix_path

        prefixes = tmp_path / "fungus" / "prefixes"
        prefixes.mkdir(parents=True)
        result = is_valid_prefix_path(str(prefixes), str(prefixes))
        assert result is True

    def test_outside_prefixes_dir(self, tmp_path):
        """Path outside the prefixes directory returns False."""
        from fungus.utils_pure import is_valid_prefix_path

        prefixes = tmp_path / "fungus" / "prefixes"
        prefixes.mkdir(parents=True)
        result = is_valid_prefix_path(
            str(tmp_path / "other" / "stuff"), str(prefixes)
        )
        assert result is False

    def test_parent_of_prefixes_dir(self, tmp_path):
        """Parent directory of prefixes_dir is not valid."""
        from fungus.utils_pure import is_valid_prefix_path

        prefixes = tmp_path / "fungus" / "prefixes"
        prefixes.mkdir(parents=True)
        result = is_valid_prefix_path(str(tmp_path / "fungus"), str(prefixes))
        assert result is False

    def test_deeply_nested_path(self, tmp_path):
        """Deeply nested paths inside prefixes_dir are valid."""
        from fungus.utils_pure import is_valid_prefix_path

        prefixes = tmp_path / "fungus" / "prefixes"
        prefixes.mkdir(parents=True)
        deep = prefixes / "a" / "b" / "c" / "d"
        result = is_valid_prefix_path(str(deep), str(prefixes))
        assert result is True

    def test_similar_prefix_name_not_matched(self, tmp_path):
        """A directory with a similar name but not under prefixes_dir is rejected."""
        from fungus.utils_pure import is_valid_prefix_path

        prefixes = tmp_path / "fungus" / "prefixes"
        prefixes.mkdir(parents=True)
        result = is_valid_prefix_path(
            str(tmp_path / "fungus" / "prefixes_extra" / "game"), str(prefixes)
        )
        assert result is False

    def test_expands_tilde(self, tmp_path, monkeypatch):
        """Tilde in path is expanded before checking."""
        from fungus.utils_pure import is_valid_prefix_path

        prefixes = tmp_path / "fungus" / "prefixes"
        prefixes.mkdir(parents=True)
        # Set HOME so ~ expands to tmp_path
        monkeypatch.setenv("HOME", str(tmp_path))
        result = is_valid_prefix_path("~/fungus/prefixes/some-game", str(prefixes))
        assert result is True


# ── is_safe_prefix_path ───────────────────────────────────────────────────


class TestIsSafePrefixPath:
    """Tests for fungus.utils_pure.is_safe_prefix_path."""

    def test_inside_user_data(self, tmp_path):
        """Path inside user-data directory returns True."""
        from fungus.utils_pure import is_safe_prefix_path

        user_data = tmp_path / "test_data"
        user_data.mkdir()
        result = is_safe_prefix_path(
            str(user_data / "fungus-launcher" / "games"),
            user_data=str(user_data),
        )
        assert result is True

    def test_inside_user_config(self, tmp_path):
        """Path inside user-config directory returns True."""
        from fungus.utils_pure import is_safe_prefix_path

        user_config = tmp_path / "test_config"
        user_config.mkdir()
        result = is_safe_prefix_path(
            str(user_config / "fungus-launcher" / "config.ini"),
            user_config=str(user_config),
        )
        assert result is True

    def test_is_user_data_root(self, tmp_path):
        """The user-data root itself is safe."""
        from fungus.utils_pure import is_safe_prefix_path

        user_data = tmp_path / "test_data"
        user_data.mkdir()
        result = is_safe_prefix_path(str(user_data), user_data=str(user_data))
        assert result is True

    def test_is_user_config_root(self, tmp_path):
        """The user-config root itself is safe."""
        from fungus.utils_pure import is_safe_prefix_path

        user_config = tmp_path / "test_config"
        user_config.mkdir()
        result = is_safe_prefix_path(
            str(user_config), user_config=str(user_config)
        )
        assert result is True

    def test_outside_both_safe_roots(self, tmp_path):
        """Path outside both safe roots returns False."""
        from fungus.utils_pure import is_safe_prefix_path

        user_data = tmp_path / "test_data"
        user_config = tmp_path / "test_config"
        user_data.mkdir()
        user_config.mkdir()
        result = is_safe_prefix_path(
            "/tmp/random/file.txt",
            user_data=str(user_data),
            user_config=str(user_config),
        )
        assert result is False

    def test_parent_of_safe_roots(self, tmp_path):
        """Parent of safe roots is not safe."""
        from fungus.utils_pure import is_safe_prefix_path

        user_data = tmp_path / "test_data"
        user_config = tmp_path / "test_config"
        user_data.mkdir()
        user_config.mkdir()
        # Parent of user_data is tmp_path itself
        result = is_safe_prefix_path(
            str(tmp_path),
            user_data=str(user_data),
            user_config=str(user_config),
        )
        assert result is False

    def test_deeply_nested_safe_path(self, tmp_path):
        """Deeply nested paths inside safe roots are valid."""
        from fungus.utils_pure import is_safe_prefix_path

        user_data = tmp_path / "test_data"
        user_data.mkdir()
        deep = user_data / "a" / "b" / "c"
        result = is_safe_prefix_path(
            str(deep), user_data=str(user_data)
        )
        assert result is True

    def test_similar_name_not_matched(self, tmp_path):
        """A path with a similar name but not under a safe root is rejected."""
        from fungus.utils_pure import is_safe_prefix_path

        user_data = tmp_path / "test_data"
        user_data.mkdir()
        # Sibling directory with a similar name
        sibling = tmp_path / "test_data_extra"
        result = is_safe_prefix_path(
            str(sibling / "file"), user_data=str(user_data)
        )
        assert result is False


# ── _resolve_runner_to_directory ──────────────────────────────────────────


class TestResolveRunnerToDirectory:
    """Tests for fungus.utils_pure._resolve_runner_to_directory."""

    def _make_compat_tree(self, tmp_path, subdirs):
        """Create a compatibilitytools.d directory tree."""
        compat = tmp_path / "Steam" / "compatibilitytools.d"
        compat.mkdir(parents=True)
        for d in subdirs:
            (compat / d).mkdir()
        return compat

    def test_direct_name_match(self, tmp_path):
        """A runner name that matches a directory name passes through."""
        from fungus.utils_pure import _resolve_runner_to_directory

        self._make_compat_tree(tmp_path, ["GE-Proton11-1"])
        with patch("fungus.constants.share_dir", str(tmp_path)):
            result = _resolve_runner_to_directory("GE-Proton11-1")
        assert result == "GE-Proton11-1"

    def test_alias_ge_proton_latest(self, tmp_path):
        """'Proton-GE Latest' alias resolves to the newest GE-Proton directory."""
        from fungus.utils_pure import _resolve_runner_to_directory

        self._make_compat_tree(tmp_path, [
            "GE-Proton9-11",
            "GE-Proton10-1",
            "GE-Proton11-1",
        ])
        with patch("fungus.constants.share_dir", str(tmp_path)):
            result = _resolve_runner_to_directory("Proton-GE Latest")
        assert result == "GE-Proton11-1"

    def test_alias_ge_proton_latest_default(self, tmp_path):
        """'GE-Proton Latest (default)' alias also resolves to newest."""
        from fungus.utils_pure import _resolve_runner_to_directory

        self._make_compat_tree(tmp_path, [
            "GE-Proton8-22",
            "GE-Proton10-5",
        ])
        with patch("fungus.constants.share_dir", str(tmp_path)):
            result = _resolve_runner_to_directory("GE-Proton Latest (default)")
        assert result == "GE-Proton10-5"

    def test_alias_proton_em_latest(self, tmp_path):
        """'Proton-EM Latest' alias resolves to newest Proton-EM."""
        from fungus.utils_pure import _resolve_runner_to_directory

        self._make_compat_tree(tmp_path, [
            "Proton-EM-1.0",
            "Proton-EM-2.0",
        ])
        with patch("fungus.constants.share_dir", str(tmp_path)):
            result = _resolve_runner_to_directory("Proton-EM Latest")
        assert result == "Proton-EM-2.0"

    def test_alias_umu_proton_returns_none(self, tmp_path):
        """'UMU-Proton Latest' has no directory equivalent → None."""
        from fungus.utils_pure import _resolve_runner_to_directory

        self._make_compat_tree(tmp_path, ["UMU-Proton-latest"])
        with patch("fungus.constants.share_dir", str(tmp_path)):
            result = _resolve_runner_to_directory("UMU-Proton Latest")
        assert result is None

    def test_unknown_runner_returns_none(self, tmp_path):
        """Unknown runner aliases return None."""
        from fungus.utils_pure import _resolve_runner_to_directory

        self._make_compat_tree(tmp_path, ["GE-Proton11-1"])
        with patch("fungus.constants.share_dir", str(tmp_path)):
            result = _resolve_runner_to_directory("Linux-Native")
        assert result is None

    def test_no_compat_directory_returns_none(self, tmp_path):
        """When compatibilitytools.d doesn't exist, returns None."""
        from fungus.utils_pure import _resolve_runner_to_directory

        with patch("fungus.constants.share_dir", str(tmp_path)):
            result = _resolve_runner_to_directory("GE-Proton11-1")
        assert result is None

    def test_no_matching_versions_returns_none(self, tmp_path):
        """When no directories match the family, returns None."""
        from fungus.utils_pure import _resolve_runner_to_directory

        self._make_compat_tree(tmp_path, ["SomeOtherTool-1.0"])
        with patch("fungus.constants.share_dir", str(tmp_path)):
            result = _resolve_runner_to_directory("Proton-GE Latest")
        assert result is None

    def test_newest_version_wins(self, tmp_path):
        """Among matching versions, the newest one is returned."""
        from fungus.utils_pure import _resolve_runner_to_directory

        self._make_compat_tree(tmp_path, [
            "GE-Proton7-27",
            "GE-Proton9-24",
            "GE-Proton10-7",
        ])
        with patch("fungus.constants.share_dir", str(tmp_path)):
            result = _resolve_runner_to_directory("GE-Proton10-7")
        assert result == "GE-Proton10-7"

    def test_direct_name_not_a_dir_returns_none(self, tmp_path):
        """If the runner name doesn't match any directory, returns None."""
        from fungus.utils_pure import _resolve_runner_to_directory

        self._make_compat_tree(tmp_path, ["GE-Proton11-1"])
        with patch("fungus.constants.share_dir", str(tmp_path)):
            result = _resolve_runner_to_directory("NonExistentRunner")
        assert result is None


# ── has_proton_installed ──────────────────────────────────────────────────


class TestHasProtonInstalled:
    """Tests for fungus.utils_pure.has_proton_installed."""

    def _make_compat_tree(self, tmp_path, entries):
        """Create a compatibilitytools.d directory tree with given entries."""
        compat = tmp_path / "Steam" / "compatibilitytools.d"
        compat.mkdir(parents=True)
        for entry in entries:
            if entry.endswith("/"):
                (compat / entry.rstrip("/")).mkdir()
            else:
                (compat / entry).mkdir()
        return compat

    def test_no_compat_directory(self, tmp_path):
        """When compatibilitytools.d doesn't exist, returns False."""
        from fungus.utils_pure import has_proton_installed

        with patch("fungus.constants.share_dir", str(tmp_path)):
            result = has_proton_installed()
        assert result is False

    def test_only_legacy_runtime(self, tmp_path):
        """Only LegacyRuntime directory → no proton installed."""
        from fungus.utils_pure import has_proton_installed

        self._make_compat_tree(tmp_path, ["LegacyRuntime/"])
        with patch("fungus.constants.share_dir", str(tmp_path)):
            result = has_proton_installed()
        assert result is False

    def test_ge_proton_installed(self, tmp_path):
        """GE-Proton directory → proton is installed."""
        from fungus.utils_pure import has_proton_installed

        self._make_compat_tree(tmp_path, ["GE-Proton11-1/"])
        with patch("fungus.constants.share_dir", str(tmp_path)):
            result = has_proton_installed()
        assert result is True

    def test_proton_em_installed(self, tmp_path):
        """Proton-EM directory → proton is installed."""
        from fungus.utils_pure import has_proton_installed

        self._make_compat_tree(tmp_path, ["Proton-EM-2.0/"])
        with patch("fungus.constants.share_dir", str(tmp_path)):
            result = has_proton_installed()
        assert result is True

    def test_official_proton_installed(self, tmp_path):
        """Official Proton directory → proton is installed."""
        from fungus.utils_pure import has_proton_installed

        self._make_compat_tree(tmp_path, ["Proton-10.0-1/"])
        with patch("fungus.constants.share_dir", str(tmp_path)):
            result = has_proton_installed()
        assert result is True

    def test_mixed_entries(self, tmp_path):
        """Mixed LegacyRuntime + actual proton → returns True."""
        from fungus.utils_pure import has_proton_installed

        self._make_compat_tree(tmp_path, [
            "LegacyRuntime/",
            "GE-Proton9-11/",
            "some-other-tool/",
        ])
        with patch("fungus.constants.share_dir", str(tmp_path)):
            result = has_proton_installed()
        assert result is True

    def test_empty_directory(self, tmp_path):
        """Empty compatibilitytools.d → returns False."""
        from fungus.utils_pure import has_proton_installed

        self._make_compat_tree(tmp_path, [])
        with patch("fungus.constants.share_dir", str(tmp_path)):
            result = has_proton_installed()
        assert result is False