Files
magpie/internal/gui/tests/sidebar-settings-steady.test.cjs
T
yetone d374168d04 gui: Settings stays where it was clicked in the sidebar (jen_celeus on X)
In the sidebar layout Settings sits at the sidebar's foot, and opening it
listed its eight tabs under the button. The foot is anchored at the
window's bottom, so it grew upward and the button rose about 223 points,
out from under the pointer, and dropped back when another page folded the
list.

Settings' tabs now open above its button and grow up from it, so the button
and the gateway's line stay where they were. The foot is laid from its end
(`.side-foot` is `column-reverse`, its rows ordered tabs, Settings, gateway
and kept whole). A window too short for the list scrolls the foot from its
end, so the button stays in sight, and the foot may take 80% of the sidebar
while the list shows instead of 55%. The list's DOM order is the order
shown, so Tab reads tabs, Settings, gateway. The rows rise from the button,
nearest first. The overlay bar's thumb math takes the reversed scroll
origin. Nothing else in the layout changes: pages' own tabs still open
under their rows. The core-script budget is paid by cutting two sidebar
comments, with no raise.

Run (mote, at this head after rebasing on 86825005e unless said):
- sidebar-settings-steady.test.cjs (new), chromium and webkit, wide 1200,
  narrow 760 (sidebar picked), short 460 and tiny 340 high, all five
  languages: 42 of 42 pass. It samples the button every frame while the
  list opens, after a tab is picked and while it folds.
