""" Tests for the settings hierarchy in app.get_settings(). Hierarchy under test: env-var defaults are returned when settings.json is missing or malformed; the file wins when it exists and parses cleanly. """ import json import os import sys import tempfile import pytest # Make the app module importable without installing it. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) import app as app_module # noqa: E402 @pytest.fixture def tmp_settings_path(monkeypatch, tmp_path): """Point app.SETTINGS_FILE at a temp path for the duration of the test.""" path = tmp_path / "settings.json" monkeypatch.setattr(app_module, "SETTINGS_FILE", str(path)) return path def test_get_settings_returns_defaults_when_file_missing(tmp_settings_path, monkeypatch): monkeypatch.setenv("SYNC_SCHEDULE", "5 5 * * *") monkeypatch.setenv("MAX_QUALITY", "720") monkeypatch.setenv("SLEEP_INTERVAL", "7") monkeypatch.setenv("TZ", "UTC") assert not tmp_settings_path.exists() result = app_module.get_settings() assert result == { "sync_schedule": "5 5 * * *", "max_quality": "720", "sleep_interval": "7", "timezone": "UTC", } def test_get_settings_returns_file_contents_when_present(tmp_settings_path): payload = { "sync_schedule": "0 4 * * *", "max_quality": "1440", "sleep_interval": "3", "timezone": "America/Phoenix", } tmp_settings_path.write_text(json.dumps(payload)) assert app_module.get_settings() == payload def test_get_settings_falls_back_on_malformed_json(tmp_settings_path, monkeypatch): monkeypatch.setenv("SYNC_SCHEDULE", "0 2 * * *") monkeypatch.setenv("MAX_QUALITY", "1080") monkeypatch.setenv("SLEEP_INTERVAL", "2") monkeypatch.setenv("TZ", "America/Phoenix") tmp_settings_path.write_text("{ this is not valid json ") result = app_module.get_settings() assert result["sync_schedule"] == "0 2 * * *" assert result["max_quality"] == "1080" assert result["sleep_interval"] == "2" assert result["timezone"] == "America/Phoenix"