Files
magpie/internal/gui/tests/reader.cjs
T
yetone 208bd5c48b gui: Settings descriptions wrap instead of ending in … (arculongus on Discord)
arculongus (English UI, screenshot) couldn't read many Settings rows: the
line under a setting's name ended in "…". The generic `.row .sub` rule holds
a row's line to one line with an ellipsis; a few Settings rows opted out
(`.wraps`) and the rest were cut, among them Image generation's ChatGPT
note, Image recognition MCP server and Web search's Searches for other
models.

Fixed for the class, not the rows in the screenshot: every setting row's
line (`.row.pref .sub`) now wraps whole beside its control. That covers
every Settings part, Plugins › Built-in and an agent's own settings (the
same rows). The control keeps its size (`.prefs .val` is `flex: none`,
max 60%), and nothing else in the layout moves. The window is never
narrower than 560pt, so a row stays a few lines at most. Lines elsewhere (provider
and account cards, chips, URLs) stay one line; their full text is already
reachable through the hover title.

Tests:
- New settings-lines-wrap.test.cjs checks every visible Settings part plus
  Plugins › Built-in, in chromium and webkit, en/zh/zh-TW/ja/de, at
  1000px and 440px. No row's line is cut or nowrap, each control stays
  inside its row, and the page never scrolls sideways (more than 30 rows
  checked). It also checks the three reported rows by name.
