-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevolution_test.go
More file actions
125 lines (103 loc) · 4.51 KB
/
evolution_test.go
File metadata and controls
125 lines (103 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// File: cmd/evolution_test.go
package cmd
import (
"context"
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"go.uber.org/zap"
"github.com/xkilldash9x/scalpel-cli/api/schemas"
"github.com/xkilldash9x/scalpel-cli/internal/config"
"github.com/xkilldash9x/scalpel-cli/internal/mocks"
"github.com/xkilldash9x/scalpel-cli/pkg/observability"
)
// MockAnalystRunner is a mock implementation of the AnalystRunner interface.
type MockAnalystRunner struct {
mock.Mock
}
func (m *MockAnalystRunner) Run(ctx context.Context, objective string, files []string) error {
args := m.Called(ctx, objective, files)
return args.Error(0)
}
// setupTestConfig is a helper to create a default config for testing.
// It now returns the config interface.
func setupTestConfig(t *testing.T) config.Interface {
t.Helper()
return config.NewDefaultConfig()
}
// NOTE: TestInitializeKGClient has been removed from this file, as
// initializeKGClient is no longer part of the 'cmd' package.
// That test should be moved to the 'internal/service' package.
// NOTE: This assumes runEvolve, AnalystRunner, and the initializer function type are defined in the cmd package.
func TestRunEvolve(t *testing.T) {
logger := zap.NewNop()
ctx := context.Background()
cfg := setupTestConfig(t) // Get a local config instance.
mockLLM := new(mocks.MockLLMClient) // Create the mock LLM
// Test Case: Successful Execution
t.Run("Success", func(t *testing.T) {
mockRunner := new(MockAnalystRunner)
expectedObjective := "test objective"
expectedFiles := []string{"file1.go"}
// Mock the initializer to return our mock runner.
// Renamed logger to _ as it is unused in this mock implementation, fixing "declared and not used" error.
mockInitFn := func(_ *zap.Logger, cfg config.Interface, llmClient schemas.LLMClient, kgClient schemas.KnowledgeGraphClient) (AnalystRunner, error) {
assert.NotNil(t, llmClient)
assert.NotNil(t, kgClient)
return mockRunner, nil
}
mockRunner.On("Run", mock.Anything, expectedObjective, expectedFiles).Return(nil)
// Execute, now passing the mockLLM.
// We pass useInMemoryKG=true to ensure the external service.InitializeKGClient call succeeds.
err := runEvolve(ctx, cfg, logger, expectedObjective, expectedFiles, true, mockLLM, mockInitFn)
// Assertions
assert.NoError(t, err)
mockRunner.AssertExpectations(t)
})
// Test Case: Validation Failure
t.Run("Missing Objective", func(t *testing.T) {
// No LLM client needed since it fails on validation first.
err := runEvolve(ctx, cfg, logger, "", []string{}, true, nil, nil)
assert.Error(t, err)
assert.Contains(t, err.Error(), "--objective is required")
})
// Test Case: Initialization Failure (Analyst)
t.Run("AnalystInitFailure", func(t *testing.T) {
expectedErr := errors.New("init failed")
// Renamed logger to _ as it is unused in this mock implementation.
mockInitFn := func(_ *zap.Logger, cfg config.Interface, llmClient schemas.LLMClient, kgClient schemas.KnowledgeGraphClient) (AnalystRunner, error) {
return nil, expectedErr
}
// Execute with the mock LLM.
// We pass useInMemoryKG=true to ensure the external service.InitializeKGClient call succeeds.
err := runEvolve(ctx, cfg, logger, "some objective", []string{}, true, mockLLM, mockInitFn)
assert.Error(t, err)
assert.Contains(t, err.Error(), "failed to initialize Improvement Analyst: init failed")
})
}
func TestEvolveCmd_RunE_LLMInitializationFailure(t *testing.T) {
// This test specifically checks the RunE function of the cobra command,
// ensuring that an error during dependency setup (before runEvolve is called)
// is handled correctly.
// Arrange
observability.ResetForTest()
observability.InitializeLogger(config.LoggerConfig{Level: "fatal"}) // Keep output clean
// Create a config that will cause the LLM client to fail initialization.
// For example, by having no models configured.
badCfg := config.NewDefaultConfig()
badCfg.AgentCfg.LLM.Models = make(map[string]config.LLMModelConfig) // No models
// Create the command instance.
evolveCmd := newEvolveCmd()
ctx := context.WithValue(context.Background(), configKey, badCfg)
evolveCmd.SetContext(ctx)
// Set required flags to pass initial cobra validation.
evolveCmd.SetArgs([]string{"--objective", "this will fail"})
// Act
// This will call the 'RunE' function, which in turn calls
// service.InitializeLLMClient with the bad config.
err := evolveCmd.Execute()
// Assert
assert.Error(t, err)
assert.Contains(t, err.Error(), "failed to initialize LLM client")
}