Files
magpie/internal/update/relaunch_open_test.go
T
yetone 27a8255fd7 update: the Mac relaunch opens a new instance (open -n), not a reopen of the gone one (Linka on Discord)
Linka's log (Discord, macOS 27.0.1, v0.1.1308) shows the cause of the
"magpie stays closed after updating itself" report that molvqingtai also
hit with the screen locked. After the old process's "Termination
complete", open found LaunchServices still holding the gone app as
running ("Specified target process 41267 does not exist"), so it sent
that process a reopen Apple event (aevt/rapp) instead of launching the
bundle, and failed with -600 procNotFound ("LAUNCH: Launch failed in
CSUI"). The retries from fb23bca5c asked for the same reopen each time.

Fix (internal/update/update.go): relaunchOpen is `open -n <bundle>`,
with `--args tray` when no window was up. -n asks LaunchServices for a
new instance, so it launches the bundle rather than look for a running
one. The retries and f3eb92924's direct start of the bundle's program
stay as the backup, with relaunch.log.

A magpie that really is up still has one instance: the new process
takes internal/gui's instance lock (a flock on app-<hash>.lock, keyed by
the program's path), finds it held and hands over (runningAlready): a
tray start ends, a window start asks the running one to show its window.

Other opens, checked: OpenBeside (a95f0b83f, the handover restart) uses
open -n already, and when it never starts the new version the handover
falls back to Relaunch after helloWait. The Codex app restart
(673651e8d, internal/agent/codex_app.go) opens another app and is
unchanged, as are the link and `open -b` handovers in instance_darwin.go.

Tests: TestRelaunchOpen wants -n in both commands, and
TestRelaunchOpenTriedAgain's fake open records its arguments and wants
"-n <bundle> --args tray". fakeOpen names the .app argument in its -600
text rather than $1. Red by hand: with -n dropped from relaunchOpen,
TestRelaunchOpen failed for both commands and TestRelaunchOpenTriedAgain
failed with opened "<bundle> --args tray"; green with it.

Real run: a scratch Lock.app (LSUIElement, ad-hoc signed) whose program
takes the same flock as holdInstance and logs its args. Real
`open -n Lock.app --args tray` twice, 1.5s apart: the first got
args=[tray] and held the lock, the second got args=[tray], found the
lock held and handed over. With one instance up, a plain
`open Lock.app --args tray` exited 0 and started nothing (routed to
the running one), the shape of Linka's reopen. The scratch app was
unregistered from LaunchServices afterwards.

Run at 17a2b00ef plus this change: gofmt -l internal/ (clean);
go vet ./... (darwin); GOOS=linux go vet -tags nogui ./...;
GOOS=windows go vet ./...; go test -count=1 ./internal/update (ok).
Rebased onto f4640c0ae (sessions GUI only); go test -count=1
./internal/update ok again at the pushed tree.

Not tried: a real update with LaunchServices holding a stale
registration (Linka's case) or with the screen locked; the real magpie
binary under open -n (the handover's OpenBeside has run that way since
a95f0b83f).
2026-10-24 02:32:29 +08:00

55 lines
2.0 KiB
Go

package update
import (
"strings"
"testing"
)
// A Mac restart by itself with no window up opens the app as a login launch
// does, its menu bar icon only (#2096); one with the window opens it. Both
// ask for a new instance (-n), so LaunchServices launches the bundle rather
// than send a reopen to the magpie that has just exited (Linka's -600).
func TestRelaunchOpen(t *testing.T) {
if got, want := relaunchOpen("/Applications/magpie.app", true), `open -n "/Applications/magpie.app"`; got != want {
t.Errorf("window: %s, want %s", got, want)
}
if got, want := relaunchOpen("/Applications/magpie.app", false), `open -n "/Applications/magpie.app" --args tray`; got != want {
t.Errorf("no window: %s, want %s", got, want)
}
}
// A restart that hands the gateway over opens the new version while this
// one runs: a new instance (-n), which LaunchServices would otherwise send
// to the one running.
func TestOpenBeside(t *testing.T) {
if got, want := openBeside("/Applications/magpie.app", true), `open -n "/Applications/magpie.app"`; got != want {
t.Errorf("window: %s, want %s", got, want)
}
if got, want := openBeside("/Applications/magpie.app", false), `open -n "/Applications/magpie.app" --args tray`; got != want {
t.Errorf("no window: %s, want %s", got, want)
}
}
// The new version a handover starts isn't told to wait for this one to
// exit (it would wait while this one waits for it), and gets the args
// RelaunchBinary gives.
func TestStartBesideWaitsForNothing(t *testing.T) {
t.Setenv("MAGPIE_REPLACES", "")
var gotArgs, gotEnv []string
stubLaunch(t, func(exe string, args, env []string) (<-chan struct{}, func(), error) {
gotArgs, gotEnv = args, env
return nil, nil, nil
})
if err := StartBeside("/opt/magpie", true, "routing"); err != nil {
t.Fatal(err)
}
if strings.Join(gotArgs, " ") != "gui routing" {
t.Errorf("args %q", gotArgs)
}
for _, e := range gotEnv {
if strings.HasPrefix(e, "MAGPIE_REPLACES=") && e != "MAGPIE_REPLACES=" {
t.Errorf("told to wait: %s", e)
}
}
}