- Tests that assumed one-line rows now follow the taller rows:
  - searcher-pick: the help wraps rather than ellipsizing with a title.
  - settings-general: one line only at 1000px, never cut at either width.
  - reader.cjs reach(): goes up to 8,000px, not 2,400.
  - member-model-setting, turn-done, connect-address-kept, tray-cost and
    tray-totals: wheel to the control with reach() before
    clicking, not scrollIntoViewIfNeeded or Playwright's own scroll, which
    the app undoes.
  - update-download, update-restart and tray-totals: a click whose row
    grows a line may scroll, but only to keep the clicked control under
    the pointer (the app's hold, #2134). tray-totals allows 2px, because
    WebKit's whole-pixel scrolls drifted 1.3px over four holds.
  - mobile-web: compares against an origin/main baseline without this
    change.
- Docs: gui-shell.md and gateway-routing.md.

Run (worktree from origin/main a6399dd49, then rebased):
- The new test passes 20/20. Broken by hand: pointed at the old assets
  (MAGPIE_WRAP_ASSETS), it is red 10/10 in chromium and red in webkit
  ("general: a row's line is cut short").
- Whole GUI suite (872 files) in two runs, at concurrency 3-4 and load
  20-150:
  - The 170 Settings-reaching files first: 2515/2575.
  - Then the other 702: 9767/9920.
  - Each red was rerun on settings-wrap-base. The files this change made
    red (settings-general, turn-done, update-download, update-restart,
    member-model-setting, codex-auto-reset-settings, mobile-web,
    connect-address-kept, quota-reads, tray-cost, tray-totals) are fixed
    above and pass on rerun: 86/86, 34/34, 112/112 and 50/50.
  - Also red on origin/main, already filed:
    - redact-words-any-case: #3500
    - s3-sync webkit: #2448
    - usage-checkin Qoder tab: #2549
    - plugin-card-update: #3079
    - builtins and gateway-mode masking tab: #3498
    - fonts webkit: #3227
  - The other files red on origin/main 31bff66be, run alone at a
    comparable count, all have issues: routing-pause #3410,
    routing-rule-effort #3411, routing-rule-remove #3094/#3096,
    routing-intent-entry #3408, routing-live-patch #2174, routing-manual
    #3409, routing-steady #3412, menu-input-scroll #3402, header-centre
    #3479, gateway-caller-keys #2572, gateway-key-models #2648,
    gateway-key-limit #3523, quota-count-wrap #3404, usage-agents-hide
    #3414, usage-fold #3415, usage-ledger #3001/#3104, sidebar and
    gateway-mode #3498, account-discovery #3497, sidebar-overlay-scrollbar
    #1863, account-alias #2984.
  - provider-user-agent failed only on browserType.launch timeouts (load)
    and passed on rerun.
- After the rebase onto b027ca264, these were run again at the pushed head: the
  14 changed or affected GUI files (the new test, the eleven above,
  quota-reads, which main already wheels, and i18n-keys) passed 302/302.
  go test -tags nogui ./internal/gui (budgets included) was ok, with no
  budget raised.
- Before and after screenshots, Settings › Models at 1000px and 700px,
  chromium and webkit, were looked at: the rows that ended in an ellipsis
  now read whole, and each control has the size and place it had before.

Not tried: the real app (WKWebView, WebKitGTK, WebView2). This is a CSS
white-space change with no motion and no new API.
2026-10-25 09:21:42 +08:00

180 lines
9.3 KiB
JavaScript

// The app deliberately undoes unsolicited scrollIntoView (including Playwright's
// automatic scroll). Bring a control into sight with real wheel input first.
// Wholly in sight: one cut off by a fraction of a pixel at the view's foot
// was scrolled the rest of the way by Playwright's click itself, before the
// app saw the click, and the page moved 1px under it (860 to 861 on the
// Usage page's Fetch from archive, once the archive switch in its header
// had moved the rows 4px down).
async function inView(page, locator) {
await locator.waitFor({state:"visible"});
if (await locator.evaluate(e => !e.closest("#view-usage"))) return;
const view = page.locator("#view-usage");
const v = await view.boundingBox();
await page.mouse.move(v.x + v.width / 2, v.y + v.height / 2);
for (let i = 0; i < 80; i++) {
const b = await locator.boundingBox();
if (!b) { await page.waitForTimeout(40); continue; }
// one taller than the view: its top in sight
if (b.y >= v.y && (b.y + b.height <= v.y + v.height || (b.height > v.height && b.y < v.y + v.height))) {
await page.waitForTimeout(300);
return;
}
const delta = b.y < v.y ? -20 : 20;
await page.mouse.wheel(0, delta);
await page.waitForTimeout(40);
}
throw new Error("control did not come into sight: " + JSON.stringify({view:v,target:await locator.boundingBox(),scroll:await view.evaluate(e=>e.scrollTop)}));
}
// reach is inView for any page: the reader wheels the view the control is
// in (its .view) until the control is wholly in that view and nothing drawn
// over it (a sticky bar, the window's foot) takes the point Playwright will
// press. Playwright's own scroll is put back by the app, as any scroll that
// isn't the reader's, and one partly in sight it doesn't scroll at all, so
// its press can wait on a control out of sight or under a bar till it times
// out (in WebKit; Chromium gets through some of these by a few pixels).
// Each wheel step waits for its scroll to land before the control is
// looked at again: a wheel's scroll reaches the page a frame or more after
// it is sent, and on a busy machine a fixed pause read where the control
// was before it. The next step was sent on that stale reading, the view
// went past the control, under the header, and the click that followed
// was on a control out of sight (#2370).
async function reach(page, locator) {
await locator.waitFor({ state: "visible" });
const id = await viewOf(page, locator);
if (!id) return;
const v = await page.locator("#" + id).boundingBox();
await page.mouse.move(v.x + v.width / 2, v.y + v.height / 2);
let stalls = 0;
// 20px a wheel: 120 of them reached only 2,400px down, and once Settings'
// lines wrapped whole (arculongus on Discord) Allowances' last account
// was further than that at 420px. The view's end stops it sooner.
for (let i = 0; i < 400; i++) {
// found and read in one call (evaluateAll): locator.evaluate finds the
// control in one call and reads it in the next, and a part the page
// draws again in between (Routing's account rows, a row whose numbers
// moved) hands it the element just replaced, out of the page, a box of
// zeros at the window's top that reads as in sight (#2658)
const at = await locator.evaluateAll((es) => {
const e = es.length === 1 ? es[0] : null, view = e?.closest(".view");
if (!view) return null;
const b = e.getBoundingClientRect(), v = view.getBoundingClientRect();
const x = b.left + b.width / 2, y = b.top + b.height / 2;
const inside = b.top >= v.top && b.bottom <= v.bottom, hit = document.elementFromPoint(x, y);
return { clear: inside && !!hit && (e === hit || e.contains(hit)), down: y > v.top + v.height / 2 };
});
// drawn again between two looks, or not drawn yet: look again
if (!at) { await page.waitForTimeout(40); continue; }
if (at.clear) { await page.waitForTimeout(300); return; }
// landed, or the view is at its end and the wheel moves nothing: three
// of those in turn and the control can't be brought clear
stalls = (await wheeled(page, id, at.down ? 20 : -20)) ? 0 : stalls + 1;
if (stalls >= 3) break;
}
throw new Error("control did not come clear into sight: " + JSON.stringify({ view: v, target: await locator.boundingBox() }));
}
// viewOf: the id of the view the control is in ("" for none), read once
// it is in the page: an element just drawn again over is out of it, and in
// no view (#2658)
async function viewOf(page, locator) {
for (let i = 0; i < 50; i++) {
const id = await locator.evaluateAll((es) => es.length === 1 ? es[0].closest(".view")?.id || "" : null);
if (id != null) return id;
await page.waitForTimeout(40);
}
throw new Error("control not in the page: " + locator);
}
// wheeled sends one wheel over the view and waits for its scroll to land;
// false when the view didn't move in a second (it is at its end).
async function wheeled(page, id, dy) {
const was = await page.locator("#" + id).evaluate((v) => v.scrollTop);
await page.mouse.wheel(0, dy);
return page.waitForFunction(([id, was]) => document.getElementById(id).scrollTop !== was, [id, was], { timeout: 1000 }).then(() => true, () => false);
}
// wheelTo scrolls the page the way a reader does, down till the control's
// top is at y or above, so a click that moved the page would show; then
// reach makes sure it is clear in sight. Each wheel lands before the
// control is looked at again (#2370).
async function wheelTo(page, locator, y) {
await locator.waitFor({ state: "visible" });
const id = await viewOf(page, locator);
const v = await page.locator("#" + id).boundingBox();
await page.mouse.move(v.x + v.width / 2, v.y + v.height / 2);
for (let i = 0; i < 80; i++) {
const top = await locator.evaluateAll((es) => es.length === 1 && es[0].closest(".view") ? es[0].getBoundingClientRect().top : null);
if (top == null) { await page.waitForTimeout(40); continue; }
if (top <= y) break;
if (!(await wheeled(page, id, 50))) break;
}
await reach(page, locator);
}
async function click(page, locator) {
await inView(page, locator);
await locator.click();
}
// Settings is drawn from each read of the settings, and opening the page
// reads them twice: the page's own read, then the window's load() once the
// state is in. Every row is replaced by the second draw, so a row found
// after the first is detached under whatever the test does to it next
// (#1653: scrollIntoViewIfNeeded in WebKit on a busy machine). Call
// watchSettingsReads before page.goto; settingsDrawn then waits until load()
// has asked for its read and every read asked for has been drawn. A page is
// watched once however many times it is opened.
const watched = new WeakSet();
async function watchSettingsReads(page) {
if (watched.has(page)) return;
watched.add(page);
await page.addInitScript(() => {
const reads = window.__settingsReads = { asked: 0, drawn: 0 };
const fetch0 = window.fetch;
window.fetch = function (input, init) {
const p = fetch0.apply(this, arguments);
const url = new URL(typeof input === "string" ? input : input.url, location.href);
if (url.pathname !== "/api/settings" || (init?.method || "GET") !== "GET") return p;
reads.asked++;
// drawn a task after its body is in: loadSettings draws in the
// microtasks that follow
const drawn = () => setTimeout(() => reads.drawn++);
return p.then((res) => {
const text = res.text;
res.text = function () { return text.call(this).finally(drawn); };
return res;
}, (e) => { drawn(); throw e; });
};
});
}
// load.done is set in the same run of load() that then asks for the
// settings, with no wait between, so once it is set the read is counted.
const settingsDrawn = (page) => page.waitForFunction(() => load.done && __settingsReads.asked === __settingsReads.drawn);
// openPart opens a part of Settings (a tab, or its entry in the sidebar's
// strip) the way the keyboard does. A click waits for the tab to hold still
// over two animation frames, and on a loaded machine WebKit draws no frames
// for seconds while the page itself runs on, so the click can sit out the
// test's whole timeout over a tab that isn't moving (#2150). Enter on the
// focused button clicks it with no frame to wait for.
async function openPart(page, id) {
await page.locator(`:is(#setTab-${id}, .nav-s[data-of=setTab-${id}]):visible`).press("Enter");
}
// agentRow is a connected agent as /api/state lists it, opened by its
// Details; openAgentSettings opens it on the Agents page, where its own
// settings are (Settings had them till the owner, 10-20), and waits till
// they are read and drawn. Watch the settings reads first.
const agentRow = (id, name) => ({ id, name, icon: id, wired: true, fields: [{ key: "model", label: "model", value: "relay/m1", options: [{ value: "relay/m1", label: "m1", ref: "relay/m1" }] }] });
async function openAgentSettings(page, id = "codex") {
const link = page.locator(`.row.agent[data-id="${id}"] .ag-link`);
await reach(page, link);
await link.click();
await page.locator(`.row.agent[data-id="${id}"] .ag-prefs`).waitFor();
await page.waitForFunction(() => __settingsReads.asked > 0 && __settingsReads.asked === __settingsReads.drawn);
}
module.exports = { inView, reach, wheelTo, click, openPart, watchSettingsReads, settingsDrawn, agentRow, openAgentSettings };