Files
magpie/internal/gui/tests/usage-ledger-pick-pending.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

122 lines
7.5 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// A filter picked on Usage > Requests is seen taken at once, while its read
// is on its way (#1990, overstarry: on Windows over a long history, the
// agent picked "has no response" and "the selection change does not take
// effect"). The read is held here as a long history holds it: the picker
// says the agent picked at once, the rows shown stay, dimmed and marked
// busy, and when the answer comes they are its rows, undimmed. Picking All
// agents again does the same. A failed read undims them too. Nothing
// moves. English, Chinese (both), Japanese and German, Chromium and
// WebKit; 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 assets = path.resolve(__dirname, "../assets");
const now = Date.now();
const ROWS = [];
for (let i = 0; i < 40; i++) {
ROWS.push({ t: new Date(now - (60 + i * 60) * 1e3).toISOString(), agent: i % 2 ? "claude" : "codex", agentName: i % 2 ? "Claude Code" : "Codex",
provider: "relay", providerName: "Relay", model: "gpt-6-sol", in: 1000 + i, out: 100, ms: 900, status: 200, cost: 0.001, priced: true });
}
function answer(q) {
const rows = ROWS.filter((r) => !q.get("agent") || r.agent === q.get("agent"));
return {
period: q.get("period"), rows: rows.slice(0, 100), offset: 0, total: rows.length, calls: rows.length, errors: 0,
input: rows.reduce((a, r) => a + r.in, 0), output: 100 * rows.length, cost: 0.001 * rows.length,
agents: [{ id: "claude", name: "Claude Code" }, { id: "codex", name: "Codex" }],
};
}
function server(lang, held) {
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/gateway/trace") return json({ mine: false, seq: 0, routes: [], totals: { requests: 0, rerouted: 0, errors: 0 }, now: new Date().toISOString() });
if (url.pathname === "/api/state") return json({ agents: [], profiles: [], settings: { lang, theme: "light" } });
if (url.pathname === "/api/usage/requests") {
const q = url.searchParams;
// a pick's read, held till the test lets it go
if (held.on) await new Promise((r) => held.waiting.push(r));
if (held.fail) return route.fulfill({ status: 500, body: "read failed" });
return json(answer(q));
}
if (url.pathname === "/api/usage") return json({ calls: 40, input: 40000, output: 4000, cost: 0.04, bucket: "day", series: [], agents: [], models: [], path: "~/.config/magpie/usage.jsonl" });
if (url.pathname === "/api/sessions") return json({ sessions: [], dirs: [] });
if (url.pathname === "/api/providers") return json({ providers: [], presets: [], excluded: [], models: [], gateway: { running: false } });
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 });
};
}
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of ["en", "zh", "zh-TW", "ja", "de"]) {
test(`${engine} ${lang}: an agent picked on Requests is seen taken while its read is on its way`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
const held = { on: false, fail: false, waiting: [] };
const release = async (page) => {
for (let i = 0; i < 60 && !held.waiting.length; i++) await page.waitForTimeout(25);
assert(held.waiting.length, "the read was asked");
held.on = false;
for (const r of held.waiting.splice(0)) r();
};
const errors = [];
const page = await (await browser.newContext({ viewport: { width: 1100, height: 700 }, reducedMotion: "reduce" })).newPage();
page.setDefaultTimeout(5000);
page.setDefaultNavigationTimeout(60000);
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", server(lang, held));
await page.goto("http://magpie.test/");
await page.locator('[data-view="usage"]').first().click();
await page.locator(":is(#usageTab .opt, .nav-s):visible").nth(1).click();
await page.locator("#ledWrap .led tbody tr").first().waitFor();
const all = await page.evaluate(() => t("All agents"));
const rows = () => page.locator("#ledWrap .led-row").count();
const pending = () => page.locator("#ledWrap").evaluate((e) => [e.classList.contains("led-pending"), e.getAttribute("aria-busy")]);
const dim = () => page.locator("#ledWrap > *").first().evaluate((e) => getComputedStyle(e).opacity);
const spot = () => page.evaluate(() => [document.scrollingElement.scrollTop, ...[...document.querySelectorAll(".view")].map((v) => v.scrollTop)]);
assert.equal(await rows(), 40);
assert.equal(await page.locator("#ledAgent").textContent(), all);
assert.deepEqual(await pending(), [false, null]);
for (const [pick, name, n] of [["Claude Code", "Claude Code", 20], [all, all, 40]]) {
held.on = true;
const was = await spot();
await page.locator("#ledAgent").click();
await page.locator(".sess-menu .pm-item", { hasText: pick }).first().click();
// at once, the read still held: the picker says it, the rows wait dimmed
assert.equal(await page.locator("#ledAgent").textContent(), name, "the picker says the pick at once");
assert.equal(await page.locator("#ledAgent").evaluate((e) => e.classList.contains("set")), name !== all);
assert.deepEqual(await pending(), [true, "true"], "the rows shown wait, marked busy");
assert(Number(await dim()) < 1, "and dimmed");
assert.equal(await rows(), n === 20 ? 40 : 20, "the rows shown stay till the answer comes");
await release(page);
await page.waitForFunction((n) => document.querySelectorAll("#ledWrap .led-row").length === n, n);
assert.deepEqual(await pending(), [false, null], "the answer undims them");
assert.equal(await dim(), "1");
assert.equal(await page.locator("#ledAgent").textContent(), name);
assert.deepEqual(await spot(), was, "the pick moved the page");
}
// a read that fails undims them too
held.on = true;
held.fail = true;
await page.locator("#ledAgent").click();
await page.locator(".sess-menu .pm-item", { hasText: "Codex" }).first().click();
assert.deepEqual(await pending(), [true, "true"]);
await release(page);
await page.waitForFunction(() => !document.querySelector("#ledWrap").classList.contains("led-pending"));
assert.equal(await page.locator("#ledWrap").getAttribute("aria-busy"), null);
assert.deepEqual(errors, []);
});
}
}