008 add telegram alerts

This commit is contained in:
Раис Юсупалиев
2026-03-08 12:04:42 +03:00
parent 8d47917ba2
commit 34d7a4c336
10 changed files with 256 additions and 6 deletions
+93
View File
@@ -0,0 +1,93 @@
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
+6
View File
@@ -15,6 +15,8 @@ def test_configuration_sections_are_loaded_from_env(monkeypatch) -> None:
monkeypatch.setenv("G2S_ADAPTER__CDEK_CACHE_TTL_SECONDS", "780")
monkeypatch.setenv("G2S_OBSERVABILITY__SERVICE_NAME", "g2s-test")
monkeypatch.setenv("G2S_ALERTS__TELEGRAM_ENABLED", "true")
monkeypatch.setenv("G2S_ALERTS__TELEGRAM_BOT_TOKEN", "bot-token")
monkeypatch.setenv("G2S_ALERTS__TELEGRAM_CHAT_ID", "-100")
settings = Settings()
@@ -57,6 +59,8 @@ observability:
service_name: "g2s-yaml"
alerts:
telegram_enabled: true
telegram_bot_token: "yaml-bot-token"
telegram_chat_id: "-100100"
""".strip(),
encoding="utf-8",
)
@@ -77,6 +81,8 @@ alerts:
assert settings.adapter.cdek_cache_ttl_seconds == 810
assert settings.observability.service_name == "g2s-yaml"
assert settings.alerts.telegram_enabled is True
assert settings.alerts.telegram_bot_token == "yaml-bot-token"
assert settings.alerts.telegram_chat_id == "-100100"
def test_get_settings_returns_cached_instance(monkeypatch) -> None: