refactor(*): 稳定期治理 - 收敛核心架构、错误语义并修复跨项目数据竞态

This commit is contained in:
ddCat committed 2026-09-09 22:02:10 +08:00
1 parent bacd502f5c
commit b5f3413ded
298 files changed
+12681 -26428

No files matched your search

+116 -1
View File
@@ -1,11 +1,21 @@
package service
import (
"encoding/json"
"testing"
"infinite-canvas/backend/internal/model"
)
func mustEncodeModelCapabilityConfig(t *testing.T, config *ModelCapabilityConfig) string {
t.Helper()
encoded, err := json.Marshal(config)
if err != nil {
t.Fatal(err)
}
return string(encoded)
}
func TestNormalizeChannelModelContract(t *testing.T) {
channel := &model.ModelChannel{APIKey: "test-key"}
modelKey, providerModelKey, capability, protocol, err := normalizeChannelModelContract(channel, ChannelModelRequest{
@@ -78,7 +88,11 @@ func TestSaveAdminChannelModelPersistsAndPublishesIcon(t *testing.T) {
if stored.Icon != "OpenAI" {
t.Fatalf("stored icon = %q, want OpenAI", stored.Icon)
}
if public := svc.sanitizeChannelModel(saved); public.Icon != "OpenAI" {
public, err := svc.sanitizeChannelModel(saved)
if err != nil {
t.Fatal(err)
}
if public.Icon != "OpenAI" {
t.Fatalf("public icon = %q, want OpenAI", public.Icon)
}
legacyPublic := publicChannel(channel, false, []model.ChannelModel{*saved})
@@ -87,6 +101,107 @@ func TestSaveAdminChannelModelPersistsAndPublishesIcon(t *testing.T) {
}
}
func TestSanitizeChannelModelRejectsCorruptCapabilityConfig(t *testing.T) {
svc := &Service{}
_, err := svc.sanitizeChannelModel(&model.ChannelModel{
ID: "broken-image-model",
Capability: "image",
CapabilityConfigJSON: `{`,
})
if err == nil {
t.Fatal("sanitizeChannelModel() should reject corrupt capability JSON")
}
}
func TestSanitizeChannelModelAvailabilityRequiresValidPriceTier(t *testing.T) {
svc := &Service{}
channelModel := &model.ChannelModel{
ID: "image-model",
ModelKey: "image-model",
Capability: "image",
Protocol: model.ChannelInterfaceOpenAIImage,
CapabilityConfigJSON: mustEncodeModelCapabilityConfig(t, DefaultModelCapabilityConfigForModel(string(model.ChannelInterfaceOpenAIImage), "image-model")),
PriceConfigured: true,
BillingMode: "fixed_request",
UnitPriceMicrocredits: 10,
}
public, err := svc.sanitizeChannelModel(channelModel)
if err != nil {
t.Fatal(err)
}
if public.Available {
t.Fatal("legacy scalar price must not make a system channel model available")
}
channelModel.PriceTiers = []model.ChannelModelPriceTier{{
ID: "tier-1", BillingMode: "fixed_request", UnitPriceMicrocredits: 10,
Enabled: true, PriceConfigured: true,
}}
public, err = svc.sanitizeChannelModel(channelModel)
if err != nil {
t.Fatal(err)
}
if !public.Available || len(public.PriceTiers) != 1 {
t.Fatalf("valid price tier was not published: %#v", public)
}
}
func TestChannelModelMatchesIntentUsesPersistedCapabilityContract(t *testing.T) {
svc := &Service{}
profile := DefaultModelCapabilityConfigForModel(string(model.ChannelInterfaceOpenAIImage), "image-model")
profile.Image.References.MaxImages = 1
channelModel := &model.ChannelModel{
ID: "image-model",
ModelKey: "image-model",
Capability: "image",
Protocol: model.ChannelInterfaceOpenAIImage,
CapabilityConfigJSON: mustEncodeModelCapabilityConfig(t, profile),
}
matched, err := svc.channelModelMatchesIntent(channelModel, &ModelRequestIntent{
Capability: "image",
Inputs: map[string]int{"image": 2},
})
if err != nil {
t.Fatal(err)
}
if matched {
t.Fatal("request exceeding the persisted reference-image limit should not match")
}
channelModel.CapabilityConfigJSON = `{`
if _, err := svc.channelModelMatchesIntent(channelModel, &ModelRequestIntent{Capability: "image"}); err == nil {
t.Fatal("corrupt persisted capability config should fail closed")
}
}
func TestPublicSystemChannelCatalogIsolatesCorruptModel(t *testing.T) {
svc, db := newChannelModelTestService(t)
channel := model.ModelChannel{ID: "system-channel", Scope: model.ChannelScopeSystem, Enabled: true, Name: "系统渠道"}
if err := db.Create(&channel).Error; err != nil {
t.Fatal(err)
}
validConfig := mustEncodeModelCapabilityConfig(t, DefaultModelCapabilityConfigForModel(string(model.ChannelInterfaceOpenAIImage), "valid-model"))
models := []model.ChannelModel{
{ID: "valid-model", ChannelID: channel.ID, ModelKey: "valid-model", Capability: "image", Protocol: model.ChannelInterfaceOpenAIImage, CapabilityConfigJSON: validConfig, Enabled: true},
{ID: "broken-model", ChannelID: channel.ID, ModelKey: "broken-model", Capability: "image", Protocol: model.ChannelInterfaceOpenAIImage, CapabilityConfigJSON: `{`, Enabled: true},
}
for index := range models {
if err := db.Create(&models[index]).Error; err != nil {
t.Fatal(err)
}
}
catalog, err := svc.publicSystemChannelCatalog(nil)
if err != nil {
t.Fatal(err)
}
if len(catalog) != 1 || len(catalog[0].Models) != 1 || catalog[0].Models[0].ID != "valid-model" {
t.Fatalf("catalog should retain only the valid model: %#v", catalog)
}
}
func TestImageTestDefaultsUseModelCapability(t *testing.T) {
tests := []struct {
name string