Files
magpie/internal/gui/tests/version-restart.test.cjs
T
yetone 74bba1fa67 gui: Settings › Version and the tray restart magpie with no update (yansheng1003 on X)
yansheng1003 asked for a Restart beside Check: "每次改了需要重新magpie,
都要自己退出再启动麻烦". The Version row now has Restart after Check
while no update is on offer (found, downloading, downloaded, blocked, a
newer version named or a failed download each have their own buttons,
which restart through the update and fill a 420px row), and the tray's
Restart magpie is always shown:
with an update ready it installs it as before, with none it starts this
magpie again as it is.

Both go through restartSelfNow (restart.go), the updater's own way of
starting again: on macOS and Linux the gateway's socket is handed over
(beginHandover), so requests in flight carry on; otherwise the bundle or
binary is started again (update.Relaunch, relaunchBinary), and on
Windows requests in flight are cut. magpie web runs itself again in
place. POST /api/restart answers 204 and then quits, or 400 "magpie
couldn't start again: quit it and open it again", and the row says
Couldn't restart magpie with the reason and gives its buttons back.
/api/update now says its run (the time this process started), so a
browser's page reloads once another run answers.

The click, its error and the reload are in versionrow.js, imported when
Restart is pressed. The mirror's Download from GitHub click moved there
too, so app.js stays under its budget with no raise (TestCoreScriptBudget
at 1,088,248 and 1,272,581, as they were). The route is named in the
CLI's notYet list. Words in en, zh, zh-TW, ja and de.

What needed a restart before, besides an edited path plugin (the commit
before): most settings already applied live (proxies, OTel, HTTPS/mTLS,
LAN from the GUI, allow-lists, CORS, shortcuts, tray, language, name,
fonts, port, hand-edited caller-keys, oidc-rules and library.json).
Still start-only: LAN switched by hand in settings.json, environment
variables, and in-memory routing state such as a rate-limited account's
order; Restart is for those.

Run:
- Full Go suite (mote, -timeout 45m) at the head before the rebase and
  before versionrow.js: 101 packages ok; the reds were
  TestEveryRouteHasACommandOrIsNamed, TestEveryRouteIsNamedForTheCLI
  (the route unnamed) and TestCoreScriptBudget, all fixed here.
- At this head (mote): go test ./internal/gui -run
  'TestCoreScriptBudget|TestEveryRouteIsNamedForTheCLI|TestRestart|
  TestUpdateSaysItsRun|TestTrayMenu' ok; ./internal/cli -run
  TestEveryRouteHasACommandOrIsNamed ok. go build, go vet, GOOS=linux
  go vet -tags nogui ./..., GOOS=windows go vet ./... ok; gofmt -l
  clean.
- GUI (mote, both engines, at this tree): update-*.test.cjs,
  version-restart, settings-update-source, refresh-page, old-webkit,
  i18n-keys and i18n-en-no-cjk: 628 pass, then 6 red in update-check
  (Restart not dimmed while a check runs), fixed here (Restart dims
  with Check); update-check, version-restart and old-webkit again: 90
  pass, 0 fail.
- Real restart (mote, sandbox HOME): a built magpie web with
  MAGPIE_WEB_KEY, signed in through ?k=; POST /api/restart answered 204,
  the same pid ran again with a new run, and the cookie still signed in.
- Broken by hand: restartAppFrom returning true on an error, and
  TestRestartStartsMagpieAgain is red; version-restart.test.cjs on the
  old app.js is red (21 on chromium); with Restart shown beside an
  update on offer again, "no Restart beside an update on offer" is red
  in all 5 languages and both engines (12 fails).
- The whole GUI suite is still running on mica at push time; its reds
  are compared against origin/main and fixed or filed after.

Not tried: the tray item and a restart in the real macOS, Windows and
Linux apps (only magpie web was restarted for real).
2026-11-18 12:58:38 +08:00

