Files
magpie/internal/gui/update_stale_test.go
T
yetone 84c50f0328 update: an old "up to date" isn't said, and Settings' Version row asks again (RickyWei)
RickyWei (Discord, Windows, Chinese UI): Settings > 关于 said 版本 0.1.1402 ·
已是最新 with v0.1.1517 out on yetone/magpie-releases.

Cause, from the v0.1.1402 code (internal/gui/update.go is unchanged since):
state "latest" is set only when a check that worked found nothing newer,
and a failed read is state "error" ("Couldn't check for updates"), so the
row's claim is the answer of one check. Nothing aged it: the state stayed
"latest" until the next check, and the row only read it. v0.1.1402 was the
newest release from 13:14 to 13:37 UTC on 10-24 (v0.1.1403 at 13:37), so
a check then said up to date, and with automatic checks off (Settings'
"only when asked") or once a day that stood for the next day while ~115
releases went out. The feed itself is right: usemagpie.ai/api/latest,
with ?lang=zh too, answers 0.1.1517 (gh release list agrees), the version
order is numeric, and nothing on Windows skips the check. Hedged: the
user's settings aren't known, so the interval is inferred, not seen.

Now:
- jsonIn reports a "latest" older than freshAfter (5 min) as "" (not
  asked lately), so no surface says up to date from an old answer.
- GET /api/update?fresh=1, which only Settings' Version row sends, asks
  the feed again over a stale "latest" with automatic updates on; the row
  shows checking and reads again. With them off nothing is asked by
  itself, and the row says "Automatic updates are off" with Check.
  The header pill's reads don't ask, so the feed's cadence is unchanged
  for a window left open.
- app.js stays inside TestCoreScriptBudget: a comment word went.

Run, at this tree:
- TestStaleLatestIsNotUpToDate (new): 1402 checked while newest, then
  the feed at 1517 and the answer aged past freshAfter: the plain read
  isn't "latest" and asks nothing, the row's read finds 1517
  ("available"); with automatic updates off it is "" and asks nothing.
  Red on origin/main's update.go ("an old answer still says up to date"),
  green with it.
- update-check.test.cjs asserts the row reads with fresh=1: red on
  origin/main's app.js (4 of 4 language/engine cases), green now.
- go test -tags nogui ./internal/gui ./internal/update (sandbox HOME): ok.
- The 21 GUI files that fake /api/update plus i18n-keys, Chromium and
  WebKit, en/zh where they cover them: 299 of 300 pass. The red is
  header-centre webkit (ResizeObserver loop as a page error), red the
  same on untouched origin/main d58053720 2 of 2, filed as #3479.
- gofmt -l, go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./..., go build: clean.

Rebased onto c888ef272 after that run: go test -tags nogui ./internal/gui
./internal/update (TestCoreScriptBudget in it), the Windows and Linux
vet of internal/gui and gofmt again there: ok.

Not tried: the full go test ./... and the whole GUI suite (load ~160);
a real Windows install; the clock's monotonic reading across a Mac or
Linux sleep (Windows' InterruptTime counts sleep, so it isn't this case).
A v0.1.1402 that says up to date finds the update with a click on 检查
(the click asks the feed itself); one with automatic checks off keeps
needing that click, or a download from usemagpie.ai.
2026-10-25 18:22:13 +08:00

102 lines
3.0 KiB
Go

package gui
import (
"encoding/json"
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
"time"
"github.com/yetone/magpie/internal/settings"
"github.com/yetone/magpie/internal/update"
)
// "Up to date" is about the moment the feed was asked. RickyWei's Version
// row said 已是最新 on v0.1.1402 with v0.1.1517 out: the answer of a check
// made while 1402 was the newest, never made again. A "latest" older than
// freshAfter is no longer said, and the Version row (fresh=1) asks again
// with automatic updates on; with them off it asks nothing by itself.
func TestStaleLatestIsNotUpToDate(t *testing.T) {
sandboxHome(t)
var newest atomic.Value
newest.Store("0.1.1402")
var asks atomic.Int32
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
asks.Add(1)
json.NewEncoder(w).Encode(update.Release{Version: newest.Load().(string)})
}))
defer srv.Close()
t.Setenv("MAGPIE_UPDATE_FEED", srv.URL)
oldV := Version
Version = "0.1.1402"
defer func() { Version = oldV }()
u := &updater{} // nothing it may replace: a newer one is "available"
oldU := updates
updates = u
defer func() { updates = oldU }()
mux := http.NewServeMux()
updateRoutes(mux, nil)
get := func(path string) updateJSON {
t.Helper()
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, httptest.NewRequest("GET", path, nil))
var j updateJSON
if err := json.Unmarshal(rec.Body.Bytes(), &j); err != nil {
t.Fatalf("%s: %v %s", path, err, rec.Body)
}
return j
}
age := func() {
u.mu.Lock()
u.asked = time.Now().Add(-freshAfter - time.Minute)
u.mu.Unlock()
}
u.check()
if j := get("/api/update?fresh=1&lang=zh"); j.State != "latest" {
t.Fatalf("just checked: %+v", j)
}
if asks.Load() != 1 {
t.Fatalf("a fresh latest asked the feed again: %d asks", asks.Load())
}
// a day on, 115 releases out, no check since
newest.Store("0.1.1517")
age()
if j := get("/api/update?lang=zh"); j.State == "latest" {
t.Fatalf("an old answer still says up to date: %+v", j)
}
if asks.Load() != 1 {
t.Fatalf("a read other than the Version row's asked the feed: %d asks", asks.Load())
}
j := get("/api/update?fresh=1&lang=zh")
for deadline := time.Now().Add(10 * time.Second); j.State == "checking" && time.Now().Before(deadline); j = get("/api/update?fresh=1&lang=zh") {
time.Sleep(10 * time.Millisecond)
}
if j.State != "available" || j.Latest != "0.1.1517" {
t.Fatalf("the Version row drawn over an old answer: %+v", j)
}
// automatic updates off: the old answer isn't said, and nothing is asked
s := settings.Load()
s.NoAutoUpdate = true
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
newest.Store("0.1.1402")
Version = "0.1.1517"
u.check()
if j := get("/api/update?fresh=1"); j.State != "latest" {
t.Fatalf("checked by a click: %+v", j)
}
age()
n := asks.Load()
if j := get("/api/update?fresh=1"); j.State != "" {
t.Fatalf("auto updates off, old answer: %+v", j)
}
if asks.Load() != n {
t.Fatal("asked the feed with automatic updates off")
}
}