Repository navigation
fix: apply command group custom filters to sub-commands #10646
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AraragiEro
wants to merge
1
commit into
AstrBotDevs:master
Choose a base branch
from
AraragiEro:fix/group-custom-filter-cascade
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| """Tests for the cascade of command-group custom filters to sub-commands.""" | ||
|
|
||
| from typing import Any | ||
|
|
||
| import pytest | ||
|
|
||
|
|
||
| class FakeEvent: | ||
| """Minimal AstrMessageEvent stand-in for filter unit tests.""" | ||
|
|
||
| def __init__(self, message_str: str) -> None: | ||
| self.message_str = message_str | ||
| self.is_at_or_wake_command = True | ||
| self._extras: dict[str, Any] = {} | ||
|
|
||
| def get_message_str(self) -> str: | ||
| return self.message_str | ||
|
|
||
| def get_extra(self, key: str | None = None, default: Any = None) -> Any: | ||
| if key is None: | ||
| return default | ||
| return self._extras.get(key, default) | ||
|
|
||
| def set_extra(self, key: str, value: Any) -> None: | ||
| self._extras[key] = value | ||
|
|
||
|
|
||
| def make_custom_filter(allowed: bool, calls: list[int] | None = None): | ||
| """Build a CustomFilter that returns ``allowed`` and counts invocations.""" | ||
| from astrbot.core.star.filter.custom_filter import CustomFilter | ||
|
|
||
| class _Filter(CustomFilter): | ||
| def filter(self, event, cfg) -> bool: | ||
| if calls is not None: | ||
| calls.append(1) | ||
| return allowed | ||
|
|
||
| return _Filter() | ||
|
|
||
|
|
||
| def make_group(name: str, parent_group=None): | ||
| from astrbot.core.star.filter.command_group import CommandGroupFilter | ||
|
|
||
| return CommandGroupFilter(name, parent_group=parent_group) | ||
|
|
||
|
|
||
| def make_sub_command(name: str, group): | ||
| from astrbot.core.star.filter.command import CommandFilter | ||
|
|
||
| sub_command = CommandFilter( | ||
| name, | ||
| parent_command_names=group.get_complete_command_names(), | ||
| parent_group=group, | ||
| ) | ||
| # Normally filled by init_handler_md() while decorating the handler. | ||
| sub_command.handler_params = {} | ||
| return sub_command | ||
|
|
||
|
|
||
| def test_group_filter_blocks_sub_command(): | ||
| group = make_group("math") | ||
| group.add_custom_filter(make_custom_filter(False)) | ||
| sub_command = make_sub_command("add", group) | ||
|
|
||
| assert sub_command.filter(FakeEvent("math add"), {}) is False | ||
|
|
||
|
|
||
| def test_group_filter_allows_sub_command(): | ||
| group = make_group("math") | ||
| group.add_custom_filter(make_custom_filter(True)) | ||
| sub_command = make_sub_command("add", group) | ||
|
|
||
| assert sub_command.filter(FakeEvent("math add"), {}) is True | ||
|
|
||
|
|
||
| def test_nested_group_filter_blocks_sub_command(): | ||
| outer = make_group("math") | ||
| outer.add_custom_filter(make_custom_filter(False)) | ||
| inner = make_group("calc", parent_group=outer) | ||
| inner.add_custom_filter(make_custom_filter(True)) | ||
| sub_command = make_sub_command("add", inner) | ||
|
|
||
| assert sub_command.filter(FakeEvent("math calc add"), {}) is False | ||
|
|
||
|
|
||
| def test_sub_command_filter_still_applies(): | ||
| group = make_group("math") | ||
| group.add_custom_filter(make_custom_filter(True)) | ||
| sub_command = make_sub_command("add", group) | ||
| sub_command.add_custom_filter(make_custom_filter(False)) | ||
|
|
||
| assert sub_command.filter(FakeEvent("math add"), {}) is False | ||
|
|
||
|
|
||
| def test_group_filter_is_evaluated_once_per_message(): | ||
| calls: list[int] = [] | ||
| group = make_group("math") | ||
| group.add_custom_filter(make_custom_filter(True, calls)) | ||
| sub_command = make_sub_command("add", group) | ||
| event = FakeEvent("math add") | ||
|
|
||
| assert group.filter(event, {}) is True | ||
| assert sub_command.filter(event, {}) is True | ||
| assert len(calls) == 1 | ||
|
|
||
|
|
||
| def test_group_filter_not_checked_for_unrelated_message(): | ||
| calls: list[int] = [] | ||
| group = make_group("math") | ||
| group.add_custom_filter(make_custom_filter(False, calls)) | ||
| sub_command = make_sub_command("add", group) | ||
|
|
||
| assert sub_command.filter(FakeEvent("other add"), {}) is False | ||
| assert calls == [] | ||
|
|
||
|
|
||
| def test_group_handler_skips_filters_for_unrelated_message(): | ||
| calls: list[int] = [] | ||
| group = make_group("math") | ||
| group.add_custom_filter(make_custom_filter(True, calls)) | ||
|
|
||
| assert group.filter(FakeEvent("weather info"), {}) is False | ||
| assert calls == [] | ||
|
|
||
|
|
||
| def test_group_handler_shows_help_for_exact_group_name(): | ||
| group = make_group("math") | ||
| group.add_custom_filter(make_custom_filter(True)) | ||
|
|
||
| with pytest.raises(ValueError, match="参数不足"): | ||
| group.filter(FakeEvent("math"), {}) | ||
|
|
||
|
|
||
| def test_group_handler_hides_help_when_filter_denies(): | ||
| group = make_group("math") | ||
| group.add_custom_filter(make_custom_filter(False)) | ||
|
|
||
| assert group.filter(FakeEvent("math"), {}) is False | ||
|
|
||
|
|
||
| def test_register_command_attaches_parent_group(): | ||
| from astrbot.core.star.filter.command_group import CommandGroupFilter | ||
| from astrbot.core.star.register.star_handler import ( | ||
| RegisteringCommandable, | ||
| register_command, | ||
| ) | ||
| from astrbot.core.star.star_handler import star_handlers_registry | ||
|
|
||
| group = CommandGroupFilter("_scope_test_math") | ||
|
|
||
| async def handler(event): | ||
| pass | ||
|
|
||
| md = None | ||
| try: | ||
| register_command(RegisteringCommandable(group), "_scope_test_add")(handler) | ||
| md = star_handlers_registry.get_handler_by_full_name( | ||
| f"{handler.__module__}_{handler.__name__}" | ||
| ) | ||
| assert md is not None | ||
| assert md.event_filters[0].parent_group is group | ||
| finally: | ||
| if md is not None: | ||
| star_handlers_registry.remove(md) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.