164 lines
10 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Run with Node's test runner and Playwright on the module path; see README.md.
// Settings › About's Version row restarts magpie as it is (yansheng1003 on
// X: "请在刷新旁边增加一个重启按钮"):
// - up to date, the row has Restart beside Check, on one line at 400px
// too; a click posts /api/restart with the tab the window is on and says
// Restarting…;
// - in a browser (magpie web), the page reloads once another run of
// magpie answers /api/update, of the same version;
// - a restart that can't be arranged says so and gives the buttons back;
// - an update downloaded has its own Restart to update, and no second
// Restart.
// Every language, both engines; 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", latest: "Up to date", check: "Check", restart: "Restart", restarting: "Restarting…", failed: "Couldn't restart magpie", update: "Restart to update" },
zh: { version: "版本", latest: "已是最新", check: "检查", restart: "重启", restarting: "正在重启…", failed: "无法重启 magpie", update: "重启以更新" },
"zh-TW": { version: "版本", latest: "已是最新", check: "檢查", restart: "重啟", restarting: "正在重啟…", failed: "無法重啟 magpie", update: "重啟以更新" },
ja: { version: "バージョン", latest: "最新です", check: "確認", restart: "再起動", restarting: "再起動中…", failed: "magpie を再起動できませんでした", update: "再起動してアップデート" },
de: { version: "Version", latest: "Aktuell", check: "Prüfen", restart: "Neu starten", restarting: "Neustart…", failed: "magpie konnte nicht neu gestartet werden", update: "Zum Aktualisieren neu starten" },
};
function server(lang, ctl) {
const settings = {
theme: "light", lang, tray: "panel", quotaLeft: false, currency: "usd", proxy: "", redact: false, redactWords: [],
version: "0.1.400", dir: "~/.config/magpie", gateway: "http://127.0.0.1:3425", proxyNow: "none", proxySource: "none",
visionModels: [], imageGenModels: [], workbuddyCheckins: [], lanURLs: [], fx: { rate: 7.2, at: new Date().toISOString(), stale: false },
};
return async (route) => {
const req = route.request();
const url = new URL(req.url());
const json = (data, status) => route.fulfill({ json: data, status: status || 200 });
if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:${!!ctl.web}};` });
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: settings.fx });
if (url.pathname === "/api/settings") return json(settings);
if (url.pathname === "/api/update") return json(ctl.update || { state: "latest", current: "0.1.400", run: ctl.run });
if (url.pathname === "/api/restart" && req.method() === "POST") {
ctl.posts.push(req.postDataJSON());
if (ctl.fail) return json({ error: "magpie couldn't start again: quit it and open it again" }, 400);
return route.fulfill({ status: 204, 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({});
if (!/^\/[\w./-]*$/.test(url.pathname) || url.host !== "magpie.test") return route.fulfill({ status: 404, body: "" });
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)];
try { await route.fulfill({ body: await fs.readFile(file), contentType }); } catch { await route.fulfill({ status: 404, body: "" }); }
};
}
async function open(browser, lang, ctl, width) {
const errors = [];
const page = await (await browser.newContext({ viewport: { width, height: 700 } })).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 watchSettingsReads(page);
await page.goto("http://magpie.test/?view=settings&tab=about");
await settingsDrawn(page);
return { page, errors };
}
const versionRow = (page, lang) => page.locator("#about .row.pref", { has: page.locator(".name", { hasText: words[lang].version }) }).first();
const buttons = (row) => row.locator(".val button").evaluateAll((bs) => bs.map((b) => b.textContent));
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
test(engine + ": the Version row restarts magpie as it is", async (t) => {
assert(["chromium", "webkit"].includes(engine), "BROWSER must be chromium or webkit");
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
for (const lang of Object.keys(words)) {
const w = words[lang];
for (const width of [900, 400]) {
await t.test(`${lang} ${width}px: Restart beside Check, and a click restarts`, async () => {
const ctl = { posts: [], run: 111 };
const { page, errors } = await open(browser, lang, ctl, width);
const row = versionRow(page, lang);
await row.locator(".sub", { hasText: w.latest }).waitFor();
assert.deepEqual(await buttons(row), [w.check, w.restart]);
const tops = await row.locator(".val button").evaluateAll((bs) => bs.map((b) => Math.round(b.getBoundingClientRect().top)));
assert.equal(new Set(tops).size, 1, `the buttons wrap: tops ${tops}`);
const over = await page.evaluate(() => document.documentElement.scrollWidth - document.documentElement.clientWidth);
assert.ok(over <= 0, `the page scrolls sideways by ${over}px`);
await row.locator(".val button", { hasText: w.restart }).click();
await row.locator(".sub", { hasText: w.restarting }).waitFor();
assert.deepEqual(ctl.posts, [{ view: "settings" }]);
assert.ok(await row.locator(".val button").evaluateAll((bs) => bs.every((b) => b.disabled)), "the buttons stay live while it restarts");
assert.deepEqual(errors, []);
await page.context().close();
});
}
await t.test(lang + ": in a browser, the page reloads once the next run answers", async () => {
const ctl = { posts: [], run: 111, web: true };
const { page, errors } = await open(browser, lang, ctl, 900);
const row = versionRow(page, lang);
await row.locator(".sub", { hasText: w.latest }).waitFor();
let loads = 0;
page.on("load", () => loads++);
await row.locator(".val button", { hasText: w.restart }).click();
await row.locator(".sub", { hasText: w.restarting }).waitFor();
assert.equal(ctl.posts.length, 1); // the tab it names is the server's to ignore (restartAppFrom)
await page.waitForTimeout(1500); // the same run still answers: no reload
assert.equal(loads, 0, "reloaded while the same run answered");
ctl.run = 222;
await page.waitForEvent("load", { timeout: 5000 });
assert.deepEqual(errors, []);
await page.context().close();
});
await t.test(lang + ": a restart that can't be arranged says so", async () => {
const ctl = { posts: [], run: 111, fail: true };
const { page, errors } = await open(browser, lang, ctl, 900);
const row = versionRow(page, lang);
await row.locator(".sub", { hasText: w.latest }).waitFor();
await row.locator(".val button", { hasText: w.restart }).click();
await row.locator(".sub", { hasText: w.failed }).waitFor();
assert.ok(await row.locator(".val button").evaluateAll((bs) => bs.every((b) => !b.disabled)), "the buttons weren't given back");
assert.deepEqual(errors, []);
await page.context().close();
});
// an update on offer restarts by its own path, and a narrow row has
// no room for a third button (update-download's 420px)
await t.test(lang + ": no Restart beside an update on offer", async () => {
for (const update of [
{ state: "found", current: "0.1.400", latest: "0.1.401", size: 3e7 },
{ state: "available", current: "0.1.400", latest: "0.1.401" },
{ state: "latest", current: "0.1.400", channel: "stable", newest: "0.1.401" },
{ state: "error", current: "0.1.400", latest: "0.1.401", error: "timed out" },
]) {
const ctl = { posts: [], update: { ...update, run: 111 } };
const { page, errors } = await open(browser, lang, ctl, 400);
const row = versionRow(page, lang);
await row.locator(".val button").first().waitFor();
assert.ok(!(await buttons(row)).includes(w.restart), update.state + ": " + (await buttons(row)).join(", "));
assert.deepEqual(errors, []);
await page.context().close();
}
});
await t.test(lang + ": an update downloaded has no second Restart", async () => {
const ctl = { posts: [], update: { state: "ready", current: "0.1.400", latest: "0.1.401", run: 111 } };
const { page, errors } = await open(browser, lang, ctl, 900);
const row = versionRow(page, lang);
await row.locator(".val button", { hasText: w.update }).waitFor();
assert.deepEqual(await buttons(row), [w.update]);
assert.deepEqual(errors, []);
await page.context().close();
});
}
});
}