Sync production source including routing, billing, Astra detection and public uptime. Update custom build instructions and license packaging.
117 lines
3.9 KiB
Go
117 lines
3.9 KiB
Go
package controller
|
|
|
|
import (
|
|
"math"
|
|
"testing"
|
|
|
|
"github.com/QuantumNous/new-api/common"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestGetDeepSeekBalanceUSD(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
responseJSON string
|
|
usdExchangeRate float64
|
|
want float64
|
|
wantErrContains string
|
|
}{
|
|
{
|
|
name: "prefers USD when USD precedes CNY",
|
|
responseJSON: `{"balance_infos":[{"currency":"USD","total_balance":"12.50"},{"currency":"CNY","total_balance":"73.00"}]}`,
|
|
usdExchangeRate: 7.3,
|
|
want: 12.5,
|
|
},
|
|
{
|
|
name: "prefers USD when CNY precedes USD",
|
|
responseJSON: `{"balance_infos":[{"currency":"CNY","total_balance":"73.00"},{"currency":"USD","total_balance":"12.50"}]}`,
|
|
usdExchangeRate: 7.3,
|
|
want: 12.5,
|
|
},
|
|
{
|
|
name: "converts CNY when USD is absent",
|
|
responseJSON: `{"balance_infos":[{"currency":"CNY","total_balance":"73.00"}]}`,
|
|
usdExchangeRate: 7.3,
|
|
want: 10,
|
|
},
|
|
{
|
|
name: "returns error when USD and CNY are absent",
|
|
responseJSON: `{"balance_infos":[{"currency":"EUR","total_balance":"10.00"}]}`,
|
|
usdExchangeRate: 7.3,
|
|
wantErrContains: "currency USD or CNY not found",
|
|
},
|
|
{
|
|
name: "returns USD parse error instead of falling back to CNY",
|
|
responseJSON: `{"balance_infos":[{"currency":"USD","total_balance":"invalid"},{"currency":"CNY","total_balance":"73.00"}]}`,
|
|
usdExchangeRate: 7.3,
|
|
wantErrContains: "invalid syntax",
|
|
},
|
|
{
|
|
name: "rejects NaN USD balance",
|
|
responseJSON: `{"balance_infos":[{"currency":"USD","total_balance":"NaN"}]}`,
|
|
usdExchangeRate: 7.3,
|
|
wantErrContains: "USD balance must be finite",
|
|
},
|
|
{
|
|
name: "rejects negative USD balance",
|
|
responseJSON: `{"balance_infos":[{"currency":"USD","total_balance":"-1.00"}]}`,
|
|
usdExchangeRate: 7.3,
|
|
wantErrContains: "USD balance must be non-negative",
|
|
},
|
|
{
|
|
name: "rejects positive infinity CNY balance",
|
|
responseJSON: `{"balance_infos":[{"currency":"CNY","total_balance":"+Inf"}]}`,
|
|
usdExchangeRate: 7.3,
|
|
wantErrContains: "CNY balance must be finite",
|
|
},
|
|
{
|
|
name: "rejects negative CNY balance",
|
|
responseJSON: `{"balance_infos":[{"currency":"CNY","total_balance":"-7.30"}]}`,
|
|
usdExchangeRate: 7.3,
|
|
wantErrContains: "CNY balance must be non-negative",
|
|
},
|
|
{
|
|
name: "returns error for non-positive CNY exchange rate",
|
|
responseJSON: `{"balance_infos":[{"currency":"CNY","total_balance":"73.00"}]}`,
|
|
usdExchangeRate: 0,
|
|
wantErrContains: "USD exchange rate must be greater than zero",
|
|
},
|
|
{
|
|
name: "rejects NaN CNY exchange rate",
|
|
responseJSON: `{"balance_infos":[{"currency":"CNY","total_balance":"73.00"}]}`,
|
|
usdExchangeRate: math.NaN(),
|
|
wantErrContains: "USD exchange rate must be finite",
|
|
},
|
|
{
|
|
name: "rejects positive infinity CNY exchange rate",
|
|
responseJSON: `{"balance_infos":[{"currency":"CNY","total_balance":"73.00"}]}`,
|
|
usdExchangeRate: math.Inf(1),
|
|
wantErrContains: "USD exchange rate must be finite",
|
|
},
|
|
{
|
|
name: "rejects CNY conversion overflow",
|
|
responseJSON: `{"balance_infos":[{"currency":"CNY","total_balance":"1.7976931348623157e+308"}]}`,
|
|
usdExchangeRate: math.SmallestNonzeroFloat64,
|
|
wantErrContains: "converted USD balance must be finite",
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
var response DeepSeekUsageResponse
|
|
require.NoError(t, common.Unmarshal([]byte(test.responseJSON), &response))
|
|
|
|
balance, err := getDeepSeekBalanceUSD(response, test.usdExchangeRate)
|
|
if test.wantErrContains != "" {
|
|
require.Error(t, err)
|
|
assert.Contains(t, err.Error(), test.wantErrContains)
|
|
return
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
assert.InDelta(t, test.want, balance, 1e-12)
|
|
})
|
|
}
|
|
}
|