package model import ( "testing" "github.com/QuantumNous/new-api/common" ) func TestGetRandomSatisfiedChannelOrderedByPriorityThenWeight(t *testing.T) { origMemoryCacheEnabled := common.MemoryCacheEnabled origCacheEnabled := group2model2channels origChannels := channelsIDM defer func() { common.MemoryCacheEnabled = origMemoryCacheEnabled group2model2channels = origCacheEnabled channelsIDM = origChannels }() common.MemoryCacheEnabled = true group2model2channels = map[string]map[string][]int{ "image": { "gpt-image-2": {1, 2, 3}, }, } channelsIDM = map[int]*Channel{ 1: {Id: 1, Priority: int64Ptr(10), Weight: uintPtr(50)}, 2: {Id: 2, Priority: int64Ptr(10), Weight: uintPtr(20)}, 3: {Id: 3, Priority: int64Ptr(5), Weight: uintPtr(99)}, } first, err := GetRandomSatisfiedChannel("image", "gpt-image-2", 0) if err != nil { t.Fatalf("unexpected error: %v", err) } if first == nil || first.Id != 1 { t.Fatalf("expected first channel id 1, got %+v", first) } second, err := GetRandomSatisfiedChannel("image", "gpt-image-2", 1) if err != nil { t.Fatalf("unexpected error: %v", err) } if second == nil || second.Id != 2 { t.Fatalf("expected second channel id 2, got %+v", second) } third, err := GetRandomSatisfiedChannel("image", "gpt-image-2", 2) if err != nil { t.Fatalf("unexpected error: %v", err) } if third == nil || third.Id != 3 { t.Fatalf("expected third channel id 3, got %+v", third) } } func int64Ptr(v int64) *int64 { return &v } func uintPtr(v uint) *uint { return &v }