Files
magpie/internal/gui/tests/usage-table-acct-lines.test.cjs
T
yetone 5bccfb4ef2 usage: a table's provider with several accounts is one folded row; an empty Account column is left out (#6740, jay68xu on X)
jay68xu on X: the Usage page gets hard to scan with many providers and
accounts. This change covers the table view only, as the issue's comment asks.

In the Allowances table (Usage > Overview, Table), a provider with
several accounts is now one row. The row shows the chevron, the
provider's icon and name, "N accounts" (a string every language already
has), and each window column's mean share over the accounts. A failed
read counts for nothing in the mean. The provider's accounts sit under
it, folded until the row (or its button, with Enter or Space) is
clicked. Each provider's open state is kept in localStorage
magpie.quotaTableOpen, the way the cards' folds are kept. A provider
with one account is one row, as before. Sorting orders accounts within
a provider, then providers by their mean (window columns) or by their
first row. The Account column is left out when no row names an account.
Window columns and Other already appear only when some row has one.
There is no motion: a toggle redraws the table, and the chevron turns.
app.js is untouched (quotatable.js loads lazily), and there are no new
strings.

Run at this tree (base origin/main d99762075):
- usage-table-groups.test.cjs (new, 40 tests: both engines x en, zh,
  zh-TW, ja, de x 1100px and 420px): all 20 chromium cases are red on
  the old quotatable.js and app.css (0 pass, 20 fail).
- The 4 table files (usage-table-groups, usage-quota-table,
  usage-table-acct-lines, usage-account-subcards), both engines, on
  mote: 102 pass, 0 fail.
- Every usage-*.test.cjs plus i18n-keys and i18n-en-no-cjk, both
  engines, on mote: 1597 pass, 106 fail. All of the reds are in 11
  files that don't touch the table. Run alone, those 11 files give the
  same not-ok list on this tree and on origin/main d99762075 (diffed).
  Their issues are #3414, #6003, #6294, #3415, #2163, #6295, #4263,
  #5048, #6499 and #5834. The three files with no issue were filed as
  #6743 (usage-chart-line, usage-upload, usage-quiet-refresh).
- go test ./internal/gui/ on mica: ok (153.8s), which includes
  TestCoreScriptBudget. The budget was not raised.
- gofmt -l: clean (no Go changed).

Not tried: the real app, WebKitGTK on Linux, Windows, real accounts.
The issue's 代理出口 and 管理页 columns aren't magpie's. No GUI table
or i18n string has them.
2026-11-15 15:54:29 +08:00

113 lines
7.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Run with Node's test runner and Playwright on the module path; see README.md.
// normalcoder_ on X, on Usage › Overview's table: the account cell put a
// named account's name and its address side by side, so a long one was
// cut. The name is now on the first line and the address, the key plan it
// stands in for and its shared keys each on a line below, every one with
// the cell's whole width; the eye of a masked page stays on the first
// line. Both engines, every language, wide and narrow; the API is faked.
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 CX = { provider: "codex", name: "Codex", kind: "subscription", icon: "codex-color", plan: "Plus" };
const quotas = [
{ ...CX, alias: "Design team's seat", named: true, user: "design-team@example-company.com", windows: [w("5 hours", 20, 2), w("7 days", 40, 90)] },
{ ...CX, user: "plain@example.com", windows: [w("5 hours", 90, 2), w("7 days", 10, 90)] },
{ provider: "zai", name: "Z.ai", icon: "zai", alias: "Ops", named: true, user: "ops-lead@example.com", seat: "GLM Coding Max", windows: [w("5 hours", 64, 1), w("7 days", 12, 70)] },
];
function serve(lang) {
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 TABLE = { en: "Table", zh: "表格", "zh-TW": "表格", ja: "表", de: "Tabelle" };
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 table's account cell has its name above its address`, 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.route("**/*", serve(lang));
await page.goto("http://magpie.test/?view=usage");
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));
const box = tr.querySelector(".qt-acct .subscription-account");
const parts = [...box.children].filter((e) => e.offsetParent).map((e) => {
const r = e.getBoundingClientRect();
return { cls: e.className, text: e.textContent, top: r.top, bottom: r.bottom, left: r.left, cut: e.scrollWidth > e.clientWidth + 1 };
});
return { parts, height: box.getBoundingClientRect().height };
}, user);
const lines = (parts) => parts.map((p) => p.text);
// a named account: the name, then the address, a line each, from the same left
const named = await cell("design-team@example-company.com");
assert.deepEqual(lines(named.parts), ["Design team's seat", "design-team@example-company.com"]);
const [name, of] = named.parts;
assert.ok(of.top >= name.bottom - 1, `the address is under the name: ${name.bottom} / ${of.top}`);
assert.ok(Math.abs(of.left - name.left) <= 1, `both lines start at one left: ${name.left} / ${of.left}`);
assert.deepEqual(named.parts.filter((p) => p.cut).map((p) => p.text), [], "nothing in it is cut");
// a name, its address and the key plan it stands in for: three lines
const seat = await cell("ops-lead@example.com");
assert.equal(seat.parts.length, 3);
for (let i = 1; i < 3; i++) assert.ok(seat.parts[i].top >= seat.parts[i - 1].bottom - 1, `line ${i + 1} under line ${i}`);
assert.deepEqual(seat.parts.filter((p) => p.cut).map((p) => p.text), [], "nothing in it is cut");
// an account with no name: its one line, as before
const plain = await cell("plain@example.com");
assert.deepEqual(lines(plain.parts), ["plain@example.com"]);
assert.ok(plain.height < named.height - 4, `one line is shorter than two: ${plain.height} / ${named.height}`);
// masked: the eye beside the name, not on a line of its own
await page.evaluate(() => document.body.classList.add("masked"));
const eye = await page.evaluate(() => {
const tr = [...document.querySelectorAll(".qt tbody tr")].find((r) => r.textContent.includes("Design team's seat"));
const a = tr.querySelector(".quota-alias").getBoundingClientRect(), e = tr.querySelector(".acct-eye")?.getBoundingClientRect();
return e && { mid: (e.top + e.bottom) / 2, top: a.top, bottom: a.bottom, left: e.left, right: a.right };
});
assert.ok(eye, "the masked page shows the eye");
assert.ok(eye.mid >= eye.top - 2 && eye.mid <= eye.bottom + 2, `the eye is on the name's line: ${JSON.stringify(eye)}`);
const over = await page.evaluate(() => document.documentElement.scrollWidth - document.documentElement.clientWidth);
assert.ok(over <= 1, `the page doesn't scroll sideways: ${over}`);
assert.deepEqual(errors, []);
});
}
}
}