The ledger kept each reply's duration, first-token time and output tokens, but the page only showed a duration. Speed is now shown in four places, measured as output tokens a second after the first token (usage.DecodeWindow, the same guard Routing's tok/s uses): - Each row has a Speed column. It shows "—" for a reply that wasn't streamed, and the row's details say it too. - A fifth KPI, Output speed, gives the period's speed with "first token in … on average". - The trend has a Speed metric. Each model, provider or agent gets a mark at its speed for every hour or day, with a faint column for the hour's overall speed. The tooltip gives each one's tok/s and TTFT, and the ranking sorts fastest first. - The ranking shows each one's tok/s and TTFT under the other metrics. usage.Part (a series point's per-model, per-provider and per-agent sums) now carries timed, ttft_ms, decode_ms and decode_out for this. Narrow window: the reporter's window is 975px wide (1220 at 125%). The table was 1222px in a 941px box, so the Status and Speed columns needed scrolling sideways. Where the table doesn't fit, it now leaves out Sent, Effort and the two cache columns. A line under the totals says those are in each row's details, and the details list them. The check runs again when the width changes or the table grows (a font arriving). At 975 the table now fits. Wide window: at 1670px the Agents, Providers, Gateway, Library, Plugins and Settings rows stretched to 1636px. They now stop at 1200px and are centred. Routing, Usage and Sessions keep the full width. zh, ja and de strings. Tests: - usage-speed.test.cjs (WebKit, en and zh) fails on the old code (no Speed column). At 975×778 it checks: the Speed column (75 tok/s, 10, "—"), the table fitting with no sideways bar, the hint, and Sent and Speed in the details. It also checks the KPI (72 tok/s, first token in 600 ms), the ranking's tok/s, the Speed trend (fastest first, a mark per model per hour), and Agents and Settings rows at most 1200 wide and centred at 1670. - TestLedgerSeriesSpeed. - usage-ledger, usage-ledger-detail and ledger-hscroll updated for the new column and details. ledger-hscroll now runs at 700px, still narrower than the tight table. Results: usage-*, ledger-hscroll, agent-*, settings-*, click-scroll, gui-ja, gui-de, import-row, member-model-setting, routing-sessions, routing-columns and sessions-overview/toolbar pass on WebKit. remote-group-served's zh run timed out waiting for Routing's By request tab in 2 of 9 runs and passed in the rest. go vet, the builds (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass.
124 lines
8.0 KiB
JavaScript
124 lines
8.0 KiB
JavaScript
// Run with Node's test runner and Playwright on the module path; see README.md.
|
|
// The Usage page's Requests table is wider than a narrow window, and its own
|
|
// scrollbar is at its foot, fifty rows down (mintonight, #799): a scrollbar
|
|
// stands in for it at the bottom of the window while the table is in sight,
|
|
// moves the table sideways and follows it, and is gone when the table fits.
|
|
// A row's details say how it went and how long it took, and link to its
|
|
// routing, so neither needs the columns out of sight. 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 reader = require("./reader.cjs");
|
|
|
|
const assets = path.resolve(__dirname, "../assets");
|
|
const now = Date.now();
|
|
|
|
const ROWS = Array.from({ length: 40 }, (_, i) => ({
|
|
route_id: 500 + i, t: new Date(now - (i + 1) * 60e3).toISOString(), agent: "trae", agentName: "Trae CN", icon: "generic",
|
|
provider: "deepseek", providerName: "DeepSeek Harness", req: "group/auto-deepseek-v4-1-flash", model: "deepseek-v4-1-flash", served: "deepseek-v4-1-flash",
|
|
in: 1200 + i, out: 80, ms: 2400, ttft_ms: 600, status: 200, rid: "req_" + i, ep: "/v1/chat/completions", cost: 0.001, priced: true,
|
|
}));
|
|
|
|
function server(lang, asked) {
|
|
return async (route) => {
|
|
const req = route.request(), url = new URL(req.url());
|
|
const json = (data, status) => route.fulfill({ json: data, status });
|
|
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: 0, input: 1, output: 1, cost: 0.04, unpriced: 0, agents: [{ id: "trae", name: "Trae CN", icon: "generic" }] });
|
|
if (url.pathname === "/api/gateway/route") { asked.push(url.searchParams.get("id")); return json({ error: "gone" }, 404); }
|
|
if (url.pathname === "/api/usage/quotas") return json([]);
|
|
if (url.pathname === "/api/usage") return json({ calls: 40, errors: 0, input: 1, output: 1, reasoning: 0, unpriced: 0, cost: 0.04, bucket: "day", series: [], agents: [], models: [], path: "" });
|
|
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: { labels: ["Status", "Duration", "Routing"], values: ["200", "2.4 s", "View routing"] },
|
|
zh: { labels: ["状态", "耗时", "路由"], values: ["200", "2.4 秒", "查看路由"] },
|
|
};
|
|
|
|
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
|
|
test(engine + ": the Requests table's scrollbar and a row's status", 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 ["en", "zh"]) {
|
|
const w = L[lang];
|
|
await t.test(lang, async () => {
|
|
const errors = [], asked = [];
|
|
// narrower than the table even with the columns a narrow window
|
|
// leaves out (#860)
|
|
const ctx = await browser.newContext({ viewport: { width: 700, height: 640 }, reducedMotion: "reduce" });
|
|
const p = await ctx.newPage();
|
|
p.setDefaultTimeout(5000);
|
|
p.on("pageerror", (e) => errors.push(e.message));
|
|
await p.route("**/*", server(lang, asked));
|
|
await p.goto("http://magpie.test/");
|
|
await p.locator('[data-view="usage"]').first().click();
|
|
await p.locator("#usageTab .opt").nth(1).click();
|
|
await p.locator("#ledWrap .led tbody tr").first().waitFor();
|
|
await p.waitForTimeout(200);
|
|
|
|
const wrap = p.locator("#ledWrap"), bar = p.locator("#ledHScroll");
|
|
assert(await wrap.evaluate((e) => e.scrollWidth > e.clientWidth + 50), "the table is wider than the window");
|
|
// the table's top rows are in sight, its foot is not, and the bar is
|
|
// at the bottom of the window, under the rows in sight
|
|
await reader.inView(p, p.locator(".led tbody tr.led-row").nth(2));
|
|
const view = await p.locator("#view-usage").boundingBox();
|
|
const wb = await wrap.boundingBox();
|
|
assert(wb.y + wb.height > view.y + view.height + 100, "the table's foot is out of sight");
|
|
assert(await bar.isVisible(), "no scrollbar in sight");
|
|
const bb = await bar.boundingBox();
|
|
assert(bb.y + bb.height <= view.y + view.height + 1 && bb.y + bb.height >= view.y + view.height - 20, "the bar is at the window's foot: " + JSON.stringify({ bb, view }));
|
|
assert(Math.abs(bb.width - wb.width) <= 2, "the bar is as wide as the table's box");
|
|
// it draws a thumb: wider content than itself
|
|
assert(await bar.evaluate((e) => e.scrollWidth > e.clientWidth && e.offsetHeight > e.clientHeight), "the bar draws a scrollbar");
|
|
|
|
// dragged to the end, the table shows its last column, Status
|
|
await bar.evaluate((e) => { e.scrollLeft = e.scrollWidth; });
|
|
await p.waitForTimeout(150);
|
|
assert.equal(await wrap.evaluate((e) => Math.round(e.scrollLeft)), await wrap.evaluate((e) => e.scrollWidth - e.clientWidth), "the table followed the bar");
|
|
const st = await p.locator(".led tbody tr.led-row").nth(2).locator(".st").boundingBox();
|
|
assert(st.x + st.width <= wb.x + wb.width + 1 && st.x >= wb.x, "Status is in sight");
|
|
// the table scrolled itself (a trackpad's swipe): the bar follows
|
|
await wrap.evaluate((e) => { e.scrollLeft = 30; });
|
|
await p.waitForTimeout(150);
|
|
assert.equal(await bar.evaluate((e) => Math.round(e.scrollLeft)), 30, "the bar followed the table");
|
|
|
|
// a row's details: how it went, how long, and its routing
|
|
await reader.click(p, p.locator(".led tbody tr.led-row").nth(2).locator("td").nth(2));
|
|
const d = p.locator(".led tbody tr.led-detail");
|
|
assert.deepEqual((await d.locator("dt").allTextContents()).slice(0, 3), w.labels);
|
|
assert.deepEqual((await d.locator("dd").allTextContents()).slice(0, 3), w.values);
|
|
// brought above the bar, which covers the window's last few pixels
|
|
await reader.inView(p, d.locator(".led-route-link"));
|
|
await p.mouse.wheel(0, 80);
|
|
await p.waitForTimeout(300);
|
|
await d.locator(".led-route-link").click();
|
|
for (let i = 0; i < 40 && !asked.length; i++) await p.waitForTimeout(25);
|
|
assert.deepEqual(asked, ["502"], "the link asks for the row's route");
|
|
assert.equal(await p.locator(".led tbody tr.led-detail").count(), 1, "the link didn't close the row");
|
|
|
|
// a window wide enough for the table has no bar
|
|
await p.setViewportSize({ width: 2200, height: 640 });
|
|
await p.waitForTimeout(250);
|
|
assert(await wrap.evaluate((e) => e.scrollWidth <= e.clientWidth + 1), "the table fits");
|
|
assert.equal(await bar.isVisible(), false, "a bar with nothing to scroll");
|
|
assert.deepEqual(errors, []);
|
|
await ctx.close();
|
|
});
|
|
}
|
|
});
|
|
}
|