topup_waffo_pancake_test.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package controller
  2. import (
  3. "testing"
  4. "github.com/QuantumNous/new-api/common"
  5. "github.com/QuantumNous/new-api/setting"
  6. "github.com/QuantumNous/new-api/setting/operation_setting"
  7. "github.com/stretchr/testify/require"
  8. )
  9. func TestFormatWaffoPancakeAmount_UsesDisplayPriceString(t *testing.T) {
  10. testCases := []struct {
  11. name string
  12. amount float64
  13. expected string
  14. }{
  15. {name: "whole amount", amount: 29, expected: "29.00"},
  16. {name: "decimal amount", amount: 29.9, expected: "29.90"},
  17. {name: "round half up to cents", amount: 29.999, expected: "30.00"},
  18. }
  19. for _, tc := range testCases {
  20. t.Run(tc.name, func(t *testing.T) {
  21. require.Equal(t, tc.expected, formatWaffoPancakeAmount(tc.amount))
  22. })
  23. }
  24. }
  25. func TestGetWaffoPancakePayMoney(t *testing.T) {
  26. originalUnitPrice := setting.WaffoPancakeUnitPrice
  27. originalQuotaDisplayType := operation_setting.GetGeneralSetting().QuotaDisplayType
  28. originalDiscounts := make(map[int]float64, len(operation_setting.GetPaymentSetting().AmountDiscount))
  29. for k, v := range operation_setting.GetPaymentSetting().AmountDiscount {
  30. originalDiscounts[k] = v
  31. }
  32. originalTopupGroupRatio := common.TopupGroupRatio2JSONString()
  33. t.Cleanup(func() {
  34. setting.WaffoPancakeUnitPrice = originalUnitPrice
  35. operation_setting.GetGeneralSetting().QuotaDisplayType = originalQuotaDisplayType
  36. operation_setting.GetPaymentSetting().AmountDiscount = originalDiscounts
  37. require.NoError(t, common.UpdateTopupGroupRatioByJSONString(originalTopupGroupRatio))
  38. })
  39. setting.WaffoPancakeUnitPrice = 2.5
  40. operation_setting.GetPaymentSetting().AmountDiscount = map[int]float64{
  41. 10: 0.8,
  42. int(common.QuotaPerUnit * 3): 0.5,
  43. 20: 0,
  44. }
  45. require.NoError(t, common.UpdateTopupGroupRatioByJSONString(`{"default":1,"vip":1.2}`))
  46. testCases := []struct {
  47. name string
  48. amount int64
  49. group string
  50. quotaDisplayType string
  51. expected float64
  52. }{
  53. {
  54. name: "currency display applies unit price group ratio and discount",
  55. amount: 10,
  56. group: "vip",
  57. quotaDisplayType: operation_setting.QuotaDisplayTypeUSD,
  58. expected: 24,
  59. },
  60. {
  61. name: "tokens display converts quota to display units before pricing",
  62. amount: int64(common.QuotaPerUnit * 3),
  63. group: "vip",
  64. quotaDisplayType: operation_setting.QuotaDisplayTypeTokens,
  65. expected: 4.5,
  66. },
  67. {
  68. name: "non-positive discount falls back to no discount",
  69. amount: 20,
  70. group: "default",
  71. quotaDisplayType: operation_setting.QuotaDisplayTypeUSD,
  72. expected: 50,
  73. },
  74. }
  75. for _, tc := range testCases {
  76. t.Run(tc.name, func(t *testing.T) {
  77. operation_setting.GetGeneralSetting().QuotaDisplayType = tc.quotaDisplayType
  78. actual := getWaffoPancakePayMoney(tc.amount, tc.group)
  79. require.InDelta(t, tc.expected, actual, 0.000001)
  80. })
  81. }
  82. }