Files
magpie/internal/gui/quiet_restart_test.go
T
yetone 244cf49371 update: a restart magpie makes by itself comes back as it was, without taking focus
xyxyxy_15591 (Discord #feedback) asked for a silent auto-update: once an
update is in, the window shouldn't pop up. With "Restart automatically"
(Settings, beside Version) on, the window state was already kept (a
closed window came back as the tray icon only), and What's New stays
quiet after an in-app restart. What did show was magpie taking focus:

- macOS relaunched with `open -n` and no `-g`, so LaunchServices brought
  magpie to the front even when only the menu-bar icon came back;
- with the window open, showHere's Focus() activated the app (every OS);
- joining the Dock activated magpie, and with Dock on Wails' Regular
  policy activated it at launch.

Now a restart magpie makes by itself (AutoRestart, `magpie update` from
a terminal) writes a one-shot restart-quiet mark (read once, good for
10 minutes). The next start reads it: `open -n -g`, no Focus(), the Dock
joined without activating, Accessory policy at launch and the Dock set
afterwards. A restart the user clicks removes any mark and comes to the
front as before. `open` gets -g also whenever no window is wanted. No
new setting: zero-config, the existing switch is the one asked for.

Run (base 841326709, rebased on ced880ef0, no upstream change to
these files):
- mote: gofmt -l clean; go vet ./internal/gui ./internal/update
  ./internal/cli; GOOS=linux go vet -tags nogui ./...; GOOS=windows go
  vet on gui/update/cli: clean. internal/update ok;
  TestRestartBySelfComesBackQuietly, TestUpdateThatWontStart,
  TestWhatsNew*, *Handover* ok, except
  TestHandoverLeavesTheMenuBarIconForTheNextMagpie, killed at 30s the
  same at origin/main 7b09d2c95 (AppKit on mote, #4125).
- Windows (win-run, CGO_ENABLED=0): internal/update ok; internal/gui
  reds the same set as origin/main 7b09d2c95 (#4115, #1827, #2958,
  #4514).
- WSL (wsl-run): full `go test -tags nogui -timeout 45m ./...`, 80 ok;
  reds TestInstalledVersion, TestGrokInstalled, TestOmpDetectedWithPiDir
  (#4033, #3548), TestNoCommandBypassesProc (#4491),
  TestSettingsSaveKeepsWhatItDoesNotSend (#4514) all red the same at
  origin/main 7b09d2c95; TestColdTurnsTaken (#4510) passed there alone.
  internal/gui + internal/update rerun at this head: internal/update ok; internal/gui red only TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and TestColdTurnsTaken (#4510).

Broken by hand: without -g, TestRelaunchOpen and
TestRelaunchOpenTriedAgain fail; without markQuietRestart,
TestRestartBySelfComesBackQuietly fails "window true: the start after a
restart by itself isn't quiet"; without the unmark on a clicked restart,
it fails "a restart the user asked for is marked quiet".

Not tried: a real update restart on any OS (`open` of a GUI app over
ssh on mote fails, RBSRequestErrorDomain 5), so -g and no-focus weren't
seen in a real session. Windows' ShowWindow inside Wails may still
bring the window forward when it was open; Linux window managers were
not tried. No GUI assets changed, so no Playwright run.
2026-10-31 15:56:40 +08:00

102 lines
3.2 KiB
Go

package gui
import (
"os"
"path/filepath"
"strconv"
"syscall"
"testing"
"time"
"github.com/yetone/magpie/internal/update"
)
// xyxyxy_15591 on Discord: an update put in by itself shouldn't put the
// window up. A restart by itself (Settings' AutoRestart) marks itself, so
// the new version comes back as the old one was without taking focus; a
// "Restart to update" the user clicked doesn't, and comes to the front as
// it always has. The mark is read once, and one left by a restart that
// never came back (stale) or refused (#894) doesn't make a later start
// quiet.
func TestRestartBySelfComesBackQuietly(t *testing.T) {
home := t.TempDir()
t.Setenv("XDG_CONFIG_HOME", home)
mark := filepath.Join(home, "magpie", quietRestartFile)
exe := filepath.Join(t.TempDir(), "magpie")
os.WriteFile(exe, []byte("old"), 0o755)
self, _ := os.Stat(exe)
os.WriteFile(exe, []byte("newer"), 0o755) // in already: install has nothing to swap
o := updates
updates = &updater{exe: exe, self: self, latest: &update.Release{Version: "0.1.9999"}}
defer func() { updates = o }()
var refuse bool
wasRelaunch := relaunchBinary
relaunchBinary = func(exe, old string, window bool, view string) error {
if refuse {
return &update.BlockedError{Err: &os.PathError{Op: "fork/exec", Path: exe, Err: syscall.Errno(4551)}}
}
return nil
}
defer func() { relaunchBinary = wasRelaunch }()
// clicked: no mark, the new one comes to the front, even after a
// restart by itself that left one (it never came back)
markQuietRestart()
if !restartToUpdate(false, true, "") {
t.Fatal("restart to update failed")
}
if _, err := os.Stat(mark); !os.IsNotExist(err) {
t.Fatalf("a restart the user asked for is marked quiet: %v", err)
}
if takeQuietRestart() {
t.Fatal("the start after a clicked restart is quiet")
}
// by itself, window up or not: marked, read once
for _, window := range []bool{true, false} {
if !restartInto(false, window, "", true) {
t.Fatalf("window %v: restart by itself failed", window)
}
if !takeQuietRestart() {
t.Fatalf("window %v: the start after a restart by itself isn't quiet", window)
}
if takeQuietRestart() {
t.Fatalf("window %v: the mark is read twice", window)
}
}
// magpie web restarts in place: nothing to mark
if !restartInto(true, false, "", true) {
t.Fatal("web restart failed")
}
webReexec.Store(false)
if takeQuietRestart() {
t.Fatal("a web restart marked the app's next start quiet")
}
// the new version refused (#894): this one runs on, unmarked
refuse = true
if restartInto(false, true, "", true) {
t.Fatal("a refused restart said to quit")
}
if _, err := os.Stat(mark); !os.IsNotExist(err) {
t.Fatalf("a refused restart left its mark: %v", err)
}
// a mark from a restart that never came back is not this start's
os.MkdirAll(filepath.Dir(mark), 0o755)
old := strconv.FormatInt(time.Now().Add(-quietFor-time.Minute).Unix(), 10)
os.WriteFile(mark, []byte(old+"\n"), 0o644)
if takeQuietRestart() {
t.Fatal("a stale mark made this start quiet")
}
if _, err := os.Stat(mark); !os.IsNotExist(err) {
t.Fatalf("a stale mark is kept: %v", err)
}
os.WriteFile(mark, []byte("garbage"), 0o644)
if takeQuietRestart() {
t.Fatal("an unreadable mark made this start quiet")
}
}