diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index b4b927648..47ea834b2 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -13881,7 +13881,9 @@ function accountQuota(data, user, capFor = () => 0, settable = null) { if (w.resetsAt) { const at = new Date(w.resetsAt); m.title = resetText(at, at.toLocaleString(intlLang())); - if (used >= 80) m.append(el("span", "aq-r", resetInText(at))); + // every window's, not only a nearly used one's: the week's at 23% + // is what the user plans by too (rosarobertist on X) + m.append(el("span", "aq-r", resetInText(at))); } const out = runsOutText(w, (at) => at.toLocaleString(intlLang())); if (out) m.title = tipLines(m.title, out); diff --git a/internal/gui/tests/account-every-reset.test.cjs b/internal/gui/tests/account-every-reset.test.cjs new file mode 100644 index 000000000..89a5729cd --- /dev/null +++ b/internal/gui/tests/account-every-reset.test.cjs @@ -0,0 +1,80 @@ +// Run with Node's test runner and Playwright on the module path; see README.md. +// Every window on a provider editor's account row says when it resets, not +// only one nearly used up (rosarobertist on X: 能在这儿加个重置时间吗?这样 +// 就能做到心中有数了). The row read "5 hours 100% used resets in 1h · 7 days +// 23% used" — the week's reset was left to the tooltip below 80%. Claude +// Code and Codex accounts, every language, Chromium and WebKit, at a desk's +// width, the window's narrowest and a phone's: the reset stays in the row +// and the sheet doesn't scroll sideways. MAGPIE_ACC_ASSETS runs it on older +// assets. +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_ACC_ASSETS || path.resolve(__dirname, "../assets")); +const soon = new Date(Date.now() + 2 * 36e5).toISOString(); +const week = new Date(Date.now() + (6 * 24 + 1) * 36e5).toISOString(); +const provider = (id, name, icon, user) => ({ + id, name, icon, chat: "", responses: "", anthropic: "", catalog: "", routing: "", + models: [{ id: "m", name: "", on: true }], agents: [], fallback: [], headers: {}, keyList: [], + account: { agent: id, agentName: name, user, plan: "pro", logins: [{ user, plan: "pro", active: true, on: true }] }, +}); +const providers = [provider("claude", "Claude Code", "claude-color", "me@example.com"), provider("codex", "Codex", "codex-color", "you@example.com")]; +const windows = () => [{ name: "5 hours", used: 100, resetsAt: soon }, { name: "7 days", used: 23, resetsAt: week }]; +const serve = (lang, web) => 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", quotaLeft: false } }); + if (url.pathname === "/api/providers") return json({ providers, 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") { + const agent = url.searchParams.get("agent"); + const p = providers.find((x) => x.id === agent); + return json(p ? { [p.account.user]: { provider: agent, windows: windows(), asOf: new Date(Date.now() - 6e4).toISOString(), readDeferred: true } } : {}); + } + 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 langs = ["en", "zh", "zh-TW", "ja", "de"]; +for (const [engine, browserType] of [["chromium", chromium], ["webkit", webkit]]) { + test(engine + ": every window on an account row says when it resets", 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 langs) for (const p of providers) { + const at = `${p.name} ${lang} ${web ? "web " : ""}${width}`; + const context = await browser.newContext({ viewport: { width, height: 900 }, reducedMotion: "reduce" }); + const page = await context.newPage(); + const errors = []; + page.on("pageerror", (e) => errors.push(e.message)); + await page.route("**/*", serve(lang, web)); + await page.goto("http://magpie.test/?view=providers"); + await page.locator(".row.provider", { hasText: p.name }).first().click(); + const row = page.locator(`.editor .acc[data-account-id="${p.account.user}"]`); + await row.locator(".aq-w").nth(1).waitFor(); + // the words the row should say, in its language + const want = await page.evaluate((lang) => { + const tr = (k, v) => Object.entries(v).reduce((s, [n, x]) => s.replace("{" + n + "}", x), (lang === "en" ? k : I18N[lang][k]) || k); + return [tr("resets {in}", { in: tr("in {n}h", { n: 2 }) }), tr("resets {in}", { in: tr("in {n}d", { n: 6 }) })]; + }, lang); + const got = await row.locator(".aq-w").evaluateAll((ms) => ms.map((m) => m.querySelector(".aq-r")?.textContent || "")); + assert.deepEqual(got, want, `${at}: the 5 hours and the 7 days each say when they reset`); + // in the row, and the sheet not scrolling sideways + const fit = await row.evaluate((r) => { + const rb = r.getBoundingClientRect(), body = document.querySelector(".modal .ebody"); + return { out: [...r.querySelectorAll(".aq-w")].map((m) => Math.round(m.getBoundingClientRect().right - rb.right)), wide: body ? body.scrollWidth - body.clientWidth : 0 }; + }); + for (const o of fit.out) assert.ok(o <= 0, `${at}: a window's reset stays in its row (${o}px past it)`); + assert.ok(fit.wide <= 0, `${at}: the sheet doesn't scroll sideways (${fit.wide}px)`); + assert.deepEqual(errors, [], at); + await context.close(); + } + }); +}