The Usage page's credits a day (#568) counted WorkBuddy alone: its allow-list named the provider, and it read one "Credits" window's display. moxuanchen asked for it on every subscription that sells credits, and tnt1223 asked again on Discord. A window is now counted by what it is, not whose it is: a meter whose unit or name says credits (credits, 积分, 積分, 点数) or money (usd/$, cny/¥, eur/€), with a used and a total, read from the plugin's amount/limit, or else from its display ("x / y", units, currency signs, thousands separators, compact k/M/万, e-notation, a trailing "· note" cut). A share alone, a text row, an unlimited, family or aside window (when the card has another), a card with an error or an old reading is not counted; of several same-unit windows over a span (OpenDesign's 5 hours and Week) the longest is. Each window keeps its own ledger, keyed provider|user|window, so Qoder's Credits and Add-on credits don't mix; the card carries daily (the first) and moreDaily (the rest), each with its window and, for money, its unit. WorkBuddy's ledger under the old provider|user key still shows, and moves to its Credits window's key on the next reading. A total that drops counts nothing that day, never a negative. GUI: a card shows a row per pool, named after the window when it has several; money says "Spent per day" in the unit's currency. Strings in zh, zh-TW, ja and de. Not changed: API-key plan cards (PlanQuotas) aren't passed through the ledger; ZCode's buckets carry no unit, so only one named "Credits" counts. Run: - go test ./internal/gui ./internal/usageapi (-timeout 45m): ok. ./internal/provider dies in TestUnreadLoginsWriteErrorsKeepCredentials with "close of synctest channel from outside bubble" (#3632, red on origin/main, filememo.Forget); with that test skipped: ok. New TestCreditWindowShapes (22 plugin window shapes from the plugins repo's packages, plus Copilot's premium requests not counted), TestDailyCreditsEveryVendor (Trae CN with no display, Qoder's two pools, CodeArts' aside buckets, OpenDesign's spans, resets, errors, Command Code's dollars), TestDailyCreditsKeptBefore (an old WorkBuddy ledger shown before and after a reading, then moved). - GUI credit-days.test.cjs, chromium and webkit: the WorkBuddy cases and a new one at 360px in en, zh, zh-TW, ja, de (Trae CN, Qoder's two pools named, Cursor's On-demand spent in $, no sideways scroll); i18n-keys, gui-zh-tw, gui-ja, gui-de, quota-pools, quota-credits, plan-credits, usage-accounts, panel-usage, quota-name-translated, credits-run-out: 57/57 ok. - gofmt -l clean; go vet darwin, GOOS=linux/windows -tags nogui ok. - Real use: magpie built from this tree in a sandbox HOME, the fake plugin (a copy of internal/plugin/testdata/fake) signed in with a key, two windows read from a file: Credits 100/1000 then 137.5/1000, and Add-on credits "5 / 50 credits" then "9 / 50 credits". The card showed "37.5 today" and "4 today", one row per pool, at 900px and 360px; credits-daily.json held fakeco|api key|Credits and fakeco|api key|Add-on credits. Credits then dropped to 2/1000: the day stayed 37.5. Broken by hand: with origin/main's credits_daily.go the three new Go tests and TestDailyCredits fail (14 shapes "counted false", [null] days, the old key not moved); with origin/main's app.js the new GUI case fails in all five languages. Not tried: a real account of any vendor other than the fake plugin; Windows and Linux GUIs. Reported by moxuanchen.
106 lines
4.3 KiB
Go
106 lines
4.3 KiB
Go
package provider
|
|
|
|
import (
|
|
"os"
|
|
"reflect"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/testenv"
|
|
)
|
|
|
|
// wbCard is a WorkBuddy card as the built-in and its plugin both make it:
|
|
// one Credits window, "used / granted".
|
|
func wbCard(provider, user string, used, total float64) SubscriptionQuota {
|
|
return SubscriptionQuota{Provider: provider, Name: "WorkBuddy", User: user, Plan: "Free",
|
|
Windows: []QuotaWindow{{Name: "Credits", Used: 100 * used / total, Display: compactNumber(used) + " / " + compactNumber(total)}}}
|
|
}
|
|
|
|
// shareCard is a card whose one window tells a share alone (Cursor's,
|
|
// Grok's): no count, nothing to count by the day.
|
|
func shareCard(provider, user string, used float64) SubscriptionQuota {
|
|
return SubscriptionQuota{Provider: provider, User: user, Windows: []QuotaWindow{{Name: "Cursor Models", Used: used}}}
|
|
}
|
|
|
|
func dailyOf(t *testing.T, qs []SubscriptionQuota, user string) *DailyCredits {
|
|
t.Helper()
|
|
for _, q := range qs {
|
|
if q.User == user {
|
|
return q.Daily
|
|
}
|
|
}
|
|
t.Fatalf("no card of %s", user)
|
|
return nil
|
|
}
|
|
|
|
// TestDailyCredits: each reading counts what was used since the one
|
|
// before on its own day; a cycle started again, a pack gone or added,
|
|
// an error and a reading kept from before count nothing, and the days
|
|
// come back with the card, oldest first (#568).
|
|
func TestDailyCredits(t *testing.T) {
|
|
testenv.SetHome(t, t.TempDir())
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
day := func(d, h int) time.Time { return time.Date(2026, 10, d, h, 0, 0, 0, time.Local) }
|
|
steps := []struct {
|
|
at time.Time
|
|
used float64
|
|
tot float64
|
|
}{
|
|
{day(1, 9), 100, 5600}, // the first reading: counting starts here
|
|
{day(1, 10), 112.5, 5600}, // 12.5
|
|
{day(1, 23), 130.25, 5600},
|
|
{day(2, 8), 130.25, 5600}, // nothing used
|
|
{day(2, 9), 160.25, 5650}, // 30, and a check-in's 50 granted
|
|
{day(3, 1), 4, 5600}, // the cycle started again: not counted
|
|
{day(3, 2), 10, 5600}, // 6
|
|
{day(5, 12), 70, 5600}, // 60 while magpie wasn't reading: on the 5th
|
|
}
|
|
for _, s := range steps {
|
|
noteDailyCredits([]SubscriptionQuota{wbCard("workbuddy", "李雷", s.used, s.tot)}, s.at)
|
|
}
|
|
// another account, the AI build's through its plugin; an error and a
|
|
// card kept from before count nothing; a window telling a share alone
|
|
// isn't counted
|
|
noteDailyCredits([]SubscriptionQuota{wbCard("workbuddy-ai", "138****5678", 10, 3000)}, day(4, 9))
|
|
noteDailyCredits([]SubscriptionQuota{wbCard("workbuddy-ai", "138****5678", 15, 3000)}, day(4, 10))
|
|
errd := wbCard("workbuddy-ai", "138****5678", 999, 3000)
|
|
errd.Error = "boom"
|
|
asOf := day(4, 11)
|
|
kept := wbCard("workbuddy-ai", "138****5678", 900, 3000)
|
|
kept.AsOf = &asOf
|
|
noteDailyCredits([]SubscriptionQuota{errd, kept, shareCard("cursor", "x", 50)}, day(4, 12))
|
|
noteDailyCredits([]SubscriptionQuota{wbCard("workbuddy-ai", "138****5678", 18, 3000)}, day(4, 13))
|
|
|
|
cards := []SubscriptionQuota{wbCard("workbuddy", "李雷", 70, 5600), wbCard("workbuddy-ai", "138****5678", 18, 3000), shareCard("cursor", "x", 50)}
|
|
got := withDailyCredits(cards, day(5, 13))
|
|
want := &DailyCredits{Since: "2026-10-01", Days: []DayCredits{{"2026-10-01", 30.25}, {"2026-10-02", 30}, {"2026-10-03", 6}, {"2026-10-05", 60}}, Window: "Credits"}
|
|
if d := dailyOf(t, got, "李雷"); !reflect.DeepEqual(d, want) {
|
|
t.Errorf("WorkBuddy's days: %+v, want %+v", d, want)
|
|
}
|
|
want = &DailyCredits{Since: "2026-10-04", Days: []DayCredits{{"2026-10-04", 8}}, Window: "Credits"}
|
|
if d := dailyOf(t, got, "138****5678"); !reflect.DeepEqual(d, want) {
|
|
t.Errorf("WorkBuddy AI's days: %+v, want %+v", d, want)
|
|
}
|
|
if d := dailyOf(t, got, "x"); d != nil {
|
|
t.Errorf("a share alone has days: %+v", d)
|
|
}
|
|
if cards[0].Daily != nil {
|
|
t.Error("the cards given were changed")
|
|
}
|
|
// what is kept: two figures a reading and the days' sums, no more
|
|
b, err := os.ReadFile(creditsPath())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(b) > 2000 {
|
|
t.Errorf("the file is %d bytes", len(b))
|
|
}
|
|
// days long gone are dropped from the file at the next reading, and a
|
|
// card isn't told of days older than creditsShown
|
|
noteDailyCredits([]SubscriptionQuota{wbCard("workbuddy", "李雷", 71, 5600)}, day(5, 14).AddDate(1, 2, 0))
|
|
got = withDailyCredits(cards[:1], day(5, 15).AddDate(1, 2, 0))
|
|
if d := dailyOf(t, got, "李雷"); len(d.Days) != 1 || d.Days[0].Used != 1 || d.Since != "2026-10-01" {
|
|
t.Errorf("a year on: %+v", d)
|
|
}
|
|
}
|