Files
magpie/internal/gui/tests/sess-agent-strip.test.cjs
T
yetone 61a0a69b5c gui: Routing, Usage and Sessions keep to the width the other pages do on a large screen, centred (面条 on Discord)
What changed: the window's page-width rule (#860), padding each view to
max(16px, calc(50% - 600px)), now names #view-routing, #view-usage and
to edge while Agents, Providers, Library, Gateway, Plugins and Settings
sat in a 1200px column in the middle. A narrow window keeps the 16px
gutter it had.

Routing's two columns, the Usage cards' packing and the requests ledger
lay out by the room they get (byWidth, packUsage), so they follow the
narrower box: at 1920 Routing still gets its two columns (min1150) and
the ledger fits 1200 without a sideways scroll.

sess-agent-strip.test.cjs asserted that eleven long agent names fit a
1600px window's Usage page in one row. With the page kept to 1200px they
were 8px too wide in WebKit zh (its scroll bar takes width), which is the
strip's designed overflow; the first two names are shortened so the "fits"
case is still one that fits, and the narrow case still overflows. One word
off the first name left it 0.34px of room (1159.66 in 1160), which went
red once in WebKit en under load; two names shorter leave 135px, and it
passed 3 runs in a row in each engine.
sessions-toolbar.test.cjs asserted that ten agents' tabs fit an 1800px
window; in a 1200px page they need 1115px of 912, so the page shows the
compact agent menu (its designed overflow). The fixture now has eight
agents (829px), so the wide case still tests tabs that fit.

Run:
- new page-width.test.cjs: every view's content width 1150..1200 and
  centred at en 1920, zh 2560, zh-TW 1920, ja 2560, de 1920; 16px at en
  900; every view's left edge and padding equal Agents'; chromium and
  webkit, 14/14. Without the CSS change it fails ("sessions: 1676 wide
  at 1920").
- before/after screenshots of Routing, Usage cards, the ledger,
  Sessions and Providers at 1920, 2560, 1100 and 800 (chromium).
- GUI regression set, both engines: click-scroll, editor-short-window,
  ledger-*, panel-usage, quota-*, routing-*, usage-*, sessions-*,
  sess-agent-strip and sessions-toolbar (after the fixture changes).
  routing-group-fold (webkit en/ja/de) and routing-sessions (webkit en)
  fail the same with origin/main 17c12621b's own assets, run alone:
  already filed as #1612 and #1672.
  usage-ledger's webkit "dark, and narrow" failed once in the full run;
  it fails the same with 48e59c5b8's assets (the view left scrolled 32px
  after the resize, the tab under the header), noted on #1671.
- Go: gofmt -l, go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./..., the builds: clean. go test -tags nogui
  ./... at load ~25-60 (other sessions' runs): every package passed but
  internal/agent and internal/gateway, which hit the 30m timeout; run
  again alone, agent passed and gateway's only red was
  TestOTelWaterfallIncludesUnbilledFallback, filed as #1794 (passes
  alone, -count=3).

Not verified: real WebView2 or WebKitGTK windows; layouts were checked
in Playwright's chromium and webkit only.
2026-10-12 22:29:45 +08:00

