61 lines
1.9 KiB
Python
61 lines
1.9 KiB
Python
"""Unit tests for bot Rich Messages sending and error fallback."""
|
|
|
|
from datetime import datetime
|
|
from unittest.mock import AsyncMock, MagicMock
|
|
import pytest
|
|
from aiogram.types import Chat, Message, User
|
|
from homeagent.bot.handlers import _send_reply
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_send_reply_rich_message_success():
|
|
bot = MagicMock()
|
|
bot.send_rich_message = AsyncMock()
|
|
|
|
user = User(id=1, is_bot=False, first_name="Test")
|
|
chat = Chat(id=-100123, type="supergroup")
|
|
msg = Message(
|
|
message_id=42,
|
|
date=datetime.now(),
|
|
chat=chat,
|
|
from_user=user,
|
|
message_thread_id=11,
|
|
is_topic_message=True,
|
|
)
|
|
msg._bot = bot
|
|
|
|
await _send_reply(msg, "Hello in topic with **Rich Message**!")
|
|
|
|
bot.send_rich_message.assert_awaited_once()
|
|
kwargs = bot.send_rich_message.await_args.kwargs
|
|
assert kwargs["chat_id"] == -100123
|
|
assert kwargs["message_thread_id"] == 11
|
|
rich_msg = kwargs["rich_message"]
|
|
assert rich_msg.markdown == "Hello in topic with **Rich Message**!"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_send_reply_rich_message_fallback_to_html():
|
|
bot = MagicMock()
|
|
# First call with markdown fails, second call with html succeeds
|
|
bot.send_rich_message = AsyncMock(side_effect=[Exception("Invalid Markdown"), None])
|
|
|
|
user = User(id=1, is_bot=False, first_name="Test")
|
|
chat = Chat(id=-100123, type="supergroup")
|
|
msg = Message(
|
|
message_id=42,
|
|
date=datetime.now(),
|
|
chat=chat,
|
|
from_user=user,
|
|
message_thread_id=11,
|
|
is_topic_message=True,
|
|
)
|
|
msg._bot = bot
|
|
|
|
await _send_reply(msg, "Text with _bad markdown")
|
|
|
|
assert bot.send_rich_message.await_count == 2
|
|
second_kwargs = bot.send_rich_message.await_args_list[1].kwargs
|
|
assert second_kwargs["rich_message"].html == "Text with _bad markdown"
|
|
assert second_kwargs["message_thread_id"] == 11
|