Files
magpie/internal/gui/tests/usage-card-refresh.test.cjs
T
yetoneandchiperman 85eef5d87a gui: quota refreshes stay centered and finish each turn without jumping (#2397)
chiperman's patch (df288db, #2397), landed here since external PRs are
collaborators only. The card redraw replaced its refresh button on a
background reading and again when the click finished, restarting its
rotation and hover reveal and moving the Updated text; a fast reply also
stopped the spin partway through a turn. Each account's control and its
animation are kept through redraws, the reply is shown at once and the
current 800ms turn finishes (at least one), Reduce Motion skips it, and the
glyph sits on the text baseline in an inline grid instead of a pixel offset.

Changed in landing: main had since given the tray panel's Allowances the
same quotaRefresh (#panelQuota, #840). A kept button is reused only when its
redraw took it out of the page, so a refresh started on the Usage page is
never moved out of the panel (or back); the other place's copy of a turning
control gets busy+spun and turns by itself until done() clears it. The
panel's hover rule (.pq-card:hover) reveals it as the Usage card's does.
quota-refresh-order's vm harness gets a document stub for that.

app.js's budget goes from 1,250,000 to 1,269,000: with four comments
shortened it is 1,250,904 here, the core's own code since the tray panel
draws the same control, plus 1.5% as the budget's comment asks. Every core
script together is still under coreScriptBudget, which is unchanged.

Run, sandbox HOME, pinned GOMODCACHE/GOCACHE, load ~110-360 from other
sessions' suites:
- Full suite on 81313b912 + this change: set -o pipefail; go test
  -timeout 45m ./...: 64 packages ok; gateway and gui red
  in 3 tests:
  - TestCoreScriptBudget (app.js 47 bytes over; this change, fixed above)
  - TestPluginModelServiceTiersListed: ENOSPC, disk full; -count=3 ok
  - TestMainWindowCorners: killed at its deadline, red alone on main too
    (#1936)
- At this head:
  - go test ./internal/gui/: only TestMainWindowCorners red (#1936)
  - TestCoreScriptBudget: ok
  - gofmt -l: clean
  - go vet -tags nogui for darwin, linux and windows: ok (./... on
    81313b912 + this change; ./internal/gui/... at the rebase)
  - node --check app.js: clean
- GUI, Chromium and WebKit, at e085d6e92 + this change: usage-card-refresh,
  quota-refresh-order, panel-quota-refresh, gui-zh-tw, gui-ja, gui-de:
  84 pass, 0 fail. At this head usage-card-refresh, quota-refresh-order
  and panel-quota-refresh: 75 pass, 0 fail.
- Broken by hand: the new tests against the old app.js/app.css fail 26 of
  46 in usage-card-refresh ("a background reading replaced the spinning
  control", "the refresh icon and the first text line must share a
  vertical center": 253.875 vs 252.375).
- Whole GUI suite (789 files) on 81313b912 + this change, per file, 3 at a
  time. Every red file was rerun alone and checked against plain main
  where it stayed red. None is from this change:
  - Red on main as well, already filed: codex-predictions (#2624),
    lightweight (#2638), segs-glide (#2683), sidebar (#2684),
    login-import, routing-keys-fold, routing-bodies (#2658),
    usage-agents-hide (#2276), quota-count-wrap, quota-name-translated
    (#2228, #2399), routing-model-rest (#2115), rest-try-now (#2564),
    routing-request-metrics (#2571), s3-sync (#2448), usage-checkin
    (#2549), usage-refresh (#2184), quota-curve (load).
  - Load reds filed now, red on main too: tray-cell-click (#2686),
    usage-ledger-abort (#2700), sessions-paging (#2703).
  - The rest were launch or goto timeouts in the parallel run and pass
    alone (about 30 files, among them provider-max-output and
    plugin-agent).

Co-authored-by: chiperman <chipermanju@gmail.com>
2026-10-17 17:26:58 +08:00

340 lines
20 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// A card's own refresh (Hu9956, #840: 在更新时间左侧加一个刷新按钮,鼠标悬停
// 在套餐卡片上时显示,可以单独刷新某个套餐的用量): left of "Updated …", or of
// why a reading failed, or under a balance, opening as the card is hovered;
// at rest the line starts where the card's others do. A click reads that card alone
// (POST /api/usage/quotas/refresh?provider=&user=), turns while it does, and
// draws what came back; no other card is asked, and the page doesn't scroll.
// Button controls in English and Chinese; refresh ordering, deferred
// readings and icon alignment in all five languages, wide and narrow.
// No backend: the API is faked here.
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 { inView } = require("./reader.cjs");
const assets = process.env.MAGPIE_QUOTA_ASSETS || path.resolve(__dirname, "../assets");
const H = 3600e3, M = 60e3;
const iso = (ms) => new Date(ms).toISOString();
function fixtures(now) {
const r5 = now + 2 * H;
return [
{ provider: "claude", name: "Claude Code", icon: "claude-color", plan: "Max", user: "a@x.com", readAt: iso(now - 40 * M),
windows: [{ name: "5 hours", used: 28, resetsAt: iso(r5) }] },
{ provider: "codex", name: "Codex", icon: "openai", plan: "Plus", user: "b@x.com", readAt: iso(now - 40 * M),
windows: [{ name: "5 hours", used: 50, resetsAt: iso(r5) }] },
{ provider: "kimi", name: "Kimi", icon: "kimi-color", error: "HTTP 503", windows: [] },
{ provider: "relay", name: "Relay", icon: "openai", balance: "$2.00", readAt: iso(now - 40 * M), windows: [] },
];
}
function serve(lang, state) {
const settings = { theme: "light", lang, tray: "panel", quotaLeft: false, currency: "usd" };
return async (route) => {
const url = new URL(route.request().url());
const json = (body) => route.fulfill({ json: body });
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({ agents: [], profiles: [], settings });
if (url.pathname === "/api/settings") return json(settings);
if (url.pathname === "/api/providers") return json({ providers: state.providers || [], presets: [], excluded: [], gateway: { running: true, window: true } });
if (url.pathname === "/api/usage/quotas") {
state.gets++;
const snapshot = JSON.parse(JSON.stringify(state.quotas));
if (state.readGate) { state.readStarted(); await state.readGate; }
return json(snapshot);
}
if (url.pathname === "/api/usage/quotas/refresh") {
assert.equal(route.request().method(), "POST");
state.refreshes.push(url.search);
await state.gate;
const q = state.quotas.find((x) => x.provider === url.searchParams.get("provider") && (x.user || "") === (url.searchParams.get("user") || ""));
if (q?.windows?.length) { q.windows[0].used = 61; q.readAt = iso(Date.now()); }
if (q?.error) { delete q.error; q.windows = [{ name: "Week", used: 10 }]; q.readAt = iso(Date.now()); }
if (q) { delete q.asOf; delete q.readDeferred; }
if (q && state.nameOnRefresh && !q.user) q.user = "a@x.com";
return json(state.quotas);
}
if (url.pathname === "/api/usage/quotas/history") return json([]);
if (url.pathname === "/api/usage") return json({ calls: 0, cost: 0, days: [], agents: [], models: [], keys: [] });
if (url.pathname === "/api/groups") return json({ groups: [], models: [] });
if (url.pathname === "/api/plugins") return json({ plugins: [] });
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)];
try { await route.fulfill({ body: await fs.readFile(file), contentType }); } catch { await route.fulfill({ status: 404, body: "" }); }
};
}
const deferredWords = {
en: "not refreshed this time",
zh: "本次未主动刷新",
"zh-TW": "本次未主動重新整理",
ja: "今回は更新を要求していません",
de: "diesmal nicht neu abgefragt",
};
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of Object.keys(deferredWords)) {
for (const named of [true, false]) test(`${engine} ${lang}: a deferred ${named ? "named" : "unnamed"} card is not a failure, and a late background reply cannot undo its refresh`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
const page = await browser.newPage({ viewport: { width: 900, height: 700 }, reducedMotion: "reduce" });
page.setDefaultTimeout(5000);
const errors = [];
page.on("pageerror", e => errors.push(e.message));
const state = { quotas: fixtures(Date.now()), gets: 0, refreshes: [], nameOnRefresh: !named };
if (!named) state.quotas[0].user = "";
state.quotas[0].asOf = state.quotas[0].readAt;
state.quotas[0].readDeferred = true;
await page.route("**/*", serve(lang, state));
await page.goto("http://magpie.test/?view=usage");
const claude = page.locator(".subscription-card", { hasText: "Claude Code" });
const read = claude.locator(".quota-read");
await read.waitFor();
assert.ok((await read.textContent()).includes(deferredWords[lang]), "a request not made was described as failed");
let release, readStarted;
const started = new Promise(r => { readStarted = r; });
state.readStarted = readStarted;
state.readGate = new Promise(r => { release = r; });
t.after(() => release());
// Hold an old whole-page response, then click the real card button.
await page.evaluate(() => { void loadQuotas(); });
await started;
await claude.hover();
await claude.locator(".quota-refresh").click();
await claude.locator(".quota-n", { hasText: "61%" }).waitFor();
release();
await page.evaluate(() => quotasLoading);
assert.match(await claude.locator(".quota-n").textContent(), /61%/, "an older reply undid the click");
assert.equal(await read.evaluate(e => e.classList.contains("stale")), false);
assert.deepEqual(state.refreshes, [named ? "?provider=claude&user=a%40x.com" : "?provider=claude"]);
assert.deepEqual(errors, []);
});
}
}
const words = {
en: { updated: "Updated 40 minutes ago", label: "Refresh this card", now: /^Updated (just now|\d+ seconds? ago)$/ },
zh: { updated: "40分钟前更新", label: "刷新这张卡片", now: /^(刚刚|\d+秒前)更新$/ },
};
function layoutFixtures(now) {
const quotas = fixtures(now);
quotas[0].asOf = quotas[0].readAt;
quotas[0].windows[0].resetsAt = iso(now - H);
quotas[1].readAt = iso(now);
// The provider's real missing-access-key reply, including its settings action.
quotas[2] = { provider: "volcengine", name: "Volcengine Ark", icon: "volcengine", plan: "Coding Plan", windows: [], needsAccessKey: true,
error: "this plan's usage is read with the account's access key: give its AccessKey ID and Secret in Volcengine Ark's settings" };
return { quotas, gets: 0, refreshes: [], providers: [
{ id: "volcengine", name: "Volcengine Ark", icon: "volcengine", preset: "volcengine", chat: "https://ark.cn-beijing.volces.com/api/coding/v3",
responses: "", anthropic: "", catalog: "", models: [], agents: [], fallback: [], headers: {}, key: { set: true, masked: "test-key" }, keyList: [],
accessKey: { vendor: "volcengine", id: "", secretSet: false } },
] };
}
async function showRefresh(row) {
await row.hover();
await row.locator(".quota-refresh").evaluate(e => new Promise(done => {
const tick = () => getComputedStyle(e).opacity === "1" && e.getBoundingClientRect().width >= 14 ? done() : requestAnimationFrame(tick);
tick();
}));
}
async function assertRefreshAligned(row) {
const boxes = await row.evaluate((e) => {
const button = e.querySelector(".quota-refresh");
const text = e.querySelector(".quota-ago")?.firstChild || [...e.childNodes].find(n => n.nodeType === Node.TEXT_NODE && n.textContent.trim());
const range = document.createRange();
range.selectNodeContents(text);
const rect = (r) => ({ x: r.x, right: r.right, cy: r.y + r.height / 2 });
return { icon: rect(button.querySelector("svg").getBoundingClientRect()), text: rect(range.getClientRects()[0]) };
});
assert.ok(boxes.icon.right <= boxes.text.x, "the refresh icon stays left of the first line");
assert.ok(Math.abs(boxes.icon.cy - boxes.text.cy) <= 0.75,
"the refresh icon and the first text line must share a vertical center: " + JSON.stringify(boxes));
}
async function assertInlineSettings(row, multiline) {
const layout = await row.evaluate(e => {
const range = document.createRange();
range.selectNodeContents([...e.childNodes].find(n => n.nodeType === Node.TEXT_NODE && n.textContent.trim()));
const rect = r => ({ x: r.x, y: r.y, right: r.right, bottom: r.bottom, width: r.width, height: r.height });
return { row: rect(e.getBoundingClientRect()), text: [...range.getClientRects()].map(rect), settings: rect(e.querySelector(".quota-fix").getBoundingClientRect()) };
});
if (multiline) assert.ok(layout.text.length > 1, "the narrow-window fixture really wraps");
for (const line of [...layout.text, layout.settings]) {
assert.ok(line.x >= layout.row.x - 0.75 && line.right <= layout.row.right + 0.75, "the error and settings fit the card: " + JSON.stringify(layout));
}
for (const line of layout.text.slice(1)) assert.ok(Math.abs(line.x - layout.row.x) < 0.75, "wrapped text uses the full row, not an indented flex item");
const last = layout.text.at(-1), go = layout.settings;
if (last.right + go.width + 5 < layout.row.right) {
assert.ok(go.y < last.bottom && go.bottom > last.y && go.x >= last.right,
"settings follows the last text line when it fits: " + JSON.stringify(layout));
}
}
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
test(`${engine}: a refresh keeps its turn and reading position through redraws, then finishes a whole turn`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
const page = await browser.newPage({ viewport: { width: 1250, height: 900 }, reducedMotion: "no-preference" });
page.setDefaultTimeout(5000);
const errors = [];
page.on("pageerror", e => errors.push(e.message));
let release, releaseRead, readStarted;
const state = { quotas: fixtures(Date.now()), gets: 0, refreshes: [], gate: new Promise(r => { release = r; }) };
t.after(() => { release(); releaseRead?.(); });
await page.route("**/*", serve("zh", state));
await page.goto("http://magpie.test/?view=usage");
const row = page.locator('.subscription-card[data-provider="codex"] .quota-read');
await showRefresh(row);
const started = new Promise(r => { readStarted = r; });
state.readStarted = readStarted;
state.readGate = new Promise(r => { releaseRead = r; });
await page.evaluate(() => { void loadQuotas(); });
await started;
await row.locator(".quota-refresh").click();
await row.evaluate(e => {
const button = e.querySelector(".quota-refresh"), animation = button.querySelector("path").getAnimations()[0];
animation.pause();
animation.currentTime = 1200; // Mid-turn, independent of the runner's speed.
window.refreshMotion = { button, animation, x: e.querySelector(".quota-ago").getBoundingClientRect().x };
});
releaseRead();
await page.evaluate(() => quotasLoading);
assert.equal(await row.evaluate(e => e.querySelector(".quota-refresh") === window.refreshMotion.button), true, "a background reading replaced the spinning control");
assert.equal(await row.evaluate(e => e.querySelector("path").getAnimations()[0] === window.refreshMotion.animation), true, "a background reading restarted the turn");
release();
await page.locator('.subscription-card[data-provider="codex"] .quota-n', { hasText: "61%" }).waitFor();
const end = await row.evaluate(e => {
const { button, animation, x } = window.refreshMotion;
return { same: e.querySelector(".quota-refresh") === button, busy: button.classList.contains("busy"), time: animation.currentTime,
turns: animation.effect.getTiming().iterations, duration: animation.effect.getTiming().duration, shift: e.querySelector(".quota-ago").getBoundingClientRect().x - x };
});
assert.deepEqual(end, { same: true, busy: true, time: 1200, turns: 2, duration: 800, shift: 0 }, "the reply interrupted the turn or shifted the reading");
await page.evaluate(() => window.refreshMotion.animation.finish());
await row.locator(".quota-refresh:not(.busy)").waitFor();
assert.equal(await row.evaluate(e => e.querySelector(".quota-refresh") === window.refreshMotion.button && getComputedStyle(window.refreshMotion.button).opacity === "1"), true, "finishing the turn hid or replaced the button");
// A response already available still gets one complete turn.
state.gate = undefined;
await row.locator(".quota-refresh").click();
await page.waitForFunction(() => document.querySelector('.subscription-card[data-provider="codex"] path').getAnimations()[0]?.effect.getTiming().iterations === 1);
await row.evaluate(e => e.querySelector("path").getAnimations()[0].finish());
await row.locator(".quota-refresh:not(.busy)").waitFor();
assert.deepEqual(errors, []);
});
}
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of Object.keys(deferredWords)) {
for (const width of [900, 420]) test(`${engine} ${lang} ${width}px: refresh icons align with their reading or error`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
const page = await browser.newPage({ viewport: { width, height: 800 }, reducedMotion: "reduce" });
page.setDefaultTimeout(5000);
const errors = [];
page.on("pageerror", e => errors.push(e.message));
const now = Date.now();
const state = layoutFixtures(now);
await page.route("**/*", serve(lang, state));
await page.goto("http://magpie.test/?view=usage");
// Fresh, stale and balance readings, plus a failed reading's first line.
for (const name of ["Codex", "Claude Code", "Relay", "Volcengine Ark"]) {
const card = page.locator(".subscription-card", { hasText: name });
const row = card.locator(".quota-read, .subscription-error");
await inView(page, row);
await showRefresh(row);
await assertRefreshAligned(row);
}
const error = page.locator(".subscription-error");
await assertInlineSettings(error, width === 420);
await inView(page, error);
await error.locator(".quota-fix").click();
await page.waitForURL(/view=providers/);
assert.equal(new URL(page.url()).searchParams.get("edit"), "volcengine");
await page.locator(".editor .volc-ak").waitFor();
assert.deepEqual(state.refreshes, [], "showing a card's refresh doesn't read it");
assert.deepEqual(errors, []);
});
}
}
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of ["en", "zh"]) {
const w = words[lang];
test(`${engine} ${lang}: a quota card is refreshed on its own`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
const errors = [];
// short, so the page scrolls and a click that moved it would show
const page = await (await browser.newContext({ viewport: { width: 900, height: 420 }, reducedMotion: "reduce" })).newPage();
page.setDefaultTimeout(5000);
page.on("pageerror", (e) => errors.push(e.message));
let open;
const state = { quotas: fixtures(Date.now()), gets: 0, refreshes: [], gate: new Promise((r) => { open = r; }) };
await page.route("**/*", serve(lang, state));
await page.goto("http://magpie.test/?view=usage");
const claude = page.locator(".subscription-card", { hasText: "Claude Code" });
const read = claude.locator(".quota-read");
await read.waitFor();
assert.equal(await read.textContent(), w.updated, "the line reads as it did");
const btn = read.locator("button.quota-refresh");
assert.equal(await btn.getAttribute("aria-label"), w.label);
const ago = read.locator(".quota-ago");
const opacity = () => btn.evaluate((e) => getComputedStyle(e).opacity);
// at rest: not there, the line starting where the account's does
await page.mouse.move(890, 5);
await btn.evaluate((e) => new Promise((done) => { const tick = () => getComputedStyle(e).opacity === "0" ? done() : requestAnimationFrame(tick); tick(); }));
const user = await claude.locator(".subscription-account .user").boundingBox();
assert.ok(Math.abs((await ago.boundingBox()).x - user.x) < 1, "the time lines up with the card at rest");
// hovered: left of the time, on its line
await claude.hover();
await btn.evaluate((e) => new Promise((done) => { const tick = () => getComputedStyle(e).opacity === "1" && e.getBoundingClientRect().width >= 14 ? done() : requestAnimationFrame(tick); tick(); }));
assert.equal(await opacity(), "1");
const [b, when] = [await btn.boundingBox(), await ago.boundingBox()];
assert.ok(b.width >= 14 && b.x + b.width <= when.x + 0.5 && Math.abs(b.y + b.height / 2 - (when.y + when.height / 2)) < 4, JSON.stringify({ b, when }));
await assertRefreshAligned(read);
// a failed reading and a balance have one too
const kimi = page.locator(".subscription-card", { hasText: "Kimi" });
assert.equal(await kimi.locator(".subscription-error button.quota-refresh").count(), 1);
assert.equal(await page.locator(".subscription-card", { hasText: "Relay" }).locator(".quota-read button.quota-refresh").count(), 1);
// the click: that card alone, turning meanwhile, the page where it was
await claude.scrollIntoViewIfNeeded();
await page.evaluate(() => window.scrollBy(0, 40));
const y = await page.evaluate(() => [scrollY, document.querySelector("#view-usage").scrollTop, document.scrollingElement.scrollTop]);
const gets = state.gets;
await claude.hover();
await btn.click();
await page.waitForFunction(() => document.querySelector(".quota-refresh.busy"));
assert.equal(await claude.locator(".quota-refresh.busy").count(), 1, "turns while it reads");
await assertRefreshAligned(read);
open();
await claude.locator(".quota-n", { hasText: "61%" }).waitFor();
assert.deepEqual(state.refreshes, ["?provider=claude&user=a%40x.com"]);
assert.equal(state.gets, gets, "no other card asked for");
assert.match(await claude.locator(".quota-read").textContent(), w.now);
assert.equal(await claude.locator(".quota-refresh.busy").count(), 0);
assert.match(await page.locator(".subscription-card", { hasText: "Codex" }).locator(".quota-n").textContent(), /50%/, "the others as they were");
assert.deepEqual(await page.evaluate(() => [scrollY, document.querySelector("#view-usage").scrollTop, document.scrollingElement.scrollTop]), y, "the click didn't scroll");
// a failed one read again from its own
await kimi.hover();
await kimi.locator(".quota-refresh").click();
await kimi.locator(".quota-n").waitFor();
assert.deepEqual(state.refreshes.slice(1), ["?provider=kimi"]);
assert.deepEqual(errors, []);
});
}
}