Files
magpie/internal/gui/update_test.go
T
yetone 257eab646d update: a click that updates takes the newest the feed has, not the last check's
coeo91 on Discord: "更新行为不对,他会在点击更新按钮后下载上一次或最近一次
获取到的最新版本,而不是实际的最新版本". Clicking Restart to update asked the
feed again, but four paths still acted on what an earlier check had found:

- With Settings' "Download updates automatically" off, the click's check
  found the newer version and kept the staged older one (the hold), so the
  restart went into the older one. The click's check now counts as a click
  (fetch), so the newest is downloaded, then restarted into.
- When the feed took longer than the 10s wait, the route restarted into
  the staged version anyway. It now answers "checking". The header pill
  reads again and restarts once the answer is in; About's Version row
  redraws, and a second click restarts into what the answer found.
- The tray's Restart to update didn't ask the feed at all. It now goes
  through restartAsked: it checks first, or waits for the gateway as the
  page's click does (#577).
- A magpie that can't replace itself opened the release page the last
  check found. When that check is over a minute old, it checks again first.

A current version that is already downloaded is not downloaded again:
the check finds the same version and keeps it. The wait on the feed is
the one the click had already (at most 10s, usually one round trip).

Not changed: usemagpie.ai/api/latest is cached at the edge for 5 minutes
(site/worker.js), so a release less than 5 minutes old may still be
unseen. magpie web's "Open the release page" opens the URL the page
holds, since window.open must run in the click for the popup blocker.

Tests (internal/gui/update_fresh_test.go, a fake feed, no network):
TestRestartClickTakesTheNewest, TestRestartWaitsForTheFeed,
TestTrayRestartTakesTheNewest, TestAvailableOpensTheNewestPage.

Run on mote (macOS 27), at this tree:
- gofmt -l internal/gui: nothing
- go vet ./internal/gui; GOOS=linux go vet -tags nogui; GOOS=windows
  go vet: clean
- go test -tags nogui -count=1 -run 'TakesTheNewest|WaitsForTheFeed|
  NewestPage|Update|Restart' ./internal/gui: ok
- the same five with -race -count=10, with RestartWhenIdle and
  RestartToUpdate: ok

Broken by hand, one at a time, each red in its own test only:
- recheck's fetch = false: TestRestartClickTakesTheNewest, "went into the
  version the last check found: ... Latest:0.1.11"
- the "checking" answer taken out: TestRestartWaitsForTheFeed,
  "restarted before the feed answered: {State:ready ... 0.1.11}"
- restartAsked restarting at once: TestTrayRestartTakesTheNewest, "the
  tray restarted into 0.1.11 with 0.1.12 out"
- the available recheck taken out: TestAvailableOpensTheNewestPage,
  opened [v0.1.11 v0.1.11]

Not tried: a real update from a published release; the tray item on
Windows and Linux (the code is shared, the menu callback runs in its own
goroutine on Wails v3 beta.24).
2026-10-28 23:22:14 +08:00

74 lines
2.1 KiB
Go

package gui
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"sync/atomic"
"testing"
"time"
"github.com/yetone/magpie/internal/update"
)
// A version downloaded and waiting for a restart gives way to a newer one
// out since, so the restart goes straight to the latest.
func TestUpdateStagedGivesWayToNewer(t *testing.T) {
body := []byte("new magpie")
sum := sha256.Sum256(body)
var latest atomic.Value
latest.Store("0.1.11")
var downloads atomic.Int32
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/asset" {
downloads.Add(1)
w.Write(body)
return
}
json.NewEncoder(w).Encode(update.Release{Version: latest.Load().(string), Assets: map[string]update.Asset{
update.BinaryAsset(): {URL: "http://" + r.Host + "/asset", SHA256: hex.EncodeToString(sum[:])},
}})
}))
defer srv.Close()
t.Setenv("MAGPIE_UPDATE_FEED", srv.URL)
old := Version
Version = "0.1.10"
defer func() { Version = old }()
exe, err := update.Executable()
if err != nil {
t.Fatal(err)
}
u := &updater{exe: exe}
u.self, _ = os.Stat(exe)
defer os.Remove(exe + ".new")
u.check()
if j := u.json(); j.State != "ready" || j.Latest != "0.1.11" || downloads.Load() != 1 {
t.Fatalf("%+v, %d downloads", j, downloads.Load())
}
// nothing newer: it stays as it is, not downloaded again
u.check()
if j := u.json(); j.State != "ready" || j.Latest != "0.1.11" || downloads.Load() != 1 {
t.Fatalf("%+v, %d downloads", j, downloads.Load())
}
// the feed down: still ready to restart into what it has
t.Setenv("MAGPIE_UPDATE_FEED", "http://127.0.0.1:1/")
u.check()
if j := u.json(); j.State != "ready" || j.Latest != "0.1.11" {
t.Fatalf("%+v", j)
}
t.Setenv("MAGPIE_UPDATE_FEED", srv.URL)
// 0.1.13 out: that is the one it restarts into
latest.Store("0.1.13")
u.recheck(false)
for u.json().State == "downloading" || u.json().State == "checking" {
time.Sleep(10 * time.Millisecond)
}
if j := u.json(); j.State != "ready" || j.Latest != "0.1.13" || downloads.Load() != 2 || u.staged == "" {
t.Fatalf("%+v, %d downloads", j, downloads.Load())
}
}