"""Tests for app.config.get_settings — env-var binding and cache isolation.""" from pathlib import Path from app.config import Settings, get_settings def test_get_settings_reads_env_vars(monkeypatch): """get_settings() must reflect env vars set before it is called.""" # Clear any cached singleton from earlier tests / module imports. get_settings.cache_clear() monkeypatch.setenv("YUNKE_BASE_URL", "https://example.test/") monkeypatch.setenv("YUNKE_USERNAME", "tester") monkeypatch.setenv("YUNKE_PASSWORD", "shh") monkeypatch.setenv( "DB_URL", "mysql+asyncmy://u:p@db.example:3306/yunke_test", ) monkeypatch.setenv("REDIS_URL", "redis://redis.example:6379/2") monkeypatch.setenv("MEDIA_DIR", "/tmp/yunke_media") monkeypatch.setenv("ENCRYPTION_KEY", "abc123") settings = get_settings() assert settings.yunke_base_url == "https://example.test/" assert settings.yunke_username == "tester" assert settings.yunke_password == "shh" assert settings.db_url == "mysql+asyncmy://u:p@db.example:3306/yunke_test" assert settings.redis_url == "redis://redis.example:6379/2" assert settings.media_dir == Path("/tmp/yunke_media") assert settings.encryption_key == "abc123" def test_get_settings_is_cached(monkeypatch): """get_settings() returns the same instance until the cache is cleared.""" get_settings.cache_clear() first = get_settings() second = get_settings() assert first is second # After clearing the cache, a new instance is built. get_settings.cache_clear() third = get_settings() assert third is not first def test_get_settings_picks_up_changes_after_cache_clear(monkeypatch): """After cache_clear(), the next call re-reads the environment.""" get_settings.cache_clear() monkeypatch.setenv("DB_URL", "sqlite+aiosqlite:///:memory:") initial = get_settings() assert initial.db_url == "sqlite+aiosqlite:///:memory:" monkeypatch.setenv("DB_URL", "mysql+asyncmy://other:3306/yunke") # Cached value must NOT change just because env changed. assert get_settings().db_url == "sqlite+aiosqlite:///:memory:" # After clearing, the new env var is picked up. get_settings.cache_clear() assert get_settings().db_url == "mysql+asyncmy://other:3306/yunke" def test_settings_constructable_without_env(): """Settings() can be built with defaults (no env required).""" s = Settings() assert isinstance(s.db_url, str) assert s.encryption_key == "" def test_stealth_defaults_are_conservative(): """The anti-risk-control settings declare conservative defaults. Asserted via Pydantic field-introspection rather than ``Settings()`` so it is immune to the test-env throttle-zeroing in conftest (which intentionally pins ``REQUEST_MIN_INTERVAL``/``REQUEST_JITTER`` to 0 for suite speed). Production deployments that don't set these env vars get the slow, jittered defaults declared here. """ f = Settings.model_fields assert f["request_min_interval"].default == 2.0 assert f["request_jitter"].default == 2.5 assert f["max_requests_per_run"].default == 300 assert f["risk_backoff_base"].default == 5.0 assert f["risk_backoff_max"].default == 120.0 assert f["risk_backoff_attempts"].default == 6