channel_upstream_update_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package controller
  2. import (
  3. "testing"
  4. "github.com/QuantumNous/new-api/dto"
  5. "github.com/QuantumNous/new-api/model"
  6. "github.com/stretchr/testify/require"
  7. )
  8. func TestNormalizeModelNames(t *testing.T) {
  9. result := normalizeModelNames([]string{
  10. " gpt-4o ",
  11. "",
  12. "gpt-4o",
  13. "gpt-4.1",
  14. " ",
  15. })
  16. require.Equal(t, []string{"gpt-4o", "gpt-4.1"}, result)
  17. }
  18. func TestMergeModelNames(t *testing.T) {
  19. result := mergeModelNames(
  20. []string{"gpt-4o", "gpt-4.1"},
  21. []string{"gpt-4.1", " gpt-4.1-mini ", "gpt-4o"},
  22. )
  23. require.Equal(t, []string{"gpt-4o", "gpt-4.1", "gpt-4.1-mini"}, result)
  24. }
  25. func TestSubtractModelNames(t *testing.T) {
  26. result := subtractModelNames(
  27. []string{"gpt-4o", "gpt-4.1", "gpt-4.1-mini"},
  28. []string{"gpt-4.1", "not-exists"},
  29. )
  30. require.Equal(t, []string{"gpt-4o", "gpt-4.1-mini"}, result)
  31. }
  32. func TestIntersectModelNames(t *testing.T) {
  33. result := intersectModelNames(
  34. []string{"gpt-4o", "gpt-4.1", "gpt-4.1", "not-exists"},
  35. []string{"gpt-4.1", "gpt-4o-mini", "gpt-4o"},
  36. )
  37. require.Equal(t, []string{"gpt-4o", "gpt-4.1"}, result)
  38. }
  39. func TestApplySelectedModelChanges(t *testing.T) {
  40. t.Run("add and remove together", func(t *testing.T) {
  41. result := applySelectedModelChanges(
  42. []string{"gpt-4o", "gpt-4.1", "claude-3"},
  43. []string{"gpt-4.1-mini"},
  44. []string{"claude-3"},
  45. )
  46. require.Equal(t, []string{"gpt-4o", "gpt-4.1", "gpt-4.1-mini"}, result)
  47. })
  48. t.Run("add wins when conflict with remove", func(t *testing.T) {
  49. result := applySelectedModelChanges(
  50. []string{"gpt-4o"},
  51. []string{"gpt-4.1"},
  52. []string{"gpt-4.1"},
  53. )
  54. require.Equal(t, []string{"gpt-4o", "gpt-4.1"}, result)
  55. })
  56. }
  57. func TestCollectPendingApplyUpstreamModelChanges(t *testing.T) {
  58. settings := dto.ChannelOtherSettings{
  59. UpstreamModelUpdateLastDetectedModels: []string{" gpt-4o ", "gpt-4o", "gpt-4.1"},
  60. UpstreamModelUpdateLastRemovedModels: []string{" old-model ", "", "old-model"},
  61. }
  62. pendingAddModels, pendingRemoveModels := collectPendingApplyUpstreamModelChanges(settings)
  63. require.Equal(t, []string{"gpt-4o", "gpt-4.1"}, pendingAddModels)
  64. require.Equal(t, []string{"old-model"}, pendingRemoveModels)
  65. }
  66. func TestNormalizeChannelModelMapping(t *testing.T) {
  67. modelMapping := `{
  68. " alias-model ": " upstream-model ",
  69. "": "invalid",
  70. "invalid-target": ""
  71. }`
  72. channel := &model.Channel{
  73. ModelMapping: &modelMapping,
  74. }
  75. result := normalizeChannelModelMapping(channel)
  76. require.Equal(t, map[string]string{
  77. "alias-model": "upstream-model",
  78. }, result)
  79. }
  80. func TestCollectPendingUpstreamModelChangesFromModels_WithModelMapping(t *testing.T) {
  81. pendingAddModels, pendingRemoveModels := collectPendingUpstreamModelChangesFromModels(
  82. []string{"alias-model", "gpt-4o", "stale-model"},
  83. []string{"gpt-4o", "gpt-4.1", "mapped-target"},
  84. []string{"gpt-4.1"},
  85. map[string]string{
  86. "alias-model": "mapped-target",
  87. },
  88. )
  89. require.Equal(t, []string{}, pendingAddModels)
  90. require.Equal(t, []string{"stale-model"}, pendingRemoveModels)
  91. }
  92. func TestBuildUpstreamModelUpdateTaskNotificationContent_OmitOverflowDetails(t *testing.T) {
  93. channelSummaries := make([]upstreamModelUpdateChannelSummary, 0, 12)
  94. for i := 0; i < 12; i++ {
  95. channelSummaries = append(channelSummaries, upstreamModelUpdateChannelSummary{
  96. ChannelName: "channel-" + string(rune('A'+i)),
  97. AddCount: i + 1,
  98. RemoveCount: i,
  99. })
  100. }
  101. content := buildUpstreamModelUpdateTaskNotificationContent(
  102. 24,
  103. 12,
  104. 56,
  105. 21,
  106. 9,
  107. []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
  108. channelSummaries,
  109. []string{
  110. "gpt-4.1", "gpt-4.1-mini", "o3", "o4-mini", "gemini-2.5-pro", "claude-3.7-sonnet",
  111. "qwen-max", "deepseek-r1", "llama-3.3-70b", "mistral-large", "command-r-plus", "doubao-pro-32k",
  112. "hunyuan-large",
  113. },
  114. []string{
  115. "gpt-3.5-turbo", "claude-2.1", "gemini-1.5-pro", "mixtral-8x7b", "qwen-plus", "glm-4",
  116. "yi-large", "moonshot-v1", "doubao-lite",
  117. },
  118. )
  119. require.Contains(t, content, "其余 4 个渠道已省略")
  120. require.Contains(t, content, "其余 1 个已省略")
  121. require.Contains(t, content, "失败渠道 ID(展示 10/12)")
  122. require.Contains(t, content, "其余 2 个已省略")
  123. }
  124. func TestShouldSendUpstreamModelUpdateNotification(t *testing.T) {
  125. channelUpstreamModelUpdateNotifyState.Lock()
  126. channelUpstreamModelUpdateNotifyState.lastNotifiedAt = 0
  127. channelUpstreamModelUpdateNotifyState.lastChangedChannels = 0
  128. channelUpstreamModelUpdateNotifyState.lastFailedChannels = 0
  129. channelUpstreamModelUpdateNotifyState.Unlock()
  130. baseTime := int64(2000000)
  131. require.True(t, shouldSendUpstreamModelUpdateNotification(baseTime, 6, 0))
  132. require.False(t, shouldSendUpstreamModelUpdateNotification(baseTime+3600, 6, 0))
  133. require.True(t, shouldSendUpstreamModelUpdateNotification(baseTime+3600, 7, 0))
  134. require.False(t, shouldSendUpstreamModelUpdateNotification(baseTime+7200, 7, 0))
  135. require.True(t, shouldSendUpstreamModelUpdateNotification(baseTime+8000, 0, 3))
  136. require.False(t, shouldSendUpstreamModelUpdateNotification(baseTime+9000, 0, 3))
  137. require.True(t, shouldSendUpstreamModelUpdateNotification(baseTime+10000, 0, 4))
  138. require.True(t, shouldSendUpstreamModelUpdateNotification(baseTime+90000, 7, 0))
  139. require.True(t, shouldSendUpstreamModelUpdateNotification(baseTime+90001, 0, 0))
  140. }