add typing simulation

This commit is contained in:
kandrusyak
2026-07-27 17:20:32 +03:00
parent 275eca8dc5
commit 4b7a50d0cc
3 changed files with 151 additions and 86 deletions

View File

@@ -1,8 +1,9 @@
import asyncio
import unittest
from types import SimpleNamespace
from unittest.mock import AsyncMock
from telegram.constants import ParseMode
from telegram.constants import ChatAction, ParseMode
from telegram.error import BadRequest
from assistant_bot.telegram_utils import (
@@ -11,6 +12,7 @@ from assistant_bot.telegram_utils import (
escape_markdown_text,
render_markdown_chunks,
reply_markdown,
typing_action,
)
@@ -112,5 +114,33 @@ class MarkdownDeliveryTests(unittest.IsolatedAsyncioTestCase):
)
class TypingActionTests(unittest.IsolatedAsyncioTestCase):
async def test_refreshes_typing_until_context_exits(self) -> None:
refreshed = asyncio.Event()
call_count = 0
async def send_chat_action(**_kwargs) -> None:
nonlocal call_count
call_count += 1
if call_count >= 2:
refreshed.set()
bot = SimpleNamespace(
send_chat_action=AsyncMock(side_effect=send_chat_action)
)
async with typing_action(bot, chat_id=42, interval=0.001):
await asyncio.wait_for(refreshed.wait(), timeout=0.5)
calls_after_exit = call_count
await asyncio.sleep(0.01)
self.assertGreaterEqual(calls_after_exit, 2)
self.assertEqual(call_count, calls_after_exit)
for call in bot.send_chat_action.await_args_list:
self.assertEqual(call.kwargs["chat_id"], 42)
self.assertEqual(call.kwargs["action"], ChatAction.TYPING)
if __name__ == "__main__":
unittest.main()