Files
magpie/internal/gui/tests/restore-on-quit.test.cjs
T
yetone 7e3e0fec4f gui: put agents back as they were when magpie quits, connect them again at start (tudjam17795 on X)
New Settings > General switch, "Put agents back when quitting" (zh: 退出时还原 Agent 配置),
default off. With it on, quitting the GUI app runs the same restore Disconnect does
for every agent magpie has connected (*.before-magpie files back byte for byte), and the
next start connects those agents again. Off keeps today's behaviour: a connected agent
keeps magpie as it quits.

- agent.RestoreOnQuit disconnects each wired agent that the user did not disconnect, then
  forgets the <id>.disconnected mark, so the quit leaves no mark; it stashes the list of ids.
  agent.RewireOnStart takes that list once and connects those still unwired and not
  disconnected since. An agent the user disconnected stays disconnected.
- gui: OnShutdown restores only when the setting is on and this process served the
  gateway; start re-wires in the background (not while the gateway is paused). A restore
  on quit also quits at once without the ask.
- settings.RestoreOnQuit, settingsapi key kept unless sent, row in index.html, i18n for
  zh, zh-TW, ja, de, docs in gui-shell.md and agent-wiring.md.
- app.js: the repeated Off/On settings rows go through one helper (segsOO), 422 bytes
  smaller overall, so TestCoreScriptBudget passes without raising the budget.

Run (sandbox HOME, mica because mote was loaded):
- go test internal/agent, internal/gui, internal/settings, internal/settingsapi, internal/cli
  at the rebased head: the new tests and the budget test pass.
- GUI, chromium and webkit, five languages, widths 1100 and 420: restore-on-quit (new),
  quit-ask-setting, quit-ask, settings-general, settings-sections, settings-places,
  i18n-keys, i18n-en-no-cjk, gui-de, gui-ja, gui-zh-tw, and for the rows moved to
  segsOO: reduce-motion, skeleton-bird, lightweight, keep-login, checkin-at,
  update-auto, update-hide, update-restart, update-download, tray-bird, tray-logos,
  tray-cost, wsl-agents-setting, login-allow. All 0 fail.
- gofmt -l clean; go vet darwin, windows, linux -tags nogui clean.

Broken by hand: not forgetting the disconnected mark turns
TestRestoreOnQuitAndRewireOnStart and TestRestoreOnQuitLeavesUserDisconnected red;
dropping the Disconnected() checks turns TestRestoreOnQuitLeavesUserDisconnected red;
dropping the setting gate turns TestRestoreAgentsOnQuit red.

