2023-09-09 13:46:09 +08:00
|
|
|
from pathlib import Path
|
2024-12-01 12:31:11 +08:00
|
|
|
import sys
|
2023-09-09 13:46:09 +08:00
|
|
|
|
2021-12-21 00:39:12 +08:00
|
|
|
from nonebug import App
|
2024-12-01 12:31:11 +08:00
|
|
|
import pytest
|
2021-12-21 00:39:12 +08:00
|
|
|
|
2023-09-09 13:46:09 +08:00
|
|
|
from nonebot import get_plugin
|
2023-02-22 23:32:48 +08:00
|
|
|
from nonebot.matcher import Matcher, matchers
|
2023-09-12 15:13:35 +08:00
|
|
|
from nonebot.message import _check_matcher, check_and_run_matcher
|
2024-12-01 12:31:11 +08:00
|
|
|
from nonebot.permission import Permission, User
|
|
|
|
from nonebot.rule import Rule
|
|
|
|
from utils import FakeMessage, make_fake_event
|
2021-12-21 00:39:12 +08:00
|
|
|
|
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
def test_matcher_info(app: App):
|
2023-09-09 13:46:09 +08:00
|
|
|
from plugins.matcher.matcher_info import matcher
|
|
|
|
|
|
|
|
assert issubclass(matcher, Matcher)
|
|
|
|
assert matcher.type == "message"
|
|
|
|
assert matcher.priority == 1
|
|
|
|
assert matcher.temp is False
|
|
|
|
assert matcher.expire_time is None
|
|
|
|
assert matcher.block is True
|
|
|
|
|
|
|
|
assert matcher._source
|
|
|
|
|
|
|
|
assert matcher._source.module_name == "plugins.matcher.matcher_info"
|
|
|
|
assert matcher.module is sys.modules["plugins.matcher.matcher_info"]
|
|
|
|
assert matcher.module_name == "plugins.matcher.matcher_info"
|
|
|
|
|
2024-04-20 14:47:12 +08:00
|
|
|
assert matcher._source.plugin_id == "matcher:matcher_info"
|
2023-09-09 13:46:09 +08:00
|
|
|
assert matcher._source.plugin_name == "matcher_info"
|
2024-04-20 14:47:12 +08:00
|
|
|
assert matcher.plugin is get_plugin("matcher:matcher_info")
|
|
|
|
assert matcher.plugin_id == "matcher:matcher_info"
|
2023-09-09 13:46:09 +08:00
|
|
|
assert matcher.plugin_name == "matcher_info"
|
|
|
|
|
|
|
|
assert (
|
|
|
|
matcher._source.file
|
|
|
|
== (Path(__file__).parent.parent / "plugins/matcher/matcher_info.py").absolute()
|
|
|
|
)
|
|
|
|
|
|
|
|
assert matcher._source.lineno == 3
|
|
|
|
|
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
@pytest.mark.anyio
|
2023-09-12 15:13:35 +08:00
|
|
|
async def test_matcher_check(app: App):
|
|
|
|
async def falsy():
|
|
|
|
return False
|
|
|
|
|
|
|
|
async def truthy():
|
|
|
|
return True
|
|
|
|
|
|
|
|
async def error():
|
|
|
|
raise RuntimeError
|
|
|
|
|
|
|
|
event = make_fake_event(_type="test")()
|
|
|
|
with app.provider.context({}):
|
|
|
|
test_perm_falsy = Matcher.new(permission=Permission(falsy))
|
|
|
|
async with app.test_api() as ctx:
|
|
|
|
bot = ctx.create_bot()
|
|
|
|
assert await _check_matcher(test_perm_falsy, bot, event, {}) is False
|
|
|
|
|
|
|
|
test_perm_truthy = Matcher.new(permission=Permission(truthy))
|
|
|
|
async with app.test_api() as ctx:
|
|
|
|
bot = ctx.create_bot()
|
|
|
|
assert await _check_matcher(test_perm_truthy, bot, event, {}) is True
|
|
|
|
|
|
|
|
test_perm_error = Matcher.new(permission=Permission(error))
|
|
|
|
async with app.test_api() as ctx:
|
|
|
|
bot = ctx.create_bot()
|
|
|
|
assert await _check_matcher(test_perm_error, bot, event, {}) is False
|
|
|
|
|
|
|
|
test_rule_falsy = Matcher.new(rule=Rule(falsy))
|
|
|
|
async with app.test_api() as ctx:
|
|
|
|
bot = ctx.create_bot()
|
|
|
|
assert await _check_matcher(test_rule_falsy, bot, event, {}) is False
|
|
|
|
|
|
|
|
test_rule_truthy = Matcher.new(rule=Rule(truthy))
|
|
|
|
async with app.test_api() as ctx:
|
|
|
|
bot = ctx.create_bot()
|
|
|
|
assert await _check_matcher(test_rule_truthy, bot, event, {}) is True
|
|
|
|
|
|
|
|
test_rule_error = Matcher.new(rule=Rule(error))
|
|
|
|
async with app.test_api() as ctx:
|
|
|
|
bot = ctx.create_bot()
|
|
|
|
assert await _check_matcher(test_rule_error, bot, event, {}) is False
|
|
|
|
|
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
@pytest.mark.anyio
|
2023-06-19 17:48:59 +08:00
|
|
|
async def test_matcher_handle(app: App):
|
|
|
|
from plugins.matcher.matcher_process import test_handle
|
2021-12-21 00:39:12 +08:00
|
|
|
|
2023-06-19 17:48:59 +08:00
|
|
|
message = FakeMessage("text")
|
2021-12-21 00:39:12 +08:00
|
|
|
event = make_fake_event(_message=message)()
|
|
|
|
|
|
|
|
assert len(test_handle.handlers) == 1
|
|
|
|
async with app.test_matcher(test_handle) as ctx:
|
|
|
|
bot = ctx.create_bot()
|
|
|
|
ctx.receive_event(bot, event)
|
|
|
|
ctx.should_call_send(event, "send", "result", at_sender=True)
|
|
|
|
ctx.should_finished()
|
|
|
|
|
2023-06-19 17:48:59 +08:00
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
@pytest.mark.anyio
|
2023-06-19 17:48:59 +08:00
|
|
|
async def test_matcher_got(app: App):
|
|
|
|
from plugins.matcher.matcher_process import test_got
|
|
|
|
|
|
|
|
message = FakeMessage("text")
|
|
|
|
event = make_fake_event(_message=message)()
|
|
|
|
message_next = FakeMessage("text_next")
|
|
|
|
event_next = make_fake_event(_message=message_next)()
|
|
|
|
|
2021-12-21 00:39:12 +08:00
|
|
|
assert len(test_got.handlers) == 1
|
|
|
|
async with app.test_matcher(test_got) as ctx:
|
|
|
|
bot = ctx.create_bot()
|
|
|
|
ctx.receive_event(bot, event)
|
|
|
|
ctx.should_call_send(event, "prompt key1", "result1")
|
|
|
|
ctx.receive_event(bot, event)
|
|
|
|
ctx.should_call_send(event, "prompt key2", "result2")
|
|
|
|
ctx.receive_event(bot, event)
|
|
|
|
ctx.should_call_send(event, "reject", "result3", at_sender=True)
|
|
|
|
ctx.should_rejected()
|
2021-12-21 10:57:47 +08:00
|
|
|
ctx.receive_event(bot, event_next)
|
2021-12-21 00:39:12 +08:00
|
|
|
|
2023-06-19 17:48:59 +08:00
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
@pytest.mark.anyio
|
2023-06-19 17:48:59 +08:00
|
|
|
async def test_matcher_receive(app: App):
|
|
|
|
from plugins.matcher.matcher_process import test_receive
|
|
|
|
|
|
|
|
message = FakeMessage("text")
|
|
|
|
event = make_fake_event(_message=message)()
|
|
|
|
|
2021-12-21 00:39:12 +08:00
|
|
|
assert len(test_receive.handlers) == 1
|
|
|
|
async with app.test_matcher(test_receive) as ctx:
|
|
|
|
bot = ctx.create_bot()
|
|
|
|
ctx.receive_event(bot, event)
|
|
|
|
ctx.receive_event(bot, event)
|
|
|
|
ctx.receive_event(bot, event)
|
|
|
|
ctx.should_call_send(event, "pause", "result", at_sender=True)
|
|
|
|
ctx.should_paused()
|
2021-12-21 10:57:47 +08:00
|
|
|
|
2023-06-19 17:48:59 +08:00
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
@pytest.mark.anyio
|
2023-09-12 15:13:35 +08:00
|
|
|
async def test_matcher_combine(app: App):
|
2023-06-19 17:48:59 +08:00
|
|
|
from plugins.matcher.matcher_process import test_combine
|
|
|
|
|
|
|
|
message = FakeMessage("text")
|
|
|
|
event = make_fake_event(_message=message)()
|
|
|
|
message_next = FakeMessage("text_next")
|
|
|
|
event_next = make_fake_event(_message=message_next)()
|
|
|
|
|
|
|
|
assert len(test_combine.handlers) == 1
|
2021-12-21 10:57:47 +08:00
|
|
|
async with app.test_matcher(test_combine) as ctx:
|
|
|
|
bot = ctx.create_bot()
|
|
|
|
ctx.receive_event(bot, event)
|
|
|
|
ctx.receive_event(bot, event)
|
|
|
|
ctx.receive_event(bot, event)
|
|
|
|
ctx.should_rejected()
|
|
|
|
ctx.receive_event(bot, event_next)
|
|
|
|
ctx.should_rejected()
|
|
|
|
ctx.receive_event(bot, event_next)
|
|
|
|
ctx.should_rejected()
|
|
|
|
ctx.receive_event(bot, event_next)
|
2021-12-24 14:09:43 +08:00
|
|
|
|
2023-06-19 17:48:59 +08:00
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
@pytest.mark.anyio
|
2023-06-19 17:48:59 +08:00
|
|
|
async def test_matcher_preset(app: App):
|
|
|
|
from plugins.matcher.matcher_process import test_preset
|
|
|
|
|
|
|
|
message = FakeMessage("text")
|
|
|
|
event = make_fake_event(_message=message)()
|
|
|
|
message_next = FakeMessage("text_next")
|
|
|
|
event_next = make_fake_event(_message=message_next)()
|
|
|
|
|
2021-12-24 14:09:43 +08:00
|
|
|
assert len(test_preset.handlers) == 2
|
|
|
|
async with app.test_matcher(test_preset) as ctx:
|
|
|
|
bot = ctx.create_bot()
|
|
|
|
ctx.receive_event(bot, event)
|
2021-12-31 22:43:29 +08:00
|
|
|
ctx.receive_event(bot, event)
|
2021-12-24 14:09:43 +08:00
|
|
|
ctx.should_rejected()
|
|
|
|
ctx.receive_event(bot, event_next)
|
2021-12-31 22:43:29 +08:00
|
|
|
|
2023-06-19 17:48:59 +08:00
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
@pytest.mark.anyio
|
2023-06-19 17:48:59 +08:00
|
|
|
async def test_matcher_overload(app: App):
|
|
|
|
from plugins.matcher.matcher_process import test_overload
|
|
|
|
|
|
|
|
message = FakeMessage("text")
|
|
|
|
event = make_fake_event(_message=message)()
|
|
|
|
|
2021-12-31 22:43:29 +08:00
|
|
|
assert len(test_overload.handlers) == 2
|
|
|
|
async with app.test_matcher(test_overload) as ctx:
|
|
|
|
bot = ctx.create_bot()
|
|
|
|
ctx.receive_event(bot, event)
|
|
|
|
ctx.should_finished()
|
2022-01-04 15:19:59 +08:00
|
|
|
|
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
@pytest.mark.anyio
|
2023-03-29 12:22:50 +08:00
|
|
|
async def test_matcher_destroy(app: App):
|
|
|
|
from plugins.matcher.matcher_process import test_destroy
|
|
|
|
|
2023-06-24 14:47:35 +08:00
|
|
|
async with app.test_matcher(test_destroy):
|
2023-03-29 12:22:50 +08:00
|
|
|
assert len(matchers) == 1
|
|
|
|
assert len(matchers[test_destroy.priority]) == 1
|
|
|
|
assert matchers[test_destroy.priority][0] is test_destroy
|
|
|
|
|
|
|
|
test_destroy.destroy()
|
|
|
|
|
|
|
|
assert len(matchers[test_destroy.priority]) == 0
|
|
|
|
|
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
@pytest.mark.anyio
|
2023-02-22 23:32:48 +08:00
|
|
|
async def test_type_updater(app: App):
|
2024-12-01 12:31:11 +08:00
|
|
|
from plugins.matcher.matcher_type import test_custom_updater, test_type_updater
|
2022-01-04 15:19:59 +08:00
|
|
|
|
|
|
|
event = make_fake_event()()
|
|
|
|
|
|
|
|
assert test_type_updater.type == "test"
|
|
|
|
async with app.test_api() as ctx:
|
|
|
|
bot = ctx.create_bot()
|
|
|
|
matcher = test_type_updater()
|
|
|
|
new_type = await matcher.update_type(bot, event)
|
|
|
|
assert new_type == "message"
|
|
|
|
|
|
|
|
assert test_custom_updater.type == "test"
|
|
|
|
async with app.test_api() as ctx:
|
|
|
|
bot = ctx.create_bot()
|
|
|
|
matcher = test_custom_updater()
|
|
|
|
new_type = await matcher.update_type(bot, event)
|
|
|
|
assert new_type == "custom"
|
|
|
|
|
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
@pytest.mark.anyio
|
2023-06-19 17:48:59 +08:00
|
|
|
async def test_default_permission_updater(app: App):
|
2022-01-04 15:19:59 +08:00
|
|
|
from plugins.matcher.matcher_permission import (
|
|
|
|
default_permission,
|
|
|
|
test_permission_updater,
|
|
|
|
)
|
|
|
|
|
|
|
|
event = make_fake_event(_session_id="test")()
|
|
|
|
|
|
|
|
assert test_permission_updater.permission is default_permission
|
|
|
|
async with app.test_api() as ctx:
|
|
|
|
bot = ctx.create_bot()
|
|
|
|
matcher = test_permission_updater()
|
|
|
|
new_perm = await matcher.update_permission(bot, event)
|
|
|
|
assert len(new_perm.checkers) == 1
|
2024-03-07 14:57:26 +08:00
|
|
|
checker = next(iter(new_perm.checkers)).call
|
2022-01-04 15:19:59 +08:00
|
|
|
assert isinstance(checker, User)
|
|
|
|
assert checker.users == ("test",)
|
|
|
|
assert checker.perm is default_permission
|
|
|
|
|
2023-06-19 17:48:59 +08:00
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
@pytest.mark.anyio
|
2023-06-19 17:48:59 +08:00
|
|
|
async def test_user_permission_updater(app: App):
|
|
|
|
from plugins.matcher.matcher_permission import (
|
|
|
|
default_permission,
|
|
|
|
test_user_permission_updater,
|
|
|
|
)
|
|
|
|
|
|
|
|
event = make_fake_event(_session_id="test")()
|
2024-03-07 14:57:26 +08:00
|
|
|
user_permission = next(iter(test_user_permission_updater.permission.checkers)).call
|
2022-09-01 10:41:43 +08:00
|
|
|
assert isinstance(user_permission, User)
|
|
|
|
assert user_permission.perm is default_permission
|
|
|
|
async with app.test_api() as ctx:
|
|
|
|
bot = ctx.create_bot()
|
|
|
|
matcher = test_user_permission_updater()
|
|
|
|
new_perm = await matcher.update_permission(bot, event)
|
|
|
|
assert len(new_perm.checkers) == 1
|
2024-03-07 14:57:26 +08:00
|
|
|
checker = next(iter(new_perm.checkers)).call
|
2022-09-01 10:41:43 +08:00
|
|
|
assert isinstance(checker, User)
|
|
|
|
assert checker.users == ("test",)
|
|
|
|
assert checker.perm is default_permission
|
|
|
|
|
2023-06-19 17:48:59 +08:00
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
@pytest.mark.anyio
|
2023-06-19 17:48:59 +08:00
|
|
|
async def test_custom_permission_updater(app: App):
|
|
|
|
from plugins.matcher.matcher_permission import (
|
|
|
|
default_permission,
|
2024-12-01 12:31:11 +08:00
|
|
|
new_permission,
|
2023-06-19 17:48:59 +08:00
|
|
|
test_custom_updater,
|
|
|
|
)
|
|
|
|
|
|
|
|
event = make_fake_event(_session_id="test")()
|
2022-01-04 15:19:59 +08:00
|
|
|
assert test_custom_updater.permission is default_permission
|
|
|
|
async with app.test_api() as ctx:
|
|
|
|
bot = ctx.create_bot()
|
|
|
|
matcher = test_custom_updater()
|
|
|
|
new_perm = await matcher.update_permission(bot, event)
|
2023-06-19 17:48:59 +08:00
|
|
|
assert new_perm is new_permission
|
2022-01-05 18:29:11 +08:00
|
|
|
|
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
@pytest.mark.anyio
|
2022-01-05 18:29:11 +08:00
|
|
|
async def test_run(app: App):
|
2023-02-22 23:32:48 +08:00
|
|
|
with app.provider.context({}):
|
|
|
|
assert not matchers
|
|
|
|
event = make_fake_event()()
|
2022-01-05 18:29:11 +08:00
|
|
|
|
2023-02-22 23:32:48 +08:00
|
|
|
async def reject():
|
|
|
|
await Matcher.reject()
|
2022-01-05 18:29:11 +08:00
|
|
|
|
2023-02-22 23:32:48 +08:00
|
|
|
test_reject = Matcher.new(handlers=[reject])
|
2022-01-05 18:29:11 +08:00
|
|
|
|
2023-02-22 23:32:48 +08:00
|
|
|
async with app.test_api() as ctx:
|
|
|
|
bot = ctx.create_bot()
|
|
|
|
await test_reject().run(bot, event, {})
|
|
|
|
assert len(matchers[0]) == 1
|
|
|
|
assert len(matchers[0][0].handlers) == 1
|
2022-01-05 18:29:11 +08:00
|
|
|
|
2023-02-22 23:32:48 +08:00
|
|
|
del matchers[0]
|
2022-01-05 18:29:11 +08:00
|
|
|
|
2023-02-22 23:32:48 +08:00
|
|
|
async def pause():
|
|
|
|
await Matcher.pause()
|
2022-01-05 18:29:11 +08:00
|
|
|
|
2023-02-22 23:32:48 +08:00
|
|
|
test_pause = Matcher.new(handlers=[pause])
|
2022-01-05 18:29:11 +08:00
|
|
|
|
2023-02-22 23:32:48 +08:00
|
|
|
async with app.test_api() as ctx:
|
|
|
|
bot = ctx.create_bot()
|
|
|
|
await test_pause().run(bot, event, {})
|
|
|
|
assert len(matchers[0]) == 1
|
|
|
|
assert len(matchers[0][0].handlers) == 0
|
2022-07-20 10:21:31 +08:00
|
|
|
|
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
@pytest.mark.anyio
|
2023-06-19 17:48:59 +08:00
|
|
|
async def test_temp(app: App):
|
|
|
|
from plugins.matcher.matcher_expire import test_temp_matcher
|
2022-07-20 10:21:31 +08:00
|
|
|
|
|
|
|
event = make_fake_event(_type="test")()
|
2024-10-26 15:36:01 +08:00
|
|
|
with app.provider.context({test_temp_matcher.priority: [test_temp_matcher]}):
|
|
|
|
async with app.test_api() as ctx:
|
|
|
|
bot = ctx.create_bot()
|
|
|
|
assert test_temp_matcher in matchers[test_temp_matcher.priority]
|
|
|
|
await check_and_run_matcher(test_temp_matcher, bot, event, {})
|
|
|
|
assert test_temp_matcher not in matchers[test_temp_matcher.priority]
|
2022-07-20 10:21:31 +08:00
|
|
|
|
2023-06-19 17:48:59 +08:00
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
@pytest.mark.anyio
|
2023-06-19 17:48:59 +08:00
|
|
|
async def test_datetime_expire(app: App):
|
|
|
|
from plugins.matcher.matcher_expire import test_datetime_matcher
|
|
|
|
|
2022-07-20 10:21:31 +08:00
|
|
|
event = make_fake_event()()
|
2024-10-26 15:36:01 +08:00
|
|
|
with app.provider.context(
|
|
|
|
{test_datetime_matcher.priority: [test_datetime_matcher]}
|
|
|
|
):
|
|
|
|
async with app.test_matcher(test_datetime_matcher) as ctx:
|
|
|
|
bot = ctx.create_bot()
|
|
|
|
assert test_datetime_matcher in matchers[test_datetime_matcher.priority]
|
|
|
|
await check_and_run_matcher(test_datetime_matcher, bot, event, {})
|
|
|
|
assert test_datetime_matcher not in matchers[test_datetime_matcher.priority]
|
2022-07-20 10:21:31 +08:00
|
|
|
|
2023-06-19 17:48:59 +08:00
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
@pytest.mark.anyio
|
2023-06-19 17:48:59 +08:00
|
|
|
async def test_timedelta_expire(app: App):
|
|
|
|
from plugins.matcher.matcher_expire import test_timedelta_matcher
|
|
|
|
|
2022-07-20 10:21:31 +08:00
|
|
|
event = make_fake_event()()
|
2024-10-26 15:36:01 +08:00
|
|
|
with app.provider.context(
|
|
|
|
{test_timedelta_matcher.priority: [test_timedelta_matcher]}
|
|
|
|
):
|
|
|
|
async with app.test_api() as ctx:
|
|
|
|
bot = ctx.create_bot()
|
|
|
|
assert test_timedelta_matcher in matchers[test_timedelta_matcher.priority]
|
|
|
|
await check_and_run_matcher(test_timedelta_matcher, bot, event, {})
|
|
|
|
assert (
|
|
|
|
test_timedelta_matcher not in matchers[test_timedelta_matcher.priority]
|
|
|
|
)
|