Files
magpie/internal/gui/tests/update-download-percent.test.cjs
T
yetone ce8b67de37 gui tests: a page's first load gets a minute, not a click's 5s (#4470)
claude-switch-only set page.setDefaultTimeout(5000) and then called
page.goto with no timeout of its own, so Playwright held the first load
of app.js, i18n.js and the plugins to the 5s bar meant for clicks. On a
busy box (a whole-suite run at load 130-160) that load took longer and
goto timed out. The same pattern is in 988 GUI test files (1174 pages):
every one of them now also sets setDefaultNavigationTimeout(60000) on
the same page, right after its default, so goto, reload and waitForURL
wait on the load itself with room, while clicks and waits keep their
short bar. group-from-model-add's own 20s navigation bar is raised to the
same minute. claude-switch-only also gives its first provider row (drawn
from the page's first reads) a minute before clicking it.

nav-timeout.test.cjs (no browser) reads every GUI test file and names
each page given a default under a minute without a navigation timeout of
at least a minute, so a new file can't bring the pattern back.

What was run (WSL magpie-desktop, Playwright 1.62.0, load 70-260 from
other sessions):
- node --check on every file in internal/gui/tests: all parse.
- nav-timeout.test.cjs against origin/main's test files: red (names the
  1174 pages); against this tree: its only reds were my own probe copies
  of the old files, since removed.
- By hand: claude-switch-only with app.js served 7s late (a busy box's
  first load), --test-name-pattern "1100 en": origin/main's file 4 of 4
  red with "page.goto: Timeout 5000ms exceeded"; this file 4 of 4 green.
- claude-switch-only.test.cjs, both engines, en/zh/ja/de, 1100 and 420:
  32 of 32. group-from-model-add, usage-access-key-card and i18n-keys:
  green. agent-plugin-agents: Chromium green; WebKit's de 420 subtest red
  on a 5s waitForFunction at line 115 (a wait after load, not the
  navigation), at load ~230.

Not tried: the whole GUI suite (about 1000 files; the change is one
mechanical line per page), Windows or mote (mote's browsers, #4125).
2026-10-31 02:09:18 +08:00

109 lines
6.3 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// The header's Update pill while an update downloads again (a click after a
// failed one): the backend's answer leaves done out while it is 0, and
// total while the size isn't known, so the first reads have no percent. The
// pill says 0% then, "Downloading…" with no size, never "NaN%" (inaction
// on Discord), and the percent once there is one. The click doesn't move the page, nothing
// has a left border. English and Chinese; no backend, the API is faked.
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 assets = path.resolve(__dirname, "../assets");
const words = {
en: { update: "Update", downloading: "Downloading…", zero: "Downloading… 0%", half: "Downloading… 50%" },
zh: { update: "更新", downloading: "正在下载…", zero: "正在下载… 0%", half: "正在下载… 50%" },
};
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 /api/update answers; ctl.installs counts the clicks' asks
function server(lang, ctl) {
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: settingsPayload(lang), fx: settingsPayload(lang).fx });
if (url.pathname === "/api/settings") return json(settingsPayload(lang));
if (url.pathname === "/api/update/install") {
ctl.installs++;
return json({ ...ctl.update });
}
if (url.pathname === "/api/update") return json({ ...ctl.update });
if (url.pathname === "/api/plugins") return json({ plugins: [] });
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 });
};
}
const scrolls = (page) => page.evaluate(() => [window.scrollY, document.scrollingElement.scrollTop, ...[...document.querySelectorAll(".view")].map((v) => v.scrollTop)].join(","));
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
test(engine + ": a downloading update never says NaN", async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
for (const lang of ["en", "zh"]) {
const w = words[lang];
await t.test(lang + ": no percent until there is one", async () => {
const ctl = { installs: 0, update: { state: "error", retry: true, current: "0.1.400", latest: "0.1.401", error: "timed out" } };
const errors = [];
const page = await (await browser.newContext({ viewport: { width: 900, height: 560 }, reducedMotion: "reduce" })).newPage();
page.setDefaultTimeout(5000);
page.setDefaultNavigationTimeout(60000);
// WebKit's notice when a layout outruns its observers, not a fault
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/");
const pill = page.locator("#update"), label = pill.locator("span").first();
await pill.waitFor({ state: "visible" });
assert.equal((await label.innerText()).trim(), w.update);
const seen = [];
await page.exposeFunction("__seen", (s) => seen.push(s));
await label.evaluate((l) => new MutationObserver(() => window.__seen(l.textContent)).observe(l, { childList: true, characterData: true, subtree: true }));
// the download starts: the size known, nothing in yet (done left out)
ctl.update = { state: "downloading", current: "0.1.400", latest: "0.1.401", total: 1000 };
const before = await scrolls(page);
await pill.click();
assert.equal(await scrolls(page), before, "the click must not scroll the page");
assert.equal(ctl.installs, 1);
await page.waitForTimeout(300);
assert.equal((await label.innerText()).trim(), w.zero);
// nor the size yet
ctl.update = { state: "downloading", current: "0.1.400", latest: "0.1.401" };
await page.waitForTimeout(900);
assert.equal((await label.innerText()).trim(), w.downloading);
// halfway
ctl.update = { state: "downloading", current: "0.1.400", latest: "0.1.401", done: 500, total: 1000 };
await page.waitForFunction((s) => document.querySelector("#update span").textContent.trim() === s, w.half);
assert.deepEqual(seen.filter((s) => /NaN|undefined|Infinity/.test(s)), [], "the pill said " + seen.join(" | "));
assert.equal(await pill.evaluate((e) => getComputedStyle(e).borderLeftWidth === "0px" || getComputedStyle(e).borderLeftColor === getComputedStyle(e).borderTopColor), true);
assert.deepEqual(errors, []);
await page.context().close();
});
}
});
}