From 3f76105b3371ceac7f92ab7b795fd6b37c8d8a5b Mon Sep 17 00:00:00 2001 From: yetone Date: Mon, 5 Oct 2026 00:43:07 +0800 Subject: [PATCH] =?UTF-8?q?gui:=20the=20Usage=20page's=20allowances=20head?= =?UTF-8?q?=20stays=20while=20the=20page=20loads=20again,=20and=20a=20fail?= =?UTF-8?q?ed=20check-in=20says=20why=20in=20its=20row=20(#809,=20#808,=20?= =?UTF-8?q?Hu9956:=20=E7=94=A8=E9=87=8F=E7=95=8C=E9=9D=A2=E7=BA=A2?= =?UTF-8?q?=E6=A1=86=E8=BF=99=E8=A1=8C=E4=BC=9A=E5=BB=B6=E8=BF=9F=E5=87=A0?= =?UTF-8?q?=E7=A7=92=E6=89=8D=E4=BC=9A=E5=87=BA=E7=8E=B0=20=E2=80=94=20?= =?UTF-8?q?=E7=82=B9=E5=88=B0=E5=85=B6=E4=BB=96=E7=95=8C=E9=9D=A2=E6=AF=94?= =?UTF-8?q?=E5=A6=82Agent=EF=BC=8C=E5=86=8D=E7=82=B9=E5=9B=9E=E7=94=A8?= =?UTF-8?q?=E9=87=8F=E7=95=8C=E9=9D=A2=E5=B0=B1=E8=83=BD=E5=A4=8D=E7=8E=B0?= =?UTF-8?q?;=20TraeCN=E8=87=AA=E5=8A=A8=E7=AD=BE=E5=88=B0=E5=A4=B1?= =?UTF-8?q?=E6=95=88)=20=E2=80=94=20renderUsageLoading=20hid=20every=20.ro?= =?UTF-8?q?w-head=20on=20the=20Usage=20page,=20#quotaHead=20(Allowances,?= =?UTF-8?q?=20Trend,=20accounts=20masked,=20Used/Left)=20among=20them,=20w?= =?UTF-8?q?hile=20renderQuotas=20had=20just=20drawn=20it=20over=20the=20ca?= =?UTF-8?q?rds=20it=20heads;=20it=20came=20back=20only=20once=20/api/usage?= =?UTF-8?q?=20answered,=20seconds=20later,=20and=20a=20period=20with=20no?= =?UTF-8?q?=20calls=20hid=20it=20for=20good=20(renderUsage=20set=20every?= =?UTF-8?q?=20head=20to=20hidden=3Dempty).=20Both=20loops=20now=20leave=20?= =?UTF-8?q?#quotaHead=20to=20renderQuotas.=20A=20failed=20WorkBuddy=20or?= =?UTF-8?q?=20Trae=20CN=20check-in=20kept=20its=20reason=20in=20the=20row'?= =?UTF-8?q?s=20tooltip=20only,=20so=20the=20card=20read=20just=20=E7=AD=BE?= =?UTF-8?q?=E5=88=B0=E5=A4=B1=E8=B4=A5=EF=BC=8Cmagpie=20=E7=A8=8D=E5=90=8E?= =?UTF-8?q?=E9=87=8D=E8=AF=95;=20the=20reason=20now=20follows=20it=20in=20?= =?UTF-8?q?the=20row=20(=C2=B7=20code=209074:=20=E2=80=A6).=20Trae=20CN's?= =?UTF-8?q?=20own=20fix=20is=20the=20plugin's:=20trae=200.1.9=20(magpie-co?= =?UTF-8?q?mmunity/plugins=2064c9e6c)=20sends=20the=20check-in=20as=200.1.?= =?UTF-8?q?6=20did,=20which=20worked=20for=20the=20reporter=20before=200.1?= =?UTF-8?q?.7.=20usage-quota-head.test.cjs=20(WebKit,=20en=20and=20zh:=20t?= =?UTF-8?q?he=20head=20stays=20visible=20through=20a=20slow=20reload=20and?= =?UTF-8?q?=20a=20period=20with=20no=20calls,=20a=20failed=20check-in=20sh?= =?UTF-8?q?ows=20its=20reason)=20fails=20on=20the=20old=20code;=20the=20us?= =?UTF-8?q?age-*.test.cjs=20and=20quota*.test.cjs=20pass=20on=20WebKit.=20?= =?UTF-8?q?go=20vet,=20builds=20(darwin,=20GOOS=3Dlinux=20nogui,=20GOOS=3D?= =?UTF-8?q?windows)=20and=20go=20test=20-tags=20nogui=20-count=3D1=20./...?= =?UTF-8?q?=20under=20a=20temp=20HOME=20pass.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/gui/assets/app.js | 9 ++- internal/gui/tests/usage-quota-head.test.cjs | 85 ++++++++++++++++++++ 2 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 internal/gui/tests/usage-quota-head.test.cjs diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index 54cc8c622..fa8439780 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -10525,7 +10525,9 @@ function renderUsageLoading() { } $("#chart").hidden = true; for (const id of ["usageAgents", "usageModels", "usageKeys"]) $("#" + id).hidden = true; - for (const h of $$("#view-usage .row-head")) h.hidden = true; + // the allowances' head is renderQuotas' alone: the quotas it heads are + // still there while the period loads (#809, Hu9956: 用量界面红框这行会延迟几秒才会出现) + for (const h of $$("#view-usage .row-head:not(#quotaHead)")) h.hidden = true; $("#usageNote").textContent = ""; } @@ -11789,7 +11791,8 @@ function checkinRow(q, first, subs) { break; default: kind = "bad"; - text = t(on ? "Check-in failed; magpie tries again later" : "Check-in failed"); + // why, in the row: a reason only in the tooltip read as no reason (#808) + text = t(on ? "Check-in failed; magpie tries again later" : "Check-in failed") + (r.msg ? " · " + r.msg : ""); } } else { kind = on ? "wait" : ""; @@ -12118,7 +12121,7 @@ function renderUsage() { const empty = !u.calls; $("#chart").hidden = empty; for (const id of ["usageAgents", "usageModels"]) $("#" + id).hidden = empty; - for (const h of $$("#view-usage .row-head")) h.hidden = empty; + for (const h of $$("#view-usage .row-head:not(#quotaHead)")) h.hidden = empty; $("#usageKeysHead").hidden = $("#usageKeys").hidden = empty || !u.callerKeys?.length; // each subscription account's share, by the account that answered (#557) $("#usageAccountsHead").hidden = $("#usageAccounts").hidden = empty || !u.accounts?.length; diff --git a/internal/gui/tests/usage-quota-head.test.cjs b/internal/gui/tests/usage-quota-head.test.cjs new file mode 100644 index 000000000..1c57507d6 --- /dev/null +++ b/internal/gui/tests/usage-quota-head.test.cjs @@ -0,0 +1,85 @@ +// Run with Node's test runner and Playwright on the module path; see README.md. +// #809 (Hu9956: 用量界面红框这行会延迟几秒才会出现 — 点到其他界面比如Agent, +// 再点回用量界面就能复现): the allowances' head (Allowances, the trend, the +// masking, Used/Left) stays over the cards it heads while the period's +// usage loads again, rather than being hidden until it comes in, and a +// period with no calls doesn't hide it. #808 (Hu9956: TraeCN自动签到失效): +// a failed check-in says why in its row, not only in its tooltip. English +// and Chinese; 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 assets = path.resolve(__dirname, "../assets"); +const today = new Date(Date.now() + 8 * 3600e3).toISOString().slice(0, 10); +const quotas = [ + { provider: "codex", name: "Codex", icon: "openai", plan: "Plus", windows: [{ name: "5 hours", used: 20 }] }, + { provider: "trae-cn", name: "Trae CN", plan: "Free", user: "hu", windows: [{ name: "Credits", used: 3 }], + checkins: true, checkinBy: "trae", checkin: { user: "hu", day: today, outcome: "failed", msg: "code 9074: 当前参与用户太多" } }, +]; + +function serve(lang, gate) { + const settings = { theme: "light", lang, quotaLeft: false, currency: "usd", traeCheckin: 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 = {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: [], presets: [], excluded: [], gateway: { running: true, window: true } }); + if (url.pathname === "/api/usage/quotas") { await gate.quotas; return json(quotas); } + if (url.pathname === "/api/usage") { await gate.usage; return json({ calls: gate.calls, input: 10, output: 5, cost: 0, agents: [], models: [], days: [] }); } + 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: "" }); } + }; +} + +for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { + for (const lang of ["en", "zh"]) { + test(`${engine} ${lang}: the allowances' head stays while the page loads again`, async (t) => { + const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" })); + t.after(() => browser.close()); + const page = await (await browser.newContext({ viewport: { width: 1000, height: 900 }, reducedMotion: "reduce" })).newPage(); + page.setDefaultTimeout(5000); + const errors = []; + page.on("pageerror", (e) => errors.push(e.message)); + const gate = { usage: Promise.resolve(), quotas: Promise.resolve(), calls: 3 }; + await page.route("**/*", serve(lang, gate)); + await page.goto("http://magpie.test/?view=usage"); + const head = page.locator("#quotaHead"); + await page.locator(".subscription-card", { hasText: "Trae CN" }).waitFor(); + await head.waitFor(); + // the failed check-in's reason is in the row + const say = (await page.locator(".wb-checkin .ci-say").innerText()).trim(); + assert.equal(say, (lang === "en" ? "Check-in failed; magpie tries again later" : "签到失败,magpie 稍后重试") + " · code 9074: 当前参与用户太多"); + + // away and back, with the vendors and the log both slow to answer + let free; + gate.usage = new Promise((r) => (free = r)); + gate.quotas = gate.usage; + await page.click('nav button[data-view="agents"], button[data-view="agents"]'); + await page.click('button[data-view="usage"]'); + await page.waitForFunction(() => document.querySelector("#view-usage").classList.contains("loading")); + assert.equal(await head.isVisible(), true, "the head went while the page loaded"); + assert.equal(await page.locator(".subscription-card", { hasText: "Codex" }).isVisible(), true); + free(); + await page.waitForFunction(() => !document.querySelector("#view-usage").classList.contains("loading")); + assert.equal(await head.isVisible(), true); + + // a period without calls keeps it over its cards + gate.calls = 0; + await page.click('button[data-view="agents"]'); + await page.click('button[data-view="usage"]'); + await page.waitForFunction(() => document.querySelector("#stats.empty")); + assert.equal(await head.isVisible(), true, "no calls hid the allowances' head"); + assert.deepEqual(errors, []); + }); + } +}