| 1 | import asyncio |
| 2 | |
| 3 | from plugins._telegram_integration.helpers import handler |
| 4 | from plugins._telegram_integration.helpers import telegram_client as tc |
| 5 | from plugins._telegram_integration.helpers.constants import ( |
| 6 | CTX_TG_BOT, |
| 7 | CTX_TG_CHAT_ID, |
| 8 | CTX_TG_REPLY_TO, |
| 9 | ) |
| 10 | |
| 11 | |
| 12 | class FakeBotInstance: |
| 13 | class Bot: |
| 14 | token = "token" |
| 15 | |
| 16 | bot = Bot() |
| 17 | |
| 18 | |
| 19 | class FakeContext: |
| 20 | def __init__(self): |
| 21 | self.data = { |
| 22 | CTX_TG_BOT: "main", |
| 23 | CTX_TG_CHAT_ID: 123, |
| 24 | CTX_TG_REPLY_TO: 456, |
| 25 | } |
| 26 | |
| 27 | |
| 28 | class FakeTempBot: |
| 29 | async def __aenter__(self): |
| 30 | return object() |
| 31 | |
| 32 | async def __aexit__(self, exc_type, exc, tb): |
| 33 | return False |
| 34 | |
| 35 | |
| 36 | class FakeMediaBot: |
| 37 | def __init__(self): |
| 38 | self.calls = [] |
| 39 | |
| 40 | async def send_voice(self, **kwargs): |
| 41 | self.calls.append(("voice", kwargs)) |
| 42 | return type("Message", (), {"message_id": 11})() |
| 43 | |
| 44 | async def send_audio(self, **kwargs): |
| 45 | self.calls.append(("audio", kwargs)) |
| 46 | return type("Message", (), {"message_id": 12})() |
| 47 | |
| 48 | async def send_video(self, **kwargs): |
| 49 | self.calls.append(("video", kwargs)) |
| 50 | return type("Message", (), {"message_id": 13})() |
| 51 | |
| 52 | |
| 53 | def test_telegram_client_native_media_helpers(monkeypatch): |
| 54 | bot = FakeMediaBot() |
| 55 | monkeypatch.setattr(tc.os.path, "isfile", lambda path: True) |
| 56 | |
| 57 | voice_id = asyncio.run(tc.send_voice(bot, 123, "voice.ogg", reply_to_message_id=456)) |
| 58 | audio_id = asyncio.run(tc.send_audio(bot, 123, "song.mp3", reply_to_message_id=456)) |
| 59 | video_id = asyncio.run(tc.send_video(bot, 123, "clip.mp4", reply_to_message_id=456)) |
| 60 | |
| 61 | assert (voice_id, audio_id, video_id) == (11, 12, 13) |
| 62 | assert [kind for kind, _ in bot.calls] == ["voice", "audio", "video"] |
| 63 | assert bot.calls[0][1]["chat_id"] == 123 |
| 64 | assert bot.calls[0][1]["reply_to_message_id"] == 456 |
| 65 | |
| 66 | |
| 67 | def test_telegram_reply_routes_native_media_attachments(monkeypatch): |
| 68 | calls = [] |
| 69 | |
| 70 | def fake_temp_bot(*args, **kwargs): |
| 71 | return FakeTempBot() |
| 72 | |
| 73 | async def record(kind, bot, chat_id, path, reply_to_message_id=None, **kwargs): |
| 74 | calls.append( |
| 75 | { |
| 76 | "kind": kind, |
| 77 | "chat_id": chat_id, |
| 78 | "path": path, |
| 79 | "reply_to_message_id": reply_to_message_id, |
| 80 | } |
| 81 | ) |
| 82 | return len(calls) |
| 83 | |
| 84 | monkeypatch.setattr(handler, "get_bot", lambda name: FakeBotInstance()) |
| 85 | monkeypatch.setattr(handler, "_temp_bot", fake_temp_bot) |
| 86 | monkeypatch.setattr(handler.files, "fix_dev_path", lambda path: path) |
| 87 | monkeypatch.setattr(handler.tc, "send_photo", lambda *a, **kw: record("photo", *a, **kw)) |
| 88 | monkeypatch.setattr(handler.tc, "send_voice", lambda *a, **kw: record("voice", *a, **kw)) |
| 89 | monkeypatch.setattr(handler.tc, "send_audio", lambda *a, **kw: record("audio", *a, **kw)) |
| 90 | monkeypatch.setattr(handler.tc, "send_video", lambda *a, **kw: record("video", *a, **kw)) |
| 91 | monkeypatch.setattr(handler.tc, "send_file", lambda *a, **kw: record("document", *a, **kw)) |
| 92 | |
| 93 | error = asyncio.run( |
| 94 | handler.send_telegram_reply( |
| 95 | FakeContext(), |
| 96 | "", |
| 97 | ["image.png", "voice.ogg", "song.mp3", "clip.mp4", "archive.zip"], |
| 98 | ) |
| 99 | ) |
| 100 | |
| 101 | assert error is None |
| 102 | assert calls == [ |
| 103 | {"kind": "photo", "chat_id": 123, "path": "image.png", "reply_to_message_id": 456}, |
| 104 | {"kind": "voice", "chat_id": 123, "path": "voice.ogg", "reply_to_message_id": 456}, |
| 105 | {"kind": "audio", "chat_id": 123, "path": "song.mp3", "reply_to_message_id": 456}, |
| 106 | {"kind": "video", "chat_id": 123, "path": "clip.mp4", "reply_to_message_id": 456}, |
| 107 | {"kind": "document", "chat_id": 123, "path": "archive.zip", "reply_to_message_id": 456}, |
| 108 | ] |