Files
magpie/internal/gui/update_autorestart_test.go
T
yetone fbb7e2cbd4 gui, update: Settings' Restart automatically restarts into a downloaded update once no agent request is in flight (#2096, xiaozhu1337)
#2096 asks for a setting that installs and restarts whenever an update is
there. Settings > About gets "Restart automatically" (Off/On), off by
default: a restart ends the window, and whatever an agent sends during it
waits for the gateway to come back.

When on, and Automatic updates is on, the updater's minute loop sees a
"ready" update and starts the same idle wait the Install-when-idle button
uses (waitIdle: 10s with nothing in flight, at most 1h). Cancelling that
wait from the GUI skips that version, so the next one is restarted into
again. The app (window or tray) and `magpie web` are covered; serve and
daemon have no in-process updater and are not. On the Mac, a restart the
app made by itself reopens to the menu bar icon only (`open … --args tray`)
when no window was shown, rather than popping a window up unasked
(update.Relaunch takes a window flag; relaunchOpen builds the command).
With Automatic updates off the row is dimmed and reads "While automatic
updates are on", like Check every.

Run:
- go test: internal/gui (-tags nogui, full package, 273s), internal/update,
  internal/settings, internal/cli — ok, with set -o pipefail.
- New: TestAutoRestartWaitsForIdle, TestAutoRestartNeedsAReadyUpdate,
  TestAutoRestartCalledOff, TestRelaunchOpen.
- Broken by hand: without autoTick, WaitsForIdle and CalledOff fail;
  without the autoSkip assignment, CalledOff fails; restarting without
  the idle wait fails WaitsForIdle and CalledOff; without prefsKeep's
  autoRestart, update-restart.test.cjs fails 11 of 11.
- gofmt -l clean; go vet and go build for darwin, GOOS=linux -tags nogui
  and GOOS=windows on internal/gui, update, settings, cli.
- GUI: new update-restart.test.cjs, chromium and webkit, 1100 and 420,
  en/zh/zh-TW/ja/de (22 pass, twice); update-*, i18n-keys and settings*
  in both engines: 253 pass, 0 fail.
- Found on the way, filed as #2134: in WebKit at 420, turning Automatic
  updates off scrolls a view at its top by ~2px (the Version row's sub
  grows). The test checks the clicked control holds still instead.

Not tried: a real update or relaunch (no real install was touched), the
tray-only reopen on a real bundle, Windows/Linux restarts by hand.
2026-10-14 15:19:14 +08:00

98 lines
3.3 KiB
Go

package gui
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"sync/atomic"
"testing"
"time"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/settings"
"github.com/yetone/magpie/internal/update"
)
// #2096: with Settings' AutoRestart on, a downloaded update is restarted
// into by itself, but only once the gateway has nothing in flight; off, or
// with automatic updates off, it waits for a click as before.
func TestAutoRestartWaitsForIdle(t *testing.T) {
busy := &fakeBusy{}
busy.set(gateway.Busy{Requests: 1})
u := &updater{state: "ready", latest: &update.Release{Version: "0.1.999"}}
quick(t, busy, u)
var restarts atomic.Int32
u.autoRestart = func() bool { restarts.Add(1); return true }
for _, s := range []settings.Settings{{}, {AutoRestart: true, NoAutoUpdate: true}} {
u.autoTick(s)
if w, _ := u.waitingFor(); w {
t.Fatalf("%+v: a restart waits", s)
}
}
on := settings.Settings{AutoRestart: true}
u.autoTick(on)
if w, _ := u.waitingFor(); !w {
t.Fatal("AutoRestart on, update ready: no restart waits")
}
time.Sleep(150 * time.Millisecond)
if restarts.Load() != 0 {
t.Fatal("restarted with a request in flight")
}
u.autoTick(on) // a tick while waiting doesn't start a second wait
busy.set(gateway.Busy{Last: time.Now()})
eventually(t, "never restarted once idle", func() bool { return restarts.Load() == 1 })
time.Sleep(50 * time.Millisecond)
if n := restarts.Load(); n != 1 {
t.Fatalf("restarted %d times", n)
}
}
// Nothing downloaded, nothing to restart into: no wait.
func TestAutoRestartNeedsAReadyUpdate(t *testing.T) {
busy := &fakeBusy{}
for _, state := range []string{"latest", "downloading", "checking", "error", "available"} {
u := &updater{state: state, latest: &update.Release{Version: "0.1.999"}}
quick(t, busy, u)
u.autoRestart = func() bool { t.Errorf("%s: restarted", state); return true }
u.autoTick(settings.Settings{AutoRestart: true})
if w, _ := u.waitingFor(); w {
t.Errorf("%s: a restart waits", state)
}
}
}
// The update card's Cancel calls the restart off for that version: the
// next tick doesn't wait for it again, a newer one is restarted into.
func TestAutoRestartCalledOff(t *testing.T) {
busy := &fakeBusy{}
busy.set(gateway.Busy{Requests: 1})
old := updates
updates = &updater{state: "ready", latest: &update.Release{Version: "0.1.999"}}
t.Cleanup(func() { updates = old })
quick(t, busy, updates)
var restarts atomic.Int32
updates.autoRestart = func() bool { restarts.Add(1); return true }
on := settings.Settings{AutoRestart: true}
updates.autoTick(on)
mux := http.NewServeMux()
updateRoutes(mux, &quitCount{})
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, httptest.NewRequest("POST", "/api/update/install", strings.NewReader(`{"when":"cancel"}`)))
var j updateJSON
if rec.Code != 200 || json.Unmarshal(rec.Body.Bytes(), &j) != nil || j.Waiting {
t.Fatalf("cancel: %d %s", rec.Code, rec.Body)
}
busy.set(gateway.Busy{})
updates.autoTick(on)
if w, _ := updates.waitingFor(); w {
t.Fatal("waits again for the version called off")
}
updates.mu.Lock()
updates.latest = &update.Release{Version: "0.1.1000"}
updates.mu.Unlock()
updates.autoTick(on)
eventually(t, "a newer version wasn't restarted into", func() bool { return restarts.Load() == 1 })
}