Files
magpie/internal/gui/tests/menu-scroll.test.cjs
T
yetone f3681b90c3 gui: the Agents page says a model's load by the picked model and in its picker (axelburks on Discord)
axelburks (Discord #feedback) could see a model's load only on its
provider's page, too deep to judge which model to pick on the Agents
page. A plugin's reported load (magpie.load) now shows there too, as the
same quiet grey badge the provider page uses:

- by the picked model in each agent's model row, before the chevron;
- on each row of that row's model picker, after the speed.

GET /api/model-loads (speeds.go) answers every fresh load (LoadFresh,
5 minutes) by "provider/model" lowercased, the ref /api/model-speeds
uses: plugin.FreshLoads copies the fresh entries under one lock, and
provider.ModelLoads keys them by magpie's provider id, the user's own
listed id too (ModelIDs), with model@size falling back to its model as
ModelLoad does. With no plugin reporting it is one lock and an empty
map. The page reads it when a model picker opens and with the Agents
page's 15s drift check, each throttled to once per 30s, and the drift
read only while a row shows a magpie model. No new poll; /api/state and
Option are unchanged. The core script stays under its budget
(1,533,802) by shortening comments in the picker and agent-row code;
no budget raised. The
route is named in the CLI's route list (read-only, U) like model-speeds.

Run at this head (rebased on origin/main b4073c06a; the rebase onto
dcc84dbc0 touches none of these files), on mote:
- go test ./internal/gui (TestModelLoadsByRef, TestModelLoadsNoneReported,
  budgets) ./internal/plugin ./internal/provider ./internal/cli: ok,
  except TestEveryRouteHasACommandOrIsNamed's POST
  /api/usage/checkin-account, red on main (#4826, e87140242).
- Full go test ./... -timeout 45m before the rebase: 84 packages ok;
  reds were TestEveryRouteHasACommandOrIsNamed (this route, now named,
  and #4826), TestCodexPoolSaysTheClientsVersion (#4807, full-package
  run only) and TestMainWindowCorners/macOS_26_SDK (#3949, macOS 27).
- go vet for darwin, GOOS=linux -tags nogui and GOOS=windows on
  plugin, provider, gui, cli: ok. gofmt -l: clean.
- agent-model-load.test.cjs: chromium and webkit, 1000 and 440px, en,
  zh, zh-TW, ja, de: 40 of 40. Screenshots looked at in both engines.
- Whole GUI suite: not finished. Three whole-suite runs (WSL, mote) were
  cut at 1,266 and 642 tests by load and a session restart; their reds
  were all in provider-editor, Settings and Usage files. Then the files
  a new API call or the Agents row can reach (183: every agent-*, every
  model/picker/speed/load file, the five strict-fixture files, every
  file using .field, ag-open, renderAgents or /api/drift, i18n-keys),
  split mote/WSL, both engines: reds only in model-chip-steady (#4388),
  mobile-web (#3952), model-copy-id (#4621), balance-fix (#4683),
  allow-ips (#4357, output added), agent-update-all (#2988), account-eye
  (#4244), models-works-only, models-select-all-api, model-suffix-own,
  provider-detect-models, provider-models-rest, remote-magpie,
  routing-rule-models, usage-speed (WebKit on WSL, #4218/#4471). Each red
  file was run alone at this head and at origin/main b4073c06a: the same
  reds both sides, except agent-update-all's fixed 200ms wait (#2988),
  red once at the head. codex-subagent-model, red under load, passed
  alone.

Broken by hand, each red, then restored: the picker row's badge line,
fieldBtn's badge line (red at the renderAgents redraw check), the
readLoads painter, and ModelLoads' listed-id loop (TestModelLoadsByRef).

Not tried: a real plugin reporting load in the live app; the tray panel
layout; WebKitGTK in the real Linux app and the Windows app.
2026-11-03 05:21:47 +08:00

190 lines
10 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
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 date = "2026-09-28";
const usage = Array.from({ length: 40 }, (_, i) => ({
agent: "fixture", cwd: "/projects/project-" + String(i + 1).padStart(2, "0"),
model: "model-" + String(i + 1).padStart(2, "0"), input: 40000 - i * 500,
output: 1000, cache_read: 0, cache_write: 0, cost: 1, priced: true,
}));
const sessions = usage.map((u, i) => ({
...u, id: String(i), title: "Session " + (i + 1), name: "Fixture",
last: date + "T08:00:00Z", start: date + "T07:00:00Z", models: [u], unpriced: 0,
}));
const state = {
agents: [{ id: "fixture", name: "Fixture", path: "/test/config.toml", wired: true, fields: [{
key: "model", label: "model", value: "model-01",
options: usage.map(u => ({ value: u.model, label: u.model, ref: u.model })),
}] }],
profiles: [], settings: { lang: "en", theme: "light" },
};
async function serve(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:"en",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/sessions/progress") return json({ indexing: false });
if (url.pathname === "/api/sessions") return json({ sessions, dirs: ["/test/sessions"] });
if (url.pathname === "/api/sessions/stats") return json({ from: date, to: date, days: [{ date, usage, active: [] }], agents: { fixture: "Fixture" } });
if (url.pathname === "/api/sessions/overview") return json({ count: sessions.length, days: [sessions.length], top: { tokens: [], cost: [], active: [] } });
if (url.pathname === "/api/update" || url.pathname === "/api/drift") return json({});
if (url.pathname === "/api/agents/cli") return json({ agents: {}, pending: false });
if (url.pathname === "/api/groups") return json({ groups: [] });
if (url.pathname === "/api/gateway/trace") return json({ mine: false, now: date, totals: { requests: 0, rerouted: 0, errors: 0 } });
if (url.pathname === "/api/plugins/updates") return json({ waiting: [] });
if (url.pathname === "/api/usage/quotas" || url.pathname === "/api/usage/quotas/history" || url.pathname === "/api/agents/install" || url.pathname === "/api/plugins/agents") return json([]);
if (url.pathname === "/api/whatsnew" || url.pathname === "/api/model-speeds" || url.pathname === "/api/model-loads") return json({});
if (url.pathname === "/api/upstream") return json({ vendors: [], providers: {} });
// the sidebar's plugin pages, and plugin panels in magpie's pages: none
if (url.pathname === "/api/plugin-pages" || url.pathname === "/api/plugin-panels") return json([]);
// the sidebar's gateway line, when no page has read the providers
if (url.pathname === "/api/providers") return json({ providers: [], gateway: { url: "http://127.0.0.1:3425", running: true } });
assert(!url.pathname.startsWith("/api/"), "Unexpected API: " + url.pathname);
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 });
}
async function scrolled(page, selector) {
await page.waitForFunction(selector => {
const box = document.querySelector(selector);
return !box || box.hidden || box.scrollTop > 0;
}, selector);
const box = page.locator(selector);
assert(await box.isVisible(), "scrolling must leave the dropdown open");
assert(await box.evaluate(e => e.scrollTop > 0), "the dropdown must actually scroll");
}
async function scrollMenu(page, selector, gesture) {
const box = page.locator(selector);
assert(await box.evaluate(e => e.scrollHeight > e.clientHeight), "the fixture must overflow");
// the menu's opening come to rest first: a click on its scrollbar's track
// while it still moved landed nowhere, now and then (both engines)
await page.evaluate(() => Promise.all(document.getAnimations().map(a => a.finished.catch(() => {}))));
const r = await box.boundingBox();
if (gesture === "wheel") {
await page.mouse.move(r.x + r.width / 2, r.y + r.height / 2);
await page.mouse.wheel(0, 240);
} else if (gesture === "track") {
await page.mouse.click(r.x + r.width - 6, r.y + r.height - 25);
} else {
await page.mouse.move(r.x + r.width - 6, r.y + 20);
await page.mouse.down();
await page.mouse.move(r.x + r.width - 6, r.y + 200, { steps: 10 });
await page.mouse.up();
}
await scrolled(page, selector);
}
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
test(engine + ": scrollable dropdowns", async t => {
assert(["chromium", "webkit"].includes(engine), "BROWSER must be chromium or webkit");
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium", ignoreDefaultArgs: ["--hide-scrollbars"] }));
const context = await browser.newContext({ viewport: { width: 1100, height: 800 }, reducedMotion: "reduce" });
const page = await context.newPage();
page.setDefaultTimeout(5000);
page.setDefaultNavigationTimeout(60000);
const errors = [];
page.on("pageerror", e => errors.push(e.message));
await page.route("**/*", serve);
await page.addInitScript(() => localStorage.setItem("magpie.usageTab", "sessions"));
await context.tracing.start({ screenshots: true, snapshots: true });
t.after(async () => {
try {
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 + ".png") });
await context.tracing.stop({ path: path.join(process.env.ARTIFACT_DIR, engine + ".zip") });
} else await context.tracing.stop();
} finally { await browser.close(); }
});
async function reset() {
await page.goto("http://magpie.test/?view=usage");
await page.locator("#sessFolder:not([hidden])").waitFor();
assert.equal(await page.title(), "magpie");
assert.equal(await page.locator("#sessList .sess-item").count(), 40);
// Always-visible scrollbars make track/thumb input independent of OS preferences.
await page.addStyleTag({ content: `
.sess-menu, #list { animation: none !important; scrollbar-width: auto; scrollbar-gutter: stable; }
:is(.sess-menu, #list)::-webkit-scrollbar { width: 14px; }
:is(.sess-menu, #list)::-webkit-scrollbar-thumb { background: #888; }
:is(.sess-menu, #list)::-webkit-scrollbar-track { background: #eee; }
` });
await page.evaluate(async () => {
await document.fonts.ready;
await Promise.all(document.getAnimations().map(a => a.finished.catch(() => {})));
});
}
for (const filter of ["sessFolder", "sessModel"]) {
for (const gesture of ["wheel", "track", "drag"]) {
await t.test(filter + ": " + gesture, async () => {
await reset();
await page.locator("#" + filter).click();
await scrollMenu(page, ".sess-menu", gesture);
await page.keyboard.press("Escape");
assert.equal(await page.locator(".sess-menu").count(), 0);
assert(await page.locator("#" + filter).evaluate(e => e === document.activeElement));
});
}
await t.test(filter + ": keyboard scroll and selection", async () => {
await reset();
await page.locator("#" + filter).click();
await page.keyboard.press("ArrowUp");
await scrolled(page, ".sess-menu");
assert(await page.locator(".sess-menu .pm-item").last().evaluate(e => e === document.activeElement));
await page.keyboard.press("Enter");
assert.equal(await page.locator(".sess-menu").count(), 0);
assert.match(await page.locator("#" + filter).innerText(), /(?:project|model)-40/);
assert.equal(await page.locator("#sessList .sess-item").count(), 1);
assert.equal(await page.locator("#sessList .name").innerText(), "Session 40");
});
}
await t.test("outside click, page scroll, resize and reopen", async () => {
await reset();
const anchor = page.locator("#sessFolder");
for (const dismiss of ["outside", "page scroll", "resize"]) {
await reset();
await anchor.click();
await page.locator(".sess-menu").waitFor();
if (dismiss === "outside") { const bar = await page.locator("#sessionsPane .sess-tools").boundingBox(); await page.mouse.click(bar.x + bar.width - 6, bar.y + bar.height / 2); }
else if (dismiss === "page scroll") {
await page.mouse.move(1000, 650);
await page.mouse.wheel(0, 200);
} else await page.setViewportSize({ width: 1101, height: 800 });
await page.locator(".sess-menu").waitFor({ state: "detached" });
}
await anchor.click();
await scrollMenu(page, ".sess-menu", "wheel");
});
await t.test("main model picker keeps scrolling", async () => {
await reset();
await page.locator('#nav [data-view="agents"]').click();
for (const gesture of ["wheel", "track", "drag"]) {
await page.locator('#agents [data-key="model"]').click();
await scrollMenu(page, "#list", gesture);
await page.locator("#q").press("Escape");
assert(await page.locator("#pop").isHidden());
}
});
await t.test("main model picker closes on a second click", async () => {
const anchor = page.locator('#agents [data-key="model"]');
await anchor.click();
assert(await page.locator("#pop").isVisible());
await anchor.click();
assert(await page.locator("#pop").isHidden());
await anchor.click();
assert(await page.locator("#pop").isVisible(), "a third click opens it again");
await page.locator("#q").press("Escape");
});
assert.deepEqual(errors, [], "page runtime errors");
});
}