Files
magpie/internal/gui/tests/usage-ledger-fit.test.cjs
T
yetone 98c4b2e814 usage: a request opens on a page of its own
geesehowardt7 on X clicked a request on the Usage page to see where it
came from. The click on its time opened Routing, which they hadn't
asked for, and the row's details had no source address.

A row's click, or Enter or Space on it, now pushes that request's own
page (#ledReqPane, ledReqOpen/renderLedReq) over the list. The page
gives the model and status, then ledDetail: time, agent, source IP
(the previous commit's client_ip: loopback reads "This computer ·
127.0.0.1", a gateway row from before the field "Not recorded", rows
read from session files, other computers and magpie's own calls none),
requested, provider, account, sent, served, effort, then status,
error, duration, tokens, cost, credits, speed and the archive. Routing
is the View routing link in it, and the time cell no longer links
there. "Requests ‹" or Escape (not while a menu, dialog or field takes
it) goes back to the list where it was left, with the row focused
(ledReqClose). Leaving the Usage page closes it (ledLeave(true)), and
a refresh keeps it. The in-table expanding row (ledOpen, tr.led-detail)
and its narrow-window lc-opt copies are gone, since the page shows
every field at every width. It slides in 200ms with the ease-out
curve, and only fades with reduced motion. "Source IP" and "Not
recorded" are added in zh, zh-TW, ja and de. The 20 ledger test files
that opened a row's details now open the page. usage-request-page.test.cjs
covers the page, the address, the way back, focus and a 560px window.
docs/subsystems/gui-shell.md describes it.

Run at this commit's tree (rebased on origin/main b4a0a2a1a, with the
two commits beside it):
- mote (macOS 27): gofmt -l clean; go vet ./..., GOOS=linux -tags
  nogui go vet and GOOS=windows go vet ok. go test internal/gui (with
  TestCoreScriptBudget) ok in 164s, internal/usage, internal/usageapi,
  internal/cli and the root package ok, internal/gateway -run
  "Usage|ClientIP" ok, internal/provider -race -count=5 -run Workspace
  ok.
- WSL VM (magpie-desktop), whole suite through the suite lock at
  be9dbce02 plus this change: go test ./... -timeout 45m ok apart from
  the GTK packages, which need a display the VM run lacks (they pass
  on mote), TestGrokInstalled (#3548) and the provider workspace test
  the next commit fixes (#5800). GUI suite: 540 of 23297 red in 114
  files, WebKit timeouts mostly (#4121); those 114 files run again
  give 378 reds here and 365 at be9dbce02, the same files, and
  own-requests was the one this change broke (fixed: a row's click now
  opens the page, and its way back keeps the list's place).
- WSL VM, the 119 Usage and Routing GUI files at the rebased head,
  both engines: 21 red, 20 of them red the same at origin/main
  b4a0a2a1a: click-scroll (#4385), routing-cline-route and
  routing-upstream (#5156), usage-agents-hide (#2276, #3414),
  usage-heatmap (#4718), usage-ledger-ttft (#4489), usage-ledger-chart
  (#4263), usage-focus-steady (#3501), usage-fold (#3415, #2163),
  usage-ledger Requests (#3104, #5048), account-refresh-holds (#5814)
  and usage-speed-all-models (#5834, red since the 24 hours period).
  The 21st, usage-compact-counts chromium "zh 万 at 420px", is the
  flake in #5648 and passes 3 of 3 alone.
Again after rebasing on origin/main b3a57b3d8 (main's WebSocket row in
the ledger, 7c1dc198d): mote go vet, internal/gui, internal/usage,
internal/usageapi, internal/cli, the root package, internal/provider
and gateway -run "Usage|ClientIP|Socket|Memory" ok, apart from
TestCoreScriptBudget, red on main since b3a57b3d8 and fixed there by
4894332d7, which this is now rebased on; the VM's 119 files plus usage-ledger-bytes and
codex-app-back: 1708 of 1782 pass, no red that isn't in the list
above, usage-ledger-bytes passing with the request page.
Not tried: a request from another device over a real LAN share.
2026-11-10 04:44:13 +08:00

124 lines
8.6 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// The Usage page's Requests table fits a narrow window with the reporter's
// own rows (#860, huoranxuanyuan, a second round: at 1569px on Windows at
// 175%, about 900 CSS px, the hint said columns were left out and Speed and
// Status were still cut off). Their rows are Codex's through openai ·
// chatgpt.com with a Responses badge, one priced from another model, whose
// "Price reference" line widened the Cost column. Where leaving out Sent,
// Effort and the cache isn't enough, that line goes too (it is in the cost's
// tooltip and the row's details) and names are cut shorter; a window wide
// enough keeps it. Chromium and WebKit, English and Chinese; 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 reader = require("./reader.cjs");
const assets = path.resolve(__dirname, "../assets");
const now = Date.now();
const H = 3600e3;
const hour = (i) => new Date(Math.floor(now / H) * H - i * H).toISOString();
// the reporter's rows, from their screenshot
const R = (i, req, inn, out, ms, ttft, ref) => ({ route_id: 900 + i, t: new Date(now - (i + 1) * 60e3).toISOString(), agent: "codex", agentName: "Codex", icon: "codex-color",
provider: "openai", providerName: "openai", host: "chatgpt.com", ep: "/v1/responses", req, model: req, served: req, effort: "medium",
in: inn, out, cache_read: 110000, cache_write: 0, ms, ttft_ms: ttft, status: 200, rid: "r" + i, cost: 0.02, priced: true, ...(ref ? { pricing_model: ref } : {}) });
const ROWS = [R(0, "gpt-6.1-sol", 3660, 240, 14000, 11000), R(1, "gpt-6.1-sol", 4102, 145, 11000, 10000), R(2, "codex-auto-review", 4493, 134, 11000, 6400, "gpt-5.6-luna"), R(3, "gpt-6.1-sol", 1030, 93, 8900, 6900)];
const FAST = { id: "gpt-6.1-sol", calls: 3, input: 1, output: 478, cache_read: 0, cache_write: 0, cost: 0.06, timed: 3, ttft_ms: 27900, decode_ms: 6000, decode_out: 478 };
const SLOW = { id: "codex-auto-review", calls: 1, input: 1, output: 134, cache_read: 0, cache_write: 0, cost: 0.02, timed: 1, ttft_ms: 6400, decode_ms: 4600, decode_out: 134 };
const part = (x) => ({ calls: x.calls, tokens: x.output, cost: x.cost, timed: x.timed, ttft_ms: x.ttft_ms, decode_ms: x.decode_ms, decode_out: x.decode_out });
const sum = (k) => FAST[k] + SLOW[k];
function ledger(period) {
return {
period, rows: ROWS, offset: 0, total: ROWS.length, calls: ROWS.length, errors: 0,
input: sum("input"), output: sum("output"), cache_read: 0, cache_write: 0, cost: sum("cost"), unpriced: 0,
// 3665 tokens in 50.6 s after the first: 72 tok/s
timed: sum("timed"), ttft_ms: sum("ttft_ms"), decode_ms: sum("decode_ms"), decode_out: sum("decode_out"),
by: { model: [FAST, SLOW] }, bucket: "hour",
series: [
{ label: "", time: hour(1), calls: 14, input: 1, output: 1890, ...{ timed: 14, ttft_ms: 8400, decode_ms: 25200, decode_out: 1890 }, by: { model: { "glm-fast": part({ ...FAST, calls: 14, output: 1890, timed: 14, ttft_ms: 8400, decode_ms: 25200, decode_out: 1890 }) } } },
// hour(0): the hour's own is 1910 tokens in 27.4 s (69.7 tok/s) while
// glm-fast answered 1755 in 23.4 s (75.0), so its mark leaves the column
// and gets a leader line; kimi-slow (10) stays under it. hour(1) is the
// hour's own equal to the one model that answered in it, so no line
{ label: "", time: hour(0), calls: 15, input: 1, output: 1910, ...{ timed: 14, ttft_ms: 8400, decode_ms: 27400, decode_out: 1910 }, by: { model: { "glm-fast": part({ ...FAST, calls: 14, output: 1890, timed: 13, ttft_ms: 7800, decode_ms: 23400, decode_out: 1755 }), "kimi-slow": part(SLOW) } } },
],
agents: [{ id: "claude", name: "Claude Code", icon: "claudecode-color" }],
};
}
const STATE = (lang) => ({
agents: [
{ id: "claude", name: "Claude Code", path: "/t/c.json", icon: "claudecode-color", wired: true, fields: [{ key: "model", label: "model", value: "a", options: [{ value: "a", label: "Claude Sonnet 4.5", ref: "claude/a" }] }] },
{ id: "codex", name: "Codex", path: "/t/c.toml", icon: "codex-color", wired: true, fields: [{ key: "model", label: "model", value: "b", options: [{ value: "b", label: "GPT", ref: "openai/b" }] }] },
],
profiles: [], settings: { lang, theme: "light" }, fx: { rate: 7.2, at: new Date().toISOString() },
});
function server(lang) {
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:false};` });
if (url.pathname === "/wails/runtime.js") return route.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
if (url.pathname === "/api/state") return json(STATE(lang));
if (url.pathname === "/api/usage/requests") return json(ledger(url.searchParams.get("period")));
if (url.pathname === "/api/usage/quotas") return json([]);
if (url.pathname === "/api/usage") return json({ calls: 0, errors: 0, input: 0, output: 0, reasoning: 0, unpriced: 0, cost: 0, bucket: "day", series: [], agents: [], models: [], path: "" });
if (url.pathname === "/api/providers") return json({ providers: [], presets: [], excluded: [], models: [], gateway: { running: false } });
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.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 });
};
}
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of ["en", "zh"]) {
test(`${engine} ${lang}: the reporter's rows fit a narrow window`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
const errors = [];
const open = async (width) => {
const p = await (await browser.newContext({ viewport: { width, height: 760 }, reducedMotion: "reduce" })).newPage();
p.setDefaultTimeout(5000);
p.setDefaultNavigationTimeout(60000);
p.on("pageerror", (e) => errors.push(e.message));
await p.route("**/*", server(lang));
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("#ledWrap .led tbody tr").first().waitFor();
await p.waitForTimeout(300);
return p;
};
const fits = (p) => p.locator("#ledWrap").evaluate((e) => {
const box = e.getBoundingClientRect(), last = [...e.querySelectorAll("thead th")].filter((th) => th.offsetParent).at(-1).getBoundingClientRect();
return { scroll: e.scrollWidth <= e.clientWidth + 1, status: last.right <= box.right + 1 };
});
// narrower than the table is with Sent, Effort and the cache left out
const p = await open(820);
assert.deepEqual(await fits(p), { scroll: true, status: true }, "the table fits, Status in sight");
assert(await p.locator("#ledTight").isVisible(), "the hint says where the columns went");
const ref = p.locator(".led tbody tr.led-row").nth(2).locator("td.cost");
assert.equal(await ref.locator(".price-reference").isVisible(), false, "the price reference line is left out");
assert((await ref.getAttribute("title")).includes("gpt-5.6-luna"), "and is in the cost's tooltip");
const det = await reader.openRequest(p, p.locator(".led tbody tr.led-row").nth(2));
assert((await det.textContent()).includes("gpt-5.6-luna"), "and in the request's page");
// wide enough: the line is there (the window wider by the sidebar
// beside the page)
const q = await open(1320);
assert.deepEqual(await fits(q), { scroll: true, status: true });
assert(await q.locator(".led tbody tr.led-row").nth(2).locator(".price-reference").isVisible(), "the price reference line at a wider window");
assert.deepEqual(errors, []);
});
}
}