forked from securego/gosec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ai_test.go
114 lines (91 loc) · 2.87 KB
/
ai_test.go
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
package autofix
import (
"context"
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/securego/gosec/v2/issue"
)
// MockGenAIClient is a mock of the GenAIClient interface
type MockGenAIClient struct {
mock.Mock
}
func (m *MockGenAIClient) Close() error {
args := m.Called()
return args.Error(0)
}
func (m *MockGenAIClient) GenerativeModel(name string) GenAIGenerativeModel {
args := m.Called(name)
return args.Get(0).(GenAIGenerativeModel)
}
// MockGenAIGenerativeModel is a mock of the GenAIGenerativeModel interface
type MockGenAIGenerativeModel struct {
mock.Mock
}
func (m *MockGenAIGenerativeModel) GenerateContent(ctx context.Context, prompt string) (string, error) {
args := m.Called(ctx, prompt)
return args.String(0), args.Error(1)
}
func TestGenerateSolutionByGemini_Success(t *testing.T) {
// Arrange
issues := []*issue.Issue{
{What: "Example issue 1"},
}
mockClient := new(MockGenAIClient)
mockModel := new(MockGenAIGenerativeModel)
mockClient.On("GenerativeModel", GeminiModel).Return(mockModel)
mockModel.On("GenerateContent", mock.Anything, mock.Anything).Return("Autofix for issue 1", nil)
// Act
err := generateSolutionByGemini(mockClient, issues)
// Assert
assert.NoError(t, err)
assert.Equal(t, "Autofix for issue 1", issues[0].Autofix)
mockClient.AssertExpectations(t)
mockModel.AssertExpectations(t)
}
func TestGenerateSolutionByGemini_NoCandidates(t *testing.T) {
// Arrange
issues := []*issue.Issue{
{What: "Example issue 2"},
}
mockClient := new(MockGenAIClient)
mockModel := new(MockGenAIGenerativeModel)
mockClient.On("GenerativeModel", GeminiModel).Return(mockModel)
mockModel.On("GenerateContent", mock.Anything, mock.Anything).Return("", nil)
// Act
err := generateSolutionByGemini(mockClient, issues)
// Assert
assert.Error(t, err)
assert.Equal(t, "no autofix returned by gemini", err.Error())
mockClient.AssertExpectations(t)
mockModel.AssertExpectations(t)
}
func TestGenerateSolutionByGemini_APIError(t *testing.T) {
// Arrange
issues := []*issue.Issue{
{What: "Example issue 3"},
}
mockClient := new(MockGenAIClient)
mockModel := new(MockGenAIGenerativeModel)
mockClient.On("GenerativeModel", GeminiModel).Return(mockModel)
mockModel.On("GenerateContent", mock.Anything, mock.Anything).Return("", errors.New("API error"))
// Act
err := generateSolutionByGemini(mockClient, issues)
// Assert
assert.Error(t, err)
assert.Equal(t, "generating autofix with gemini: API error", err.Error())
mockClient.AssertExpectations(t)
mockModel.AssertExpectations(t)
}
func TestGenerateSolution_UnsupportedProvider(t *testing.T) {
// Arrange
issues := []*issue.Issue{
{What: "Example issue 4"},
}
// Act
err := GenerateSolution("unsupported-provider", "test-api-key", "", issues)
// Assert
assert.Error(t, err)
assert.Equal(t, "ai provider not supported", err.Error())
}