fix: 修复 AI 服务商配置新增失败 (#222)

This commit is contained in:
chengma
2026-08-14 11:04:51 +08:00
parent 57fc8a3392
commit 01469f74b2
4 changed files with 216 additions and 1 deletions
+32
View File
@@ -9,6 +9,7 @@ import (
"time"
"cmautobuy/admin/model"
"cmautobuy/admin/repository"
)
func TestParseConfidenceThresholdBPS_精确转换(t *testing.T) {
@@ -25,6 +26,37 @@ func TestParseConfidenceThresholdBPS_精确转换(t *testing.T) {
}
}
func TestSaveAIProviderConfig_新增后可查询并与审计同事务(t *testing.T) {
db := newTestDB(t)
now := time.Date(2026, 8, 14, 1, 2, 3, 0, time.UTC)
actor := model.User{UserID: "USR-AI-CONFIG", Username: "ai-admin", PasswordHash: "test-hash",
Role: model.RoleAdmin, Status: model.UserActive, PasswordChangedAt: model.NowISO(),
CreatedAt: model.NowISO(), UpdatedAt: model.NowISO()}
if err := repository.CreateUser(db, actor); err != nil {
t.Fatal(err)
}
input := AIProviderInput{Name: "主线路", BaseURL: "https://api.example.com/v1", Model: "model-1",
TimeoutSeconds: 30, MaxConcurrency: 2, ConfidenceThresholdBPS: 8500}
providerID, err := SaveAIProviderConfig(db, &actor, input, NewAIEndpointPolicy(nil), now)
if err != nil || providerID == "" {
t.Fatalf("新增 AI 配置失败: id=%q err=%v", providerID, err)
}
items, err := repository.ListAIProviders(db)
if err != nil || len(items) != 1 || items[0].ProviderID != providerID || items[0].Name != input.Name {
t.Fatalf("新增后列表未回显配置: items=%+v err=%v", items, err)
}
var auditCount int
if err := db.QueryRow(`SELECT COUNT(*) FROM ai_provider_audits WHERE provider_id=? AND action='create'`, providerID).Scan(&auditCount); err != nil || auditCount != 1 {
t.Fatalf("创建审计数量=%d err=%v", auditCount, err)
}
if _, err := SaveAIProviderConfig(db, &actor, input, NewAIEndpointPolicy(nil), now.Add(time.Minute)); !IsValidationError(err) {
t.Fatalf("重名配置应返回可展示的校验错误: %v", err)
}
if err := db.QueryRow(`SELECT COUNT(*) FROM ai_provider_configs`).Scan(&auditCount); err != nil || auditCount != 1 {
t.Fatalf("失败请求不得留下半条配置: count=%d err=%v", auditCount, err)
}
}
func TestAIEndpointPolicy_阻止凭据和内网地址(t *testing.T) {
policy := NewAIEndpointPolicy(nil)
for _, raw := range []string{