Files
magpie/internal/gui/trayusage_shared_test.go
T
yetone f4a134d78f tray: a ticked card with no account-wide window still shows (Antigravity, nivekxft on X)
nivekxft on X: Antigravity ticked under Settings › Allowances in the menu
bar, and the Mac's menu bar showed the other three cards but none for it.

Cause: trayWindows kept only windows that are neither Aside nor a single
model's (Model set). Antigravity's card has nothing else: a window per
model, and its pools' windows ("Gemini", "Claude & GPT"), which
google.go's pools() marks Aside so routing goes on reading the
per-model windows. So trayCardText returned an empty label and an empty
tooltip, and trayCardsView dropped the card. The real card read from
quotas.json (an Antigravity Starter account, 10-31) has 13 per-model
windows and two pool 7-day windows, and nothing the old filter kept.

Fix: when a card has no account-wide window, the menu bar takes, in
order,
- trayShared: its windows over all models that routing sets aside and
  that tell a share (Antigravity's pools, named "Gemini · 7 days" as the
  Usage page names them; Ollama's plan windows, all Aside while bought
  credits carry the account),
- trayModels: else its per-model windows, the tightest of each family
  (as the Usage page shows one a family), most used first (Gemini CLI,
  whose card is per-model only; Antigravity when the pool summary can't
  be read).
Text rows, unlimited rows, wallet balances and a money figure with no
share are never shown as a percent. A card with an account-wide window
is as before. withTrayKinds now marks the windows trayWindows returns, so
Settings' window picker follows the same choice.

Siblings: Gemini CLI (per-model windows only) and Ollama with bought
credits (every window Aside) were dropped the same way and are covered.

Run (mote, at 394a92f0f + this change):
- go test -run 'Tray[^C]|WithTrayKinds|TrayAntigravity|TrayModel|TraySetAside'
  ./internal/gui: ok. The three new tests (trayusage_shared_test.go,
  fixture from the real quotas.json card) are red on the old
  trayusage.go: "cells []" each.
