channel_test_internal_test.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package controller
  2. import (
  3. "net/http/httptest"
  4. "testing"
  5. "github.com/QuantumNous/new-api/common"
  6. "github.com/QuantumNous/new-api/dto"
  7. "github.com/QuantumNous/new-api/pkg/billingexpr"
  8. relaycommon "github.com/QuantumNous/new-api/relay/common"
  9. "github.com/QuantumNous/new-api/types"
  10. "github.com/gin-gonic/gin"
  11. "github.com/stretchr/testify/require"
  12. )
  13. func TestSettleTestQuotaUsesTieredBilling(t *testing.T) {
  14. info := &relaycommon.RelayInfo{
  15. TieredBillingSnapshot: &billingexpr.BillingSnapshot{
  16. BillingMode: "tiered_expr",
  17. ExprString: `param("stream") == true ? tier("stream", p * 3) : tier("base", p * 2)`,
  18. ExprHash: billingexpr.ExprHashString(`param("stream") == true ? tier("stream", p * 3) : tier("base", p * 2)`),
  19. GroupRatio: 1,
  20. EstimatedTier: "stream",
  21. QuotaPerUnit: common.QuotaPerUnit,
  22. ExprVersion: 1,
  23. },
  24. BillingRequestInput: &billingexpr.RequestInput{
  25. Body: []byte(`{"stream":true}`),
  26. },
  27. }
  28. quota, result := settleTestQuota(info, types.PriceData{
  29. ModelRatio: 1,
  30. CompletionRatio: 2,
  31. }, &dto.Usage{
  32. PromptTokens: 1000,
  33. })
  34. require.Equal(t, 1500, quota)
  35. require.NotNil(t, result)
  36. require.Equal(t, "stream", result.MatchedTier)
  37. }
  38. func TestBuildTestLogOtherInjectsTieredInfo(t *testing.T) {
  39. gin.SetMode(gin.TestMode)
  40. ctx, _ := gin.CreateTestContext(httptest.NewRecorder())
  41. info := &relaycommon.RelayInfo{
  42. TieredBillingSnapshot: &billingexpr.BillingSnapshot{
  43. BillingMode: "tiered_expr",
  44. ExprString: `tier("base", p * 2)`,
  45. },
  46. ChannelMeta: &relaycommon.ChannelMeta{},
  47. }
  48. priceData := types.PriceData{
  49. GroupRatioInfo: types.GroupRatioInfo{GroupRatio: 1},
  50. }
  51. usage := &dto.Usage{
  52. PromptTokensDetails: dto.InputTokenDetails{
  53. CachedTokens: 12,
  54. },
  55. }
  56. other := buildTestLogOther(ctx, info, priceData, usage, &billingexpr.TieredResult{
  57. MatchedTier: "base",
  58. })
  59. require.Equal(t, "tiered_expr", other["billing_mode"])
  60. require.Equal(t, "base", other["matched_tier"])
  61. require.NotEmpty(t, other["expr_b64"])
  62. }