Files
magpie/internal/gui/tests/sessions-speed.test.cjs
T
yetone e6f151507f sessions: each session's output speed, a curve of its last two minutes
The owner liked a tweet's token-speed curve and approved a mockup
(claude.ai/artifact/7mrkMKdVi6jA3HA1Z6pRr5). Each Sessions row now
shows its output speed with a small curve; clicking it opens a popover
with the current tok/s, two minutes of seconds, tool gaps, and the
round's average, fastest second and output so far. A row not live
shows "Avg {n} tok/s".

Gateway: a curveMeter (session_speed.go) on the Server counts every
reply's content bytes per agent and session, from liveRow.observe, the
same count the Live view uses, and turns them into tokens with the
learned per-model rate (tokenRates.rate). A model with no rate yet
shows the curve's shape and no number: a guess would be a wrong
number. counted() opens a session's in-flight span, so a second with
nothing in flight is a gap (a tool ran), not a zero. A pause past 2m
starts a new round; 10m of quiet forgets the session; at most 256
sessions. Codex's own sign-in (codex_backend.go) went past serve and
liveRow before; it now has a liveRow too, so its sessions are counted.

GUI: GET /api/speeds, kept out of /api/sessions* so a once-a-second
poll doesn't set off session reading. The page polls only while
Sessions is shown and visible. managedJSON gains keys, so a row
matches a session the gateway saw under another of its keys. The
curve hides below 620px.

