mirror of
https://github.com/truewhile/MeBox.git
synced 2026-09-28 03:06:38 +08:00
71 lines
1.8 KiB
Go
71 lines
1.8 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"github.com/ShukeBta/MediaStationGo/internal/config"
|
|
"github.com/ShukeBta/MediaStationGo/internal/model"
|
|
"github.com/ShukeBta/MediaStationGo/internal/repository"
|
|
)
|
|
|
|
func TestAIStatusUsesDatabaseOpenAIConfig(t *testing.T) {
|
|
db := newServiceTestDB(t, &model.APIConfig{})
|
|
repo := &repository.Container{DB: db}
|
|
crypto := NewCryptoService("test-secret", zap.NewNop())
|
|
apiConfig := NewAPIConfigService(zap.NewNop(), repo, crypto)
|
|
key := "sk-test"
|
|
baseURL := "https://example.test/v1"
|
|
enabled := true
|
|
if _, err := apiConfig.Update(context.Background(), "openai", APIConfigPatch{
|
|
APIKey: &key,
|
|
BaseURL: &baseURL,
|
|
Enabled: &enabled,
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
ai := NewAIService(&config.Config{
|
|
AI: config.AIConfig{
|
|
Enabled: false,
|
|
Model: "gpt-4o-mini",
|
|
},
|
|
}, zap.NewNop(), apiConfig)
|
|
|
|
status := ai.Status(context.Background())
|
|
if !status.Enabled {
|
|
t.Fatalf("AI status disabled, want enabled from database config")
|
|
}
|
|
if status.Provider != "openai" {
|
|
t.Fatalf("provider = %q, want openai", status.Provider)
|
|
}
|
|
}
|
|
|
|
func TestAIStatusHonorsDisabledDatabaseOpenAIConfig(t *testing.T) {
|
|
db := newServiceTestDB(t, &model.APIConfig{})
|
|
repo := &repository.Container{DB: db}
|
|
apiConfig := NewAPIConfigService(zap.NewNop(), repo, NewCryptoService("test-secret", zap.NewNop()))
|
|
key := "sk-test"
|
|
enabled := false
|
|
if _, err := apiConfig.Update(context.Background(), "openai", APIConfigPatch{
|
|
APIKey: &key,
|
|
Enabled: &enabled,
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
ai := NewAIService(&config.Config{
|
|
AI: config.AIConfig{
|
|
Enabled: true,
|
|
APIKey: "sk-file",
|
|
Model: "gpt-4o-mini",
|
|
},
|
|
}, zap.NewNop(), apiConfig)
|
|
|
|
if ai.Status(context.Background()).Enabled {
|
|
t.Fatalf("AI status enabled, want disabled when database config is explicitly disabled")
|
|
}
|
|
}
|