Files
magpie/internal/gui/tests/balance-url-first.test.cjs
T
yetone e2aaf21f73 gui: a custom provider's Balance URL is asked above its balance token
A relay's user wanting their balance opened the editor and found
"Account balance" asking for a new-api System Access Token or a sub2api
JWT and user id, while the setting that worked (Balance URL
https://app.yylx.io/v1/usage, token left empty) sat at the foot of the
form under More endpoints (小鱼 of yylx.io, WeChat, 11-05).

The Balance URL and Balance field now sit right above Account balance in
Advanced (balSlot), and Advanced opens when a Balance URL is saved. The
token's hint says first to leave it empty, that most relays tell a key's
balance at the Balance URL above (sub2api's …/v1/usage, new-api's
…/api/usage/token), and only then what the token is for. zh, zh-TW, ja
and de have it. In Advanced's narrower column the method picker and URL
wrap, so the URL keeps at least 120px at 420px.

Run (mote, Playwright 1.62.0):
- balance-url-first (new), balance-fix, balance-method and the other
  balance-* files: 186/186 in Chromium and WebKit, at 900 and 420px,
  en/zh/zh-TW/ja/de.
- The whole GUI suite, cut short after 733 tests (it ran for hours
  beside other sessions): red only in account-eye chromium en/ja (#5052)
  and account-weight webkit ja 390 (#4644), both open and in files this
  change doesn't reach.
- i18n-keys: "a plugin's words are its own" red, the same on main
  (#5327).

TestCoreScriptBudget: the hint took the core 494 bytes over its budget;
five comments in the provider editor were cut (573 bytes), so it is 79
under, with no raise.
- go test ./internal/provider ./internal/gui ./internal/cli ./internal/tui
  at the rebased head: ok but internal/gui's TestMainWindowCorners on
  mote's macOS 27 (#3949, open) and the budget before the cut.

Broken by hand: balance-url-first on the old app.js fails at "a saved
Balance URL opens Advanced" (chromium).

Filed: #5336, Advanced is 46px too wide at 420px on main (the Rests
tick's label); the new test checks only the balance rows for it.

Not tried: the real app on Linux (WebKitGTK); no motion or new API here.
2026-11-05 14:19:28 +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").getByRole("button", { name: await tr("Cancel"), exact: true }).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();
});
}
}
});
}