Files
magpie/internal/gui/tests/usage-ledger-ttft.test.cjs
T
yetone dc1a9cde27 gui: Usage → Requests lists the first token's time in a column of its own (#2522)
tianxia3111 asked to compare time to first token down the request list
(首字时间). Until now it showed only inside a row's details and the
Duration tooltip. A "TTFT" column (zh 首响, zh-TW 首響, the word the
row details and Routing already use) now sits before Duration. Its
header tooltip reads "Time to first token". A row told from a session
file's times gets "≈", like Duration does. A row with none (not
streamed, or failed before a token) shows a faint dash, not 0 ms.

Narrow windows: ledFit used to have two stages, tight (Sent, Effort,
cache) and tighter (the price reference, shorter names). The new column
pushed the reporter-sized 975px window of #860 into tighter, so the
price reference went. A stage between them, tight2, now hides only the
TTFT column (LED_COLS opt level 2 → .lc-opt2). The hint then says the
first token is in each row's details too.

"TTFT" and "Time to first token" go into the core i18n.js, not the
plugin's. Other code says those words too, and i18n-keys' "a plugin's
words are its own" would fail otherwise.

Tests:
- usage-ledger-ttft is new: the header and its tooltip, the value and the
  dash, the ≈, and the column leaving at a narrow width with the hint. It
  runs in both engines and all five languages, 12/12.
- usage-ledger's column lists and usage-speed's narrow hint are updated,
  and usage-ledger-detail finds Duration by its class.
- The staged tree alone (index checked out): i18n-keys, usage-ledger-ttft,
  usage-ledger, usage-speed and usage-ledger-detail all pass, 61/61.

Run on origin/main d9b943555 (head 663db0ecf), with the #2517,
#2521 and #2522 changes, load ~240:
- gofmt -l clean; go vet -tags nogui for darwin, linux and windows: ok.
- go test -timeout 120m -count=1 ./... (pipefail): exit 0, every package
  ok (gateway 1416.8s, agent 1535.5s). Before the rebase, on 63be47717:
  - The 45m run had agent and gateway past 45m (#2544, #2108).
  - TestMainWindowCorners was killed at its deadline (#1936).
  - TestSkillRelinkKeepsUnreadableCopy failed (#2555; it passes alone).
  - gateway alone with 120m was ok.
- GUI, the 41 Requests/Usage files in serial, both engines, before the
  rebase: 493 pass, 3 fail. The 3 were usage-refresh "not yet 2 !== 1",
  the same on 63be47717 alone (1 of 3); seen again on #2184.
- Rebased, these all pass, 102/102: i18n-keys, usage-ledger-ttft,
  usage-ledger-key-split, usage-ledger, usage-speed, usage-ledger-detail,
  gui-ja, gui-de and gui-zh-tw.
- Rebased again onto 9b04c4e20, which touches ledger.go, request_page.go
  and gateway.go:
  - vet ×3: ok.
  - internal/usage, internal/usageapi and internal/cli: ok.
  - internal/gateway -run 'NativeCompaction|UsageOverGateway|Capped|Cap':
    ok.
  - usage-ledger-ttft, usage-ledger-key-split, usage-ledger and
    usage-speed: 86 pass. i18n-keys was red from main's own words (#2586,
    fixed in the next commit).
2026-10-16 21:59:33 +08:00

122 lines
8.7 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 it out with
// Sent, Effort and the cache, its value still in the details, and the hint
// says so. Every language, both engines; 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, the first token and the cache are in each row's details" },
zh: { head: "首响", detail: "首响", duration: "耗时", hint: "窗口较窄:发送模型、推理强度、首响和缓存在每行的详情里" },
"zh-TW": { head: "首響", detail: "首響", duration: "耗時", hint: "視窗較窄:傳送模型、推理強度、首響和快取在每行的詳情裡" },
ja: { head: "TTFT", detail: "最初のトークン", duration: "所要時間", hint: "ウィンドウが狭いため、送信したモデル・推論レベル・最初のトークン・キャッシュは各行の詳細にあります" },
de: { head: "TTFT", detail: "Erstes Token", duration: "Dauer", hint: "Schmales Fenster: Gesendet, Effort, erstes Token und Cache stehen in den Details jeder Zeile" },
};
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.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").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(".led tbody tr.led-detail");
const dts = await d.locator("dt").allTextContents(), dds = await d.locator("dd").allTextContents();
await reader.click(p, rows.nth(i).locator("td").nth(2));
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));
// a narrow window: the column goes, the details keep it, the hint says so
await p.setViewportSize({ width: 760, 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);
await reader.click(p, rows.nth(0).locator("td").nth(2));
const shown = await p.locator(".led tbody tr.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");
assert.deepEqual(errors, []);
});
}
});
}