Files
magpie/internal/gui/tests/segs-glide.test.cjs
T
yetone 8f9bc3fccf gui tests: the 37 other Settings tests act on rows once the page has drawn its last read of the settings, as the masking-rules tests do since #1653 (#1659)
Opening Settings reads the settings twice: the page's own read
(showView), then the window's load() once the state is in. Each read
redraws every row. A test that found a row after the first draw, and
then called scrollIntoViewIfNeeded or boundingBox on it, failed when the
second read landed in between. The errors were "Element is not
attached", or a null box. #1653 fixed the three masking-rules tests and
listed these 37 in #1659.

Each of the 37 now calls watchSettingsReads(page) before every
page.goto, and settingsDrawn(page) right after it. Right after goto is
exact for ?view=settings: at boot, show(view) asks for showView's read
before load() runs. load() sets load.done in the same run that then asks
for its own read, so settingsDrawn's "load.done and every read asked
for drawn" holds only once the second read is drawn. For a test opened
on another page that clicks into Settings later, load is done by then,
the click makes the only read, and the wait passes at once.
watchSettingsReads now watches a page once, however many times it is
opened, since some of these tests open one page several times. The
page is unchanged.

Reproduced with a generic injection (scratchpad inject1659.cjs, loaded
with NODE_OPTIONS=--require). A route added at each goto holds the
page's second GET /api/settings. It lets the read go just as the test
calls scrollIntoViewIfNeeded or boundingBox, or waits on the page with
waitForFunction (which settingsDrawn is), with a 4s timer as a backstop.
This is #1653's injection, made generic.

Before, injected, both engines, all languages:
- 5 files red with the issue's error:
  - github-token: null box.
  - member-model-setting, searcher-pick, search-first and update-wait:
    "Element is not attached to the DOM".
- 4 files red on a check of a row drawn from the first read:
  model-suffix-own, otel, readable-focus and warm-at-times.
- 28 green.

After, injected: 36 of the 37 files green, every held read let go by
settingsDrawn's wait. otel stays red in WebKit at 360px. It is red the
same way with no injection, on origin/main's own copy of the test. That
cause is separate (the narrow nav laid out after setViewportSize
returns) and has its own commit next.

Plain (no injection), both engines, all 37 files: 460 cases passed and
2 failed, both otel's WebKit 360px check above.

The 8 files red under injection before, injected again after,
5 rounds: 390 of 390 cases passed.
2026-10-12 03:25:47 +08:00

