Files
magpie/internal/gui/tests/plugin-discover-virtual.test.cjs
T
yetone 5a0e0378c0 plugins: Discover draws only the cards near the view in a long list
The owner, 11-13: "这个页面有点卡,记得做列表虚拟化" (Plugins › Discover with
Subscriptions picked, 369 cards). Every redraw of Discover's body (npm's
numbers coming in, a count changing) made all of them again.

A grid of 48 cards or more (a kind picked, a search that matches hundreds,
npm's hits) now draws only its rows near the view, 700px past each edge,
and keeps the whole list's height in its padding above and below. The rows
follow the scroll and the width (a ResizeObserver on the page): columns are
read from the grid's own computed columns, so narrow widths and every
language lay out as before. Row heights are measured as they are drawn. A
card with focus stays in the page, held where it would be, so keys go on
from it. Cards are made once per list and kept, so a click lands on the
same node. A list shorter than 48 is drawn whole, as before.

The code is in plugingrid.js, imported when Plugins first draws, so the
core doesn't grow: TestCoreScriptBudget was 3,299 bytes over with it in
plugins.js. Until the import lands, a long list is drawn whole, and the
body is drawn again once it has.

Measured with plugin-discover-virtual.test.cjs's PERF=1 on mote (load
~25 to 55, so the times are high). The time is from the Subscriptions
click to the cards drawn, with a market of 503 plugins (370 subscriptions).
- Before (origin/main): chromium 69.0ms at 1180px and 56.5ms at 420px,
  webkit 77ms and 74ms. 370 cards and 9,312 nodes in #view-plugins.
- After: chromium 6.2 to 9.7ms, webkit 10 to 13ms. 30 cards and 810 nodes
  at 1180px, 10 cards and 312 nodes at 420px (de: 9 and 285).

Guard: plugin-discover-virtual.test.cjs (new), in chromium and webkit, in
en, zh, zh-TW, ja and de, at 1180px and 420px. It checks:
- at most 72 cards are drawn, and the list keeps its whole height;
- wheeling to the middle never shows an empty stretch, and the focused
  card is kept;
- at the bottom pv-369 is in view and its Install posts its spec;
- "pv-12" lists all 11 matches, and "pv" draws at most 72;
- nothing scrolls sideways, and there are no page errors.

Broken by hand: VMIN at 1e9 (every list drawn whole) turned it 22 of 22 red.

Run on mote at this tree, before the rebase (mote-run):
- go test ./internal/gui: ok (the budget too). gofmt -l: nothing.
- plugin-discover-virtual: 22 of 22.
- Every GUI file that draws plugin cards or Plugins (the 54 that grep finds
  pm-card, view=plugins or pluginAgentsTo), with i18n-keys and
  i18n-en-no-cjk: all pass, except the reds below.
