Files
zero/internal/cli/exec_parse_prompt_test.go
euxaristia 5b4f48d2dc fix(cli): prevent consuming positional arguments as flag values (#619)
When parsing CLI flags, positional prompt text placed immediately after flags requiring values (e.g. --auto, --notify, --max-turns) would be greedily consumed as their value by nextFlagValue. Refine nextFlagValue to validate choices/types (autonomy levels, notify modes, integers) to detect invalid values early and fail validation rather than eating positional arguments.
2026-07-10 10:11:12 +08:00

41 lines
1.5 KiB
Go

package cli
import (
"strings"
"testing"
)
// TestParseExecArgsInlinePromptKeepsLeadingDash guards the M19 fix: the top-level
// `zero -p "<prompt>"` path forwards the prompt as the inline `--prompt=<value>`
// form precisely so a prompt whose first character is a dash is taken verbatim
// instead of being mistaken for a flag and rejected.
func TestParseExecArgsInlinePromptKeepsLeadingDash(t *testing.T) {
opts, early, err := parseExecArgs([]string{"--prompt=-foo bar"})
if err != nil {
t.Fatalf("inline dash-leading prompt rejected: %v", err)
}
if early {
t.Fatal("a normal prompt parse must not request early exit")
}
if got := strings.TrimSpace(strings.Join(opts.promptParts, " ")); got != "-foo bar" {
t.Fatalf("inline prompt = %q, want %q", got, "-foo bar")
}
}
func TestParseExecArgsPreventsPositionalFromBeingConsumedAsFlagValue(t *testing.T) {
_, _, err := parseExecArgs([]string{"--auto", "do a build"})
if err == nil || !strings.Contains(err.Error(), "Invalid autonomy level") {
t.Fatalf("expected error that --auto has invalid autonomy level, got: %v", err)
}
_, _, err = parseExecArgs([]string{"--notify", "do a build"})
if err == nil || !strings.Contains(err.Error(), "invalid --notify") {
t.Fatalf("expected error that --notify is invalid, got: %v", err)
}
_, _, err = parseExecArgs([]string{"--max-turns", "do a build"})
if err == nil || !strings.Contains(err.Error(), "invalid --max-turns") {
t.Fatalf("expected error that --max-turns is invalid, got: %v", err)
}
}