Files
magpie/internal/gui/tests/sidebar-glide.test.cjs
T
yetone 17a2b00efb gui: the sidebar's open page moves from row to row on a spring, as iOS's selection does
The owner asked for the sidebar's pages (Providers, Routing, Gateway…)
to slide from one to the next like their sub-tabs do, "like iOS".

The open page's row is now marked by one shape (.nav-top-hl) that
navTopGo moves on a damped spring (NAV_SPRING: SwiftUI's response
0.42s, damping 0.84; navTopStep, four sub-steps a frame, dt capped):
- it sets off as the row is pressed, and comes back if the press
  leaves the row or the page refuses to be left;
- being physics, a press that changes its mind turns it with the speed
  it has, never a jump;
- it follows its row while Usage's tab list folds above it and lands
  where the row ends up;
- it is put in place, not sent, when the window opens on a kept page or
  the rows move under it at rest (ResizeObserver); away while Settings
  is open, it fades back in on its row; reduced motion jumps; the
  header's tabs keep their .thumb and draw none.
The rows give up their own ground (open, hovered, pressed) to it.
#nav is position: relative in the sidebar for it, so .seg's centring
left/top are reset (without that the whole list sat 105px right).

Tests:
- sidebar-glide.test.cjs (new): Chromium + WebKit, en + zh, 1024 and
  1440, plus 900 (header): at rest on its row and the list in the
  sidebar, no ground hovered, press sets off before release and comes
  back off the row, Providers→Usage→Agents by frames (≥3 moving
  frames, overshoot < 3px) landing on the row, Settings hides it and
  Routing brings it back, reduced motion jumps, no page errors.
  Broken by hand: without the left/top reset the list check goes red;
  before the "not seen yet" rule the boot slid from Agents (red).
- sidebar.test.cjs: a pressed page row no longer darkens itself (the
  shape comes to it); Settings still must.
- routing-idle-frame.test.cjs counted every self-asking rAF callback as
  Routing's loop, so navTopStep after the click read as 33 Routing
  frames. It now counts only callbacks no global holds (app.js's are
  globals; Routing's frame is in its plugin's scope). Broken by hand:
  Routing's loop made to always ask again → 4 red; green on main's
  assets and on this change.
- TestCoreScriptBudget: app.js 1,343,324 / core 1,535,084 with no room
  added; the sidebar is the window's shell, not a page a plugin can take.