183 lines
9.9 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// A segmented control's thumb glides once, from where it is, to the option
// picked, and no other thumb on the page moves. Every save's answer draws
// Settings again, a new thumb for every control, and a new thumb took its
// place from what was remembered: a redraw mid-glide started the glide over
// (Lightweight mode's thumb went most of the way to On, jumped back to Off
// and slid again), and Settings drawn while hidden (opened on another tab,
// or at start) was remembered 0 wide, so the first save grew every thumb on
// the page in from the left. Checked frame by frame: General shown from
// another tab has every thumb under its option; a click whose save comes
// back mid-glide, one whose save comes back at once, and one redrawn twice
// on the way all move the thumb one way only and end under the option, with
// every other thumb still; a refused save glides it back. Chromium and
// WebKit, en, zh, zh-TW, ja and de, 900 and 440px, the API faked.
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 { watchSettingsReads, settingsDrawn } = require("./reader.cjs");
const assets = path.resolve(__dirname, "../assets");
function serve(lang, st, ctl) {
const settings = () => ({ lang, theme: "light", searchVendors: [], searchAPIs: [], ...st });
return async (r) => {
const url = new URL(r.request().url());
const json = (data, status = 200) => r.fulfill({ status, 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({ agents: [], profiles: [], settings: settings() });
if (url.pathname === "/api/settings" && r.request().method() === "POST") {
await new Promise((ok) => setTimeout(ok, ctl.hold));
if (ctl.refuse) return json({ error: "refused" }, 500);
Object.assign(st, JSON.parse(r.request().postData()));
return json(settings());
}
if (url.pathname.startsWith("/api/settings")) return json(settings());
if (url.pathname === "/api/providers") return json({ providers: [], presets: [], excluded: [], gateway: { running: true, window: true, url: "http://127.0.0.1:3999" } });
if (url.pathname === "/api/groups") return json({ models: [], groups: [], pools: [] });
if (url.pathname === "/api/plugins") return json({ plugins: [] });
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)];
await r.fulfill({ body: await fs.readFile(file), contentType });
};
}
// every thumb on General: where it is drawn, and where its option is
const THUMBS = `[...document.querySelectorAll("#setPage-general .segs")].filter((s) => s.offsetParent).map((s) => {
const th = s.querySelector(":scope > .thumb"), on = s.querySelector(":scope > .on"), b = s.getBoundingClientRect();
const r = th?.getBoundingClientRect(), o = on?.getBoundingClientRect();
return { id: s.parentElement.id || s.dataset.kind, x: r ? r.left - b.left : -1, w: r ? r.width : -1, ox: o ? o.left - b.left : -1, ow: o ? o.width : -1 };
})`;
// records every thumb on General at each frame, and between them
async function record(page) {
await page.evaluate((src) => {
const read = new Function("return " + src);
window.frames_ = [];
window.recording = true;
const tick = () => { window.frames_.push(read()); if (window.recording) requestAnimationFrame(tick); };
requestAnimationFrame(tick);
window.between = setInterval(() => window.frames_.push(read()), 4);
}, THUMBS);
}
async function stop(page) {
return page.evaluate(() => { window.recording = false; clearInterval(window.between); return window.frames_; });
}
// one control's thumb moved one way only, from its start to its end, and
// every other control's thumb didn't move at all
function oneWay(frames, id, label) {
const xs = frames.map((f) => f.find((c) => c.id === id)).filter(Boolean);
assert.ok(xs.length > 3, label + ": frames recorded");
const up = xs[xs.length - 1].x >= xs[0].x;
for (let i = 1; i < xs.length; i++) {
const d = xs[i].x - xs[i - 1].x;
assert.ok(up ? d > -0.75 : d < 0.75, `${label}: ${id}'s thumb went back from ${xs[i - 1].x.toFixed(1)} to ${xs[i].x.toFixed(1)} (${xs.map((p) => p.x.toFixed(0)).join(" ")})`);
assert.ok(xs[i].w > 4, `${label}: ${id}'s thumb ${xs[i].w.toFixed(1)} wide`);
}
for (const c of frames[0]) {
if (c.id === id) continue;
for (const f of frames) {
const n = f.find((x) => x.id === c.id);
if (!n) continue;
assert.ok(Math.abs(n.x - c.x) < 0.75 && Math.abs(n.w - c.w) < 0.75, `${label}: ${c.id}'s thumb moved (${c.x.toFixed(1)}/${c.w.toFixed(1)} → ${n.x.toFixed(1)}/${n.w.toFixed(1)}) when ${id} was clicked`);
}
}
}
// every shown thumb under its option once it has come to rest
async function underPicks(page, label) {
await page.waitForTimeout(400);
for (const c of await page.evaluate((src) => new Function("return " + src)(), THUMBS)) {
assert.ok(Math.abs(c.x - c.ox) < 1 && Math.abs(c.w - c.ow) < 1, `${label}: ${c.id}'s thumb at ${c.x.toFixed(1)}/${c.w.toFixed(1)}, its option at ${c.ox.toFixed(1)}/${c.ow.toFixed(1)}`);
}
}
// brought out from under the header and the foot by the reader's wheel, as
// code's scrolls are put back, till the pointer at its middle is on it
async function reveal(page, loc) {
for (let i = 0; i < 40; i++) {
const b = await loc.boundingBox();
const h = page.viewportSize().height;
if (b && await loc.evaluate((o, [x, y]) => o.contains(document.elementFromPoint(x, y)), [b.x + b.width / 2, b.y + b.height / 2])) return;
await page.mouse.move(page.viewportSize().width / 2, h / 2);
await page.mouse.wheel(0, b && b.y < h / 2 ? -150 : 150);
await page.waitForTimeout(60);
}
throw new Error("could not bring the option into view");
}
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of ["en", "zh", "zh-TW", "ja", "de"]) {
for (const width of [900, 440]) {
test(`${engine} ${lang} ${width}px: a thumb glides once, one way, and no other thumb moves`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
const page = await (await browser.newContext({ viewport: { width, height: 1000 } })).newPage();
page.setDefaultTimeout(5000);
if (process.env.THROTTLE && engine === "chromium") await (await page.context().newCDPSession(page)).send("Emulation.setCPUThrottlingRate", { rate: +process.env.THROTTLE });
const errors = [];
// WebKit's notice of a ResizeObserver loop is no error of the page's
page.on("pageerror", (e) => { if (!/ResizeObserver loop/.test(e.message)) errors.push(e.message); });
const st = {}, ctl = { hold: 0, refuse: false };
await page.route("**/*", serve(lang, st, ctl));
// opened on another tab: General is drawn while hidden
await watchSettingsReads(page);
await page.goto("http://magpie.test/?view=settings&tab=about");
await settingsDrawn(page); // rows found are the ones that stay (#1659)
await page.locator("#setTab-general").waitFor();
await page.waitForTimeout(300);
await page.locator("#setTab-general").click();
await page.locator("#lightweightSegs .opt").first().waitFor();
await underPicks(page, "General shown from another tab");
const opt = (i) => page.locator("#lightweightSegs .opt").nth(i);
const scroll = () => page.evaluate(() => document.querySelector("#view-settings").scrollTop);
await reveal(page, opt(1));
await page.waitForTimeout(300);
const top = await scroll();
// the save's answer comes back mid-glide, and draws the page again
ctl.hold = 120;
await record(page);
await opt(1).click();
await page.waitForTimeout(700);
oneWay(await stop(page), "lightweightSegs", "answer mid-glide");
assert.equal(st.lightweight, true);
await underPicks(page, "after On");
// the answer at once, then the page drawn twice more on the way
ctl.hold = 0;
await record(page);
await opt(0).click();
await page.waitForTimeout(60);
await page.evaluate(() => renderSettings());
await page.waitForTimeout(60);
await page.evaluate(() => renderSettings());
await page.waitForTimeout(600);
oneWay(await stop(page), "lightweightSegs", "drawn again on the way");
assert.equal(st.lightweight, false);
await underPicks(page, "after Off");
// a refused save puts Off back: the thumb glides back to it
ctl.refuse = true;
ctl.hold = 300;
await record(page);
await opt(1).click();
await page.waitForTimeout(1000);
const frames = await stop(page);
const lw = frames.map((f) => f.find((c) => c.id === "lightweightSegs")).filter(Boolean);
assert.ok(lw.some((p) => p.x > lw[0].x + 8), "it went towards On first");
await underPicks(page, "a refused save");
assert.equal(await opt(0).evaluate((b) => b.classList.contains("on")), true, "Off is back");
assert.equal(await scroll(), top, "a click never moves the page");
assert.deepEqual(errors, []);
});
}
}
}