"""Opening a session folder in the platform file manager. The operator needs the folder the recording is in, not a dialog that only shows a path string. The launch is mocked here, because the test must assert the exact argument the platform file manager receives — ``explorer /select,`` is a single argument whose comma and quoting are the part that breaks silently. """ from __future__ import annotations import pytest from orisys_autocal.ui import reveal as reveal_module def _capture_launch(monkeypatch) -> list: calls: list = [] monkeypatch.setattr(reveal_module.subprocess, "Popen", lambda command: calls.append(command)) return calls @pytest.mark.parametrize( ("platform", "executable"), [("win32", "explorer"), ("linux", "xdg-open"), ("darwin", "open")], ) def test_revealing_a_folder_uses_the_platform_file_manager( tmp_path, monkeypatch, platform, executable ): folder = tmp_path / "session" folder.mkdir() monkeypatch.setattr(reveal_module.sys, "platform", platform) calls = _capture_launch(monkeypatch) assert reveal_module.reveal(folder) is True assert calls == [[executable, str(folder)]] def test_a_selected_file_is_highlighted_with_explorer(tmp_path, monkeypatch): folder = tmp_path / "session" folder.mkdir() video = folder / "raw_0001.mp4" video.write_bytes(b"video") monkeypatch.setattr(reveal_module.sys, "platform", "win32") calls = _capture_launch(monkeypatch) assert reveal_module.reveal(folder, select=video) is True # One command-line string: the comma belongs to the switch and the path is # quoted, which is what explorer expects for a path with spaces. assert calls == [f'explorer /select,"{video}"'] def test_a_selected_file_that_is_gone_falls_back_to_the_folder(tmp_path, monkeypatch): folder = tmp_path / "session" folder.mkdir() missing = folder / "raw_0001.mp4" monkeypatch.setattr(reveal_module.sys, "platform", "win32") calls = _capture_launch(monkeypatch) assert reveal_module.reveal(folder, select=missing) is True assert calls == [["explorer", str(folder)]] def test_a_missing_path_is_not_opened(tmp_path, monkeypatch): monkeypatch.setattr(reveal_module.sys, "platform", "win32") calls = _capture_launch(monkeypatch) assert reveal_module.reveal(tmp_path / "gone") is False assert calls == [] def test_no_path_is_not_opened(monkeypatch): calls = _capture_launch(monkeypatch) assert reveal_module.reveal(None) is False assert reveal_module.reveal("") is False assert calls == [] def test_a_failed_launch_is_reported_instead_of_raised(tmp_path, monkeypatch): folder = tmp_path / "session" folder.mkdir() monkeypatch.setattr(reveal_module.sys, "platform", "win32") def fail(command): raise OSError("no file manager") monkeypatch.setattr(reveal_module.subprocess, "Popen", fail) assert reveal_module.reveal(folder) is False