feat(commands): improve discovery and normalize parameters

This commit is contained in:
tiennm99 committed 2026-07-21 11:45:20 +07:00
1 parent 3316eec74c
commit f5ba9d4032
31 files changed
+479 -56

No files matched your search

+41
View File
@@ -2,6 +2,7 @@ package modules
import (
"context"
"strings"
"testing"
"github.com/go-telegram/bot"
@@ -29,6 +30,46 @@ func TestValidateCommand_RejectsBadNames(t *testing.T) {
}
}
func TestValidateCommand_RejectsInvalidPresentationMetadata(t *testing.T) {
tests := []struct {
name string
command Command
}{
{
name: "blank description",
command: Command{Name: "ok", Visibility: VisibilityPublic, Description: " ", Handler: okHandler},
},
{
name: "multiline description",
command: Command{Name: "ok", Visibility: VisibilityPublic, Description: "one\ntwo", Handler: okHandler},
},
{
name: "multiline parameters",
command: Command{Name: "ok", Visibility: VisibilityPublic, Description: "d", Parameters: "<a>\n<b>", Handler: okHandler},
},
{
name: "multiline example",
command: Command{Name: "ok", Visibility: VisibilityPublic, Description: "d", Example: "/ok one\n/ok two", Handler: okHandler},
},
{
name: "example invokes another command",
command: Command{Name: "ok", Visibility: VisibilityPublic, Description: "d", Example: "/other", Handler: okHandler},
},
{
name: "native description too long",
command: Command{Name: "ok", Visibility: VisibilityPublic, Description: strings.Repeat("x", 250), Handler: okHandler},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
if err := validateCommand(tc.command); err == nil {
t.Fatal("expected validation error")
}
})
}
}
func TestValidateCommand_AcceptsLegalNames(t *testing.T) {
for _, name := range []string{"ping", "do_it", "a", "abc123", "x_1_y"} {
if err := validateCommand(Command{Name: name, Visibility: VisibilityPublic, Description: "d", Handler: okHandler}); err != nil {