diff --git a/cmd/alcove/main.go b/cmd/alcove/main.go index a8da2a43..07be395b 100644 --- a/cmd/alcove/main.go +++ b/cmd/alcove/main.go @@ -77,6 +77,7 @@ func main() { Use: "alcove", Short: "Alcove — sandboxed AI coding agents", Long: "Alcove CLI for dispatching and managing AI coding sessions via the Bridge API.", + Version: Version, SilenceUsage: true, SilenceErrors: true, } diff --git a/cmd/alcove/main_test.go b/cmd/alcove/main_test.go index ed19086f..30712792 100644 --- a/cmd/alcove/main_test.go +++ b/cmd/alcove/main_test.go @@ -561,4 +561,67 @@ func TestSaveConfigCreatesDirectory(t *testing.T) { if loaded.Server != "https://test.example.com" { t.Errorf("server: got %q, want %q", loaded.Server, "https://test.example.com") } +} + +func TestVersionFlag(t *testing.T) { + // Create the root command to test version flag + root := &cobra.Command{ + Use: "alcove", + Version: "test-version", + SilenceUsage: true, + SilenceErrors: true, + } + + // Test --version flag + root.SetArgs([]string{"--version"}) + output := captureOutput(t, func() { + if err := root.Execute(); err != nil { + t.Fatalf("command execution failed: %v", err) + } + }) + + expected := "alcove version test-version" + if output != expected { + t.Errorf("--version output: got %q, want %q", output, expected) + } + + // Test -v flag (short version) + root.SetArgs([]string{"-v"}) + output = captureOutput(t, func() { + if err := root.Execute(); err != nil { + t.Fatalf("command execution failed: %v", err) + } + }) + + if output != expected { + t.Errorf("-v output: got %q, want %q", output, expected) + } +} + +func captureOutput(t *testing.T, fn func()) string { + t.Helper() + + // Create a pipe to capture output + r, w, err := os.Pipe() + if err != nil { + t.Fatalf("failed to create pipe: %v", err) + } + + // Save original stdout + oldStdout := os.Stdout + os.Stdout = w + + // Run the function + fn() + + // Restore stdout + os.Stdout = oldStdout + w.Close() + + // Read captured output + output := make([]byte, 1024) + n, _ := r.Read(output) + r.Close() + + return string(output[:n]) } \ No newline at end of file