|
3 | 3 | package run_test |
4 | 4 |
|
5 | 5 | import ( |
| 6 | + "context" |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + "path/filepath" |
| 10 | + "strings" |
6 | 11 | "testing" |
| 12 | + "time" |
| 13 | + |
| 14 | + "chuchu/internal/config" |
| 15 | + "chuchu/internal/llm" |
| 16 | + "chuchu/internal/testgen" |
7 | 17 | ) |
8 | 18 |
|
9 | 19 | func TestTestGeneration(t *testing.T) { |
10 | 20 | t.Run("generate unit tests", func(t *testing.T) { |
11 | | - t.Skip("TODO: Implement - Cover new code with tests") |
| 21 | + if os.Getenv("SKIP_E2E_LLM") != "" { |
| 22 | + t.Skip("Skipping LLM-dependent E2E test") |
| 23 | + } |
| 24 | + |
| 25 | + tmpDir := t.TempDir() |
| 26 | + |
| 27 | + // Create a simple Go file to test |
| 28 | + sourceCode := `package calculator |
| 29 | +
|
| 30 | +// Add returns the sum of two integers |
| 31 | +func Add(a, b int) int { |
| 32 | + return a + b |
| 33 | +} |
| 34 | +
|
| 35 | +// Subtract returns the difference between two integers |
| 36 | +func Subtract(a, b int) int { |
| 37 | + return a - b |
| 38 | +} |
| 39 | +
|
| 40 | +// Multiply returns the product of two integers |
| 41 | +func Multiply(a, b int) int { |
| 42 | + return a * b |
| 43 | +} |
| 44 | +
|
| 45 | +// Divide returns the quotient of two integers and an error if dividing by zero |
| 46 | +func Divide(a, b int) (int, error) { |
| 47 | + if b == 0 { |
| 48 | + return 0, fmt.Errorf("division by zero") |
| 49 | + } |
| 50 | + return a / b, nil |
| 51 | +} |
| 52 | +` |
| 53 | + |
| 54 | + sourceFile := filepath.Join(tmpDir, "calculator.go") |
| 55 | + if err := os.WriteFile(sourceFile, []byte(sourceCode), 0644); err != nil { |
| 56 | + t.Fatalf("Failed to create source file: %v", err) |
| 57 | + } |
| 58 | + |
| 59 | + // Create go.mod so it's a valid Go module |
| 60 | + goMod := `module testcalc |
| 61 | +
|
| 62 | +go 1.21 |
| 63 | +` |
| 64 | + if err := os.WriteFile(filepath.Join(tmpDir, "go.mod"), []byte(goMod), 0644); err != nil { |
| 65 | + t.Fatalf("Failed to create go.mod: %v", err) |
| 66 | + } |
| 67 | + |
| 68 | + // Load config and create generator |
| 69 | + setup, err := config.LoadSetup() |
| 70 | + if err != nil { |
| 71 | + t.Fatalf("Failed to load config: %v", err) |
| 72 | + } |
| 73 | + |
| 74 | + backendName := setup.Defaults.Backend |
| 75 | + if backendName == "" { |
| 76 | + backendName = "anthropic" |
| 77 | + } |
| 78 | + backendCfg := setup.Backend[backendName] |
| 79 | + |
| 80 | + var provider llm.Provider |
| 81 | + if backendCfg.Type == "ollama" { |
| 82 | + provider = llm.NewOllama(backendCfg.BaseURL) |
| 83 | + } else { |
| 84 | + provider = llm.NewChatCompletion(backendCfg.BaseURL, backendName) |
| 85 | + } |
| 86 | + |
| 87 | + queryModel := backendCfg.GetModelForAgent("query") |
| 88 | + if queryModel == "" { |
| 89 | + queryModel = backendCfg.DefaultModel |
| 90 | + } |
| 91 | + |
| 92 | + generator, err := testgen.NewTestGenerator(provider, queryModel, tmpDir) |
| 93 | + if err != nil { |
| 94 | + t.Fatalf("Failed to create test generator: %v", err) |
| 95 | + } |
| 96 | + |
| 97 | + // Generate tests |
| 98 | + t.Log("Generating unit tests...") |
| 99 | + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute) |
| 100 | + defer cancel() |
| 101 | + |
| 102 | + result, err := generator.GenerateUnitTests(ctx, "calculator.go") |
| 103 | + if err != nil && result == nil { |
| 104 | + t.Fatalf("Failed to generate tests: %v", err) |
| 105 | + } |
| 106 | + |
| 107 | + // Verify test file was created |
| 108 | + testFile := filepath.Join(tmpDir, result.TestFile) |
| 109 | + if _, err := os.Stat(testFile); os.IsNotExist(err) { |
| 110 | + t.Fatalf("Test file was not created: %s", result.TestFile) |
| 111 | + } |
| 112 | + |
| 113 | + // Verify test file has content |
| 114 | + testContent, err := os.ReadFile(testFile) |
| 115 | + if err != nil { |
| 116 | + t.Fatalf("Failed to read test file: %v", err) |
| 117 | + } |
| 118 | + |
| 119 | + if len(testContent) == 0 { |
| 120 | + t.Fatal("Test file is empty") |
| 121 | + } |
| 122 | + |
| 123 | + testStr := string(testContent) |
| 124 | + t.Logf("Generated test file (%d bytes)", len(testContent)) |
| 125 | + |
| 126 | + // Verify basic test structure |
| 127 | + if !strings.Contains(testStr, "package calculator") { |
| 128 | + t.Error("Test file missing package declaration") |
| 129 | + } |
| 130 | + if !strings.Contains(testStr, "func Test") { |
| 131 | + t.Error("Test file missing test functions") |
| 132 | + } |
| 133 | + if !strings.Contains(testStr, "*testing.T") { |
| 134 | + t.Error("Test file missing testing.T parameter") |
| 135 | + } |
| 136 | + |
| 137 | + // Try to compile the test |
| 138 | + t.Log("Validating test compiles...") |
| 139 | + cmd := exec.Command("go", "test", "-c", "-o", "/dev/null", ".") |
| 140 | + cmd.Dir = tmpDir |
| 141 | + output, compileErr := cmd.CombinedOutput() |
| 142 | + |
| 143 | + if compileErr != nil { |
| 144 | + t.Logf("Compilation output:\n%s", string(output)) |
| 145 | + t.Errorf("Generated test does not compile: %v", compileErr) |
| 146 | + } else { |
| 147 | + t.Log("✓ Test compiles successfully") |
| 148 | + } |
| 149 | + |
| 150 | + // Try to run the test |
| 151 | + if compileErr == nil { |
| 152 | + t.Log("Running generated tests...") |
| 153 | + cmd = exec.Command("go", "test", "-v", ".") |
| 154 | + cmd.Dir = tmpDir |
| 155 | + output, runErr := cmd.CombinedOutput() |
| 156 | + t.Logf("Test output:\n%s", string(output)) |
| 157 | + |
| 158 | + if runErr != nil { |
| 159 | + t.Logf("⚠️ Tests failed to run: %v (this is OK if logic is wrong, not if syntax is wrong)", runErr) |
| 160 | + } else { |
| 161 | + t.Log("✓ Tests ran successfully") |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + // Overall validation |
| 166 | + if result.Valid { |
| 167 | + t.Log("✓ Test generation succeeded with valid output") |
| 168 | + } else { |
| 169 | + t.Logf("⚠️ Test generation completed but validation failed: %v", result.Error) |
| 170 | + } |
12 | 171 | }) |
13 | 172 |
|
14 | 173 | t.Run("generate integration tests", func(t *testing.T) { |
|
0 commit comments