usage: a plugin account told by its balance alone is drawn over time
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.
This commit is contained in:
@@ -5,7 +5,8 @@
|
||||
// "Runs out in 3d at this pace" in the curve's head, with what is spent a
|
||||
// day in its legend. A click on the plot makes it larger, another back, and
|
||||
// neither moves the page; Enter does the same. A key of one reading has no
|
||||
// curve, and the allowances' "Off" takes it away too.
|
||||
// curve, and the allowances' "Off" takes it away too. A plugin account's
|
||||
// balance alone (Raccoon's credits) has the same curve.
|
||||
// English and Chinese, light and dark; no backend, the API is faked here.
|
||||
const assert = require("node:assert/strict");
|
||||
const fs = require("node:fs/promises");
|
||||
@@ -25,6 +26,10 @@ function fixtures(now) {
|
||||
balanceTrend: { points, fitFrom: iso(now - 48 * H), fitStart: 50, fitNow: 30, perDay: 10, runsOut: iso(now + 72 * H) } },
|
||||
{ provider: "moonshot", name: "Moonshot", icon: "moonshot", windows: [], balance: "$12.00", readAt: iso(now),
|
||||
balanceTrend: { points: [{ at: iso(now), amount: 12 }] } },
|
||||
// a plugin's account told by its balance alone, as Raccoon's is when its
|
||||
// bills don't add up to it (corddis_114 on Discord): the same curve
|
||||
{ provider: "raccoon", name: "Raccoon", icon: "raccoon", user: "138****0000", plan: "Free", windows: [], balance: "5900 credits", readAt: iso(now),
|
||||
balanceTrend: { points: [[48, 6122], [24, 6000], [0, 5900]].map(([h, v]) => ({ at: iso(now - h * H), amount: v })) } },
|
||||
];
|
||||
}
|
||||
|
||||
@@ -98,6 +103,9 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium",
|
||||
assert.ok(x1 > 50 && x1 < 150 && y1 < 10, `they start at the top-up: ${x1},${y1}`);
|
||||
assert.ok(await page.locator("#quotaHead #quotaTrend").isVisible(), "the range pick is there for balances alone too");
|
||||
assert.equal(await page.locator(".subscription-card", { hasText: "Moonshot" }).locator(".balance-curve").count(), 0, "one reading, no curve");
|
||||
const wallet = page.locator(".subscription-card", { hasText: "Raccoon" }).locator(".balance-curve");
|
||||
assert.equal(await wallet.count(), 1, "an account's balance alone has its curve too");
|
||||
assert.equal(await wallet.locator("path.qc-line").count(), 1);
|
||||
assert.equal(await page.locator(".quota-curve select, .quota-curve button").count(), 0, "no control on the card");
|
||||
const left = await curve.evaluate((e) => [...e.querySelectorAll("*")].filter((x) => parseFloat(getComputedStyle(x).borderLeftWidth) > 0).length);
|
||||
assert.equal(left, 0, "no left borders");
|
||||
|
||||
@@ -304,3 +304,32 @@ func balanceTrend(pts []BalancePoint, now time.Time) *BalanceTrend {
|
||||
}
|
||||
|
||||
func round4(v float64) float64 { return math.Round(v*1e4) / 1e4 }
|
||||
|
||||
// noteCardHistory keeps what an account's cards among qs tell over time:
|
||||
// each window's share (noteQuotaHistory) and, for a card that tells a
|
||||
// balance and no window, its balance, with the trend set on that card as
|
||||
// KeyBalances sets it on a key's. A plugin's account told by a balance
|
||||
// alone (Raccoon's credits when its bills don't add up to the balance)
|
||||
// had no curve on the Usage page, where every other card has one
|
||||
// (corddis_114 on Discord). A card with windows keeps its windows' curve
|
||||
// alone.
|
||||
func noteCardHistory(qs []SubscriptionQuota, now time.Time) {
|
||||
noteQuotaHistory(qs, now)
|
||||
var idx []int
|
||||
for i, q := range qs {
|
||||
if q.Balance != "" && len(q.Windows) == 0 && len(q.BalanceParts) == 0 {
|
||||
idx = append(idx, i)
|
||||
}
|
||||
}
|
||||
if len(idx) == 0 {
|
||||
return
|
||||
}
|
||||
sub := make([]SubscriptionQuota, len(idx))
|
||||
for j, i := range idx {
|
||||
sub[j] = qs[i]
|
||||
}
|
||||
noteBalanceHistory(sub, now)
|
||||
for j, i := range idx {
|
||||
qs[i].BalanceTrend = sub[j].BalanceTrend
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/yetone/magpie/internal/plugin"
|
||||
"github.com/yetone/magpie/internal/testenv"
|
||||
)
|
||||
|
||||
@@ -169,3 +170,49 @@ func TestBalanceHistoryTwoCurrencies(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,7 +153,7 @@ func refreshCards(ctx context.Context, provider, user string) (subs, plans, bala
|
||||
out := fetchSubscriptionUsage(ctx)
|
||||
now := time.Now()
|
||||
noteDailyCredits(out, now)
|
||||
noteQuotaHistory(out, now)
|
||||
noteCardHistory(out, now)
|
||||
c := &subscriptionUsageCache
|
||||
c.Lock()
|
||||
c.data = cacheCards(c.data, out, seq, true)
|
||||
|
||||
@@ -331,7 +331,7 @@ func SubscriptionUsage(ctx context.Context) []SubscriptionQuota {
|
||||
go func() {
|
||||
out := fetchSubscriptionUsage(readCtx)
|
||||
noteDailyCredits(out, time.Now())
|
||||
noteQuotaHistory(out, time.Now())
|
||||
noteCardHistory(out, time.Now())
|
||||
c.Lock()
|
||||
c.data = cacheCards(c.data, out, seq, false)
|
||||
c.at, c.pending = time.Now(), nil
|
||||
|
||||
Reference in New Issue
Block a user