Files
magpie/internal/gui/tests/view-middle-autoscroll.test.cjs
T
yetone cba59b05e1 gui: the middle button's autoscroll scrolls a page instead of twitching (#2767)
#2767 (znbsf, with AvatarGanymede's recording on the Discover page) and
多喝热水 on Discord, on Windows: pressing the wheel and moving the mouse
made a page twitch instead of scrolling, or pulled it back. znbsf's patch
found the same cause.

Chromium and WebView2 give the autoscroll a pointerdown of button 1 and
then no wheel, touch or drag of the reader's. The "where the reader is"
rule put back each of its scrolls as it came, so the page twitched
between where it went and where it was.

What changed (internal/gui/assets/app.js, the click guard):
- From a trusted middle press on a view that can scroll (`panning`),
  that view's scrolls count as the reader's, and what a click held is let
  go. This lasts until the autoscroll ends as Chromium ends it: on the
  next press (seen only as a pointerup with no pointerdown), on a key, on
  blur, or on the lift after the mouse moved more than 15px while held.
- A field the guard brings back into view after putting a scroll back
  must have been focused since the reader last scrolled that view
  (`scrolledFrom`). Before, the button the middle press focused, which
  the autoscroll or the wheel took out of sight, was brought back by the
  next scroll that was put back, and the page jumped to it.
- docs/subsystems/gui-shell.md says both.

Guarded by internal/gui/tests/view-middle-autoscroll.test.cjs: a real
middle press and move in Chromium, en and zh, a scroll by code still put
back after the autoscroll ends, and WebKit (no autoscroll) left as it
was.