Run (mote's sshd resets every connection today, so Linux and Windows):
- Linux (magpie-desktop, wsl-run): gofmt clean; go test -timeout 45m
  ./internal/gateway ./internal/sessions ok except
  TestGroupFailoverTimesEachTry, red once under load, 5/5 green alone
  here and on main (#5948); node --test over every sessions-*.test.cjs
  and i18n-*.test.cjs: 546/549, the reds sessions-export en in both
  engines, the same on origin/main d657e182a alone (#5947), and
  sessions-folder-paging webkit zh, green alone. sessions-speed in
  Chromium and WebKit x en, zh, zh-TW, ja, de (row, popover, live
  update, narrow width, no poll off the page).
- Windows (xiaomi-gpu, win-run), at this head: go vet ./... clean; the
  curve/live/rates/codex tests of ./internal/gateway ok; go test
  ./internal/gui ./internal/sessions ok but 12 tests, every one red the
  same on origin/main 4d95d81ea and filed (#4115, #5928, #4043, #4925,
  #5324; TestAntigravityAppFolder new as #5957). (internal/gui needs gtk4 for cgo, which
  the WSL distro lacks.)
- At the pushed head (on 6886da1ca, which changed core i18n.js and
  gateway.go), Linux: sessions-speed, i18n-keys, i18n-en-no-cjk,
  sessions-row-narrow 22/22; the gateway curve/live/rates/codex/CORS
  tests ok. The Windows run above was at 4d95d81ea.
Broken by hand: with live.go's curves.add skipped, both
TestSessionCurveFromARealStream and TestSessionCurveFromCodexOwnSignIn
went red; green once restored.

Not tried: a real agent session in the built app; macOS (mote down);
darwin vet; the full go and GUI suites (run after the push, reds
filed).
2026-11-10 20:22:17 +08:00

152 lines
8.9 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// A session the gateway is serving shows its output speed on its Sessions
// row: a curve of the last two minutes and tokens a second now, read from
// /api/speeds once a second; a click opens the round's numbers, with the
// gap where a tool ran. A session known by another key (s.keys) gets its
// speed too; a model with no rate yet shows "—", never a guess; a session
// with no speed has no cell. Every language, Chromium and WebKit, wide and
// narrow.
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 at = (min) => new Date(Date.now() - min * 60e3).toISOString();
const sess = (id, min, extra = {}) => ({
agent: "claude", id, cwd: "/work/app", title: "Session " + id, start: at(min + 5), last: at(min),
models: [{ model: "claude-opus-5-5", priced: true, input: 1000, output: 200, calls: 4 }],
input: 1000, output: 200, cost: 0.1, messages: 12, size: 4096, files: 1, deletable: true,
resume: `claude --resume ${id}`, path: `~/.claude/projects/x/${id}.jsonl`, ...extra,
});
// 120 seconds: 20 at 100, a 30 second gap, then 70 rising to the last at `now`
function curve(now) {
const c = [];
for (let i = 0; i < 120; i++) c.push(i < 20 ? null : i < 40 ? 100 : i < 70 ? null : 100 + (i - 70) * (now - 100) / 49);
return c;
}
function serve(lang, reads) {
const sessions = [sess("s-1", 0), sess("s-2", 1, { keys: ["claude|other-key"] }), sess("s-3", 30)];
const state = { agents: [], profiles: [], settings: { lang, theme: "light" } };
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:true};` });
if (url.pathname === "/wails/runtime.js") return route.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
if (url.pathname === "/api/state") return json(state);
if (url.pathname === "/api/plugins") return json({ plugins: [] });
if (url.pathname === "/api/groups") return json({ groups: [] });
if (url.pathname === "/api/providers") return json({ providers: [], presets: [], excluded: [], gateway: { running: true, window: true } });
if (url.pathname === "/api/sessions/manage") {
return json({
agents: [{ agent: "claude", count: sessions.length, deletable: true, name: "Claude Code", icon: "claudecode-color" }],
agent: "claude", sessions, terminal: true, trash: [], trashDir: "~/trash",
});
}
if (url.pathname === "/api/speeds") {
reads.n++;
const now = reads.n < 3 ? 128 : 140;
return json([
{ agent: "claude", session: "s-1", live: true, rated: true, now, avg: 109.4, peak: 315, output: 3700, curve: curve(now), idle: 0 },
{ agent: "claude", session: "other-key", live: false, rated: false, now: 0, avg: 0, peak: 0, output: 0, curve: curve(300).map((v, i) => i < 110 ? v : null), idle: 12 },
]);
}
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 words = {
en: { nav: "Sessions", unit: "tok/s", avg: "Round average", peak: "Fastest second", written: "Out so far", gap: "Tool 30s" },
zh: { nav: "会话", unit: "token/秒", avg: "本轮平均", peak: "峰值", written: "已输出", gap: "工具 30 秒" },
"zh-TW": { nav: "會話", unit: "token/秒", avg: "本輪平均", peak: "峰值", written: "已輸出", gap: "工具 30 秒" },
ja: { nav: "セッション", unit: "トークン/秒", avg: "このターンの平均", peak: "ピーク", written: "出力済み", gap: "ツール 30 秒" },
de: { nav: "Sitzungen", unit: "Token/s", avg: "Ø dieser Runde", peak: "Spitze", written: "Ausgegeben", gap: "Tool 30 s" },
};
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of Object.keys(words)) {
test(`${engine} ${lang}: a session's speed on its row and in its popover`, async (t) => {
const w = words[lang];
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
const page = await (await browser.newContext({ viewport: { width: 1100, height: 700 }, reducedMotion: "reduce" })).newPage();
t.after(async () => {
if (process.env.ARTIFACT_DIR) {
await fs.mkdir(process.env.ARTIFACT_DIR, { recursive: true });
await page.screenshot({ path: path.join(process.env.ARTIFACT_DIR, `${engine}-${lang}-sessions-speed.png`) });
}
await browser.close();
});
page.setDefaultTimeout(8000);
page.setDefaultNavigationTimeout(60000);
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
const reads = { n: 0 };
await page.route("**/*", serve(lang, reads));
await page.goto("http://magpie.test/?view=providers");
assert.equal(reads.n, 0, "/api/speeds is read before the Sessions page is shown");
await page.locator("#nav").getByRole("button", { name: w.nav, exact: true }).click();
const row = (id) => page.locator(`#view-sessions .row.sm-sess[data-id="${id}"]`);
const cell = row("s-1").locator(".sm-speed");
await cell.locator(".sm-speed-val b").filter({ hasText: "128" }).waitFor();
assert.equal((await cell.locator(".sm-speed-val small").textContent()).trim(), w.unit);
assert.equal(await cell.locator("svg.sm-curve polyline").count(), 2, "two runs of seconds");
assert.equal(await cell.locator("svg.sm-curve line.sm-curve-gap").count(), 1, "one gap");
// another key of the session's, a model with no rate: the shape, no number
const other = row("s-2").locator(".sm-speed");
await other.waitFor();
assert.equal(await other.locator(".sm-speed-val").textContent(), "—");
assert.ok(await other.locator("svg.sm-curve").count());
assert.equal(await row("s-3").locator(".sm-speed").isHidden(), true, "a session with no speed shows none");
// the popover: the number now, the curve, the tool's gap and the round
await cell.click();
const pop = page.locator(".pop.sm-speed-pop");
await pop.waitFor();
assert.equal(await cell.getAttribute("aria-expanded"), "true");
assert.equal(await pop.locator(".sm-sp-unit").textContent(), w.unit);
assert.match(await pop.locator(".sm-sp-big").textContent(), /^(128|140)$/);
assert.equal(await pop.locator(".sm-sp-gap").textContent(), w.gap);
assert.equal(await pop.locator(".sm-sp-dot").count(), 1, "a live session's last second is marked");
const stats = await pop.locator(".sm-sp-k").allTextContents();
assert.deepEqual(stats, [w.avg, w.peak, w.written]);
const vals = await pop.locator(".sm-sp-v").allTextContents();
assert.match(vals[0], /109/);
assert.match(vals[1], /315/);
assert.match(vals[2], /3[.,]?7/);
// it follows the next reads, open
await pop.locator(".sm-sp-big").filter({ hasText: "140" }).waitFor();
const box = await pop.boundingBox(), vw = page.viewportSize().width;
assert.ok(box.x >= 0 && box.x + box.width <= vw, `popover inside the window: ${JSON.stringify(box)}`);
await page.keyboard.press("Escape");
await pop.waitFor({ state: "detached" });
assert.equal(await cell.getAttribute("aria-expanded"), "false");
// narrow: the number stays, nothing runs past the row
for (const width of [720, 520, 380]) {
await page.setViewportSize({ width, height: 700 });
await page.waitForFunction(() => new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(() => r(true)))));
const m = await row("s-1").evaluate((r) => {
const b = r.getBoundingClientRect();
return [...r.children].filter((c) => c.offsetParent && (c.getBoundingClientRect().right > b.right + 1 || c.getBoundingClientRect().left < b.left - 1)).map((c) => c.className);
});
assert.deepEqual(m, [], `at ${width}px past the row's edge`);
assert.ok(await cell.locator(".sm-speed-val").isVisible(), `at ${width}px the number shows`);
}
// off the page, no more reads
await page.setViewportSize({ width: 1100, height: 700 });
await page.locator("#nav").getByRole("button").first().click();
await page.waitForTimeout(300);
const was = reads.n;
await page.waitForTimeout(2500);
assert.ok(reads.n <= was + 1, `read ${reads.n - was} times off the page`);
assert.deepEqual(errors, []);
});
}
}