gui: each account in a provider editor has its own usage refresh

The owner, on the editor's account list: 在这里也要加上手动刷新用量的按钮。
Each signed-in account gets a refresh after its plan chip, always shown.
It posts usage/quotas/refresh for that provider and user, as the Usage
page's does, then reads the account rows again (quotaRefresh takes a
then-hook its turn waits for), so the row shows the new reading. A
signed-out account has none.

It sits on the account's own line, which never wraps. First it was at
the end of the readings line: where the meters filled it, it added a
second line, and WebKit kept the old row height until a click, drawing
that line over the next row (window-cap, WebKit 1100 en: "row moved
152 !== 134"). A nowrap wrapper with the last meter still wrapped.

Run: account-refresh.test.cjs (new: shown without hover, beside the
plan, inside the row, tip in every language, a click asks for
"claude first@example.com" and the line shows 64, no sideways scroll),
both engines, en/zh/zh-TW/ja/de at 1000, 560 and web 390, on wsl-run
(mote's WebKit is broken, #4125). With window-cap, account-models,
editor-advanced-align and account-row-line: 90/90.
Broken by hand: without the then-hook the line never shows 64 and the
test times out; the readings-line placement is red in window-cap.
Budgets: app.js 1,315,479 and the core 1,538,554 at this head, rebased
on 23a5e822d; both raised to exactly that, no room added (the editor is
the core's own; noted in core_budget_test.go).
go test ./internal/gui/ on mote: ok but TestCoveredWindowStaysUp, killed
at its deadline (mote's AppKit hang, #4125). go vet ./internal/gui/ on the
Windows box: ok; on the WSL box it can't build (no webkitgtk-6.0/gtk4
dev packages there), so Linux vet is CI's.
i18n-keys, gui-ja and three editor files: 45/45 on wsl-run.
Not tried: a real account's refresh in the app.
This commit is contained in:
yetone
2026-10-31 10:40:12 +08:00
parent ba970b1b49
commit 923812362a
5 changed files with 126 additions and 3 deletions
+2
View File
@@ -2904,6 +2904,8 @@ body.window .ag-sk-icon { width: 22px; height: 22px; border-radius: 6px; }
/* a subscription's allowance, under its name */
.accts .acc.with-aq { flex-wrap: wrap; row-gap: 3px; padding-top: 7px; padding-bottom: 8px; }
.acc .aq { flex-basis: 100%; display: flex; flex-wrap: wrap; align-items: center; gap: 4px 16px; padding-left: 25px; font-size: 10.5px; color: var(--muted); min-height: 14px; }
/* its refresh always shown, after its plan on the line that never wraps: on the readings' line it added a line where the meters filled it (the owner asked for a button here) */
.acc-line > .quota-refresh { width: 14px; margin: 0 0 0 -5px; opacity: 1; }
.acc .aq-w { display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; }
.acc .aq-track { width: 54px; height: 4px; border-radius: 2px; background: var(--pill); overflow: hidden; }
.acc .aq-track i { display: block; height: 100%; border-radius: inherit; background: var(--fg-2); }
+17 -1
View File
@@ -13381,6 +13381,7 @@ function renderAccounts(a, p) {
line.append(forget, use);
}
}
if (quota && !l.lapsed) line.querySelector(".plan").after(accountRefresh(a.agent, p, l.user));
row.append(accountQuota(l.lapsed ? { [l.user]: { error: t(l.lapsed) } } : quota, l.user, capFor, p ? (m, w) => windowCapMeter(m, p, l.user, w) : null));
row.classList.add("with-aq"); // not :has(.aq), which Safari 15.0 lacks (#220)
// Remove goes under ⋯ too, but beside Sign in again while signed out
@@ -13813,6 +13814,19 @@ function balanceFix(p) {
return box;
}
// accountRefresh: an account row's refresh, then its rows read again
function accountRefresh(agent, p, user) {
const b = quotaRefresh({ provider: p?.id || agent, user }, async () => {
await loginUsage[agent]?.loading;
if (loginUsage[agent]) loginUsage[agent].at = 0;
loginUsageOf(agent);
await loginUsage[agent].loading;
});
b.classList.remove("reading");
b.title = t("Read this account's usage again");
return b;
}
// accountQuota: an account's allowance as a line of small meters under its
// name, the reset time on the ones nearly used up, credits and held resets.
function accountQuota(data, user, capFor = () => 0, settable = null) {
@@ -18426,7 +18440,8 @@ function pqLine(say, q, c = "") {
l.append(say.nodeType ? say : el("span", "pq-say", say), quotaRefresh(q));
return l;
}
function quotaRefresh(q) {
// then: what else its turn waits for
function quotaRefresh(q, then) {
const key = usageAcctKey(q);
// only one its redraw took out: the panel and Usage page each draw it
const kept = [quotaRefreshing.get(key)?.button, keptQuotaRefresh?.get(key)].find((k) => k && !k.isConnected);
@@ -18476,6 +18491,7 @@ function quotaRefresh(q) {
const version = ++quotaRefreshVersion;
for (const x of [q, ...before, ...quotas]) if (belongs(x)) quotaRefreshVersions.set(usageAcctKey(x), version);
}
if (then) await then();
} catch (err) {
statusErr(err);
} finally {
+4
View File
@@ -2907,6 +2907,7 @@ const I18N = {
"Refresh now": "立即刷新",
"Refresh this card": "刷新这张卡片",
"Read this card again": "只刷新这张卡片的用量,其他卡片不动",
"Read this account's usage again": "重新读取这个账号的用量",
"Read this card again, by running Claude Code's own /usage": "只刷新这张卡片的用量(运行 Claude Code 自己的 /usage),其他卡片不动",
"Open Usage": "打开用量",
"No requests today": "今天还没有请求",
@@ -7133,6 +7134,7 @@ const I18N = {
"Refresh now": "立即重新整理",
"Refresh this card": "重新整理這張卡片",
"Read this card again": "只重新整理這張卡片的用量,其他卡片不動",
"Read this account's usage again": "重新讀取這個帳號的用量",
"Read this card again, by running Claude Code's own /usage": "只重新整理這張卡片的用量(執行 Claude Code 自己的 /usage),其他卡片不動",
"Open Usage": "開啟用量",
"No requests today": "今天還沒有請求",
@@ -11361,6 +11363,7 @@ const I18N = {
"Refresh now": "今すぐ更新",
"Refresh this card": "このカードを更新",
"Read this card again": "このカードの使用量だけを読み直します(ほかのカードはそのまま)",
"Read this account's usage again": "このアカウントの使用量を読み直します",
"Read this card again, by running Claude Code's own /usage": "このカードの使用量だけを読み直します(Claude Code 自身の /usage を実行。ほかのカードはそのまま)",
"Open Usage": "使用量を開く",
"No requests today": "今日のリクエストはまだありません",
@@ -15599,6 +15602,7 @@ const I18N = {
"Refresh now": "Jetzt aktualisieren",
"Refresh this card": "Diese Karte aktualisieren",
"Read this card again": "Nur diese Karte neu lesen, die anderen bleiben, wie sie sind",
"Read this account's usage again": "Die Nutzung dieses Kontos neu lesen",
"Read this card again, by running Claude Code's own /usage": "Nur diese Karte neu lesen, mit Claude Codes eigenem /usage; die anderen bleiben, wie sie sind",
"Open Usage": "Nutzung öffnen",
"No requests today": "Heute keine Anfragen",
File diff suppressed because one or more lines are too long
+101
View File
@@ -0,0 +1,101 @@
// Run with Node's test runner and Playwright on the module path; see README.md.
// The owner, on a provider editor's accounts: 在这里也要加上手动刷新用量的按钮。
// Each signed-in account has a refresh after its plan, shown, not on hover
// only: it reads that account's card again (provider and user, as the
// Usage page's refresh does), and its allowance line then shows the new
// reading. One that can't be read has it too; a signed-out one has none.
// It is on the account's own line, which never wraps: at the end of the
// readings it added a line where the meters filled it (window-cap, WebKit
// 1100), and WebKit drew that line over the next row until a click.
// In every language, wide, narrow and in a phone's browser, both engines.
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(process.env.MAGPIE_GUI_ASSETS || path.resolve(__dirname, "../assets"));
const claude = () => ({
id: "claude", name: "Claude Code", icon: "claude-color", chat: "", responses: "", anthropic: "", catalog: "", routing: "",
models: [{ id: "claude-opus-5-5", name: "Claude Opus 5.5", on: true }], agents: [], fallback: [], headers: {}, keyList: [],
account: {
agent: "claude", agentName: "Claude Code", user: "first@example.com", plan: "team",
logins: [
{ user: "first@example.com", plan: "team", active: true, on: true },
{ user: "spare@example.com", plan: "max", on: true },
{ user: "gone@example.com", plan: "pro", on: true, lapsed: "Sign-in has expired" },
],
},
});
const serve = (lang, web, ctl) => 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:${JSON.stringify(lang)},theme:"light",web:${web}};` });
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: [claude()], presets: [], excluded: [], gateway: { running: true, window: true } });
if (url.pathname === "/api/groups") return json({ groups: [] });
if (url.pathname === "/api/plugins") return json({ plugins: [] });
if (url.pathname === "/api/login/usage") return json({
"first@example.com": { windows: [{ name: "5h", used: ctl.used, limit: 100 }] },
"spare@example.com": { error: "unavailable" },
});
if (url.pathname === "/api/usage/quotas/refresh") {
ctl.asked.push(url.searchParams.get("provider") + " " + url.searchParams.get("user"));
ctl.used = 64;
return json([{ provider: "claude", user: "first@example.com", windows: [{ name: "5h", used: 64, limit: 100 }] }]);
}
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: "" }); }
};
for (const [engine, browserType] of [["chromium", chromium], ["webkit", webkit]]) {
test(engine + ": each account's allowance has a refresh that reads it again", async (t) => {
const browser = await browserType.launch();
t.after(() => browser.close());
for (const [web, width] of [[false, 1000], [false, 560], [true, 390]]) for (const lang of ["en", "zh", "zh-TW", "ja", "de"]) {
const at = `${lang} ${web ? "web " : ""}${width}`;
const ctl = { used: 12, asked: [] };
const context = await browser.newContext({ viewport: { width, height: 900 }, reducedMotion: "reduce" });
const page = await context.newPage();
page.setDefaultTimeout(5000);
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", serve(lang, web, ctl));
await page.goto("http://magpie.test/?view=providers");
await page.locator(".row.provider", { hasText: "Claude Code" }).first().click();
const row = (u) => page.locator(`.editor .accts .acc[data-account-id="${u}"]`);
await row("first@example.com").locator(".aq-w").waitFor();
await page.mouse.move(1, 1);
// shown without a hover, after the plan on the account's line, inside the row
const tip = await page.evaluate((lang) => lang === "en" ? "Read this account's usage again" : I18N[lang]?.["Read this account's usage again"], lang);
assert.ok(tip, `${at}: the refresh's tip has no ${lang}`);
for (const u of ["first@example.com", "spare@example.com"]) {
const b = row(u).locator(".acc-line > .plan + .quota-refresh");
assert.equal(await b.count(), 1, `${at}: ${u} has no refresh`);
assert.equal(await b.getAttribute("title"), tip);
const box = await b.boundingBox(), aq = await row(u).locator(".acc-line").boundingBox(), r = await row(u).boundingBox();
assert.ok(box && box.width >= 10 && +(await b.evaluate((x) => getComputedStyle(x).opacity)) === 1, `${at}: ${u}'s refresh is not shown`);
assert.ok(box.y >= aq.y - 1 && box.y + box.height <= aq.y + aq.height + 1, `${at}: ${u}'s refresh is off its line`);
assert.ok(box.x + box.width <= r.x + r.width + 1, `${at}: ${u}'s refresh is out of its row`);
// beside the plan
const plan = await row(u).locator(".acc-line > .plan").boundingBox();
assert.ok(box.y < plan.y + plan.height && box.y + box.height > plan.y && box.x >= plan.x + plan.width && box.x - plan.x - plan.width <= 8, `${at}: ${u}'s refresh is not beside its plan`);
}
assert.equal(await row("gone@example.com").locator(".quota-refresh").count(), 0, `${at}: a signed-out account has a refresh`);
// it reads that account's card again, and the line shows the new reading
await row("first@example.com").locator(".quota-refresh").click();
await page.waitForFunction(() => document.querySelector('.editor .acc[data-account-id="first@example.com"] .aq-w b')?.textContent.includes("64"));
assert.deepEqual(ctl.asked, ["claude first@example.com"]);
await page.waitForFunction(() => !document.querySelector(".editor .quota-refresh.busy"));
assert.equal(await page.evaluate(() => document.documentElement.scrollWidth <= document.documentElement.clientWidth + 1), true, `${at}: the page scrolls sideways`);
await page.screenshot({ path: path.join(process.env.SHOTS || "/tmp", `account-refresh-${engine}-${lang}-${width}.png`) }).catch(() => {});
assert.deepEqual(errors, []);
await context.close();
}
});
}