Files
magpie/internal/gui/tests/balance-url-first.test.cjs
T
yetone b53e23ba3e gui: a provider's editor says Close while nothing waits for its Save
The provider editor mixes two kinds of control. An account's auto-switch,
a key's concurrency and spend pills and the Claude Code rows act at once
and say so in the status pill, while the rest waits for Save. Its Cancel
said Cancel either way, so after a switch that had already been made, it
read as if Cancel would take it back. The button now says Close while
the draft matches what is saved (providerDirty, the same check Cancel's
discard question asks), and Cancel once a field waits for Save. It is
read again after each input, change and click in the editor. A new
provider's editor keeps Cancel. Escape and the backdrop are unchanged.

Tests: six files that pressed the editor's Cancel on a clean editor press
its .ed-cancel now. lane-limit's draft-pill test brings the pill into
sight before opening its menu: under four files at once a scroll landing
after the click closed it (Chromium, 2 of 4).

Run (mote, at this tree):
- lane-limit.test.cjs: 24 pass.
- the 29 GUI files that press an editor's Cancel, Chromium and WebKit, four
  at a time: 491 pass, 13 red. 12 are red on 68dd531c6 the same way
  ("Working only unpicks", "Try again takes the models unticked", #4392,
  #4410). The other is login-import, Chromium en, once per run under the
  four-way run, at a different line each time. It passed 3 of 3 alone, and
  at 6x and 12x CPU throttling, here and on 68dd531c6. Already filed under
  load: #5035, #5597.
- go test ./internal/gui -run 'TestCoreScriptBudget|TestI18n|TestCSS': ok.

Broken by hand: lane-limit against the old app.js, 8 of 24 red (no
Close, in the Relay and the Codex editor, both engines, en and zh).

Not tried: the real app window, and the whole GUI suite.
2026-11-18 13:35:20 +08:00

106 lines
7.1 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// A relay's user who wanted its balance found the account token's field
// first, asking for a new-api System Access Token or a sub2api JWT, while
// the Balance URL that reads it with the key alone sat at the foot of the
// form under More endpoints (a relay's report, 11-05). A custom provider's
// Balance URL and field now stand in Advanced right above the token, whose
// hint says to leave it empty for the key's own balance; a saved Balance
// URL opens Advanced.
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 relay = {
id: "relay", name: "Relay", icon: "generic", chat: "https://relay.example.com/v1", responses: "", anthropic: "", catalog: "",
models: [{ id: "model-a", name: "Model A", on: true }], agents: [], fallback: [], headers: {},
key: { set: true, masked: "sk-…one" }, keyList: [],
balanceURL: "https://relay.example.com/v1/usage", balancePath: "",
balanceToken: { takes: true, set: false },
};
const plain = { ...relay, id: "plain", name: "Plain", balanceURL: "" };
function server(lang, list) {
const providers = { providers: list, presets: [], excluded: [], gateway: { running: true, window: true } };
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 = ${JSON.stringify({ 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({ agents: [], profiles: [], settings: { lang, theme: "light" } });
if (url.pathname === "/api/providers") return json(providers);
if (url.pathname === "/api/groups") return json({ groups: [] });
if (url.pathname === "/api/provider/balance") return json({ ok: true, amount: "$10472.14" });
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 });
};
}
// the order the rows of Advanced come in, by their labels
const order = (page, labels) => page.locator(".editor details.advanced").evaluate((adv, labels) => {
const all = [...adv.querySelectorAll("*")].filter((e) => !e.children.length && labels.includes(e.textContent.trim()));
return labels.map((l) => all.findIndex((e) => e.textContent.trim() === l));
}, labels);
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
test(engine + ": a custom provider's Balance URL comes before the account token", async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
for (const width of [900, 420]) {
for (const lang of ["en", "zh", "zh-TW", "ja", "de"]) {
await t.test(`${width}px ${lang}`, async () => {
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) => errors.push(e.message));
await page.route("**/*", server(lang, [relay, plain]));
await page.goto("http://magpie.test/?view=providers");
const tr = (k) => page.evaluate((k) => t(k), k);
const labels = [await tr("Balance URL"), await tr("Balance field"), await tr("Account balance")];
const hint = await tr("Leave it empty to read the key's own balance: most relays tell it at the Balance URL above (a sub2api relay's …/v1/usage, a new-api relay's …/api/usage/token). Only for the whole account's balance: a new-api relay's System Access Token, or a sub2api panel's login token (a JWT, sent as a Bearer); it is used for nothing else.");
if (lang !== "en") assert(!hint.startsWith("Leave it empty"), "the hint is translated");
// the reporter's case: a saved relay with its /v1/usage
await page.locator(".row.provider", { hasText: "Relay" }).click();
assert(await page.locator(".editor details.advanced").evaluate((d) => d.open), "a saved Balance URL opens Advanced");
assert.equal(await page.locator(".editor details.advanced .bal-url").count(), 1, "the Balance URL is in Advanced");
assert.equal(await page.locator(".editor details.more:not(.advanced) .bal-url").count(), 0, "not under More endpoints");
const [u, f, a] = await order(page, labels);
assert(u >= 0 && f > u && a > f, `Balance URL, then its field, then the token: ${[u, f, a]}`);
assert.equal(await page.locator(".editor details.advanced").getByText(hint, { exact: true }).count(), 1, "the token's hint says to leave it empty");
await page.locator(".editor .bal-url").scrollIntoViewIfNeeded();
assert(await page.locator(".editor .bal-url").isVisible());
await page.locator(".editor").getByRole("button", { name: await tr("Check balance"), exact: true }).click();
await page.locator(".editor .bal-res.ok").waitFor();
// the balance rows fit at a narrow width, the URL wide enough to read
const fit = await page.locator(".editor details.advanced").evaluate((adv) => {
const a = adv.getBoundingClientRect();
const rows = [adv.querySelector(".bal-ask"), adv.querySelector(".bal-path"), adv.querySelector(".bal-fix")];
return { inside: rows.every((e) => { const r = e.getBoundingClientRect(); return r.right <= a.right + 1; }), url: adv.querySelector(".bal-url").getBoundingClientRect().width >= 120 };
});
assert.deepEqual(fit, { inside: true, url: true });
// one with no Balance URL keeps Advanced shut, as before
await page.locator(".editor .bar .ed-cancel").click();
await page.locator(".row.provider", { hasText: "Plain" }).click();
assert.equal(await page.locator(".editor details.advanced").evaluate((d) => d.open), false, "nothing set, nothing opened");
// a new custom provider: the same order
await page.evaluate(() => { adding = true; editing = { custom: true }; draft = null; renderProviders(); });
await page.locator(".editor details.advanced > summary").click();
const [nu, nf, na] = await order(page, labels);
assert(nu >= 0 && nf > nu && na > nf, `new: ${[nu, nf, na]}`);
assert.deepEqual(errors, []);
await context.close();
});
}
}
});
}