Files
magpie/internal/gui/restart_test.go
T
yetone 74bba1fa67 gui: Settings › Version and the tray restart magpie with no update (yansheng1003 on X)
yansheng1003 asked for a Restart beside Check: "每次改了需要重新magpie,
都要自己退出再启动麻烦". The Version row now has Restart after Check
while no update is on offer (found, downloading, downloaded, blocked, a
newer version named or a failed download each have their own buttons,
which restart through the update and fill a 420px row), and the tray's
Restart magpie is always shown:
with an update ready it installs it as before, with none it starts this
magpie again as it is.

Both go through restartSelfNow (restart.go), the updater's own way of
starting again: on macOS and Linux the gateway's socket is handed over
(beginHandover), so requests in flight carry on; otherwise the bundle or
binary is started again (update.Relaunch, relaunchBinary), and on
Windows requests in flight are cut. magpie web runs itself again in
place. POST /api/restart answers 204 and then quits, or 400 "magpie
couldn't start again: quit it and open it again", and the row says
Couldn't restart magpie with the reason and gives its buttons back.
/api/update now says its run (the time this process started), so a
browser's page reloads once another run answers.

The click, its error and the reload are in versionrow.js, imported when
Restart is pressed. The mirror's Download from GitHub click moved there
too, so app.js stays under its budget with no raise (TestCoreScriptBudget
at 1,088,248 and 1,272,581, as they were). The route is named in the
CLI's notYet list. Words in en, zh, zh-TW, ja and de.

What needed a restart before, besides an edited path plugin (the commit
before): most settings already applied live (proxies, OTel, HTTPS/mTLS,
LAN from the GUI, allow-lists, CORS, shortcuts, tray, language, name,
fonts, port, hand-edited caller-keys, oidc-rules and library.json).
Still start-only: LAN switched by hand in settings.json, environment
variables, and in-memory routing state such as a rate-limited account's
order; Restart is for those.

Run:
- Full Go suite (mote, -timeout 45m) at the head before the rebase and
  before versionrow.js: 101 packages ok; the reds were
  TestEveryRouteHasACommandOrIsNamed, TestEveryRouteIsNamedForTheCLI
  (the route unnamed) and TestCoreScriptBudget, all fixed here.
- At this head (mote): go test ./internal/gui -run
  'TestCoreScriptBudget|TestEveryRouteIsNamedForTheCLI|TestRestart|
  TestUpdateSaysItsRun|TestTrayMenu' ok; ./internal/cli -run
  TestEveryRouteHasACommandOrIsNamed ok. go build, go vet, GOOS=linux
  go vet -tags nogui ./..., GOOS=windows go vet ./... ok; gofmt -l
  clean.
- GUI (mote, both engines, at this tree): update-*.test.cjs,
  version-restart, settings-update-source, refresh-page, old-webkit,
  i18n-keys and i18n-en-no-cjk: 628 pass, then 6 red in update-check
  (Restart not dimmed while a check runs), fixed here (Restart dims
  with Check); update-check, version-restart and old-webkit again: 90
  pass, 0 fail.
- Real restart (mote, sandbox HOME): a built magpie web with
  MAGPIE_WEB_KEY, signed in through ?k=; POST /api/restart answered 204,
  the same pid ran again with a new run, and the cookie still signed in.
- Broken by hand: restartAppFrom returning true on an error, and
  TestRestartStartsMagpieAgain is red; version-restart.test.cjs on the
  old app.js is red (21 on chromium); with Restart shown beside an
  update on offer again, "no Restart beside an update on offer" is red
  in all 5 languages and both engines (12 fails).
- The whole GUI suite is still running on mica at push time; its reds
  are compared against origin/main and fixed or filed after.

Not tried: the tray item and a restart in the real macOS, Windows and
Linux apps (only magpie web was restarted for real).
2026-11-18 12:58:38 +08:00

122 lines
3.9 KiB
Go

