diff --git a/pkg/commands/builtin.go b/pkg/commands/builtin.go index 48d3d3ce1..a36dd3eba 100644 --- a/pkg/commands/builtin.go +++ b/pkg/commands/builtin.go @@ -11,5 +11,6 @@ func BuiltinDefinitions() []Definition { showCommand(), listCommand(), switchCommand(), + checkCommand(), } } diff --git a/pkg/commands/cmd_check.go b/pkg/commands/cmd_check.go new file mode 100644 index 000000000..f0193dc4f --- /dev/null +++ b/pkg/commands/cmd_check.go @@ -0,0 +1,33 @@ +package commands + +import ( + "context" + "fmt" +) + +func checkCommand() Definition { + return Definition{ + Name: "check", + Description: "Check channel availability", + SubCommands: []SubCommand{ + { + Name: "channel", + Description: "Check if a channel is available", + ArgsUsage: "", + Handler: func(_ context.Context, req Request, rt *Runtime) error { + if rt == nil || rt.SwitchChannel == nil { + return req.Reply(unavailableMsg) + } + value := nthToken(req.Text, 2) + if value == "" { + return req.Reply("Usage: /check channel ") + } + if err := rt.SwitchChannel(value); err != nil { + return req.Reply(err.Error()) + } + return req.Reply(fmt.Sprintf("Channel '%s' is available and enabled", value)) + }, + }, + }, + } +} diff --git a/pkg/commands/cmd_switch.go b/pkg/commands/cmd_switch.go index ca4057260..fb8fc109e 100644 --- a/pkg/commands/cmd_switch.go +++ b/pkg/commands/cmd_switch.go @@ -8,7 +8,7 @@ import ( func switchCommand() Definition { return Definition{ Name: "switch", - Description: "Switch model or channel", + Description: "Switch model", SubCommands: []SubCommand{ { Name: "model", @@ -32,20 +32,9 @@ func switchCommand() Definition { }, { Name: "channel", - Description: "Switch to a different channel", - ArgsUsage: "to ", - Handler: func(_ context.Context, req Request, rt *Runtime) error { - if rt == nil || rt.SwitchChannel == nil { - return req.Reply(unavailableMsg) - } - value := nthToken(req.Text, 3) - if nthToken(req.Text, 2) != "to" || value == "" { - return req.Reply("Usage: /switch channel to ") - } - if err := rt.SwitchChannel(value); err != nil { - return req.Reply(err.Error()) - } - return req.Reply(fmt.Sprintf("Channel '%s' is available and enabled", value)) + Description: "Moved to /check channel", + Handler: func(_ context.Context, req Request, _ *Runtime) error { + return req.Reply("This command has moved. Please use: /check channel ") }, }, }, diff --git a/pkg/commands/cmd_switch_test.go b/pkg/commands/cmd_switch_test.go index 419d1d82d..59ed305bb 100644 --- a/pkg/commands/cmd_switch_test.go +++ b/pkg/commands/cmd_switch_test.go @@ -122,7 +122,27 @@ func TestSwitchModel_NilDep(t *testing.T) { } } -func TestSwitchChannel_Success(t *testing.T) { +func TestSwitchChannel_Redirect(t *testing.T) { + ex := NewExecutor(NewRegistry(BuiltinDefinitions()), &Runtime{}) + + var reply string + res := ex.Execute(context.Background(), Request{ + Text: "/switch channel to telegram", + Reply: func(text string) error { + reply = text + return nil + }, + }) + if res.Outcome != OutcomeHandled { + t.Fatalf("outcome=%v, want=%v", res.Outcome, OutcomeHandled) + } + want := "This command has moved. Please use: /check channel " + if reply != want { + t.Fatalf("reply=%q, want=%q", reply, want) + } +} + +func TestCheckChannel_Success(t *testing.T) { rt := &Runtime{ SwitchChannel: func(value string) error { return nil @@ -132,7 +152,7 @@ func TestSwitchChannel_Success(t *testing.T) { var reply string res := ex.Execute(context.Background(), Request{ - Text: "/switch channel to telegram", + Text: "/check channel telegram", Reply: func(text string) error { reply = text return nil @@ -147,7 +167,7 @@ func TestSwitchChannel_Success(t *testing.T) { } } -func TestSwitchChannel_Error(t *testing.T) { +func TestCheckChannel_Error(t *testing.T) { rt := &Runtime{ SwitchChannel: func(value string) error { return fmt.Errorf("channel '%s' not found", value) @@ -157,7 +177,7 @@ func TestSwitchChannel_Error(t *testing.T) { var reply string res := ex.Execute(context.Background(), Request{ - Text: "/switch channel to unknown", + Text: "/check channel unknown", Reply: func(text string) error { reply = text return nil @@ -171,12 +191,12 @@ func TestSwitchChannel_Error(t *testing.T) { } } -func TestSwitchChannel_NilDep(t *testing.T) { +func TestCheckChannel_NilDep(t *testing.T) { ex := NewExecutor(NewRegistry(BuiltinDefinitions()), &Runtime{}) var reply string res := ex.Execute(context.Background(), Request{ - Text: "/switch channel to telegram", + Text: "/check channel telegram", Reply: func(text string) error { reply = text return nil @@ -190,6 +210,30 @@ func TestSwitchChannel_NilDep(t *testing.T) { } } +func TestCheckChannel_MissingValue(t *testing.T) { + rt := &Runtime{ + SwitchChannel: func(value string) error { + return nil + }, + } + ex := NewExecutor(NewRegistry(BuiltinDefinitions()), rt) + + var reply string + res := ex.Execute(context.Background(), Request{ + Text: "/check channel", + Reply: func(text string) error { + reply = text + return nil + }, + }) + if res.Outcome != OutcomeHandled { + t.Fatalf("outcome=%v, want=%v", res.Outcome, OutcomeHandled) + } + if reply != "Usage: /check channel " { + t.Fatalf("reply=%q, want usage message", reply) + } +} + func TestSwitch_BangPrefix(t *testing.T) { rt := &Runtime{ SwitchModel: func(value string) (string, error) {