選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 

177 行
5.1 KiB

  1. package middleware
  2. import (
  3. "fmt"
  4. "os"
  5. "strings"
  6. "testing"
  7. "github.com/QuantumNous/new-api/common"
  8. "github.com/QuantumNous/new-api/constant"
  9. "github.com/QuantumNous/new-api/model"
  10. relayconstant "github.com/QuantumNous/new-api/relay/constant"
  11. "github.com/gin-gonic/gin"
  12. "github.com/stretchr/testify/assert"
  13. "github.com/stretchr/testify/require"
  14. "gorm.io/gorm"
  15. "gorm.io/gorm/logger"
  16. )
  17. func setupDoubaoVideoBindingDB(t *testing.T) *gorm.DB {
  18. t.Helper()
  19. oldDB := model.DB
  20. oldLOGDB := model.LOG_DB
  21. oldSQLitePath := common.SQLitePath
  22. oldMemoryCacheEnabled := common.MemoryCacheEnabled
  23. oldIsMasterNode := common.IsMasterNode
  24. oldUsingSQLite := common.UsingSQLite
  25. oldUsingMySQL := common.UsingMySQL
  26. oldUsingPostgreSQL := common.UsingPostgreSQL
  27. oldSQLDSN, hadSQLDSN := os.LookupEnv("SQL_DSN")
  28. common.SQLitePath = "file:" + strings.NewReplacer("/", "_", " ", "_").Replace(t.Name()) + "?mode=memory&cache=shared"
  29. common.MemoryCacheEnabled = false
  30. common.IsMasterNode = false
  31. common.UsingSQLite = false
  32. common.UsingMySQL = false
  33. common.UsingPostgreSQL = false
  34. require.NoError(t, os.Setenv("SQL_DSN", "local"))
  35. require.NoError(t, model.InitDB())
  36. model.DB = model.DB.Session(&gorm.Session{Logger: logger.Default.LogMode(logger.Silent)})
  37. db := model.DB
  38. sqlDB, err := db.DB()
  39. require.NoError(t, err)
  40. sqlDB.SetMaxOpenConns(1)
  41. require.NoError(t, db.AutoMigrate(&model.Channel{}, &model.Ability{}, &model.UserAssetChannel{}))
  42. t.Cleanup(func() {
  43. _ = sqlDB.Close()
  44. model.DB = oldDB
  45. model.LOG_DB = oldLOGDB
  46. common.SQLitePath = oldSQLitePath
  47. common.MemoryCacheEnabled = oldMemoryCacheEnabled
  48. common.IsMasterNode = oldIsMasterNode
  49. common.UsingSQLite = oldUsingSQLite
  50. common.UsingMySQL = oldUsingMySQL
  51. common.UsingPostgreSQL = oldUsingPostgreSQL
  52. if hadSQLDSN {
  53. _ = os.Setenv("SQL_DSN", oldSQLDSN)
  54. } else {
  55. _ = os.Unsetenv("SQL_DSN")
  56. }
  57. })
  58. return db
  59. }
  60. func doubaoVideoChannelForBindingTest(id int, group string) *model.Channel {
  61. priority := int64(id)
  62. weight := uint(10)
  63. autoBan := 1
  64. return &model.Channel{
  65. Id: id,
  66. Type: constant.ChannelTypeDoubaoVideoCompatibleAiping,
  67. Key: fmt.Sprintf("key-%d", id),
  68. Status: common.ChannelStatusEnabled,
  69. Name: fmt.Sprintf("doubao-video-%d", id),
  70. Group: group,
  71. Models: "seedance-2",
  72. Priority: &priority,
  73. Weight: &weight,
  74. AutoBan: &autoBan,
  75. }
  76. }
  77. func TestPreferBoundDoubaoVideoChannel(t *testing.T) {
  78. db := setupDoubaoVideoBindingDB(t)
  79. channel := doubaoVideoChannelForBindingTest(77, "default")
  80. priority := int64(channel.Id)
  81. require.NoError(t, db.Create(&model.Ability{
  82. Group: "default",
  83. Model: "seedance-2",
  84. ChannelId: channel.Id,
  85. Enabled: true,
  86. Priority: &priority,
  87. Weight: 10,
  88. }).Error)
  89. assert.True(t, shouldUseBoundDoubaoVideoChannel(nil, channel, "default", "seedance-2"))
  90. }
  91. func TestPersistDoubaoVideoBindingSkipsNonDoubaoWithoutDB(t *testing.T) {
  92. oldDB := model.DB
  93. model.DB = nil
  94. t.Cleanup(func() {
  95. model.DB = oldDB
  96. })
  97. err := persistDoubaoVideoBindingIfNeeded(10, "default", &model.Channel{
  98. Id: 88,
  99. Type: constant.ChannelTypeOpenAI,
  100. Status: common.ChannelStatusEnabled,
  101. Key: "openai-key",
  102. Group: "default",
  103. })
  104. require.NoError(t, err)
  105. }
  106. func TestPersistDoubaoVideoBindingSkipsBlankAndAutoGroupWithoutDB(t *testing.T) {
  107. oldDB := model.DB
  108. model.DB = nil
  109. t.Cleanup(func() {
  110. model.DB = oldDB
  111. })
  112. require.NoError(t, persistDoubaoVideoBindingIfNeeded(10, "", doubaoVideoChannelForBindingTest(89, "")))
  113. require.NoError(t, persistDoubaoVideoBindingIfNeeded(10, "auto", doubaoVideoChannelForBindingTest(90, "auto")))
  114. }
  115. func TestPersistDoubaoVideoBindingWritesBinding(t *testing.T) {
  116. setupDoubaoVideoBindingDB(t)
  117. channel := doubaoVideoChannelForBindingTest(99, "vip")
  118. require.NoError(t, persistDoubaoVideoBindingIfNeeded(10, "vip", channel))
  119. binding, err := model.GetUserAssetChannel(10, constant.ChannelTypeDoubaoVideoCompatibleAiping, "vip")
  120. require.NoError(t, err)
  121. require.NotNil(t, binding)
  122. assert.Equal(t, channel.Id, binding.ChannelId)
  123. }
  124. func TestShouldCheckDoubaoVideoBindingPreferenceSkipsNonVideoRelayMode(t *testing.T) {
  125. c, _ := gin.CreateTestContext(nil)
  126. c.Set("relay_mode", relayconstant.RelayModeChatCompletions)
  127. assert.False(t, shouldCheckDoubaoVideoBindingPreference(c, "default", "seedance-2"))
  128. }
  129. func TestShouldCheckDoubaoVideoBindingPreferenceRequiresVideoSubmitRelayMode(t *testing.T) {
  130. db := setupDoubaoVideoBindingDB(t)
  131. channel := doubaoVideoChannelForBindingTest(101, "default")
  132. require.NoError(t, db.Create(channel).Error)
  133. require.NoError(t, db.Create(&model.Ability{
  134. Group: "default",
  135. Model: "seedance-2",
  136. ChannelId: channel.Id,
  137. Enabled: true,
  138. }).Error)
  139. c, _ := gin.CreateTestContext(nil)
  140. c.Set("relay_mode", relayconstant.RelayModeVideoSubmit)
  141. assert.True(t, shouldCheckDoubaoVideoBindingPreference(c, "default", "seedance-2"))
  142. }
  143. func TestShouldPersistDoubaoVideoBindingRequiresVideoSubmitRelayMode(t *testing.T) {
  144. c, _ := gin.CreateTestContext(nil)
  145. c.Set("relay_mode", relayconstant.RelayModeChatCompletions)
  146. assert.False(t, shouldPersistDoubaoVideoBinding(c))
  147. c.Set("relay_mode", relayconstant.RelayModeVideoSubmit)
  148. assert.True(t, shouldPersistDoubaoVideoBinding(c))
  149. }