gogcli/internal/cmd/root_test.go
salmonumbrella 3371e3f3ad
feat(cli): agent ergonomics + gmail watch exclude labels (#201)
* feat(cli): improve agent ergonomics

* fix(cli): address code review findings

- Fix nil pointer dereference in confirmDestructive when flags is nil
- Deduplicate dry-run logic by delegating to dryRunExit
- Remove deprecated net.Error.Temporary() call (dead since Go 1.18)
- Add unit tests for resolveTasklistID and resolveCalendarID

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: resolve PR #201 conflicts and follow-ups (#201) (thanks @salmonumbrella)

* fix: resolve rebase fallout for PR #201 landing (#201) (thanks @salmonumbrella)

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Co-authored-by: Peter Steinberger <steipete@gmail.com>
2026-02-14 03:09:49 +01:00

105 lines
2.9 KiB
Go

package cmd
import (
"errors"
"strings"
"testing"
)
func TestEnvOr(t *testing.T) {
t.Setenv("X_TEST", "")
if got := envOr("X_TEST", "fallback"); got != "fallback" {
t.Fatalf("unexpected: %q", got)
}
t.Setenv("X_TEST", "value")
if got := envOr("X_TEST", "fallback"); got != "value" {
t.Fatalf("unexpected: %q", got)
}
}
func TestExecute_Help(t *testing.T) {
out := captureStdout(t, func() {
_ = captureStderr(t, func() {
if err := Execute([]string{"--help"}); err != nil {
t.Fatalf("Execute: %v", err)
}
})
})
if !strings.Contains(out, "Google CLI") && !strings.Contains(out, "Usage:") {
t.Fatalf("unexpected help output: %q", out)
}
if !strings.Contains(out, "config.json") || !strings.Contains(out, "keyring backend") {
t.Fatalf("expected config info in help output: %q", out)
}
if strings.Contains(out, "gmail (mail,email) thread get") {
t.Fatalf("expected collapsed help (no expanded subcommands), got: %q", out)
}
}
func TestExecute_Help_GmailHasGroupsAndRelativeCommands(t *testing.T) {
out := captureStdout(t, func() {
_ = captureStderr(t, func() {
if err := Execute([]string{"gmail", "--help"}); err != nil {
t.Fatalf("Execute: %v", err)
}
})
})
if !strings.Contains(out, "\nRead\n") || !strings.Contains(out, "\nWrite\n") || !strings.Contains(out, "\nAdmin\n") {
t.Fatalf("expected command groups in gmail help, got: %q", out)
}
if !strings.Contains(out, "\n search") || !strings.Contains(out, "Search threads using Gmail query syntax") {
t.Fatalf("expected relative command summaries in gmail help, got: %q", out)
}
if strings.Contains(out, "\n gmail (mail,email) search <query>") {
t.Fatalf("unexpected full command prefix in gmail help, got: %q", out)
}
if strings.Contains(out, "\n watch <command>") {
t.Fatalf("expected watch to be under gmail settings (not top-level gmail help), got: %q", out)
}
if !strings.Contains(out, "\n settings <command>") {
t.Fatalf("expected settings subgroup in gmail help, got: %q", out)
}
}
func TestExecute_UnknownCommand(t *testing.T) {
errText := captureStderr(t, func() {
_ = captureStdout(t, func() {
if err := Execute([]string{"no_such_cmd"}); err == nil {
t.Fatalf("expected error")
}
})
})
if errText == "" {
t.Fatalf("expected stderr output")
}
}
func TestExecute_UnknownFlag(t *testing.T) {
errText := captureStderr(t, func() {
_ = captureStdout(t, func() {
if err := Execute([]string{"--definitely-nope"}); err == nil {
t.Fatalf("expected error")
}
})
})
if errText == "" {
t.Fatalf("expected stderr output")
}
}
func TestNewUsageError(t *testing.T) {
if newUsageError(nil) != nil {
t.Fatalf("expected nil for nil error")
}
err := errors.New("bad")
wrapped := newUsageError(err)
if wrapped == nil {
t.Fatalf("expected wrapped error")
}
var exitErr *ExitError
if !errors.As(wrapped, &exitErr) || exitErr.Code != 2 || !errors.Is(exitErr.Err, err) {
t.Fatalf("unexpected wrapped error: %#v", wrapped)
}
}