fix(release): 发布回归 - 修复资源引用保护并校准验证合同
This commit is contained in:
1 parent
04f49768cd
commit
037b288668
19 files changed
+363
-105
No files matched your search
@@ -168,7 +168,8 @@ func TestDuplicateSystemChannelCopiesSecretsModelsAndPriceTiers(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestChannelFromRequestStoresAndClearsHeaders(t *testing.T) {
|
||||
request := ChannelRequest{Name: "Headers", BaseURL: "https://example.com/v1", Headers: []OutboundHeader{{Name: "User-Agent", Value: "Custom Agent"}}}
|
||||
// URL validation is real, but this non-network test must not depend on DNS.
|
||||
request := ChannelRequest{Name: "Headers", BaseURL: "https://93.184.216.34/v1", Headers: []OutboundHeader{{Name: "User-Agent", Value: "Custom Agent"}}}
|
||||
channel, err := channelFromRequest(request, model.ModelChannel{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -217,7 +218,7 @@ func TestRuntimeConcurrencyUsesEnvironmentFallback(t *testing.T) {
|
||||
}
|
||||
|
||||
useGlobal := true
|
||||
channel, err := channelFromRequest(ChannelRequest{Name: "Global", BaseURL: "https://example.com/v1", UseGlobalConcurrency: &useGlobal}, model.ModelChannel{ConcurrencyLimit: 4})
|
||||
channel, err := channelFromRequest(ChannelRequest{Name: "Global", BaseURL: "https://93.184.216.34/v1", UseGlobalConcurrency: &useGlobal}, model.ModelChannel{ConcurrencyLimit: 4})
|
||||
if err != nil || channel.ConcurrencyLimit != 0 {
|
||||
t.Fatalf("global concurrency channel = %#v, error = %v", channel, err)
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
@@ -133,7 +135,7 @@ func TestPrepareOfficialGeminiAgentCacheCreatesAndReusesStablePrefix(t *testing.
|
||||
stableInstruction := strings.Repeat("stable system instruction ", 1000)
|
||||
spec := protocol.RequestSpec{
|
||||
Method: http.MethodPost,
|
||||
Path: "/models/gemini-test:generateContent",
|
||||
Path: "/v1beta/models/gemini-test:generateContent",
|
||||
ContentType: "application/json",
|
||||
Body: map[string]any{
|
||||
"systemInstruction": map[string]any{"parts": []any{map[string]any{"text": stableInstruction}}},
|
||||
@@ -142,6 +144,24 @@ func TestPrepareOfficialGeminiAgentCacheCreatesAndReusesStablePrefix(t *testing.
|
||||
"contents": []any{map[string]any{"role": "user", "parts": []any{map[string]any{"text": "hello"}}}},
|
||||
},
|
||||
}
|
||||
// Build the request through the shipped plugin, so the fixture cannot
|
||||
// silently diverge from its versioned endpoint or body mapping.
|
||||
manifest, err := os.ReadFile(filepath.Join("..", "..", "..", "plugin-packages", "google-gemini-generate-content", "manifest.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
adapter, err := protocol.LoadManifest(manifest)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
agentAdapter, ok := adapter.(protocol.AgentAdapter)
|
||||
if !ok {
|
||||
t.Fatal("official Gemini plugin does not support Agent requests")
|
||||
}
|
||||
spec, err = agentAdapter.BuildAgent(ctx, protocol.AgentRequestContext{BaseURL: server.URL, Model: input.Config.Model, Request: map[string]any{"gemini": spec.Body}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
prepared, used, err := prepareOfficialGeminiAgentCache(ctx, input, spec)
|
||||
if err != nil {
|
||||
|
||||
@@ -47,7 +47,8 @@ func TestProviderPollingDeadlineDefaultsToOneHour(t *testing.T) {
|
||||
|
||||
func TestOnlyResumableNewAPIChannel2VideoDeadlinesStayRunning(t *testing.T) {
|
||||
svc := &Service{}
|
||||
input, err := json.Marshal(canvasGenerationInput{Mode: "video", Config: providerConfig{BaseURL: "https://example.com", InterfaceType: string(model.ChannelInterfaceNewAPIChannel2)}})
|
||||
// Only validate policy here; a public IP keeps SSRF checks without external DNS.
|
||||
input, err := json.Marshal(canvasGenerationInput{Mode: "video", Config: providerConfig{BaseURL: "https://93.184.216.34", InterfaceType: string(model.ChannelInterfaceNewAPIChannel2)}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -83,7 +84,7 @@ func TestOnlyResumableNewAPIChannel2VideoDeadlinesStayRunning(t *testing.T) {
|
||||
if svc.shouldDeferVideoProviderTask(base, string(input), providerHTTPError{StatusCode: 400, Body: `{"code":"task_not_exist"}`}) {
|
||||
t.Fatal("untyped provider error must not be deferred")
|
||||
}
|
||||
other, err := json.Marshal(canvasGenerationInput{Mode: "video", Config: providerConfig{BaseURL: "https://example.com", InterfaceType: string(model.ChannelInterfaceNewAPIVideo)}})
|
||||
other, err := json.Marshal(canvasGenerationInput{Mode: "video", Config: providerConfig{BaseURL: "https://93.184.216.34", InterfaceType: string(model.ChannelInterfaceNewAPIVideo)}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -102,7 +103,7 @@ func TestResumableVideoDeadlineUsesResolvedSystemChannelProtocol(t *testing.T) {
|
||||
}
|
||||
channel := model.ModelChannel{
|
||||
ID: "channel-video", UserID: "admin", Scope: model.ChannelScopeSystem, Enabled: true, Name: "Video",
|
||||
BaseURL: "https://example.com", APIKey: "test-key", APIFormat: "openai", ModelsJSON: `["video-model"]`,
|
||||
BaseURL: "https://93.184.216.34", APIKey: "test-key", APIFormat: "openai", ModelsJSON: `["video-model"]`,
|
||||
}
|
||||
channelModel := model.ChannelModel{
|
||||
ID: "model-video", ChannelID: channel.ID, ModelKey: "video-model", Capability: "video",
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"math"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"infinite-canvas/backend/internal/model"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func prefixedIDTestDB(t *testing.T) *gorm.DB {
|
||||
t.Helper()
|
||||
db, err := Open(Config{Driver: "sqlite", DSN: filepath.Join(t.TempDir(), "ids.db")})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
sqlDB, err := db.DB()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = sqlDB.Close() })
|
||||
if err := db.AutoMigrate(&model.ModelChannel{}, &model.IDSequence{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return db
|
||||
}
|
||||
|
||||
func TestAllocatePrefixedIDReservesDeletedAndExistingIDs(t *testing.T) {
|
||||
db := prefixedIDTestDB(t)
|
||||
for _, id := range []string{"CHANNEL_000012", "CHANNEL_000030", "CHANNEL_bad", "OTHER_999999", "CHANNEL_999999999999999999999999"} {
|
||||
if err := db.Create(&model.ModelChannel{ID: id}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if err := db.Delete(&model.ModelChannel{}, "id = ?", "CHANNEL_000030").Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Create(&model.IDSequence{Name: "id:CHANNEL", Value: 2}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, want := range []string{"CHANNEL_000031", "CHANNEL_000032"} {
|
||||
got, err := AllocatePrefixedID(db, " channel ")
|
||||
if err != nil || got != want {
|
||||
t.Fatalf("got %q, %v; want %q", got, err, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestReconcilePrefixedIDSequencesNeverRegresses(t *testing.T) {
|
||||
db := prefixedIDTestDB(t)
|
||||
if err := db.Create(&model.ModelChannel{ID: "CHANNEL_000030"}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, want := range []int64{30, 30, 100, 100} {
|
||||
if want == 100 {
|
||||
if err := db.Model(&model.IDSequence{}).Where("name = ?", "id:CHANNEL").Update("value", want).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if err := ReconcilePrefixedIDSequences(db); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var sequence model.IDSequence
|
||||
if err := db.First(&sequence, "name = ?", "id:CHANNEL").Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if sequence.Value != want {
|
||||
t.Fatalf("got %d; want %d", sequence.Value, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAllocatePrefixedIDRejectsOverflowWithoutMutation(t *testing.T) {
|
||||
db := prefixedIDTestDB(t)
|
||||
if err := db.Create(&model.IDSequence{Name: "id:CHANNEL", Value: math.MaxInt64}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := AllocatePrefixedID(db, "CHANNEL")
|
||||
if err == nil || got != "" {
|
||||
t.Fatalf("expected no ID and overflow error, got %q, %v", got, err)
|
||||
}
|
||||
var sequence model.IDSequence
|
||||
if err := db.First(&sequence, "name = ?", "id:CHANNEL").Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if sequence.Value != math.MaxInt64 {
|
||||
t.Fatalf("overflow mutated sequence: %d", sequence.Value)
|
||||
}
|
||||
}
|
||||
@@ -297,16 +297,17 @@ func (r *Repository) ResourceReferenceSnapshotExcludingAssets(userID string, exc
|
||||
}
|
||||
|
||||
type joinedRepresentation struct {
|
||||
ID string
|
||||
Title string
|
||||
ResourceID string
|
||||
ID string
|
||||
Title string
|
||||
ResourceID string
|
||||
MetadataJSON string
|
||||
}
|
||||
var representations []joinedRepresentation
|
||||
representationQuery := r.db.Table("asset_representations").
|
||||
Select("asset_representations.id, assets.title, asset_representations.resource_id").
|
||||
Select("asset_representations.id, assets.title, asset_representations.resource_id, asset_representations.metadata_json").
|
||||
Joins("JOIN asset_versions ON asset_versions.id = asset_representations.asset_version_id").
|
||||
Joins("JOIN assets ON assets.id = asset_versions.asset_id").
|
||||
Where("assets.user_id = ? AND asset_representations.resource_id IN ?", userID, resourceIDs)
|
||||
Where("assets.user_id = ?", userID)
|
||||
if len(excludingAssetIDs) > 0 {
|
||||
representationQuery = representationQuery.Where("assets.id NOT IN ?", excludingAssetIDs)
|
||||
}
|
||||
@@ -315,6 +316,7 @@ func (r *Repository) ResourceReferenceSnapshotExcludingAssets(userID string, exc
|
||||
}
|
||||
for _, representation := range representations {
|
||||
snapshot.Direct = append(snapshot.Direct, ResourceDirectReference{Kind: "素材", ID: representation.ID, Title: representation.Title, ResourceID: representation.ResourceID})
|
||||
snapshot.Documents = append(snapshot.Documents, ResourceReferenceDocument{Kind: "素材", ID: representation.ID, Title: representation.Title, PrimaryJSON: representation.MetadataJSON})
|
||||
}
|
||||
|
||||
var voices []model.VoiceProfile
|
||||
|
||||
Reference in new issue
Block a user