corddis_114 on Discord: SenseTime Raccoon's (商汤小浣熊) usage card had no trend chart like the other cards. The Raccoon plugin tells a "Credits" window when its bills add up to the balance, and only `balance: "N credits"` with no window when they don't. A window's share is kept in quota-history.json and drawn; a balance was kept (balance-history.json, balanceTrend) only for API keys' balance cards, from KeyBalances. A subscription or plugin card's balance was never recorded, so such a card had no curve at all. noteCardHistory, called where SubscriptionUsage and a card's refresh note a read, keeps each window's share as before and, for a card with a balance and no windows, its balance, and sets the card's balanceTrend, so the Usage page draws "Balance over time" for it as for a key. A card with windows keeps its windows' curve alone (its balance isn't recorded). The keep-alive's keepCardRead is unchanged (Claude cards have windows). Run (mote, at8e97e26d6+ this change; after the rebase on7661a0aa7, the TestBalance|TestQuotaHist|TestPluginUsage run again ok): - go test -count=1 ./internal/provider/ ok (166s); go vet for darwin, linux and windows on internal/provider ok; gofmt -l clean. - node --test internal/gui/tests/balance-curve.test.cjs: 6/6, chromium and webkit, en and zh; a Raccoon-shaped card (user, plan, windows [], "5900 credits", three points) draws its curve. Broken by hand: noteCardHistory without its noteBalanceHistory call: TestBalanceOnlyCardHasTrend red ("wallet's trend = <nil>"). Not tried: a real Raccoon account (none here); which branch corddis's account takes is not known. An account on the window branch already had a curve. Full suite and GUI suite not run before push.
219 lines
9.3 KiB
Go
219 lines
9.3 KiB
Go
package provider
|
|
|
|
import (
|
|
"math"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/plugin"
|
|
"github.com/yetone/magpie/internal/testenv"
|
|
)
|
|
|
|
func balCard(user, balance string) SubscriptionQuota {
|
|
return SubscriptionQuota{Provider: "deepseek", Name: "DeepSeek", User: user, Windows: []QuotaWindow{}, Balance: balance}
|
|
}
|
|
|
|
// TestBalanceHistoryRecords: each balance read is a point of its amount,
|
|
// by key; readings within five minutes are one point, a run of the same
|
|
// amount its first and last, and a failed balance, one kept from before or
|
|
// one told as several amounts isn't kept. Every card with a balance is
|
|
// given its trend, the stale one too.
|
|
func TestBalanceHistoryRecords(t *testing.T) {
|
|
testenv.SetHome(t, t.TempDir())
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
t0 := time.Date(2026, 10, 3, 8, 0, 0, 0, time.UTC)
|
|
at := func(m int) time.Time { return t0.Add(time.Duration(m) * time.Minute) }
|
|
note := func(m int, qs ...SubscriptionQuota) []SubscriptionQuota {
|
|
noteBalanceHistory(qs, at(m))
|
|
return qs
|
|
}
|
|
note(0, balCard("Main", "¥100.00"))
|
|
note(1, balCard("Main", "¥99.90")) // within 5 min, still spending: moves it
|
|
note(2, balCard("Main", "¥99.80"))
|
|
note(10, balCard("Main", "¥99.00"))
|
|
note(20, balCard("Main", "¥99.00")) // the same: the run's end moves
|
|
note(30, balCard("Main", "¥99.00"))
|
|
bad := balCard("Main", "")
|
|
bad.Error = "401"
|
|
stale := balCard("Main", "¥1.00")
|
|
stale.AsOf = &t0
|
|
parts := balCard("Main", "$5 / ¥30")
|
|
parts.BalanceParts = []BalancePart{{Text: "$5"}, {Text: "¥30"}}
|
|
got := note(40, bad, stale, parts, balCard("Other", "$3"))
|
|
|
|
h := readBalanceHist()
|
|
pts := h[quotaHistKey("deepseek", "main")]
|
|
want := []BalancePoint{{at(0), 100}, {at(2), 99.8}, {at(10), 99}, {at(30), 99}}
|
|
if len(pts) != len(want) {
|
|
t.Fatalf("points = %+v, want %+v", pts, want)
|
|
}
|
|
for i := range want {
|
|
if !pts[i].At.Equal(want[i].At) || pts[i].Amount != want[i].Amount {
|
|
t.Fatalf("point %d = %+v, want %+v", i, pts[i], want[i])
|
|
}
|
|
}
|
|
if o := h[quotaHistKey("deepseek", "other")]; len(o) != 1 || o[0].Amount != 3 {
|
|
t.Fatalf("other key = %+v", o)
|
|
}
|
|
if got[1].BalanceTrend == nil || len(got[1].BalanceTrend.Points) != 4 {
|
|
t.Fatalf("stale card's trend = %+v, want its key's points", got[1].BalanceTrend)
|
|
}
|
|
if got[0].BalanceTrend != nil || got[2].BalanceTrend != nil || got[3].BalanceTrend != nil {
|
|
t.Fatalf("a failed card, one of several amounts and a key of one point have no trend: %+v", got)
|
|
}
|
|
}
|
|
|
|
// TestBalanceTrendRunsOut: a balance spent evenly is fitted by least
|
|
// squares, from the last top-up on, and runs out where the line meets
|
|
// zero; one not spent, or one of too few points or too short a span, has
|
|
// no runs-out.
|
|
func TestBalanceTrendRunsOut(t *testing.T) {
|
|
now := time.Date(2026, 10, 5, 12, 0, 0, 0, time.UTC)
|
|
h := func(n float64) time.Time { return now.Add(time.Duration(-n * float64(time.Hour))) }
|
|
// spent 10 a day for two days, with noise, after a top-up to 50
|
|
pts := []BalancePoint{{h(96), 5}, {h(72), 3}, {h(48), 50}, {h(36), 45.2}, {h(24), 39.8}, {h(12), 35.1}, {h(0), 30}}
|
|
tr := balanceTrend(pts, now)
|
|
if tr == nil || len(tr.Points) != len(pts) {
|
|
t.Fatalf("trend = %+v", tr)
|
|
}
|
|
if !tr.FitFrom.Equal(h(48)) {
|
|
t.Fatalf("fit from %v, want the top-up at %v", tr.FitFrom, h(48))
|
|
}
|
|
if math.Abs(tr.PerDay-10) > 0.3 {
|
|
t.Fatalf("per day = %v, want about 10", tr.PerDay)
|
|
}
|
|
if tr.RunsOut == nil || math.Abs(tr.RunsOut.Sub(now.Add(72*time.Hour)).Hours()) > 3 {
|
|
t.Fatalf("runs out %v, want about 3 days from now", tr.RunsOut)
|
|
}
|
|
if math.Abs(tr.FitNow-30) > 0.5 || math.Abs(tr.FitStart-50) > 0.5 {
|
|
t.Fatalf("fit %v → %v", tr.FitStart, tr.FitNow)
|
|
}
|
|
|
|
flat := []BalancePoint{{h(48), 20}, {h(24), 20}, {h(0), 20}}
|
|
if tr := balanceTrend(flat, now); tr == nil || tr.RunsOut != nil || tr.PerDay != 0 {
|
|
t.Fatalf("a balance not spent: %+v", tr)
|
|
}
|
|
topped := []BalancePoint{{h(48), 20}, {h(24), 10}, {h(0), 60}}
|
|
if tr := balanceTrend(topped, now); tr == nil || tr.RunsOut != nil || tr.FitFrom != nil {
|
|
t.Fatalf("just topped up: %+v", tr)
|
|
}
|
|
short := []BalancePoint{{now.Add(-20 * time.Minute), 10}, {now.Add(-10 * time.Minute), 9}, {now, 8}}
|
|
if tr := balanceTrend(short, now); tr == nil || tr.RunsOut != nil {
|
|
t.Fatalf("within an hour: %+v", tr)
|
|
}
|
|
slow := []BalancePoint{{h(48), 1000}, {h(24), 999.99}, {h(0), 999.98}}
|
|
if tr := balanceTrend(slow, now); tr == nil || tr.RunsOut != nil || tr.PerDay <= 0 {
|
|
t.Fatalf("runs out past a year isn't said: %+v", tr)
|
|
}
|
|
old := []BalancePoint{{now.Add(-20 * 24 * time.Hour), 10}, {h(0), 9}}
|
|
if tr := balanceTrend(old, now); tr != nil {
|
|
t.Fatalf("one point within the span shown: %+v", tr)
|
|
}
|
|
}
|
|
|
|
// TestBalanceHistoryTwoCurrencies (gakki on Discord): DeepSeek tells an
|
|
// account's CNY and USD balances in an order that changes between reads.
|
|
// The card says them in one order every time, the money left first, and
|
|
// the trend is the USD alone, in dollars, not ¥-0.05 and $11.12 joined
|
|
// into one line; the CNY keeps a line of its own. The card's old line,
|
|
// kept before and alternating, goes.
|
|
func TestBalanceHistoryTwoCurrencies(t *testing.T) {
|
|
testenv.SetHome(t, t.TempDir())
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
cny := `{"currency":"CNY","total_balance":"-0.05","granted_balance":"0.00","topped_up_balance":"-0.05"}`
|
|
usd := func(v string) string {
|
|
return `{"currency":"USD","total_balance":"` + v + `","granted_balance":"0.00","topped_up_balance":"` + v + `"}`
|
|
}
|
|
t0 := time.Date(2026, 10, 8, 8, 0, 0, 0, time.UTC)
|
|
key := quotaHistKey("deepseek", "main")
|
|
// what an earlier magpie kept: whichever amount came first
|
|
writeBalanceHist(balanceHist{key: {{t0.Add(-30 * time.Minute), 11.2}, {t0.Add(-20 * time.Minute), -0.05}, {t0.Add(-10 * time.Minute), 11.15}}})
|
|
var last []SubscriptionQuota
|
|
for i, v := range []string{"11.12", "11.10", "11.08", "11.05"} {
|
|
body := `{"is_available":true,"balance_infos":[` + cny + `,` + usd(v) + `]}`
|
|
if i%2 == 1 {
|
|
body = `{"is_available":true,"balance_infos":[` + usd(v) + `,` + cny + `]}`
|
|
}
|
|
bal, err := readDeepSeek([]byte(body))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if want := "$" + v + " · ¥-0.05"; bal != want {
|
|
t.Fatalf("read %d: card says %q, want %q", i, bal, want)
|
|
}
|
|
last = []SubscriptionQuota{balCard("Main", bal)}
|
|
noteBalanceHistory(last, t0.Add(time.Duration(i)*10*time.Minute))
|
|
}
|
|
tr := last[0].BalanceTrend
|
|
if tr == nil || tr.Currency != "$" || len(tr.Points) != 4 {
|
|
t.Fatalf("trend = %+v, want the four USD readings in $", tr)
|
|
}
|
|
for _, p := range tr.Points {
|
|
if p.Amount < 11 {
|
|
t.Fatalf("a CNY amount in the USD line: %+v", tr.Points)
|
|
}
|
|
}
|
|
h := readBalanceHist()
|
|
if c := h[currencyHistKey(key, "¥")]; len(c) != 2 || c[0].Amount != -0.05 {
|
|
t.Fatalf("CNY line = %+v, want its own run of -0.05", c)
|
|
}
|
|
if old, ok := h[key]; ok {
|
|
t.Fatalf("the old alternating line is kept: %+v", old)
|
|
}
|
|
if n, ok := BalanceNumber(last[0].Balance); !ok || n != 11.05 {
|
|
t.Fatalf("an alert reads %v, want the USD's 11.05", n)
|
|
}
|
|
// anything else with " · " in it is one amount, as before
|
|
for _, s := range []string{"$3 · 2 keys", "$1 · $2", "¥12.30", "12 · 13"} {
|
|
if cs := balanceCurrencies(s); cs != nil {
|
|
t.Errorf("%q read as currencies %+v", s, cs)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestBalanceOnlyCardHasTrend: a plugin's account told by a balance alone
|
|
// (Raccoon's "6122 credits" with no window) is drawn over time as a key's
|
|
// balance is (corddis_114 on Discord): each read is a point, and the card
|
|
// read again carries the trend. A card with windows and a balance keeps its
|
|
// windows' curve alone, and a failed read adds no point.
|
|
func TestBalanceOnlyCardHasTrend(t *testing.T) {
|
|
testenv.SetHome(t, t.TempDir())
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
t0 := time.Date(2026, 11, 4, 8, 0, 0, 0, time.UTC)
|
|
read := func(m int, balance string) []SubscriptionQuota {
|
|
wallet := quotaOfPlugin(SubscriptionQuota{Provider: "raccoon", Name: "Raccoon", User: "138****0000"},
|
|
plugin.Usage{Plan: "Free", Balance: balance})
|
|
windowed := quotaOfPlugin(SubscriptionQuota{Provider: "fakeco", Name: "FakeCo", User: "a@fake"},
|
|
plugin.Usage{Balance: "$3", Windows: []plugin.UsageWindow{{Name: "5 hours", Used: fptr(25)}}})
|
|
qs := []SubscriptionQuota{wallet, windowed}
|
|
noteCardHistory(qs, t0.Add(time.Duration(m)*time.Minute))
|
|
return qs
|
|
}
|
|
if got := read(0, "6122 credits"); got[0].BalanceTrend != nil {
|
|
t.Fatalf("one reading's trend = %+v, want none", got[0].BalanceTrend)
|
|
}
|
|
read(30, "6000 credits")
|
|
got := read(60, "5900 credits")
|
|
tr := got[0].BalanceTrend
|
|
if tr == nil || len(tr.Points) != 3 || tr.Points[0].Amount != 6122 || tr.Points[2].Amount != 5900 {
|
|
t.Fatalf("wallet's trend = %+v, want its three readings", tr)
|
|
}
|
|
if got[1].BalanceTrend != nil {
|
|
t.Fatalf("windowed card's balance trend = %+v, want none", got[1].BalanceTrend)
|
|
}
|
|
h := readBalanceHist()
|
|
if pts := h[quotaHistKey("fakeco", "a@fake")]; len(pts) != 0 {
|
|
t.Fatalf("windowed card's balance kept: %+v", pts)
|
|
}
|
|
failed := quotaOfPlugin(SubscriptionQuota{Provider: "raccoon", Name: "Raccoon", User: "138****0000"},
|
|
plugin.Usage{Balance: "1 credits", Error: "401"})
|
|
noteCardHistory([]SubscriptionQuota{failed}, t0.Add(90*time.Minute))
|
|
if pts := readBalanceHist()[quotaHistKey("raccoon", "138****0000")]; len(pts) != 3 {
|
|
t.Fatalf("after a failed read, points = %+v, want the three", pts)
|
|
}
|
|
// the window's share is still kept as before
|
|
if lines := QuotaHistories(t0.Add(-time.Hour), "fakeco", ""); len(lines) != 1 {
|
|
t.Fatalf("windowed card's quota history = %+v", lines)
|
|
}
|
|
}
|