# ABOUTME: Tests for configuration loading and wallpaper path resolution. # ABOUTME: Verifies TOML parsing, fallback hierarchy, and default values. from pathlib import Path from unittest.mock import patch import pytest from moonset.config import Config, load_config, resolve_background_path class TestLoadConfig: """Tests for TOML config loading.""" def test_returns_default_config_when_no_files_exist(self) -> None: config = load_config(config_paths=[Path("/nonexistent")]) assert config.background_path is None def test_reads_background_path_from_toml(self, tmp_path: Path) -> None: conf = tmp_path / "moonset.toml" conf.write_text('background_path = "/custom/wallpaper.jpg"\n') config = load_config(config_paths=[conf]) assert config.background_path == "/custom/wallpaper.jpg" def test_later_paths_override_earlier(self, tmp_path: Path) -> None: conf1 = tmp_path / "first.toml" conf1.write_text('background_path = "/first.jpg"\n') conf2 = tmp_path / "second.toml" conf2.write_text('background_path = "/second.jpg"\n') config = load_config(config_paths=[conf1, conf2]) assert config.background_path == "/second.jpg" def test_skips_missing_config_files(self, tmp_path: Path) -> None: conf = tmp_path / "exists.toml" conf.write_text('background_path = "/exists.jpg"\n') config = load_config(config_paths=[Path("/nonexistent"), conf]) assert config.background_path == "/exists.jpg" def test_default_config_has_none_background(self) -> None: config = Config() assert config.background_path is None class TestResolveBackgroundPath: """Tests for wallpaper path resolution fallback hierarchy.""" def test_uses_config_path_when_file_exists(self, tmp_path: Path) -> None: wallpaper = tmp_path / "custom.jpg" wallpaper.touch() config = Config(background_path=str(wallpaper)) assert resolve_background_path(config) == wallpaper def test_ignores_config_path_when_file_missing(self, tmp_path: Path) -> None: config = Config(background_path="/nonexistent/wallpaper.jpg") # Falls through to system or package fallback result = resolve_background_path(config) assert result is not None def test_uses_moonarch_wallpaper_as_second_fallback(self, tmp_path: Path) -> None: moonarch_wp = tmp_path / "wallpaper.jpg" moonarch_wp.touch() config = Config(background_path=None) with patch("moonset.config.MOONARCH_WALLPAPER", moonarch_wp): assert resolve_background_path(config) == moonarch_wp def test_uses_package_fallback_as_last_resort(self) -> None: config = Config(background_path=None) with patch("moonset.config.MOONARCH_WALLPAPER", Path("/nonexistent")): result = resolve_background_path(config) # Package fallback should always exist assert result is not None