Files
magpie/internal/gui/update_test.go
T
yetone b05a525a33 update: Restart to update goes into the download waiting when downloads are automatic
canlee886 on X: auto-download had a version ready, but magpie releases
many times a day, so by the time they pressed Restart to update a newer
one was out. The click asked the feed (coeo91's "take the newest") and
downloaded the newer one whole while they waited, which made the
download done ahead of time useless ("这个自动下载在如此频繁的更新之下
简直就是摆设了").

Two ways to fix it: keep the staged copy current (check more often,
download each newer one), or restart into the staged copy and let the
next check fetch anything newer. This takes the second:
- the restart is at once, which is what the download ahead of time is for;
- no more bytes from R2 than before: one download per update. Keeping
  the copy current would download 40 to 95 MB per release, several times
  a day, which iiiiiimone's fix (the clock leaves the one waiting) took
  out;
- the new version's first clock check, 5s after it starts, fetches
  anything newer in the background, and the next restart goes into that.

So with downloads automatic (the default), recheck leaves a ready
download alone and doesn't ask the feed (keepsStaged). That covers the
page's Restart to update, the tray's restart, and Restart when idle.
The notes dialog's freshen (#910) still asks the feed for late notes
(#649), but with `keep` set it leaves the staged copy as the clock's
check does, so the dialog names the version the restart goes into.
With "Download updates automatically" off, nothing changes: a click asks
the feed and downloads the newest (coeo91).

Tests: TestRestartTakesTheDownloadWaiting (new) covers the page's click,
the tray's restartAsked, AutoRestart's waitIdle and a stale freshen. Each
keeps 0.1.11 with one download while the feed has 0.1.12, and a later
check takes 0.1.12. The coeo91 and #910 tests now set NoAutoDownload
(downloadsOnClick), the behaviour they guard.
TestUpdateClockLeavesWaitingDownload and TestUpdateNotesAskTheFeedAgain
expect the staged copy kept under auto-download.

Run (mote-run, at this change rebased on 414517ccf, with the next commit):
  go test -count=1 -timeout 45m ./internal/gui/... .
  internal/gui ok (129s); the root package's TestNoEagerPackageRegexps is
  red the same on plain 414517ccf (internal/davsync/setuptext.go:73,
  filed as #6534), nothing else.
  go vet ./internal/gui/, GOOS=linux go vet -tags nogui ./internal/gui/,
  GOOS=windows go vet ./internal/gui/: ok. gofmt -l clean.
Broken by hand: keepsStaged returning false makes
TestRestartTakesTheDownloadWaiting ("the page's click: Latest 0.1.12")
and TestUpdateClockLeavesWaitingDownload ("2 downloads") red.
Not tried: a real restart of the packaged app across two releases.
2026-11-13 19:31:37 +08:00

140 lines
4.5 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, with downloads on
// a click (with them automatic it is restarted into as it is:
// TestRestartTakesTheDownloadWaiting).
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: that is the one it restarts into
latest.Store("0.1.13")
downloadsOnClick(t)
u.recheck(true)
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())
}
}