Files
magpie/internal/gui/update_remote_test.go
T
yetone d32a7a70cb remote magpie: once magpie updates, the Remote magpies it uses update too
nnlrl_75403 on Discord: a magpie used as another's Remote magpie stayed on
its old version after the local one updated, and had to be updated by
hand on the other computer. Now nothing needs setting up:

- provider.KeepRemotesUpdated (started by the app's updater and by
  magpie serve) runs UpdateRemotes 30 s after start and hourly. A
  released build with automatic updates on reads each Remote magpie's
  version (GET /v1) and, when it is older, asks it through
  POST /v1/magpie/update?version=<v> with the provider's key, at most
  once in six hours per version. Off here, or a dev build: nothing asked.
- The remote updates as its own automatic update would: the app
  downloads and restarts once idle (updater.updateAsked → download,
  waitIdle); magpie serve puts the latest in where it runs from, one at
  a time, never elevated, and serveFollow restarts into it. Automatic
  updates or downloads off there: it answers "off" and stays.
- A copy that can't replace itself says why: a container (docker pull
  ghcr.io/yetone/magpie:latest), a package manager's (its command), a
  folder it can't write, a build from source. A magpie from before the
  route answers 404 ("too old"), a read-only or held key is "refused".
  The provider's row on the Providers page adds a short status to its
  line ("updating to v…" for 30 min, or "can't update itself" etc.),
  with the reason and the command in its tooltip. en/zh/zh-TW/ja/de.
- The route replaces the program, so it takes the Codex reset's
  admission: another machine only while shared, never a browser page,
  an enabled key with full access from loopback too (401 without one),
  read-only 403 gateway_key_read_only, held 403 gateway_key_held. It is
  in readOnlyRefused and the CORS route list.

App.js stays under TestCoreScriptBudget by cutting two comments; the
budget is not raised.

Run (on mote, at the tree committed here):
- gofmt -l internal/: clean. go vet of gateway, gui, cli, provider for
  darwin, GOOS=linux and GOOS=windows: ok.
- go test -count=1 ./internal/gui ./internal/cli ./internal/provider
  ./internal/gateway: gui and cli ok but for #5644's two settings tests
  (red on main since 6b7ee4a98, owned); provider and gateway were
  still running at the push, and go in the full run after it.
- Rebased on origin/main c7c05dcf2, again: gofmt, vet, the new tests,
  ReadOnlyKeyEveryRoute, CORS, TestCoreScriptBudget (not raised) and
  both GUI files 32 of 32: ok.
- New: TestRemoteUpdateNeedsAFullKey (no key from loopback, Origin null,
  an app scheme, *.localhost, a rebound Host, a foreign page with and
  without the full key, another machine with none or a wrong key,
  read-only and held keys, then not shared; the updater is never
  called until a full key asks, from LAN and loopback),
  TestRemoteMagpieAskedToFollow (UpdateRemotes against a real gateway:
  dev build, same and newer remote, NoAutoUpdate here, asked once per
  version, container answer kept, read-only key refused, a 404 magpie
  "old"), TestUpdateAskedByAnotherMagpie, TestRemoteUpdateNote,
  TestServeUpdateAsked.
