The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
test(telegram): fold video-note coverage into the existing media tests
kshitijk4poor48f744e99743
diff --git a/tests/gateway/test_telegram_documents.py b/tests/gateway/test_telegram_documents.pyindex 4c17d3456b5..eb35258c4a4 100644--- a/tests/gateway/test_telegram_documents.py+++ b/tests/gateway/test_telegram_documents.py@@ -257,14 +257,29 @@ class TestDocumentDownloadBlock:event = adapter.handle_message.call_args[0][0]assert "could not be downloaded" in (event.text or "")+ @pytest.mark.asyncio+ async def test_video_note_failure_reply_is_localized(self, adapter, monkeypatch):+ monkeypatch.setenv("HERMES_LANGUAGE", "de")+ msg = _make_message()+ msg.video_note = _make_video()+ msg.video_note.file_size = 100+ msg.video_note.get_file = AsyncMock(side_effect=RuntimeError("CDN down"))++ await adapter._handle_media_message(_make_update(msg), MagicMock())++ reply = str(msg.reply_text.await_args)+ assert "Videodatei" in reply and "video note" not in reply+ assert "send a video note" in adapter.handle_message.call_args[0][0].text+class TestVideoDownloadBlock:@pytest.mark.asyncio- async def test_native_video_is_cached(self, adapter):+ @pytest.mark.parametrize("attr", ["video", "video_note"])+ async def test_native_video_is_cached(self, adapter, attr):file_obj = _make_file_obj(b"fake-mp4")- file_obj.file_path = "videos/clip.mp4"+ file_obj.file_path = f"{attr}s/clip.mp4"msg = _make_message()- msg.video = _make_video(file_obj)+ setattr(msg, attr, _make_video(file_obj))update = _make_update(msg)await adapter._handle_media_message(update, MagicMock())@@ -274,6 +289,46 @@ class TestVideoDownloadBlock:assert os.path.exists(event.media_urls[0])assert event.media_types == [SUPPORTED_VIDEO_TYPES[".mp4"]]+ def test_media_handler_accepts_video_notes(self, adapter):+ class _Names: # filter stand-in: | collects names; & and ~ only need to compose+ def __init__(self, *names):+ self.names = set(names)++ def __or__(self, other):+ return _Names(*self.names, *other.names)++ __and__ = __or__++ def __invert__(self):+ return self++ fake_filters = SimpleNamespace(+ **{name: _Names(name) for name in ("TEXT", "COMMAND", "LOCATION", "VENUE", "PHOTO", "VIDEO",+ "VIDEO_NOTE", "AUDIO", "VOICE")},+ Document=SimpleNamespace(ALL=_Names("DOCUMENT")), Sticker=SimpleNamespace(ALL=_Names("STICKER")),+ StatusUpdate=None)+ registered = {}+ with patch.multiple("plugins.platforms.telegram.adapter", filters=fake_filters,+ TelegramMessageHandler=lambda flt, callback: registered.setdefault(callback, flt)):+ adapter._register_handlers(MagicMock())+ assert {"VIDEO", "VIDEO_NOTE"} <= registered[adapter._handle_media_message].names++ @pytest.mark.asyncio+ async def test_reply_to_a_video_note_caches_it(self, adapter):+ file_obj = _make_file_obj(b"fake-mp4")+ file_obj.file_path = "video_notes/file_1.mp4"+ replied = _make_message()+ replied.video_note = _make_video(file_obj)+ replied.video_note.file_size = 1024+ msg = _make_message()+ msg.reply_to_message = replied+ event = MessageEvent(text="what is in this?", message_type=MessageType.TEXT)++ await adapter._cache_replied_media(msg, event)+ assert event.message_type == MessageType.VIDEO+ assert event.media_types == ["video/mp4"]+ assert os.path.exists(event.media_urls[0])+# ---------------------------------------------------------------------------# TestMediaGroups — media group (album) bufferingdiff --git a/tests/gateway/test_telegram_video_note.py b/tests/gateway/test_telegram_video_note.pydeleted file mode 100644index 1d5f8d10da4..00000000000--- a/tests/gateway/test_telegram_video_note.py+++ /dev/null@@ -1,126 +0,0 @@-from types import SimpleNamespace-from unittest.mock import AsyncMock, patch--import pytest--from gateway.platforms.event import MessageType-from plugins.platforms.telegram.adapter import TelegramAdapter---class _DummyApp:- def __init__(self):- self.handlers = []-- def add_handler(self, handler, group=0):- self.handlers.append((group, handler))---class _Filter:- def __init__(self, name):- self.names = {name}-- def __or__(self, other):- combined = _Filter("combined")- combined.names = self.names | other.names- return combined-- __and__ = __or__-- def __invert__(self):- return self---def test_media_handler_registration_accepts_video_note():- adapter = object.__new__(TelegramAdapter)- app = _DummyApp()- fake_filters = SimpleNamespace(- TEXT=_Filter("text"),- COMMAND=_Filter("command"),- LOCATION=_Filter("location"),- VENUE=_Filter("venue"),- PHOTO=_Filter("photo"),- VIDEO=_Filter("video"),- VIDEO_NOTE=_Filter("video_note"),- AUDIO=_Filter("audio"),- VOICE=_Filter("voice"),- Document=SimpleNamespace(ALL=_Filter("document")),- Sticker=SimpleNamespace(ALL=_Filter("sticker")),- )-- def handler(filter_value, callback):- return SimpleNamespace(filter_value=filter_value, callback=callback)-- with patch.multiple(- "plugins.platforms.telegram.adapter",- filters=fake_filters,- TelegramMessageHandler=handler,- CallbackQueryHandler=lambda callback: SimpleNamespace(callback=callback),- InlineQueryHandler=lambda callback: SimpleNamespace(callback=callback),- TypeHandler=lambda update_type, callback: SimpleNamespace(callback=callback),- ):- adapter._register_handlers(app)-- media_filter_names = app.handlers[3][1].filter_value.names- assert "video" in media_filter_names- assert "video_note" in media_filter_names--[email protected]("attachment_attr", ["video", "video_note"])-async def test_inbound_video_types_use_existing_video_cache(attachment_attr):- adapter = object.__new__(TelegramAdapter)- adapter._is_user_authorized_from_message = lambda _message: True- adapter._should_process_message = lambda _message: True- adapter._telegram_media_size_allowed = lambda _source, _label: (True, None)- adapter._apply_telegram_group_observe_attribution = lambda event: event-- handled = []-- async def handle_message(event):- handled.append(event)-- adapter.handle_message = handle_message- adapter._build_message_event = lambda _message, message_type, update_id=None: SimpleNamespace(- text="",- message_type=message_type,- media_urls=[],- media_types=[],- )-- file_obj = SimpleNamespace(- file_path="telegram-video.mp4",- download_as_bytearray=AsyncMock(return_value=bytearray(b"mp4-data")),- )- attachment = SimpleNamespace(- file_size=128,- get_file=AsyncMock(return_value=file_obj),- )- fields = {- "caption": None,- "sticker": None,- "photo": None,- "voice": None,- "audio": None,- "video": None,- "video_note": None,- "document": None,- "media_group_id": None,- }- fields[attachment_attr] = attachment- msg = SimpleNamespace(**fields)- update = SimpleNamespace(message=msg, update_id=303)-- with patch(- "plugins.platforms.telegram.adapter.cache_video_from_bytes_async",- new=AsyncMock(return_value="/cache/video.mp4"),- ) as cache_video:- await TelegramAdapter._handle_media_message(adapter, update, SimpleNamespace())-- assert len(handled) == 1- event = handled[0]- assert event.message_type == MessageType.VIDEO- assert event.media_urls == ["/cache/video.mp4"]- assert event.media_types == ["video/mp4"]- attachment.get_file.assert_awaited_once_with()- file_obj.download_as_bytearray.assert_awaited_once_with()- cache_video.assert_awaited_once_with(b"mp4-data", ext=".mp4")