refactor(testgen): enhance debugging for empty responses and refine Go test prompt
Some checks failed
CI / Test (push) Successful in 29s
CI / Lint (push) Failing after 7s
CI / Build (push) Failing after 6s

- Add user-facing message showing the selected model
- Implement detailed error logging when AI response is empty, including raw preview
- Update Go test generation prompt for better naming, rules against monkey-patching, and simplicity
This commit is contained in:
Greg Gauthier 2026-03-03 18:58:40 +00:00
parent b76aa6f800
commit fd9fbee655

View File

@ -32,6 +32,8 @@ Examples:
modelFlag, _ := cmd.Flags().GetString("model")
model := config.GetModel("testgen", modelFlag)
color.Cyan("🔧 testgen using model: %s (override with -m flag)", model)
logger.Info("testgen started", "num_files", len(args), "model", model, "auto_apply", yesFlag)
client := grok.NewClient()
@ -129,9 +131,19 @@ func processTestgenFile(client *grok.Client, filePath, lang, systemPrompt, model
color.Yellow("🤖 Generating tests for %s → %s...", filepath.Base(filePath), filepath.Base(testPath))
rawResponse := client.StreamSilent(messages, model)
newTestCode := grok.CleanCodeResponse(rawResponse)
// ← NEW: detailed debugging when the model returns nothing useful
newTestCode := grok.CleanCodeResponse(rawResponse)
if len(newTestCode) == 0 || strings.TrimSpace(newTestCode) == "" {
color.Red("❌ Cleaned response is empty")
color.Red(" Raw AI response length : %d characters", len(rawResponse))
if len(rawResponse) > 0 {
previewLen := 300
if len(rawResponse) < previewLen {
previewLen = len(rawResponse)
}
color.Red(" Raw preview (first %d chars):\n%s", previewLen, rawResponse[:previewLen])
}
return fmt.Errorf("empty generation response")
}
@ -197,15 +209,14 @@ func getTestPrompt(lang string) string {
Generate a COMPLETE, production-ready *_test.go file that follows this modern Unit + Live test pattern:
1. Fast unit test (always runs on "go test"):
func TestXXX_Unit(t *testing.T) { // or TestCreateIniFile if it fits better
func TestXXX_Unit(t *testing.T) {
t.Parallel()
t.Log("✓ Fast XXX unit test")
// Zero external calls
// Use t.TempDir() + os.Chdir with proper defer restore + error logging
// Use standard testing.T (add testify/assert only if the project already uses it)
// Cover happy path, errors, edge cases
// Table-driven where it makes sense
// Use table-driven tests with real function calls
// Cover happy path + error cases
// NO monkey-patching: never assign to runtime.GOOS, exec.Command, os/exec.Command, or any exported var/func
// Use only standard testing.T (no testify unless the project already imports it)
}
2. Optional live integration test (skipped by default):
@ -214,16 +225,18 @@ Generate a COMPLETE, production-ready *_test.go file that follows this modern Un
t.Skip("skipping live integration test. Run with:\n go test -run TestXXX_Live -short -v")
}
t.Log("🧪 Running live integration test...")
// Real behavior (file I/O, exec, API calls, etc.)
// Use t.Logf messages so it never looks hung
// Real calls only
}
Exact rules:
- Derive sensible test names from the source filename and functions (e.g. filer.go TestCreateIniFile_Unit / TestCreateIniFile_Live)
- The XXX in the t.Skip command MUST exactly match the live test function name you created
- Derive test names from functions (e.g. isInstalled TestIsInstalled_Unit)
- The XXX in t.Skip MUST exactly match the live function name
- t.Parallel() on unit tests only
- NO unused imports
- Return ONLY the full test file. No explanations, no markdown, no backticks, no extra text whatsoever.`
- Keep tests simple and idiomatic Go
- Return ONLY the full test file. No explanations, no markdown, no backticks.`
// Python/C/C++ prompts unchanged
case "Python":
return `You are a pytest expert. Generate COMPLETE pytest unit tests for the Python source.