diff --git a/internal/gui/assets/app.css b/internal/gui/assets/app.css index 7811e9810..38752ee61 100644 --- a/internal/gui/assets/app.css +++ b/internal/gui/assets/app.css @@ -1050,12 +1050,10 @@ body.panel .row.pref.flash { border-radius: 10px; } .subscription-head .ic { width: 17px; height: 17px; } .subscription-head b { font-size: 12.5px; font-weight: 600; } .subscription-account { display: flex; align-items: baseline; gap: 7px; margin: 0 0 6px; font-size: 11px; color: var(--fg-2); } -.subscription-card.several .quota-windows:not([hidden]) + .subscription-account, -.subscription-card.several .subscription-error:not([hidden]) + .subscription-account, -.subscription-card.several .quota-balance:not([hidden]) + .subscription-account, -.subscription-card.several .quota-read:not([hidden]) + .subscription-account, -.subscription-card.several .credit-days:not([hidden]) + .subscription-account, -.subscription-card.several .quota-curve:not([hidden]) + .subscription-account { margin-top: 10px; padding-top: 9px; border-top: 1px solid var(--line); } +/* every account after the first has a hairline over it, whatever the one + before ends with (normalcoder_ on X: a check-in row or magpie's own + requests, which no list of row kinds named, ran two accounts together) */ +.subscription-card.several > :not(.subscription-head, .quota-all) + .subscription-account { margin-top: 10px; padding-top: 9px; border-top: 1px solid var(--line); } .subscription-account .user { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } /* an account the user named (#1515): the name, then the account and the key plan it stands in for, muted, giving their room up first */ @@ -1204,7 +1202,6 @@ body.panel .row.pref.flash { border-radius: 10px; } :root[data-colorblind="1"] :is(.qc-line, .qc-dash)[data-series="6"] { stroke-dasharray: 12 4 3.5 4 3.5 4; } .quota-resets { display: flex; align-items: center; gap: 8px; margin-top: 9px; font-size: 10.5px; font-variant-numeric: tabular-nums; } .quota-resets .text { margin-left: auto; flex: none; font-size: 11px; padding: 2px 7px; } -.subscription-card.several .quota-resets:not([hidden]) + .subscription-account { margin-top: 10px; padding-top: 9px; border-top: 1px solid var(--line); } /* a remote magpie's gateway key (#1547): its cycle, calls, used up */ .quota-keyline { margin-top: 7px; font-size: 10.5px; color: var(--muted); font-variant-numeric: tabular-nums; overflow-wrap: anywhere; } .quota-keyline.spent { color: var(--red); } @@ -1213,7 +1210,6 @@ body.panel .row.pref.flash { border-radius: 10px; } .led .who .own-badge { margin-left: 0; } .quota-own { display: flex; align-items: center; flex-wrap: wrap; gap: 6px; margin-top: 7px; font-size: 10.5px; color: var(--muted); font-variant-numeric: tabular-nums; } .quota-own .own-badge { margin-left: 0; } -.subscription-card.several .quota-keyline + .subscription-account { margin-top: 10px; padding-top: 9px; border-top: 1px solid var(--line); } .reset-ask .reset-idle { color: var(--amber); } /* which of an account's resets is used, the soonest unless another is picked */ .reset-ask .reset-pick { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; margin: 2px 0 10px; font-size: 12px; color: var(--muted); } @@ -2210,8 +2206,6 @@ body.panel .profiles #save { height: 25px; } /* the field's: the line keeps its .quota-autoreset .ar-say, .pq-autoreset .ar-say, .quota-credits .ar-say, .pq-credits .ar-say { line-height: 16px; } .quota-autoreset, .quota-credits { margin-top: 8px; font-size: 10.5px; } .quota-resets + .quota-autoreset, .quota-autoreset + .quota-credits { margin-top: 6px; } -.subscription-card.several .quota-autoreset + .subscription-account, -.subscription-card.several .quota-credits + .subscription-account { margin-top: 10px; padding-top: 9px; border-top: 1px solid var(--line); } .pq-autoreset, .pq-credits { grid-column: 1 / -1; margin-top: 5px; padding-top: 7px; border-top: 1px solid var(--line-2); font-size: 11px; } .pq-resets + .pq-autoreset, .pq-autoreset + .pq-credits { margin-top: 5px; padding-top: 0; border-top: 0; } /* spending them only after everything else, a line of its own under the diff --git a/internal/gui/assets/plugins/routing/client.js b/internal/gui/assets/plugins/routing/client.js index 12a8a4bcc..1b0d144d9 100644 --- a/internal/gui/assets/plugins/routing/client.js +++ b/internal/gui/assets/plugins/routing/client.js @@ -2544,7 +2544,7 @@ function routingPage(part) { let f = folds.get(key); if (!f) { const li = el("li", "rt-fold"), b = el("b"), st = el("em"), tg = el("span", "tag"); - const n = el("span", "n"), chev = el("span", "chev"), bar = el("div", "bar"), bi = el("i"); + const n = el("span", "n"), chev = el("span", "chev"), bar = el("div", "bar quota-track"), bi = el("i"); chev.append(svg(CHEV, 12, 1.7)); bar.append(bi); li.append(el("i", "dot"), b, st, chev, bar, tg); @@ -2589,7 +2589,8 @@ function routingPage(part) { b.append(f); } if (w.fallback) b.append(el("small", "fb", t("fallback"))); - const st = el("em"), bar = el("div", "bar"), bi = el("i"), tg = el("span", "tag"); + // its bar is Usage's .quota-track, coloured alike (normalcoder_ on X) + const st = el("em"), bar = el("div", "bar quota-track"), bi = el("i"), tg = el("span", "tag"); bar.append(bi); // what the row says, cut short beside the model (routing.css), is read // whole over it (yi_time on Discord). Measured as the pointer comes, @@ -5008,7 +5009,7 @@ function routingPage(part) { fix.append(again); } if (w.known && w.balance == null) { - const bar = el("div", "bar"), bi = el("i"); + const bar = el("div", "bar quota-track"), bi = el("i"); bi.style.width = fill(w); bar.append(bi); row.append(bar); diff --git a/internal/gui/assets/plugins/routing/routing.css b/internal/gui/assets/plugins/routing/routing.css index 9f686e304..ba5dbeeec 100644 --- a/internal/gui/assets/plugins/routing/routing.css +++ b/internal/gui/assets/plugins/routing/routing.css @@ -135,13 +135,15 @@ /* the session an account answers (connor_yang on Discord): one line cut short, also where a narrow stage lets the status wrap */ .rt-accts em .ses { display: inline-block; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; vertical-align: bottom; } -.rt-accts .bar { grid-column: 2 / 4; height: 3px; border-radius: 2px; background: var(--card-2); overflow: hidden; } -.rt-accts .bar i { display: block; height: 100%; width: 0; border-radius: 2px; background: var(--muted); transition: width .4s; } +/* an account's bar is Usage's .quota-track (app.css), its colours the + same in either theme (normalcoder_ on X: grey and black here, blue + there); only its size and place are the stage's */ +.rt-accts .bar { grid-column: 2 / 4; height: 3px; } +.rt-accts .bar i { width: 0; transition: width .4s; } .rt-accts li.nobar .bar { display: none; } .rt-accts .dot { width: 8px; height: 8px; border-radius: 50%; border: 1.5px solid var(--muted); } .rt-accts li.on { border-color: var(--fg); } .rt-accts li.on em { color: var(--fg); } -.rt-accts li.on .bar i { background: var(--fg); } .rt-accts li.on .dot { background: var(--fg); border-color: var(--fg); } /* A live dot's pulse: a disk 6px wider all round grows from the dot and fades. It moves by transform and opacity, on the compositor: as a @@ -554,9 +556,8 @@ button.rt-session { cursor: pointer; } /* under the cell, in its bottom padding: every cell keeps its height. In the flow, not absolute: Chromium put an absolute bar of a row in a column's unbroken block (.rt-lead) over the row above it */ -.rt-act .bar { height: 2px; margin: 2px 0 -4px; border-radius: 2px; background: var(--card-2); overflow: hidden; } +.rt-act .bar { height: 2px; margin: 2px 0 -4px; } .rt-act:has(> .bar) { padding-bottom: 8px; } -.rt-act .bar i { display: block; height: 100%; border-radius: 2px; background: var(--muted); } /* a provider's keys summed up in one row (#1319), opened to each */ .rt-act.rt-keys { cursor: pointer; user-select: none; } .rt-act.rt-keys .chev { display: inline-flex; align-self: center; margin-left: auto; color: var(--muted); transition: transform .2s; } diff --git a/internal/gui/tests/quota-bars-alike.test.cjs b/internal/gui/tests/quota-bars-alike.test.cjs new file mode 100644 index 000000000..c4908c5c2 --- /dev/null +++ b/internal/gui/tests/quota-bars-alike.test.cjs @@ -0,0 +1,131 @@ +// Run with Node's test runner and Playwright on the module path; see README.md. +// normalcoder_ on X: the Routing page's quota bars were grey and black while +// the Usage overview's are blue, and in a Usage card with several accounts +// one account ran into the next. Routing's bars are Usage's .quota-track, +// the same track and fill in light and dark; and in Usage every account +// after the first has a hairline over it, whatever the one before ends +// with (here a check-in row, which no list of row kinds named). Every +// language, 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 assets = path.resolve(__dirname, "../assets"); +const nowMs = Date.now(), now = new Date(nowMs).toISOString(), iso = (ms) => new Date(ms).toISOString(); +const acct = (provider, name, user, used, extra) => ({ provider, name, kind: "subscription", icon: "generic", plan: "Pro", user, readAt: now, + windows: [{ name: "5h", used, resetsAt: iso(nowMs + 3600e3) }, { name: "Weekly", used: used / 2, resetsAt: iso(nowMs + 3 * 864e5) }], ...extra }); +const quotas = [ + acct("codex", "Codex", "a@example.com", 40, { lastServedAt: now, resets: { count: 2 } }), + acct("codex", "Codex", "b@example.com", 70), + // brief accounts that check in: the row under one is a check-in row + acct("trae", "Trae", "t1@example.com", 20, { checkins: true, lastServedAt: now }), + acct("trae", "Trae", "t2@example.com", 60, { checkins: true }), + acct("trae", "Trae", "t3@example.com", 30, { checkins: true }), +]; +const accts = [ + { id: "acct-1", provider: "codex", name: "Codex", who: "work@example.com", kind: "account", model: "gpt-6", known: true, used: 30, plan: "Plus" }, + { id: "acct-2", provider: "codex", name: "Codex", who: "spare@example.com", kind: "account", model: "gpt-6", known: true, used: 95, plan: "Pro" }, +]; +const route = { id: 7, seq: 7, time: iso(nowMs - 2000), agent: "codex", model: "gpt-6", provider: "codex", done: true, status: 200, ms: 900, tokens: 1500, + order: accts, tries: [{ id: "acct-1", model: "gpt-6", start: iso(nowMs - 2000), done: true, ms: 800, status: 200 }] }; + +function serve(lang, theme) { + const settings = { theme, lang, tray: "panel", quotaLeft: true, currency: "usd" }; + return async (r) => { + const url = new URL(r.request().url()); + const json = (data) => r.fulfill({ json: data }); + if (url.pathname === "/boot.js") return r.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"${theme}",web:true};` }); + if (url.pathname === "/wails/runtime.js") return r.fulfill({ contentType: "text/javascript", body: "export const Window = {};" }); + if (url.pathname === "/api/state") return json({ agents: [{ id: "codex", name: "Codex", path: "/test/codex", fields: [] }], 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, url: "http://127.0.0.1:3999" } }); + if (url.pathname === "/api/usage/quotas") return json(quotas); + if (url.pathname === "/api/gateway/trace") { + if (url.searchParams.get("wait")) return new Promise(() => {}); // nothing more comes + return json({ mine: true, now: new Date().toISOString(), seq: 7, totals: { requests: 1, rerouted: 0, errors: 0 }, routes: [route] }); + } + if (url.pathname === "/api/gateway/history") return json({ cut: false, days: [], routes: [] }); + if (url.pathname === "/api/groups") return json({ models: [], groups: [], pools: [] }); + 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 r.fulfill({ body: await fs.readFile(file), contentType }); } catch { await r.fulfill({ status: 404, body: "" }); } + }; +} + +// each bar's track and fill colours, beside the Usage overview's .quota-track +// drawn on the same page +const bars = (page, sel) => page.evaluate((sel) => { + const probe = document.createElement("div"); + probe.className = "quota-track"; + probe.append(document.createElement("i")); + document.body.append(probe); + const usage = { track: getComputedStyle(probe).backgroundColor, fill: getComputedStyle(probe.firstChild).backgroundColor }; + probe.remove(); + const seen = [...document.querySelectorAll(sel)].filter((b) => b.offsetParent).map((b) => ({ + track: getComputedStyle(b).backgroundColor, fill: getComputedStyle(b.querySelector("i")).backgroundColor, + })); + return { usage, seen }; +}, sel); + +for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { + for (const lang of ["en", "zh", "zh-TW", "ja", "de"]) { + test(`${engine} ${lang}: Routing's quota bars are Usage's, and Usage's accounts are apart`, async (t) => { + const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" })); + t.after(() => browser.close()); + for (const theme of ["light", "dark"]) for (const width of [1100, 420]) { + const at = `${theme} ${width}px`; + const ctx = await browser.newContext({ viewport: { width, height: 900 }, reducedMotion: "reduce", colorScheme: theme }); + const page = await ctx.newPage(); + page.setDefaultTimeout(5000); + page.setDefaultNavigationTimeout(60000); + const errors = []; + page.on("pageerror", (e) => errors.push(e.message)); + await page.route("**/*", serve(lang, theme)); + + await page.goto("http://magpie.test/?view=usage"); + await page.locator(".subscription-card[data-provider='trae'] .subscription-account").nth(2).waitFor(); + const lines = await page.evaluate(() => [...document.querySelectorAll("#subscriptionUsage > .subscription-card")].map((c) => { + const line = getComputedStyle(c).getPropertyValue("--line").trim(); + const probe = document.createElement("i"); + probe.style.color = line; + c.append(probe); + const want = getComputedStyle(probe).color; + probe.remove(); + return { card: c.dataset.provider, want, rows: [...c.querySelectorAll(":scope > .subscription-account")].map((a) => { + const s = getComputedStyle(a); + return { width: s.borderTopWidth, style: s.borderTopStyle, color: s.borderTopColor }; + }) }; + })); + for (const c of lines) { + assert.ok(c.rows.length >= 2, `${at}: ${c.card} shows its accounts`); + assert.equal(c.rows[0].style, "none", `${at}: ${c.card}'s first account has no line over it`); + c.rows.slice(1).forEach((r, i) => { + assert.equal(r.style, "solid", `${at}: ${c.card}'s account ${i + 2} has a line over it`); + assert.equal(r.width, "1px", `${at}: ${c.card}'s account ${i + 2}'s line is a hairline`); + assert.equal(r.color, c.want, `${at}: ${c.card}'s account ${i + 2}'s line is the app's --line`); + }); + } + const u = await bars(page, "#subscriptionUsage .quota-track"); + assert.ok(u.seen.length > 0, `${at}: Usage draws bars`); + for (const b of u.seen) assert.deepEqual(b, u.usage, `${at}: Usage's bars`); + + await page.goto("http://magpie.test/?view=routing"); + await page.locator(".rt-act .bar").first().waitFor(); + for (const sel of [".rt-accts li:not(.nobar) > .bar", ".rt-act > .bar"]) { + const r = await bars(page, sel); + assert.ok(r.seen.length > 0, `${at}: Routing draws ${sel}`); + for (const b of r.seen) assert.deepEqual(b, r.usage, `${at}: Routing's ${sel} is coloured as Usage's bars`); + } + // the account that answered is told by its frame and dot, its bar + // the same as the others' + assert.equal(await page.locator(".rt-accts li.on").count() > 0, true, `${at}: an account answered`); + assert.deepEqual(errors, [], at); + await ctx.close(); + } + }); + } +}