- GUI: remote-update.test.cjs (new) and remote-self.test.cjs, Chromium
  and WebKit, 1100 and 420 wide, en/zh/ja/de: 32 of 32 ok, with every
  key checked in zh, zh-TW, ja and de. i18n-en-no-cjk ok. i18n-keys'
  "a plugin's words are its own" is red, the same on origin/main
  f8525323d (#5327/#5332, #5426, #5482); its other cases ok.

Broken by hand, each red, then put back: the handler's key check
(loopback without a key got 200 updating), its held-key check, the
route itself (404 = the old code), asking a remote that is current,
ignoring NoAutoUpdate here, asking again for the same version, the
app ignoring automatic updates off, the app never waiting to restart,
serve's single flight (2 downloads), serve's container answer.
The handler's own Origin and not-shared checks stay green when removed:
corsGuard and lanGuard refuse those first; they are kept as the Codex
reset keeps them.

Not tried: a real update of a real remote (a Docker magpie and an app
on another Mac) end to end; Windows and Linux runs of the new tests
(vet only); the full suite (run after the push).

Reported-by: nnlrl_75403
2026-11-08 03:13:15 +08:00

138 lines
5.0 KiB
Go

package gui
import (
"strings"
"sync/atomic"
"testing"
"time"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/settings"
"github.com/yetone/magpie/internal/update"
)
// nnlrl_75403 on Discord: the magpie using this one as its Remote magpie
// updated, and asks this one to follow. It updates as its own automatic
// update would, restarting once nothing is in flight, or says why not:
// already there, the user's updates off, a container, a package
// manager's copy, a folder it can't write, a build from source.
func TestUpdateAskedByAnotherMagpie(t *testing.T) {
was := Version
t.Cleanup(func() { Version = was })
Version = "v0.1.100"
busy := &fakeBusy{}
busy.set(gateway.Busy{Requests: 1})
// a download under way: begin finds it, and the ask waits for it
u := &updater{state: "downloading", exe: "/opt/magpie/magpie", latest: &update.Release{Version: "v0.1.200"}}
quick(t, busy, u)
var restarts atomic.Int32
u.autoRestart = func() bool { restarts.Add(1); return true }
if got := u.updateAsked("v0.1.100"); got.State != "current" {
t.Fatalf("same version: %+v", got)
}
if got := u.updateAsked("v0.1.99"); got.State != "current" {
t.Fatalf("an older asker: %+v", got)
}
for _, s := range []settings.Settings{{NoAutoUpdate: true}, {NoAutoDownload: true}} {
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
if got := u.updateAsked("v0.1.200"); got.State != "off" {
t.Fatalf("%+v: %+v", s, got)
}
if w, _ := u.waitingFor(); w {
t.Fatalf("%+v: a restart waits", s)
}
}
if err := settings.Save(settings.Settings{}); err != nil {
t.Fatal(err)
}
if got := u.updateAsked("v0.1.200"); got.State != "updating" {
t.Fatalf("newer asker: %+v", got)
}
if w, _ := u.waitingFor(); !w {
t.Fatal("no restart waits for the download")
}
u.mu.Lock()
u.state = "ready"
u.mu.Unlock()
time.Sleep(100 * time.Millisecond)
if restarts.Load() != 0 {
t.Fatal("restarted with a request in flight")
}
busy.set(gateway.Busy{Last: time.Now().Add(-time.Minute)})
eventually(t, "never restarted once idle", func() bool { return restarts.Load() == 1 })
Version = "dev"
if got := (&updater{exe: "/x"}).updateAsked("v0.1.200"); got.State != "unsupported" || got.Why != "source" {
t.Fatalf("a build from source: %+v", got)
}
Version = "v0.1.100"
restart := func() bool { return true }
for _, c := range []struct {
u *updater
why string
}{
{&updater{stuck: "container", autoRestart: restart}, "container"},
{&updater{stuck: "not-writable", autoRestart: restart}, "not-writable"},
{&updater{stuck: "translocated", autoRestart: restart}, "not-writable"},
{&updater{stuck: "package", packaged: &update.Packaged{Manager: "pacman", Package: "magpie-bin"}, autoRestart: restart}, "package"},
// magpie serve's place, with nothing that restarts it here
{&updater{exe: "/opt/magpie/magpie"}, "not-writable"},
} {
got := c.u.updateAsked("v0.1.200")
if got.State != "unsupported" || got.Why != c.why {
t.Errorf("%s: %+v", c.why, got)
}
if c.why == "package" && got.Manager == "" {
t.Errorf("package: no manager named: %+v", got)
}
if w, _ := c.u.waitingFor(); w {
t.Errorf("%s: a restart waits", c.why)
}
}
}
// The row of a Remote magpie says what its last ask came to, in words
// the page translates: updating for a while, or why it didn't, with the
// command a container or a package manager is updated by.
func TestRemoteUpdateNote(t *testing.T) {
provider.ForgetRemoteUpdatesForTest()
t.Cleanup(provider.ForgetRemoteUpdatesForTest)
now := time.Now()
if j := remoteUpdateNote("office", now); j != nil {
t.Fatalf("never asked: %+v", j)
}
cases := []struct {
out provider.RemoteUpdate
say, tip string
}{
{provider.RemoteUpdate{State: "updating", Version: "v0.1.200"}, "updating to {v}", "{v}"},
{provider.RemoteUpdate{State: "unsupported", Why: "container"}, "can't update itself", "docker pull ghcr.io/yetone/magpie:latest"},
{provider.RemoteUpdate{State: "unsupported", Why: "package", Manager: "Homebrew", Command: "brew upgrade magpie"}, "can't update itself", "{cmd}"},
{provider.RemoteUpdate{State: "unsupported", Why: "not-writable"}, "can't update itself", "update it there"},
{provider.RemoteUpdate{State: "off"}, "automatic updates off", "turned off"},
{provider.RemoteUpdate{State: "old"}, "update it there", "too old"},
{provider.RemoteUpdate{State: "refused"}, "update it there", "full access"},
}
for _, c := range cases {
provider.SetRemoteUpdateForTest("office", c.out)
j := remoteUpdateNote("office", now)
if j == nil || j.Say != c.say || !strings.Contains(j.Tip, c.tip) {
t.Errorf("%+v: %+v", c.out, j)
}
}
provider.SetRemoteUpdateForTest("office", provider.RemoteUpdate{State: "current"})
if j := remoteUpdateNote("office", now); j != nil {
t.Fatalf("current: %+v", j)
}
provider.SetRemoteUpdateForTest("office", provider.RemoteUpdate{State: "updating", Version: "v0.1.200"})
if j := remoteUpdateNote("office", now.Add(time.Hour)); j != nil {
t.Fatalf("updating, an hour on: %+v", j)
}
}