package main import ( "bytes" "regexp" "strings" "testing" ) func TestVersionCommandPrintsSemanticVersion(t *testing.T) { var output bytes.Buffer handled, err := handleVersionCommand([]string{"--version"}, &output) if err != nil { t.Fatal(err) } if !handled || output.String() != "diple "+dipleVersion+"\n" { t.Fatalf("version output = %q, handled=%t", output.String(), handled) } if !regexp.MustCompile(`^0\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$`).MatchString(dipleVersion) { t.Fatalf("version %q is not a pre-1.0 semantic version", dipleVersion) } } func TestVersionCommandRejectsAdditionalArguments(t *testing.T) { handled, err := handleVersionCommand( []string{"--version", "--help"}, &bytes.Buffer{}, ) if !handled || err == nil || !strings.Contains(err.Error(), "usage: diple --version") { t.Fatalf("handled=%t error=%v", handled, err) } } func TestCompletionCommandGeneratesSupportedShells(t *testing.T) { for _, shell := range completionShells { t.Run(shell, func(t *testing.T) { var output bytes.Buffer handled, err := handleCompletionCommand([]string{"completion", shell}, &output) if err != nil { t.Fatal(err) } if !handled || output.Len() == 0 || !strings.Contains(output.String(), "diple") { t.Fatalf("completion output = %q, handled=%t", output.String(), handled) } }) } } func TestZshCompletionRegistersWithoutCallingCompletionFunction(t *testing.T) { var output bytes.Buffer handled, err := handleCompletionCommand([]string{"completion", "zsh"}, &output) if err != nil { t.Fatal(err) } script := output.String() if !handled || !strings.Contains(script, "compdef _diple diple") { t.Fatalf("Zsh completion does not register _diple:\n%s", script) } if strings.Contains(script, `_diple "$@"`) { t.Fatalf("Zsh completion invokes _diple while being sourced:\n%s", script) } } func TestCompletionCommandRejectsUnknownShell(t *testing.T) { var output bytes.Buffer handled, err := handleCompletionCommand( []string{"completion", "powershell"}, &output, ) if !handled || err == nil || !strings.Contains(err.Error(), "unsupported shell") { t.Fatalf("handled=%t error=%v", handled, err) } } func TestCompletionCommandDoesNotClaimNormalInvocation(t *testing.T) { handled, err := handleCompletionCommand([]string{"--repo", "owner/repo"}, &bytes.Buffer{}) if handled || err != nil { t.Fatalf("handled=%t error=%v", handled, err) } } func TestCLIHelpIsGroupedAndActionable(t *testing.T) { var output bytes.Buffer writeCLIHelp(&output, defaultConfig(), "/tmp/diple/config.toml") help := output.String() for _, expected := range []string{ "Usage:", "Pull-request selection:", "GitHub and refresh:", "Appearance and navigation:", "Local state:", "diple completion ", "--version", "--repo OWNER/REPOSITORY", "--cache=false", "gh auth login", "/tmp/diple/config.toml", } { if !strings.Contains(help, expected) { t.Fatalf("help does not contain %q:\n%s", expected, help) } } }