grokkit/cmd/root_test.go
Greg Gauthier ebb0cbcf3a
Some checks failed
CI / Test (push) Failing after 26s
CI / Lint (push) Has been skipped
CI / Build (push) Has been skipped
test(cmd): add unit tests for completion, root execution, config, errors, and logger
- Add tests for shell completion generation in completion_test.go
- Add tests for root command execution and flags in root_test.go
- Expand config tests for temperature, timeout, and log level getters
- Add APIError unwrap test in errors_test.go
- Add WithContext tests in logger_test.go
- Stage test output artifact in .output.txt
2026-03-02 22:12:54 +00:00

53 lines
1.1 KiB
Go

package cmd
import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"
)
func rootSetHomeForTest(t *testing.T, dir string) {
t.Helper()
if err := os.Setenv("HOME", dir); err != nil {
t.Fatal(err)
}
}
func TestExecute(t *testing.T) {
tests := []struct {
name string
args []string
exitCode int
wantLevel string
}{
{"version", []string{"version"}, 0, ""},
{"help", []string{}, 0, ""},
{"debug flag", []string{"version", "--debug"}, 0, "debug"},
{"verbose flag", []string{"version", "-v"}, 0, "info"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir := t.TempDir()
rootSetHomeForTest(t, tmpDir)
oldArgs := os.Args
os.Args = append([]string{"grokkit"}, tt.args...)
defer func() { os.Args = oldArgs }()
Execute()
if tt.wantLevel != "" {
logFile := filepath.Join(tmpDir, ".config", "grokkit", "grokkit.log")
content, err := os.ReadFile(logFile)
if err != nil {
t.Error(err)
return
}
str := string(content)
if !strings.Contains(str, fmt.Sprintf(`"log_level":"%s"`, tt.wantLevel)) {
t.Errorf("log level not %s:\n%s", tt.wantLevel, str)
}
}
})
}
}