refactor(testgen): enhance debugging for empty responses and refine Go test prompt
- 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:
parent
b76aa6f800
commit
fd9fbee655
@ -32,6 +32,8 @@ Examples:
|
|||||||
modelFlag, _ := cmd.Flags().GetString("model")
|
modelFlag, _ := cmd.Flags().GetString("model")
|
||||||
model := config.GetModel("testgen", modelFlag)
|
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)
|
logger.Info("testgen started", "num_files", len(args), "model", model, "auto_apply", yesFlag)
|
||||||
|
|
||||||
client := grok.NewClient()
|
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))
|
color.Yellow("🤖 Generating tests for %s → %s...", filepath.Base(filePath), filepath.Base(testPath))
|
||||||
rawResponse := client.StreamSilent(messages, model)
|
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) == "" {
|
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")
|
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:
|
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"):
|
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.Parallel()
|
||||||
t.Log("✓ Fast XXX unit test")
|
t.Log("✓ Fast XXX unit test")
|
||||||
|
|
||||||
// Zero external calls
|
// Use table-driven tests with real function calls
|
||||||
// Use t.TempDir() + os.Chdir with proper defer restore + error logging
|
// Cover happy path + error cases
|
||||||
// Use standard testing.T (add testify/assert only if the project already uses it)
|
// NO monkey-patching: never assign to runtime.GOOS, exec.Command, os/exec.Command, or any exported var/func
|
||||||
// Cover happy path, errors, edge cases
|
// Use only standard testing.T (no testify unless the project already imports it)
|
||||||
// Table-driven where it makes sense
|
|
||||||
}
|
}
|
||||||
|
|
||||||
2. Optional live integration test (skipped by default):
|
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.Skip("skipping live integration test. Run with:\n go test -run TestXXX_Live -short -v")
|
||||||
}
|
}
|
||||||
t.Log("🧪 Running live integration test...")
|
t.Log("🧪 Running live integration test...")
|
||||||
// Real behavior (file I/O, exec, API calls, etc.)
|
// Real calls only
|
||||||
// Use t.Logf messages so it never looks hung
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Exact rules:
|
Exact rules:
|
||||||
- Derive sensible test names from the source filename and functions (e.g. filer.go → TestCreateIniFile_Unit / TestCreateIniFile_Live)
|
- Derive test names from functions (e.g. isInstalled → TestIsInstalled_Unit)
|
||||||
- The XXX in the t.Skip command MUST exactly match the live test function name you created
|
- The XXX in t.Skip MUST exactly match the live function name
|
||||||
- t.Parallel() on unit tests only
|
- t.Parallel() on unit tests only
|
||||||
- NO unused imports
|
- 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":
|
case "Python":
|
||||||
return `You are a pytest expert. Generate COMPLETE pytest unit tests for the Python source.
|
return `You are a pytest expert. Generate COMPLETE pytest unit tests for the Python source.
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user