"""Tests for fungus.config_manager — ConfigManager INI wrapper.""" import pytest from fungus.config_manager import ConfigManager @pytest.fixture() def config_path(tmp_config_dir): """Return a path for the config.ini file and ensure clean state.""" config_dir = tmp_config_dir / "fungus-launcher" config_dir.mkdir(parents=True, exist_ok=True) return config_dir / "config.ini" @pytest.fixture() def config_manager(config_path): """Return a ConfigManager instance backed by a temporary file.""" from fungus import config_manager as cm # Patch the module-level _config_path so it uses our temp file original_path = cm._config_path cm._config_path = config_path # Reset the instance to pick up the new path instance = ConfigManager() yield instance # Restore original cm._config_path = original_path class TestConfigManagerDefaults: def test_default_runner(self, config_manager): assert config_manager.get("default-runner") == "GE-Proton" def test_default_discrete_gpu(self, config_manager): assert config_manager.get("discrete-gpu") == "False" def test_default_show_hidden(self, config_manager): assert config_manager.get("show-hidden") == "False" def test_default_game_directories(self, config_manager): assert config_manager.get("game-directories") == "" class TestConfigManagerGetSet: def test_set_and_get(self, config_manager): config_manager.set("default-runner", "Proton-9.0") assert config_manager.get("default-runner") == "Proton-9.0" def test_get_nonexistent_key_returns_empty(self, config_manager): assert config_manager.get("nonexistent-key") == "" def test_get_with_fallback(self, config_manager): assert config_manager.get("nonexistent-key", "fallback") == "fallback" class TestConfigManagerBool: def test_true_values(self, config_manager): for val in ("True", "true", "1", "Yes", "yes", "ON", "on"): config_manager.set("discrete-gpu", val) assert config_manager.get_bool("discrete-gpu") is True def test_false_values(self, config_manager): for val in ("False", "false", "0", "No", "no", "OFF", "off"): config_manager.set("discrete-gpu", val) assert config_manager.get_bool("discrete-gpu") is False def test_default_false(self, config_manager): assert config_manager.get_bool("nonexistent-key") is False def test_custom_fallback(self, config_manager): assert config_manager.get_bool("nonexistent-key", fallback=True) is True class TestConfigManagerSave: def test_save_and_reload(self, config_manager, config_path): config_manager.set("discrete-gpu", "True") config_manager.set("game-directories", "/home/user/Games") config_manager.save() # Reload from disk fresh = ConfigManager() assert fresh.get("discrete-gpu") == "True" assert fresh.get("game-directories") == "/home/user/Games" def test_save_creates_directory(self, config_manager, tmp_config_dir): config_dir = tmp_config_dir / "new-fungus" from fungus import config_manager as cm original = cm._config_path cm._config_path = config_dir / "config.ini" config_manager.save() assert config_dir.exists() assert cm._config_path.exists() cm._config_path = original def test_save_overwrites_existing(self, config_manager, config_path): config_manager.set("default-runner", "GE-Proton9-11") config_manager.save() config_manager.set("default-runner", "Proton-10") config_manager.save() # Reload fresh = ConfigManager() assert fresh.get("default-runner") == "Proton-10" class TestConfigManagerLegacy: def test_legacy_key_value_format(self, config_path): # Write a legacy-style config (no section header) config_path.write_text( 'default-runner = GE-Proton-GE-Latest\ndiscrete-gpu = true\n' '; comment line\nshow-hidden = false\n' ) from fungus import config_manager as cm original = cm._config_path cm._config_path = config_path mgr = ConfigManager() assert mgr.get("default-runner") == "GE-Proton-GE-Latest" assert mgr.get_bool("discrete-gpu") is True assert mgr.get("show-hidden") == "false" cm._config_path = original def test_legacy_ignores_unknown_keys(self, config_path): config_path.write_text("unknown-key = something\ndefault-runner = TestRunner\n") from fungus import config_manager as cm original = cm._config_path cm._config_path = config_path mgr = ConfigManager() assert mgr.get("default-runner") == "TestRunner" assert mgr.get("unknown-key") == "" # not in defaults cm._config_path = original