Files
magpie/internal/gui/tests/usage-chart-axis.test.cjs
T
yetone 18e82adc62 gui: the window's pages down a sidebar in groups, the open page's tabs under it, and a button to have the header's tabs instead
From 1024 points wide (SIDE_AT) the window lays its pages down the left
as a Mac app's sidebar: Work (Agents, Sessions, Library), Models
(Providers, Routing, Gateway), Insights (Usage), Extend (Plugins), with
Settings and the gateway's address at the foot. The open page's own tabs
(Usage, Library, Plugins, Settings) are listed under it, opening and
folding on an iOS-like spring, and the page's strip is hidden. The header
becomes a breadcrumb, and pages whose head held only tools have them in
the title bar. Graphite: the open page is in the text's ink, not purple.
A pressed row darkens and never shrinks. The selection under the tabs
sets off as the row is pressed, on a 0.3s curve (it felt behind the
finger), and is the list's only shape: a sub-row hovered or pressed has
no ground of its own, which showed a block under the finger before the
selection slid there. The page's tab is clicked at once, so what is
typed next goes to it.

The title bar's button (or ⌃⌘S, Ctrl+Shift+S), there at every width,
picks the sidebar or the header's tabs. The choice is kept in
localStorage magpie.navLayout (side or top) at every width, so it is laid
out from the first frame; picked in a narrow window the sidebar is 184
points under 860. The two trade places in
a view transition: the sidebar slides, each tab flies to its place, and
the page slides as one. Without view transitions or under reduced
motion, the change is at once.

With nothing picked, narrower than 1024 the page beside a sidebar would
be narrower than Usage's requests table needs, so the header's tabs come
back. A resize
drops what a click held (heldWidth). Usage's KPI strip goes two to a line
by the room the page gives it (a container query), and Japanese counts
wrap there. Tests whose window is 1024+ were updated for the sidebar,
and docs/subsystems/gui-shell.md says how it works.

