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).
90 lines
5.2 KiB
JavaScript
90 lines
5.2 KiB
JavaScript
// A gateway key's limit lines say what it has used while the Gateway
|
|
// page stays open: Young (Discord, v0.1.18xx) reset his keys' usage and
|
|
// their lines read $0.00 from then on, while Usage showed the same keys
|
|
// spending, because the keys were read only on a visit to the page. Now
|
|
// the window coming back, and a timer while the page is in sight, read
|
|
// them again. Both engines, en and zh.
|
|
const assert = require("node:assert/strict");
|
|
const fs = require("node:fs");
|
|
const path = require("node:path");
|
|
const { test } = require("node:test");
|
|
const { chromium, webkit } = require("playwright");
|
|
const { fixture } = require("./fixtures/caller-keys.cjs");
|
|
|
|
const src = fs.readFileSync(path.resolve(__dirname, "../assets/i18n.js"), "utf8");
|
|
const I18N = new Function(src.slice(0, src.indexOf("\n};") + 3) + "; return I18N;")();
|
|
|
|
const hour = 36e5, day = 864e5;
|
|
// Young's shape: a key held to some models, $ caps over 5 hours and 7 days
|
|
// and a token cap over 30, just reset
|
|
const used = (cost, tokens) => {
|
|
const since = new Date(Date.now() - 60e3).toISOString();
|
|
const u = (period, extra) => ({ period, start: since, calls: cost ? 12 : 0, tokens, cost, tokensLeft: 0, costLeft: 0, spent: false, ...extra });
|
|
return [
|
|
u("hours", { hours: 5, reset: new Date(Date.now() + 5 * hour).toISOString(), costLimit: 30, costLeft: 30 - cost }),
|
|
u("days", { days: 7, reset: new Date(Date.now() + 7 * day).toISOString(), costLimit: 153, costLeft: 153 - cost }),
|
|
u("days", { days: 30, reset: new Date(Date.now() + 30 * day).toISOString(), tokenLimit: 1e13, tokensLeft: 1e13 - tokens }),
|
|
];
|
|
};
|
|
|
|
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
|
|
for (const lang of ["en", "zh"]) {
|
|
test(`${engine} ${lang}: a key's used limits are read again while the page is open`, async (t) => {
|
|
const tr = (k, vars = {}) => {
|
|
const s = lang === "en" ? k : I18N[lang][k];
|
|
assert.equal(typeof s, "string", `${lang} has ${k}`);
|
|
return s.replace(/\{(\w+)\}/g, (_, v) => String(vars[v]));
|
|
};
|
|
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
|
|
t.after(() => browser.close());
|
|
const page = await (await browser.newContext({ viewport: { width: 1100, height: 760 }, reducedMotion: "reduce" })).newPage();
|
|
page.setDefaultTimeout(6000);
|
|
page.setDefaultNavigationTimeout(60000);
|
|
const errors = [];
|
|
page.on("pageerror", (e) => errors.push(e.message));
|
|
const key = {
|
|
id: "pool", name: "拼车-自用", masked: "sk-magpie-key-…6fcf81", models: ["a/m", "group/mine"],
|
|
limit: { period: "hours", hours: 5, cost: 30 },
|
|
moreLimits: [{ period: "days", days: 7, cost: 153 }, { period: "days", days: 30, tokens: 1e13 }],
|
|
};
|
|
const keys = [key, { id: "other", name: "其他模型", masked: "sk-magpie-key-…222222" }];
|
|
const spend = (cost, tokens) => { key.usedAll = used(cost, tokens); key.used = key.usedAll[0]; };
|
|
spend(0, 0);
|
|
await page.clock.install();
|
|
await page.route("**/*", fixture(lang, "light", [], { lan: true, keys }));
|
|
await page.goto("http://magpie.test/?view=gateway");
|
|
const lines = page.locator('#gatewayKeys .acc[data-key="pool"] .key-limit');
|
|
await lines.first().waitFor();
|
|
const zero = tr("{used} of {limit} estimated · {left} left", { used: "$0.00", limit: "$30.00", left: "$30.00" });
|
|
assert((await lines.nth(0).textContent()).includes(zero), await lines.nth(0).textContent());
|
|
|
|
// calls made through the key: the window coming back shows them
|
|
spend(9.93, 63e6);
|
|
await page.evaluate(() => window.dispatchEvent(new Event("focus")));
|
|
const spent = tr("{used} of {limit} estimated · {left} left", { used: "$9.93", limit: "$30.00", left: "$20.07" });
|
|
await page.waitForFunction((s) => document.querySelector('#gatewayKeys .acc[data-key="pool"] .key-limit')?.textContent.includes(s), spent);
|
|
assert((await lines.nth(1).textContent()).includes("$9.93"), await lines.nth(1).textContent());
|
|
|
|
// and, left alone in sight, the timer does
|
|
spend(12.5, 80e6);
|
|
await page.clock.runFor(16e3);
|
|
const later = tr("{used} of {limit} estimated · {left} left", { used: "$12.50", limit: "$30.00", left: "$17.50" });
|
|
await page.waitForFunction((s) => document.querySelector('#gatewayKeys .acc[data-key="pool"] .key-limit')?.textContent.includes(s), later);
|
|
|
|
// an open limit editor is someone typing: never drawn over
|
|
await page.locator('#gatewayKeys .acc[data-key="pool"] .amodels.limit').click();
|
|
const ed = page.locator('#gatewayKeys .acc[data-key="pool"] .key-limit-ed');
|
|
await ed.waitFor();
|
|
const costIn = ed.getByRole("textbox", { name: tr("Estimated cost limit, US$"), exact: true });
|
|
await costIn.fill("31");
|
|
spend(14, 90e6);
|
|
await page.clock.runFor(16e3);
|
|
await page.evaluate(() => window.dispatchEvent(new Event("focus")));
|
|
await page.clock.runFor(2e3);
|
|
assert.equal(await costIn.inputValue(), "31", "the editor kept what was typed");
|
|
assert.equal(await costIn.evaluate((e) => e === document.activeElement), true, "and the field being typed in");
|
|
assert.deepEqual(errors, []);
|
|
});
|
|
}
|
|
}
|