fflake33 on Discord asked for the pace as a figure, "+12%" or "−8%"
against an even pace through the window, on the Usage cards and in the
menu bar.
- provider: QuotaWindow.Pace(readAt, now) is the share used (0-100) less
the share of the window gone by, from Bounds (reset less span; a
month's window a calendar month); none for an unlimited window, one
whose span or reset isn't known, or outside it. WithWindowStarts sets
startsAt on copies of the cards for the GUI (the caches' cards are
left alone); /api/usage/quotas and its refresh send it.
- Usage page: "Pace +20%" on the reset line after the wait (quotaPace,
paceLine), its sign turned with the meter in Left mode, amber while
more is used than an even pace would have, what it means in its
tooltip. Past cycles and windows with no known start show none.
- Menu bar: Settings > Menu bar pace (settings.TrayPace, off by default;
hidden on Windows, whose tray shows no text) puts " +12%" beside each
figure ("42% −14%", at most 10 runes, trayRowMax) and "pace −14%" in
the tooltip. KeepOwn keeps it device-local like the other tray
settings; the Settings save keeps it (prefsKeep).
- Strings in en, zh, zh-TW, ja, de. gui-shell.md says it.
Run:
- go test -tags nogui ./internal/provider ./internal/gui ./internal/settings
./internal/usage: ok; ./internal/davsync: ok (now covers TrayPace;
red with TrayPace taken out of KeepOwn). go test ./internal/gui -run
Tray (darwin, with the tray image tests): ok.
- New TestWindowPace, TestWithWindowStarts (input not mutated),
TestTrayUsagePace (off unchanged, used and left, rows, tooltip).
- quota-pace.test.cjs, chromium and webkit: 4 pass; cards in en, zh,
zh-TW, ja, de at 1100 and 420px, used and left, amber, tooltips, no
sideways scroll, a click moves nothing; the Settings row in all five
at both widths, On posts trayPace, a currency save keeps it, no
scroll; hidden with no card and on Windows, shown on Linux. Red with
paceLine not called, with trayPace out of prefsKeep, and with the
Left-mode sign not turned.
- gui-de, gui-ja, gui-zh-tw, i18n-keys: 12 pass; tray-bird 6, quota-holds
2, quota-read-at 4 pass.
- gofmt -l clean; go vet darwin, GOOS=linux -tags nogui, GOOS=windows clean.
Not tried: the real menu bar on a Mac or Linux tray.
69 lines
2.6 KiB
Go
69 lines
2.6 KiB
Go
package gui
|
||
|
||
import (
|
||
"slices"
|
||
"testing"
|
||
"time"
|
||
|
||
"github.com/yetone/magpie/internal/provider"
|
||
)
|
||
|
||
// The menu bar's Pace number (settings.TrayPace, fflake33 on Discord): off,
|
||
// the rows are as they were; on, each window's figure has its Pace beside
|
||
// it, turned where what is left is shown, and the tooltip says it too.
|
||
func TestTrayUsagePace(t *testing.T) {
|
||
now := time.Date(2026, 9, 27, 12, 0, 0, 0, time.UTC)
|
||
in := func(d time.Duration) *time.Time { at := now.Add(d); return &at }
|
||
q := provider.SubscriptionQuota{Provider: "claude", Name: "Claude", User: "a@b.c", Windows: []provider.QuotaWindow{
|
||
// 2h 50m of 5h gone by (56.7%), 42.2% used: 14 points under
|
||
{Name: "5-hour", Used: 42.2, ResetsAt: in(2*time.Hour + 10*time.Minute), Span: 5 * time.Hour},
|
||
// 4 of 7 days gone by (57.1%), 70% used: 13 points over
|
||
{Name: "Weekly", Used: 70, ResetsAt: in(3 * 24 * time.Hour), Span: 7 * 24 * time.Hour},
|
||
}}
|
||
|
||
label, tip := trayUsageText(q, now, false, false)
|
||
if label != "42% · 70%" || tip != "Claude\n5-hour 42.2% used · resets in 2h 10m\nWeekly 70% used · resets in 3d 0h" {
|
||
t.Errorf("pace off: %q %q", label, tip)
|
||
}
|
||
|
||
label, tip = trayUsageText(q, now, false, true)
|
||
if label != "42% −14% · 70% +13%" {
|
||
t.Errorf("used label %q", label)
|
||
}
|
||
if want := "Claude\n5-hour 42.2% used · pace −14% · resets in 2h 10m\nWeekly 70% used · pace +13% · resets in 3d 0h"; tip != want {
|
||
t.Errorf("used tip %q, want %q", tip, want)
|
||
}
|
||
|
||
label, tip = trayUsageText(q, now, true, true)
|
||
if label != "58% +14% · 30% −13%" {
|
||
t.Errorf("left label %q", label)
|
||
}
|
||
if want := "Claude\n5-hour 57.8% left · pace +14% · resets in 2h 10m\nWeekly 30% left · pace −13% · resets in 3d 0h"; tip != want {
|
||
t.Errorf("left tip %q, want %q", tip, want)
|
||
}
|
||
|
||
// each row still fits the menu bar whole, at its widest
|
||
cells, _, _ := trayUsageView([]provider.SubscriptionQuota{q}, now, false, true)
|
||
if len(cells) != 1 || !slices.Equal(cells[0].Rows, []string{"42% −14%", "70% +13%"}) {
|
||
t.Fatalf("cells %+v", cells)
|
||
}
|
||
if r := trayRow("100% −100%"); r != "100% −100%" {
|
||
t.Errorf("widest row cut: %q", r)
|
||
}
|
||
|
||
// a window whose start can't be known, or with no limit, has no Pace
|
||
q.Windows = []provider.QuotaWindow{
|
||
{Name: "Credits", Used: 40, ResetsAt: in(time.Hour)},
|
||
{Name: "Daily", Used: 0, Unlimited: true, ResetsAt: in(time.Hour)},
|
||
}
|
||
if label, _ := trayUsageText(q, now, false, true); label != "40% · 0%" {
|
||
t.Errorf("unknown span %q", label)
|
||
}
|
||
|
||
for p, want := range map[float64]string{12.4: "+12%", -8: "−8%", 0.4: "±0%", -0.4: "±0%"} {
|
||
if got := trayPace(p, false); got != want {
|
||
t.Errorf("trayPace(%v) = %q, want %q", p, got, want)
|
||
}
|
||
}
|
||
}
|