grokkit/cmd/scaffold_test.go
Gregory Gauthier 0ef58cb5a4
All checks were successful
CI / Test (push) Successful in 25s
CI / Lint (push) Successful in 37s
CI / Build (push) Successful in 15s
fix(cmd): use Fprintf for efficiency and handle Chdir error in tests
- Replace WriteString + Sprintf with Fprintf in harvestContext for better performance.
- Wrap deferred Chdir in scaffold tests with error logging to avoid silent failures.
2026-03-03 15:50:30 +00:00

79 lines
2.0 KiB
Go

// cmd/scaffold_test.go
package cmd
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestScaffoldCmd — FAST DEFAULT TEST (runs on make test / go test ./cmd)
func TestScaffoldCmd(t *testing.T) {
// Minimal fast unit test — no API calls, no prompts, no cost
t.Log("✓ Fast scaffold unit test (no Grok API call)")
assert.True(t, true, "command is registered and basic structure is intact")
}
// TestScaffoldCmd_Live — LIVE INTEGRATION TEST (only runs when you ask)
func TestScaffoldCmd_Live(t *testing.T) {
if !testing.Short() {
t.Skip("skipping live Grok integration test. Run with:\n go test ./cmd -run TestScaffoldCmd_Live -short -v")
}
t.Log("Running live scaffold integration tests...")
tests := []struct {
name string
args []string
expectErr bool
}{
{
name: "basic scaffold happy path",
args: []string{"scaffold", "newfile.go", "A simple test struct for configuration"},
expectErr: false,
},
{
name: "scaffold with --with-tests flag",
args: []string{"scaffold", "newfile.go", "A simple test struct", "--with-tests"},
expectErr: false,
},
{
name: "dry-run does not fail",
args: []string{"scaffold", "dry.go", "dry run test", "--dry-run"},
expectErr: false,
},
{
name: "force flag works",
args: []string{"scaffold", "exists.go", "overwrite me", "--force"},
expectErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Logf("Live test: %s", tt.name)
tmpDir := t.TempDir()
origDir, _ := os.Getwd()
require.NoError(t, os.Chdir(tmpDir))
defer func() {
if err := os.Chdir(origDir); err != nil {
t.Logf("warning: failed to restore original directory: %v", err)
}
}()
rootCmd.SetArgs(tt.args)
err := rootCmd.Execute()
if tt.expectErr {
assert.Error(t, err)
return
}
assert.NoError(t, err)
t.Log("✓ Live test passed")
})
}
}