Files
magpie/internal/settings/trayusages_test.go
T
yetone ef4edf8c58 tray: the menu bar shows several subscriptions' usage side by side (KevinXC on Discord) — Settings → Usage in the menu bar picked one card; its menu now ticks any number (kept open while ticking, saved once when it closes, Off unticks all, a ticked card that has gone stays listed to untick) and the pill says "3 subscriptions". settings.TrayUsages keeps them in order; a file with only the older trayUsage shows that one, and trayUsage is still written as the first for an older magpie. On the Mac the bird and a cell per card are drawn as one image by AppKit on the status item's button: the card's logo (a black glyph tinted in the menu bar's text colour, a coloured one as it is, the name's letter in a ring when there is none) and its two windows stacked, "42%" over "18%", in the bar's colour light or dark and sharp at any scale; the bird's flap goes through it. Arc flags run into numbers (Codex's logo) are spaced out for AppKit's SVG reader. A row longer than 10 characters (a plan's balance read from its page) is cut with an ellipsis in the bar and whole in the tooltip. Windows and Linux show the cards as text, " | " between them. TestTrayUsageCells, TestSVGArcFlags, TestTrayImage (darwin, renders 0-3 cards light and dark at 2x), TestTrayUsagesMigrate and tray-usages.test.cjs (Chromium, WebKit, en/zh) are new. go vet, go build (darwin GUI), go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./..., go test ./internal/gui pass; tray-usages, settings-groups, panel-*, click-scroll, old-webkit pass. Not seen in a live menu bar.
2026-09-30 19:45:44 +08:00

60 lines
1.8 KiB
Go

package settings
import (
"os"
"slices"
"strings"
"testing"
)
// The menu bar shows several cards (TrayUsages); a settings file from before
// them, with the one TrayUsage, shows that one, and TrayUsage is kept as the
// first for an older magpie.
func TestTrayUsagesMigrate(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
if err := os.MkdirAll(Dir(), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(Path(), []byte(`{"trayUsage":"claude|a@b.c","trayUsageEvery":5}`), 0o644); err != nil {
t.Fatal(err)
}
s := Load()
if !slices.Equal(s.TrayUsages, []string{"claude|a@b.c"}) || s.TrayUsage != "claude|a@b.c" {
t.Fatalf("old file: %q %q", s.TrayUsages, s.TrayUsage)
}
// saved as it was read, then with more: TrayUsage follows the first
if err := Save(s); err != nil {
t.Fatal(err)
}
if s = Load(); !slices.Equal(s.TrayUsages, []string{"claude|a@b.c"}) {
t.Fatalf("saved as read: %q", s.TrayUsages)
}
s.TrayUsages = []string{" codex|x@y.z ", "claude|a@b.c", "codex|x@y.z", ""}
if err := Save(s); err != nil {
t.Fatal(err)
}
b, _ := os.ReadFile(Path())
s = Load()
if !slices.Equal(s.TrayUsages, []string{"codex|x@y.z", "claude|a@b.c"}) || s.TrayUsage != "codex|x@y.z" {
t.Fatalf("several: %q %q\n%s", s.TrayUsages, s.TrayUsage, b)
}
if !strings.Contains(string(b), `"trayUsage": "codex|x@y.z"`) {
t.Fatalf("first not kept for an older magpie:\n%s", b)
}
// all turned off, though the page still sends the first it was drawn with
s.TrayUsages = []string{}
if err := Save(s); err != nil {
t.Fatal(err)
}
if s = Load(); len(s.TrayUsages) != 0 || s.TrayUsage != "" {
t.Fatalf("off: %q %q", s.TrayUsages, s.TrayUsage)
}
// and none of either is none
if err := Save(Settings{}); err != nil {
t.Fatal(err)
}
if s = Load(); s.TrayUsages != nil || s.TrayUsage != "" {
t.Fatalf("none: %q %q", s.TrayUsages, s.TrayUsage)
}
}