Files
magpie/internal/gui/update_test.go
T
yetone 9434044773 update: Restart to update puts in the version already downloaded, with downloads on a click too
sakamotokurome on X: "重启能不能跟更新分开,我本来下载一个新版本了,点击restart,
结果它又跑去下载了". With Settings' "Download updates automatically" off, a
version downloaded on a click (Download update) was not what Restart to
update put in: the click's recheck asked the feed first and, magpie having
released again since, downloaded the newer version whole while the user
waited (keepsStaged kept the one waiting only with downloads automatic,
b05a525a3, or for 5 minutes after a restart's own download, elis_pina).
The same held for the tray's Restart to Update and a click's restart that
waited for the gateway to be idle. It is the same code on macOS, Windows
and Linux (internal/gui/update.go); only the install step differs by OS.

Now a version downloaded and waiting is what every restart goes into, as
it is, however it was downloaded, and the feed isn't asked: keepsStaged is
"ready with something staged". The new version's first check, seconds
after it starts, finds whatever is newer, as with downloads automatic.
The feed is still asked first when nothing is staged (another magpie put
the update in), and a click on Download update (state found) still checks
and downloads the newest, so coeo91's "the click takes the newest, not the
last check's" holds for the download; it no longer replaces a download
already made. The notes dialog's freshen keeps the one waiting in both
modes, so it names the version the restart goes into. restartStaged and
forRestart are gone with the 5-minute rule they served.

Tests (internal/gui, fake feed, sandbox HOME, no network):
- new TestRestartPutsInTheClickedDownload: the reporter's sequence (downloads
  on a click, Download update, 0.1.12 released, Restart): the page's click,
  the tray's, a click's idle wait and the dialog all go into 0.1.11, one
  download, the feed not asked. Red on origin/main dbac411b2: "the page's
  click: {State:ready ... Latest:0.1.12}".
- changed to the new rule: TestRestartClickTakesTheNewest (now the found ->
  Download click takes the newest, then Restart downloads nothing),
  TestRestartWaitsForTheFeed (nothing staged), TestRestartAfterItsDownload
  DoesNotAskAgain (no restart asks), TestUpdateStagedGivesWayToNewer (a
  restart's recheck keeps it; Settings' Check takes the newer),
  TestRestartWhenIdleIntoTheNewest -> TestRestartWhenIdleIntoTheWaiting.
  TestTrayRestartTakesTheNewest is removed: its case is the opposite of
  this fix, and the tray is covered by the new test.

Run at this tree:
- mote (macOS 27): go test ./internal/gui -count=1 (whole package) ok at
  this commit rebased on a63923e3d; the update tests -count=3 ok; go vet ./internal/gui,
  GOOS=linux go vet -tags nogui ./internal/gui, GOOS=windows go vet
  ./internal/gui ok; gofmt -l internal/gui empty.
- xiaomi-gpu (Windows 11, win-run) and magpie-desktop (WSL Ubuntu, wsl-run,
  -tags nogui): the update/restart tests ok.

Not run: the full go test ./... suite (mote was at load 142 with another
session's suite running, mica's disk full). Not tried: a real restart of a
real build into a downloaded update; the restart path after keepsStaged is
unchanged.
2026-11-14 18:23:16 +08:00

152 lines
5.0 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/settings"
"github.com/yetone/magpie/internal/update"
)
// A version downloaded and waiting for a restart gives way to a newer one
// out since when a check is asked for (Settings' Check). A restart doesn't
// ask, with downloads on a click too: it goes into the one waiting
// (TestRestartPutsInTheClickedDownload).
func TestUpdateStagedGivesWayToNewer(t *testing.T) {
sandboxHome(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: the restart's recheck leaves 0.1.11, with downloads on
// a click too; a check asked for, downloads automatic, takes 0.1.13
latest.Store("0.1.13")
u.recheck(true)
downloadsOnClick(t)
u.recheck(true)
if j := u.json(); j.State != "ready" || j.Latest != "0.1.11" || downloads.Load() != 1 || u.staged == "" {
t.Fatalf("the restart's recheck: %+v, %d downloads", j, downloads.Load())
}
s := settings.Load()
s.NoAutoDownload = false
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
u.check()
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())
}
}
// The clock's check, with a version downloaded and waiting, doesn't
// download a newer one out since: magpie releases many times a day, and
// each check downloaded the whole app again on the user's proxy
// (iiiiiimone on X). Nor does the feed asked again before a restart, with
// downloads automatic (canlee886 on X). A check asked for still fetches
// the newest.
func TestUpdateClockLeavesWaitingDownload(t *testing.T) {
sandboxHome(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")
// nothing waiting: the clock's check downloads what it finds
u.checkByClock()
if j := u.json(); j.State != "ready" || j.Latest != "0.1.11" || downloads.Load() != 1 {
t.Fatalf("%+v, %d downloads", j, downloads.Load())
}
// newer ones out, check after check: the one waiting stays
for _, v := range []string{"0.1.12", "0.1.13", "0.1.14"} {
latest.Store(v)
u.checkByClock()
if j := u.json(); j.State != "ready" || j.Latest != "0.1.11" || downloads.Load() != 1 || u.staged == "" {
t.Fatalf("%s out: %+v, %d downloads", v, j, downloads.Load())
}
}
// the restart's recheck: the one waiting is what it goes into
u.recheck(true)
if j := u.json(); j.State != "ready" || j.Latest != "0.1.11" || downloads.Load() != 1 {
t.Fatalf("recheck: %+v, %d downloads", j, downloads.Load())
}
// a check asked for takes the newest
latest.Store("0.1.15")
u.check()
if j := u.json(); j.State != "ready" || j.Latest != "0.1.15" || downloads.Load() != 2 {
t.Fatalf("check: %+v, %d downloads", j, downloads.Load())
}
}