Files
magpie/internal/provider/window_pace_test.go
T
yetone 3a7d0314a9 usage: a window's Pace as a number on its card, and in the menu bar when asked (fflake33)
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.
2026-10-14 13:02:48 +08:00

86 lines
3.3 KiB
Go

package provider
import (
"math"
"testing"
"time"
)
func TestWindowPace(t *testing.T) {
now := time.Date(2026, 9, 27, 12, 0, 0, 0, time.UTC)
at := func(d time.Duration) *time.Time { t := now.Add(d); return &t }
// half of a 10-hour window gone by: 62% used is 12 points over, 42% 8 under
w := QuotaWindow{Name: "10h", Used: 62, ResetsAt: at(5 * time.Hour), Span: 10 * time.Hour}
if p, ok := w.Pace(now, now); !ok || math.Abs(p-12) > 1e-9 {
t.Errorf("over: %v %v", p, ok)
}
w.Used = 42
if p, ok := w.Pace(now, now); !ok || math.Abs(p+8) > 1e-9 {
t.Errorf("under: %v %v", p, ok)
}
// the span from the window's name when the vendor gave none
if p, ok := (QuotaWindow{Name: "Weekly", Used: 50, ResetsAt: at(84 * time.Hour)}).Pace(now, now); !ok || math.Abs(p) > 1e-9 {
t.Errorf("weekly: %v %v", p, ok)
}
// a reset given as seconds counts from when the card was read
readAt := now.Add(-time.Hour)
if p, ok := (QuotaWindow{Used: 50, ResetSecs: 5400, Span: 2 * time.Hour}).Pace(readAt, now); !ok || math.Abs(p+25) > 1e-9 {
t.Errorf("reset secs: %v %v", p, ok)
}
// used past the whole counts as the whole
if p, ok := (QuotaWindow{Name: "10h", Used: 130, ResetsAt: at(5 * time.Hour), Span: 10 * time.Hour}).Pace(now, now); !ok || math.Abs(p-50) > 1e-9 {
t.Errorf("clamped: %v %v", p, ok)
}
for name, w := range map[string]QuotaWindow{
"unlimited": {Name: "10h", Unlimited: true, ResetsAt: at(time.Hour), Span: 10 * time.Hour},
"no reset": {Name: "Weekly", Used: 10},
"no span": {Name: "Credits", Used: 10, ResetsAt: at(time.Hour)},
"already over": {Name: "10h", Used: 10, ResetsAt: at(-time.Minute), Span: 10 * time.Hour},
"not begun": {Name: "1h", Used: 10, ResetsAt: at(2 * time.Hour), Span: time.Hour},
} {
if p, ok := w.Pace(now, now); ok {
t.Errorf("%s: pace %v", name, p)
}
}
}
func TestWithWindowStarts(t *testing.T) {
now := time.Date(2026, 9, 27, 12, 0, 0, 0, time.UTC)
reset := now.Add(2 * time.Hour)
monthEnd := time.Date(2026, 10, 15, 0, 0, 0, 0, time.UTC)
readAt := now.Add(-time.Hour)
qs := []SubscriptionQuota{{Name: "A", ReadAt: &readAt, Windows: []QuotaWindow{
{Name: "5-hour", ResetsAt: &reset, Span: 5 * time.Hour},
{Name: "Monthly", ResetsAt: &monthEnd},
{Name: "Seconds", ResetSecs: 3600, Span: 2 * time.Hour},
{Name: "Credits", ResetsAt: &reset},
{Name: "Daily", Unlimited: true, ResetsAt: &reset},
}}, {Name: "B"}}
got := WithWindowStarts(qs, now)
ws := got[0].Windows
if ws[0].StartsAt == nil || !ws[0].StartsAt.Equal(reset.Add(-5*time.Hour)) {
t.Errorf("5-hour start %v", ws[0].StartsAt)
}
if ws[1].StartsAt == nil || !ws[1].StartsAt.Equal(time.Date(2026, 9, 15, 0, 0, 0, 0, time.UTC)) {
t.Errorf("monthly start %v", ws[1].StartsAt)
}
// ResetSecs counts from when the card was read, not from now
if ws[2].StartsAt == nil || !ws[2].StartsAt.Equal(readAt.Add(-time.Hour)) {
t.Errorf("seconds start %v", ws[2].StartsAt)
}
if ws[3].StartsAt != nil || ws[4].StartsAt != nil {
t.Errorf("unknown span or no limit given a start: %v %v", ws[3].StartsAt, ws[4].StartsAt)
}
if len(got) != 2 || got[1].Name != "B" {
t.Errorf("cards %+v", got)
}
// the caches' own cards are left as they were
for _, w := range qs[0].Windows {
if w.StartsAt != nil {
t.Fatalf("input window %q given a start", w.Name)
}
}
}