diff --git a/internal/gui/tests/balance-curve.test.cjs b/internal/gui/tests/balance-curve.test.cjs index 49a75ce35..2dc37417f 100644 --- a/internal/gui/tests/balance-curve.test.cjs +++ b/internal/gui/tests/balance-curve.test.cjs @@ -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"); diff --git a/internal/provider/balance_history.go b/internal/provider/balance_history.go index c44b5c2a3..67389efc5 100644 --- a/internal/provider/balance_history.go +++ b/internal/provider/balance_history.go @@ -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 + } +} diff --git a/internal/provider/balance_history_test.go b/internal/provider/balance_history_test.go index 33d63ed5e..b92ea82a9 100644 --- a/internal/provider/balance_history_test.go +++ b/internal/provider/balance_history_test.go @@ -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) + } +} diff --git a/internal/provider/quota_refresh.go b/internal/provider/quota_refresh.go index d9dd1c176..4a566d133 100644 --- a/internal/provider/quota_refresh.go +++ b/internal/provider/quota_refresh.go @@ -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) diff --git a/internal/provider/subscription_usage.go b/internal/provider/subscription_usage.go index 9139d39a4..808f2c2ce 100644 --- a/internal/provider/subscription_usage.go +++ b/internal/provider/subscription_usage.go @@ -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