Files
magpie/internal/gui/tests/usage-ledger-ttft.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

174 lines
12 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// Each request's first token in a column of its own on the Usage page's
// Requests (tianxia3111, #2522): the duration holds the wait for it and the
// writing after it, so a list of durations can't tell a vendor slow to
// start from one slow to write. The column says the row details' "First
// token", "≈" for one told from a session file's times, and a dash for one
// with none (failed, or not streamed). A narrow window leaves Sent, Effort
// and the cache out, then cuts names and wraps headers, and only then the
// column too, which then reads small under each duration instead
// (unscientific9 on Discord), a row with none adding nothing; the
// hint names only what is in the details alone. Every language, both
// engines, from wide to narrow; 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 reader = require("./reader.cjs");
const assets = path.resolve(__dirname, "../assets");
const now = Date.now();
const base = { agent: "claude", agentName: "Claude Code", icon: "claudecode-color", provider: "relay", providerName: "Relay", host: "team", req: "sol", model: "gpt-6-sol", in: 1200, out: 300, cost: 0.01, priced: true };
// the reporter's own sample (ttft_ms 5504 of 8502), a Codex turn read from
// its session file, a failure before any token, and a reply not streamed
const ROWS = [
{ ...base, t: new Date(now - 60e3).toISOString(), ms: 8502, ttft_ms: 5504, sent_ms: 119, flow_ms: 2658, status: 200, rid: "req_a", ep: "/v1/messages" },
{ ...base, t: new Date(now - 120e3).toISOString(), source: "log", agent: "codex", agentName: "Codex", icon: "codex-color", session: "s1", ms: 4200, ttft_ms: 2100 },
{ ...base, t: new Date(now - 180e3).toISOString(), in: 0, out: 0, ms: 610, status: 429, err: "rate limited", priced: false },
{ ...base, t: new Date(now - 240e3).toISOString(), ms: 3000, status: 200 },
];
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({ agents: [], profiles: [], settings: { lang, theme: "light" }, fx: { rate: 7.2, at: new Date().toISOString() } });
if (url.pathname === "/api/usage/requests") {
return json({ period: url.searchParams.get("period"), rows: ROWS, offset: 0, total: ROWS.length, calls: ROWS.length, errors: 1, input: 3600, output: 900, cache_read: 0, cache_write: 0, cost: 0.03, unpriced: 0, agents: [{ id: "claude", name: "Claude Code", icon: "claudecode-color" }, { id: "codex", name: "Codex", icon: "codex-color" }] });
}
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 });
};
}
const L = {
en: { head: "TTFT", detail: "First token", duration: "Duration", hint: "Sent, Effort and the cache are in each row's details", under: "TTFT " },
zh: { head: "首字耗时", detail: "首字耗时", duration: "耗时", hint: "窗口较窄:发送模型、推理强度和缓存在每行的详情里", under: "首字 " },
"zh-TW": { head: "首字耗時", detail: "首字耗時", duration: "耗時", hint: "視窗較窄:傳送模型、推理強度和快取在每行的詳情裡", under: "首字 " },
ja: { head: "TTFT", detail: "最初のトークン", duration: "所要時間", hint: "ウィンドウが狭いため、送信したモデル・推論レベル・キャッシュは各行の詳細にあります", under: "TTFT " },
de: { head: "TTFT", detail: "Erstes Token", duration: "Dauer", hint: "Schmales Fenster: Gesendet, Effort und Cache stehen in den Details jeder Zeile", under: "TTFT " },
};
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
test(engine + ": each request's first token, a column of its own", async (t) => {
assert(["chromium", "webkit"].includes(engine), "BROWSER must be chromium or webkit");
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
for (const lang of Object.keys(L)) {
const w = L[lang];
await t.test(lang, async () => {
const errors = [];
const ctx = await browser.newContext({ viewport: { width: 1600, height: 800 }, reducedMotion: "reduce" });
t.after(() => ctx.close());
const p = await ctx.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(200);
// the column, just before the duration it is part of
const heads = await p.locator(".led thead th").allTextContents();
assert.equal(heads.indexOf(w.head) + 1, heads.indexOf(w.duration), "first token before duration: " + heads.join(", "));
assert((await p.locator(".led thead th.lc-opt2", { hasText: w.head }).getAttribute("title")).length > 0, "its header says what it is in full");
const rows = p.locator(".led tbody tr.led-row");
const cell = (i) => rows.nth(i).locator("td.ttft");
assert(await cell(0).isVisible(), "a wide window shows it");
// it says what the details say, "≈" for a session file's
const detail = async (i) => {
await reader.click(p, rows.nth(i).locator("td").nth(2));
const d = p.locator("#ledReqPane .led-detail");
await d.waitFor();
const dts = await d.locator("dt").allTextContents(), dds = await d.locator("dd").allTextContents();
await reader.closeRequest(p);
return dds[dts.indexOf(w.detail)];
};
const first = await detail(0);
assert(first && /5/.test(first), "the details' first token: " + first);
assert.equal((await cell(0).textContent()).trim(), first);
assert((await cell(0).getAttribute("title")).length > 0, "its exact ms in the tooltip");
assert.equal((await cell(1).textContent()).trim(), "≈" + (await detail(1)), "about, from the session file's times");
// a failure before any token, and a reply not streamed: a dash
assert.equal((await cell(2).textContent()).trim(), "—");
assert.equal((await cell(3).textContent()).trim(), "—");
assert(await cell(3).evaluate((c) => c.classList.contains("faint")), "a dash reads faint, as the others' do");
// numbers line up as the duration's do
assert.deepEqual(await cell(0).evaluate((c) => getComputedStyle(c).textAlign), await rows.nth(0).locator("td.duration").evaluate((c) => getComputedStyle(c).textAlign));
// what reads under each duration: nothing while the column is there
const under = (i) => rows.nth(i).locator("td.duration").evaluate((c) => {
const a = getComputedStyle(c, "::after");
return a.content === "none" || a.content === "normal" || a.display === "none" ? "" : JSON.parse(a.content);
});
const durText = await rows.nth(0).locator("td.duration").textContent();
for (let i = 0; i < 4; i++) assert.equal(await under(i), "", "a wide window adds nothing under the duration");
// a narrowing window shortens names, wraps headers and drops the
// price reference before it leaves the column out: a column is the
// larger loss, and two Windows windows a little apart in width
// showed it on one and not the other (unscientific9 on Discord)
const fits = [];
for (let vw = 1600; vw >= 600; vw -= 20) {
await p.setViewportSize({ width: vw, height: 800 });
await p.waitForTimeout(40);
fits.push([vw, await p.locator("table.led").evaluate((tb) => [...tb.classList].filter((c) => c !== "led").join(" ")), await cell(0).isVisible()]);
}
const said = fits.map((f) => f.join(":")).join(", ");
for (const [vw, cls, shown] of fits) {
assert.equal(shown, !cls.includes("tight2"), "the column is out exactly while tight2 is on, at " + vw + ": " + said);
if (cls.includes("tight2")) assert(cls.includes("tighter"), "the column goes only after the rest is made tighter, at " + vw + ": " + said);
}
assert(fits.some(([, cls, shown]) => cls.includes("tighter") && shown), "some width keeps the column with the rest made tighter: " + said);
// a narrow window: the column goes, the details keep it, the hint says so
await p.setViewportSize({ width: 600, height: 800 });
await p.waitForTimeout(300);
assert.equal(await cell(0).isVisible(), false, "a narrow window leaves it out");
assert.equal((await p.locator("#ledTight").textContent()).trim(), w.hint);
// and says it under the duration, small and faint; a row with none, nothing
assert.equal(await under(0), w.under + first);
assert.equal(await under(1), w.under + "≈" + (await detail(1)), "about, from the session file's times");
assert.equal(await under(2), "", "a failure before any token adds nothing");
assert.equal(await under(3), "", "nor a reply not streamed");
assert.equal(await rows.nth(0).locator("td.duration").textContent(), durText, "the cell's own text is the duration alone");
const look = await rows.nth(0).locator("td.duration").evaluate((c) => {
const a = getComputedStyle(c, "::after"), d = getComputedStyle(c);
return { small: parseFloat(a.fontSize) < parseFloat(d.fontSize), block: a.display, color: a.color !== d.color };
});
assert.deepEqual(look, { small: true, block: "block", color: true }, "a small second line, muted");
assert(await rows.nth(0).evaluate((r) => r.getBoundingClientRect().height) < 60, "the row stays a row");
await reader.click(p, rows.nth(0).locator("td").nth(2));
await p.locator("#ledReqPane .led-detail").waitFor();
const shown = await p.locator("#ledReqPane .led-detail dt").evaluateAll((dts) => dts.filter((x) => x.offsetParent).map((x) => x.textContent));
assert(shown.includes(w.detail), "the details still say it: " + shown.join(", "));
assert.equal(await p.locator("#view-usage").evaluate((v) => v.scrollWidth <= v.clientWidth + 1), true, "the page doesn't scroll sideways");
await reader.closeRequest(p);
// a phone-narrow window: still under the duration, the page not sideways
await p.setViewportSize({ width: 420, height: 800 });
await p.waitForTimeout(300);
if (await rows.nth(0).locator("td.duration").isVisible()) {
assert.equal(await under(0), w.under + first, "still under the duration at 420");
assert.equal(await under(3), "");
}
assert.equal(await p.locator("#view-usage").evaluate((v) => v.scrollWidth <= v.clientWidth + 1), true, "the page doesn't scroll sideways at 420");
assert.deepEqual(errors, []);
});
}
});
}