Sync production source including routing, billing, Astra detection and public uptime. Update custom build instructions and license packaging.
61 lines
1.8 KiB
Go
61 lines
1.8 KiB
Go
package model
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/QuantumNous/new-api/common"
|
|
"github.com/QuantumNous/new-api/constant"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestInitChannelCacheHandlesChannelBeforeAbilityRefresh(t *testing.T) {
|
|
resetPricingEndpointTestTables(t)
|
|
|
|
channel := &Channel{
|
|
Id: 501,
|
|
Type: constant.ChannelTypeOpenAI,
|
|
Key: "key-501",
|
|
Status: common.ChannelStatusEnabled,
|
|
Name: "channel-before-ability-refresh",
|
|
Models: "grok-4.6",
|
|
Group: "groksuper",
|
|
}
|
|
require.NoError(t, DB.Create(channel).Error)
|
|
|
|
require.NotPanics(t, InitChannelCache)
|
|
|
|
selected, err := GetRandomSatisfiedChannel("groksuper", "grok-4.6", 0, nil)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, selected)
|
|
assert.Equal(t, channel.Id, selected.Id)
|
|
}
|
|
|
|
func TestSelectionTraceSnapshot(t *testing.T) {
|
|
resetPricingEndpointTestTables(t)
|
|
previous := common.MemoryCacheEnabled
|
|
common.MemoryCacheEnabled = true
|
|
t.Cleanup(func() { common.MemoryCacheEnabled = previous })
|
|
for i := 1; i <= 2; i++ {
|
|
ch := &Channel{Id: 700 + i, Type: constant.ChannelTypeOpenAI, Key: "secret", Status: common.ChannelStatusEnabled, Name: "original", Models: "test", Group: "trace", Priority: common.GetPointer(int64(i))}
|
|
require.NoError(t, DB.Create(ch).Error)
|
|
require.NoError(t, ch.AddAbilities(nil))
|
|
}
|
|
InitChannelCache()
|
|
trace := &SelectionTrace{}
|
|
ch, err := GetRandomSatisfiedChannel("trace", "test", 0, nil, trace)
|
|
require.NoError(t, err)
|
|
require.Equal(t, 702, ch.Id)
|
|
require.Equal(t, 2, trace.Total)
|
|
require.Len(t, trace.Candidates, 2)
|
|
for _, candidate := range trace.Candidates {
|
|
if candidate.ChannelID == 702 {
|
|
require.Equal(t, "selected", candidate.Reason)
|
|
} else {
|
|
require.Equal(t, "different_priority", candidate.Reason)
|
|
}
|
|
}
|
|
ch.Name = "changed"
|
|
require.Equal(t, "original", trace.Candidates[0].Name)
|
|
}
|