feat(copilot): move provider configuration to settings
This commit is contained in:
@@ -9,8 +9,10 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gochat/gochat/internal/llm"
|
||||
"github.com/gochat/gochat/internal/model"
|
||||
"github.com/gochat/gochat/internal/repository"
|
||||
"github.com/gochat/gochat/internal/security"
|
||||
"github.com/gochat/gochat/internal/service"
|
||||
"github.com/stretchr/testify/require"
|
||||
"gorm.io/datatypes"
|
||||
@@ -30,14 +32,21 @@ func newCaptainPreferenceFixture(t *testing.T) *captainPreferenceFixture {
|
||||
gin.SetMode(gin.TestMode)
|
||||
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{Logger: logger.Default.LogMode(logger.Silent)})
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, db.AutoMigrate(&model.Account{}, &model.CaptainPreference{}))
|
||||
require.NoError(t, db.AutoMigrate(&model.Account{}, &model.CaptainPreference{}, &model.InstallationConfig{}))
|
||||
|
||||
account := &model.Account{Name: "Captain Preferences", Active: true, CaptainModels: datatypes.JSON(`{}`), CaptainFeatures: datatypes.JSON(`{}`)}
|
||||
require.NoError(t, db.Create(account).Error)
|
||||
|
||||
prefRepo := repository.NewCaptainPreferenceRepo(db)
|
||||
accountRepo := repository.NewAccountRepo(db)
|
||||
handler := NewCaptainPreferenceHandler(service.NewCaptainPreferenceService(prefRepo, accountRepo))
|
||||
installationConfigRepo := repository.NewInstallationConfigRepo(db)
|
||||
encryptor, err := security.NewEncryptor(security.DefaultEncryptionConfig())
|
||||
require.NoError(t, err)
|
||||
manager := llm.NewProviderManager()
|
||||
copilotConfigService := service.NewCopilotConfigService(installationConfigRepo, encryptor, manager)
|
||||
preferenceService := service.NewCaptainPreferenceService(prefRepo, accountRepo)
|
||||
preferenceService.SetCopilotConfigService(copilotConfigService)
|
||||
handler := NewCaptainPreferenceHandler(preferenceService)
|
||||
router := gin.New()
|
||||
router.GET("/api/v1/accounts/:account_id/captain/preferences", handler.Get)
|
||||
router.PUT("/api/v1/accounts/:account_id/captain/preferences", func(c *gin.Context) {
|
||||
@@ -92,6 +101,7 @@ func TestCaptainPreferencesGetReturnsRawChatwootConfig(t *testing.T) {
|
||||
require.Contains(t, payload, "providers")
|
||||
require.Contains(t, payload, "models")
|
||||
require.Contains(t, payload, "features")
|
||||
require.Contains(t, payload, "provider_config")
|
||||
|
||||
features := payload["features"].(map[string]any)
|
||||
editor := features["editor"].(map[string]any)
|
||||
@@ -101,6 +111,27 @@ func TestCaptainPreferencesGetReturnsRawChatwootConfig(t *testing.T) {
|
||||
require.NotEmpty(t, editor["models"].([]any))
|
||||
}
|
||||
|
||||
func TestCaptainPreferencesUpdateProviderConfiguration(t *testing.T) {
|
||||
f := newCaptainPreferenceFixture(t)
|
||||
|
||||
w := f.request(http.MethodPut, f.path(""), map[string]any{
|
||||
"provider_config": map[string]any{
|
||||
"provider": "openai_compatible",
|
||||
"base_url": "https://llm.example.com/v1",
|
||||
"model": "example-model",
|
||||
"api_key": "secret-key-value",
|
||||
},
|
||||
})
|
||||
require.Equal(t, http.StatusOK, w.Code, w.Body.String())
|
||||
payload := decodeCaptainPreferencePayload(t, w)
|
||||
providerConfig := payload["provider_config"].(map[string]any)
|
||||
require.Equal(t, "openai_compatible", providerConfig["provider"])
|
||||
require.Equal(t, "https://llm.example.com/v1", providerConfig["base_url"])
|
||||
require.Equal(t, "example-model", providerConfig["model"])
|
||||
require.Equal(t, true, providerConfig["api_key_configured"])
|
||||
require.NotContains(t, w.Body.String(), "secret-key-value")
|
||||
}
|
||||
|
||||
func TestCaptainPreferencesUpdateMergesAccountModelsAndFeatures(t *testing.T) {
|
||||
f := newCaptainPreferenceFixture(t)
|
||||
require.NoError(t, f.db.Model(&model.Account{}).Where("id = ?", f.account.ID).Updates(map[string]any{
|
||||
|
||||
Reference in New Issue
Block a user