refactor: add argument validation and improve tests per review

- Add validation to reject unknown arguments with helpful error message
- Add test cases to verify status argument routing
- Add test case to verify unknown arguments are rejected
- Addresses review feedback from nikolasdehor
This commit is contained in:
GhostC 2026-03-01 09:40:28 +08:00
parent 04f6f90071
commit 807d3612d0
2 changed files with 28 additions and 3 deletions

View file

@ -1,6 +1,8 @@
package gateway
import (
"fmt"
"github.com/sipeed/picoclaw/cmd/picoclaw/internal/status"
"github.com/spf13/cobra"
)
@ -15,10 +17,14 @@ func NewGatewayCommand() *cobra.Command {
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
// If "status" is provided as an argument, delegate to status command
if len(args) > 0 && args[0] == "status" {
if len(args) > 0 {
if args[0] == "status" {
status.StatusCmd()
return nil
}
// Reject unknown arguments
return fmt.Errorf("unknown argument: %s (did you mean 'picoclaw status'?)", args[0])
}
// Otherwise, start the gateway
return gatewayCmd(debug)
},

View file

@ -30,3 +30,22 @@ func TestNewGatewayCommand(t *testing.T) {
assert.True(t, cmd.HasFlags())
assert.NotNil(t, cmd.Flags().Lookup("debug"))
}
func TestGatewayCommandStatusArgument(t *testing.T) {
cmd := NewGatewayCommand()
// Test that "status" argument is accepted and routes correctly
err := cmd.RunE(cmd, []string{"status"})
// StatusCmd() doesn't return an error, so this should succeed
assert.NoError(t, err)
}
func TestGatewayCommandUnknownArgument(t *testing.T) {
cmd := NewGatewayCommand()
// Test that unknown arguments are rejected
err := cmd.RunE(cmd, []string{"foo"})
require.Error(t, err)
assert.Contains(t, err.Error(), "unknown argument: foo")
assert.Contains(t, err.Error(), "did you mean 'picoclaw status'?")
}