- Broken by hand: the old code is red in all 20 cases per engine ("the
  button moved 223px", 185.8px in the short window). With the foot back to
  `column`, red (223px). With the list put under the button in the DOM,
  red on the reading order. Before the rows were kept whole, the tiny
  window was red at 21.7px.
- sidebar, sidebar-glide, sidebar-tab-top, sidebar-overlay-scrollbar,
  nav-lamp, nav-sub-unroll, side-toggle, side-width, side-gateway-lan,
  settings-tabs-stick, text-size, click-scroll, readable-focus,
  page-width, i18n-keys at 3f27eab03 (this change before the rebase, no
  GUI files changed under it except the one new test): 322 pass, 1 red,
  sidebar-overlay-scrollbar chromium en "dragged to the end: 224 of 253",
  the known #1863. Alone it passed 3 of 3. Commented on #1863.
- go test ./internal/gui/... at 3f27eab03: ok.
- At this head: sidebar-settings-steady, sidebar, side-toggle and
  i18n-keys, chromium and webkit, 90 of 90 pass; TestCoreScriptBudget ok
  (220 bytes under the core budget).

Not run: the whole GUI suite, the full Go suite (started after the tag),
Windows and WebKitGTK by hand. No Go code changed, so no gofmt or vet.
2026-11-10 15:45:35 +08:00

115 lines
7.1 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// Settings at the sidebar's foot stays where it was clicked (jen_celeus on
// X): its tabs used to be listed under it, so the foot grew upward and the
// button rose with it, out from under the pointer. The list now opens
// above the button, growing from it, and folds back the same way; the
// button never moves, while the list unrolls or after, in a wide window, a
// narrow one, a short one and one too short for the whole list.
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.resolve(__dirname, "../assets"));
function serve(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:${JSON.stringify(lang)},theme:"light",web:false,sideGateway:{url:"http://127.0.0.1:3425",running:true,boot: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: [], profiles: [], settings: { lang, theme: "light" } });
if (url.pathname === "/api/providers") return json({ providers: [], presets: [], excluded: [], gateway: { url: "http://127.0.0.1:3425", running: true, window: true } });
if (url.pathname === "/api/groups") return json({ groups: [] });
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 });
};
}
// every frame for a while, the furthest Settings' button got from where it was
const drift = (page, ms) => page.evaluate((ms) => new Promise((done) => {
const b = document.querySelector("#prefs"), at = b.getBoundingClientRect();
let most = 0;
const end = performance.now() + ms;
const look = () => {
const r = b.getBoundingClientRect();
most = Math.max(most, Math.abs(r.top - at.top), Math.abs(r.bottom - at.bottom), Math.abs(r.left - at.left), Math.abs(r.right - at.right));
if (performance.now() < end) requestAnimationFrame(look); else done(most);
};
requestAnimationFrame(look);
}), ms);
const sizes = [
{ name: "wide", width: 1200, height: 760 },
{ name: "narrow", width: 760, height: 760, side: true },
{ name: "short", width: 1200, height: 460 },
// too short for the whole list: the foot scrolls, its end in sight
{ name: "tiny", width: 1200, height: 340, over: true },
];
const langs = ["en", "zh", "zh-TW", "ja", "de"];
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
test(`${engine}: Settings stays where it was clicked`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
for (const size of sizes) for (const lang of langs) await t.test(`${size.name} ${lang}`, async () => {
const context = await browser.newContext({ viewport: { width: size.width, height: size.height } });
if (size.side) await context.addInitScript(() => { try { localStorage.setItem("magpie.navLayout", "side"); } catch {} });
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(lang));
await page.goto("http://magpie.test/");
await page.waitForSelector("#nav button.on");
assert.ok(await page.evaluate(() => document.body.classList.contains("side-nav")), "the sidebar shows");
const prefs = page.locator("#side .side-foot #prefs");
const before = await prefs.boundingBox();
// clicked, the list unrolls and the button stays under the pointer
await page.mouse.click(before.x + before.width / 2, before.y + before.height / 2);
const opening = await drift(page, 700);
const subs = page.locator("#side .nav-sub:not(.leaving) .nav-s");
await subs.first().waitFor();
assert.equal(await subs.count(), 8, "Settings' tabs are listed");
const after = await prefs.boundingBox();
assert.ok(opening < 1, `the button moved ${opening}px while the list opened`);
assert.ok(Math.abs(after.y - before.y) < 1 && Math.abs(after.x - before.x) < 1, `the button stays put: ${JSON.stringify(before)} then ${JSON.stringify(after)}`);
assert.ok(await page.locator("#prefs").evaluate((b) => b.classList.contains("on")), "Settings is open");
// the list grows from the button: it sits right above it, the open tab in sight
const [listBottom, btnTop] = await page.evaluate(() => [document.querySelector(".nav-sub:not(.leaving)").getBoundingClientRect().bottom, document.querySelector("#prefs").getBoundingClientRect().top]);
assert.ok(listBottom <= btnTop + 0.5 && btnTop - listBottom < 12, `the list is just above the button: ${listBottom} ${btnTop}`);
// and is read in the order shown, tabs then Settings then the gateway
assert.deepEqual(await page.locator(".side-foot").evaluate((f) => [...f.children].map((c) => c.id || c.className)), ["nav-sub", "prefs", "sideGw"], "read top down");
const inSight = (sel) => page.locator(sel).evaluate((s) => {
const r = s.getBoundingClientRect(), f = s.closest(".side-foot").getBoundingClientRect();
return r.top >= f.top - 0.5 && r.bottom <= f.bottom + 0.5;
});
assert.ok(await inSight("#prefs"), "the button is in sight");
const over = await page.locator(".side-foot").evaluate((f) => f.scrollHeight > f.clientHeight + 1);
assert.equal(over, !!size.over, "the foot scrolls only when the list can't fit");
if (!size.over) assert.ok(await inSight(".nav-s.cur"), "the open tab is in sight");
// a tab picked there: the button still where it was
await page.locator(".nav-s[data-of=setTab-network]").click();
await page.waitForFunction(() => document.querySelector("#setTab-network").classList.contains("on"));
assert.ok((await drift(page, 300)) < 1, "picking a tab leaves the button put");
assert.ok(Math.abs((await prefs.boundingBox()).y - before.y) < 1);
// another page: the list folds away and the button never moved
await page.locator('#nav button[data-view="agents"]').click();
const closing = await drift(page, 600);
await page.waitForFunction(() => !document.querySelector(".nav-sub"));
assert.ok(closing < 1, `the button moved ${closing}px while the list folded`);
assert.ok(Math.abs((await prefs.boundingBox()).y - before.y) < 1, "the button is where it began");
assert.deepEqual(errors, []);
await context.close();
});
});
}