Files
magpie/internal/gui/tests/key-own-balance.test.cjs
T
yetone c3a9574de4 gui: a key's row keeps its controls shown and stays put under the pointer
tenacious_eagle_96548 (Discord, #general): in a provider's editor a key's
row showed its controls only under the pointer, so it kept growing and
shrinking, and moving away to fill in something else collapsed it.

Cause: app.css (#841) gave a key's unset controls (No limit at once, No
spend cap, Set balance, Switch off, Remove) no room off the pointer. A
hover made the row wrap from 38px to 59px and pushed the rows below
down. With Set balance open and typed into, moving the pointer to the
Name field collapsed the other pills, and the field moved 158px right
and 25px up under the caret (real magpie web, sandbox HOME on mote).

Fix: the list is drawn only in the provider's editor, where every key is
there to be set, so a key's row now shows every control, laid out the
same under the pointer as off it, as a phone's already did. The name and
the masked key stay whole; what doesn't fit beside them goes under the
row's first line. The "held" class that kept a clicked row open is gone
with what it worked around. Subscription accounts' rows (opacity only,
no shift), grips and the ⋯ menu are unchanged.

New key-row-steady.test.cjs: chromium and webkit, en/zh/zh-TW/ja/de, app
560/1000px and tab 420/1000px. Every control is shown off the pointer,
nothing in any row moves with the pointer over any row, and a balance
being typed keeps its place, caret and "100" when the pointer goes to the
Name field. key-row-chips-fit's "one line when not hovered" became "laid
out the same hovered or not"; stale hover comments updated in
key-own-balance, key-spend-cap, key-switch, key-gemini and key-plan.

