diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index 67839a877..0b5ff6b12 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -4611,7 +4611,7 @@ async function whatsNewOnce() { // something else opened meanwhile: the notes wait for the next load if (!w?.show || !w.releases?.length || !$("#modal").hidden) { if (w?.show) whatsNewAsked = false; return; } api("whatsnew/seen", {}).catch(() => {}); - showWhatsNew(w.releases, true); + showWhatsNew(w.releases, true, { total: w.total }); } // openWhatsNew is Settings' way back to the notes: the current version's (or diff --git a/internal/gui/assets/i18n.js b/internal/gui/assets/i18n.js index 393da1d3c..2bd280ba3 100644 --- a/internal/gui/assets/i18n.js +++ b/internal/gui/assets/i18n.js @@ -203,6 +203,7 @@ const I18N = { "{n} earlier turns": "更早的 {n} 轮", "{n} more": "另外 {n} 项", "{n} releases · {from} – {to}": "{n} 个版本 · {from} – {to}", + "{n} releases · the newest {m} shown, {from} – {to}": "{n} 个版本 · 显示最新 {m} 个,{from} – {to}", "{n} files": "{n} 个文件", "Healthy": "健康", "Filling up": "渐满", @@ -4665,6 +4666,7 @@ const I18N = { "{n} earlier turns": "更早的 {n} 輪", "{n} more": "另外 {n} 項", "{n} releases · {from} – {to}": "{n} 個版本 · {from} – {to}", + "{n} releases · the newest {m} shown, {from} – {to}": "{n} 個版本 · 顯示最新 {m} 個,{from} – {to}", "{n} files": "{n} 個檔案", "Healthy": "健康", "Filling up": "漸滿", @@ -9074,6 +9076,7 @@ const I18N = { "{n} earlier turns": "それ以前の {n} ターン", "{n} more": "ほか {n} 件", "{n} releases · {from} – {to}": "{n} リリース · {from} – {to}", + "{n} releases · the newest {m} shown, {from} – {to}": "{n} リリース · 最新の {m} 件を表示、{from} – {to}", "{n} files": "{n} ファイル", "Healthy": "良好", "Filling up": "埋まりつつある", @@ -13504,6 +13507,7 @@ const I18N = { "{n} earlier turns": "{n} frühere Runden", "{n} more": "{n} weitere", "{n} releases · {from} – {to}": "{n} Versionen · {from} – {to}", + "{n} releases · the newest {m} shown, {from} – {to}": "{n} Versionen · die neuesten {m} gezeigt, {from} – {to}", "{n} files": "{n} Dateien", "Healthy": "Gesund", "Filling up": "Füllt sich", diff --git a/internal/gui/assets/whatsnew.js b/internal/gui/assets/whatsnew.js index daf88a646..ec914c5a9 100644 --- a/internal/gui/assets/whatsnew.js +++ b/internal/gui/assets/whatsnew.js @@ -36,7 +36,7 @@ function showWhatsNew(releases, auto, more = {}) { h.append(el("b", "", "v" + wait.at(-1).version + (wait.length > 1 ? " – v" + wait[0].version : "")), el("span", "badge", t("Not installed yet"))); sec.append(h); } - sec.append(mergedNotes(releases)); + sec.append(mergedNotes(releases, more.total)); ed.append(sec); } else for (const r of releases) { @@ -120,7 +120,10 @@ const NOTE_ORDER = ["Features", "Improvements", "Performance Improvements", "Bug // one heading per kind, New features first, each with the version it came // in, newest first, and a line that says the range. A line outside a list // is a bullet of its own; a release with no notes is left out. -function mergedNotes(releases) { +// total: how many releases there are in all when the server kept only the +// newest (update.MostNotes), so 156 behind doesn't read "40 releases" +// (sujingshen on X). +function mergedNotes(releases, total = 0) { const secs = new Map(); for (const r of releases) { const of = (k) => secs.get(k) || secs.set(k, []).get(k); @@ -145,7 +148,10 @@ function mergedNotes(releases) { const rank = (k) => { const i = NOTE_ORDER.findIndex((o) => t(o) === k); return i < 0 ? NOTE_ORDER.length : i; }; const box = el("div", "wn-notes wn-merged"); const vs = releases.map((r) => r.version); - box.append(el("div", "wn-span", t("{n} releases · {from} – {to}", { n: vs.length, from: "v" + vs.at(-1), to: "v" + vs[0] }))); + const span = total > vs.length + ? t("{n} releases · the newest {m} shown, {from} – {to}", { n: total, m: vs.length, from: "v" + vs.at(-1), to: "v" + vs[0] }) + : t("{n} releases · {from} – {to}", { n: vs.length, from: "v" + vs.at(-1), to: "v" + vs[0] }); + box.append(el("div", "wn-span", span)); for (const [k, items] of [...secs].filter(([, x]) => x.length).sort((a, b) => rank(a[0]) - rank(b[0]))) { const h = el("div", "wn-h wn-h2"); h.append(k, el("span", "wn-n", String(items.length))); @@ -201,11 +207,11 @@ async function confirmUpdate(u, go) { head.append(title); if (u.current) head.append(el("span", "wn-since", t("What changed since {v}", { v: "v" + u.current }))); const box = el("div", "wn-list"); - const draw = (releases) => { + const draw = (releases, total) => { box.replaceChildren(); if (releases.length > 1) { const sec = el("section", "wn-rel"); - sec.append(mergedNotes(releases)); + sec.append(mergedNotes(releases, total)); box.append(sec); } else for (const r of releases) { @@ -239,7 +245,7 @@ async function confirmUpdate(u, go) { const w = await api(updatePath("update/notes")).catch(() => null); if (confirmAsk !== ed) return; if (w?.latest && w.latest !== u.latest) title.textContent = t("Update to {v}", { v: "v" + w.latest }); - if (w?.releases?.length) draw(w.releases); + if (w?.releases?.length) draw(w.releases, w.total); } window.wnShow = showWhatsNew; diff --git a/internal/gui/tests/update-notes-ask.test.cjs b/internal/gui/tests/update-notes-ask.test.cjs index 9af826fd6..aca267587 100644 --- a/internal/gui/tests/update-notes-ask.test.cjs +++ b/internal/gui/tests/update-notes-ask.test.cjs @@ -15,8 +15,8 @@ const { chromium, webkit } = require("playwright"); const assets = path.resolve(__dirname, "../assets"); const words = { - en: { pill: "Update", restart: "Restart to update", later: "Later", head: "Update to v0.1.402", newer: "Update to v0.1.403", since: "What changed since v0.1.400", span: "2 releases · v0.1.401 – v0.1.402", span3: "3 releases · v0.1.401 – v0.1.403", version: "Version" }, - zh: { pill: "更新", restart: "重启以更新", later: "稍后", head: "更新到 v0.1.402", newer: "更新到 v0.1.403", since: "自 v0.1.400 以来的更新内容", span: "2 个版本 · v0.1.401 – v0.1.402", span3: "3 个版本 · v0.1.401 – v0.1.403", version: "版本" }, + en: { pill: "Update", restart: "Restart to update", later: "Later", head: "Update to v0.1.402", newer: "Update to v0.1.403", since: "What changed since v0.1.400", span: "2 releases · v0.1.401 – v0.1.402", span3: "3 releases · v0.1.401 – v0.1.403", spanMore: "156 releases · the newest 3 shown, v0.1.401 – v0.1.403", version: "Version" }, + zh: { pill: "更新", restart: "重启以更新", later: "稍后", head: "更新到 v0.1.402", newer: "更新到 v0.1.403", since: "自 v0.1.400 以来的更新内容", span: "2 个版本 · v0.1.401 – v0.1.402", span3: "3 个版本 · v0.1.401 – v0.1.403", spanMore: "156 个版本 · 显示最新 3 个,v0.1.401 – v0.1.403", version: "版本" }, }; function server(lang, ctl) { @@ -34,7 +34,7 @@ function server(lang, ctl) { await new Promise((r) => setTimeout(r, 200)); // #910: the feed asked again names a release out since the download // a line two releases both carry is said once, at the newest - if (ctl.newer) return json({ latest: "0.1.403", releases: [{ version: "0.1.403", notes: "- Three: out since\n- One: a fix" }, { version: "0.1.402", notes: "- Two: the pill asks first" }, { version: "0.1.401", notes: "- One: a fix" }] }); + if (ctl.newer) return json({ latest: "0.1.403", releases: [{ version: "0.1.403", notes: "- Three: out since\n- One: a fix" }, { version: "0.1.402", notes: "- Two: the pill asks first" }, { version: "0.1.401", notes: "- One: a fix" }], ...(ctl.total ? { total: ctl.total } : {}) }); if (ctl.one) return json({ latest: "0.1.402", releases: [{ version: "0.1.402", notes: "- Two: the pill asks first" }] }); return json({ latest: "0.1.402", releases: [{ version: "0.1.402", notes: "- Two: the pill asks first" }, { version: "0.1.401", notes: "- One: a fix" }] }); } @@ -170,5 +170,27 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", assert.equal(await ask.locator(".wn-span").textContent(), w.span3); assert.deepEqual(errors, []); }); + + // sujingshen on X: v0.1.2606 to v0.1.2762 read "40 releases", the + // newest 40 the app keeps (MostNotes); the line says how many in all + test(`${engine} ${lang}: a list cut short says how many releases there are`, async (t) => { + const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" })); + t.after(() => browser.close()); + const w = words[lang], ctl = { installs: [], asked: [], newer: true, total: 156 }, errors = []; + const page = await (await browser.newContext({ viewport: { width: 1000, height: 640 }, reducedMotion: "reduce" })).newPage(); + page.setDefaultTimeout(5000); + page.setDefaultNavigationTimeout(60000); + page.on("pageerror", (e) => { if (!/^ResizeObserver loop completed with undelivered notifications/.test(e.message)) errors.push(e.message); }); + await page.route("**/*", server(lang, ctl)); + await page.goto("http://magpie.test/?view=agents"); + const pill = page.locator("#update"); + await pill.waitFor({ state: "visible" }); + await pill.click(); + const ask = page.locator("#modal .update-ask"); + await ask.waitFor(); + await page.waitForFunction(lines, 3); + assert.equal(await ask.locator(".wn-span").textContent(), w.spanMore); + assert.deepEqual(errors, []); + }); } } diff --git a/internal/gui/tests/whats-new.test.cjs b/internal/gui/tests/whats-new.test.cjs index e48d68e0d..980859f2e 100644 --- a/internal/gui/tests/whats-new.test.cjs +++ b/internal/gui/tests/whats-new.test.cjs @@ -77,7 +77,7 @@ function serve(lang, ctl) { const releases = show || (all && ctl.upgraded) ? RELEASES : all ? [RELEASES[0]] : []; // Settings' ask: the app puts every release up to the waiting update // first, marked pending (小八: 0.1.1399 to 0.1.1408 were missing) - return json({ show, current: "0.1.604", releases: all && ctl.ahead ? [...ctl.ahead, ...releases] : releases }); + return json({ show, current: "0.1.604", releases: all && ctl.ahead ? [...ctl.ahead, ...releases] : releases, ...(show && ctl.total ? { total: ctl.total } : {}) }); } if (url.pathname === "/api/whatsnew/seen") { ctl.seen++; return route.fulfill({ status: 204 }); } if (url.pathname === "/api/whatsnew/today") { (ctl.today ||= []).push(req.postDataJSON().on); return route.fulfill({ status: 204 }); } @@ -93,8 +93,8 @@ function serve(lang, ctl) { const heads = (box) => box.locator(".wn-h").evaluateAll((hs) => hs.map((h) => [h.firstChild.textContent, h.querySelector(".wn-n")?.textContent])); const words = { - en: { span: "2 releases · v0.1.603 – v0.1.604", heads: [["Improvements", "1"], ["Bug Fixes", "4"], ["Thanks", "1"]], span5: "5 releases · v0.1.603 – v0.1.607", headsPending: [["Features", "1"], ["Improvements", "1"], ["Bug Fixes", "4"], ["Changes", "2"], ["Thanks", "1"]], quiet: "Don't show again today", title: "What's new in v0.1.604", pendingTitle: "What's new in v0.1.607", close: "Close", again: "What's new", open: "Open", pending: "Not installed yet" }, - zh: { span: "2 个版本 · v0.1.603 – v0.1.604", heads: [["改进", "1"], ["错误修复", "4"], ["Thanks", "1"]], span5: "5 个版本 · v0.1.603 – v0.1.607", headsPending: [["新功能", "1"], ["改进", "1"], ["错误修复", "4"], ["更新", "2"], ["Thanks", "1"]], quiet: "今天不再弹出", title: "v0.1.604 更新内容", pendingTitle: "v0.1.607 更新内容", close: "关闭", again: "更新内容", open: "打开", pending: "尚未安装" }, + en: { spanMore: "156 releases · the newest 2 shown, v0.1.603 – v0.1.604", span: "2 releases · v0.1.603 – v0.1.604", heads: [["Improvements", "1"], ["Bug Fixes", "4"], ["Thanks", "1"]], span5: "5 releases · v0.1.603 – v0.1.607", headsPending: [["Features", "1"], ["Improvements", "1"], ["Bug Fixes", "4"], ["Changes", "2"], ["Thanks", "1"]], quiet: "Don't show again today", title: "What's new in v0.1.604", pendingTitle: "What's new in v0.1.607", close: "Close", again: "What's new", open: "Open", pending: "Not installed yet" }, + zh: { spanMore: "156 个版本 · 显示最新 2 个,v0.1.603 – v0.1.604", span: "2 个版本 · v0.1.603 – v0.1.604", heads: [["改进", "1"], ["错误修复", "4"], ["Thanks", "1"]], span5: "5 个版本 · v0.1.603 – v0.1.607", headsPending: [["新功能", "1"], ["改进", "1"], ["错误修复", "4"], ["更新", "2"], ["Thanks", "1"]], quiet: "今天不再弹出", title: "v0.1.604 更新内容", pendingTitle: "v0.1.607 更新内容", close: "关闭", again: "更新内容", open: "打开", pending: "尚未安装" }, }; for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { @@ -159,6 +159,17 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", await page.close(); } + // sujingshen on X: 156 releases behind, the app keeps the newest + // (MostNotes) and the line says how many there are in all + { + const ctl = fresh({ upgraded: true, total: 156 }); + const { page, errors } = await open(ctl); + await dialog(page).waitFor(); + assert.equal(await dialog(page).locator(".wn-span").textContent(), w.spanMore); + assert.deepEqual(errors, []); + await page.close(); + } + // upgraded: the notes since, newest first, once const ctl = fresh({ upgraded: true }); const { page, errors } = await open(ctl); diff --git a/internal/gui/update.go b/internal/gui/update.go index 61145544d..3004e8576 100644 --- a/internal/gui/update.go +++ b/internal/gui/update.go @@ -682,13 +682,19 @@ func updateRoutes(mux *http.ServeMux, w Windows) { out := struct { Latest string `json:"latest,omitempty"` Releases []update.Note `json:"releases"` - Error string `json:"error,omitempty"` + // Total: how many releases there are since this one when + // more than Releases holds (MostNotes keeps the newest) + Total int `json:"total,omitempty"` + Error string `json:"error,omitempty"` }{Latest: j.Latest, Releases: []update.Note{}} if j.Latest != "" { - notes, err := fetchNotes(r.Context(), Version, j.Latest, pageLang(r)) + notes, total, err := fetchNotes(r.Context(), Version, j.Latest, pageLang(r)) if len(notes) == 0 && strings.TrimSpace(j.Notes) != "" { notes = []update.Note{{Version: j.Latest, Notes: j.Notes, URL: j.URL}} } + if total > len(notes) { + out.Total = total + } if len(notes) > 0 { out.Releases = notes } else if err != nil { diff --git a/internal/gui/whatsnew.go b/internal/gui/whatsnew.go index e564689f9..1615c903e 100644 --- a/internal/gui/whatsnew.go +++ b/internal/gui/whatsnew.go @@ -41,6 +41,7 @@ type whatsNew struct { quiet bool // the upgrade was the user's own restart to update: not shown by itself shown bool // a page has shown them notes map[string][]update.Note // by language and channel (notesKey) + totals map[string]int // how many releases each of notes covers, past MostNotes too // again: when notes kept by that key, some in English though asked in // Chinese, are asked for again (see untranslated) again map[string]time.Time @@ -190,6 +191,9 @@ type whatsNewJSON struct { Show bool `json:"show"` Current string `json:"current"` Releases []update.Note `json:"releases"` + // Total: how many releases the notes since the last update cover when + // more than Releases holds (MostNotes keeps the newest) + Total int `json:"total,omitempty"` // Error: the notes couldn't be had (the site down, or GitHub failing // it), as opposed to releases that have none (#661); URL is the // current version's release page, where they can be read instead @@ -219,7 +223,7 @@ func (n *whatsNew) get(ctx context.Context, all bool, lang string) whatsNewJSON } j.URL = update.ReleasePage(Version) if notes == nil || again { - got, err := fetchNotes(ctx, after, Version, lang) + got, total, err := fetchNotes(ctx, after, Version, lang) if err != nil && !again { j.Error = err.Error() } @@ -232,6 +236,10 @@ func (n *whatsNew) get(ctx context.Context, all bool, lang string) whatsNewJSON n.notes = map[string][]update.Note{} } n.notes[key] = got + if n.totals == nil { + n.totals = map[string]int{} + } + n.totals[key] = total } if len(got) > 0 || again { if n.again == nil { @@ -245,6 +253,11 @@ func (n *whatsNew) get(ctx context.Context, all bool, lang string) whatsNewJSON } if len(notes) > 0 { j.Releases = notes + n.mu.Lock() + if t := n.totals[key]; t > len(notes) { + j.Total = t + } + n.mu.Unlock() } j.Show = popup && len(j.Releases) > 0 return j @@ -337,7 +350,7 @@ func withWaiting(ctx context.Context, list []update.Note, u updateJSON, lang str if !update.Released(Version) || u.Latest == "" || !update.Newer(u.Latest, Version) { return list } - ahead, _ := fetchNotes(ctx, Version, u.Latest, lang) + ahead, _, _ := fetchNotes(ctx, Version, u.Latest, lang) if len(ahead) == 0 && strings.TrimSpace(u.Notes) != "" { ahead = []update.Note{{Version: u.Latest, Notes: u.Notes, URL: u.URL}} } @@ -366,25 +379,28 @@ func (n *whatsNew) seen() { // newest release has its notes, for when that is the current one. The // error says the notes couldn't be had, which the page tells apart from // releases without notes (#661): the list failed and the feed had none. -func fetchNotes(ctx context.Context, after, upto, lang string) ([]update.Note, error) { +// +// total is how many releases have notes in that range, the ones past +// MostNotes too, so a list cut short says how many it left out. +func fetchNotes(ctx context.Context, after, upto, lang string) (notes []update.Note, total int, _ error) { ctx, cancel := context.WithTimeout(ctx, 15*time.Second) defer cancel() - notes, failed := update.NotesBetween(ctx, after, upto, lang) + notes, total, failed := update.NotesBetweenTotal(ctx, after, upto, lang) if failed == nil && len(notes) > 0 { - return notes, nil + return notes, total, nil } if failed != nil { log.Println("release notes:", failed) } rel, err := update.LatestIn(ctx, lang) if err != nil { - return nil, failed + return nil, 0, failed } notes = update.Between([]update.Note{{Version: rel.Version, Notes: rel.Notes, URL: rel.URL}}, after, upto) if len(notes) == 0 { - return nil, failed + return nil, 0, failed } - return notes, nil + return notes, len(notes), nil } // pageLang is the language a page asks in, its own (?lang=, zh or en), or @@ -418,7 +434,11 @@ func whatsNewRoutes(mux *http.ServeMux) { } j := news.get(r.Context(), all, lang) if all { + had := len(j.Releases) j.Releases = withWaiting(r.Context(), j.Releases, updates.jsonIn(askedLang(r)), lang) + if len(j.Releases) != had || (had > 0 && j.Releases[0].Pending) { + j.Total = 0 // counted the installed releases alone + } } writeJSON(rw, j) }) diff --git a/internal/update/notes.go b/internal/update/notes.go index d2bcf9721..fbb89a1e8 100644 --- a/internal/update/notes.go +++ b/internal/update/notes.go @@ -44,15 +44,24 @@ func ReleasePage(version string) string { // (none when "") up to and including upto, newest first, in lang (see // InLang). func NotesBetween(ctx context.Context, after, upto, lang string) ([]Note, error) { + notes, _, err := NotesBetweenTotal(ctx, after, upto, lang) + return notes, err +} + +// NotesBetweenTotal is NotesBetween with how many releases there are in +// all before MostNotes keeps the newest, so a list cut short can say so +// (sujingshen on X: 156 releases behind read "40 releases"). +func NotesBetweenTotal(ctx context.Context, after, upto, lang string) ([]Note, int, error) { q := url.Values{"upto": {upto}} if after != "" { q.Set("after", after) } notes, err := askNotes(ctx, q, lang) if err != nil { - return nil, err + return nil, 0, err } - return Between(notes, after, upto), nil + all := between(notes, after, upto) + return capNotes(all), len(all), nil } // BackCounts are the ranges Settings' What's new offers besides the notes @@ -182,6 +191,19 @@ func withChannel(feed string) string { // upto, newest first, at most MostNotes; with after "" only upto's. Each // one's Install section is taken out, and one left empty is dropped. func Between(notes []Note, after, upto string) []Note { + return capNotes(between(notes, after, upto)) +} + +// capNotes keeps the newest MostNotes of notes, newest first. +func capNotes(notes []Note) []Note { + if most := MostNotes(); len(notes) > most { + return notes[:most] + } + return notes +} + +// between is Between with every release kept. +func between(notes []Note, after, upto string) []Note { var out []Note seen := map[string]bool{} for _, n := range notes { @@ -204,9 +226,6 @@ func Between(notes []Note, after, upto string) []Note { out = append(out, n) } sort.SliceStable(out, func(i, j int) bool { return Newer(out[i].Version, out[j].Version) }) - if most := MostNotes(); len(out) > most { - out = out[:most] - } return out } diff --git a/internal/update/notes_test.go b/internal/update/notes_test.go index 7ada0eb7f..883d2adba 100644 --- a/internal/update/notes_test.go +++ b/internal/update/notes_test.go @@ -120,6 +120,33 @@ func TestNotesBetween(t *testing.T) { } } +// sujingshen on X: v0.1.2606 to v0.1.2762 showed the newest MaxNotes and +// read "40 releases". The total counts every release in the range, so the +// list cut short can say how many there are. +func TestNotesBetweenTotalPastTheCap(t *testing.T) { + var rel []Note + for i := 1; i <= MaxNotes+16; i++ { + rel = append(rel, Note{Version: fmt.Sprintf("0.1.%d", 600+i), Notes: "- x"}) + } + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + json.NewEncoder(w).Encode(map[string]any{"releases": rel}) + })) + defer srv.Close() + t.Setenv("MAGPIE_NOTES_FEED", srv.URL) + upto := fmt.Sprintf("0.1.%d", 600+MaxNotes+16) + got, total, err := NotesBetweenTotal(context.Background(), "0.1.600", upto, "") + if err != nil { + t.Fatal(err) + } + if len(got) != MaxNotes || got[0].Version != upto || total != MaxNotes+16 { + t.Fatalf("kept %d of %d, newest %s", len(got), total, got[0].Version) + } + got, total, err = NotesBetweenTotal(context.Background(), "0.1.650", upto, "") + if err != nil || len(got) != 6 || total != 6 { + t.Fatalf("under the cap: kept %d of %d (%v)", len(got), total, err) + } +} + // Settings' What's new can show this version and the 10, 20, 50 or 100 // before it (Linka on Discord): the site is asked for that many alone, and // a site that sends more (from before it took a limit) is cut to them.