59 lines
1.8 KiB
Python
59 lines
1.8 KiB
Python
from __future__ import annotations
|
|
|
|
import unittest
|
|
from types import SimpleNamespace
|
|
from unittest.mock import AsyncMock
|
|
|
|
from app.commands import CommandDispatcher, CommandRegistry
|
|
|
|
|
|
class CommandDispatcherTests(unittest.IsolatedAsyncioTestCase):
|
|
async def test_command_in_media_caption_is_dispatched(self) -> None:
|
|
registry = CommandRegistry()
|
|
handler = AsyncMock()
|
|
registry.command("photo", "Caption command")(handler)
|
|
dispatcher = CommandDispatcher(registry)
|
|
message = SimpleNamespace(
|
|
text=None,
|
|
caption=".photo argument",
|
|
from_user=SimpleNamespace(id=123),
|
|
reply_text=AsyncMock(),
|
|
)
|
|
account = SimpleNamespace(me=SimpleNamespace(id=123), acl_store=None)
|
|
|
|
handled = await dispatcher.dispatch(
|
|
client=SimpleNamespace(),
|
|
message=message,
|
|
manager=SimpleNamespace(),
|
|
account=account,
|
|
)
|
|
|
|
self.assertTrue(handled)
|
|
handler.assert_awaited_once()
|
|
context = handler.await_args.args[0]
|
|
self.assertEqual(context.args, "argument")
|
|
self.assertEqual(context.raw_text, ".photo argument")
|
|
|
|
async def test_unknown_owner_command_is_silent(self) -> None:
|
|
dispatcher = CommandDispatcher(CommandRegistry())
|
|
message = SimpleNamespace(
|
|
text=".missing",
|
|
from_user=SimpleNamespace(id=123),
|
|
reply_text=AsyncMock(),
|
|
)
|
|
account = SimpleNamespace(me=SimpleNamespace(id=123))
|
|
|
|
handled = await dispatcher.dispatch(
|
|
client=SimpleNamespace(),
|
|
message=message,
|
|
manager=SimpleNamespace(),
|
|
account=account,
|
|
)
|
|
|
|
self.assertTrue(handled)
|
|
message.reply_text.assert_not_awaited()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|