2023-04-27 21:58:56 +08:00
|
|
|
import re
|
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
from exceptiongroup import BaseExceptionGroup
|
2024-12-01 12:31:11 +08:00
|
|
|
from nonebug import App
|
|
|
|
import pytest
|
2021-12-16 23:22:25 +08:00
|
|
|
|
2024-12-01 12:31:11 +08:00
|
|
|
from nonebot.consts import (
|
|
|
|
CMD_ARG_KEY,
|
|
|
|
CMD_KEY,
|
|
|
|
CMD_START_KEY,
|
|
|
|
CMD_WHITESPACE_KEY,
|
|
|
|
ENDSWITH_KEY,
|
|
|
|
FULLMATCH_KEY,
|
|
|
|
KEYWORD_KEY,
|
|
|
|
PREFIX_KEY,
|
|
|
|
RAW_CMD_KEY,
|
|
|
|
REGEX_MATCHED,
|
|
|
|
SHELL_ARGS,
|
|
|
|
SHELL_ARGV,
|
|
|
|
STARTSWITH_KEY,
|
|
|
|
)
|
2023-05-21 16:01:55 +08:00
|
|
|
from nonebot.dependencies import Dependent
|
2023-02-22 23:32:48 +08:00
|
|
|
from nonebot.exception import TypeMisMatch
|
2024-12-01 12:31:11 +08:00
|
|
|
from nonebot.matcher import Matcher
|
2023-02-22 23:32:48 +08:00
|
|
|
from nonebot.params import (
|
|
|
|
ArgParam,
|
|
|
|
BotParam,
|
|
|
|
DefaultParam,
|
2024-12-01 12:31:11 +08:00
|
|
|
DependParam,
|
|
|
|
EventParam,
|
2023-02-22 23:32:48 +08:00
|
|
|
ExceptionParam,
|
2024-12-01 12:31:11 +08:00
|
|
|
MatcherParam,
|
|
|
|
StateParam,
|
2023-02-22 23:32:48 +08:00
|
|
|
)
|
2024-12-01 12:31:11 +08:00
|
|
|
from utils import FakeMessage, make_fake_event
|
2021-12-16 23:22:25 +08:00
|
|
|
|
2023-06-24 14:47:35 +08:00
|
|
|
UNKNOWN_PARAM = "Unknown parameter"
|
|
|
|
|
2021-12-16 23:22:25 +08:00
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
@pytest.mark.anyio
|
2023-02-22 23:32:48 +08:00
|
|
|
async def test_depend(app: App):
|
2021-12-20 14:50:11 +08:00
|
|
|
from plugins.param.param_depend import (
|
|
|
|
ClassDependency,
|
2024-12-01 12:31:11 +08:00
|
|
|
annotated_class_depend,
|
|
|
|
annotated_depend,
|
|
|
|
annotated_multi_depend,
|
|
|
|
annotated_prior_depend,
|
|
|
|
cache_exception_func1,
|
|
|
|
cache_exception_func2,
|
2021-12-20 14:50:11 +08:00
|
|
|
class_depend,
|
2024-12-01 12:31:11 +08:00
|
|
|
depends,
|
|
|
|
runned,
|
|
|
|
sub_type_mismatch,
|
2021-12-20 14:50:11 +08:00
|
|
|
test_depends,
|
2024-12-01 12:31:11 +08:00
|
|
|
validate,
|
2023-08-29 18:45:12 +08:00
|
|
|
validate_fail,
|
|
|
|
validate_field,
|
|
|
|
validate_field_fail,
|
2021-12-20 14:50:11 +08:00
|
|
|
)
|
2021-12-16 23:22:25 +08:00
|
|
|
|
2021-12-20 00:28:02 +08:00
|
|
|
async with app.test_dependent(depends, allow_types=[DependParam]) as ctx:
|
|
|
|
ctx.should_return(1)
|
2021-12-16 23:22:25 +08:00
|
|
|
|
2023-06-24 14:47:35 +08:00
|
|
|
assert len(runned) == 1
|
|
|
|
assert runned[0] == 1
|
2021-12-16 23:22:25 +08:00
|
|
|
|
|
|
|
runned.clear()
|
|
|
|
|
|
|
|
async with app.test_matcher(test_depends) as ctx:
|
|
|
|
bot = ctx.create_bot()
|
|
|
|
event_next = make_fake_event()()
|
|
|
|
ctx.receive_event(bot, event_next)
|
|
|
|
|
2023-08-29 18:45:12 +08:00
|
|
|
assert runned == [1, 1]
|
2021-12-20 00:28:02 +08:00
|
|
|
|
2023-02-22 23:32:48 +08:00
|
|
|
runned.clear()
|
|
|
|
|
2021-12-20 14:50:11 +08:00
|
|
|
async with app.test_dependent(class_depend, allow_types=[DependParam]) as ctx:
|
|
|
|
ctx.should_return(ClassDependency(x=1, y=2))
|
|
|
|
|
2023-03-22 20:54:46 +08:00
|
|
|
async with app.test_dependent(annotated_depend, allow_types=[DependParam]) as ctx:
|
|
|
|
ctx.should_return(1)
|
|
|
|
|
|
|
|
async with app.test_dependent(
|
|
|
|
annotated_prior_depend, allow_types=[DependParam]
|
|
|
|
) as ctx:
|
|
|
|
ctx.should_return(1)
|
2023-09-14 00:14:45 +08:00
|
|
|
|
|
|
|
async with app.test_dependent(
|
|
|
|
annotated_multi_depend, allow_types=[DependParam]
|
|
|
|
) as ctx:
|
|
|
|
ctx.should_return(1)
|
|
|
|
|
|
|
|
assert runned == [1, 1, 1]
|
2023-03-22 20:54:46 +08:00
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
runned.clear()
|
|
|
|
|
2023-03-22 20:54:46 +08:00
|
|
|
async with app.test_dependent(
|
|
|
|
annotated_class_depend, allow_types=[DependParam]
|
|
|
|
) as ctx:
|
|
|
|
ctx.should_return(ClassDependency(x=1, y=2))
|
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
with pytest.raises((TypeMisMatch, BaseExceptionGroup)) as exc_info: # noqa: PT012
|
2023-08-29 18:45:12 +08:00
|
|
|
async with app.test_dependent(
|
|
|
|
sub_type_mismatch, allow_types=[DependParam, BotParam]
|
|
|
|
) as ctx:
|
|
|
|
bot = ctx.create_bot()
|
|
|
|
ctx.pass_params(bot=bot)
|
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
if isinstance(exc_info.value, BaseExceptionGroup):
|
|
|
|
assert exc_info.group_contains(TypeMisMatch)
|
|
|
|
|
2023-08-29 18:45:12 +08:00
|
|
|
async with app.test_dependent(validate, allow_types=[DependParam]) as ctx:
|
|
|
|
ctx.should_return(1)
|
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
with pytest.raises((TypeMisMatch, BaseExceptionGroup)) as exc_info:
|
2023-08-29 18:45:12 +08:00
|
|
|
async with app.test_dependent(validate_fail, allow_types=[DependParam]) as ctx:
|
|
|
|
...
|
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
if isinstance(exc_info.value, BaseExceptionGroup):
|
|
|
|
assert exc_info.group_contains(TypeMisMatch)
|
|
|
|
|
2023-08-29 18:45:12 +08:00
|
|
|
async with app.test_dependent(validate_field, allow_types=[DependParam]) as ctx:
|
|
|
|
ctx.should_return(1)
|
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
with pytest.raises((TypeMisMatch, BaseExceptionGroup)) as exc_info:
|
2023-08-29 18:45:12 +08:00
|
|
|
async with app.test_dependent(
|
|
|
|
validate_field_fail, allow_types=[DependParam]
|
|
|
|
) as ctx:
|
|
|
|
...
|
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
if isinstance(exc_info.value, BaseExceptionGroup):
|
|
|
|
assert exc_info.group_contains(TypeMisMatch)
|
2021-12-20 00:28:02 +08:00
|
|
|
|
2024-10-29 14:22:41 +08:00
|
|
|
# test cache reuse when exception raised
|
|
|
|
dependency_cache = {}
|
|
|
|
with pytest.raises((TypeMisMatch, BaseExceptionGroup)) as exc_info:
|
|
|
|
async with app.test_dependent(
|
|
|
|
cache_exception_func1, allow_types=[DependParam]
|
|
|
|
) as ctx:
|
|
|
|
ctx.pass_params(dependency_cache=dependency_cache)
|
|
|
|
|
|
|
|
if isinstance(exc_info.value, BaseExceptionGroup):
|
|
|
|
assert exc_info.group_contains(TypeMisMatch)
|
|
|
|
|
|
|
|
# dependency solve tasks should be shielded even if one of them raises an exception
|
|
|
|
assert len(dependency_cache) == 2
|
|
|
|
|
|
|
|
async with app.test_dependent(
|
|
|
|
cache_exception_func2, allow_types=[DependParam]
|
|
|
|
) as ctx:
|
|
|
|
ctx.pass_params(dependency_cache=dependency_cache)
|
|
|
|
ctx.should_return(1)
|
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
|
|
|
|
@pytest.mark.anyio
|
2023-02-22 23:32:48 +08:00
|
|
|
async def test_bot(app: App):
|
2022-05-22 19:42:30 +08:00
|
|
|
from plugins.param.param_bot import (
|
|
|
|
FooBot,
|
2024-12-01 12:31:11 +08:00
|
|
|
generic_bot,
|
|
|
|
generic_bot_none,
|
2022-05-22 19:42:30 +08:00
|
|
|
get_bot,
|
2024-12-01 12:31:11 +08:00
|
|
|
legacy_bot,
|
2022-05-22 19:42:30 +08:00
|
|
|
not_bot,
|
2024-12-01 12:31:11 +08:00
|
|
|
not_legacy_bot,
|
|
|
|
postpone_bot,
|
2022-05-22 19:42:30 +08:00
|
|
|
sub_bot,
|
|
|
|
union_bot,
|
|
|
|
)
|
2021-12-20 00:28:02 +08:00
|
|
|
|
|
|
|
async with app.test_dependent(get_bot, allow_types=[BotParam]) as ctx:
|
|
|
|
bot = ctx.create_bot()
|
|
|
|
ctx.pass_params(bot=bot)
|
|
|
|
ctx.should_return(bot)
|
|
|
|
|
2024-05-09 15:08:49 +08:00
|
|
|
async with app.test_dependent(postpone_bot, allow_types=[BotParam]) as ctx:
|
|
|
|
bot = ctx.create_bot()
|
|
|
|
ctx.pass_params(bot=bot)
|
|
|
|
ctx.should_return(bot)
|
|
|
|
|
2022-05-22 19:42:30 +08:00
|
|
|
async with app.test_dependent(legacy_bot, allow_types=[BotParam]) as ctx:
|
|
|
|
bot = ctx.create_bot()
|
|
|
|
ctx.pass_params(bot=bot)
|
|
|
|
ctx.should_return(bot)
|
|
|
|
|
2023-06-24 14:47:35 +08:00
|
|
|
with pytest.raises(ValueError, match=UNKNOWN_PARAM):
|
|
|
|
app.test_dependent(not_legacy_bot, allow_types=[BotParam])
|
2022-05-22 19:42:30 +08:00
|
|
|
|
2022-03-20 19:40:43 +08:00
|
|
|
async with app.test_dependent(sub_bot, allow_types=[BotParam]) as ctx:
|
2022-05-22 19:42:30 +08:00
|
|
|
bot = ctx.create_bot(base=FooBot)
|
2022-03-20 19:40:43 +08:00
|
|
|
ctx.pass_params(bot=bot)
|
|
|
|
ctx.should_return(bot)
|
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
with pytest.raises((TypeMisMatch, BaseExceptionGroup)) as exc_info: # noqa: PT012
|
2022-03-20 19:40:43 +08:00
|
|
|
async with app.test_dependent(sub_bot, allow_types=[BotParam]) as ctx:
|
|
|
|
bot = ctx.create_bot()
|
|
|
|
ctx.pass_params(bot=bot)
|
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
if isinstance(exc_info.value, BaseExceptionGroup):
|
|
|
|
assert exc_info.group_contains(TypeMisMatch)
|
|
|
|
|
2022-05-22 19:42:30 +08:00
|
|
|
async with app.test_dependent(union_bot, allow_types=[BotParam]) as ctx:
|
|
|
|
bot = ctx.create_bot(base=FooBot)
|
|
|
|
ctx.pass_params(bot=bot)
|
|
|
|
ctx.should_return(bot)
|
|
|
|
|
2023-06-11 15:33:33 +08:00
|
|
|
async with app.test_dependent(generic_bot, allow_types=[BotParam]) as ctx:
|
|
|
|
bot = ctx.create_bot()
|
|
|
|
ctx.pass_params(bot=bot)
|
|
|
|
ctx.should_return(bot)
|
|
|
|
|
|
|
|
async with app.test_dependent(generic_bot_none, allow_types=[BotParam]) as ctx:
|
|
|
|
bot = ctx.create_bot()
|
|
|
|
ctx.pass_params(bot=bot)
|
|
|
|
ctx.should_return(bot)
|
|
|
|
|
2023-06-24 14:47:35 +08:00
|
|
|
with pytest.raises(ValueError, match=UNKNOWN_PARAM):
|
|
|
|
app.test_dependent(not_bot, allow_types=[BotParam])
|
2022-05-22 19:42:30 +08:00
|
|
|
|
2021-12-20 00:28:02 +08:00
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
@pytest.mark.anyio
|
2023-02-22 23:32:48 +08:00
|
|
|
async def test_event(app: App):
|
2021-12-20 00:28:02 +08:00
|
|
|
from plugins.param.param_event import (
|
2022-05-22 19:42:30 +08:00
|
|
|
FooEvent,
|
2021-12-20 00:28:02 +08:00
|
|
|
event,
|
|
|
|
event_message,
|
|
|
|
event_plain_text,
|
2024-12-01 12:31:11 +08:00
|
|
|
event_to_me,
|
|
|
|
event_type,
|
|
|
|
generic_event,
|
2023-06-11 15:33:33 +08:00
|
|
|
generic_event_none,
|
2024-12-01 12:31:11 +08:00
|
|
|
legacy_event,
|
|
|
|
not_event,
|
|
|
|
not_legacy_event,
|
|
|
|
postpone_event,
|
|
|
|
sub_event,
|
|
|
|
union_event,
|
2021-12-20 00:28:02 +08:00
|
|
|
)
|
|
|
|
|
2023-06-19 17:48:59 +08:00
|
|
|
fake_message = FakeMessage("text")
|
2021-12-20 00:28:02 +08:00
|
|
|
fake_event = make_fake_event(_message=fake_message)()
|
2022-05-22 19:42:30 +08:00
|
|
|
fake_fooevent = make_fake_event(_base=FooEvent)()
|
2021-12-20 00:28:02 +08:00
|
|
|
|
|
|
|
async with app.test_dependent(event, allow_types=[EventParam]) as ctx:
|
|
|
|
ctx.pass_params(event=fake_event)
|
|
|
|
ctx.should_return(fake_event)
|
|
|
|
|
2024-05-09 15:08:49 +08:00
|
|
|
async with app.test_dependent(postpone_event, allow_types=[EventParam]) as ctx:
|
|
|
|
ctx.pass_params(event=fake_event)
|
|
|
|
ctx.should_return(fake_event)
|
|
|
|
|
2022-05-22 19:42:30 +08:00
|
|
|
async with app.test_dependent(legacy_event, allow_types=[EventParam]) as ctx:
|
|
|
|
ctx.pass_params(event=fake_event)
|
|
|
|
ctx.should_return(fake_event)
|
|
|
|
|
2023-06-24 14:47:35 +08:00
|
|
|
with pytest.raises(ValueError, match=UNKNOWN_PARAM):
|
|
|
|
app.test_dependent(not_legacy_event, allow_types=[EventParam])
|
2022-05-22 19:42:30 +08:00
|
|
|
|
2022-03-20 19:40:43 +08:00
|
|
|
async with app.test_dependent(sub_event, allow_types=[EventParam]) as ctx:
|
2022-05-22 19:42:30 +08:00
|
|
|
ctx.pass_params(event=fake_fooevent)
|
|
|
|
ctx.should_return(fake_fooevent)
|
2022-03-20 19:40:43 +08:00
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
with pytest.raises((TypeMisMatch, BaseExceptionGroup)) as exc_info:
|
2022-03-20 19:40:43 +08:00
|
|
|
async with app.test_dependent(sub_event, allow_types=[EventParam]) as ctx:
|
|
|
|
ctx.pass_params(event=fake_event)
|
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
if isinstance(exc_info.value, BaseExceptionGroup):
|
|
|
|
assert exc_info.group_contains(TypeMisMatch)
|
|
|
|
|
2022-05-22 19:42:30 +08:00
|
|
|
async with app.test_dependent(union_event, allow_types=[EventParam]) as ctx:
|
|
|
|
ctx.pass_params(event=fake_fooevent)
|
2024-01-26 11:12:57 +08:00
|
|
|
ctx.should_return(fake_fooevent)
|
2022-05-22 19:42:30 +08:00
|
|
|
|
2023-06-11 15:33:33 +08:00
|
|
|
async with app.test_dependent(generic_event, allow_types=[EventParam]) as ctx:
|
|
|
|
ctx.pass_params(event=fake_event)
|
|
|
|
ctx.should_return(fake_event)
|
|
|
|
|
|
|
|
async with app.test_dependent(generic_event_none, allow_types=[EventParam]) as ctx:
|
|
|
|
ctx.pass_params(event=fake_event)
|
|
|
|
ctx.should_return(fake_event)
|
|
|
|
|
2023-06-24 14:47:35 +08:00
|
|
|
with pytest.raises(ValueError, match=UNKNOWN_PARAM):
|
|
|
|
app.test_dependent(not_event, allow_types=[EventParam])
|
2022-05-22 19:42:30 +08:00
|
|
|
|
2021-12-20 00:28:02 +08:00
|
|
|
async with app.test_dependent(
|
|
|
|
event_type, allow_types=[EventParam, DependParam]
|
|
|
|
) as ctx:
|
|
|
|
ctx.pass_params(event=fake_event)
|
|
|
|
ctx.should_return(fake_event.get_type())
|
|
|
|
|
|
|
|
async with app.test_dependent(
|
|
|
|
event_message, allow_types=[EventParam, DependParam]
|
|
|
|
) as ctx:
|
|
|
|
ctx.pass_params(event=fake_event)
|
|
|
|
ctx.should_return(fake_event.get_message())
|
|
|
|
|
|
|
|
async with app.test_dependent(
|
|
|
|
event_plain_text, allow_types=[EventParam, DependParam]
|
|
|
|
) as ctx:
|
|
|
|
ctx.pass_params(event=fake_event)
|
|
|
|
ctx.should_return(fake_event.get_plaintext())
|
|
|
|
|
|
|
|
async with app.test_dependent(
|
|
|
|
event_to_me, allow_types=[EventParam, DependParam]
|
|
|
|
) as ctx:
|
|
|
|
ctx.pass_params(event=fake_event)
|
|
|
|
ctx.should_return(fake_event.is_tome())
|
|
|
|
|
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
@pytest.mark.anyio
|
2023-02-22 23:32:48 +08:00
|
|
|
async def test_state(app: App):
|
2021-12-20 00:28:02 +08:00
|
|
|
from plugins.param.param_state import (
|
|
|
|
command,
|
2024-12-01 12:31:11 +08:00
|
|
|
command_arg,
|
|
|
|
command_start,
|
|
|
|
command_whitespace,
|
2022-10-12 13:41:28 +08:00
|
|
|
endswith,
|
|
|
|
fullmatch,
|
2024-12-01 12:31:11 +08:00
|
|
|
keyword,
|
|
|
|
legacy_state,
|
|
|
|
not_legacy_state,
|
|
|
|
postpone_state,
|
2021-12-20 00:28:02 +08:00
|
|
|
raw_command,
|
2024-12-01 12:31:11 +08:00
|
|
|
regex_dict,
|
2021-12-20 00:28:02 +08:00
|
|
|
regex_group,
|
|
|
|
regex_matched,
|
2024-12-01 12:31:11 +08:00
|
|
|
regex_str,
|
2021-12-20 00:28:02 +08:00
|
|
|
shell_command_args,
|
|
|
|
shell_command_argv,
|
2024-12-01 12:31:11 +08:00
|
|
|
startswith,
|
|
|
|
state,
|
2021-12-20 00:28:02 +08:00
|
|
|
)
|
|
|
|
|
2023-06-19 17:48:59 +08:00
|
|
|
fake_message = FakeMessage("text")
|
2023-04-27 21:58:56 +08:00
|
|
|
fake_matched = re.match(r"\[cq:(?P<type>.*?),(?P<arg>.*?)\]", "[cq:test,arg=value]")
|
2021-12-20 00:28:02 +08:00
|
|
|
fake_state = {
|
2022-04-20 14:43:29 +08:00
|
|
|
PREFIX_KEY: {
|
|
|
|
CMD_KEY: ("cmd",),
|
|
|
|
RAW_CMD_KEY: "/cmd",
|
|
|
|
CMD_START_KEY: "/",
|
|
|
|
CMD_ARG_KEY: fake_message,
|
2023-02-27 00:11:24 +08:00
|
|
|
CMD_WHITESPACE_KEY: " ",
|
2022-04-20 14:43:29 +08:00
|
|
|
},
|
2021-12-20 00:28:02 +08:00
|
|
|
SHELL_ARGV: ["-h"],
|
|
|
|
SHELL_ARGS: {"help": True},
|
2023-04-27 21:58:56 +08:00
|
|
|
REGEX_MATCHED: fake_matched,
|
2022-10-12 13:41:28 +08:00
|
|
|
STARTSWITH_KEY: "startswith",
|
|
|
|
ENDSWITH_KEY: "endswith",
|
|
|
|
FULLMATCH_KEY: "fullmatch",
|
|
|
|
KEYWORD_KEY: "keyword",
|
2021-12-20 00:28:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async with app.test_dependent(state, allow_types=[StateParam]) as ctx:
|
|
|
|
ctx.pass_params(state=fake_state)
|
|
|
|
ctx.should_return(fake_state)
|
|
|
|
|
2024-05-09 15:08:49 +08:00
|
|
|
async with app.test_dependent(postpone_state, allow_types=[StateParam]) as ctx:
|
|
|
|
ctx.pass_params(state=fake_state)
|
|
|
|
ctx.should_return(fake_state)
|
|
|
|
|
2022-05-22 19:42:30 +08:00
|
|
|
async with app.test_dependent(legacy_state, allow_types=[StateParam]) as ctx:
|
|
|
|
ctx.pass_params(state=fake_state)
|
|
|
|
ctx.should_return(fake_state)
|
|
|
|
|
2023-06-24 14:47:35 +08:00
|
|
|
with pytest.raises(ValueError, match=UNKNOWN_PARAM):
|
|
|
|
app.test_dependent(not_legacy_state, allow_types=[StateParam])
|
2022-05-22 19:42:30 +08:00
|
|
|
|
2021-12-20 00:28:02 +08:00
|
|
|
async with app.test_dependent(
|
|
|
|
command, allow_types=[StateParam, DependParam]
|
|
|
|
) as ctx:
|
|
|
|
ctx.pass_params(state=fake_state)
|
|
|
|
ctx.should_return(fake_state[PREFIX_KEY][CMD_KEY])
|
|
|
|
|
|
|
|
async with app.test_dependent(
|
|
|
|
raw_command, allow_types=[StateParam, DependParam]
|
|
|
|
) as ctx:
|
|
|
|
ctx.pass_params(state=fake_state)
|
|
|
|
ctx.should_return(fake_state[PREFIX_KEY][RAW_CMD_KEY])
|
|
|
|
|
|
|
|
async with app.test_dependent(
|
|
|
|
command_arg, allow_types=[StateParam, DependParam]
|
|
|
|
) as ctx:
|
|
|
|
ctx.pass_params(state=fake_state)
|
|
|
|
ctx.should_return(fake_state[PREFIX_KEY][CMD_ARG_KEY])
|
|
|
|
|
2022-04-20 14:43:29 +08:00
|
|
|
async with app.test_dependent(
|
|
|
|
command_start, allow_types=[StateParam, DependParam]
|
|
|
|
) as ctx:
|
|
|
|
ctx.pass_params(state=fake_state)
|
|
|
|
ctx.should_return(fake_state[PREFIX_KEY][CMD_START_KEY])
|
|
|
|
|
2021-12-20 00:28:02 +08:00
|
|
|
async with app.test_dependent(
|
2023-02-27 00:11:24 +08:00
|
|
|
command_whitespace, allow_types=[StateParam, DependParam]
|
|
|
|
) as ctx:
|
|
|
|
ctx.pass_params(state=fake_state)
|
|
|
|
ctx.should_return(fake_state[PREFIX_KEY][CMD_WHITESPACE_KEY])
|
|
|
|
|
|
|
|
async with app.test_dependent(
|
2021-12-20 00:28:02 +08:00
|
|
|
shell_command_argv, allow_types=[StateParam, DependParam]
|
|
|
|
) as ctx:
|
|
|
|
ctx.pass_params(state=fake_state)
|
|
|
|
ctx.should_return(fake_state[SHELL_ARGV])
|
|
|
|
|
|
|
|
async with app.test_dependent(
|
|
|
|
shell_command_args, allow_types=[StateParam, DependParam]
|
|
|
|
) as ctx:
|
|
|
|
ctx.pass_params(state=fake_state)
|
|
|
|
ctx.should_return(fake_state[SHELL_ARGS])
|
|
|
|
|
|
|
|
async with app.test_dependent(
|
|
|
|
regex_matched, allow_types=[StateParam, DependParam]
|
|
|
|
) as ctx:
|
|
|
|
ctx.pass_params(state=fake_state)
|
|
|
|
ctx.should_return(fake_state[REGEX_MATCHED])
|
|
|
|
|
2022-12-09 14:42:54 +08:00
|
|
|
async with app.test_dependent(
|
|
|
|
regex_str, allow_types=[StateParam, DependParam]
|
|
|
|
) as ctx:
|
|
|
|
ctx.pass_params(state=fake_state)
|
2024-01-11 11:52:07 +08:00
|
|
|
ctx.should_return(
|
|
|
|
("[cq:test,arg=value]", "test", "arg=value", ("test", "arg=value"))
|
|
|
|
)
|
2022-12-09 14:42:54 +08:00
|
|
|
|
2021-12-20 00:28:02 +08:00
|
|
|
async with app.test_dependent(
|
|
|
|
regex_group, allow_types=[StateParam, DependParam]
|
|
|
|
) as ctx:
|
|
|
|
ctx.pass_params(state=fake_state)
|
2023-04-27 21:58:56 +08:00
|
|
|
ctx.should_return(("test", "arg=value"))
|
2021-12-20 00:28:02 +08:00
|
|
|
|
|
|
|
async with app.test_dependent(
|
|
|
|
regex_dict, allow_types=[StateParam, DependParam]
|
|
|
|
) as ctx:
|
|
|
|
ctx.pass_params(state=fake_state)
|
2023-04-27 21:58:56 +08:00
|
|
|
ctx.should_return({"type": "test", "arg": "arg=value"})
|
2021-12-20 00:28:02 +08:00
|
|
|
|
2022-10-12 13:41:28 +08:00
|
|
|
async with app.test_dependent(
|
|
|
|
startswith, allow_types=[StateParam, DependParam]
|
|
|
|
) as ctx:
|
|
|
|
ctx.pass_params(state=fake_state)
|
|
|
|
ctx.should_return(fake_state[STARTSWITH_KEY])
|
|
|
|
|
|
|
|
async with app.test_dependent(
|
|
|
|
endswith, allow_types=[StateParam, DependParam]
|
|
|
|
) as ctx:
|
|
|
|
ctx.pass_params(state=fake_state)
|
|
|
|
ctx.should_return(fake_state[ENDSWITH_KEY])
|
|
|
|
|
|
|
|
async with app.test_dependent(
|
|
|
|
fullmatch, allow_types=[StateParam, DependParam]
|
|
|
|
) as ctx:
|
|
|
|
ctx.pass_params(state=fake_state)
|
|
|
|
ctx.should_return(fake_state[FULLMATCH_KEY])
|
|
|
|
|
|
|
|
async with app.test_dependent(
|
|
|
|
keyword, allow_types=[StateParam, DependParam]
|
|
|
|
) as ctx:
|
|
|
|
ctx.pass_params(state=fake_state)
|
|
|
|
ctx.should_return(fake_state[KEYWORD_KEY])
|
|
|
|
|
2021-12-20 00:28:02 +08:00
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
@pytest.mark.anyio
|
2023-02-22 23:32:48 +08:00
|
|
|
async def test_matcher(app: App):
|
2023-06-11 15:33:33 +08:00
|
|
|
from plugins.param.param_matcher import (
|
|
|
|
FooMatcher,
|
2024-12-01 12:31:11 +08:00
|
|
|
generic_matcher,
|
|
|
|
generic_matcher_none,
|
|
|
|
last_receive,
|
|
|
|
legacy_matcher,
|
2023-06-11 15:33:33 +08:00
|
|
|
matcher,
|
2024-12-01 12:31:11 +08:00
|
|
|
not_legacy_matcher,
|
2023-06-11 15:33:33 +08:00
|
|
|
not_matcher,
|
2024-12-01 12:31:11 +08:00
|
|
|
postpone_matcher,
|
|
|
|
receive,
|
2023-06-11 15:33:33 +08:00
|
|
|
sub_matcher,
|
|
|
|
union_matcher,
|
|
|
|
)
|
2021-12-20 00:28:02 +08:00
|
|
|
|
|
|
|
fake_matcher = Matcher()
|
2023-06-11 15:33:33 +08:00
|
|
|
foo_matcher = FooMatcher()
|
2021-12-20 00:28:02 +08:00
|
|
|
|
|
|
|
async with app.test_dependent(matcher, allow_types=[MatcherParam]) as ctx:
|
|
|
|
ctx.pass_params(matcher=fake_matcher)
|
|
|
|
ctx.should_return(fake_matcher)
|
|
|
|
|
2024-05-09 15:08:49 +08:00
|
|
|
async with app.test_dependent(postpone_matcher, allow_types=[MatcherParam]) as ctx:
|
|
|
|
ctx.pass_params(matcher=fake_matcher)
|
|
|
|
ctx.should_return(fake_matcher)
|
|
|
|
|
2023-06-11 15:33:33 +08:00
|
|
|
async with app.test_dependent(legacy_matcher, allow_types=[MatcherParam]) as ctx:
|
|
|
|
ctx.pass_params(matcher=fake_matcher)
|
|
|
|
ctx.should_return(fake_matcher)
|
|
|
|
|
2023-06-24 14:47:35 +08:00
|
|
|
with pytest.raises(ValueError, match=UNKNOWN_PARAM):
|
|
|
|
app.test_dependent(not_legacy_matcher, allow_types=[MatcherParam])
|
2023-06-11 15:33:33 +08:00
|
|
|
|
|
|
|
async with app.test_dependent(sub_matcher, allow_types=[MatcherParam]) as ctx:
|
|
|
|
ctx.pass_params(matcher=foo_matcher)
|
|
|
|
ctx.should_return(foo_matcher)
|
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
with pytest.raises((TypeMisMatch, BaseExceptionGroup)) as exc_info:
|
2023-06-11 15:33:33 +08:00
|
|
|
async with app.test_dependent(sub_matcher, allow_types=[MatcherParam]) as ctx:
|
|
|
|
ctx.pass_params(matcher=fake_matcher)
|
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
if isinstance(exc_info.value, BaseExceptionGroup):
|
|
|
|
assert exc_info.group_contains(TypeMisMatch)
|
|
|
|
|
2023-06-11 15:33:33 +08:00
|
|
|
async with app.test_dependent(union_matcher, allow_types=[MatcherParam]) as ctx:
|
|
|
|
ctx.pass_params(matcher=foo_matcher)
|
|
|
|
ctx.should_return(foo_matcher)
|
|
|
|
|
|
|
|
async with app.test_dependent(generic_matcher, allow_types=[MatcherParam]) as ctx:
|
|
|
|
ctx.pass_params(matcher=fake_matcher)
|
|
|
|
ctx.should_return(fake_matcher)
|
|
|
|
|
|
|
|
async with app.test_dependent(
|
|
|
|
generic_matcher_none, allow_types=[MatcherParam]
|
|
|
|
) as ctx:
|
|
|
|
ctx.pass_params(matcher=fake_matcher)
|
|
|
|
ctx.should_return(fake_matcher)
|
|
|
|
|
2023-06-24 14:47:35 +08:00
|
|
|
with pytest.raises(ValueError, match=UNKNOWN_PARAM):
|
|
|
|
app.test_dependent(not_matcher, allow_types=[MatcherParam])
|
2023-06-11 15:33:33 +08:00
|
|
|
|
2021-12-20 00:28:02 +08:00
|
|
|
event = make_fake_event()()
|
|
|
|
fake_matcher.set_receive("test", event)
|
|
|
|
event_next = make_fake_event()()
|
2021-12-21 00:39:12 +08:00
|
|
|
fake_matcher.set_receive("", event_next)
|
2021-12-20 00:28:02 +08:00
|
|
|
|
|
|
|
async with app.test_dependent(
|
|
|
|
receive, allow_types=[MatcherParam, DependParam]
|
|
|
|
) as ctx:
|
|
|
|
ctx.pass_params(matcher=fake_matcher)
|
|
|
|
ctx.should_return(event)
|
|
|
|
|
|
|
|
async with app.test_dependent(
|
|
|
|
last_receive, allow_types=[MatcherParam, DependParam]
|
|
|
|
) as ctx:
|
|
|
|
ctx.pass_params(matcher=fake_matcher)
|
|
|
|
ctx.should_return(event_next)
|
2021-12-20 14:31:48 +08:00
|
|
|
|
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
@pytest.mark.anyio
|
2023-02-22 23:32:48 +08:00
|
|
|
async def test_arg(app: App):
|
2023-06-24 19:18:24 +08:00
|
|
|
from plugins.param.param_arg import (
|
|
|
|
annotated_arg,
|
2024-12-01 12:31:11 +08:00
|
|
|
annotated_arg_plain_text,
|
2023-06-24 19:18:24 +08:00
|
|
|
annotated_arg_str,
|
2023-09-14 00:14:45 +08:00
|
|
|
annotated_multi_arg,
|
|
|
|
annotated_prior_arg,
|
2024-12-01 12:31:11 +08:00
|
|
|
arg,
|
|
|
|
arg_plain_text,
|
|
|
|
arg_str,
|
2023-06-24 19:18:24 +08:00
|
|
|
)
|
2021-12-20 14:31:48 +08:00
|
|
|
|
|
|
|
matcher = Matcher()
|
2023-06-19 17:48:59 +08:00
|
|
|
message = FakeMessage("text")
|
2021-12-23 22:16:55 +08:00
|
|
|
matcher.set_arg("key", message)
|
2021-12-20 14:31:48 +08:00
|
|
|
|
|
|
|
async with app.test_dependent(arg, allow_types=[ArgParam]) as ctx:
|
|
|
|
ctx.pass_params(matcher=matcher)
|
|
|
|
ctx.should_return(message)
|
|
|
|
|
|
|
|
async with app.test_dependent(arg_str, allow_types=[ArgParam]) as ctx:
|
|
|
|
ctx.pass_params(matcher=matcher)
|
|
|
|
ctx.should_return(str(message))
|
|
|
|
|
2021-12-23 22:16:55 +08:00
|
|
|
async with app.test_dependent(arg_plain_text, allow_types=[ArgParam]) as ctx:
|
2021-12-20 14:31:48 +08:00
|
|
|
ctx.pass_params(matcher=matcher)
|
2021-12-23 22:16:55 +08:00
|
|
|
ctx.should_return(message.extract_plain_text())
|
2021-12-20 14:31:48 +08:00
|
|
|
|
2023-06-24 19:18:24 +08:00
|
|
|
async with app.test_dependent(annotated_arg, allow_types=[ArgParam]) as ctx:
|
|
|
|
ctx.pass_params(matcher=matcher)
|
|
|
|
ctx.should_return(message)
|
|
|
|
|
|
|
|
async with app.test_dependent(annotated_arg_str, allow_types=[ArgParam]) as ctx:
|
|
|
|
ctx.pass_params(matcher=matcher)
|
|
|
|
ctx.should_return(str(message))
|
|
|
|
|
|
|
|
async with app.test_dependent(
|
|
|
|
annotated_arg_plain_text, allow_types=[ArgParam]
|
|
|
|
) as ctx:
|
|
|
|
ctx.pass_params(matcher=matcher)
|
|
|
|
ctx.should_return(message.extract_plain_text())
|
|
|
|
|
2023-09-14 00:14:45 +08:00
|
|
|
async with app.test_dependent(annotated_multi_arg, allow_types=[ArgParam]) as ctx:
|
|
|
|
ctx.pass_params(matcher=matcher)
|
|
|
|
ctx.should_return(message.extract_plain_text())
|
|
|
|
|
|
|
|
async with app.test_dependent(annotated_prior_arg, allow_types=[ArgParam]) as ctx:
|
|
|
|
ctx.pass_params(matcher=matcher)
|
|
|
|
ctx.should_return(message.extract_plain_text())
|
|
|
|
|
2021-12-20 14:31:48 +08:00
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
@pytest.mark.anyio
|
2023-02-22 23:32:48 +08:00
|
|
|
async def test_exception(app: App):
|
2024-01-26 11:12:57 +08:00
|
|
|
from plugins.param.param_exception import exc, legacy_exc
|
2021-12-20 14:31:48 +08:00
|
|
|
|
|
|
|
exception = ValueError("test")
|
|
|
|
async with app.test_dependent(exc, allow_types=[ExceptionParam]) as ctx:
|
|
|
|
ctx.pass_params(exception=exception)
|
|
|
|
ctx.should_return(exception)
|
|
|
|
|
2024-01-26 11:12:57 +08:00
|
|
|
async with app.test_dependent(legacy_exc, allow_types=[ExceptionParam]) as ctx:
|
|
|
|
ctx.pass_params(exception=exception)
|
|
|
|
ctx.should_return(exception)
|
|
|
|
|
2021-12-20 14:31:48 +08:00
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
@pytest.mark.anyio
|
2023-02-22 23:32:48 +08:00
|
|
|
async def test_default(app: App):
|
2021-12-20 14:31:48 +08:00
|
|
|
from plugins.param.param_default import default
|
|
|
|
|
|
|
|
async with app.test_dependent(default, allow_types=[DefaultParam]) as ctx:
|
|
|
|
ctx.should_return(1)
|
2023-05-21 16:01:55 +08:00
|
|
|
|
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
def test_priority():
|
2023-05-21 16:01:55 +08:00
|
|
|
from plugins.param.priority import complex_priority
|
|
|
|
|
2024-08-06 14:19:17 +08:00
|
|
|
dependent = Dependent[None].parse(
|
2023-05-21 16:01:55 +08:00
|
|
|
call=complex_priority,
|
|
|
|
allow_types=[
|
|
|
|
DependParam,
|
|
|
|
BotParam,
|
|
|
|
EventParam,
|
|
|
|
StateParam,
|
|
|
|
MatcherParam,
|
|
|
|
ArgParam,
|
|
|
|
ExceptionParam,
|
|
|
|
DefaultParam,
|
|
|
|
],
|
|
|
|
)
|
|
|
|
for param in dependent.params:
|
|
|
|
if param.name == "sub":
|
|
|
|
assert isinstance(param.field_info, DependParam)
|
|
|
|
elif param.name == "bot":
|
|
|
|
assert isinstance(param.field_info, BotParam)
|
|
|
|
elif param.name == "event":
|
|
|
|
assert isinstance(param.field_info, EventParam)
|
|
|
|
elif param.name == "state":
|
|
|
|
assert isinstance(param.field_info, StateParam)
|
|
|
|
elif param.name == "matcher":
|
|
|
|
assert isinstance(param.field_info, MatcherParam)
|
|
|
|
elif param.name == "arg":
|
|
|
|
assert isinstance(param.field_info, ArgParam)
|
|
|
|
elif param.name == "exception":
|
|
|
|
assert isinstance(param.field_info, ExceptionParam)
|
|
|
|
elif param.name == "default":
|
|
|
|
assert isinstance(param.field_info, DefaultParam)
|
|
|
|
else:
|
|
|
|
raise ValueError(f"unknown param {param.name}")
|