Run:
- Full GUI suite at 183fa169b (this change on 3b10a403d), alone on the
  machine but at load ~216: 11942 pass, 104 fail. The 25 red files
  rerun at load ~10 on this change and on 3b10a403d: the same reds on
  both except routing-idle-frame (fixed above); plugin-card-update,
  segment-press-dialog and sidebar-overlay-scrollbar pass alone here
  (segment-press-dialog red alone on main: #3416). Reds on main, filed:
  #3402 #3403 #3404 #3405 #3406 #3407 #3408 #3409 #3410 #3411 #3412
  #3413 #3414 #3415; already filed: #3350 #3099 #3096 #3094 #3091
  #3104 #3001. codex-predictions and codex-auto-reset were red only at
  load ~216 and passed in both reruns.
- At the pushed head (rebased on f3eb92924): sidebar, sidebar-glide,
  nav-sub-unroll, side-toggle, side-width, header-relang,
  sidebar-tab-top, settings-tabs-stick, omarchy,
  sidebar-overlay-scrollbar, click-scroll, routing-idle-frame,
  plugin-card-update, segment-press-dialog, i18n-keys: 230/230;
  go test -tags nogui -run 'TestCoreScriptBudget|I18n|Docs'
  ./internal/gui: ok; gofmt -l clean.

Not tried: the real Linux app (WebKitGTK); this uses only rAF and
transforms, no view transition. Not run: the full Go suite (only
internal/gui's budget/docs tests; no Go code changed).
2026-10-24 02:21:45 +08:00

141 lines
9.0 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// Beside the sidebar the open page's row is marked by one shape (app.js
// navTopGo) that moves to the row picked on a spring, as iOS moves a
// selection, the way the sub-rows' selection slides: it sets off as the row
// is pressed, goes back when the press is let go off the row, follows its
// row while the list of the page left folds away above it, and lands on
// it. The rows have no ground of their own under it, hovered or not.
// Settings, at the foot, has its own; the shape is away while it is open.
// With reduced motion it is put there at once. In the header's tabs it
// isn't drawn. Both engines, two languages, two widths.
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");
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};` });
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 }, plugins: [] });
if (url.pathname === "/api/groups") return json({ groups: [], pools: [] });
if (url.pathname.startsWith("/api/")) return json({});
if (!/^\/[\w./-]*$/.test(url.pathname) || url.host !== "magpie.test") return route.fulfill({ status: 404, body: "" });
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: "" }); }
};
}
// the shape's top and height, and a row's, in the sidebar's list
const shape = (page) => page.evaluate(() => {
const hl = document.querySelector("#nav > .nav-top-hl");
if (!hl || hl.hidden || getComputedStyle(hl).display === "none") return null;
const b = hl.getBoundingClientRect();
return { top: b.top, h: b.height };
});
const rowOf = (page, v) => page.evaluate((v) => {
const b = document.querySelector(`#nav button[data-view="${v}"]`).getBoundingClientRect();
return { top: b.top, h: b.height };
}, v);
// the shape's tops, one a frame, until it has been still for 20 frames
const watch = (page) => page.evaluate(() => { window.__tops = []; const f = () => { const hl = document.querySelector("#nav > .nav-top-hl"); window.__tops.push(hl.getBoundingClientRect().top); const t = window.__tops; if (t.length < 400 && !(t.length > 20 && t.slice(-20).every((y) => y === t[t.length - 1]))) requestAnimationFrame(f); else window.__still = true; }; window.__still = false; requestAnimationFrame(f); });
const settled = async (page) => { await page.waitForFunction(() => window.__still); return page.evaluate(() => window.__tops); };
const on = async (page, v) => {
const [s, r] = [await shape(page), await rowOf(page, v)];
assert(s, `the shape is drawn for ${v}`);
assert(Math.abs(s.top - r.top) < 0.5 && Math.abs(s.h - r.h) < 0.5, `the shape on ${v}'s row: ${JSON.stringify(s)} vs ${JSON.stringify(r)}`);
};
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
test(`${engine}: the sidebar's open page is one shape that springs from row to row`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
for (const lang of ["en", "zh"]) for (const width of [1024, 1440]) await t.test(`${lang} ${width}`, async () => {
const context = await browser.newContext({ viewport: { width, height: 800 } });
const page = await context.newPage();
page.setDefaultTimeout(5000);
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", serve(lang));
await page.goto("http://magpie.test/?view=providers");
await page.waitForSelector("#nav > .nav-top-hl:not([hidden])");
assert.ok(await page.evaluate(() => document.body.classList.contains("side-nav")), "a sidebar at " + width);
await on(page, "providers");
// the list where it was before the shape: in the sidebar, at its edge
const [side, nav, hl] = await page.evaluate(() => ["#side", "#nav", "#nav > .nav-top-hl"].map((q) => { const b = document.querySelector(q).getBoundingClientRect(); return { l: b.left, r: b.right }; }));
assert(nav.l - side.l < 16 && nav.r <= side.r && hl.l === nav.l && hl.r === nav.r, `the list in the sidebar: ${JSON.stringify({ side, nav, hl })}`);
const ground = (v) => page.locator(`#nav button[data-view="${v}"]`).evaluate((b) => getComputedStyle(b).backgroundColor);
assert.equal(await ground("providers"), "rgba(0, 0, 0, 0)", "the open row's ground is the shape's");
// hovered, a row has no ground of its own
await page.locator('#nav button[data-view="agents"]').hover();
assert.equal(await ground("agents"), "rgba(0, 0, 0, 0)", "no block under the pointer ahead of the shape");
// pressed, the shape sets off before the press ends; let go off the
// row, it goes back and the page stays
const usage = await page.locator('#nav button[data-view="usage"]').boundingBox();
await page.mouse.move(usage.x + 20, usage.y + usage.height / 2);
await watch(page);
await page.mouse.down();
await page.waitForFunction((from) => Math.abs(document.querySelector("#nav > .nav-top-hl").getBoundingClientRect().top - from) > 2, (await shape(page)).top);
assert.equal(await page.locator("#nav button.on").getAttribute("data-view"), "providers", "the page changes only on the click");
await page.mouse.move(usage.x + usage.width + 300, usage.y + 200);
await page.mouse.up();
await settled(page);
assert.equal(await page.locator("#nav button.on").getAttribute("data-view"), "providers");
await on(page, "providers");
// to Usage, then up to Agents while Usage's tabs fold away under it:
// it moves by frames, never at once, and lands on its row
for (const v of ["usage", "agents"]) {
const from = (await shape(page)).top, to = (await rowOf(page, v)).top;
await watch(page);
await page.locator(`#nav button[data-view="${v}"]`).click();
const tops = await settled(page);
await on(page, v);
const moving = tops.filter((y) => Math.abs(y - from) > 0.5 && Math.abs(y - (tops[tops.length - 1])) > 0.5);
assert(moving.length >= 3, `${v}: the shape moves by frames, not at once: ${tops.map(Math.round).join(" ")}`);
const end = tops[tops.length - 1], past = Math.max(...tops.map((y) => Math.sign(end - from) * (y - end)));
assert(past < 3, `${v}: it settles with at most a little overshoot (${past.toFixed(2)}px): ${tops.map(Math.round).join(" ")}`);
if (v === "usage") assert(Math.abs(end - to) < 0.5);
}
// Settings has its own; back, the shape is on the row picked
await page.locator("#prefs").click();
await page.waitForFunction(() => document.querySelector("#nav > .nav-top-hl").hidden);
assert.notEqual(await page.locator("#prefs").evaluate((b) => getComputedStyle(b).backgroundColor), "rgba(0, 0, 0, 0)", "Settings keeps its own selection");
await page.locator('#nav button[data-view="routing"]').click();
await page.waitForTimeout(300);
await on(page, "routing");
// reduced motion: there at once
await page.emulateMedia({ reducedMotion: "reduce" });
await page.locator('#nav button[data-view="gateway"]').click();
await on(page, "gateway");
assert.deepEqual(errors, []);
await context.close();
});
await t.test("the header's tabs draw none", async () => {
const context = await browser.newContext({ viewport: { width: 900, height: 700 } });
const page = await context.newPage();
page.setDefaultTimeout(5000);
await page.route("**/*", serve("en"));
await page.goto("http://magpie.test/?view=providers");
await page.waitForSelector("#nav button.on");
assert.ok(!(await page.evaluate(() => document.body.classList.contains("side-nav"))));
await page.locator('#nav button[data-view="usage"]').click();
assert.equal(await shape(page), null);
assert.notEqual(await page.locator("#nav > .thumb").count(), 0, "the header's own thumb");
await context.close();
});
});
}