initial commit
This commit is contained in:
70
tests/test_telegram_sender.py
Normal file
70
tests/test_telegram_sender.py
Normal file
@@ -0,0 +1,70 @@
|
||||
import pytest
|
||||
|
||||
from glitchup_bot.api.schemas import WebhookAttachment
|
||||
from glitchup_bot.services import telegram_sender
|
||||
|
||||
|
||||
class FakeBot:
|
||||
def __init__(self) -> None:
|
||||
self.calls: list[dict] = []
|
||||
|
||||
async def send_message(self, **kwargs) -> None:
|
||||
self.calls.append(kwargs)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_send_alert_routes_to_topic_and_subscribers(monkeypatch):
|
||||
fake_bot = FakeBot()
|
||||
monkeypatch.setattr(telegram_sender, "get_bot", lambda: fake_bot)
|
||||
monkeypatch.setattr(
|
||||
telegram_sender, "resolve_group", lambda project_slug: _async_value("backend")
|
||||
)
|
||||
monkeypatch.setattr(telegram_sender, "resolve_topic_id", lambda group_name: _async_value(11))
|
||||
monkeypatch.setattr(
|
||||
telegram_sender,
|
||||
"resolve_subscribers",
|
||||
lambda group_name: _async_value([101, 202]),
|
||||
)
|
||||
|
||||
await telegram_sender.send_alert(
|
||||
WebhookAttachment(
|
||||
title="<boom>",
|
||||
text="service <api>",
|
||||
title_link="https://glitchtip.example.com/issues/1?x=<x>",
|
||||
color="#e52b50",
|
||||
),
|
||||
project_slug="backend-production",
|
||||
priority="P1",
|
||||
release_name="2026.03.27",
|
||||
)
|
||||
|
||||
assert len(fake_bot.calls) == 3
|
||||
assert fake_bot.calls[0]["chat_id"] == -1001234567890
|
||||
assert fake_bot.calls[0]["message_thread_id"] == 11
|
||||
assert "<boom>" in fake_bot.calls[0]["text"]
|
||||
assert "service <api>" in fake_bot.calls[0]["text"]
|
||||
assert "2026.03.27" in fake_bot.calls[0]["text"]
|
||||
assert fake_bot.calls[1]["chat_id"] == 101
|
||||
assert fake_bot.calls[2]["chat_id"] == 202
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_send_digest_message_uses_digest_topic(monkeypatch):
|
||||
fake_bot = FakeBot()
|
||||
monkeypatch.setattr(telegram_sender, "get_bot", lambda: fake_bot)
|
||||
monkeypatch.setattr(telegram_sender, "resolve_topic_id", lambda group_name: _async_value(33))
|
||||
|
||||
await telegram_sender.send_digest_message("<b>digest</b>")
|
||||
|
||||
assert fake_bot.calls == [
|
||||
{
|
||||
"chat_id": -1001234567890,
|
||||
"message_thread_id": 33,
|
||||
"text": "<b>digest</b>",
|
||||
"disable_web_page_preview": True,
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
async def _async_value(value):
|
||||
return value
|
||||
Reference in New Issue
Block a user