Run (mote, through mote-run, at this head on origin/main c736906b1):
- go test ./internal/gui -run Budget: ok. gofmt -l internal/gui: clean.
- GUI key-*.test.cjs (44 files, both engines): 457 pass, 5 red:
  key-replace webkit 420px "Change key puts the new value" in all five
  languages, red the same on origin/main c736906b1 (#4126, ebody holds).
- key-row-steady: 40/40.
- Real app: rows 58/59/59px with the pointer off and over each row; the
  balance field's rect unchanged and focused after the pointer left.

Broken by hand: with origin/main's app.css and app.js, key-row-steady and
key-row-chips-fit failed 80 of 100 ("Set balance is hidden off the
pointer", "no proto shown"); with those visibility checks taken out,
"the pointer over k1 moved k1's row" 15 of 15. The 420px tab passed
either way (a phone already showed everything). A first cut let the
masked key shrink: key-weight-fit at 900px went red ("its masked key is
cut"), fixed here by keeping it whole.

Not run at push: the full Go suite and the full GUI suite (queued on
mote behind other sessions' suites under suite.lock; reds go to issues).
Not tried: Linux WebKitGTK and Windows WebView2 apps (CSS only, no new
API or motion).

Sibling left alone: a gateway key's unset quotas badge takes room only
under the pointer on the Gateway page (#5676).

Reported-by: tenacious_eagle_96548
2026-11-08 14:11:10 +08:00

306 lines
18 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// A key's balance typed by hand (tenacious_eagle on Discord: a provider
// with no balance URL). Where the vendor tells no balance, each key's row
// has a pill: what is left of the balance typed, marked as an estimate
// (≈), else Set balance (always shown, as the row's other unset pills).
// Set balance opens the amount in place; a balance set opens the app's
// menu (no native select): Enter balance…, a refill each month (a day)
// or each week (a weekday), Never refills, or No balance, posted to
// provider/keyownbalance and provider/keybalancerefill. An amount in yuan
// ("¥100", or 100 with ¥ CNY picked beside the field) is posted as CNY
// and shown in yuan; a balance's currency is picked there, the number kept. An amount that isn't one is
// refused and nothing is posted. A click moves nothing; at a phone's width the row stays
// inside the window. In every language.
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 fresh = () => ({
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" },
takesOwnBalance: true,
keyList: [
{ id: "k1", name: "Main", masked: "sk-…one", on: true, balance: { set: 20, at: new Date(Date.now() - 864e5).toISOString(), spent: 7.5, left: 12.5 } },
{ id: "k2", name: "Team", masked: "sk-…two", on: true },
],
balanceToken: { takes: false, set: false }, proxy: "",
});
function serve(lang, posts) {
const relay = fresh();
const providers = { providers: [relay], presets: [], excluded: [], gateway: { running: true, window: true } };
const state = { agents: [], profiles: [], settings: { lang, theme: "light" } };
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 = {lang:"${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(state);
if (url.pathname === "/api/providers") return json(providers);
if (url.pathname === "/api/lanes") return json({});
if (url.pathname === "/api/groups") return json({ groups: [] });
if (url.pathname === "/api/plugins") return json({ plugins: [] });
if (url.pathname.startsWith("/api/provider/") && route.request().method() === "POST") {
const action = url.pathname.slice("/api/provider/".length), body = route.request().postDataJSON();
posts.push({ action, body });
if (action === "keyownbalance") {
const k = relay.keyList.find((x) => x.id === body.account);
if (body.balance != null) k.balance = { set: body.balance, at: new Date().toISOString(), spent: 0, left: body.balance, ...body.currency && { currency: body.currency }, ...k.balance?.refill && { refill: k.balance.refill, next: k.balance.next } };
else delete k.balance;
}
if (action === "keybalancerefill") {
const k = relay.keyList.find((x) => x.id === body.account);
if (body.refill) Object.assign(k.balance, { refill: body.refill, next: new Date(Date.now() + 5 * 864e5).toISOString() });
else delete k.balance.refill, delete k.balance.next;
}
return json(providers);
}
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 });
};
}
const words = {
en: { currency: "Currency", yuan40: "≈¥40.00 left", usd100: "≈$100.00 left", k1: "≈$12.50 left", none: "Set balance", menu: "Balance", enter: "Enter balance…", off: "No balance", k2: "≈$5.00 left", k1again: "≈$40.00 left", yuan: "≈¥100.00 left", bad: "A balance is an amount from 0 to 1,000,000, in USD or in yuan (¥)", month: "Refills each month…", week: "Refills each week…", never: "Never refills", on: "Refills on", refills: "Refills to $40.00 on" },
zh: { currency: "货币", yuan40: "≈¥40.00 剩余", usd100: "≈$100.00 剩余", k1: "≈$12.50 剩余", none: "设置余额", menu: "余额", enter: "填写余额…", off: "无余额", k2: "≈$5.00 剩余", k1again: "≈$40.00 剩余", yuan: "≈¥100.00 剩余", bad: "余额是 0 到 1,000,000 之间的金额,美元或人民币(¥)", month: "每月恢复…", week: "每周恢复…", never: "不恢复", on: "恢复于", refills: "恢复为 $40.00" },
"zh-TW": { currency: "貨幣", yuan40: "≈¥40.00 剩餘", usd100: "≈$100.00 剩餘", k1: "≈$12.50 剩餘", none: "設定餘額", menu: "餘額", enter: "填寫餘額…", off: "無餘額", k2: "≈$5.00 剩餘", k1again: "≈$40.00 剩餘", yuan: "≈¥100.00 剩餘", bad: "餘額是 0 到 1,000,000 之間的金額,美元或人民幣(¥)", month: "每月恢復…", week: "每週恢復…", never: "不恢復", on: "恢復於", refills: "恢復為 $40.00" },
ja: { currency: "通貨", yuan40: "残り ≈¥40.00", usd100: "残り ≈$100.00", k1: "残り ≈$12.50", none: "残高を設定", menu: "残高", enter: "残高を入力…", off: "残高なし", k2: "残り ≈$5.00", k1again: "残り ≈$40.00", yuan: "残り ≈¥100.00", bad: "残高は 0 から 1,000,000 までの金額です(USD または人民元 ¥)", month: "毎月戻す…", week: "毎週戻す…", never: "戻さない", on: "戻す曜日", refills: "$40.00 へ戻る" },
de: { currency: "Währung", yuan40: "≈¥40.00 übrig", usd100: "≈$100.00 übrig", k1: "≈$12.50 übrig", none: "Guthaben festlegen", menu: "Guthaben", enter: "Guthaben eingeben…", off: "Kein Guthaben", k2: "≈$5.00 übrig", k1again: "≈$40.00 übrig", yuan: "≈¥100.00 übrig", bad: "Ein Guthaben ist ein Betrag von 0 bis 1.000.000, in USD oder in Yuan (¥)", month: "Monatlich auffüllen…", week: "Wöchentlich auffüllen…", never: "Nie auffüllen", on: "Auffüllen am", refills: "wieder auf $40.00 aufgefüllt" },
};
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of Object.keys(words)) {
for (const width of [900, 400]) {
const w = words[lang];
test(`${engine} ${lang} ${width}px: a key's balance typed by hand shows what is left as an estimate, and is typed again or taken off`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
let page;
t.after(async () => {
if (process.env.ARTIFACT_DIR && page) {
await fs.mkdir(process.env.ARTIFACT_DIR, { recursive: true });
await page.screenshot({ path: path.join(process.env.ARTIFACT_DIR, `${engine}-${lang}-${width}-key-own-balance.png`) });
}
await browser.close();
});
page = await (await browser.newContext({ viewport: { width, height: 760 }, reducedMotion: "reduce" })).newPage();
page.setDefaultTimeout(5000);
page.setDefaultNavigationTimeout(60000);
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
const posts = [];
await page.route("**/*", serve(lang, posts));
await page.goto("http://magpie.test/?view=providers");
await page.locator(".row.provider", { hasText: "Relay" }).first().click();
await page.locator(".editor .accts .acc .abal").first().waitFor({ state: "attached" });
await page.waitForTimeout(300);
const row = (id) => page.locator(`.editor .accts .acc[data-account-id="${id}"]`);
const pill = (id) => row(id).locator(".abal");
const box = (id) => row(id).locator("input.abal-in");
const cur = (id) => row(id).locator(".abal-cur");
const menu = page.locator(".proto-menu");
const item = (name) => menu.locator(".pm-item", { has: page.locator(".pm-name", { hasText: new RegExp(`^${name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}$`) }) });
const scrolled = () => page.evaluate(() => [window.scrollY, ...[...document.querySelectorAll("*")].filter((e) => e.scrollTop > 0).map((e) => `${e.className}:${e.scrollTop}`)].join(" "));
const still = async (id) => {
await row(id).hover();
await page.waitForTimeout(150);
const before = await row(id).evaluate((e) => Math.round(e.getBoundingClientRect().top)), sc = await scrolled();
await pill(id).click();
await page.waitForTimeout(200);
assert.equal(await row(id).evaluate((e) => Math.round(e.getBoundingClientRect().top)), before, `${id}'s row moved`);
assert.equal(await scrolled(), sc, "a click scrolled");
};
const posted = async (n) => {
for (let i = 0; i < 60 && posts.length === n; i++) await new Promise((r) => setTimeout(r, 50));
assert(posts.length > n, "nothing posted");
return posts.at(-1);
};
assert.equal(await pill("k1").textContent(), w.k1);
assert.equal(await pill("k2").textContent(), w.none);
assert.equal(await page.locator("select").count(), 0, "no native select");
// a balance typed is shown off the pointer, and so is none (#841)
await page.mouse.move(0, 0);
await page.waitForTimeout(250);
assert.equal(await pill("k1").evaluate((e) => getComputedStyle(e).opacity), "1");
const over = await page.evaluate(() => [...document.querySelectorAll(".editor .accts .acc")].filter((e) => e.getBoundingClientRect().right > innerWidth + 1).length);
assert.equal(over, 0, "a key's row past the window's edge");
assert.equal(await page.evaluate(() => document.scrollingElement.scrollWidth <= innerWidth + 1), true, "the page scrolls sideways");
// k2: none typed, so the amount opens in place; a bad one is refused
await still("k2");
await box("k2").waitFor();
let n = posts.length;
await box("k2").fill("lots");
await box("k2").press("Enter");
await page.waitForTimeout(300);
assert.equal(posts.length, n, "a bad amount was posted");
assert.equal(await page.locator("#status").textContent(), w.bad);
// the Enter that was refused can press the pill drawn in the
// field's place, which opens the field again: type in it, else
// press the pill
await page.waitForTimeout(300);
if (!(await box("k2").isVisible())) await still("k2");
await box("k2").waitFor();
await box("k2").fill("$5");
await box("k2").press("Enter");
let p = await posted(n);
assert.equal(p.action, "keyownbalance");
assert.deepEqual(p.body, { id: "relay", account: "k2", balance: 5 });
await page.waitForTimeout(250);
assert.equal(await pill("k2").textContent(), w.k2);
// k1: typed again from the menu, then taken off
await still("k1");
await menu.waitFor();
assert.equal(await menu.locator(".pm-head").textContent(), w.menu);
await item(w.enter).click();
await box("k1").waitFor();
assert.equal(await box("k1").inputValue(), "20");
n = posts.length;
await box("k1").fill("40");
await box("k1").press("Enter");
p = await posted(n);
assert.deepEqual(p.body, { id: "relay", account: "k1", balance: 40 });
await page.waitForTimeout(250);
assert.equal(await pill("k1").textContent(), w.k1again);
// k1 refills on a day of each month, then each Friday, then never
await still("k1");
await menu.waitFor();
await item(w.month).click();
await box("k1").waitFor();
n = posts.length;
await box("k1").fill("40");
await box("k1").press("Enter");
await page.waitForTimeout(300);
assert.equal(posts.length, n, "day 40 was posted");
// the refused Enter can press the pill, which opens its menu
await page.waitForTimeout(300);
if (!(await box("k1").isVisible())) {
if (!(await menu.isVisible())) await still("k1");
await item(w.month).click();
}
await box("k1").waitFor();
await box("k1").fill("1");
await box("k1").press("Enter");
p = await posted(n);
assert.equal(p.action, "keybalancerefill");
assert.deepEqual(p.body, { id: "relay", account: "k1", refill: { every: "month", day: 1 } });
await page.waitForTimeout(250);
assert.match(await pill("k1").getAttribute("title"), new RegExp(w.refills.replace(/[$.]/g, "\\$&")));
await still("k1");
await menu.waitFor();
await item(w.week).click();
await page.waitForTimeout(200);
await menu.waitFor();
assert.equal(await menu.locator(".pm-head").textContent(), w.on);
assert.equal(await menu.locator(".pm-item").count(), 7);
const friday = await page.evaluate(() => new Date(2026, 10, 6).toLocaleDateString(intlLang(), { weekday: "long" }));
n = posts.length;
await item(friday).click();
p = await posted(n);
assert.deepEqual(p.body, { id: "relay", account: "k1", refill: { every: "week", day: 5 } });
await page.waitForTimeout(250);
await still("k1");
await menu.waitFor();
n = posts.length;
await item(w.never).click();
p = await posted(n);
assert.deepEqual(p.body, { id: "relay", account: "k1", refill: null });
await page.waitForTimeout(250);
assert.doesNotMatch(await pill("k1").getAttribute("title"), new RegExp(w.refills.replace(/[$.]/g, "\\$&")));
// k2 typed again in yuan
await still("k2");
await menu.waitFor();
await item(w.enter).click();
await box("k2").waitFor();
n = posts.length;
await box("k2").fill("¥100");
// the sign typed says yuan, and the currency beside it follows
assert.equal((await cur("k2").textContent()).trim(), "CNY");
await box("k2").press("Enter");
p = await posted(n);
assert.deepEqual(p.body, { id: "relay", account: "k2", balance: 100, currency: "cny" });
await page.waitForTimeout(250);
assert.equal(await pill("k2").textContent(), w.yuan);
// k1's $40 was meant in yuan (tenacious_eagle): the currency is
// picked beside the field, the number kept as typed, and nothing
// is posted before Enter
await still("k1");
await menu.waitFor();
await item(w.enter).click();
await box("k1").waitFor();
assert.equal(await box("k1").inputValue(), "40");
assert.equal((await cur("k1").textContent()).trim(), "USD");
n = posts.length;
await cur("k1").click();
await menu.waitFor();
assert.equal(await menu.locator(".pm-head").textContent(), w.currency);
assert.equal(await menu.locator(".pm-item.on .pm-name").textContent(), "$ USD");
await item("¥ CNY").click();
await page.waitForTimeout(200);
assert.equal(posts.length, n, "picking the currency posted");
assert.equal(await box("k1").inputValue(), "40");
assert.equal((await cur("k1").textContent()).trim(), "CNY");
assert.equal(await box("k1").evaluate((e) => e === document.activeElement), true, "the field lost its focus");
assert.equal(await box("k1").getAttribute("aria-label"), await page.evaluate(() => t("Balance, in CNY")));
assert.equal(await page.evaluate(() => [...document.querySelectorAll(".editor .accts .acc")].filter((e) => e.getBoundingClientRect().right > innerWidth + 1).length), 0, "the field's row past the window's edge");
await box("k1").press("Enter");
p = await posted(n);
assert.deepEqual(p.body, { id: "relay", account: "k1", balance: 40, currency: "cny" });
await page.waitForTimeout(250);
assert.equal(await pill("k1").textContent(), w.yuan40);
// k2's ¥100 back to dollars by the keyboard, saved as it is left;
// Escape in the menu picks nothing and posts nothing
await still("k2");
await menu.waitFor();
await item(w.enter).click();
await box("k2").waitFor();
assert.equal(await box("k2").inputValue(), "100");
assert.equal((await cur("k2").textContent()).trim(), "CNY");
n = posts.length;
await cur("k2").click();
await menu.waitFor();
await page.keyboard.press("Escape");
await page.waitForTimeout(200);
assert.equal(await menu.count(), 0);
assert.equal(posts.length, n, "Escape in the currency's menu posted");
assert.equal((await cur("k2").textContent()).trim(), "CNY");
await cur("k2").click();
await menu.waitFor();
await item("$ USD").click();
await page.waitForTimeout(200);
assert.equal(posts.length, n, "picking the currency posted");
await page.keyboard.press("Tab");
await page.keyboard.press("Tab");
p = await posted(n);
assert.deepEqual(p.body, { id: "relay", account: "k2", balance: 100 });
await page.waitForTimeout(250);
assert.equal(await pill("k2").textContent(), w.usd100);
await still("k1");
await menu.waitFor();
n = posts.length;
await item(w.off).click();
p = await posted(n);
assert.deepEqual(p.body, { id: "relay", account: "k1", balance: null });
await page.waitForTimeout(250);
assert.equal(await pill("k1").textContent(), w.none);
const stripes = await page.evaluate(() => [...document.querySelectorAll(".abal, .abal-in")].map((e) => getComputedStyle(e)).filter((s) => parseFloat(s.borderLeftWidth) > parseFloat(s.borderRightWidth) || (parseFloat(s.borderLeftWidth) > 0 && s.borderLeftColor !== s.borderRightColor)).length);
assert.equal(stripes, 0, "no border stripes");
assert.deepEqual(errors, []);
});
}
}
}