Files
magpie/internal/gui/traymenu_test.go
T
yetone e8fbee60ea gui: pause the gateway without quitting magpie (me2seeks on X)
Disconnect and quit was the only way to stop the gateway, and it quits
magpie too. Pause does the same without the quit, from the tray's Pause
Gateway (Resume Gateway while paused), Settings › General › Gateway
(On/Paused, the app's only) and the Gateway page's Resume gateway, all
through POST /api/gateway/pause.

- Pause disconnects each connected agent as its row's Disconnect does
  (its own settings put back), keeps their ids in gateway-paused.json in
  the config folder, and stops the listener with requests in flight
  drained. An agent that can't be disconnected keeps the gateway serving,
  those before it connected again, and is named in the status line.
- While paused, serveGatewayLocked serves nothing: not the 15s watch, a
  port change nor a handover; a magpie started again stays paused. A
  pause file that can't be read counts as paused.
- Resume serves again and connects the agents the pause disconnected.
  A port held by something else keeps the pause and says what holds it.
  The Gateway page's Start/Restart, connecting an agent and picking one
  of magpie's models for it resume too.
- quitask's wired list and the pause share wiredOf.
- Strings in en, zh, zh-TW, ja, de. gui-shell.md documents it.

Run (mote was unreachable for the later runs; the Windows box instead):
- mote, before the rebase: go vet ./internal/gui; go test -run
  'TestGatewayPause|TestGatewayResumes|TestTray|TestGatewayFix|TestQuit'
  ./internal/gui: ok.
- at this tree on b46f23ffa: win-run go vet ./internal/gui ok; wsl-run
  go vet -tags nogui ./internal/gui ok; gofmt -l clean.
- win-run go test -count=1 ./internal/gui: the pause and tray tests ok;
  8 reds, each the same on origin/main d4b4a188e run alone on Windows
  and filed (#4115 %AppData% import rows, #5928 ~\ paths, #2107/#3649
  TestGatewayHeldByAContainer).
- wsl-run GUI (chromium + webkit): gateway-pause (5 languages x 1000/420),
  settings-general, gateway-takeover, i18n-keys, i18n-en-no-cjk: 51/52.
  The red is gateway-takeover webkit en's dark-mode case, red 1 of 2 on
  untouched origin/main too: #5942.
- rebased on 401187300: win-run go test -run 'Budget|TestGatewayPause|
  TestGatewayResumes|TestTray|TestQuit' ./internal/gui ok; wsl-run go vet
  -tags nogui ./internal/gui ok; gofmt clean. darwin vet not rerun after
  the rebase (mote refused SSH).

Broken by hand: serveGatewayLocked without its pausedNow check, and the
rollback's reconnect skipped: TestGatewayPauseAndResume ("served while
paused") and TestGatewayPauseRollsBack ("codex not connected again")
went red; green again restored.

Not tried: the real app's tray on a desktop, a real agent's config
round-trip (the Go tests fake the agents; Disconnect/connect are the
rows' own code), an agent wired from the CLI while the app is paused
(it doesn't resume), the whole GUI suite, the full Go suite (started after the tag).
2026-11-10 19:36:24 +08:00

126 lines
4.9 KiB
Go

package gui
import (
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/settings"
)
// The tray menu is in the page's language: the setting, or the system's
// when it follows the system (#301).
func TestTrayMenuLabels(t *testing.T) {
zhSys := func() string { return "zh-Hans-CN" }
enSys := func() string { return "en-US" }
for _, c := range []struct {
pref string
sys func() string
want string
}{
{"zh", enSys, "zh"},
{"en", zhSys, "en"},
{"system", zhSys, "zh"},
{"system", enSys, "en"},
{"", func() string { return "zh_CN.UTF-8" }, "zh"},
{"system", func() string { return "" }, "en"},
{"ja", enSys, "ja"},
{"system", func() string { return "ja-JP" }, "ja"},
{"", func() string { return "ja_JP.UTF-8" }, "ja"},
{"de", enSys, "de"},
{"system", func() string { return "de-DE" }, "de"},
{"", func() string { return "de_AT.UTF-8" }, "de"},
{"zh-TW", enSys, "zh-TW"},
{"system", func() string { return "zh-Hant-TW" }, "zh-TW"},
{"system", func() string { return "zh-TW" }, "zh-TW"},
{"system", func() string { return "zh-HK" }, "zh-TW"},
{"system", func() string { return "zh-Hant" }, "zh-TW"},
{"", func() string { return "zh_TW.UTF-8" }, "zh-TW"},
{"", func() string { return "zh_HK" }, "zh-TW"},
{"system", func() string { return "zh-Hans-HK" }, "zh"},
{"system", func() string { return "zh-SG" }, "zh"},
} {
if got := trayLang(c.pref, c.sys); got != c.want {
t.Errorf("trayLang(%q, %s) = %s, want %s", c.pref, c.sys(), got, c.want)
}
}
de := trayMenuLabels("de", "0.1.500", "0.1.501", false)
if de != (trayLabels{"magpie öffnen", "Zugangspunkt pausieren", "Version 0.1.500", "Zum Aktualisieren auf 0.1.501 neu starten", "magpie beenden"}) {
t.Errorf("de: %+v", de)
}
for lang, want := range map[string]string{"de": "en", "en": "en", "zh": "zh", "zh-TW": "zh"} {
if got := notesLang(lang); got != want {
t.Errorf("notesLang(%q) = %q, want %q", lang, got, want)
}
}
zh := trayMenuLabels("zh", "0.1.500", "", false)
if zh != (trayLabels{"打开 magpie", "暂停网关", "版本 0.1.500", "重启以更新", "退出 magpie"}) {
t.Errorf("zh: %+v", zh)
}
if tw := trayMenuLabels("zh-TW", "0.1.500", "0.1.501", false); tw != (trayLabels{"開啟 magpie", "暫停閘道", "版本 0.1.500", "重新啟動以更新到 0.1.501", "結束 magpie"}) {
t.Errorf("zh-TW: %+v", tw)
}
if ja := trayMenuLabels("ja", "0.1.500", "", false); ja != (trayLabels{"magpie を開く", "ゲートウェイを一時停止", "バージョン 0.1.500", "再起動してアップデート", "magpie を終了"}) {
t.Errorf("ja: %+v", ja)
}
if l := trayMenuLabels("zh", "0.1.500", "0.1.501", false); l.restart != "重启以更新到 0.1.501" {
t.Errorf("zh restart: %q", l.restart)
}
en := trayMenuLabels("en", "0.1.500", "0.1.501", false)
if en != (trayLabels{"Open magpie", "Pause Gateway", "Version 0.1.500", "Restart to Update to 0.1.501", "Quit magpie"}) {
t.Errorf("en: %+v", en)
}
// paused, the menu's line resumes the gateway (me2seeks on X)
for lang, want := range map[string]string{"en": "Resume Gateway", "zh": "恢复网关", "zh-TW": "恢復閘道", "ja": "ゲートウェイを再開", "de": "Zugangspunkt fortsetzen"} {
if l := trayMenuLabels(lang, "0.1.500", "", true); l.pause != want {
t.Errorf("%s paused: %q, want %q", lang, l.pause, want)
}
}
env := map[string]string{"LC_ALL": "C", "LANG": "zh_CN.UTF-8"}
if got := envLang(func(k string) string { return env[k] }); got != "zh_CN.UTF-8" {
t.Errorf("envLang skips C: %q", got)
}
if got := firstAppleLang("(\n \"zh-Hans-CN\",\n \"en-US\"\n)\n"); got != "zh-Hans-CN" {
t.Errorf("firstAppleLang: %q", got)
}
if got := firstAppleLang("(\n en,\n \"zh-Hans\"\n)\n"); got != "en" {
t.Errorf("firstAppleLang unquoted: %q", got)
}
}
// A language changed on the Settings page relabels the tray menu; a save
// that leaves it as it was doesn't.
func TestSettingsLangRelabelsTray(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"))
if err := settings.Save(settings.Settings{Lang: "en"}); err != nil {
t.Fatal(err)
}
calls := 0
onLang = func() { calls++ }
t.Cleanup(func() { onLang = nil })
post := func(body string) {
rec := httptest.NewRecorder()
Handler(nil, nil).ServeHTTP(rec, httptest.NewRequest("POST", "/api/settings", strings.NewReader(body)))
if rec.Code != http.StatusOK {
t.Fatalf("%d %s", rec.Code, rec.Body)
}
}
post(`{"theme":"dark","lang":"en"}`)
if calls != 0 {
t.Fatalf("relabelled %d times with the language unchanged", calls)
}
post(`{"theme":"dark","lang":"zh"}`)
if calls != 1 {
t.Fatalf("relabelled %d times after en → zh, want 1", calls)
}
if l := trayMenuLabels(trayLang(settings.Load().Lang, systemLang), "1", "", false); l.quit != "退出 magpie" {
t.Fatalf("after the change the menu reads %+v", l)
}
}