-
Notifications
You must be signed in to change notification settings - Fork 4
Without explicit scope, set default scope only with default handler
#79
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
base: main
Are you sure you want to change the base?
Conversation
Change handler check to _hasHandler property
|
I would like to test what happens when someone tries to register a command without any handlers... Just because I don't remember exactly what it could compromise, but this little change makes a lot of sense tbh. something which test the following: commands
.command('broadcast', 'Send broadcast to all users')
await commands.setCommands(bot) |
|
@carafelix is that test cases correct ? commands
.command('...', '...') // Command without default handler and explicit scope
void {
scope: { type: 'default' },
commands: [{ command: '...', description: '...' }],
}commands
.command('...', '...', () => {}) // Command with default handler
void {
scope: { type: 'default' },
commands: [{ command: '...', description: '...' }],
}commands
.command('...', '...') // Command without default handler
.addToScope({ type: 'chat', chat_id: 0 }) // but with explicit scope
void {
scope: { type: 'chat', chat_id: 0 },
commands: [{ command: '...', description: '...' }],
}commands
.command('...', '...', () => {}) // Command with default handler
.addToScope({ type: 'chat', chat_id: 0 }) // and explicit scope
void {
scope: { type: 'default' },
commands: [{ command: '...', description: '...' }],
}
void {
scope: { type: 'chat', chat_id: 0 },
commands: [{ command: '...', description: '...' }],
}
|
default scope only for handlersdefault scope only with default handler
|
@PonomareVlad yup! That looks like the should-be-intended functionality :P Please add those test cases and fix the unrelated test cases that got affected by this and I'll merge. TO-DO reminder for myself: Refactor test cases which are too-heavily coupled with the inners output. Just check for the properties needed |
|
If we assume that the user of this plugin uses it to set explicit scopes, then previous
@carafelix so, in this case do we should to perform reset of commands
.command('...', '...') // Command without default handler
.addToScope({ type: 'chat', chat_id: 0 }) // but with explicit scope
void {
scope: { type: 'default' },
commands: [],
}
void {
scope: { type: 'chat', chat_id: 0 },
commands: [{ command: '...', description: '...' }],
} |
If, and only if, all the above test passes. As a backwards fix, I think it would be ideal. I think would benefit people who use to declare a default scoped command in any manner and later on decided to remove it from the default scope and only scope it. Iirc, if you have any other command in the default scope this would get reset anyways but for groups with only 1 command, it would not. Thanks for thinking about that too |
…avior Co-authored-by: PonomareVlad <2877584+PonomareVlad@users.noreply.github.com>
|
@carafelix I saw some test cases for commands/test/command-group.test.ts Line 211 in 6653e2a
commands/test/command-group.test.ts Line 302 in 6653e2a
commands/test/command-group.test.ts Line 328 in 6653e2a
|
Removed unused command tests and added new tests for command scope behavior.
|
@carafelix also, can you check current changes in PR before I will go next (is this proper test cases, placement and format) ? |
It's okay. Chat id is a required property in every update manage by the plugin. We pass it in the testing simply as a mock. |
carafelix
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice :3
The tests you added are all good ^-^ gj
| language_code: undefined, | ||
| commands: [ | ||
| { command: "test", description: "handler", hasHandler: true }, | ||
| { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test is render useless here. Deletion is just making it pass but defeat the porpoise of the test.
Instead could you test it in a specific scope but without handler? So it is clear that toSingleScopeArgs we still get commands without handlers but marked.
something like:
commands.command("test", "handler", (_) => _)
.addToScope({
type: "all_private_chats",
});
commands.command("markme", "nohandler")
.addToScope({
type: "all_private_chats",
});
// Test for it in scope: "all_private_chats"There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
commands/test/command-group.test.ts
Lines 144 to 147 in 485c8cf
| commands.command("test", "handler", (_) => _) | |
| .addToScope({ type: "all_private_chats" }); | |
| commands.command("markme", "nohandler") | |
| .addToScope({ type: "all_private_chats" }); |
|
|
||
| const mergedCommands = MyCommandParams.from([a, b], 10); | ||
| const expected = [ | ||
| { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here, instead of just deleting the default case, could you manually add a scope back in the command definition?
maybe:
.addToScope({
type: "all_private_chats",
})
.addToScope({
type: "all_chat_administrators",
});
// check each scope is merge toomaybe diverge command a from b and check each it correspond so they share like... all_chat_administrators and all_group_chats but diverge in all_private_chats
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
commands/test/command-group.test.ts
Lines 324 to 334 in 485c8cf
| const a = new CommandGroup(); | |
| a.command("a", "private chats") | |
| .addToScope({ type: "all_private_chats" }) | |
| .addToScope({ type: "all_chat_administrators" }) | |
| .localize("es", "a_es", "private localized"); | |
| const b = new CommandGroup(); | |
| b.command("b", "group chats") | |
| .addToScope({ type: "all_group_chats" }) | |
| .addToScope({ type: "all_chat_administrators" }) | |
| .localize("fr", "b_fr", "group localized"); |
This pull request refines command scope assignment logic in the
CommandGroupandCommandclasses and significantly expands the test suite to verify correct behavior for commands with and without handlers, as well as various scope configurations. The changes ensure that commands are properly assigned to default and explicit scopes based on their handler presence and scope declarations.Command scope assignment improvements
CommandGroupto automatically add commands with no explicit scopes to the default scope, ensuring all commands are properly scoped even if not explicitly assigned.Commandto use an internal_hasHandlerflag to determine if a handler exists before adding the command to the default scope, improving reliability of handler detection.Test suite enhancements
command-group.test.tsto cover new scope assignment behaviors, including cases for commands with/without handlers and with/without explicit scopes.