- go test ./internal/gui (AppKit window tests skipped/killed): reds are
  the AppKit tests killed at their deadline on mote (#4125, #2645),
  TestColdTurnsTaken (#4510), and TestSettingsSaveKeepsWhatItDoesNotSend,
  red on origin/main 394a92f0f without this change (#4514).
- gofmt -l: clean. go vet, GOOS=linux go vet -tags nogui,
  GOOS=windows go vet of ./internal/gui (and provider for linux): ok.
Not run: the full suite (another session's go test was running on
mote); GUI Playwright suites (no app.js/traywindows.js change).
Not tried: the reporter's real menu bar; the Settings window picker
with Antigravity's pool kinds in a running app.
2026-10-31 11:24:04 +08:00

145 lines
6.3 KiB
Go

package gui
import (
"slices"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// antigravityStarter is an Antigravity Starter account's card as magpie
// read it (quotas.json, 10-31, the account renamed): a window for each
// model, and the two pools' 7 days, which routing sets aside; no window
// over the whole account.
func antigravityStarter() provider.SubscriptionQuota {
week := 7 * 24 * time.Hour
gem := time.Date(2026, 11, 4, 7, 24, 29, 0, time.UTC)
cla := time.Date(2026, 11, 2, 16, 12, 46, 0, time.UTC)
q := provider.SubscriptionQuota{Provider: "antigravity", Name: "Antigravity", Icon: "antigravity-color",
Plan: "Antigravity Starter Quota", User: "someone@gmail.com"}
for _, m := range []struct{ name, id, family string }{
{"Claude Opus 4.6 (Thinking)", "claude-opus-4-6-thinking", "Claude"},
{"Claude Sonnet 4.6 (Thinking)", "claude-sonnet-4-6", "Claude"},
{"Gemini 2.5 Flash", "gemini-2.5-flash", "Gemini"},
{"Gemini 3 Flash", "gemini-3-flash", "Gemini"},
{"Gemini 3.8 Flash (High)", "gemini-3.8-flash-high", "Gemini"},
{"GPT-OSS 120B (Medium)", "gpt-oss-120b-medium", "GPT-OSS"},
} {
w := provider.QuotaWindow{Name: m.name, Model: m.id, Family: m.family, Used: 6.27976, ResetsAt: &gem}
if m.family != "Gemini" {
w.Used, w.ResetsAt = 100, &cla
}
q.Windows = append(q.Windows, w)
}
q.Windows = append(q.Windows,
provider.QuotaWindow{Name: "7 days", Pool: "Gemini", Used: 6.27976, ResetsAt: &gem, Span: week, Aside: true},
provider.QuotaWindow{Name: "7 days", Pool: "Claude & GPT", Used: 100, ResetsAt: &cla, Span: week, Aside: true})
return q
}
// A card ticked for the menu bar is there whatever windows it has
// (nivekxft on X: Antigravity ticked, no Antigravity in the menu bar).
// Antigravity's pools stand in for windows over the account, named with
// their pool in the tooltip.
func TestTrayAntigravityPools(t *testing.T) {
now := time.Date(2026, 10, 31, 3, 0, 0, 0, time.UTC)
q := antigravityStarter()
cells, label, tip := trayCardsView([]provider.SubscriptionQuota{q}, now, true, false, nil)
if len(cells) != 1 || !slices.Equal(cells[0].Rows, []string{"94%", "0%"}) || cells[0].ID != "antigravity|someone@gmail.com" {
t.Fatalf("cells %+v", cells)
}
if label != "94% · 0%" {
t.Fatalf("label %q", label)
}
for _, s := range []string{"Gemini · 7 days 93.7% left", "Claude & GPT · 7 days 0% left"} {
if !strings.Contains(tip, s) {
t.Errorf("tooltip lacks %q:\n%s", s, tip)
}
}
if strings.Contains(tip, "Gemini 2.5 Flash") {
t.Errorf("the tooltip lists the models the pools stand for:\n%s", tip)
}
// Settings is offered the pools' window, and the card's own windows
// are left as the cache has them
out := withTrayKinds([]provider.SubscriptionQuota{q})
var kinds []string
for _, w := range out[0].Windows {
if w.TrayKind != "" {
kinds = append(kinds, w.Pool+"="+w.TrayKind)
}
}
if !slices.Equal(kinds, []string{"Gemini=7d", "Claude & GPT=7d"}) {
t.Fatalf("kinds %q", kinds)
}
if q.Windows[6].Name != "7 days" || q.Windows[6].TrayKind != "" {
t.Fatalf("the cache's window was written: %+v", q.Windows[6])
}
}
// With no pool to read (Gemini CLI's card, or Antigravity's when the
// summary can't be read) the models' own windows stand in: one a family,
// its tightest, the most used first.
func TestTrayModelWindows(t *testing.T) {
now := time.Date(2026, 10, 31, 3, 0, 0, 0, time.UTC)
q := antigravityStarter()
q.Windows = slices.DeleteFunc(q.Windows, func(w provider.QuotaWindow) bool { return w.Pool != "" })
q.Windows[3].Used = 40 // Gemini 3 Flash, the family's tightest
cells, _, tip := trayCardsView([]provider.SubscriptionQuota{q}, now, false, false, nil)
if len(cells) != 1 || !slices.Equal(cells[0].Rows, []string{"100%", "100%"}) {
t.Fatalf("cells %+v", cells)
}
for _, s := range []string{"Claude Opus 4.6 (Thinking) 100% used", "Gemini 3 Flash 40% used", "GPT-OSS · GPT-OSS 120B"} {
if s == "GPT-OSS · GPT-OSS 120B" {
if strings.Contains(tip, s) {
t.Errorf("a family its model's name says is said twice:\n%s", tip)
}
continue
}
if !strings.Contains(tip, s) {
t.Errorf("tooltip lacks %q:\n%s", s, tip)
}
}
if strings.Contains(tip, "Gemini 2.5 Flash") {
t.Errorf("more than one window a family:\n%s", tip)
}
gemini := provider.SubscriptionQuota{Provider: "gemini", Name: "Gemini CLI", Windows: []provider.QuotaWindow{
{Name: "gemini-2.5-pro", Model: "gemini-2.5-pro", Used: 12},
{Name: "gemini-2.5-flash", Model: "gemini-2.5-flash", Used: 30},
}}
if cells, _, _ := trayCardsView([]provider.SubscriptionQuota{gemini}, now, false, false, nil); len(cells) != 1 || !slices.Equal(cells[0].Rows, []string{"30%", "12%"}) {
t.Fatalf("gemini %+v", cells)
}
}
// A plan's windows that bought credits carry the account past (Ollama's,
// all set aside) are still the plan's to watch; money aside, a row of
// text, or an account's own windows beside them are as before.
func TestTraySetAsideWindows(t *testing.T) {
now := time.Date(2026, 10, 31, 3, 0, 0, 0, time.UTC)
ollama := provider.SubscriptionQuota{Provider: "ollama", Name: "Ollama", Windows: []provider.QuotaWindow{
{Name: "Weekly", Used: 30, Span: 7 * 24 * time.Hour, Aside: true},
{Name: "Session limit", Used: 10, Aside: true},
}}
if cells, _, _ := trayCardsView([]provider.SubscriptionQuota{ollama}, now, false, false, nil); len(cells) != 1 || !slices.Equal(cells[0].Rows, []string{"30%", "10%"}) {
t.Fatalf("ollama %+v", cells)
}
text := provider.SubscriptionQuota{Provider: "autoclaw", Name: "AutoClaw", Windows: []provider.QuotaWindow{
{Name: "积分余额", Display: "120", Text: true, Aside: true},
{Name: "Extra usage", Display: "$3.00", Aside: true},
}}
if cells, _, tip := trayCardsView([]provider.SubscriptionQuota{text}, now, false, false, nil); len(cells) != 0 || tip != "" {
t.Fatalf("text rows made a cell: %+v %q", cells, tip)
}
claude := provider.SubscriptionQuota{Provider: "claude", Name: "Claude", Windows: []provider.QuotaWindow{
{Name: "5 hours", Used: 42, Span: 5 * time.Hour},
{Name: "Extra", Used: 90, Span: 30 * 24 * time.Hour, Aside: true},
{Name: "Opus", Used: 80, Span: 7 * 24 * time.Hour, Model: "opus"},
}}
if cells, _, _ := trayCardsView([]provider.SubscriptionQuota{claude}, now, false, false, nil); len(cells) != 1 || !slices.Equal(cells[0].Rows, []string{"42%"}) {
t.Fatalf("claude %+v", cells)
}
}