Files
magpie/internal/gui/tests/update-check.test.cjs
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

265 lines
14 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// Settings → Version. A check leaves its button in place, dimmed, and a
// second click does nothing. The answer puts the button back. A read still
// out cannot draw "checking" over that answer, and a row drawn again while
// the check runs keeps the button and catches up. English and Chinese; no
// backend, the API is faked here.
const assert = require("node:assert/strict");
const fs = require("node:fs/promises");
const path = require("node:path");
const { test } = require("node:test");
const { chromium, webkit } = require("playwright");
const { watchSettingsReads, settingsDrawn } = require("./reader.cjs");
const assets = path.resolve(__dirname, "../assets");
const words = {
en: { version: "Version", check: "Check", checking: "Checking for updates…", latest: "Up to date", failed: "Couldn't check for updates · timed out" },
zh: { version: "版本", check: "检查", checking: "正在检查更新…", latest: "已是最新", failed: "检查更新失败 · timed out" },
};
function settingsPayload(lang) {
return {
theme: "light", lang, tray: "panel", quotaLeft: false, currency: "usd",
dock: false, dockWindow: false, proxy: "", redact: false, redactPersonal: false, redactWords: [],
codexWarmup: "", claudeWarmup: "", codexWarmAt: "", claudeWarmAt: "", workbuddyCheckin: false, noStats: false,
trayUsage: "", trayUsageEvery: 3, vision: "", imageGen: "",
version: "0.1.400", dir: "~/.config/magpie", gateway: "http://127.0.0.1:3425",
proxyNow: "none", proxySource: "none", login: false,
visionModels: [], imageGenModels: [], workbuddyCheckins: [], lanURLs: [],
fx: { rate: 7.2, at: new Date().toISOString(), stale: false },
};
}
// ctl.update is what a finished check reports. While a check is held, a read
// still says checking, which is what a second click used to draw.
function server(lang, ctl) {
const postWait = [], getWait = [];
ctl.releasePosts = () => { for (const go of postWait.splice(0)) go(); };
ctl.releaseGets = () => { for (const go of getWait.splice(0)) go(); };
const cur = settingsPayload(lang);
return async (route) => {
const req = route.request(), url = new URL(req.url());
const json = (data) => route.fulfill({ json: data });
if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:false};` });
if (url.pathname === "/wails/runtime.js") return route.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
if (url.pathname === "/api/state") return json({ agents: [], profiles: [], settings: { lang, theme: "light" }, fx: cur.fx });
if (url.pathname === "/api/settings") return json(cur);
if (url.pathname === "/api/update/check") {
ctl.posts++;
ctl.inflight++;
if (ctl.holdPost) await new Promise((go) => postWait.push(go));
ctl.inflight--;
return json({ ...ctl.update });
}
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);
}
if (url.pathname === "/api/usage/quotas") return json([]);
if (url.pathname === "/api/groups") return json({ groups: [], models: [] });
if (url.pathname.startsWith("/api/")) return json({});
const file = path.join(assets, url.pathname === "/" ? "index.html" : url.pathname);
const contentType = { ".html": "text/html", ".js": "text/javascript", ".css": "text/css", ".svg": "image/svg+xml", ".png": "image/png" }[path.extname(file)];
await route.fulfill({ body: await fs.readFile(file), contentType });
};
}
function fresh(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);
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
test(engine + ": a version check keeps its button", async (t) => {
assert(["chromium", "webkit"].includes(engine), "BROWSER must be chromium or webkit");
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
const pages = [];
t.after(async () => {
if (process.env.ARTIFACT_DIR) {
await fs.mkdir(process.env.ARTIFACT_DIR, { recursive: true });
for (const [i, p] of pages.entries()) await p.screenshot({ path: path.join(process.env.ARTIFACT_DIR, `${engine}-update-check-${i}.png`) });
}
await browser.close();
});
const open = async (lang, ctl) => {
const errors = [];
const page = await (await browser.newContext({ viewport: { width: 900, height: 300 }, reducedMotion: "reduce" })).newPage();
pages.push(page);
page.setDefaultTimeout(5000);
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", server(lang, ctl));
await watchSettingsReads(page);
await page.goto("http://magpie.test/?view=settings&tab=about");
await settingsDrawn(page); // rows found are the ones that stay (#1659)
return { page, errors };
};
const rowOf = (page, name) => page.locator("#about .row.pref", { has: page.locator(".name", { hasText: name }) }).first();
// wheel the version row into sight; a later click must not move the page
const showRow = async (page, row) => {
await page.locator("#view-settings").hover();
for (let i = 0; i < 40; i++) {
const box = await row.boundingBox();
const view = await page.locator("#view-settings").boundingBox();
if (box && view && box.y >= view.y + 4 && box.y + box.height <= view.y + view.height - 4 && (await viewTop(page)) > 0) break;
await page.mouse.wheel(0, 8);
await page.waitForTimeout(16);
}
await page.waitForTimeout(250);
const top = await viewTop(page);
assert(top > 0, "the settings page must be long enough to scroll");
return top;
};
for (const lang of ["en", "zh"]) {
const w = words[lang];
await t.test(lang + ": two clicks, one check, the button stays", async () => {
const ctl = fresh({ holdPost: true });
const { page, errors } = await open(lang, ctl);
const row = rowOf(page, w.version);
const button = row.locator("button", { hasText: w.check });
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();
assert.equal(await viewTop(page), top, "the click must not scroll the page");
assert.equal(await button.isDisabled(), true, "the button stays, dimmed");
assert.equal((await row.locator(".sub").textContent()).trim(), w.checking);
assert.equal(ctl.posts, 1, "one check was asked for");
// the handler itself, not the disabled button: a second click is ignored
await button.evaluate((b) => { const fn = b.onclick; fn(); fn(); });
await page.waitForTimeout(50);
assert.equal(ctl.posts, 1, "a second click must not ask again");
assert.equal(await row.locator("button").count(), 1);
// a read that left while the check ran, answered "checking" only after
// the check itself has answered — it must not be drawn over that
ctl.holdGets = true;
const gets = ctl.gets;
await page.evaluate(() => { void renderUpdate(document.querySelector("#about .row.pref")); });
for (let i = 0; i < 50 && ctl.gets === gets; i++) await page.waitForTimeout(20);
assert(ctl.gets > gets, "the late read left before the answer");
ctl.update = { state: "latest", current: "0.1.400" };
ctl.releasePosts();
await button.waitFor({ state: "attached" });
await page.waitForFunction((latest) => {
const sub = document.querySelector("#about .row.pref .sub");
const b = document.querySelector("#about .row.pref button");
return sub && sub.textContent.trim() === latest && b && !b.disabled;
}, w.latest);
assert.equal(await viewTop(page), top, "the answer must not scroll the page");
ctl.releaseGets();
await page.waitForTimeout(1200);
assert.equal((await row.locator(".sub").textContent()).trim(), w.latest, "a late checking read must not replace the answer");
assert.equal(await row.locator("button").count(), 1);
assert.equal(await button.isEnabled(), true);
assert.equal(ctl.posts, 1);
assert.deepEqual(errors, []);
});
await t.test(lang + ": drawing the row again while a check runs", async () => {
const ctl = fresh({ holdPost: true });
const { page, errors } = await open(lang, ctl);
const row = rowOf(page, w.version);
const button = row.locator("button", { hasText: w.check });
await button.waitFor();
await showRow(page, row);
await button.click();
assert.equal(ctl.posts, 1);
await page.evaluate(() => renderSettings());
const again = rowOf(page, w.version);
await again.locator("button", { hasText: w.check }).waitFor();
assert.equal(await again.locator("button").isDisabled(), true, "the new row keeps the button while the check runs");
assert.equal((await again.locator(".sub").textContent()).trim(), w.checking);
assert.equal(ctl.posts, 1);
ctl.update = { state: "latest", current: "0.1.400" };
ctl.releasePosts();
await page.waitForFunction((latest) => {
const sub = document.querySelector("#about .row.pref .sub");
const b = document.querySelector("#about .row.pref button");
return sub && sub.textContent.trim() === latest && b && !b.disabled;
}, w.latest);
assert.equal(ctl.posts, 1);
assert.deepEqual(errors, []);
});
await t.test(lang + ": a failed check gives the button back", async () => {
const ctl = fresh();
const { page, errors } = await open(lang, ctl);
const row = rowOf(page, w.version);
const button = row.locator("button", { hasText: w.check });
await button.waitFor();
assert.equal((await row.locator(".sub").textContent()).trim(), w.latest);
await showRow(page, row);
const top = await viewTop(page);
ctl.update = { state: "error", current: "0.1.400", error: "timed out" };
await button.click();
await page.waitForFunction((failed) => document.querySelector("#about .row.pref .sub")?.textContent.trim() === failed, w.failed);
assert.equal(await row.locator("button", { hasText: w.check }).isEnabled(), true);
assert.equal(ctl.posts, 1);
assert.equal(await viewTop(page), top);
assert.deepEqual(errors, []);
});
await t.test(lang + ": a check already under way still shows the button", async () => {
const ctl = fresh({ update: { state: "checking", current: "0.1.400" } });
const { page, errors } = await open(lang, ctl);
const row = rowOf(page, w.version);
const button = row.locator("button", { hasText: w.check });
await button.waitFor();
assert.equal(await button.isDisabled(), true);
assert.equal((await row.locator(".sub").textContent()).trim(), w.checking);
ctl.update = { state: "latest", current: "0.1.400" };
await page.waitForFunction((latest) => {
const sub = document.querySelector("#about .row.pref .sub");
const b = document.querySelector("#about .row.pref button");
return sub && sub.textContent.trim() === latest && b && !b.disabled;
}, w.latest);
assert.equal(ctl.posts, 0, "watching a check already under way does not start another");
assert.deepEqual(errors, []);
});
// freecss on Discord: "0.1.628 已下载 · couldn't move
// magpie-windows-amd64.exe aside to put the new version in: rename C:\Us…"
// — the reason was cut off. It is read in full, and the release page is
// a click away.
await t.test(lang + ": an install that failed says why in full", async () => {
const error = "couldn't move magpie-windows-amd64.exe aside to put the new version in: The process cannot access the file because it is being used by another process; another program has it open (often an antivirus or OneDrive): let magpie through it and restart to update again, or download the new version and put it in place of this one";
const url = "https://github.com/yetone/magpie-releases/releases/tag/v0.1.628";
const ctl = fresh({ update: { state: "ready", current: "0.1.627", latest: "0.1.628", error, url } });
const { page, errors } = await open(lang, ctl);
const opened = [];
await page.route("**/api/open", async (route) => { opened.push(route.request().postDataJSON()); await route.fulfill({ json: {} }); });
const row = rowOf(page, w.version);
const sub = row.locator(".sub");
await page.waitForFunction((e) => document.querySelector("#about .row.pref .sub")?.textContent.includes(e), error);
assert.equal(await sub.getAttribute("title"), error);
const cut = await sub.evaluate((s) => s.scrollWidth > s.clientWidth + 1);
assert.equal(cut, false, "the reason must not be cut off");
const download = row.locator("button", { hasText: lang === "zh" ? "下载" : "Download" });
await download.click();
assert.deepEqual(opened, [{ url }]);
// a later state is one line again
ctl.update = { state: "latest", current: "0.1.628" };
await page.evaluate(() => { void renderUpdate(document.querySelector("#about .row.pref")); });
await page.waitForFunction((latest) => document.querySelector("#about .row.pref .sub")?.textContent.trim() === latest, w.latest);
assert.equal(await sub.evaluate((s) => s.classList.contains("wraps")), false);
assert.deepEqual(errors, []);
});
}
});
}