Broken by hand:
- app.js put back to its base: Chromium 0/3 ("the autoscroll scrolled
  the page: at 0, passed 0,0,0,…"). WebKit passes, as expected.
- Only the panning line taken out: Chromium 0/3, same message.
- Only the scrolledFrom check taken out: Chromium 0/3 ("after the
  autoscroll: a scroll by code is put back (was 374, asked 1212, at
  126)").

Record for this push: 13 commits on origin/main ca4a1cde0, head tree
539c84e92. Go 1.26.8, sandbox HOME, GOMODCACHE/GOCACHE pinned. The full Go
run and vet were at tree d6393dfc8, the same Go code as this head: only
app.js comments (cut under the core script budget) and one ja string
changed after it.
- gofmt -l: nothing. go build ./...; go vet ./... (darwin), GOOS=linux
  go vet -tags nogui ./..., GOOS=windows go vet ./...: clean.
- go test -timeout 45m ./..., through tee, one suite on the machine:
  68 packages ok. The one red was internal/gui's
  TestCoreScriptBudget: app.js 1,335,314 > 1,335,000, since main grew
  app.js under the stack. The autoscroll commit's own comments were cut
  (to 1,334,629) rather than the budget raised, and go test -count=1
  ./internal/gui is ok at this head (113s). Packages the run took from
  Go's test cache had the same inputs.
- GUI: the 18 files this stack touches or names, both engines,
  202 of 204 pass (tree 068acf128, this push less the last commit). The
  two reds: gui-ja, red on main since c2636b7b5 and fixed by this push's
  last commit (gui-ja and i18n-keys 6 of 6 there), and usage-fold's
  chromium "several accounts" (#2163, red on main the same way).
- view-middle-autoscroll and click-scroll pass, both engines.
- plugin-github's "a click scrolls nothing" (1px) was red under the
  whole run's load and passes alone at the stack and on main (#3218).
  The whole GUI suite ran at 1c1fc7183 (this stack's first 12 commits on
  0ddcf28a1): 11713 of 11769 pass. Each of the 21 red files was then run
  alone at the stack and at origin/main 8278fd9b0. Eight are red the same
  way on main: plugin-card-update (#3079), quota-count-wrap and
  quota-name-translated (#2228), s3-sync (#2448), usage-agents-hide
  (#2276), usage-checkin (#3091), usage-fold's chromium "several
  accounts" (#2163), usage-ledger (#3104). The other 13 pass alone on both
  and were red only under that run's load (50 to 335): currency #3101,
  plugin-github #3218, provider-fetch-again #3292, rest-try-now #2564,
  routing-auto-reset #3293, routing-keys-fold #1612, routing-steady #1804,
  routing-wb-restricted #1905, sessions-folder-delete #2218,
  sessions-manage #3294, switch-only #3295, text-size #1866, usage-pack
  #3296.

Not tried: the real Windows app (WebView2) on the Windows box; only
headless Chromium's autoscroll. Linux WebKitGTK has no middle-button
autoscroll, so it is left as WebKit is.
Not done: znbsf's patch doesn't start this on a middle press on a link
(which opens a tab, not an autoscroll). Here it does start, and lets a
scroll by code in that view stand until the next press or key. Left on
#2767, which stays open for the reporters.
2026-10-23 01:58:01 +08:00

168 lines
9.1 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// A page scrolled by the middle button's autoscroll (多喝热水 on Discord,
// Windows): pressing the wheel and moving the mouse scrolls the view with no
// wheel, touch or drag of the reader's for the page to see. Chromium and
// WebView2 send a pointerdown of button 1 and then only pointer moves with no
// button held (the press is let go at once and the autoscroll runs on), or
// none at all while the mouse is held still. The "where the reader is" rule
// put each of those scrolls back as it came, and the page twitched between
// where the autoscroll took it and where it was. A scroll the reader never
// asked for (code setting scrollTop) is still put back once the autoscroll
// has ended, by the next press of a button, as Chromium ends it. The middle
// press focuses the button under it, and the autoscroll takes it out of
// sight: the next scroll put back brought it back into view, and the page
// jumped to it. So did a button a press focused and the wheel scrolled away.
// Chromium only scrolls: WebKit has no middle-button autoscroll, and there the
// press must leave the rule as it was. 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 AGENTS = Array.from({ length: 14 }, (_, i) => ({
id: "a" + i, name: "Agent " + i, path: "/t/a" + i + ".json", icon: "generic", wired: true,
fields: [{ key: "model", label: "model", value: "m", options: [{ value: "m", label: "M", ref: "a/m" }] }],
}));
const VIEWPORT = { width: 440, height: 620 };
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:true};` });
if (url.pathname === "/wails/runtime.js") return route.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
if (url.pathname === "/api/state") return json({ agents: AGENTS, profiles: [], settings: { lang, theme: "light" }, fx: { rate: 7.2, at: new Date().toISOString() } });
if (url.pathname === "/api/providers") return json({ providers: [], gateway: { running: true, window: true } });
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 });
};
}
// the page's height once it has stopped changing
async function settled(p, view) {
let last = -1;
for (let i = 0; i < 40; i++) {
const h = await view.evaluate((v) => v.scrollHeight);
if (h === last) break;
last = h;
await p.waitForTimeout(120);
}
return view.evaluate((v) => v.scrollHeight - v.clientHeight);
}
// Every scroll position the view passes through, read in the page each frame:
// a scroll put back shows as a step up while the autoscroll goes down.
async function record(view) {
await view.evaluate((v) => {
window.__tops = [];
const read = () => { window.__tops.push(v.scrollTop); window.__rec = requestAnimationFrame(read); };
v.addEventListener("scroll", () => window.__tops.push(v.scrollTop), { passive: true });
read();
});
}
const recorded = (view) => view.evaluate(() => { cancelAnimationFrame(window.__rec); return window.__tops.splice(0); });
const top = (view) => view.evaluate((v) => v.scrollTop);
const backs = (tops) => tops.slice(1).filter((y, i) => y < tops[i] - 0.5).length;
// a scroll by code is put back, as it was before any middle press
async function codePutBack(p, view, over, what) {
await p.waitForTimeout(400); // the reader's last scroll lets the next 250ms stand
const cur = await top(view);
const want = cur > over / 2 ? 0 : over;
await view.evaluate((v, y) => { v.scrollTop = y; }, want);
await p.waitForTimeout(250);
const now = await top(view);
assert.ok(Math.abs(now - cur) < 1, `${what}: a scroll by code is put back (was ${cur}, asked ${want}, at ${now})`);
}
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
test(engine + ": the middle button's autoscroll scrolls a page", { timeout: 120000 }, async (t) => {
assert(["chromium", "webkit"].includes(engine), "BROWSER must be chromium or webkit");
// Chromium has the autoscroll only on Windows by default; WebView2 is that
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium", args: ["--enable-blink-features=MiddleClickAutoscroll"] }));
t.after(() => browser.close());
for (const lang of ["en", "zh"]) {
await t.test(lang, async () => {
const errors = [];
const ctx = await browser.newContext({ viewport: VIEWPORT, reducedMotion: "reduce" });
const p = await ctx.newPage();
p.setDefaultTimeout(8000);
p.on("pageerror", (e) => errors.push(e.message));
await p.route("**/*", server(lang));
const view = p.locator("#view-agents");
await p.goto("http://magpie.test/?view=agents");
await p.locator("#view-agents .ag-lead").first().waitFor();
const over = await settled(p, view);
assert.ok(over > 150, `the page must overflow: ${over}`);
const r = await view.boundingBox();
// pressed on a button near the page's top, which the press focuses
const b = await view.locator("button.ag-start").first().boundingBox();
const x = b.x + b.width / 2, y = b.y + b.height / 2;
// pressed and let go, then the mouse moved below where it was pressed
await record(view);
await p.mouse.move(x, y);
await p.mouse.down({ button: "middle" });
await p.mouse.up({ button: "middle" });
for (let i = 1; i <= 6; i++) { await p.mouse.move(x, y + i * 12); await p.waitForTimeout(40); }
// held still: the autoscroll runs on with no event of the reader's
await p.waitForTimeout(600);
let tops = await recorded(view);
const ran = await top(view);
if (engine === "chromium") {
assert.ok(ran > 60, `the autoscroll scrolled the page: at ${ran}, passed ${tops.join(",")}`);
assert.equal(backs(tops), 0, `no scroll was put back under the autoscroll: ${tops.join(",")}`);
}
// the next press ends it, and with it the reader's scroll: it was a
// left press in the view's own padding, away from its scrollbar, which does nothing
await p.mouse.click(r.x + 3, r.y + r.height - 4);
await p.waitForTimeout(150);
const ended = await top(view);
await p.waitForTimeout(300);
assert.ok(Math.abs((await top(view)) - ended) < 1, "the autoscroll ended on the next press");
await codePutBack(p, view, over, "after the autoscroll");
// a button a press focused, then scrolled away by the wheel: a scroll
// put back leaves the page where the wheel took it
await p.mouse.wheel(0, -2000);
await p.waitForTimeout(400);
await p.mouse.move(x, y);
await p.mouse.down();
await p.mouse.move(r.x + 3, r.y + r.height - 4, { steps: 4 });
await p.mouse.up();
await p.mouse.move(r.x + r.width / 2, r.y + r.height / 2);
await p.mouse.wheel(0, 500);
let wheeled = NaN;
for (let i = 0; i < 40 && wheeled !== (wheeled = await top(view)); i++) await p.waitForTimeout(80);
assert.ok(wheeled > 100, `the wheel scrolled the page: ${wheeled}`);
await codePutBack(p, view, over, "after the wheel took a focused button away");
// pressed and held while the mouse moves, let go once it has gone on
if (engine === "chromium") {
// from where the wheel left the page, pressed low in the view
const before = await top(view);
await p.mouse.move(r.x + r.width / 2, r.y + r.height - 60);
await record(view);
await p.mouse.down({ button: "middle" });
for (let i = 1; i <= 6; i++) { await p.mouse.move(r.x + r.width / 2, r.y + r.height - 60 - i * 12); await p.waitForTimeout(40); }
await p.waitForTimeout(500);
tops = await recorded(view);
const now = await top(view);
assert.ok(now < before - 40, `the held autoscroll scrolled the page up: ${before} to ${now}`);
assert.equal(tops.slice(1).filter((v, i) => v > tops[i] + 0.5).length, 0, `no scroll was put back under the held autoscroll: ${tops.join(",")}`);
// the lift after a move ends a held one
await p.mouse.up({ button: "middle" });
await p.waitForTimeout(300);
await codePutBack(p, view, over, "after the held autoscroll");
}
assert.deepEqual(errors, []);
await ctx.close();
});
}
});
}