refactor(commands): remove unused dispatcher abstraction

This commit is contained in:
mingmxren 2026-03-01 17:52:06 +08:00
parent 9c610bc037
commit 03032b15f4
2 changed files with 0 additions and 122 deletions

View file

@ -16,53 +16,6 @@ type Request struct {
Reply func(text string) error
}
type Result struct {
Matched bool
Handled bool
Command string
Err error
}
type Dispatcher struct {
reg *Registry
}
type Dispatching interface {
Dispatch(ctx context.Context, req Request) Result
}
type DispatchFunc func(ctx context.Context, req Request) Result
func (f DispatchFunc) Dispatch(ctx context.Context, req Request) Result {
return f(ctx, req)
}
func NewDispatcher(reg *Registry) *Dispatcher {
return &Dispatcher{reg: reg}
}
func (d *Dispatcher) Dispatch(ctx context.Context, req Request) Result {
cmdName, ok := parseCommandName(req.Text)
if !ok {
return Result{Matched: false}
}
for _, def := range d.reg.ForChannel(req.Channel) {
if def.Name != cmdName && !contains(def.Aliases, cmdName) {
continue
}
if def.Handler == nil {
// Definition-only command (for menu registration / discovery).
// Let the inbound message continue to the agent loop.
return Result{Matched: false, Handled: false, Command: def.Name}
}
err := def.Handler(ctx, req)
return Result{Matched: true, Handled: true, Command: def.Name, Err: err}
}
return Result{Matched: false}
}
func firstToken(input string) string {
parts := strings.Fields(strings.TrimSpace(input))
if len(parts) == 0 {

View file

@ -1,75 +0,0 @@
package commands
import (
"context"
"testing"
)
func TestDispatcher_MatchSlashCommand(t *testing.T) {
called := false
defs := []Definition{
{
Name: "help",
Handler: func(context.Context, Request) error {
called = true
return nil
},
},
}
d := NewDispatcher(NewRegistry(defs))
res := d.Dispatch(context.Background(), Request{
Channel: "telegram",
Text: "/help",
})
if !res.Matched || !called || res.Err != nil {
t.Fatalf("dispatch result = %+v, called=%v", res, called)
}
}
func TestDispatcher_DoesNotMatchWithoutSlash(t *testing.T) {
d := NewDispatcher(NewRegistry([]Definition{{Name: "help"}}))
res := d.Dispatch(context.Background(), Request{
Channel: "telegram",
Text: "help",
})
if res.Matched {
t.Fatalf("expected unmatched for plain text, got %+v", res)
}
}
func TestDispatcher_MatchTelegramMentionSyntax(t *testing.T) {
called := false
d := NewDispatcher(NewRegistry([]Definition{
{
Name: "help",
Handler: func(context.Context, Request) error {
called = true
return nil
},
},
}))
res := d.Dispatch(context.Background(), Request{
Channel: "telegram",
Text: "/help@my_bot",
})
if !res.Matched || !res.Handled || !called || res.Err != nil {
t.Fatalf("dispatch result = %+v, called=%v", res, called)
}
}
func TestDispatcher_PassThroughDefinitionWithoutHandler(t *testing.T) {
d := NewDispatcher(NewRegistry([]Definition{
{Name: "session"}, // menu-only / pass-through definition
}))
res := d.Dispatch(context.Background(), Request{
Channel: "telegram",
Text: "/session list",
})
if res.Matched {
t.Fatalf("expected pass-through unmatched result, got %+v", res)
}
}