feat: publish sanitized custom.97 source

This commit is contained in:
cneicy committed 2026-10-27 20:43:39 +08:00
1 parent 8e6ed0317b
commit 86c0276156
1022 files changed
+69903 -19207

No files matched your search

+49
View File
@@ -2,9 +2,13 @@ package controller
import (
"math"
"net/http"
"net/http/httptest"
"testing"
"github.com/QuantumNous/new-api/common"
"github.com/QuantumNous/new-api/constant"
"github.com/QuantumNous/new-api/model"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@@ -114,3 +118,48 @@ func TestGetDeepSeekBalanceUSD(t *testing.T) {
})
}
}
func TestUpdateChannelNewAPIBalanceUsesTheSmallerRemainingQuota(t *testing.T) {
for _, tc := range []struct {
name string
usage string
want float64
wantErr string
}{
{name: "user quota below the key quota", usage: `{"code":true,"data":{"total_available":2000000,"unlimited_quota":false,"user_quota":{"total_available":500000}}}`, want: 0.5},
{name: "key quota below the user quota", usage: `{"code":true,"data":{"total_available":500000,"unlimited_quota":false,"user_quota":{"total_available":2000000}}}`, want: 0.5},
{name: "unlimited key uses the user quota", usage: `{"code":true,"data":{"total_available":0,"unlimited_quota":true,"user_quota":{"total_available":3000000}}}`, want: 3},
{name: "key quota alone without user quota", usage: `{"code":true,"data":{"total_available":1500000,"unlimited_quota":false}}`, want: 1.5},
{name: "unlimited key without user quota", usage: `{"code":true,"data":{"total_available":0,"unlimited_quota":true}}`, wantErr: "Allow querying user quota"},
{name: "upstream rejects the key", usage: `{"success":false,"message":"invalid token"}`, wantErr: "invalid token"},
} {
t.Run(tc.name, func(t *testing.T) {
setupSub2APIBalanceTest(t)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/api/usage/token/":
assert.Equal(t, "Bearer sk-upstream", r.Header.Get("Authorization"))
_, _ = w.Write([]byte(tc.usage))
case "/api/status":
assert.Empty(t, r.Header.Get("Authorization"), "the key is not sent to the public status endpoint")
_, _ = w.Write([]byte(`{"success":true,"data":{"quota_per_unit":1000000}}`))
default:
http.NotFound(w, r)
}
}))
defer server.Close()
baseURL := server.URL + "/"
channel := &model.Channel{Type: constant.ChannelTypeNewAPI, BaseURL: &baseURL, Key: "sk-upstream", Name: "new-api-upstream"}
require.NoError(t, model.DB.Create(channel).Error)
result, err := updateChannelBalance(channel)
if tc.wantErr != "" {
require.ErrorContains(t, err, tc.wantErr)
return
}
require.NoError(t, err)
assert.InDelta(t, tc.want, result.Balance, 1e-9)
assert.InDelta(t, tc.want, channel.Balance, 1e-9)
})
}
}