Files
magpie/internal/provider/balance_shared_test.go
T
yetone c3b0484a96 usage: keys of one account show its balance once (mimi)
mimi on Discord (#feedback): "同一个网站,添加多个key时,余额会重复展示" -
a site's keys 1-A031-Pro, 2-S009-Plus and 3-S010-Team-2 each showed the
account's $8.39 and its own curve.

KeyBalances still reads, keeps and charts every key on its own. What
changed is what the Usage page draws:
- balanceRead says whether the reading is the account's (balanceAccountWide),
  going by the source, not the vendor's name:
  - account-wide: a balance token (new-api /api/user/self, sub2api
    /api/v1/user/profile, AiHubMix), a known host's account query
    (DeepSeek, Moonshot, OpenRouter credits, SiliconFlow, StepFun, ...),
    and sub2api /v1/usage when its mode is "unrestricted" (wallet or the
    group's subscription, per sub2api's gateway_handler.go Usage);
  - per key: AiHubMix's /dashboard/billing/remain, new-api's
    /api/usage/token, sub2api in quota_limited mode, any other Balance
    URL the user named.
- Two such keys of one provider with the same amount are one account only
  when their balances moved together: the same latest change (from and
  to), first read within balanceHistGap. Two accounts with the same
  amount stay apart until one is spent from; keys whose amounts differ
  are never one. SharedBalance (the first key's name) says so in
  /api/usage/quotas, magpie quota --json and /v1/magpie/quotas.
- Refreshing a shared card reads every key it stands for (sharesAskedCard),
  so it stays one card. The history is read outside the cache's lock.
- The Usage page draws them as one row, one balance, one curve, with
  "+2 keys" beside the key and the keys on hover; the head's sum counts
  the account once. The code is in plugins/usage/page.js, so app.js is 9
  bytes smaller than before; no budget raised.

Run (mote-run, at the pre-rebase base d50f84c9f):
- go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet
  ./...: ok. gofmt -l: clean.
- go test -tags nogui -timeout 45m ./...: every package ok except
  internal/gui's TestCoreScriptBudget (app.js 676 bytes over; fixed by
  moving the code into the Usage plugin, then ok) and internal/update's
  TestRelaunchOpenTriedAgain (TempDir cleanup under load, #3881; 3 of 3 ok
  alone).
- go test -race on internal/provider's balance tests: ok.
- GUI, both engines, en and zh: 122 files (balance-*, usage-*, quota-*,
  tray-*, panel-*, gui-ja/de/zh-tw, i18n-keys, key-balance-card,
  routing-wallet-balance, ...): 1637 pass, 42 fail in 6 files, all the
  same at base a05b37353 run alone and all filed (#3679 quota-refresh-
  order, #3404 quota-count-wrap, #3414 usage-agents-hide, #3415
  usage-fold, #3104 usage-ledger, #3897 i18n-keys "Runs as"). After the
  move into the plugin, 17 of those files again: only #3897's red.

Broken by hand, each red:
- SharedBalance never set (the old code): "after they moved together".
- Shared on equal amount alone: "first reading" ("other" merged).
- Every query taken as account-wide: quota'd and new-api token keys
  merged.
- No sharesAskedCard: the shared card's refresh didn't read sk-plus.
- keyCards without sharedBalances: usage-shared-balance red in all four
  (chromium/webkit x en/zh).

Not tried: a real sub2api or new-api account (fake server with sub2api's
literal replies). A key's balance is shown once only after the account's
next change is seen by every key, not on the first read. Multi-currency
balances (DeepSeek "$ · ¥") and balances with parts are never shared.
2026-10-28 10:52:46 +08:00

184 lines
7.2 KiB
Go

package provider
import (
"context"
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"sync"
"testing"
"time"
"github.com/yetone/magpie/internal/agentenv"
)
// mimi on Discord: a sub2api site's keys on the wallet each told its
// $8.39, and the Usage page showed it once a key. Keys whose balance is
// the account's, and moved with the others', are told as one account's;
// one with the same amount that didn't move with them (another account),
// a key with a quota of its own and a key's own query (new-api's
// /api/usage/token) stay each its own.
func TestKeysOfOneAccountShareItsBalance(t *testing.T) {
isolate(t)
h := t.TempDir()
t.Setenv("HOME", h)
t.Setenv("USERPROFILE", h)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
t.Setenv("PATH", h)
for _, v := range agentenv.Vars {
t.Setenv(v, "")
}
keyBalanceCache.data = nil
t.Cleanup(func() { keyBalanceCache.data = nil })
// the replies as sub2api's /v1/usage gives them (gateway_handler.go's
// usageUnrestricted and usageQuotaLimited), and new-api's
// /api/usage/token
wallet := `{"mode":"unrestricted","isValid":true,"planName":"钱包余额","remaining":8.39,"unit":"USD","balance":8.39}`
own := `{"mode":"quota_limited","isValid":true,"status":"active","quota":{"limit":20,"used":11.61,"remaining":8.39,"unit":"USD"},"remaining":8.39,"unit":"USD"}`
token := `{"code":true,"message":"ok","data":{"object":"token_usage","total_granted":5000000,"total_used":805000,"total_available":4195000,"unlimited_quota":false}}`
var mu sync.Mutex
var asked []string
team := wallet // what the third key reads
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
key := strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ")
mu.Lock()
asked = append(asked, key)
teamReads := team
mu.Unlock()
switch key {
case "sk-team":
w.Write([]byte(teamReads))
case "sk-pro", "sk-plus", "sk-other":
w.Write([]byte(wallet))
case "sk-quota":
w.Write([]byte(own))
case "sk-t1", "sk-t2":
w.Write([]byte(token))
default:
w.WriteHeader(http.StatusUnauthorized)
}
}))
defer srv.Close()
if err := Save(Provider{ID: "site", Name: "Site", Chat: srv.URL + "/v1", Key: "sk-pro", KeyName: "1-A031-Pro",
Keys: []KeyAccount{{Name: "2-S009-Plus", Key: "sk-plus"}, {Name: "3-S010-Team-2", Key: "sk-team"},
{Name: "other", Key: "sk-other"}, {Name: "quota", Key: "sk-quota"}},
BalanceURL: srv.URL + "/v1/usage"}); err != nil {
t.Fatal(err)
}
if err := Save(Provider{ID: "panel", Name: "Panel", Chat: srv.URL + "/v1", Key: "sk-t1", KeyName: "t1",
Keys: []KeyAccount{{Name: "t2", Key: "sk-t2"}}, BalanceURL: srv.URL + "/api/usage/token"}); err != nil {
t.Fatal(err)
}
shared := func() string {
var lines []string
for _, q := range KeyBalances(context.Background()) {
lines = append(lines, q.Provider+"|"+q.User+"|"+q.Balance+"|"+q.SharedBalance)
}
return strings.Join(lines, "\n")
}
// the first reading: every one $8.39, none seen to move yet, so
// none is taken for another's account
first := "site|1-A031-Pro|$8.39|\nsite|2-S009-Plus|$8.39|\nsite|3-S010-Team-2|$8.39|\nsite|other|$8.39|\nsite|quota|$8.39|\npanel|t1|$8.39|\npanel|t2|$8.39|"
if got := shared(); got != first {
t.Fatalf("first reading:\n%s\nwant\n%s", got, first)
}
// an hour before, the three keys of the account and the quota'd key
// and new-api's two read $8.80, the other account $8.39 already: the
// next reading has the three moved together
before := time.Now().Add(-time.Hour).UTC().Truncate(time.Second)
balanceHistMu.Lock()
bh := balanceHist{}
for _, k := range []string{"site|1-A031-Pro", "site|2-S009-Plus", "site|3-S010-Team-2", "site|quota", "panel|t1", "panel|t2"} {
p, u, _ := strings.Cut(k, "|")
bh[quotaHistKey(p, u)] = []BalancePoint{{At: before, Amount: 8.80}}
}
bh[quotaHistKey("site", "other")] = []BalancePoint{{At: before.Add(-time.Hour), Amount: 8.39}, {At: before, Amount: 8.39}}
writeBalanceHist(bh)
balanceHistMu.Unlock()
ForgetBalances()
want := "site|1-A031-Pro|$8.39|1-A031-Pro\nsite|2-S009-Plus|$8.39|1-A031-Pro\nsite|3-S010-Team-2|$8.39|1-A031-Pro\nsite|other|$8.39|\nsite|quota|$8.39|\npanel|t1|$8.39|\npanel|t2|$8.39|"
if got := shared(); got != want {
t.Fatalf("after they moved together:\n%s\nwant\n%s", got, want)
}
// as magpie quota --json and /v1/magpie/quotas tell them
for _, q := range quotaReport(nil, nil, KeyBalances(context.Background()), time.Now()) {
if q.User == "2-S009-Plus" && q.SharedBalance != "1-A031-Pro" {
t.Fatalf("the report's card: %+v", q)
}
}
// the card's refresh reads every key it stands for, so it stays one
mu.Lock()
asked = nil
mu.Unlock()
ctx := context.WithValue(Asked(context.Background()), cardRefreshKey{}, cardRefresh{"site", "1-A031-Pro"})
got := KeyBalances(ctx)
mu.Lock()
keys := strings.Join(asked, ",")
mu.Unlock()
for _, k := range []string{"sk-pro", "sk-plus", "sk-team"} {
if !strings.Contains(keys, k) {
t.Fatalf("the shared card's refresh asked %s, not %s", keys, k)
}
}
if strings.Contains(keys, "sk-other") || strings.Contains(keys, "sk-quota") {
t.Fatalf("the shared card's refresh asked %s", keys)
}
for _, q := range got {
if q.User == "1-A031-Pro" && q.SharedBalance != "1-A031-Pro" {
t.Fatalf("the refreshed card: %+v", q)
}
}
if got := shared(); got != want {
t.Fatalf("after the refresh:\n%s\nwant\n%s", got, want)
}
// the keys apart again: one read another amount, so it is no longer
// the others' account
mu.Lock()
team = `{"mode":"unrestricted","isValid":true,"planName":"钱包余额","remaining":5.00,"unit":"USD","balance":5.00}`
mu.Unlock()
ForgetBalances()
apart := "site|1-A031-Pro|$8.39|1-A031-Pro\nsite|2-S009-Plus|$8.39|1-A031-Pro\nsite|3-S010-Team-2|$5.00|\nsite|other|$8.39|\nsite|quota|$8.39|\npanel|t1|$8.39|\npanel|t2|$8.39|"
if got := shared(); got != apart {
t.Fatalf("one key read another amount:\n%s\nwant\n%s", got, apart)
}
}
// Two balances moved together when their latest change was the same, from
// and to, at the same time; not when either hasn't changed, or changed
// from another amount, or long apart.
func TestBalancesMovedTogether(t *testing.T) {
t0 := time.Date(2026, 10, 28, 8, 0, 0, 0, time.UTC)
pts := func(at time.Time, amounts ...float64) []BalancePoint {
var out []BalancePoint
for i, a := range amounts {
out = append(out, BalancePoint{At: at.Add(time.Duration(i) * time.Hour), Amount: a})
}
return out
}
for _, c := range []struct {
name string
a, b []BalancePoint
want bool
}{
{"the same moves", pts(t0, 9, 8.8, 8.39), pts(t0.Add(3*time.Second), 9, 8.8, 8.39), true},
{"a run of the last amount", pts(t0, 8.8, 8.39, 8.39), pts(t0, 8.8, 8.39), true},
{"a key added later", pts(t0, 9, 8.8, 8.39), pts(t0.Add(time.Hour), 8.8, 8.39), true},
{"never moved", pts(t0, 8.39, 8.39), pts(t0, 8.39, 8.39), false},
{"one never moved", pts(t0, 8.8, 8.39), pts(t0, 8.39, 8.39), false},
{"from another amount", pts(t0, 8.8, 8.39), pts(t0, 8.5, 8.39), false},
{"hours apart", pts(t0, 8.8, 8.39), pts(t0.Add(2*time.Hour), 8.8, 8.39), false},
{"one point each", pts(t0, 8.39), pts(t0, 8.39), false},
} {
if got := movedTogether(c.a, c.b); got != c.want {
t.Errorf("%s: %v, want %v", c.name, got, c.want)
}
}
}