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

68 行
3.0 KiB

  1. package service
  2. import (
  3. "errors"
  4. "net/http"
  5. "testing"
  6. "github.com/QuantumNous/new-api/common"
  7. "github.com/QuantumNous/new-api/constant"
  8. "github.com/QuantumNous/new-api/setting/operation_setting"
  9. "github.com/QuantumNous/new-api/types"
  10. "github.com/stretchr/testify/require"
  11. )
  12. func TestShouldDisableChannelHonorsFeatureFlagAndChannelErrors(t *testing.T) {
  13. original := common.AutomaticDisableChannelEnabled
  14. t.Cleanup(func() { common.AutomaticDisableChannelEnabled = original })
  15. channelErr := types.NewError(errors.New("no usable key"), types.ErrorCodeChannelNoAvailableKey)
  16. common.AutomaticDisableChannelEnabled = false
  17. require.False(t, ShouldDisableChannel(constant.ChannelTypeOpenAI, channelErr))
  18. common.AutomaticDisableChannelEnabled = true
  19. require.False(t, ShouldDisableChannel(constant.ChannelTypeOpenAI, nil))
  20. require.True(t, ShouldDisableChannel(constant.ChannelTypeOpenAI, channelErr))
  21. skipRetry := types.NewError(errors.New("retry later"), types.ErrorCodeBadResponse, types.ErrOptionWithSkipRetry())
  22. require.False(t, ShouldDisableChannel(constant.ChannelTypeOpenAI, skipRetry))
  23. }
  24. func TestShouldDisableChannelRecognizesStatusAndOpenAIErrorRules(t *testing.T) {
  25. originalEnabled := common.AutomaticDisableChannelEnabled
  26. originalRanges := operation_setting.AutomaticDisableStatusCodeRanges
  27. t.Cleanup(func() {
  28. common.AutomaticDisableChannelEnabled = originalEnabled
  29. operation_setting.AutomaticDisableStatusCodeRanges = originalRanges
  30. })
  31. common.AutomaticDisableChannelEnabled = true
  32. operation_setting.AutomaticDisableStatusCodeRanges = []operation_setting.StatusCodeRange{{Start: http.StatusTooManyRequests, End: http.StatusTooManyRequests}}
  33. byStatus := types.NewOpenAIError(errors.New("rate limited"), types.ErrorCodeBadResponse, http.StatusTooManyRequests)
  34. require.True(t, ShouldDisableChannel(constant.ChannelTypeOpenAI, byStatus))
  35. forbidden := types.NewOpenAIError(errors.New("forbidden"), types.ErrorCodeBadResponse, http.StatusForbidden)
  36. require.True(t, ShouldDisableChannel(constant.ChannelTypeGemini, forbidden))
  37. require.False(t, ShouldDisableChannel(constant.ChannelTypeOpenAI, forbidden))
  38. invalidKey := types.WithOpenAIError(types.OpenAIError{
  39. Message: "bad key",
  40. Type: "invalid_request_error",
  41. Code: "invalid_api_key",
  42. }, http.StatusBadRequest)
  43. require.True(t, ShouldDisableChannel(constant.ChannelTypeOpenAI, invalidKey))
  44. }
  45. func TestShouldEnableChannelRequiresAutoDisabledStatusWithoutError(t *testing.T) {
  46. original := common.AutomaticEnableChannelEnabled
  47. t.Cleanup(func() { common.AutomaticEnableChannelEnabled = original })
  48. common.AutomaticEnableChannelEnabled = false
  49. require.False(t, ShouldEnableChannel(nil, common.ChannelStatusAutoDisabled))
  50. common.AutomaticEnableChannelEnabled = true
  51. require.False(t, ShouldEnableChannel(types.NewError(errors.New("still failing"), types.ErrorCodeBadResponse), common.ChannelStatusAutoDisabled))
  52. require.False(t, ShouldEnableChannel(nil, common.ChannelStatusManuallyDisabled))
  53. require.True(t, ShouldEnableChannel(nil, common.ChannelStatusAutoDisabled))
  54. }