diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index 7b4e7b156..c2e977100 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -22858,9 +22858,9 @@ async function renderUpdate(r, u) { if (u == null) { const early = !updateBusy && updateLast && !r.querySelector(".sub"); if (early) renderUpdate(r, updateLast); - u = await api(updatePath("update")).catch(() => null); + u = await api(updatePath("update?fresh=1")).catch(() => null); if (!u || !r.isConnected || seqOf(r) !== seq) return; - if (early) updateSeq.set(r, seq + 1); // stops its timers + if (early) updateSeq.set(r, seq + 1); // stops timers } else if (!r.isConnected) return; updateLast = u; const who = r.querySelector(".who"), val = r.querySelector(".val"); diff --git a/internal/gui/tests/update-check.test.cjs b/internal/gui/tests/update-check.test.cjs index bff343e76..9221f0c93 100644 --- a/internal/gui/tests/update-check.test.cjs +++ b/internal/gui/tests/update-check.test.cjs @@ -54,6 +54,7 @@ function server(lang, ctl) { } if (url.pathname === "/api/update") { ctl.gets++; + if (url.searchParams.get("fresh") === "1") ctl.freshGets++; const body = ctl.inflight ? { state: "checking", current: ctl.update.current || "0.1.400" } : { ...ctl.update }; if (ctl.holdGets) await new Promise((go) => getWait.push(go)); return json(body); @@ -68,7 +69,7 @@ function server(lang, ctl) { } function fresh(over) { - return { update: { state: "latest", current: "0.1.400" }, posts: 0, gets: 0, inflight: 0, holdPost: false, holdGets: false, ...over }; + return { update: { state: "latest", current: "0.1.400" }, posts: 0, gets: 0, freshGets: 0, inflight: 0, holdPost: false, holdGets: false, ...over }; } const viewTop = (page) => page.locator("#view-settings").evaluate((v) => v.scrollTop); @@ -127,6 +128,9 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", await button.waitFor(); assert.equal(await button.isEnabled(), true); assert.equal((await row.locator(".sub").textContent()).trim(), w.latest); + // the row's read asks for a fresh answer: an old "latest" is asked + // again (RickyWei: 已是最新 on 0.1.1402 with 0.1.1517 out) + assert(ctl.freshGets > 0, "the Version row must read with fresh=1"); const top = await showRow(page, row); await button.click(); diff --git a/internal/gui/update.go b/internal/gui/update.go index 914330624..fcbe157fb 100644 --- a/internal/gui/update.go +++ b/internal/gui/update.go @@ -267,6 +267,36 @@ func (u *updater) freshen() { } } +// An answer of "latest" is about the moment the feed was asked, and magpie +// releases many times a day: Settings' Version row said 已是最新 on +// v0.1.1402 with v0.1.1517 out (RickyWei on Discord, Windows), the answer +// of a check made while 1402 was the newest and never made again (automatic +// checks off, or once a day). So it is shown only while younger than +// freshAfter. When the Version row is drawn (GET /api/update?fresh=1) an +// older one is asked again, with automatic updates on; with them off the +// row says so, and its Check asks. + +// latestStale is whether the state is a "latest" older than freshAfter. +// The caller holds u.mu. +func (u *updater) latestStale() bool { + return u.state == "latest" && time.Since(u.asked) >= freshAfter +} + +// refreshLatest asks the feed again when the Version row is drawn over a +// stale "latest", unless automatic updates are off. It doesn't wait for +// the answer: the row shows checking, and reads again. +func (u *updater) refreshLatest(s settings.Settings) { + if s.NoAutoUpdate { + return + } + u.mu.Lock() + stale := u.latestStale() + u.mu.Unlock() + if stale && u.begin() { + go u.run() + } +} + // begin marks a check as under way, unless one is. One with a version // already downloaded checks too: a newer one out since takes its place, // so the restart goes straight to the latest. @@ -463,6 +493,9 @@ func (u *updater) jsonIn(lang string) updateJSON { } } j := updateJSON{State: u.state, Current: Version, Error: u.err, Retry: u.retry} + if u.latestStale() { + j.State = "" // not asked lately: no claim to be up to date + } if u.state == "error" { j.Mirror = u.mirror } @@ -529,6 +562,9 @@ func (u *updater) relang(version, lang string) { func updateRoutes(mux *http.ServeMux, w Windows) { mux.HandleFunc("GET /api/update", func(rw http.ResponseWriter, r *http.Request) { + if r.URL.Query().Get("fresh") == "1" { // the Version row + updates.refreshLatest(settings.Load()) + } writeJSON(rw, updates.jsonIn(askedLang(r))) }) // notes: what changed in every release after this one up to the update, diff --git a/internal/gui/update_stale_test.go b/internal/gui/update_stale_test.go new file mode 100644 index 000000000..7ca9be471 --- /dev/null +++ b/internal/gui/update_stale_test.go @@ -0,0 +1,101 @@ +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") + } +}