From d1eaa5234bf891eed4f04bec62b9b315647fa6dd Mon Sep 17 00:00:00 2001 From: Gregory Gauthier Date: Tue, 3 Mar 2026 16:52:50 +0000 Subject: [PATCH] refactor(testgen): update Go prompt to enforce unit + live test pattern - Modify user message to reference new pattern in system prompt - Revise Go test prompt for exact unit/live structure matching scaffold_test.go - Update testgen_test.go to match new prompt prefix --- cmd/testgen.go | 41 ++++++++++++++++++++++++++--------------- cmd/testgen_test.go | 2 +- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/cmd/testgen.go b/cmd/testgen.go index 5654798..24f58bf 100644 --- a/cmd/testgen.go +++ b/cmd/testgen.go @@ -124,8 +124,7 @@ func processTestgenFile(client *grok.Client, filePath, lang, systemPrompt, model }, { "role": "user", - "content": fmt.Sprintf("Source file %s (%s):\n```%s\n%s\n```\n\nGenerate the language-appropriate test file.", filepath.Base(filePath), lang, codeLang, cleanSrc), - }, + "content": fmt.Sprintf("Source file %s (%s):\n```%s\n%s\n```\n\nGenerate the test file using the new Unit + Live integration pattern described in the system prompt.", filepath.Base(filePath), lang, codeLang, cleanSrc)}, } color.Yellow("🤖 Generating tests for %s → %s...", filepath.Base(filePath), filepath.Base(testPath)) @@ -193,21 +192,33 @@ func removeSourceComments(content, lang string) string { func getTestPrompt(lang string) string { switch lang { case "Go": - return `You are an expert Go testing specialist. Generate COMPLETE, production-ready unit tests for the provided Go source: + return `You are an expert Go test writer for the Grokkit project. -- Table-driven with t.Run(subtest, func(t *testing.T)) and t.Parallel() -- Match exact style of gmgauthier/grokkit/internal/version/version_test.go -- Use t.Context() for ctx-aware tests -- Modern Go 1.24+: slices.Contains/IndexFunc, maps.Keys, errors.Is/Join, any, etc. -- Cover ALL public funcs/methods/fields: happy path, edges, errors, panics, zero values -- Realistic inputs, no external deps/mocks unless code requires -- func TestXxx(t *testing.T) { ... } only +Generate a COMPLETE, production-ready *_test.go file that follows this EXACT pattern: -Respond with ONLY the full *_test.go file: -- Correct package name (infer from code) -- Necessary imports -- No benchmarks unless obvious perf func -- NO prose, explanations, markdown, code blocks, or extra text. Pure Go test file.` +1. Fast unit test(s) named TestXXX_Unit (or just TestXXX for the main test) + - Zero API calls + - Uses t.TempDir() + os.Chdir with proper defer restore (and error logging) + - Uses testify/assert and require + - Always runs instantly on "make test" + +2. Optional live integration test named TestXXX_Live + - Skipped by default with a clear t.Skip message showing the exact command to run it + - Only runs when the user passes -short + - Does real Grok API calls (exactly like the scaffold live test) + - Includes t.Log messages so it never looks hung + +Match the style of cmd/scaffold_test.go exactly: +- Clear t.Logf messages +- Table-driven where it makes sense +- Proper temp-dir isolation +- No unused imports +- defer restore of working directory with error logging + +Return ONLY the full Go test file. No explanations, no markdown, no backticks, no extra text whatsoever. + +Current project style (match imports, helpers, error handling): +{{.Context}}` case "Python": return `You are a pytest expert. Generate COMPLETE pytest unit tests for the Python source. diff --git a/cmd/testgen_test.go b/cmd/testgen_test.go index 5751045..6004bed 100644 --- a/cmd/testgen_test.go +++ b/cmd/testgen_test.go @@ -93,7 +93,7 @@ func TestGetTestPrompt(t *testing.T) { lang string wantPrefix string }{ - {"Go", "You are an expert Go testing specialist."}, + {"Go", "You are an expert Go test writer for the Grokkit project."}, {"Python", "You are a pytest expert."}, {"C", "You are a C unit testing expert using Check framework."}, {"C++", "You are a Google Test expert."},