Files
magpie/internal/gui/traynumber_test.go
T
yetone eb779ce92a gui: the tray icon's tooltip is in magpie's language (yi_time)
yi_time in #feedback: magpie set to Chinese on Windows, and the tray
icon's hover tooltip said "Today: 121K tokens (2.9K in · 180 out ·
118K cached)" and "3 requests" in English. The tray menu already
followed magpie's language (trayLang, #301); the tooltip's words were
hard-coded English in trayusage.go.

Now every tooltip is in trayLang(settings.Lang, systemLang), on
Windows, macOS and Linux alike. That covers trayIdleTip, magpie's
totals (trayTotalCard), and each subscription card (trayCardText: its
use or what is left, the pace, when it resets, until's d/h/m, "as of
…"). The words are in trayWords beside the menu's, for zh, zh-TW, ja
and de; English stays the key and the fallback. A window's name and an
error are still the card's own. A change of language in Settings now
also refreshes the tooltip (onTrayUsage); before, only the menu was
relabelled. The test-only trayUsageText and trayUsageView stay in
English, so the existing tests keep their strings.

Test: TestTrayTooltipFollowsMagpiesLanguage.
- yi_time's own numbers in zh: "magpie\n今天:121K token(输入 2.9K ·
  输出 180 · 缓存 118K)\n3 个请求".
- The same numbers in en are unchanged.
- Covers the total card and a card's used, left, reset and as-of.
- For zh, zh-TW, ja and de, no English word in the idle, totals, card
  (with pace) or combined tooltips.

Run at this head (rebased on 5573f7923):
- mote: gofmt -l clean; CGO_ENABLED=0 GOOS=linux go vet -tags nogui
  and GOOS=windows go vet of ./internal/gui ok.
- mote: go test ./internal/gui. Everything passes except
  TestMainWindowCorners/macOS_26_SDK, which is #3949 (red on mote's
  macOS 27, not touched here).
- Windows (xiaomi-gpu, win-run): go test -run Tray ./internal/gui ok.

Broken by hand: with the tooltip words taken out of trayWords, the
test goes red. Its zh output is then exactly the old English tooltip.

Not tried:
- The real tray on Windows, hovering the icon.
- The watch loop's choice of language has no test of its own. Passing
  "en" there would not go red.
2026-11-03 10:14:23 +08:00

111 lines
4.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//go:build !nogui
package gui
import (
"bytes"
"image"
"image/color"
"image/png"
"testing"
"time"
)
// Windows' tray icon is the first card's figure (Benjaminliang_1 on X),
// the percentage as the Mac's menu bar shows it, used or left; the
// next card's when the first has none; the bird with no cards, off
// Windows, or with Settings › Tray icon number off.
func TestTrayIconFigure(t *testing.T) {
now := time.Date(2026, 9, 30, 12, 0, 0, 0, time.UTC)
cards := trayCards()
view := func(ids []string, left bool, pick map[string][]string) []trayCell {
cells, _, _ := trayCardsView("en", trayPick(cards, ids, now), now, left, false, pick)
return cells
}
for _, tc := range []struct {
name string
goos string
noNumber bool
cells []trayCell
want string
}{
{"the first card's first window", "windows", false, view([]string{"claude|a@b.c", "codex|x@y.z"}, false, nil), "42"},
{"what is left, as Settings has it", "windows", false, view([]string{"claude|a@b.c"}, true, nil), "58"},
{"the window picked for the menu bar", "windows", false, view([]string{"claude|a@b.c"}, false, map[string][]string{"claude": {"7d"}}), "18"},
{"a full window", "windows", false, view([]string{"codex|x@y.z"}, false, map[string][]string{"codex": {"7d"}}), "100"},
{"a balance first: the next card's", "windows", false, view([]string{"deepseek", "copilot"}, false, nil), "63"},
{"a balance alone: the bird", "windows", false, view([]string{"deepseek"}, false, nil), ""},
{"no cards: the bird", "windows", false, view(nil, false, nil), ""},
{"turned off: the bird", "windows", true, view([]string{"claude|a@b.c"}, false, nil), ""},
{"the Mac draws its cells", "darwin", false, view([]string{"claude|a@b.c"}, false, nil), ""},
{"Linux shows the label", "linux", false, view([]string{"claude|a@b.c"}, false, nil), ""},
{"with Pace beside it", "windows", false, []trayCell{{Rows: []string{"42% +3%"}}}, "42"},
{"a balance's own text", "windows", false, []trayCell{{Rows: []string{"1234%x"}}, {Rows: []string{"Σ1.2M"}}}, ""},
} {
if got := trayIconFigure(tc.goos, tc.noNumber, tc.cells); got != tc.want {
t.Errorf("%s: %q, want %q", tc.name, got, tc.want)
}
}
}
// The figure is drawn at the size Windows asks for, in the taskbar's text
// colour on transparency, as tall as fits with room above and below, and
// "100" narrowed into the square, not cut.
func TestTrayNumberIcon(t *testing.T) {
// off by more than the rounding of a half-covered pixel
far := func(a, b uint8) bool { d := int(a) - int(b); return d < -4 || d > 4 }
for _, size := range []int{16, 20, 24, 32} {
for _, dark := range []bool{true, false} {
for _, fig := range []string{"7", "42", "100"} {
b := trayNumberIcon(fig, size, dark)
img, err := png.Decode(bytes.NewReader(b))
if err != nil {
t.Fatalf("%s at %d: %v", fig, size, err)
}
if img.Bounds() != image.Rect(0, 0, size, size) {
t.Fatalf("%s at %d: %v", fig, size, img.Bounds())
}
want := trayNumberColour(dark)
// white on a dark taskbar, near black on a light one
if lum := int(want.R) + int(want.G) + int(want.B); dark != (lum > 3*200) || !dark && lum > 3*64 || want.A != 0xff {
t.Fatalf("dark=%v: drawn in %v", dark, want)
}
ink := image.Rectangle{}
for y := range size {
for x := range size {
c := color.NRGBAModel.Convert(img.At(x, y)).(color.NRGBA)
if c.A < 0x80 {
continue
}
if far(c.R, want.R) || far(c.G, want.G) || far(c.B, want.B) {
t.Fatalf("%s at %d dark=%v: (%d,%d) is %v, want %v", fig, size, dark, x, y, c, want)
}
ink = ink.Union(image.Rect(x, y, x+1, y+1))
}
}
if ink.Empty() {
t.Fatalf("%s at %d: nothing drawn", fig, size)
}
// legible: the digits most of the square's height, a pixel
// or more clear of its top and bottom
if h := ink.Dy(); h < size*6/10 || ink.Min.Y < 1 || ink.Max.Y > size-1 {
t.Errorf("%s at %d: ink %v", fig, size, ink)
}
// centred, and three digits fill the width without leaving it
if d := ink.Min.X - (size - ink.Max.X); d < -2 || d > 2 {
t.Errorf("%s at %d: off centre, ink %v", fig, size, ink)
}
if fig == "100" && ink.Dx() < size*8/10 {
t.Errorf("100 at %d: %d wide", size, ink.Dx())
}
}
}
}
if bytes.Equal(trayNumberIcon("42", 16, true), trayNumberIcon("17", 16, true)) {
t.Fatal("42 and 17 drawn alike")
}
if trayNumberIcon("", 16, true) != nil || trayNumberIcon("42", 0, true) != nil {
t.Fatal("drawn with no figure or no size")
}
}