Files
magpie/internal/gui/trayusage_inuse_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

154 lines
6.4 KiB
Go

package gui
import (
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// StringKe on Discord: the menu bar shows one account's use, picked in
// Settings, not the one in use. A card id "claude|*" follows the account
// the gateway goes to first, whichever that is now, and the tooltip names it.
func TestTrayInUse(t *testing.T) {
inUse := "b@x.y"
was := trayInUseAccount
trayInUseAccount = func(agent string) string {
if agent != "claude" {
t.Errorf("asked about %q", agent)
}
return inUse
}
t.Cleanup(func() { trayInUseAccount = was })
cards := []provider.SubscriptionQuota{
{Provider: "claude", Name: "Claude Code", User: "a@x.y", Windows: []provider.QuotaWindow{{Name: "5-hour", Used: 10}}},
{Provider: "claude", Name: "Claude Code", User: "B@x.y", Windows: []provider.QuotaWindow{{Name: "5-hour", Used: 70}}},
{Provider: "zcode", Name: "ZCode", User: "z@x.y", Windows: []provider.QuotaWindow{{Name: "5 小时", Used: 5}}},
{Provider: "deepseek", Name: "DeepSeek", Balance: "¥1.00"},
}
got := trayPick(cards, []string{"claude|*", "zcode|*", "deepseek", "claude|a@x.y", "gone|*"}, time.Now())
if len(got) != 4 {
t.Fatalf("cards: %+v", got)
}
if got[0].User != "B@x.y" || got[0].Name != "Claude Code · B@x.y" {
t.Errorf("in use: %+v", got[0])
}
if got[1].User != "z@x.y" || got[1].Name != "ZCode · z@x.y" {
t.Errorf("one account: %+v", got[1])
}
if got[2].Name != "DeepSeek" || got[3].User != "a@x.y" || got[3].Name != "Claude Code" {
t.Errorf("named ones as they were: %+v", got[2:])
}
if label, tip := trayUsageText(got[0], time.Now(), false, false); label != "70%" || tip != "Claude Code · B@x.y\n5-hour 70% used" {
t.Errorf("text: %q %q", label, tip)
}
// switched: the card follows
inUse = "a@x.y"
if got = trayPick(cards, []string{"claude|*"}, time.Now()); len(got) != 1 || got[0].User != "a@x.y" {
t.Errorf("after a switch: %+v", got)
}
// can't tell: the first, the one in use first among them
inUse = ""
if got = trayPick(cards, []string{"claude|*"}, time.Now()); len(got) != 1 || got[0].User != "a@x.y" {
t.Errorf("unknown: %+v", got)
}
}
// okingkee on X: three Codex accounts, the menu bar on "account in use".
// A used its allowance up and the gateway went on to B, but the menu bar
// stayed on A: it asked whom the agent is signed in to. It follows the
// account that answered last, while that is lately; else, as before, the
// one InUseLogin reckons the gateway goes to first.
func TestTrayInUseFollowsWhoAnswered(t *testing.T) {
was := trayInUseAccount
trayInUseAccount = func(string) string { return "a@x.y" }
t.Cleanup(func() { trayInUseAccount = was })
now := time.Now()
at := func(d time.Duration) *time.Time { t := now.Add(-d); return &t }
cards := []provider.SubscriptionQuota{
{Provider: "codex", Name: "Codex", User: "a@x.y", Windows: []provider.QuotaWindow{{Name: "5-hour", Used: 100}}, LastServedAt: at(40 * time.Minute)},
{Provider: "codex", Name: "Codex", User: "b@x.y", Windows: []provider.QuotaWindow{{Name: "5-hour", Used: 12}}, LastServedAt: at(time.Minute)},
{Provider: "codex", Name: "Codex", User: "c@x.y", Windows: []provider.QuotaWindow{{Name: "5-hour", Used: 0}}},
}
got := trayPick(cards, []string{"codex|*"}, now)
if len(got) != 1 || got[0].User != "b@x.y" || got[0].Name != "Codex · b@x.y" {
t.Fatalf("in use: %+v", got)
}
if label, _ := trayUsageText(got[0], now, false, false); label != "12%" {
t.Errorf("label %q", label)
}
// nothing answered lately: whom the gateway goes to first
cards[0].LastServedAt, cards[1].LastServedAt = at(3*time.Hour), at(2*time.Hour)
if got = trayPick(cards, []string{"codex|*"}, now); len(got) != 1 || got[0].User != "a@x.y" {
t.Errorf("stale answers: %+v", got)
}
// one account alone is the one, whoever answered
if got = trayPick(cards[2:], []string{"codex|*"}, now); len(got) != 1 || got[0].User != "c@x.y" {
t.Errorf("one: %+v", got)
}
}
// #1516, ikxlpz: a plugin's accounts (moved Kiro, Devin, ...) aren't
// sign-ins InUseLogin knows, so when none answered in the last hour the
// menu bar fell back to the first card, though the gateway had last gone
// to another. It shows the one that answered last, however long ago.
func TestTrayInUsePluginFollowsLastServed(t *testing.T) {
was := trayInUseAccount
trayInUseAccount = func(string) string { return "" }
t.Cleanup(func() { trayInUseAccount = was })
now := time.Now()
at := func(d time.Duration) *time.Time { t := now.Add(-d); return &t }
cards := []provider.SubscriptionQuota{
{Provider: "kiro", Name: "Kiro", User: "k1@x.y", Windows: []provider.QuotaWindow{{Name: "Month", Used: 5}}, LastServedAt: at(5 * time.Hour)},
{Provider: "kiro", Name: "Kiro", User: "k2@x.y", Windows: []provider.QuotaWindow{{Name: "Month", Used: 90}}, LastServedAt: at(3 * time.Hour)},
}
if got := trayPick(cards, []string{"kiro|*"}, now); len(got) != 1 || got[0].User != "k2@x.y" || got[0].Name != "Kiro · k2@x.y" {
t.Fatalf("plugin: %+v", got)
}
// none ever answered: the first, as before
cards[0].LastServedAt, cards[1].LastServedAt = nil, nil
if got := trayPick(cards, []string{"kiro|*"}, now); len(got) != 1 || got[0].User != "k1@x.y" {
t.Errorf("none served: %+v", got)
}
}
// #1516: the menu bar reads its cards again when another account starts
// answering, not up to trayUsageEvery later. NoteServed tells OnServedMoved
// when a provider's answers move to another account, and only then.
func TestServedMovedWakesTheMenuBar(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
moved := make(chan struct{}, 8)
provider.OnServedMoved(func() { moved <- struct{}{} })
t.Cleanup(func() { provider.OnServedMoved(nil) })
told := func() bool {
select {
case <-moved:
return true
default:
return false
}
}
a := provider.Provider{ID: "codex", Account: &provider.Account{Agent: "codex", User: "a@x.y"}}
b := provider.Provider{ID: "codex", Account: &provider.Account{Agent: "codex", User: "b@x.y"}}
now := time.Now()
provider.NoteServed(a, now)
if !told() {
t.Error("the first answer told nobody")
}
provider.NoteServed(a, now.Add(time.Second))
if told() {
t.Error("the same account again woke the menu bar")
}
provider.NoteServed(b, now.Add(2*time.Second))
if !told() {
t.Error("b@ answering after a@ told nobody")
}
// and what was noted is what the menu bar's cards read
if at := provider.LastServed()["codex@b@x.y"]; !at.Equal(now.Add(2 * time.Second)) {
t.Errorf("served: %v", provider.LastServed())
}
}