from pathlib import Path import pytest from common_cents import config @pytest.fixture(autouse=True) def isolate_config(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path: """Point XDG_CONFIG_HOME at a temp dir so tests don't touch the real config.""" monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path)) return tmp_path def test_config_path_uses_xdg(isolate_config: Path) -> None: assert config.config_path() == isolate_config / "common-cents" / "config.json" def test_load_returns_empty_when_missing() -> None: assert config.load_config() == {} def test_load_returns_empty_on_corrupt_json(isolate_config: Path) -> None: p = config.config_path() p.parent.mkdir(parents=True) p.write_text("{not valid json") assert config.load_config() == {} def test_set_and_get_active_db_path(tmp_path: Path) -> None: target = tmp_path / "elsewhere" / "my.db" config.set_active_db_path(target) assert config.get_active_db_path() == target def test_get_active_db_path_returns_none_when_unset() -> None: assert config.get_active_db_path() is None def test_clear_active_db_path(tmp_path: Path) -> None: target = tmp_path / "x.db" config.set_active_db_path(target) config.clear_active_db_path() assert config.get_active_db_path() is None def test_clear_when_unset_is_noop() -> None: config.clear_active_db_path() assert config.get_active_db_path() is None def test_set_preserves_other_keys() -> None: config.save_config({"theme": "dark"}) config.set_active_db_path(Path("/tmp/x.db")) data = config.load_config() assert data["theme"] == "dark" assert data["db_path"] == "/tmp/x.db"