2024-08-12 11:52:41 +01:00
|
|
|
package autofix
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/mock"
|
2024-12-20 09:03:23 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2024-08-12 11:52:41 +01:00
|
|
|
|
|
|
|
"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)
|
2024-12-20 09:03:23 +00:00
|
|
|
mockClient.On("GenerativeModel", GeminiModel).Return(mockModel).Once()
|
|
|
|
mockModel.On("GenerateContent", mock.Anything, mock.Anything).Return("Autofix for issue 1", nil).Once()
|
2024-08-12 11:52:41 +01:00
|
|
|
|
|
|
|
// Act
|
|
|
|
err := generateSolutionByGemini(mockClient, issues)
|
|
|
|
|
|
|
|
// Assert
|
2024-12-20 09:03:23 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, []*issue.Issue{{What: "Example issue 1", Autofix: "Autofix for issue 1"}}, issues)
|
|
|
|
mock.AssertExpectationsForObjects(t, mockClient, mockModel)
|
2024-08-12 11:52:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenerateSolutionByGemini_NoCandidates(t *testing.T) {
|
|
|
|
// Arrange
|
|
|
|
issues := []*issue.Issue{
|
|
|
|
{What: "Example issue 2"},
|
|
|
|
}
|
|
|
|
|
|
|
|
mockClient := new(MockGenAIClient)
|
|
|
|
mockModel := new(MockGenAIGenerativeModel)
|
2024-12-20 09:03:23 +00:00
|
|
|
mockClient.On("GenerativeModel", GeminiModel).Return(mockModel).Once()
|
|
|
|
mockModel.On("GenerateContent", mock.Anything, mock.Anything).Return("", nil).Once()
|
2024-08-12 11:52:41 +01:00
|
|
|
|
|
|
|
// Act
|
|
|
|
err := generateSolutionByGemini(mockClient, issues)
|
|
|
|
|
|
|
|
// Assert
|
2024-12-20 09:03:23 +00:00
|
|
|
require.EqualError(t, err, "no autofix returned by gemini")
|
|
|
|
mock.AssertExpectationsForObjects(t, mockClient, mockModel)
|
2024-08-12 11:52:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenerateSolutionByGemini_APIError(t *testing.T) {
|
|
|
|
// Arrange
|
|
|
|
issues := []*issue.Issue{
|
|
|
|
{What: "Example issue 3"},
|
|
|
|
}
|
|
|
|
|
|
|
|
mockClient := new(MockGenAIClient)
|
|
|
|
mockModel := new(MockGenAIGenerativeModel)
|
2024-12-20 09:03:23 +00:00
|
|
|
mockClient.On("GenerativeModel", GeminiModel).Return(mockModel).Once()
|
|
|
|
mockModel.On("GenerateContent", mock.Anything, mock.Anything).Return("", errors.New("API error")).Once()
|
2024-08-12 11:52:41 +01:00
|
|
|
|
|
|
|
// Act
|
|
|
|
err := generateSolutionByGemini(mockClient, issues)
|
|
|
|
|
|
|
|
// Assert
|
2024-12-20 09:03:23 +00:00
|
|
|
require.EqualError(t, err, "generating autofix with gemini: API error")
|
|
|
|
mock.AssertExpectationsForObjects(t, mockClient, mockModel)
|
2024-08-12 11:52:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenerateSolution_UnsupportedProvider(t *testing.T) {
|
|
|
|
// Arrange
|
|
|
|
issues := []*issue.Issue{
|
|
|
|
{What: "Example issue 4"},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Act
|
|
|
|
err := GenerateSolution("unsupported-provider", "test-api-key", "", issues)
|
|
|
|
|
|
|
|
// Assert
|
2024-12-20 09:03:23 +00:00
|
|
|
require.EqualError(t, err, "ai provider not supported")
|
2024-08-12 11:52:41 +01:00
|
|
|
}
|