Known reds, not from this change (same on the base 60559ee73): 20 GUI failures in
settings-hints, settings-lines-wrap and settings-search (#6133, #6107, #6156, #6073);
internal/gui TestPanelHiddenByWailsMarksPageOutOfSight killed under load (#6063);
internal/agent TestPiGroupEntryKeepsItsMembers on mica.

Not tried: real agent configs (sandbox HOME only), a real Wails quit on a Mac with real
accounts, Windows via win-run, the Linux WebKitGTK app.
2026-11-12 00:19:34 +08:00

135 lines
7.1 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.
// tudjam17795 on X: after magpie quits, the harness it was connected to
// can't go on. Settings › General › Put agents back when quitting is Off by
// default, On saves restoreOnQuit with the rest of the settings kept, and
// Off saves it false. The app's only: a browser's page has no quit to
// restore at. In every language, both engines, 1100 and 420 wide; segments,
// never a native <select>, and the click moves nothing.
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: { name: "Put agents back when quitting", off: "Off", on: "On" },
zh: { name: "退出时还原 Agent 配置", off: "关闭", on: "开启" },
"zh-TW": { name: "退出時還原 Agent 設定", off: "關閉", on: "開啟" },
ja: { name: "終了時にエージェントを元に戻す", off: "オフ", on: "オン" },
de: { name: "Agenten beim Beenden zurücksetzen", off: "Aus", on: "An" },
};
function serve(lang, saved, own, web) {
const settings = { lang, theme: "light", redact: true, keepAwake: true, ...own };
const state = { agents: [], profiles: [], settings };
return async (r) => {
const url = new URL(r.request().url());
const json = (data) => r.fulfill({ json: data });
if (url.pathname === "/boot.js") return r.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:${web}};` });
if (url.pathname === "/wails/runtime.js") return r.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
if (url.pathname === "/api/state") return json(state);
if (url.pathname === "/api/settings") {
if (r.request().method() === "POST") {
saved.push(JSON.parse(r.request().postData()));
Object.assign(settings, saved.at(-1));
}
return json(settings);
}
if (url.pathname === "/api/groups") return json({ models: [], groups: [], pools: [] });
if (url.pathname === "/api/plugins") return json({ plugins: [] });
if (url.pathname === "/api/usage/quotas") return json([]);
if (url.pathname === "/api/providers") return json({ providers: [], presets: [], excluded: [], gateway: { running: true, window: true, url: "http://127.0.0.1:3999" } });
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 r.fulfill({ body: await fs.readFile(file), contentType });
};
}
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of Object.keys(words)) for (const width of [1100, 420]) {
const w = words[lang];
const open = async (t, own = {}, web = false) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
const page = await (await browser.newContext({ viewport: { width, height: 900 } })).newPage();
page.setDefaultTimeout(5000);
page.setDefaultNavigationTimeout(60000);
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
t.after(() => assert.deepEqual(errors, []));
const saved = [];
await page.route("**/*", serve(lang, saved, own, web));
await watchSettingsReads(page);
await page.goto("http://magpie.test/?view=settings&tab=general");
await settingsDrawn(page);
return { page, saved };
};
// scrolled to as a reader does, with the wheel
const reach = async (page, row) => {
await page.mouse.move(width / 2, 450);
for (let i = 0; i < 40; i++) {
const box = await row.boundingBox();
if (box && box.y >= 0 && box.y + box.height <= 900) break;
await page.mouse.wheel(0, 120);
await page.waitForTimeout(50);
}
};
const press = async (page, button) => {
const b = await button.boundingBox();
await page.mouse.click(b.x + b.width / 2, b.y + b.height / 2);
};
const until = async (f) => { for (let i = 0; i < 100 && !f(); i++) await new Promise((r) => setTimeout(r, 50)); };
test(`${engine} ${lang} ${width}: Put agents back is Off, and On saves restoreOnQuit`, async (t) => {
const { page, saved } = await open(t);
const row = page.locator("#setPage-general #restoreQuitRow");
await row.waitFor();
await reach(page, row);
assert.equal(await row.locator(".name").textContent(), w.name);
for (const part of [".name", ".segs"]) {
const fit = await row.locator(part).first().evaluate((e) => {
const r = e.getBoundingClientRect();
return e.scrollWidth <= e.clientWidth + 1 && r.width > 0 && r.left >= 0 && r.right <= innerWidth + 1;
});
assert.ok(fit, `${part} fits at ${width}`);
}
assert.equal(await row.locator("select").count(), 0, "no native <select>");
const on = row.locator(".segs button", { hasText: w.on });
const off = row.locator(".segs button", { hasText: w.off });
assert.equal(await off.getAttribute("aria-pressed"), "true", "Off by default");
const before = await page.evaluate(() => [scrollX, scrollY, document.querySelector("#view-settings")?.scrollTop]);
await press(page, on);
await until(() => saved.length);
assert.equal(saved.length, 1, "saved once");
assert.equal(saved[0].restoreOnQuit, true);
assert.equal(saved[0].quitNoAsk, false);
assert.equal(saved[0].keepAwake, true, "the row near it kept");
assert.equal(saved[0].redact, true, "the rest kept");
assert.equal(saved[0].lang, lang);
assert.deepEqual(await page.evaluate(() => [scrollX, scrollY, document.querySelector("#view-settings")?.scrollTop]), before, "the click moved nothing");
});
test(`${engine} ${lang} ${width}: a saved restoreOnQuit shows On, and Off saves it false`, async (t) => {
const { page, saved } = await open(t, { restoreOnQuit: true });
const row = page.locator("#setPage-general #restoreQuitRow");
await row.waitFor();
await reach(page, row);
assert.equal(await row.locator(".segs button", { hasText: w.on }).getAttribute("aria-pressed"), "true");
await press(page, row.locator(".segs button", { hasText: w.off }));
await until(() => saved.length);
assert.equal(saved.length, 1, "saved once");
assert.equal(saved[0].restoreOnQuit, false);
assert.equal(saved[0].keepAwake, true, "the rest kept");
});
if (width === 420) test(`${engine} ${lang}: a browser's page has no Put agents back`, async (t) => {
const { page } = await open(t, {}, true);
await page.locator("#setPage-general #langPick").waitFor({ state: "attached" });
assert.equal(await page.locator("#restoreQuitRow").isVisible(), false);
});
}
}