diff --git a/docs/subsystems/gui-shell.md b/docs/subsystems/gui-shell.md index 97f5fb982..f85e726c5 100644 --- a/docs/subsystems/gui-shell.md +++ b/docs/subsystems/gui-shell.md @@ -394,9 +394,17 @@ beside an empty Gemini one). A pooled window goes to its pool's column (`poolWord`, "Claude & GPT" under Claude), naming its window in the cell; any other window goes by its span (`spanOf`: "Weekly", "7 days" and "7d" share one column, headed by the name most rows use, a number on a tie), -and a window with no span by its name. It +and a window with no span by its name. A provider with several accounts +(grouped as its card groups them) is one row (jay68xu on X, #6740): its +name, how many accounts, and each column's share averaged over the accounts +that have one; its accounts are rows under it, without the provider's name +again, folded until the row is clicked. What is open is kept by provider in +localStorage `magpie.quotaTableOpen`. Sorting orders providers by that mean +(a name or account by its first account) and accounts within each. A column +no row fills is left out (Account when no account is named; the window +columns and Other only exist when some row has one). It scrolls in its own box at a narrow width, the subscription held at the left. -See `usage-quota-table.test.cjs`. +See `usage-quota-table.test.cjs` and `usage-table-groups.test.cjs`. ### The Sessions page's folders diff --git a/internal/gui/assets/app.css b/internal/gui/assets/app.css index b6440eda3..19eb54d43 100644 --- a/internal/gui/assets/app.css +++ b/internal/gui/assets/app.css @@ -1062,6 +1062,16 @@ body.panel .row.pref.flash { border-radius: 10px; } .qt-name { font-weight: 400; color: var(--muted); } .qt-reset { font-size: 10px; color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; } .qt .qt-msg { color: var(--muted); } +/* a provider's accounts under its row, folded until it is clicked (#6740) */ +.qt-group { cursor: pointer; } +.qt-fold { padding: 0; font: inherit; color: inherit; text-align: left; background: none; border: 0; cursor: pointer; } +.qt-fold .chev { display: inline-flex; width: 10px; color: var(--muted); } +.qt-fold[aria-expanded="true"] .chev { transform: rotate(90deg); } +.qt .qt-count { color: var(--muted); white-space: nowrap; text-transform: none; } +/* beside a provider's chevron, every row's icon and plan line up */ +.qt.grouped tr:not(.qt-group) .qt-id::before { content: ""; width: 10px; flex: none; } +.qt.grouped .plan { margin-left: 37px; } +.qt .qt-kid .qt-sub > .plan:first-child { margin-top: 0; } .subscription-card { padding: 10px 12px 11px; border: 1px solid var(--line); border-radius: 10px; background: var(--card); min-width: 0; } /* a card's captions (resets, the period, a curve's dates) read at 4.5:1 or better on the card, below its figures (Jiabubu on Discord): light's diff --git a/internal/gui/assets/quotatable.js b/internal/gui/assets/quotatable.js index 2f2cd2627..81b8fb763 100644 --- a/internal/gui/assets/quotatable.js +++ b/internal/gui/assets/quotatable.js @@ -15,8 +15,16 @@ // column, a family's in its own, and a window named for its span in the // span's, however the vendor words it ("Weekly", "7 days"). A cell holding // more than one, or one named otherwise than its column, names each. +// +// A provider with several accounts is one row (jay68xu on X, #6740): its +// name and how many, and each column's share averaged over its accounts +// (the pool as a whole). Its accounts are rows under it, folded until it +// is clicked, and what is open is remembered (magpie.quotaTableOpen, by +// provider), as the cards remember theirs. A column with nothing in any +// row (Account, when no row names one) is left out. const TABLE_WINDOWS = 4; let sorted = null; // { key, down } +const opened = new Set(kept("quotaTableOpen", [])); // the figure a window sorts and bars by: the share as the page shows it // (used, or left), -1 when it has none @@ -84,16 +92,33 @@ function quotaTable(subs) { : key === "acct" ? (r.sub.alias || r.sub.user || "").toLowerCase() || null : Math.max(-1, ...inCol(r.ws, key).map(share)); if (sorted && sorted.key !== "sub" && sorted.key !== "acct" && !names.includes(sorted.key)) sorted = null; - const list = sorted ? [...rows].sort((a, b) => { - const x = value(a, sorted.key), y = value(b, sorted.key); + const by = (val) => (a, b) => { + const x = val(a), y = val(b); // a row with nothing here stays below the rest either way const none = (v) => v === null || v < 0; if (none(x) !== none(y)) return none(x) ? 1 : -1; return (x < y ? -1 : x > y ? 1 : 0) * (sorted.down ? -1 : 1) || a.order - b.order; - }) : rows; + }; + // a provider's accounts together, as its card has them + const groups = []; + for (const r of rows) { + const g = r.sub.user && groups.find((x) => x.rows[0].sub.user && x.rows[0].sub.provider === r.sub.provider); + if (g) g.rows.push(r); else groups.push({ order: r.order, rows: [r] }); + } + // a column's share over a provider's accounts: their mean, -1 when none has one + const mean = (g, key) => { + const v = g.rows.map((r) => value(r, key)).filter((x) => x >= 0); + return v.length ? Math.round(v.reduce((a, b) => a + b, 0) / v.length) : -1; + }; + if (sorted) { + for (const g of groups) g.rows.sort(by((r) => value(r, sorted.key))); + groups.sort(by((g) => g.rows.length > 1 && sorted.key !== "sub" && sorted.key !== "acct" ? mean(g, sorted.key) : value(g.rows[0], sorted.key))); + } + const whos = new Map(rows.map((r) => [r, quotaWho(r.sub)])); + const acct = [...whos.values()].some((x) => x.length); const wrap = el("div", "qt-wrap"); - const table = el("table", "qt"); + const table = el("table", groups.some((g) => g.rows.length > 1) ? "qt grouped" : "qt"); const head = el("tr"); const th = (key, label, cls = "") => { const c = el("th", cls); @@ -114,51 +139,98 @@ function quotaTable(subs) { c.append(b); return c; }; - head.append(th("sub", t("Subscription"), "qt-sub"), th("acct", t("Account"), "qt-acct")); + head.append(th("sub", t("Subscription"), "qt-sub")); + if (acct) head.append(th("acct", t("Account"), "qt-acct")); for (const n of names) head.append(th(n, t(headOf(n)), "qt-w")); if (more) head.append(el("th", "qt-w", t("Other"))); table.append(el("thead", "", head)); const body = el("tbody"); - for (const { sub, ws } of list) { - const tr = el("tr"); - tr.dataset.card = trayCardID(sub); - tr.dataset.provider = sub.provider; - const s = el("td", "qt-sub"); - const who = el("div", "qt-id"); - who.append(icon(sub.icon), el("b", "", quotaName(sub))); - s.append(who); - if (sub.plan || sub.until) s.append(planSpan(sub)); - const a = el("td", "qt-acct"); - const name = el("div", "subscription-account"); - name.append(...quotaWho(sub)); - a.append(name); - tr.append(s, a); - const cols = names.length + (more ? 1 : 0); - if (sub.error) { - const e = el("td", "qt-msg", quotaError(sub.error, sub)); - e.colSpan = cols; - e.title = sub.error; - tr.append(e); - } else { - if (bare({ sub, ws })) { - const b = el("td", "qt-msg", t("{n} left", { n: sub.balance })); - b.colSpan = cols; - tr.append(b); - } else for (const n of names) { - const here = inCol(ws, n); - const c = el("td", "qt-w"); - for (const w of here) c.append(windowCell(w, here.length > 1 || w.name !== headOf(n))); + const cols = names.length + (more ? 1 : 0); + for (const g of groups) { + const several = g.rows.length > 1; + if (several) { + const p = g.rows[0].sub.provider, open = opened.has(p); + const tr = el("tr", "qt-group"); + tr.dataset.provider = p; + const s = el("td", "qt-sub"); + const b = el("button", "qt-id qt-fold"); + b.type = "button"; + b.setAttribute("aria-expanded", String(open)); + b.append(el("span", "chev", svg(CHEV_R, 10, 1.6)), icon(g.rows[0].sub.icon), el("b", "", quotaName(g.rows[0].sub))); + s.append(b); + tr.append(s); + const n = t("{n} accounts", { n: g.rows.length }); + if (acct) tr.append(el("td", "qt-acct qt-count", n)); + else s.append(el("span", "plan qt-count", n)); + for (const k of names) { + const c = el("td", "qt-w"), v = mean(g, k); + if (v >= 0) { + const box = el("div", "qt-win"), track = el("div", "quota-track"), fill = el("i"); + fill.style.width = v + "%"; + track.append(fill); + box.append(el("div", "qt-fig", el("span", "qt-n", v + "%")), track); + c.append(box); + } tr.append(c); } - if (more && !bare({ sub, ws })) { - const o = el("td", "qt-w qt-other"); - for (const w of extra({ ws })) o.append(windowCell(w, true)); - if (sub.balance) o.append(el("div", "qt-fig", t("{n} left", { n: sub.balance }))); - tr.append(o); + if (more) tr.append(el("td", "qt-w qt-other")); + tr.onclick = () => { + const back = document.activeElement === b; + if (open) opened.delete(p); else opened.add(p); + keep("quotaTableOpen", [...opened]); + renderQuotas(); + backToReader($("#view-usage")); + if (back) $(`.qt-group[data-provider="${CSS.escape(p)}"] .qt-fold`)?.focus({ preventScroll: true }); + }; + body.append(tr); + if (!open) continue; + } + for (const r of g.rows) { + const { sub, ws } = r; + const tr = el("tr", several ? "qt-kid" : ""); + tr.dataset.card = trayCardID(sub); + tr.dataset.provider = sub.provider; + const s = el("td", "qt-sub"); + if (!several) { + const who = el("div", "qt-id"); + who.append(icon(sub.icon), el("b", "", quotaName(sub))); + s.append(who); } + if (sub.plan || sub.until) s.append(planSpan(sub)); + tr.append(s); + if (acct) { + const a = el("td", "qt-acct"); + const name = el("div", "subscription-account"); + name.append(...whos.get(r)); + a.append(name); + tr.append(a); + } + if (sub.error) { + const e = el("td", "qt-msg", quotaError(sub.error, sub)); + e.colSpan = cols; + e.title = sub.error; + tr.append(e); + } else { + if (bare({ sub, ws })) { + const b = el("td", "qt-msg", t("{n} left", { n: sub.balance })); + b.colSpan = cols; + tr.append(b); + } else for (const n of names) { + const here = inCol(ws, n); + const c = el("td", "qt-w"); + for (const w of here) c.append(windowCell(w, here.length > 1 || w.name !== headOf(n))); + tr.append(c); + } + if (more && !bare({ sub, ws })) { + const o = el("td", "qt-w qt-other"); + for (const w of extra({ ws })) o.append(windowCell(w, true)); + if (sub.balance) o.append(el("div", "qt-fig", t("{n} left", { n: sub.balance }))); + tr.append(o); + } + } + body.append(tr); } - body.append(tr); } table.append(body); wrap.append(table); diff --git a/internal/gui/tests/usage-account-subcards.test.cjs b/internal/gui/tests/usage-account-subcards.test.cjs index 9a54f30fa..b245722bd 100644 --- a/internal/gui/tests/usage-account-subcards.test.cjs +++ b/internal/gui/tests/usage-account-subcards.test.cjs @@ -137,6 +137,8 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", // --- the table await page.locator("#quotaView .opt", { hasText: TABLE[lang] }).click(); await page.waitForSelector("#subscriptionUsage .qt tbody tr"); + // a provider's accounts fold under its row (#6740): open each + while (await page.locator('.qt-fold[aria-expanded="false"]').count()) await page.locator('.qt-fold[aria-expanded="false"]').first().click(); const table = await page.evaluate(() => { const heads = [...document.querySelectorAll(".qt thead th")].map((x) => x.textContent.replace(/[↑↓]/g, "").trim()); const rows = Object.fromEntries([...document.querySelectorAll(".qt tbody tr")].map((tr) => [ @@ -167,7 +169,8 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", } // sorting by the Gemini column reads the pooled row by its fuller window await page.locator(".qt thead th .qt-sort", { hasText: "Gemini" }).click(); - const firstRow = await page.$eval(".qt tbody tr:nth-child(1) .qt-acct", (x) => x.textContent.trim()); + assert.equal(await page.$eval(".qt tbody tr:nth-child(1)", (x) => x.className + " " + x.dataset.provider), "qt-group antigravity", "the provider with Gemini first"); + const firstRow = await page.$eval(".qt tbody tr:nth-child(2) .qt-acct", (x) => x.textContent.trim()); assert.equal(firstRow, "bare1@example.com", "40% before the pooled row's 23%"); if (shots) await page.screenshot({ path: path.join(shots, `table-${engine}-${lang}-${width}.png`), fullPage: true }); diff --git a/internal/gui/tests/usage-quota-table.test.cjs b/internal/gui/tests/usage-quota-table.test.cjs index 1c19dde72..adbdb8be0 100644 --- a/internal/gui/tests/usage-quota-table.test.cjs +++ b/internal/gui/tests/usage-quota-table.test.cjs @@ -82,14 +82,22 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", await page.locator("#quotaView .opt", { hasText: TABLE[lang] }).click(); await page.waitForSelector("#subscriptionUsage .qt tbody tr"); assert.equal(await page.locator("#subscriptionUsage .subscription-card").count(), 0); - // a row an account (a card's several accounts are rows too) + // a row a provider, its several accounts folded under it (#6740) const accounts = quotas.length; - assert.equal(await page.locator(".qt tbody tr").count(), accounts); + assert.equal(await page.locator(".qt tbody tr").count(), 7); + assert.equal(await page.locator(".qt tbody tr.qt-group").count(), 3); const heads = await page.$$eval(".qt thead th", (ths) => ths.map((x) => x.textContent.trim())); assert.equal(heads.length, 2 + 4 + 1, `${heads}`); // the cards' figures, the same readings - const row = await page.$eval('.qt tbody tr:nth-child(1)', (tr) => tr.innerText.replace(/\s+/g, " ")); - assert.match(row, /Codex/); + const nth = (n) => page.$eval(`.qt tbody tr:nth-child(${n})`, (tr) => tr.innerText.replace(/\s+/g, " ")); + // Codex's five accounts: their windows' mean share + const group = await nth(1); + assert.match(group, /Codex/); + assert.match(group, /34%/); + assert.match(group, /45%/); + while (await page.locator('.qt-fold[aria-expanded="false"]').count()) await page.locator('.qt-fold[aria-expanded="false"]').first().click(); + assert.equal(await page.locator(".qt tbody tr").count(), accounts + 3); + const row = await nth(2); assert.match(row, /a@example\.com/); assert.match(row, /20%/); assert.match(row, /40%/); @@ -104,22 +112,28 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", await page.reload(); await page.waitForSelector("#subscriptionUsage .qt tbody tr"); assert.equal(await page.locator("#quotaView .opt.on").innerText(), TABLE[lang]); + assert.equal(await page.locator(".qt tbody tr").count(), accounts + 3, "the open providers stay open"); // sorted by a window: the most used first, again the least, then the cards' order - const first = () => page.$eval(".qt tbody tr:nth-child(1)", (tr) => tr.innerText.replace(/\s+/g, " ")); + // a provider by its accounts' mean, its accounts among themselves + const first = () => nth(1); const fiveHours = page.locator(".qt thead th.qt-w .qt-sort").first(); await fiveHours.click(); - assert.match(await first(), /b@example\.com/); + assert.match(await first(), /j@example\.com/, "Z.ai's 64% over Codex's 34%"); + assert.match(await nth(2), /Codex/); + assert.match(await nth(3), /b@example\.com/); assert.equal(await page.locator('.qt thead th[aria-sort="descending"]').count(), 1); await page.locator(".qt thead th.qt-w .qt-sort").first().click(); - assert.match(await first(), /l@example\.com/, "ascending: unused first"); + assert.match(await first(), /Claude Code/, "ascending: Claude Code's 31% first"); + assert.match(await nth(2), /g@example\.com/); assert.equal(await page.locator('.qt thead th[aria-sort="ascending"]').count(), 1); await page.locator(".qt thead th.qt-w .qt-sort").first().click(); - assert.match(await first(), /a@example\.com/, "back to the cards' order"); + assert.match(await first(), /Codex/, "back to the cards' order"); + assert.match(await nth(2), /a@example\.com/); assert.equal(await page.locator('.qt thead th[aria-sort]:not([aria-sort="none"])').count(), 0); // by account, A first await page.locator(".qt thead th.qt-acct .qt-sort").click(); - assert.match(await first(), /a@example\.com/); + assert.match(await nth(2), /a@example\.com/); // the page itself never scrolls sideways; the table scrolls in its box const over = await page.evaluate(() => document.documentElement.scrollWidth - document.documentElement.clientWidth); @@ -142,11 +156,12 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", t.after(() => browser.close()); const page = await (await browser.newContext({ viewport: { width: 1100, height: 800 }, reducedMotion: "reduce" })).newPage(); page.setDefaultTimeout(5000); - await page.addInitScript(() => localStorage.setItem("magpie.quotaView", '"table"')); + await page.addInitScript(() => { localStorage.setItem("magpie.quotaView", '"table"'); localStorage.setItem("magpie.quotaTableOpen", '["codex"]'); }); await page.route("**/*", serve("en", true)); await page.goto("http://magpie.test/?view=usage"); await page.waitForSelector("#subscriptionUsage .qt tbody tr"); - const row = await page.$eval(".qt tbody tr:nth-child(1)", (tr) => tr.innerText.replace(/\s+/g, " ")); + assert.match(await page.$eval(".qt tbody tr:nth-child(1)", (tr) => tr.innerText.replace(/\s+/g, " ")), /66%/, "Codex's accounts' mean left"); + const row = await page.$eval(".qt tbody tr:nth-child(2)", (tr) => tr.innerText.replace(/\s+/g, " ")); assert.match(row, /80%/); assert.match(row, /60%/); }); diff --git a/internal/gui/tests/usage-table-acct-lines.test.cjs b/internal/gui/tests/usage-table-acct-lines.test.cjs index 84241d922..0042d497f 100644 --- a/internal/gui/tests/usage-table-acct-lines.test.cjs +++ b/internal/gui/tests/usage-table-acct-lines.test.cjs @@ -59,6 +59,9 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", await page.waitForSelector("#quotaView .opt"); await page.locator("#quotaView .opt", { hasText: TABLE[lang] }).click(); await page.waitForSelector("#subscriptionUsage .qt tbody tr"); + // Codex's two accounts are under its row (#6740) + await page.locator('.qt-group[data-provider="codex"]').click(); + await page.waitForSelector(".qt tbody tr.qt-kid"); const cell = (user) => page.evaluate((user) => { const tr = [...document.querySelectorAll(".qt tbody tr")].find((r) => r.querySelector(".qt-acct")?.textContent.includes(user)); diff --git a/internal/gui/tests/usage-table-groups.test.cjs b/internal/gui/tests/usage-table-groups.test.cjs new file mode 100644 index 000000000..95660bca2 --- /dev/null +++ b/internal/gui/tests/usage-table-groups.test.cjs @@ -0,0 +1,139 @@ +// Run with Node's test runner and Playwright on the module path; see README.md. +// jay68xu on X (#6740), on the Usage page's table: a provider's several +// accounts were rows of their own, its icon and name again on each, and +// columns no row filled took room. A provider with several accounts is now +// one row, its name, how many and each column's share over them, its +// accounts folded under it until it is clicked; what is open is kept across +// a reload, by provider. A provider with one account is its one row, as +// before. A column with nothing in any row (Account, when no account is +// named) is left out. The clicks don't move the page, and Enter on the +// row's button keeps the focus on it. Both engines, 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 at = (h) => new Date(Date.now() + h * 36e5).toISOString(); +const w = (name, used, h) => ({ name, used, resetsAt: at(h) }); +const ZC = { provider: "zcode", name: "ZCode", icon: "generic", plan: "Pro" }; +const accounts = [ + ...[["a", 10, 20], ["b", 30, 40], ["c", 80, 60]].map(([u, five, week]) => ({ ...ZC, user: u + "@example.com", windows: [w("5 hours", five, 2), w("7 days", week, 90)] })), + { provider: "kimi", name: "Kimi", icon: "kimi", user: "k@example.com", windows: [w("5 hours", 50, 1), w("7 days", 70, 70)] }, + { provider: "qoder", name: "Qoder", icon: "generic", user: "q1@example.com", windows: [w("5 hours", 0, 3)] }, + { provider: "qoder", name: "Qoder", icon: "generic", user: "q2@example.com", windows: [], error: "unauthorized" }, +]; +// no account named anywhere: keys and balances +const unnamed = [ + { provider: "deepseek", name: "DeepSeek", icon: "deepseek", windows: [], balance: "¥17.41" }, + { provider: "zai", name: "Z.ai", icon: "zai", windows: [w("5 hours", 64, 1), w("7 days", 12, 70)] }, +]; + +function serve(lang, quotas) { + const settings = { theme: "light", lang, tray: "panel", quotaLeft: false, currency: "usd" }; + 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") return json(quotas); + 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 COUNT = { en: "3 accounts", zh: "3 个账号", "zh-TW": "3 個帳號", ja: "3 個のアカウント", de: "3 Konten" }; + +for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { + for (const lang of ["en", "zh", "zh-TW", "ja", "de"]) { + for (const width of [1100, 420]) { + test(`${engine} ${lang} ${width}px: a provider's accounts fold under its row`, 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, height: 800 }, reducedMotion: "reduce" })).newPage(); + page.setDefaultTimeout(5000); + page.setDefaultNavigationTimeout(60000); + const errors = []; + page.on("pageerror", (e) => errors.push(e.message)); + await page.addInitScript(() => localStorage.setItem("magpie.quotaView", '"table"')); + await page.route("**/*", serve(lang, accounts)); + await page.goto("http://magpie.test/?view=usage"); + await page.waitForSelector("#subscriptionUsage .qt tbody tr"); + const rows = () => page.$$eval(".qt tbody tr", (trs) => trs.map((tr) => ({ cls: tr.className, p: tr.dataset.provider, text: tr.innerText.replace(/\s+/g, " ").trim() }))); + + // folded: a row a provider, one account's provider its one row + let r = await rows(); + assert.deepEqual(r.map((x) => [x.cls, x.p]), [["qt-group", "zcode"], ["", "kimi"], ["qt-group", "qoder"]]); + assert.ok(r[0].text.includes("ZCode") && r[0].text.includes(COUNT[lang]), r[0].text); + // each column's share over the accounts: (10+30+80)/3, (20+40+60)/3 + assert.match(r[0].text, /40%.*40%/); + assert.doesNotMatch(r[0].text, /@example/); + assert.match(r[1].text, /k@example\.com/); + // a failed read counts for nothing in the mean + assert.match(r[2].text, /\b0%/); + assert.equal(await page.locator('.qt-group[data-provider="zcode"] .qt-fold').getAttribute("aria-expanded"), "false"); + assert.equal(await page.evaluate(() => localStorage.getItem("magpie.quotaTableOpen")), null); + + // a click opens it in place, the page not moving + const at0 = await page.evaluate(() => [scrollY, document.querySelector("#view-usage")?.scrollTop ?? 0]); + await page.locator('.qt-group[data-provider="zcode"]').click(); + r = await rows(); + assert.deepEqual(r.slice(0, 5).map((x) => [x.cls, x.p]), [["qt-group", "zcode"], ["qt-kid", "zcode"], ["qt-kid", "zcode"], ["qt-kid", "zcode"], ["", "kimi"]]); + assert.match(r[1].text, /a@example\.com.*10%.*20%/); + assert.doesNotMatch(r[1].text, /ZCode/, "the provider's name isn't said again"); + assert.equal(await page.locator('.qt-group[data-provider="zcode"] .qt-fold').getAttribute("aria-expanded"), "true"); + assert.deepEqual(await page.evaluate(() => [scrollY, document.querySelector("#view-usage")?.scrollTop ?? 0]), at0, "the page didn't move"); + + // kept across a reload + await page.reload(); + await page.waitForSelector("#subscriptionUsage .qt tbody tr.qt-kid"); + assert.equal(await page.locator(".qt tbody tr.qt-kid").count(), 3); + assert.equal(await page.evaluate(() => localStorage.getItem("magpie.quotaTableOpen")), '["zcode"]'); + + // the keyboard: Enter on the row's button folds it, the focus stays + await page.locator('.qt-group[data-provider="zcode"] .qt-fold').focus(); + await page.keyboard.press("Enter"); + assert.equal(await page.locator(".qt tbody tr.qt-kid").count(), 0); + assert.equal(await page.evaluate(() => document.activeElement?.closest(".qt-group")?.dataset.provider), "zcode"); + await page.keyboard.press("Space"); + assert.equal(await page.locator(".qt tbody tr.qt-kid").count(), 3); + + // the narrow table scrolls in its own box, never the page + const over = await page.evaluate(() => document.documentElement.scrollWidth - document.documentElement.clientWidth); + assert.ok(over <= 0, `the page scrolls sideways by ${over}px`); + assert.deepEqual(errors, []); + }); + + test(`${engine} ${lang} ${width}px: a column no row fills is left out`, 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, height: 800 }, reducedMotion: "reduce" })).newPage(); + page.setDefaultTimeout(5000); + page.setDefaultNavigationTimeout(60000); + const errors = []; + page.on("pageerror", (e) => errors.push(e.message)); + await page.addInitScript(() => localStorage.setItem("magpie.quotaView", '"table"')); + await page.route("**/*", serve(lang, unnamed)); + await page.goto("http://magpie.test/?view=usage"); + await page.waitForSelector("#subscriptionUsage .qt tbody tr"); + const account = await page.evaluate(() => t("Account")); + const heads = await page.$$eval(".qt thead th", (ths) => ths.map((x) => x.textContent.replace(/[↑↓]/g, "").trim())); + assert.ok(!heads.includes(account), `${heads}`); + assert.equal(await page.locator(".qt .qt-acct").count(), 0); + // every row has a cell under each head + const cells = await page.$$eval(".qt tbody tr", (trs) => trs.map((tr) => [...tr.children].reduce((n, td) => n + td.colSpan, 0))); + assert.deepEqual(cells, cells.map(() => heads.length)); + assert.deepEqual(errors, []); + }); + } + } +}