Run: sidebar.test.cjs and side-toggle.test.cjs 16/16 in chromium and
webkit, in en, zh, zh-TW, ja and de. Each was broken by hand (press
shrink, address cut, press glide, sub-row ground, view transition, kept
choice) and went red. Every page at 560 points with the sidebar picked,
zh and de, both engines: nothing scrolls sideways. session-identity's
ledger row sits under the footer beside the sidebar; WebKit's click
doesn't scroll to it, so the test wheels it up first. Animation frames were checked from a recorded video. The 105
affected GUI files in both engines; their reds were rerun alone and fixed
(filter-ime: the sub-row's click now at once; session-identity, above) or
fail the same on main (tray-logos). The full GUI suite in chromium, on
the rebase onto d5cae267a, was cut short at 352 tests on the owner's
"ship": 6 reds (account-alias, account-models, codex-auto-review,
codex-credits, context-text) each pass alone, here and on main d5cae267a,
so they are timing under the full run's load and are to be filed. After
rebasing onto e4e22b23e: sidebar, side-toggle, session-identity,
filter-ime, library-instr-busy, usage-speed-count and view-scrollbar,
50/50 in chromium and webkit. go build ./... passes.
2026-10-12 00:41:34 +08:00

160 lines
9.8 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// The usage chart's side labels (the window's Requests tab and the tray
// panel's Usage tab) keep the card's padding: the plot starts where the widest
// label ends, so "8000 万", "125 亿", "¥9000" stand as far in from the card's
// edge as its tabs and its ranking do, and none is cut off or runs past the
// card — for tokens, cost in dollars and in yuan, and requests, small and
// large, wide and narrow. English and Chinese; no backend, 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 midnight = new Date();
midnight.setHours(0, 0, 0, 0);
// a day by the hour of one provider, scaled: tokens, cost and calls an hour
function page(scale) {
const per = { tokens: 7e7 * scale, cost: 1.2 * scale, calls: Math.max(1, Math.round(900 * scale)) };
const split = (tokens) => ({ input: tokens * 0.01, output: tokens * 0.005, cache_write: tokens * 0.05, cache_read: tokens * 0.935 });
const series = Array.from({ length: 24 }, (_, h) => {
const on = h >= 2 ? 1 : 0;
const part = { calls: per.calls * on, tokens: per.tokens * on, cost: per.cost * on };
const by = { provider: { zcode: part }, agent: { claude: part }, model: { "glm-5": part } };
return { label: String(h), time: new Date(midnight.getTime() + h * 3600e3).toISOString(), calls: part.calls, errors: 0, ...split(part.tokens), cost: part.cost, by };
});
const sum = (k) => series.reduce((a, p) => a + p[k], 0);
const tot = { calls: sum("calls"), errors: 0, input: sum("input"), output: sum("output"), cache_write: sum("cache_write"), cache_read: sum("cache_read"), reasoning: 0, cost: sum("cost"), unpriced: 0 };
const share = (id, name, icon) => ({ id, name, icon, ...tot });
return {
period: "today", rows: [], offset: 0, total: 1, ...tot, bucket: "hour", series,
by: { provider: [share("zcode", "ZCode", "generic")], agent: [share("claude", "Claude Code", "claudecode-color")], model: [share("glm-5", "glm-5", "")] },
agents: [{ id: "claude", name: "Claude Code", icon: "claudecode-color" }], providers: [{ id: "zcode", name: "ZCode", icon: "generic" }],
};
}
function serve(lang, currency, scale, web) {
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:${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", currency }, fx: { rate: 7.2, at: new Date().toISOString() } });
if (url.pathname === "/api/usage/requests") return json(page(scale));
if (url.pathname === "/api/usage/quotas") return json([]);
if (url.pathname === "/api/usage") return json({ calls: 1, errors: 0, input: 1, output: 1, cache_read: 0, cache_write: 0, reasoning: 0, unpriced: 0, cost: 1, bucket: "day", series: [], agents: [], models: [] });
if (url.pathname === "/api/sessions") return json({ sessions: [], dirs: [] });
if (url.pathname === "/api/sessions/stats") return json({ from: "", to: "", days: [], agents: {} });
if (url.pathname === "/api/groups") return json({ groups: [], models: [] });
if (url.pathname === "/api/plugins") return json({ plugins: [] });
if (url.pathname === "/api/gateway/trace") return json({ mine: true, now: new Date().toISOString(), seq: 0, totals: { requests: 0, rerouted: 0, errors: 0 }, routes: [] });
if (url.pathname === "/api/gateway/history") return json({ cut: false, days: [], routes: [] });
if (url.pathname === "/api/providers") return json({ providers: [], gateway: { running: true, window: 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)];
await route.fulfill({ body: await fs.readFile(file), contentType });
};
}
// where the side labels are against their card: each label's box, the card's
// padded inside, where the plot starts (the grid's left end), and whether
// anything in the card runs past it
async function measure(p, card) {
return p.locator(card).first().evaluate((c) => {
const r = c.getBoundingClientRect(), cs = getComputedStyle(c);
const inner = { left: r.left + parseFloat(cs.paddingLeft) + parseFloat(cs.borderLeftWidth), right: r.right - parseFloat(cs.paddingRight) - parseFloat(cs.borderRightWidth) };
const svg = c.querySelector(".led-chart svg");
const labs = [...svg.querySelectorAll("text.axis[text-anchor=end]")].map((x) => ({ text: x.textContent, ...x.getBoundingClientRect().toJSON() }));
const grid = Math.min(...[...svg.querySelectorAll(".grid")].map((g) => g.getBoundingClientRect().left));
const out = [...c.querySelectorAll("svg text, svg rect.col")].filter((e) => { const b = e.getBoundingClientRect(); return b.width && (b.left < r.left - 0.5 || b.right > r.right + 0.5); }).map((e) => e.textContent || e.tagName);
const ref = c.querySelector(".led-bar .segs, .led-bar").getBoundingClientRect().left;
return { inner, labs, grid, out, ref, sideways: document.documentElement.scrollWidth > innerWidth + 1 };
});
}
function check(m, what) {
assert.equal(m.labs.length, 5, what + ": five side labels");
for (const l of m.labs) {
assert(l.width > 0, `${what}: "${l.text}" is drawn`);
assert(l.left >= m.inner.left - 0.5, `${what}: "${l.text}" starts at ${l.left.toFixed(1)}, left of the card's padding at ${m.inner.left.toFixed(1)}`);
assert(l.right <= m.grid + 0.5, `${what}: "${l.text}" runs into the plot (${l.right.toFixed(1)} > ${m.grid.toFixed(1)})`);
}
// as far in as the tabs above it
const widest = Math.min(...m.labs.map((l) => l.left));
assert(widest >= m.ref - 1.5, `${what}: the widest label at ${widest.toFixed(1)}, the tabs at ${m.ref.toFixed(1)}`);
assert.deepEqual(m.out, [], what + ": past the card");
assert(!m.sideways, what + ": the page scrolls sideways");
}
const CASES = [
// scale, currency: tokens 8000 万 / 80M, cost $1.5 → ¥10, requests 1000
[1, "usd"], [1, "cny"],
// large: 125 亿 tokens, $250 / ¥2000 an hour, 2 万 requests
[150, "usd"], [150, "cny"],
// small: 5000 tokens, cents
[0.00007, "usd"],
];
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
test(engine + ": the usage chart's side labels keep the card's padding", async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
const shots = process.env.ARTIFACT_DIR;
if (shots) await fs.mkdir(shots, { recursive: true });
for (const lang of ["en", "zh"]) {
await t.test(lang + ", the window's Requests tab", async () => {
for (const [scale, currency] of CASES) {
for (const width of [1180, 560]) {
const ctx = await browser.newContext({ viewport: { width, height: 760 }, reducedMotion: "reduce" });
const p = await ctx.newPage();
const errors = [];
p.setDefaultTimeout(5000);
p.on("pageerror", (e) => errors.push(e.message));
await p.route("**/*", serve(lang, currency, scale, false));
await p.goto("http://magpie.test/");
await p.locator('[data-view="usage"]').first().click();
await p.locator(":is(#usageTab .opt, .nav-s):visible").nth(1).click();
await p.locator("#ledChart svg").waitFor();
for (let i = 0; i < 3; i++) {
await p.locator("#ledMetric .opt").nth(i).click();
await p.locator("#ledChart svg").waitFor();
check(await measure(p, ".led-trend"), `${lang} ${width} ${scale} ${currency} metric ${i}`);
}
if (shots && scale === 1) await p.locator(".led-trend").screenshot({ path: path.join(shots, `axis-${engine}-${lang}-${currency}-${width}.png`) });
assert.deepEqual(errors, []);
await ctx.close();
}
}
});
await t.test(lang + ", the tray panel", async () => {
for (const [scale, currency] of CASES) {
for (const width of [440, 340]) {
const ctx = await browser.newContext({ viewport: { width, height: 560 }, reducedMotion: "reduce" });
const p = await ctx.newPage();
const errors = [];
p.setDefaultTimeout(5000);
p.on("pageerror", (e) => errors.push(e.message));
await p.route("**/*", serve(lang, currency, scale, true));
await p.goto("http://magpie.test/?mode=panel");
await p.locator('[data-ptab="stats"]').click();
await p.locator("#panelUsage .pu-card .led-chart svg").waitFor();
for (let i = 0; i < 3; i++) {
await p.locator("#panelUsage .pu-card .led-bar .opt").nth(i).click();
await p.locator("#panelUsage .pu-card .led-chart svg").waitFor();
check(await measure(p, "#panelUsage .pu-card"), `panel ${lang} ${width} ${scale} ${currency} metric ${i}`);
}
if (shots && scale === 1) await p.locator("#panelUsage .pu-card").screenshot({ path: path.join(shots, `axis-${engine}-${lang}-${currency}-panel-${width}.png`) });
assert.deepEqual(errors, []);
await ctx.close();
}
}
});
}
});
}