208 lines
13 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// #929 (yetone/magpie): with a dozen agents in range, Usage → Sessions' agent
// filter grew wider than the window, so the whole page scrolled sideways and
// the last agent's filter was only reachable by dragging the page itself. A
// strip too wide for the window scrolls in itself — the regions strip and
// Connect's five APIs already do — so this one does too, and its options keep
// their own width. English and Chinese, Chromium and WebKit.
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();
// names long enough that eleven agents make a strip wider than an 866px window,
// and short enough to fit a wide one, whose pages keep to 1200px (面条 on Discord),
// with room to spare: a strip that fits by a fraction of a pixel fails on a
// glyph's rounding
const NAMES = ["Trae CN Agent", "Kilo Code Agent", "Pi", "WorkBuddy",
"DeepSeek Harness Agent", "Claude Code", "Codex", "omp", "OpenCode Agent", "Qoder Code", "ZCode Agent"];
const NARROW = { width: 866, height: 800 };
const WIDE = { width: 1600, height: 800 };
const sess = (agent, name, k) => ({
agent, id: `${agent}-${k}`, cwd: `/work/${agent}/part${k}`, title: `task ${k} on ${name}`, start: at(20), last: at(10),
models: [{ model: k ? "m-two" : "m-one", input: 1000, output: 100, cache_read: 500, cache_write: 0, cost: 0.5, priced: true }],
input: 1000, output: 100, cache_read: 500, cache_write: 0, cost: 0.5, unpriced: 0,
resume: "x", path: `~/.${agent}/x/${k}.jsonl`, name, icon: "generic",
});
function serve(lang, names) {
const sessions = names.flatMap((name, i) => [0, 1].map((k) => sess(`ag${i}`, name, k)));
const agents = Object.fromEntries(names.map((name, i) => [`ag${i}`, name]));
const state = { agents: [], profiles: [], settings: { lang, theme: "light" } };
return async (r) => {
const url = new URL(r.request().url());
const json = (data) => r.fulfill({ json: data });
if (url.pathname === "/boot.js") return r.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:false};` });
if (url.pathname === "/wails/runtime.js") return r.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
if (url.pathname === "/api/state") return json(state);
if (url.pathname === "/api/sessions") return json({ sessions, terminal: true, dirs: [] });
if (url.pathname === "/api/sessions/stats") {
const today = new Date().toISOString().slice(0, 10);
const usage = sessions.map((s) => ({ agent: s.agent, cwd: s.cwd, model: s.models[0].model, input: s.input, output: s.output, cache_read: s.cache_read, cache_write: 0, cost: s.cost, priced: true }));
return json({ from: today, to: today, days: [{ date: today, usage, active: [] }], agents });
}
if (url.pathname === "/api/sessions/overview") return json({ count: sessions.length, median: 5000, p90: 15000, days: [1], top: { tokens: [], cost: [], active: [] } });
if (url.pathname === "/api/sessions/manage") {
const ids = Object.keys(agents);
const want = url.searchParams.get("agent");
const agent = ids.includes(want) ? want : ids[0];
return json({
agents: ids.map((id) => ({ agent: id, count: 2, deletable: true, name: agents[id], icon: "generic" })),
agent, sessions: sessions.filter((s) => s.agent === agent), terminal: true, trash: [], trashDir: "~/magpie/trash/sessions",
});
}
if (url.pathname === "/api/groups") return json({ groups: [] });
if (url.pathname === "/api/usage/quotas") return json([]);
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)];
const body = await fs.readFile(file).catch(() => null);
await (body ? r.fulfill({ body, contentType }) : r.fulfill({ status: 404, body: "" }));
};
}
// a box's own sideways overflow, and whether it scrolls sideways at all
const box = (page, sel) => page.locator(sel).evaluate((el) => ({
scroll: el.scrollWidth, client: el.clientWidth, overflow: getComputedStyle(el).overflowX,
}));
const inside = (page, sel, outer = "#view-usage") => page.locator(sel).evaluate((el, o) => {
const o1 = document.querySelector(o).getBoundingClientRect();
const r = el.getBoundingClientRect();
return r.left >= o1.left - 1 && r.right <= o1.right + 1;
}, outer);
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of ["en", "zh"]) {
test(`${engine}: ${lang}: Usage → Sessions' agent strip scrolls in itself, not the page`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
const page = await (await browser.newContext({ viewport: NARROW, reducedMotion: "reduce" })).newPage();
t.after(() => browser.close());
page.setDefaultTimeout(8000);
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", serve(lang, NAMES));
await page.addInitScript(() => {
localStorage.setItem("magpie.usageTab", "sessions");
localStorage.setItem("magpie.sessRange", "all");
// the Usage page's own 5 s refresh would rebuild the strip between a
// measurement and the click it measures; one read is enough here
localStorage.setItem("magpie.usageEvery", "0");
});
await page.goto("http://magpie.test/?view=usage");
await page.waitForFunction(() => document.querySelectorAll("#sessAgent .opt").length === 12);
// #929: eleven agents in range made the strip wider than the window, and
// the page scrolled sideways instead of the strip
const view = await box(page, "#view-usage");
assert.ok(view.scroll <= view.client + 1, `the Usage page scrolls sideways by ${view.scroll - view.client}px with ${NAMES.length} agents in range`);
const seg = await box(page, "#sessAgent");
assert.equal(seg.overflow, "auto", "the agent strip doesn't scroll sideways in itself");
assert.ok(seg.scroll > seg.client + 1, `the strip fits the window at ${seg.client}px, so nothing is being fixed`);
// every option is reachable: the last one, then the first, by scrolling
// the strip — none of them is cut off past the strip's own edge
const opts = page.locator("#sessAgent .opt");
// a real click where a reader's would land: scroll the option into the
// strip, then press the middle of it rather than trust a stale handle
const press = async (which) => {
for (let tries = 3; tries; tries--) {
const p = await page.locator("#sessAgent").evaluate((strip, which) => {
const all = strip.querySelectorAll(".opt");
const el = which === "first" ? all[0] : all[all.length - 1];
el.scrollIntoView({ block: "nearest", inline: "nearest" });
const r = el.getBoundingClientRect();
const s = strip.getBoundingClientRect();
return { x: r.left + r.width / 2, y: r.top + r.height / 2, text: el.textContent, cut: r.left < s.left - 1 || r.right > s.right + 1 };
}, which);
assert.equal(p.cut, false, `the ${which} agent's filter sits outside the strip and is cut off`);
await page.mouse.click(p.x, p.y);
try {
await page.waitForFunction((text) => document.querySelector("#sessAgent .opt.on")?.textContent === text, p.text, { polling: 100, timeout: 2000 });
return p.text;
} catch (e) {
if (!tries) throw e; // WebKit drops the odd press; a reader would press again
}
}
};
const first = (await opts.first().innerText()).trim();
assert.equal(await press("last"), NAMES[NAMES.length - 1], "clicking the last agent's filter didn't choose it");
assert.equal(await press("first"), first, "clicking the first agent's filter didn't choose it again");
// the picks beside the strip stay visible and inside the window
for (const id of ["#sessModel", "#sessFolder"]) {
assert(await page.locator(id).isVisible(), `${id} is not visible beside the strip`);
assert(await inside(page, id), `${id} sits outside the window`);
}
await page.locator("#sessModel").click();
await page.locator(".pop.proto-menu.sess-menu").waitFor();
await page.keyboard.press("Escape");
await page.locator("#sessFolder").click();
await page.locator(".pop.proto-menu.sess-menu").waitFor();
await page.keyboard.press("Escape");
// no click on the strip left an accent behind; the picks beside it keep
// their own box border on all four sides
const borders = await page.evaluate(() => [...document.querySelectorAll("#sessAgent .opt")]
.map((e) => getComputedStyle(e).borderLeftWidth).filter((w) => w && parseFloat(w) > 0));
assert.deepEqual(borders, [], "no left-border accent");
// a window wider than the strip: nothing scrolls sideways, and the
// options keep their own widths instead of being stretched to fill it
await page.setViewportSize(WIDE);
await page.waitForTimeout(120);
const wideView = await box(page, "#view-usage");
assert.ok(wideView.scroll <= wideView.client + 1, `a window wider than the strip still scrolls the page sideways by ${wideView.scroll - wideView.client}px`);
const fit = await box(page, "#sessAgent");
assert.ok(fit.scroll <= fit.client + 1, `the strip scrolls sideways in a window that fits it (${fit.scroll} in ${fit.client})`);
const widths = await opts.evaluateAll((els) => els.map((el) => ({ w: el.getBoundingClientRect().width, need: el.scrollWidth })));
assert.ok(widths.every((o) => o.need <= o.w + 1), "an option was squeezed narrower than its own text");
assert.ok(new Set(widths.map((o) => Math.round(o.w))).size > 1, "every option is the same width: they were stretched, not laid out by their text");
assert.deepEqual(errors, []);
// one agent: the strip stays hidden, as it was before (#929 changed
// nothing about when the filter shows)
await page.unroute("**/*");
await page.route("**/*", serve(lang, NAMES.slice(0, 1)));
await page.reload();
await page.waitForFunction(() => document.querySelectorAll("#sessAgent .opt").length > 0);
assert.equal(await page.locator("#sessAgent").isVisible(), false, "one agent in range should leave the agent filter hidden");
const one = await box(page, "#view-usage");
assert.ok(one.scroll <= one.client + 1, "one agent in range still scrolls the page sideways");
// the other strips are untouched: the Usage page's own tab strip and
// range strip still don't scroll in themselves, and the regions strip
// and Connect's still do
await page.unroute("**/*");
await page.route("**/*", serve(lang, NAMES));
await page.goto("http://magpie.test/?view=usage");
await page.waitForFunction(() => document.querySelectorAll("#sessAgent .opt").length === 12);
assert.equal((await box(page, "#period")).overflow, "visible", "the Usage page's range strip started scrolling in itself");
assert.equal((await box(page, "#usageTab")).overflow, "visible", "the Usage page's tab strip started scrolling in itself");
const others = await page.evaluate(() => {
const out = {};
const probe = (classes, host) => {
const wrap = document.createElement("div");
if (host) wrap.className = host;
const el = document.createElement("div");
el.className = classes;
el.append(Object.assign(document.createElement("button"), { className: "opt", textContent: "x" }));
wrap.append(el);
document.body.append(wrap);
out[host ? `${classes} in ${host}` : classes] = { overflow: getComputedStyle(el).overflowX, flex: getComputedStyle(el.firstChild).flex };
wrap.remove();
};
probe("segs regions");
probe("segs", "connect");
return out;
});
assert.equal(others["segs regions"].overflow, "auto", "the regions strip stopped scrolling in itself");
assert.equal(others["segs regions"].flex, "0 0 auto", "the regions strip's options lost their own width");
assert.equal(others["segs in connect"].overflow, "auto", "Connect's API strip stopped scrolling in itself");
assert.equal(others["segs in connect"].flex, "0 0 auto", "Connect's API options lost their own width");
assert.deepEqual(errors, []);
});
}
}