Files
magpie/internal/gui/tests/routing-idle-frame.test.cjs
T
yetone eb7199e120 gui: Routing is a plugin's page, its script, style and words in plugins/routing (P2)
P2 of docs/subsystems/plugin-runtime.md, the third page after Sessions
and Library.

- routing.js and routing.css move to assets/plugins/routing/ (client.js,
  routing.css). Its part's html is the page's head and boxes that were
  index.html's <main id="view-routing">; the rest is drawn by
  routingPage, run at mount since it builds on app.js. It keeps itself
  after that as it did (its own hidden, the language, focus), so its
  hooks are dirty and discard, a routing group being edited, which
  show() and the window's close ask through magpie.call:
  window.routingDirty and discardRouting are gone.
- plugins/routing: Essential, Requires window and gateway, in use while
  the user has a routing group (not Auto, not Hidden), since the page is
  where it is edited. The gateway routes the same with it off.
- With it off, what leads to the page goes too: the nav and panel tabs
  and the panel's section (data-feature="routing"), the guide's Open
  Routing, Providers' "Make a routing group of …", the Usage list's
  View routing and its time as a link, the context card's Latest
  request. A panel left on its Routing tab shows Agents.
- What other pages use of it stays on window: openRoute (context.js),
  kindName, swapWhy, routedWhy (Usage's Requests), newGroupWith
  (Providers), panelRoutingShown (the panel).
- The 505 strings only Routing says move from i18n.js to
  plugins/routing/i18n.js (4 languages); i18n.js 17048 → 15032 lines.
  The plugin's own name and line stay in i18n.js: the Plugins page
  says them with the plugin off (TestBuiltinsTranslated).
- Tests and comments that named routing.js name the new path (gateway's
  antigravity_rest and loop_guard, codeassist.go, usage.go,
  web_compress, group_member_info, assets_revalidate, font-cascade,
  win-fonts, three routing-*.test.cjs). builtins.test.cjs and
  builtins_test.go count seven built-ins and switch Routing off: its
  tabs go, the page shown stays.
- docs: plugin-runtime.md, gui-shell.md, gateway-routing.md.

Run, first on origin/main 3ec45e715, then after rebasing onto af27b70b4
(main's routing.js, css, app.js and i18n.js changes since were carried
into the moved files; its new Routing words, as the rules' conditions,
are in the plugin's i18n.js):
- gofmt -l clean; go vet darwin, GOOS=linux -tags nogui, GOOS=windows.
- go test -count=1 ./internal/gui ./internal/gateway ./internal/usage
  ./internal/pluginrt ./plugins/...: ok (gateway 1800s under load,
  -timeout 45m; gui with -skip TestMainWindowCorners, #1936).
- All 193 GUI test files, 3 at a time at load ~250, Chromium and WebKit,
  at the Usage commit on 743fcc035: 185 and 186 green. Every red run
  alone here and on a just-fetched origin/main c524bc018:
  - the same on main: routing-model-rest (#2115), routing-wb-restricted
    (#1905), usage-agents-hide (11 of 16 in both, #2211), usage-fold
    (#2163: 12 of 14 here, 9 of 14 on main over 7 runs, the same 4px),
    quota-count-wrap and quota-name-translated in WebKit (#2228);
  - red only under load, green alone on both: routing-flood (#2215),
    routing-log-icons, usage-ledger (#2211), and three plugin-* files
    whose tree was rebased under them mid-run.
- days-fit, failed-open, side-calls and burst-speed, red on main after
  cf455ebd5, are fixed before this commit (#2134, d692b2548).
2026-10-15 01:32:12 +08:00

209 lines
12 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// The Routing page's own frame loop runs only while the page is the one in
// sight. With Agents selected — #view-routing.hidden, the window drawing
// frames, the tab in sight — the loop must stop asking for frames: the
// function frame that drew the page every frame until now asked for the
// next one whatever the page was showing, so the magpies were posed (and
// the loop kept awake) forever while nobody looked at Routing. In sight
// again the view is picked and the loop resumes. What the poll lists came
// in meanwhile is another test's business (routing-flood); this one counts
// the loop's own callbacks.
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 engines = process.env.BROWSER ? [process.env.BROWSER] : ["webkit"];
const assets = path.resolve(__dirname, "../assets");
const routing = path.join(assets, "plugins/routing/client.js");
const iso = (ms) => new Date(ms).toISOString();
const acct = (n) => ({ id: `acct-${n}`, provider: `prov${n}`, name: `Provider ${n}`, who: `user${n}@example.com`, kind: "account", model: `model-${n}`, known: true, used: 10 * n, plan: "Pro" });
const agentsOf = ["codex", "claude", "opencode", "kimi"];
function req(id) {
const t0 = Date.now() - 500;
const order = [acct(id % 5), acct((id + 1) % 5)];
const tries = order.slice(0, 1).map((w) => ({ id: w.id, model: w.model, start: iso(t0), done: true, ms: 300, status: 200 }));
return { id, seq: id, time: iso(t0), agent: agentsOf[id % agentsOf.length], model: order[0].model, provider: order[0].provider, order, tries, done: true, status: 200, ms: 400, tokens: 900 };
}
function serve(lang, feed = [req(1)]) {
const state = { agents: agentsOf.map((id) => ({ id, name: id[0].toUpperCase() + id.slice(1), path: `/test/${id}`, fields: [] })), profiles: [], settings: { lang, theme: "light" } };
return async (route) => {
const url = new URL(route.request().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:true};` });
if (url.pathname === "/wails/runtime.js") return route.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
if (url.pathname === "/api/state") return json(state);
if (url.pathname === "/api/plugins") return json({ plugins: [] });
if (url.pathname === "/api/gateway/trace") return json({ mine: true, now: new Date().toISOString(), seq: url.searchParams.get("after") || 0, totals: { requests: 0, rerouted: 0, errors: 0 }, routes: url.searchParams.get("wait") ? [] : feed });
if (url.pathname === "/api/gateway/history") return json({ cut: false, days: [], routes: [] });
if (url.pathname === "/api/groups") return json({ models: [], groups: [], pools: [] });
if (url.pathname === "/api/providers") return json({ providers: [], 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 route.fulfill({ body: await fs.readFile(file), contentType });
};
}
// counts the callbacks of the loop's own frame, whatever name the minifier
// would give it: the function that asks for itself again
const init = () => {
const raf = window.requestAnimationFrame.bind(window);
window.__frames = 0;
window.__steps = 0;
window.__packets = 0;
window.__hidden = false;
Object.defineProperty(document, "hidden", { configurable: true, get: () => window.__hidden });
window.__visibility = (hidden) => { window.__hidden = hidden; document.dispatchEvent(new Event("visibilitychange")); };
new MutationObserver((records) => {
for (const record of records) for (const node of record.addedNodes) {
if (node.nodeType === 1 && node.matches(".pkt")) window.__packets++;
}
}).observe(document, { childList: true, subtree: true });
window.__seenFrame = false;
// marked between frames, and the next frame awaited, so the one already
// asked for when the mark falls is out of the count before it settles
window.__mark = () => { window.__frames = 0; return new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r))); };
window.__count = (ms) => new Promise((r) => setTimeout(() => r(window.__frames), ms));
window.requestAnimationFrame = (cb) => {
const self = typeof cb === "function" && /requestAnimationFrame\s*\(\s*([A-Za-z_$][\w$]*)/.test(cb.toString()) && new RegExp(`\\b${RegExp.$1}\\s*\\(`).test(cb.toString());
if (typeof cb === "function" && (cb.name === "frame" || self)) window.__seenFrame = true;
return raf((ts) => { if (cb.name === "step") window.__steps++; if (typeof cb === "function" && (cb.name === "frame" || self)) window.__frames++; cb(ts); });
};
};
const byName = (src) => /(?:function\s+frame\s*\(|(?:const|let|var)\s+frame\s*=\s*(?:function|\(?\s*[\w,\s]*\)?\s*=>)|frame[:\s]*\(?\s*[\w,\s]*\)?\s*(?::[^=]+)?=>)/.test(src);
const selfRaf = (src) => { const m = src.match(/requestAnimationFrame\s*\(\s*([A-Za-z_$][\w$]*)\s*\)/); return m ? new RegExp(`function\\s+${m[1]}\\s*\\(`).test(src) : false; };
test("the loop's frame is still there to be counted, and asks for itself", async () => {
const src = await fs.readFile(routing, "utf8");
assert(byName(src) || selfRaf(src), "routing/client.js must still keep its frame loop in a function of its own, or the test below counts nothing");
});
for (const engine of engines) for (const lang of ["en", "zh"]) {
test(`${engine} ${lang}: with Agents selected the Routing loop asks for no more frames, and picking Routing again resumes it`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
const context = await browser.newContext({ viewport: { width: 1100, height: 900 } });
await context.addInitScript(init);
const page = await context.newPage();
page.setDefaultTimeout(5000);
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", serve(lang));
t.after(async () => {
if (process.env.ARTIFACT_DIR) {
await fs.mkdir(process.env.ARTIFACT_DIR, { recursive: true });
await page.screenshot({ path: path.join(process.env.ARTIFACT_DIR, `${engine}-${lang}-idle-frame.png`) });
}
await browser.close();
});
await page.goto("http://magpie.test/?view=routing");
await page.locator(".rt-req").first().waitFor();
// Routing in sight: the loop is running, and it is this loop we count
await page.waitForFunction(() => window.__seenFrame, null, { timeout: 5000 });
await page.evaluate(() => window.__mark());
const flying = await page.evaluate(() => window.__count(600));
assert(flying > 10, `the loop must run while Routing is in sight (${flying} frames in 600ms)`);
assert(errors.length === 0, errors.join("\n"));
// Agents picked: #view-routing.hidden, the tab in sight, frames drawn
await page.locator('#nav button[data-view="agents"]').click();
assert.equal(await page.evaluate(() => document.querySelector("#view-routing").hidden), true, "Agents selected must hide the Routing view");
await page.evaluate(() => window.__mark());
const idle = await page.evaluate(() => window.__count(1200));
// the frame already asked for when Agents was picked still runs, and
// finds the page out of sight: it is the last one
assert(idle <= 1, `${idle} frames of the Routing loop were drawn while Agents was shown, and it kept asking for more`);
// and it stays stopped, rather than waking on a later request
await page.evaluate(() => window.__mark());
assert.equal(await page.evaluate(() => window.__count(1200)), 0, "the Routing loop came back with Agents still selected");
// Routing picked again: the loop resumes, and the page is drawn
await page.locator('#nav button[data-view="routing"]').click();
await page.evaluate(() => window.__mark());
const again = await page.evaluate(() => window.__count(600));
assert(again > 10, `picking Routing again must resume the loop (${again} frames in 600ms)`);
assert.equal(await page.evaluate(() => document.querySelectorAll(".rt-req").length >= 1), true, "the page is drawn again");
assert(errors.length === 0, errors.join("\n"));
// A document visibility event must stop the loop even when the engine
// would suspend its last scheduled frame before it can observe hidden.
await page.evaluate(() => window.__visibility(true));
await page.evaluate(() => window.__mark());
assert.equal(await page.evaluate(() => window.__count(600)), 0);
await page.evaluate(() => window.__visibility(false));
await page.evaluate(() => window.__mark());
assert(await page.evaluate(() => window.__count(600)) > 10);
assert.equal(errors.length, 0, errors.join("\n"));
});
}
const hiddenWays = {
"another view": {
hide: page => page.locator('#nav button[data-view="agents"]').click(),
show: page => page.locator('#nav button[data-view="routing"]').click(),
},
"document visibility": {
hide: page => page.evaluate(() => window.__visibility(true)),
show: page => page.evaluate(() => window.__visibility(false)),
},
};
async function routingPage(t, engine, lang, feed, view = "routing") {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
const context = await browser.newContext({ viewport: { width: 1100, height: 900 } });
await context.addInitScript(init);
const page = await context.newPage();
page.setDefaultTimeout(5000);
const errors = [];
page.on("pageerror", e => errors.push(e.message));
await page.route("**/*", serve(lang, feed));
await page.goto(`http://magpie.test/?view=${view}`);
await page.waitForFunction(() => document.querySelectorAll(".rt-req").length > 0);
return { page, errors };
}
for (const engine of engines) {
for (const lang of ["en", "zh"]) {
for (const [way, how] of Object.entries(hiddenWays)) {
test(`${engine} ${lang}: hiding a running replay through ${way} ends it and stops its frames`, async t => {
const { page, errors } = await routingPage(t, engine, lang, [1, 2, 3, 4, 5].map(req));
await page.getByRole("button", { name: lang === "zh" ? "全部重放" : "Replay them all", exact: true }).click();
await page.waitForFunction(() => !document.querySelector(".rt-replay").hidden && document.querySelectorAll(".pkt").length > 0);
await how.hide(page);
// Check the bar's own hidden flag, not visibility inherited from Agents.
await page.waitForFunction(() => document.querySelector(".rt-replay").hidden && document.querySelectorAll(".pkt").length === 0);
await page.evaluate(() => { window.__steps = 0; });
await page.waitForTimeout(300);
assert(await page.evaluate(() => window.__steps) <= 1, "replay kept scheduling frames after it was hidden");
await how.show(page);
assert(await page.evaluate(() => document.querySelector(".rt-replay").hidden), "showing Routing restarted the old replay");
assert.equal(errors.length, 0, errors.join("\n"));
});
}
test(`${engine} ${lang}: each live request plays once when Routing resumes`, async t => {
const r = req(1);
r.done = false;
r.status = 0;
r.tries.forEach(tr => { tr.done = false; tr.status = 0; });
const { page, errors } = await routingPage(t, engine, lang, [r], "agents");
const showOnce = async show => {
await page.evaluate(() => { window.__packets = window.__frames = 0; });
await show(page);
await page.waitForFunction(() => window.__frames >= 2);
assert.equal(await page.evaluate(() => window.__packets), 1, "resume played a live request twice");
};
await showOnce(hiddenWays["another view"].show);
for (const how of Object.values(hiddenWays)) {
await how.hide(page);
await page.evaluate(() => window.__mark());
await showOnce(how.show);
}
assert.equal(errors.length, 0, errors.join("\n"));
});
}
}