Reds, the same at origin/main be8e352a2:
- settings-lines-wrap: chromium de 440px (#6353).
- old-webkit: headerpresets.js isn't in the list (#6030), and app.css
  uses :has() (#5919). Its plugingrid.js entry is added here.
- title-bar-tools: webkit ja went red once in the first run, then passed
  10 of 10 when run again (#2740, #4716).

Run again at this commit, rebased on origin/main 7583ed65c (mote-run).
plugin-discover-virtual 22 of 22. plugin-market, plugin-kinds-discover,
plugins-more-discover, plugin-progressive, i18n-keys and i18n-en-no-cjk
all pass. TestCoreScriptBudget passes. Reds, the same at 7583ed65c run
alone:
- TestProviderInfoReadsTheListOnce: a row is ~10 lists against a bound of
  5 (#6375, filed now).
- plugin-card-update: webkit red 1 of 2 on both trees (#3079).
- old-webkit: as above (#6030, #5919).

Not tried: the real app on Linux WebKitGTK, and the real market from
usemagpie.ai.
2026-11-13 06:08:46 +08:00

157 lines
9.8 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// Discover with a market of 500 plugins stays light: a kind picked (369
// subscriptions) or a search that matches hundreds draws only the cards
// near the view, while the list keeps its whole height. Scrolled to the
// bottom the last card is there and its Install works; a card with focus
// stays in the page while the reader scrolls away; a narrow search still
// lists every match. Every language, both engines, wide and narrow; the API
// is faked. PERF=1 prints how long a kind takes to draw.
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(process.env.MAGPIE_ASSETS || path.join(__dirname, "../assets"));
// 370 subscriptions and the rest, the most downloaded first: pv-0 … pv-369
const KINDS = [["pv", 370, null], ["mw", 50, "middleware"], ["ag", 48, "agent"], ["tl", 8, "tools"], ["pg", 27, "page"]];
const listings = [];
for (const [k, n, kind] of KINDS) for (let i = 0; i < n; i++) {
const l = { package: `@x/${k}-${i}`, name: `${k}-${i}`, community: i % 3 === 0, summary: { en: `Plugin ${k} number ${i}, a summary long enough to take two lines of a card at its narrowest width` }, npm: { version: "0.1." + i, weekly: 100000 - i } };
if (kind) l.kind = kind; else l.providers = [`${k}${i}`];
listings.push(l);
}
const npm = Object.fromEntries(listings.map((l) => [l.package, l.npm]));
const state = { bun: true, bunVersion: "1.3.0", plugins: [] };
const added = [];
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" } });
if (url.pathname === "/api/providers") return json({ providers: [], presets: [], excluded: [], gateway: { running: true, window: true }, plugins: [] });
if (url.pathname === "/api/groups") return json({ groups: [], pools: [] });
if (url.pathname === "/api/gateway/trace") return json({ routes: [] });
if (url.pathname === "/api/plugins") return json(state);
if (url.pathname === "/api/plugins/listings") return json({ listings });
if (url.pathname === "/api/plugins/github") return json({ listings: [] });
if (url.pathname === "/api/plugins/npm") return json({ npm });
if (url.pathname === "/api/plugins/search") return json({ hits: [] });
if (url.pathname === "/api/plugins/add") { added.push(route.request().postDataJSON()?.spec); 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)];
try { await route.fulfill({ body: await fs.readFile(file), contentType }); } catch { await route.fulfill({ status: 404, body: "" }); }
};
}
const L = { en: "Plugins", zh: "插件", "zh-TW": "外掛", ja: "プラグイン", de: "Erweiterungen" };
// a page of cards and the rows around it, never the whole market
const MOST = 72;
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
test(engine + ": Discover draws the cards near the view, not all 500", async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
for (const lang of (process.env.LANGS ? process.env.LANGS.split(",") : Object.keys(L))) for (const width of [1180, 420]) {
await t.test(lang + " " + width, async () => {
const page = await (await browser.newContext({ viewport: { width, height: 820 } })).newPage();
page.setDefaultTimeout(8000);
page.setDefaultNavigationTimeout(60000);
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", server(lang));
await page.goto("http://magpie.test/?view=plugins");
const view = page.locator("#view-plugins");
await view.locator(".pm-kinds .opt").nth(1).waitFor();
// npm's numbers in, so nothing redraws under the measure
await view.locator(`.pm-card[data-pkg="@x/pv-0"] .pm-act.get`).waitFor();
// Subscriptions, timed from the click to the cards drawn
const ms = await page.evaluate(() => {
const t0 = performance.now();
document.querySelectorAll("#view-plugins .pm-kinds .opt")[1].click();
document.body.offsetHeight;
return performance.now() - t0;
});
const sec = view.locator('.pm-sec[data-kind="pv"]');
await sec.waitFor();
await page.evaluate(() => new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r))));
const cards = await sec.locator(".pm-card").count();
const nodes = await page.evaluate(() => document.querySelectorAll("#view-plugins *").length);
if (process.env.PERF) console.log(`${engine} ${lang} ${width}: Subscriptions drawn in ${ms.toFixed(1)}ms, ${cards} cards, ${nodes} nodes`);
assert.ok(cards > 0 && cards <= MOST, `${cards} of 370 cards in the page`);
// the list keeps its whole height: 370 cards' rows
const geo = await sec.locator(".pm-grid").evaluate((g) => {
const cols = getComputedStyle(g).gridTemplateColumns.split(" ").length;
return { cols, h: g.getBoundingClientRect().height, card: g.querySelector(".pm-card").getBoundingClientRect().height };
});
const rows = Math.ceil(370 / geo.cols);
assert.ok(geo.h >= rows * geo.card, `the list is ${geo.h}px for ${rows} rows of ${geo.card}px`);
// a card with focus stays while the reader scrolls away from it
const first = sec.locator('.pm-card[data-pkg="@x/pv-0"]');
await first.focus();
const inView = () => page.evaluate(() => {
const p = document.querySelector("#view-plugins").getBoundingClientRect();
return [...document.querySelectorAll('#view-plugins .pm-sec[data-kind="pv"] .pm-card')]
.filter((c) => { const r = c.getBoundingClientRect(); return r.bottom > p.top && r.top < p.bottom; }).map((c) => c.dataset.pkg);
});
// wheeled as a reader does (the app puts back a scroll that isn't
// the reader's), each step landed before the next; at every stop
// the view has cards in it, never an empty stretch
const box = await view.boundingBox();
await page.mouse.move(box.x + box.width / 2, box.y + box.height / 2);
const scrollTo = async (f) => {
for (let i = 0; i < 200; i++) {
const [top, max] = await view.evaluate((v) => [v.scrollTop, v.scrollHeight - v.clientHeight]);
const want = Math.round(max * f);
if (Math.abs(want - top) < 2 || (f === 1 && top >= max - 1)) break;
await page.mouse.wheel(0, Math.max(-2500, Math.min(2500, want - top)));
for (let k = 0; k < 50 && (await view.evaluate((v) => v.scrollTop)) === top; k++) await page.waitForTimeout(20);
await page.evaluate(() => new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r))));
assert.ok((await inView()).length > 0, "an empty stretch at " + (await view.evaluate((v) => v.scrollTop)) + "px");
}
await page.evaluate(() => new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r))));
};
await scrollTo(0.5);
const mid = await inView();
assert.ok(mid.length > 0, "cards in the view halfway down");
assert.ok(await page.evaluate(() => document.activeElement?.dataset?.pkg === "@x/pv-0" && document.activeElement.isConnected), "the focused card is kept");
assert.ok((await sec.locator(".pm-card").count()) <= MOST + 1);
// the bottom: the last card, and its Install works
await scrollTo(1);
const end = await inView();
assert.ok(end.includes("@x/pv-369"), "the last card at the bottom: " + end.join(" "));
added.length = 0;
await sec.locator('.pm-card[data-pkg="@x/pv-369"] .pm-act.get').click();
for (let i = 0; i < 100 && !added.length; i++) await page.waitForTimeout(20);
assert.deepEqual(added, ["@x/pv-369"]);
// a search: a few matches are all there; hundreds are drawn near the view
await scrollTo(0);
const q = view.locator(".pm-find input");
await q.fill("pv-12");
const hits = view.locator(".pm-sec").filter({ has: page.locator(".pm-sechead h3", { hasText: new RegExp("^" + L[lang] + "$") }) });
await hits.locator('.pm-card[data-pkg="@x/pv-129"]').waitFor();
assert.equal(await hits.locator(".pm-card").count(), 11);
await q.fill("pv");
await hits.locator('.pm-card[data-pkg="@x/pv-0"]').waitFor();
await page.evaluate(() => new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r))));
const many = await hits.locator(".pm-card").count();
assert.ok(many > 0 && many <= MOST, `${many} of 370 matches in the page`);
const over = await page.evaluate(() => document.scrollingElement.scrollWidth - document.scrollingElement.clientWidth);
assert.ok(over <= 0, "page scrolls sideways by " + over);
assert.deepEqual(errors, []);
await page.context().close();
});
}
});
}