def test_exception_hooks(): called = [] app, _ = make_kutana([("123", 1), ("321", 1)]) pl = Plugin("") @pl.on_messages() async def __(msg, ctx): called.append("bruh") await ctx.reply("bruh") raise Exception("bruh") @pl.on_exception() async def __(update, ctx, exc): called.append("exception") assert exc == Exception assert str(exc) == "bruh" @pl.on_after() async def __(update, ctx, r): called.append("after") app.add_plugin(pl) app.run() assert called == ["bruh", "exception"] * 2
def test_before_and_after_hooks(): app, _ = make_kutana([("123", 1), ("321", 1)]) called = [] saved_ctxs = [] pl = Plugin("") @pl.on_before() async def __(update, ctx): called.append("before") saved_ctxs.append(ctx) @pl.on_messages() async def __(msg, ctx): await ctx.reply("ok") called.append("handle") @pl.on_after() async def __(update, ctx, r): called.append("after") saved_ctxs.append(ctx) app.add_plugin(pl) app.run() assert called == ["before", "handle", "after"] * 2 assert saved_ctxs[0] is saved_ctxs[1]
def test_handler_with_exception(): app, _ = make_kutana([("msg1", 1), ("msg2", 2)]) pl = Plugin("") @pl.on_match("msg1") async def _(message, ctx): await ctx.reply("ok") raise asyncio.CancelledError @pl.on_match("msg2") async def _(message, ctx): await ctx.reply("ok") raise Exception app.add_plugin(pl) app.run()
def test_routers_merge(): app, backend = make_kutana([("123", 1), ("321", 1)]) pl1 = Plugin("") @pl1.on_match("123") async def __(msg, ctx): await ctx.reply("ok1") app.add_plugin(pl1) pl2 = Plugin("") @pl2.on_match("321") async def __(msg, ctx): await ctx.reply("ok2") app.add_plugin(pl2) app.run() assert len(app._routers) == 1 assert backend.answers[1] == [("ok1", (), {}), ("ok2", (), {})]
def test_start_and_shutdown_hooks(): app, _ = make_kutana([("123", 1), ("321", 1)]) called = [] pl = Plugin("") @pl.on_start() async def __(app): called.append("start") @pl.on_messages() async def __(msg, ctx): await ctx.reply("ok") @pl.on_shutdown() async def __(app): called.append("shutdown") app.add_plugin(pl) app.run() assert called == ["start", "shutdown"]