Files
magpie/internal/gui/tests/gateway-key-quotas.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

71 lines
4.7 KiB
JavaScript

// A gateway key held to a budget, models or accounts is told no quotas
// (#1981) unless its owner lets it be told those of what it may use
// (#2216, Kenoia): a "Quotas" badge on the key's row, shown on hover while
// off and always while on, sends "quotas-key".
const assert = require("node:assert/strict");
const { test } = require("node:test");
const { chromium, webkit } = require("playwright");
const { fixture } = require("./fixtures/caller-keys.cjs");
const words = {
en: { label: "Show this key its quotas", off: "Quotas", on: "Quotas shown" },
zh: { label: "让这个密钥查看额度", off: "额度", on: "可看额度" },
"zh-TW": { label: "讓這個金鑰查看額度", off: "額度", on: "可看額度" },
ja: { label: "このキーに残量を見せる", off: "残量", on: "残量を表示" },
de: { label: "Diesem Schlüssel seine Kontingente zeigen", off: "Kontingente", on: "Kontingente sichtbar" },
};
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const width of [1100, 420]) {
for (const [lang, w] of Object.entries(words)) {
test(`${engine} ${width} ${lang}: a held key is let see its quotas`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
const page = await (await browser.newContext({ viewport: { width, height: 700 }, reducedMotion: "reduce" })).newPage();
page.setDefaultTimeout(6000);
page.setDefaultNavigationTimeout(60000);
const events = [], errors = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", fixture(lang, "light", events, { lan: true, keyHolds: { server: { models: ["relay/*"], quotas: true }, work: { models: ["relay/*"] } } }));
await page.goto("http://magpie.test/?view=gateway");
await page.locator("#gatewayKeys .acc[data-key]").last().waitFor();
const row = (id) => page.locator(`#gatewayKeys .acc[data-key="${id}"]`);
const badge = (id) => row(id).getByRole("button", { name: w.label, exact: true });
// on: said always, not only on hover
await page.mouse.move(0, 0);
assert.equal((await badge("server").textContent()).trim(), w.on);
assert.equal(await badge("server").getAttribute("aria-pressed"), "true");
assert.equal(await badge("server").evaluate((b) => getComputedStyle(b).opacity), "1");
assert.ok((await badge("server").boundingBox()).width > 0, "an on badge is hidden");
// off: on hover only
assert.equal(await badge("work").getAttribute("aria-pressed"), "false");
assert.equal(await badge("work").evaluate((b) => getComputedStyle(b).opacity), "0");
const view = page.locator("#view-gateway");
const scrolled = async () => [await view.evaluate((v) => v.scrollTop), await page.evaluate(() => window.scrollY)];
const before = await scrolled();
await row("work").hover();
assert.equal((await badge("work").textContent()).trim(), w.off);
await badge("work").click();
await page.waitForFunction(() => document.querySelector('#gatewayKeys .acc[data-key="work"] .key-quotas')?.getAttribute("aria-pressed") === "true");
assert.deepEqual(events.filter((e) => e.action === "quotas-key").map((e) => e.body), [{ key: "work", quotas: true }]);
assert.equal((await badge("work").textContent()).trim(), w.on);
await row("server").hover();
await badge("server").click();
await page.waitForFunction(() => document.querySelector('#gatewayKeys .acc[data-key="server"] .key-quotas')?.getAttribute("aria-pressed") === "false");
assert.deepEqual(events.filter((e) => e.action === "quotas-key").at(-1).body, { key: "server", quotas: false });
assert.deepEqual(await scrolled(), before, "a click moved the page");
// the row still fits: nothing runs past the window's edge
const over = await page.evaluate(() => document.documentElement.scrollWidth - document.documentElement.clientWidth);
assert.ok(over <= 0, `the page scrolls sideways by ${over}px`);
const rowBox = await row("server").boundingBox(), b = await badge("server").boundingBox();
await row("server").hover();
assert.ok(b.x >= rowBox.x && b.x + b.width <= rowBox.x + rowBox.width + 1, "the badge runs out of its row");
assert.equal(await page.locator("select").count(), 0, "no native select");
const lefts = await page.locator("#gatewayKeys .key-quotas").evaluateAll((els) => els.map((e) => getComputedStyle(e).borderLeftStyle));
assert(lefts.every((s) => s === "none"), "a coloured left border");
assert.deepEqual(errors, []);
});
}
}
}