Files
magpie/internal/gui/tests/gateway-pause.test.cjs
T
yetone e8fbee60ea gui: pause the gateway without quitting magpie (me2seeks on X)
Disconnect and quit was the only way to stop the gateway, and it quits
magpie too. Pause does the same without the quit, from the tray's Pause
Gateway (Resume Gateway while paused), Settings › General › Gateway
(On/Paused, the app's only) and the Gateway page's Resume gateway, all
through POST /api/gateway/pause.

- Pause disconnects each connected agent as its row's Disconnect does
  (its own settings put back), keeps their ids in gateway-paused.json in
  the config folder, and stops the listener with requests in flight
  drained. An agent that can't be disconnected keeps the gateway serving,
  those before it connected again, and is named in the status line.
- While paused, serveGatewayLocked serves nothing: not the 15s watch, a
  port change nor a handover; a magpie started again stays paused. A
  pause file that can't be read counts as paused.
- Resume serves again and connects the agents the pause disconnected.
  A port held by something else keeps the pause and says what holds it.
  The Gateway page's Start/Restart, connecting an agent and picking one
  of magpie's models for it resume too.
- quitask's wired list and the pause share wiredOf.
- Strings in en, zh, zh-TW, ja, de. gui-shell.md documents it.

Run (mote was unreachable for the later runs; the Windows box instead):
- mote, before the rebase: go vet ./internal/gui; go test -run
  'TestGatewayPause|TestGatewayResumes|TestTray|TestGatewayFix|TestQuit'
  ./internal/gui: ok.
- at this tree on b46f23ffa: win-run go vet ./internal/gui ok; wsl-run
  go vet -tags nogui ./internal/gui ok; gofmt -l clean.
- win-run go test -count=1 ./internal/gui: the pause and tray tests ok;
  8 reds, each the same on origin/main d4b4a188e run alone on Windows
  and filed (#4115 %AppData% import rows, #5928 ~\ paths, #2107/#3649
  TestGatewayHeldByAContainer).
- wsl-run GUI (chromium + webkit): gateway-pause (5 languages x 1000/420),
  settings-general, gateway-takeover, i18n-keys, i18n-en-no-cjk: 51/52.
  The red is gateway-takeover webkit en's dark-mode case, red 1 of 2 on
  untouched origin/main too: #5942.
- rebased on 401187300: win-run go test -run 'Budget|TestGatewayPause|
  TestGatewayResumes|TestTray|TestQuit' ./internal/gui ok; wsl-run go vet
  -tags nogui ./internal/gui ok; gofmt clean. darwin vet not rerun after
  the rebase (mote refused SSH).

Broken by hand: serveGatewayLocked without its pausedNow check, and the
rollback's reconnect skipped: TestGatewayPauseAndResume ("served while
paused") and TestGatewayPauseRollsBack ("codex not connected again")
went red; green again restored.

Not tried: the real app's tray on a desktop, a real agent's config
round-trip (the Go tests fake the agents; Disconnect/connect are the
rows' own code), an agent wired from the CLI while the app is paused
(it doesn't resume), the whole GUI suite, the full Go suite (started after the tag).
2026-11-10 19:36:24 +08:00

123 lines
8.0 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// Pausing the gateway (me2seeks on X: the gateway could be stopped only by
// quitting magpie with Disconnect and quit). Settings → General has a
// Gateway row, On or Paused: Paused asks the backend to pause (agents
// disconnected, the listener stopped), and the row reads Paused once it
// says so. An agent that couldn't be disconnected is named in the status
// line. The Gateway page's card then reads paused, with Resume gateway,
// which asks to resume and leaves it running. In every language, both
// engines, wide and narrow; what is clicked stays put on screen, nothing runs off
// it sideways. 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 { watchSettingsReads, settingsDrawn } = require("./reader.cjs");
const assets = path.resolve(__dirname, "../assets");
const words = {
en: { on: "On", paused: "Paused", state: "paused", resume: "Resume gateway", restart: "Restart gateway", failed: "Codex couldn't be disconnected: config is read-only" },
zh: { on: "开启", paused: "已暂停", state: "已暂停", resume: "恢复网关", restart: "重启网关", failed: "Codex 断开失败:config is read-only" },
"zh-TW": { on: "開啟", paused: "已暫停", state: "已暫停", resume: "恢復閘道", restart: "重啟閘道", failed: "Codex 中斷連線失敗:config is read-only" },
ja: { on: "オン", paused: "一時停止中", state: "一時停止中", resume: "ゲートウェイを再開", restart: "ゲートウェイを再起動", failed: "Codex の接続を解除できませんでした: config is read-only" },
de: { on: "An", paused: "Pausiert", state: "pausiert", resume: "Zugangspunkt fortsetzen", restart: "Zugangspunkt neu starten", failed: "Codex konnte nicht getrennt werden: config is read-only" },
};
function serve(lang, world) {
const settings = () => ({ lang, theme: "light", searchVendors: [], searchAPIs: [], gatewayPaused: world.paused });
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:false};` });
if (url.pathname === "/wails/runtime.js") return r.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
if (url.pathname === "/api/gateway/pause") {
const { on } = JSON.parse(r.request().postData());
world.posts.push(on);
if (on && world.refuse) return json({ paused: false, agent: "Codex", error: "config is read-only" });
world.paused = on;
return json({ paused: on, agents: ["codex"] });
}
if (url.pathname === "/api/state") return json({ agents: [], profiles: [], settings: settings() });
if (url.pathname === "/api/settings") return json(settings());
if (url.pathname === "/api/providers") return json({
providers: [], presets: [], excluded: [],
gateway: world.paused
? { running: false, mine: false, paused: true, window: true, url: "http://127.0.0.1:3999", calls: [], groups: [], models: 0 }
: { running: true, mine: true, window: true, url: "http://127.0.0.1:3999", calls: [], groups: [], models: 3 },
});
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/gateway/trace") return json({ mine: false, now: new Date().toISOString(), seq: 1, totals: { requests: 0, rerouted: 0, errors: 0 }, routes: [] });
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 [1000, 420]) {
const w = words[lang];
test(`${engine} ${lang} ${width}px: the gateway pauses from Settings and resumes from its page`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch());
t.after(() => browser.close());
const context = await browser.newContext({ viewport: { width, height: 760 }, reducedMotion: "reduce" });
const page = await context.newPage();
page.setDefaultTimeout(5000);
page.setDefaultNavigationTimeout(60000);
const errors = [];
page.on("pageerror", (e) => { if (engine === "webkit" && /due to access control checks/.test(e.message)) return; errors.push(e.message); }); // a goto in WebKit can cut off a fetch (#1350)
const world = { paused: false, posts: [], refuse: true };
await page.route("**/*", serve(lang, world));
await watchSettingsReads(page);
await page.goto("http://magpie.test/?view=settings&tab=general");
await settingsDrawn(page);
const row = page.locator("#gatewayPauseRow");
await row.locator(".opt.on").waitFor();
assert.equal(await row.isVisible(), true, "the row is hidden in the app");
assert.deepEqual(await row.locator(".opt").allInnerTexts(), [w.on, w.paused]);
assert.equal(await row.locator(".opt.on").innerText(), w.on);
// what the reader clicks stays put on screen (gui-shell.md, "A click
// never moves"); the view's scrollTop may change around it
const top = (l) => l.evaluate((e) => Math.round(e.getBoundingClientRect().top));
// an agent that can't be disconnected: named, and the row stays On
await row.locator(".opt", { hasText: w.paused }).click();
await page.waitForFunction((m) => document.querySelector("#status")?.textContent === m, w.failed);
assert.deepEqual(world.posts, [true]);
await page.waitForFunction((on) => document.querySelector("#gatewayPauseRow .opt.on")?.textContent === on, w.on);
// paused: the row, in sight since the click, stays put
const before = await top(row);
world.refuse = false;
await row.locator(".opt", { hasText: w.paused }).click();
await page.waitForFunction((p) => document.querySelector("#gatewayPauseRow .opt.on")?.textContent === p, w.paused);
assert.deepEqual(world.posts, [true, true]);
assert.equal(await top(row), before, "the click moved the row");
assert.equal(await page.evaluate(() => document.documentElement.scrollWidth > innerWidth), false, "the page scrolls sideways");
// the Gateway page says so, and resumes it
await page.goto("http://magpie.test/?view=gateway");
const fix = page.locator("#gateway .gw-fix");
await fix.waitFor();
assert.equal((await page.locator("#gateway .state").textContent()).trim(), w.state);
assert.equal((await fix.textContent()).trim(), w.resume);
assert.equal(await page.locator("#gateway .sub.err").count(), 0);
// the card's own text changes length, so the page's scroll is what
// holds here
const scrolls = () => page.evaluate(() => [...document.querySelectorAll(".view, html, body")].map((e) => e.scrollTop).join(","));
const gwBefore = await scrolls();
await fix.click();
await page.waitForFunction((r) => document.querySelector("#gateway .gw-fix")?.textContent.trim() === r, w.restart);
assert.deepEqual(world.posts, [true, true, false]);
assert.notEqual((await page.locator("#gateway .state").textContent()).trim(), w.state);
assert.equal(await scrolls(), gwBefore, "the click scrolled the page");
assert.equal(await page.evaluate(() => document.documentElement.scrollWidth > innerWidth), false, "the page scrolls sideways");
assert.deepEqual(errors, []);
});
}
}