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

78 lines
3.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.
package gui
import (
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/usage"
)
// yi_time on Discord: magpie set to Chinese on Windows, and the tray icon's
// tooltip said "Today: 121K tokens (2.9K in · 180 out · 118K cached)" and
// "3 requests" in English. The tooltip is in magpie's language, as the
// tray menu is: the idle tip, magpie's totals and a subscription's
// windows, in every language magpie has. A window's name and the numbers
// are the card's own.
func TestTrayTooltipFollowsMagpiesLanguage(t *testing.T) {
// yi_time's numbers
today := usage.Totals{Calls: 3, Input: 2_900, Output: 180, CacheRead: 118_000}
if got, want := trayIdleTip("zh", today, "usd", 0), "magpie\n今天:121K token(输入 2.9K · 输出 180 · 缓存 118K)\n3 个请求"; got != want {
t.Errorf("zh idle tip =\n%q, want\n%q", got, want)
}
if got, want := trayIdleTip("en", today, "usd", 0), "magpie\nToday: 121K tokens (2.9K in · 180 out · 118K cached)\n3 requests"; got != want {
t.Errorf("en idle tip =\n%q, want\n%q", got, want)
}
if got, want := trayTotalCard("zh", "usage:today", today, "usd", 0).Name, "magpie 今日\n121K token(输入 2.9K · 输出 180 · 缓存 118K)\n3 个请求"; got != want {
t.Errorf("zh total card =\n%q, want\n%q", got, want)
}
now := time.Now()
asOf := now.Add(-time.Hour)
card := provider.SubscriptionQuota{Provider: "claude", Name: "Claude", AsOf: &asOf,
Windows: []provider.QuotaWindow{{Name: "5h", Used: 42, ResetSecs: 3*3600 + 10*60}}}
_, tip := trayCardText("zh", card, now, false, false, nil)
for _, w := range []string{"Claude\n", "5h 已用 42%", "3 小时 10 分 后重置", "截至 ", "暂时无法获取最新额度"} {
if !strings.Contains(tip, w) {
t.Errorf("zh card tooltip %q lacks %q", tip, w)
}
}
if _, tip := trayCardText("zh", card, now, true, false, nil); !strings.Contains(tip, "5h 剩余 58%") {
t.Errorf("zh card tooltip, what is left: %q", tip)
}
english := []string{"Today", "tokens", "K in", "0 in", " out", "cached", "request", " used", " left", "resets", "pace ", "as of", "Last 7", "no known price", "couldn't", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}
for _, lang := range []string{"zh", "zh-TW", "ja", "de"} {
priced := today
priced.Cost, priced.Unpriced = 1.5, 1
readAt := now.Add(-2 * time.Hour)
paced := card
paced.ReadAt = &readAt
paced.Windows = []provider.QuotaWindow{{Name: "5 hours", Used: 42.5, ResetSecs: 3*3600 + 10*60}, {Name: "7 days", Used: 3, ResetSecs: 4*86400 + 3600}, {Name: "x", Used: 1, ResetSecs: 120}}
tips := []string{
trayIdleTip(lang, priced, "usd", 0),
trayIdleTip(lang, usage.Totals{Calls: 1, Input: 5}, "usd", 0),
trayIdleTip(lang, usage.Totals{}, "usd", 0),
trayTotalCard(lang, "usage:today", priced, "usd", 0).Name,
trayTotalCard(lang, "usage:7d", priced, "usd", 0).Name,
}
for _, left := range []bool{false, true} {
_, tip := trayCardText(lang, paced, now, left, true, nil)
if pace := trayText(lang, "pace %s", ""); !strings.Contains(tip, pace) {
t.Errorf("%s: no %q in %q", lang, pace, tip)
}
tips = append(tips, tip)
}
_, _, view := trayCardsView(lang, []provider.SubscriptionQuota{trayTotalCard(lang, "usage:today", priced, "usd", 0), paced}, now, false, true, nil)
tips = append(tips, view)
for _, tip := range tips {
for _, w := range english {
if strings.Contains(tip, w) {
t.Errorf("%s tooltip says %q in English:\n%s", lang, w, tip)
}
}
}
}
}