Files
magpie/internal/gui/trayusage_hidden_test.go
T
yetone 1ab2c39843 tray: the menu bar and panel leave out what Usage's Overview hides (onkashiko on X)
onkashiko on X: a quota hidden in the main window still showed from the
menu bar. 7ea1e6a4b made the tray panel and the TUI leave out Overview-hidden
cards and rows, but two gaps were left:

- The menu bar's cells, its tooltip and Windows' tray figure still showed a
  hidden card and its hidden rows. trayUsageCards now goes through
  trayShown: a cell naming an account takes the card the Overview still
  shows (its balance with its plan hidden), a cell following the account
  in use (`|*`) picks that account among every card as before and is left
  empty while its card is hidden, never moved to another account. Hidden
  rows are left out, a card never left with none. settings.TrayUsages is
  untouched, so the cell comes back with the card.
- POST /api/usage/arrange now wakes the menu bar (onTrayUsage) when
  cardsHidden or windowsHidden is sent, so a hide or unhide reaches it at
  once rather than at the next tick (3 minutes by default).
- The panel hid a card after keyCards had put a key's balance on its plan's
  card, so "Hide its balance from Overview" left the balance on the panel's
  plan card, and a hidden plan took its unhidden balance with it.
  panelPageCut now takes the quotas as read and leaves the hidden out before
  keyCards, as overviewCards does; gone counts a merged card hidden only
  when its plan and balance both are.

The Go hide rule (cardHideKey/usageShown) moved from internal/tui to
internal/provider (ShownQuotas, QuotaCardHidden, QuotaRowsShown,
QuotaHideKey), shared by the TUI and the menu bar. Not changed: Bark's
daily push still warns about a near-limit hidden card (an alert, not a
display). Docs: settings.go comments, docs/subsystems/gui-shell.md.

Run on mica (mote at load 117 on 18 cores), at base ee6352b63:
- go test -count=1 -timeout 45m ./internal/gui ./internal/tui
  ./internal/provider ./internal/settings: all ok.
- go vet of those packages; GOOS=linux go vet -tags nogui and GOOS=windows
  go vet of internal/gui, internal/tui, internal/provider: clean. gofmt -l
  clean.
- GUI (Chromium + WebKit, en/zh/zh-TW/ja/de): panel-hidden-balance (new),
  panel-page-hidden, panel-arrange: 28/28. panel-balance-order,
  usage-rows-hide pass. usage-cards-hide and usage-hidden-way-back are red
  at "de 560" (no sideways scroll), the same 8 on an origin/main
  ee6352b63 copy (#6294, #6295). tray-cell-click "chromium zh/en" is red
  1 run in 3 on origin/main as on this tree (#5215).
Broken by hand: panel-hidden-balance is red on origin/main's assets
(the plan card carries the hidden balance, MiniMax's balance gone);
TestTrayLeavesOutWhatTheOverviewHides is red with trayShown returning
trayPick, TestUsageArrangeWakesTheMenuBar red with the onTrayUsage call
removed.
At the pushed head (rebased on 534b1a334, which adds installdir_windows.go):
GOOS=windows go vet ./internal/gui ./internal/update and go test -run
"TestTray|TestUsageArrange|TestCoreScriptBudget" ./internal/gui: ok.
Not tried: the real menu bar on a Mac or Windows with a real account; no
full go test ./... (a full suite was running on mica).
2026-11-14 19:37:57 +08:00

92 lines
3.5 KiB
Go

package gui
import (
"net/http"
"net/http/httptest"
"path/filepath"
"strconv"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/settings"
)
// onkashiko on X: a card hidden from the Usage page's Overview showed on in
// the menu bar. Its cell, the tooltip and Windows' tray figure leave it out
// as the Overview and the tray panel do, and the rows hidden there too;
// the cells ticked are kept for when it is shown again.
func TestTrayLeavesOutWhatTheOverviewHides(t *testing.T) {
was := trayInUseAccount
trayInUseAccount = func(string) string { return "b@x.y" }
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}, {Name: "Weekly", Used: 20}}},
{Provider: "claude", Name: "Claude Code", User: "b@x.y", Windows: []provider.QuotaWindow{{Name: "5-hour", Used: 70}, {Name: "Weekly", Used: 30}}},
{Provider: "stepfun", Name: "StepFun", User: "s", Plan: "Plus", Windows: []provider.QuotaWindow{{Name: "5-hour", Used: 5}}},
{Provider: "stepfun", Name: "StepFun", User: "s", Balance: "¥9.00"},
{Provider: "deepseek", Name: "DeepSeek", Balance: "¥1.00"},
{Provider: "kimi", Name: "Kimi", Windows: []provider.QuotaWindow{{Name: "Weekly", Used: 1}}},
}
ids := []string{"claude|*", "claude|a@x.y", "stepfun|s", "deepseek", "kimi"}
now := time.Now()
if got := trayShown(cards, ids, now, settings.Settings{}); len(got) != 5 {
t.Fatalf("nothing hidden: %d cards, want 5", len(got))
}
s := settings.Settings{
UsageCardsHidden: []string{"claude|b@x.y|plan", "deepseek|balance", "stepfun|s|plan"},
UsageWindowsHidden: []string{"claude|Weekly"},
}
got := trayShown(cards, ids, now, s)
var names []string
for _, q := range got {
names = append(names, trayCardID(q)+":"+q.Balance+":"+strconv.Itoa(len(q.Windows)))
}
// the account in use (b) is hidden: its cell is empty, not moved to a;
// a's card loses its weekly row; StepFun's plan hidden, its balance
// shows; DeepSeek's balance hidden; Kimi's one row stays, a card never
// left with none
want := "claude|a@x.y::1 stepfun|s:¥9.00:0 kimi::1"
if strings.Join(names, " ") != want {
t.Fatalf("got %q, want %q", strings.Join(names, " "), want)
}
if cards[0].Windows[1].Name != "Weekly" || len(cards[0].Windows) != 2 {
t.Fatal("the cached cards were changed")
}
}
// A card hidden or shown again from the Usage page reaches the menu bar at
// once, not at its next tick.
func TestUsageArrangeWakesTheMenuBar(t *testing.T) {
h := t.TempDir()
t.Setenv("HOME", h)
t.Setenv("USERPROFILE", h)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
t.Setenv("APPDATA", filepath.Join(h, "AppData", "Roaming"))
t.Setenv("LOCALAPPDATA", filepath.Join(h, "AppData", "Local"))
woke := 0
was := onTrayUsage
onTrayUsage = func() { woke++ }
t.Cleanup(func() { onTrayUsage = was })
for _, c := range []struct {
body string
want int
}{
{`{"order":["kimi"]}`, 0},
{`{"cardsHidden":["deepseek|balance"]}`, 1},
{`{"windowsHidden":["claude|Weekly"]}`, 2},
{`{"cardsHidden":[]}`, 3},
} {
rec := httptest.NewRecorder()
Handler(nil, nil).ServeHTTP(rec, httptest.NewRequest("POST", "/api/usage/arrange", strings.NewReader(c.body)))
if rec.Code != http.StatusOK {
t.Fatalf("%s: %d %s", c.body, rec.Code, rec.Body)
}
if woke != c.want {
t.Fatalf("after %s the menu bar was woken %d times, want %d", c.body, woke, c.want)
}
}
}