Files
magpie/internal/gateway/kimi_booster_test.go
T
yetone aa3d11d5b7 kimi: a key's booster wallet is read, and a key spending it isn't rested to the month (#5721)
apple_test_src on X (#5721): a Kimi Code key at 98.85% of its month was
rested until the month renews, while Kimi still served it from its
booster wallet. magpie never read the wallet.

readKimiCode now reads booster_wallet (or boosterWallet). A BOOSTER wallet
with an amount over 0 adds an "Extra usage" row: amountLeft, in cents x 1e6,
in the currency of monthlyChargeLimit, else monthlyUsed, else USD. When
something is left, the plan windows are marked Credits. The gateway's full()
then doesn't count a key on its booster as full at the month's 98%. A 429
quota refusal rests it as any refusal does, not until the month renews.
A wallet that is empty, of another type or missing changes nothing.

Tests: TestReadKimiCodeBooster runs on the reporter's literal body
(testdata/kimi-booster-5721.json, where 0 is left: no Credits) and on the
same body with amountLeft injected (snake and camel case). A GIFT wallet
and amount 0 add nothing. TestKimiKeyOnItsBoosterIsNotRestedToTheMonth:
a key with booster left isn't full and its 429 rests it under 24h; a
spent key is still rested by its window.

Run on mote: gofmt -l clean; GOOS=linux and GOOS=windows go vet of
internal/provider and internal/gateway; go test ./internal/provider
./internal/gateway -count=1 -timeout 45m: both ok (154.9s, 951.7s).
Broken by hand: routing.go reverted turns the gateway test red ("a key
on its booster counts as full at the month's 98%"); dropping the Credits
marking turns the provider test red.
Not tried: a real Kimi key with booster balance left (the reporter's
body has 0 left).
2026-11-09 05:06:48 +08:00

46 lines
1.9 KiB
Go

package gateway
import (
"net/http"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// A Kimi Code key whose month is at 98.85% goes on on its booster while
// the booster holds money (#5721): a quota refusal rests it the quota rest,
// not until the month renews 17 days on. With the booster used up, the
// month counts as full as before (the rest held to its longest).
func TestKimiKeyOnItsBoosterIsNotRestedToTheMonth(t *testing.T) {
forgetRouting()
now := time.Now()
month := now.Add(17 * 24 * time.Hour)
windows := func(credits bool) provider.Allowance {
return provider.Allowance{
{Name: "5 hours", Used: 0, Resets: now.Add(80 * time.Minute), Span: 5 * time.Hour, Credits: credits},
{Name: "Month", Used: 98.85, Resets: month, Span: 30 * 24 * time.Hour, Credits: credits},
}
}
keyWindows(t, map[string]provider.Allowance{"sk-left": windows(true), "sk-spent": windows(false)})
key := func(k string) candidate {
return candidate{p: provider.Provider{ID: "kimi-code", Key: k}, model: "kimi-for-coding", rest: "kimi-code#" + k}
}
s := &Server{}
refused := []byte(`{"error":{"message":"You've reached your usage limit","type":"rate_limit_error"}}`)
if !key("sk-left").full(now).IsZero() {
t.Fatal("a key on its booster counts as full at the month's 98%")
}
if r := s.restAfter(key("sk-left"), 429, http.Header{}, refused); r.By == "window" || !r.Until.Before(now.Add(24*time.Hour)) {
t.Fatalf("a key on its booster rests %v by %s (%s)", time.Until(r.Until), r.By, r.Why)
}
if !key("sk-spent").full(now).Equal(month) {
t.Fatal("a key with its booster used up isn't full at the month's 98%")
}
if r := s.restAfter(key("sk-spent"), 429, http.Header{}, refused); r.Why != failQuota || r.By != "window" || r.Until.Before(now.Add(7*24*time.Hour)) {
t.Fatalf("a key with its booster used up rests %v by %s (%s)", time.Until(r.Until), r.By, r.Why)
}
}