package gui
import (
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
// quitSays is a window that says when it quits.
type quitSays struct {
folderOnly
quit chan struct{}
}
func (q quitSays) Quit() { q.quit <- struct{}{} }
// TestRestartStartsMagpieAgain (yansheng1003 on X: "请在刷新旁边增加一个重启
// 按钮"): Settings' Restart starts this magpie again with no update
// waiting, as it is, back on the tab the window was on, and the app quits
// for it. A relaunch that can't be arranged leaves it running and says so.
// magpie web runs again in its own place; a browser on the app's page
// restarts the app.
func TestRestartStartsMagpieAgain(t *testing.T) {
sandboxHome(t)
oldBundle, oldExe, oldRelaunch, oldHands := bundleOf, executable, relaunchBinary, handsOver
t.Cleanup(func() {
bundleOf, executable, relaunchBinary, handsOver = oldBundle, oldExe, oldRelaunch, oldHands
webReexec.Store(false)
})
bundleOf = func() string { return "" }
executable = func() (string, error) { return "/opt/magpie/magpie", nil }
handsOver = func() bool { return false } // Windows': opened once this one has exited
if s := updates.json().State; s == "ready" {
t.Fatalf("an update is staged in the test: %q", s)
}
type relaunch struct {
exe, aside, view string
window bool
}
var got []relaunch
var fail error
relaunchBinary = func(exe, aside string, window bool, view string) error {
got = append(got, relaunch{exe, aside, view, window})
return fail
}
post := func(w Windows, body string) *httptest.ResponseRecorder {
t.Helper()
mux := http.NewServeMux()
restartRoutes(mux, w)
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, httptest.NewRequest("POST", "/api/restart", strings.NewReader(body)))
return rec
}
quits := func(q quitSays) bool {
select {
case <-q.quit:
return true
case <-time.After(2 * time.Second):
return false
}
}
app := quitSays{quit: make(chan struct{}, 1)}
if rec := post(app, `{"view":"settings"}`); rec.Code != http.StatusNoContent {
t.Fatalf("restart: %d %s", rec.Code, rec.Body)
}
if len(got) != 1 || got[0] != (relaunch{"/opt/magpie/magpie", "", "settings", true}) {
t.Fatalf("relaunched %+v, want this binary again on Settings", got)
}
if !quits(app) {
t.Fatal("the app didn't quit for the restart")
}
got, fail = nil, errors.New("no helper to open it again")
if rec := post(app, `{}`); rec.Code != http.StatusBadRequest || !strings.Contains(rec.Body.String(), "couldn't start again") {
t.Fatalf("a relaunch that failed: %d %s", rec.Code, rec.Body)
}
if quits(app) {
t.Fatal("the app quit with nothing to open it again")
}
fail = nil
// a browser on the app's page: the app restarts and quits
got = nil
inner := quitSays{quit: make(chan struct{}, 1)}
if rec := post(appWebHost{webHost{}, inner}, `{"view":"usage"}`); rec.Code != http.StatusNoContent {
t.Fatalf("from a browser: %d %s", rec.Code, rec.Body)
}
if len(got) != 1 || got[0].view != "" {
t.Fatalf("from a browser relaunched %+v: the app, not a tab of the browser's", got)
}
if !quits(inner) {
t.Fatal("the app didn't quit for a browser's restart")
}
// magpie web: run again in its place, nothing relaunched
got = nil
quit := make(chan struct{}, 1)
if rec := post(webHost{quit: func() { quit <- struct{}{} }}, `{}`); rec.Code != http.StatusNoContent {
t.Fatalf("magpie web: %d %s", rec.Code, rec.Body)
}
if len(got) != 0 || !webReexec.Load() {
t.Fatalf("magpie web relaunched %+v, re-exec %v", got, webReexec.Load())
}
select {
case <-quit:
case <-time.After(2 * time.Second):
t.Fatal("magpie web didn't end for its re-exec")
}
}
// TestUpdateSaysItsRun: a page that restarted magpie with no update
// reloads once another run answers; /api/update says which run it is.
func TestUpdateSaysItsRun(t *testing.T) {
if j := updates.json(); j.Run == 0 || j.Run != runStarted {
t.Fatalf("run %d, want %d", j.Run, runStarted)
}
}