Files
magpie/internal/gui/update_test.go
T
yetone daaa11cb53 update: the clock's check leaves a waiting download alone (iiiiiimone on X)
"With magpie my proxy's traffic is far higher than usual." An idle
`magpie web` (a released v0.1.4000 build, sandbox HOME, no accounts, every
request through a counting proxy, 25 minutes in WSL) sent 89.4 MB: 88.6 MB
of it the update download from usemagpie.ai, 0.7 MB models.dev's catalog
(daily), ~80 KB the update feed and notes. Nothing else.

The download is the waste. With a version downloaded and waiting for a
restart, every check by the clock (every 6 hours by default, 30 minutes
at the shortest) found a newer one, as magpie releases 100 to 160 times a
day, and downloaded it whole (37 to 95 MB by platform) in place of the
one waiting: up to ~380 MB a day by default on Windows and ~4.5 GB at 30
minutes, for an app left running, all over the user's proxy.

Now the clock's check keeps the version waiting, as Settings'
NoAutoDownload already did. Nothing is lost: every restart into it asks
the feed first (recheck, the autorestart's waitRecheck) and fetches the
newest then, quitting puts in the one waiting, and a check asked for
(Settings' Check, Download update) still downloads the newest. With
nothing waiting the clock's check downloads as before.

Run:
- TestUpdateClockLeavesWaitingDownload (new): three clock checks with
  newer versions out keep 0.1.11 and one download; recheck and a check
  asked for each fetch the newest.
- go test ./internal/gui (nogui, CGO off) in WSL magpie-desktop at
  b883bd44e (rebased on 92bce5c72): ok.
- go test -run TestUpdate ./internal/gui on Windows (xiaomi-gpu): all
  pass but TestUpdateThatWontStart, which is red the same at origin/main
  5a48a4251 and is #6059.
- Broken by hand: without `|| u.byClock` the new test is red ("0.1.12
  out: ... Latest:0.1.12 ..., 2 downloads").

Not tried: the macOS run (mote was over its load the whole time, mica's
disk is full); the full suite follows on mote. Traffic of a signed-in
magpie (usage reads per account) was not measured: the sandbox had no
accounts.
2026-11-13 16:01:55 +08:00

137 lines
4.4 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())
}
}
// 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). A check asked for, and the feed asked again before a
// restart, still fetch the newest.
func TestUpdateClockLeavesWaitingDownload(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")
// 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 feed asked again before a restart: the newest is fetched
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.14" || downloads.Load() != 2 {
t.Fatalf("recheck: %+v, %d downloads", j, downloads.Load())
}
// and so is it on a check asked for
latest.Store("0.1.15")
u.check()
if j := u.json(); j.State != "ready" || j.Latest != "0.1.15" || downloads.Load() != 3 {
t.Fatalf("check: %+v, %d downloads", j, downloads.Load())
}
}