import pytest from pydantic import ValidationError from app.config import AlertsConfig, Settings def _clear_alert_env(monkeypatch: pytest.MonkeyPatch) -> None: for name in ( "G2S_ALERTS__TELEGRAM_ENABLED", "G2S_ALERTS__TELEGRAM_BOT_TOKEN", "G2S_ALERTS__TELEGRAM_CHAT_ID", ): monkeypatch.delenv(name, raising=False) def test_alerts_settings_are_loaded_from_yaml(tmp_path, monkeypatch) -> None: _clear_alert_env(monkeypatch) config_file = tmp_path / "config.yaml" config_file.write_text( """ alerts: telegram_enabled: true telegram_bot_token: "bot-token" telegram_chat_id: "-100777" provider_5xx: error_count: 7 window_minutes: 6 provider_p99_latency: threshold_ms: 5200 window_minutes: 11 provider_unavailable: duration_minutes: 8 """.strip(), encoding="utf-8", ) monkeypatch.setenv("G2S_CONFIG_FILE", str(config_file)) settings = Settings() assert settings.alerts.telegram_enabled is True assert settings.alerts.telegram_bot_token == "bot-token" assert settings.alerts.telegram_chat_id == "-100777" assert settings.alerts.provider_5xx.error_count == 7 assert settings.alerts.provider_5xx.window_minutes == 6 assert settings.alerts.provider_p99_latency.threshold_ms == 5200 assert settings.alerts.provider_p99_latency.window_minutes == 11 assert settings.alerts.provider_unavailable.duration_minutes == 8 @pytest.mark.parametrize( ("yaml_body", "error_message"), [ ( """ alerts: telegram_enabled: true telegram_chat_id: "-100777" """.strip(), "alerts.telegram_bot_token is required when alerts.telegram_enabled=true", ), ( """ alerts: telegram_enabled: true telegram_bot_token: "bot-token" """.strip(), "alerts.telegram_chat_id is required when alerts.telegram_enabled=true", ), ], ) def test_enabled_telegram_requires_credentials( tmp_path, monkeypatch, yaml_body: str, error_message: str, ) -> None: _clear_alert_env(monkeypatch) config_file = tmp_path / "config.yaml" config_file.write_text(yaml_body, encoding="utf-8") monkeypatch.setenv("G2S_CONFIG_FILE", str(config_file)) with pytest.raises(ValidationError, match=error_message): Settings() def test_alert_condition_defaults_match_required_thresholds() -> None: alerts = AlertsConfig() assert alerts.provider_5xx.error_count == 5 assert alerts.provider_5xx.window_minutes == 5 assert alerts.provider_p99_latency.threshold_ms == 5000 assert alerts.provider_p99_latency.window_minutes == 10 assert alerts.provider_unavailable.duration_minutes == 5