def test_ignore_non_vkontakte(): app, debug, hu = make_kutana_no_run() pl = Plugin("") @pl.vk.on_payloads([{"command": "echo"}]) async def __(msg, ctx): await ctx.reply(ctx.payload["text"]) @pl.vk.on_callbacks([{"val": 1}]) async def __(upd, ctx): await ctx.send_message_event_answer({ "type": "show_snackbar", "text": "hey hey hey", }) app.add_plugin(pl) raw1 = make_message_update('{"command": "echo", "text": "hello"}') raw2 = MESSAGES["inline_callback_1"] hu(Message(raw1, UpdateType.MSG, "hey1", (), 1, 0, 0, 0, {})) hu(Update(raw2, UpdateType.UPD, {})) assert not debug.answers assert not debug.requests
def test_event_handlers_order(): app, __, __ = make_kutana_no_run() pl = Plugin("", called=[]) @pl.on_before(priority=5) async def __(upd, ctx): pl.called.append(2) @pl.on_before(priority=0) async def __(upd, ctx): pl.called.append(3) @pl.on_before(priority=10) async def __(upd, ctx): pl.called.append(1) @pl.on_before(priority=-10) async def __(upd, ctx): pl.called.append(4) app.add_plugin(pl) sync(app._handle_event("before", None, None)) assert pl.called == [1, 2, 3, 4]
def test_commands(): app, debug, hu = make_kutana_no_run() pl = Plugin("") @pl.on_commands(["echo", "ec"]) async def _(msg, ctx): await ctx.reply(msg.text) app.add_plugin(pl) hu(Message(None, UpdateType.MSG, ".echo 123", (), 1, 0, 0, 0)) hu(Message(None, UpdateType.MSG, ".ec abc", (), 1, 0, 0, 0)) hu(Message(None, UpdateType.MSG, ".echo", (), 1, 0, 0, 0)) hu(Message(None, UpdateType.MSG, ".ec\n123", (), 1, 0, 0, 0)) hu(Message(None, UpdateType.MSG, ".ecabc", (), 1, 0, 0, 0)) hu(Message(None, UpdateType.MSG, ".e cabc", (), 1, 0, 0, 0)) hu(Message(None, UpdateType.MSG, "abc", (), 1, 0, 0, 0)) hu(Message(None, UpdateType.MSG, "echo abc", (), 1, 0, 0, 0)) assert hu(Update(None, UpdateType.UPD)) == hr.SKIPPED assert len(debug.answers[1]) == 4 assert debug.answers[1][0] == (".echo 123", (), {}) assert debug.answers[1][1] == (".ec abc", (), {}) assert debug.answers[1][2] == (".echo", (), {}) assert debug.answers[1][3] == (".ec\n123", (), {})
def test_router_priority(): app, debug, hu = make_kutana_no_run() pl = Plugin("") received = [] @pl.on_updates(router_priority=10) async def __(upd, ctx): received.append(upd.raw * 2) @pl.on_unprocessed_updates(priority=1, router_priority=10) async def __(upd, ctx): received.append(upd.raw * 3) @pl.on_updates(router_priority=20) async def __(upd, ctx): if upd.raw == 'a': return hr.SKIPPED received.append(upd.raw) app.add_plugin(pl) hu(Update('a', UpdateType.UPD, {})) hu(Update('b', UpdateType.UPD, {})) assert hu(Message(None, UpdateType.MSG, ".123", (), 1, 0, 0, 0, {})) == hr.SKIPPED assert received[0] == 'aaa' assert received[1] == 'b'
def test_attachments(): app, debug, hu = make_kutana_no_run() pl = Plugin("") @pl.on_attachments(["image", "voice"]) async def _(msg, ctx): await ctx.reply(msg.text) app.add_plugin(pl) image = Attachment("", "image", "", None, "", None, "present") voice = Attachment("", "voice", "", None, "", None, "present") sticker = Attachment("", "sticker", "", None, "", None, "present") hu(Message(None, UpdateType.MSG, "i", (image, ), 1, 0, 0, 0)) hu(Message(None, UpdateType.MSG, "v", (voice, ), 1, 0, 0, 0)) hu(Message(None, UpdateType.MSG, "vi", (voice, image), 1, 0, 0, 0)) hu(Message(None, UpdateType.MSG, "si", ( sticker, image, ), 1, 0, 0, 0)) hu(Message(None, UpdateType.MSG, "s", (sticker, ), 1, 0, 0, 0)) assert hu(Update(None, UpdateType.UPD)) == hr.SKIPPED assert len(debug.answers[1]) == 4 assert debug.answers[1][0] == ("i", (), {}) assert debug.answers[1][1] == ("v", (), {}) assert debug.answers[1][2] == ("vi", (), {}) assert debug.answers[1][3] == ("si", (), {})
def test_receiver_states(): app, debug, hu = make_kutana_no_run() pl = Plugin("") pl.app = app @pl.on_commands(["a"]) @pl.expect_receiver(state="") async def __(msg, ctx): await ctx.receiver.update({"state": "state"}) await ctx.reply("ok:a") @pl.on_commands(["b"]) @pl.expect_receiver(state="state") async def __(msg, ctx): await ctx.receiver.update({"state": ""}) await ctx.reply("ok:b") @pl.on_commands(["a"]) @pl.expect_receiver(state="other_state") async def __(msg, ctx): await ctx.receiver.update({"state": ""}) await ctx.reply("ok:b") app.add_plugin(pl) hu(Message(None, UpdateType.MSG, ".a", (), 1, 0, 0, 0, {})) hu(Message(None, UpdateType.MSG, ".b", (), 1, 0, 0, 0, {})) hu(Message(None, UpdateType.MSG, ".b", (), 1, 0, 0, 0, {})) hu(Message(None, UpdateType.MSG, ".a", (), 1, 0, 0, 0, {})) assert len(debug.answers[1]) == 3 assert debug.answers[1].count(("ok:a", (), {})) == 2 assert debug.answers[1].count(("ok:b", (), {})) == 1 assert debug.answers[1][-1] == ("ok:a", (), {})
def test_quest(): app, debug, hu = make_kutana_no_run() app.add_plugin(pl) hu(Message(None, UpdateType.MSG, ".start", (), 1, 0, 0, 0)) hu(Message(None, UpdateType.MSG, ".left", (), 1, 0, 0, 0)) hu(Message(None, UpdateType.MSG, ".ok", (), 1, 0, 0, 0)) assert debug.answers[1][-1] == ("Bye", (), {}) hu(Message(None, UpdateType.MSG, ".start", (), 1, 0, 0, 0)) hu(Message(None, UpdateType.MSG, ".start", (), 2, 0, 0, 0)) hu(Message(None, UpdateType.MSG, ".left", (), 1, 0, 0, 0)) hu(Message(None, UpdateType.MSG, ".right", (), 2, 0, 0, 0)) hu(Message(None, UpdateType.MSG, ".ok", (), 1, 0, 0, 0)) hu(Message(None, UpdateType.MSG, ".ok", (), 2, 0, 0, 0)) assert debug.answers[1][-1] == ("Bye", (), {}) assert debug.answers[2][-1] == ("Bye", (), {}) hu(Message(None, UpdateType.MSG, ".left", (), 1, 0, 0, 0)) hu(Message(None, UpdateType.MSG, ".right", (), 1, 0, 0, 0)) assert debug.answers[1][-1] == ("Bye", (), {}) hu(Message(None, UpdateType.MSG, ".start", (), 1, 0, 0, 0)) hu(Message(None, UpdateType.MSG, ".left", (), 1, 0, 0, 0)) hu(Message(None, UpdateType.MSG, ".bruh", (), 1, 0, 0, 0)) assert debug.answers[1][-1] == ("Write '.ok'", (), {}) hu(Message(None, UpdateType.MSG, ".ok", (), 1, 0, 0, 0)) assert debug.answers[1][-1] == ("Bye", (), {}) hu(Message(None, UpdateType.MSG, ".bruh", (), 1, 0, 0, 0)) assert debug.answers[1][-1] == ("Bye", (), {})
def test_on_payloads_types(): app, debug, hu = make_kutana_no_run(backend_source="vkontakte") pl = Plugin("") @pl.vk.on_payloads([{"command": {"why": "test"}}, "txt"]) async def __(msg, ctx): await ctx.reply(msg.text) app.add_plugin(pl) raw1 = make_message_update('{"command": {"why": "test"}}') raw2 = make_message_update('{"command": []}') raw3 = make_message_update('{"command": [{"a": 1}]}') raw4 = make_message_update('"txt"') raw5 = make_message_update("error") hu(Message(raw1, UpdateType.MSG, "hey1", (), 1, 0, 0, 0, {})) hu(Message(raw2, UpdateType.MSG, "hey2", (), 1, 0, 0, 0, {})) hu(Message(raw3, UpdateType.MSG, "hey3", (), 1, 0, 0, 0, {})) hu(Message(raw4, UpdateType.MSG, "hey4", (), 1, 0, 0, 0, {})) hu(Message(raw5, UpdateType.MSG, "hey5", (), 1, 0, 0, 0, {})) assert hu(Update(None, UpdateType.UPD, {})) == hr.SKIPPED assert len(debug.answers[1]) == 2 assert debug.answers[1][0] == ("hey1", (), {}) assert debug.answers[1][1] == ("hey4", (), {})
def test_storage(): app, __, __ = make_kutana_no_run() pl = Plugin("", storage={}) pl.app = app assert pl.storage == {} pl = Plugin("", storage='default') pl.app = app assert pl.storage == app._storages['default'] pl = Plugin("", storage='unknown') pl.app = app assert pl.storage is None
def test_command_full_body(): app, debug, hu = make_kutana_no_run() pl = Plugin("") @pl.on_commands(["echo"]) async def _(msg, ctx): await ctx.reply(ctx.body) app.add_plugin(pl) hu(Message(None, UpdateType.MSG, ".echo abc\nabc\nabc", (), 1, 0, 0, 0)) assert len(debug.answers[1]) == 1 assert debug.answers[1][0] == ("abc\nabc\nabc", (), {})
def test_any_message(): app, debug, hu = make_kutana_no_run() pl = Plugin("") @pl.on_any_message() async def _(msg, ctx): await ctx.reply(msg.text) app.add_plugin(pl) hu(Message(None, UpdateType.MSG, "123", (), 1, 0, 0, 0)) hu(Message(None, UpdateType.MSG, "abc", (), 1, 0, 0, 0)) assert hu(Update(None, UpdateType.UPD)) == hr.SKIPPED assert debug.answers[1][0] == ("123", (), {}) assert debug.answers[1][1] == ("abc", (), {})
def test_on_message_actions_types(): app, debug, hu = make_kutana_no_run(backend_source="vkontakte") pl = Plugin("") @pl.vk.on_message_actions(["chat_photo_remove"]) async def __(msg, ctx): await ctx.reply(msg.raw['object']['message']["action"]["photo"]["photo_50"]) @pl.vk.on_message_actions(['chat_invite_user']) async def __(msg, ctx): if msg.raw['object']['message']['action']['member_id'] < 0: await ctx.reply(msg.raw['object']['message']['action']['email']) app.add_plugin(pl) raw1 = make_message_action('chat_photo_update', 1, None, None, { "photo_50": "http://example.com/50", "photo_100": "http://example.com/100", "photo_200": "http://example.com/200", }) raw2 = make_message_action('chat_photo_remove', 1, None, None, { "photo_50": "http://example.com/50", "photo_100": "http://example.com/100", "photo_200": "http://example.com/200", }) raw3 = make_message_action('chat_title_update', 1, 'title1', None, None) raw4 = make_message_action('chat_invite_user', -1, None, "*****@*****.**", None) raw5 = make_message_action('chat_kick_user', -1, None, "*****@*****.**", None) raw6 = make_message_update("error") hu(Message(raw1, UpdateType.MSG, "hey1", (), 1, 0, 0, 0, {})) hu(Message(raw2, UpdateType.MSG, "hey2", (), 1, 0, 0, 0, {})) hu(Message(raw3, UpdateType.MSG, "hey3", (), 1, 0, 0, 0, {})) hu(Message(raw4, UpdateType.MSG, "hey4", (), 1, 0, 0, 0, {})) hu(Message(raw5, UpdateType.MSG, "hey5", (), 1, 0, 0, 0, {})) hu(Message(raw6, UpdateType.MSG, "hey6", (), 1, 0, 0, 0, {})) assert hu(Update(None, UpdateType.UPD, {})) == hr.SKIPPED assert len(debug.answers[1]) == 2 assert debug.answers[1][0] == ("http://example.com/50", (), {}) assert debug.answers[1][1] == ("*****@*****.**", (), {})
def test_commands_with_spaces(): app, debug, hu = make_kutana_no_run() pl = Plugin("") @pl.on_commands(["echo", "ec"]) async def __(msg, ctx): await ctx.reply(msg.text) app.add_plugin(pl) hu(Message(None, UpdateType.MSG, " . echo 123", (), 1, 0, 0, 0, {})) hu(Message(None, UpdateType.MSG, ". echo 123", (), 1, 0, 0, 0, {})) hu(Message(None, UpdateType.MSG, ".\nec abc", (), 1, 0, 0, 0, {})) assert len(debug.answers[1]) == 3 assert debug.answers[1][0] == (" . echo 123", (), {}) assert debug.answers[1][1] == (". echo 123", (), {}) assert debug.answers[1][2] == (".\nec abc", (), {})
def test_on_message_actions(): app, debug, hu = make_kutana_no_run(backend_source="vkontakte") pl = Plugin("") @pl.vk.on_message_actions(['chat_title_update', 'chat_create']) async def __(msg, ctx): await ctx.reply(msg.raw["object"]["message"]["action"]["text"]) app.add_plugin(pl) raw1 = make_message_action('chat_invite_user_by_link', 1, None, None, None) raw2 = make_message_action('chat_title_update', 1, 'they1', None, None) hu(Message(raw1, UpdateType.MSG, 'hey1', (), 1, 0, 0, 0, {})) hu(Message(raw2, UpdateType.MSG, 'hey2', (), 1, 0, 0, 0, {})) assert len(debug.answers[1]) == 1 assert debug.answers[1][0] == ("they1", (), {})
def test_on_payloads(): app, debug, hu = make_kutana_no_run(backend_source="vkontakte") pl = Plugin("") @pl.vk.on_payloads([{"command": "test"}]) async def __(msg, ctx): await ctx.reply(msg.text) app.add_plugin(pl) raw1 = make_message_update('{"command": "test"}') raw2 = make_message_update('{"command": "error"}') hu(Message(raw1, UpdateType.MSG, "hey1", (), 1, 0, 0, 0, {})) hu(Message(raw2, UpdateType.MSG, "hey2", (), 1, 0, 0, 0, {})) assert len(debug.answers[1]) == 1 assert debug.answers[1][0] == ("hey1", (), {})
def test_any_update(): app, debug, hu = make_kutana_no_run() pl = Plugin("") received = [] @pl.on_updates() async def __(upd, ctx): received.append(upd.raw) app.add_plugin(pl) hu(Update('a', UpdateType.UPD, {})) hu(Update('b', UpdateType.UPD, {})) assert hu(Message(None, UpdateType.MSG, ".123", (), 1, 0, 0, 0, {})) == hr.SKIPPED assert received[0] == 'a' assert received[1] == 'b'
def test_any_unprocessed_message(): app, debug, hu = make_kutana_no_run() pl = Plugin("") @pl.on_commands(["abc"]) async def __(msg, ctx): await ctx.reply(msg.text) @pl.on_unprocessed_messages() async def __(msg, ctx): await ctx.reply(msg.text * 2) app.add_plugin(pl) hu(Message(None, UpdateType.MSG, ".123", (), 1, 0, 0, 0, {})) hu(Message(None, UpdateType.MSG, ".abc", (), 1, 0, 0, 0, {})) assert hu(Update(None, UpdateType.UPD, {})) == hr.SKIPPED assert debug.answers[1][0] == (".123" * 2, (), {}) assert debug.answers[1][1] == (".abc", (), {})
def test_on_callback(): app, debug, hu = make_kutana_no_run(backend_source="vkontakte") pl = Plugin("") @pl.vk.on_callbacks([{"val": 1}]) async def __(upd, ctx): await ctx.send_message_event_answer({ "type": "show_snackbar", "text": "hey hey hey", }) @pl.vk.on_callbacks(['val']) async def __(upd, ctx): await ctx.send_message_event_answer({ "type": "show_snackbar", "text": "val val val", }) app.add_plugin(pl) hu(Update(MESSAGES["inline_callback_1"], UpdateType.UPD, {})) hu(Update(MESSAGES["inline_callback_2"], UpdateType.UPD, {})) hu(Update(MESSAGES["inline_callback_val"], UpdateType.UPD, {})) hu(Message(make_message_update({"val": 1}), UpdateType.MSG, "hey3", (), 1, 0, 0, 0, {})) assert debug.requests == [ ("messages.sendMessageEventAnswer", { 'event_data': '{"type": "show_snackbar", "text": "hey hey hey"}', 'event_id': '3159dc190b1g', 'user_id': 87641997, 'peer_id': 87641997, }), ("messages.sendMessageEventAnswer", { 'event_data': '{"type": "show_snackbar", "text": "val val val"}', 'event_id': '3159dc190b1g', 'user_id': 87641997, 'peer_id': 87641997, }), ]
def test_matches(): app, debug, hu = make_kutana_no_run() pl = Plugin("") @pl.on_match(r"\d.\d") async def _(msg, ctx): await ctx.reply(ctx.match.group(0)) @pl.on_match(r"(a|b|c).[a-z]") async def _(msg, ctx): await ctx.reply(ctx.match.group(0)) await ctx.reply(ctx.match.group(1)) app.add_plugin(pl) hu(Message(None, UpdateType.MSG, "123", (), 1, 0, 0, 0)) hu(Message(None, UpdateType.MSG, "abc", (), 1, 0, 0, 0)) assert hu(Update(None, UpdateType.UPD)) == hr.SKIPPED assert debug.answers[1][0] == ("123", (), {}) assert debug.answers[1][1] == ("abc", (), {}) assert debug.answers[1][2] == ("a", (), {})