Files
magpie/internal/gui/tests/side-toggle.test.cjs
T
yetone 18e82adc62 gui: the window's pages down a sidebar in groups, the open page's tabs under it, and a button to have the header's tabs instead
From 1024 points wide (SIDE_AT) the window lays its pages down the left
as a Mac app's sidebar: Work (Agents, Sessions, Library), Models
(Providers, Routing, Gateway), Insights (Usage), Extend (Plugins), with
Settings and the gateway's address at the foot. The open page's own tabs
(Usage, Library, Plugins, Settings) are listed under it, opening and
folding on an iOS-like spring, and the page's strip is hidden. The header
becomes a breadcrumb, and pages whose head held only tools have them in
the title bar. Graphite: the open page is in the text's ink, not purple.
A pressed row darkens and never shrinks. The selection under the tabs
sets off as the row is pressed, on a 0.3s curve (it felt behind the
finger), and is the list's only shape: a sub-row hovered or pressed has
no ground of its own, which showed a block under the finger before the
selection slid there. The page's tab is clicked at once, so what is
typed next goes to it.

The title bar's button (or ⌃⌘S, Ctrl+Shift+S), there at every width,
picks the sidebar or the header's tabs. The choice is kept in
localStorage magpie.navLayout (side or top) at every width, so it is laid
out from the first frame; picked in a narrow window the sidebar is 184
points under 860. The two trade places in
a view transition: the sidebar slides, each tab flies to its place, and
the page slides as one. Without view transitions or under reduced
motion, the change is at once.

With nothing picked, narrower than 1024 the page beside a sidebar would
be narrower than Usage's requests table needs, so the header's tabs come
back. A resize
drops what a click held (heldWidth). Usage's KPI strip goes two to a line
by the room the page gives it (a container query), and Japanese counts
wrap there. Tests whose window is 1024+ were updated for the sidebar,
and docs/subsystems/gui-shell.md says how it works.

Run: sidebar.test.cjs and side-toggle.test.cjs 16/16 in chromium and
webkit, in en, zh, zh-TW, ja and de. Each was broken by hand (press
shrink, address cut, press glide, sub-row ground, view transition, kept
choice) and went red. Every page at 560 points with the sidebar picked,
zh and de, both engines: nothing scrolls sideways. session-identity's
ledger row sits under the footer beside the sidebar; WebKit's click
doesn't scroll to it, so the test wheels it up first. Animation frames were checked from a recorded video. The 105
affected GUI files in both engines; their reds were rerun alone and fixed
(filter-ime: the sub-row's click now at once; session-identity, above) or
fail the same on main (tray-logos). The full GUI suite in chromium, on
the rebase onto d5cae267a, was cut short at 352 tests on the owner's
"ship": 6 reds (account-alias, account-models, codex-auto-review,
codex-credits, context-text) each pass alone, here and on main d5cae267a,
so they are timing under the full run's load and are to be filed. After
rebasing onto e4e22b23e: sidebar, side-toggle, session-identity,
filter-ime, library-instr-busy, usage-speed-count and view-scrollbar,
50/50 in chromium and webkit. go build ./... passes.
2026-10-12 00:41:34 +08:00

197 lines
12 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// The sidebar's button (app.js toggleSideNav): at every width the reader
// picks the sidebar or the header's tabs, the choice kept for the next time
// the window opens, and the two trade places as one motion.
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) {
const state = { agents: [], profiles: [], settings: { lang, theme: "light" } };
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(state);
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 });
};
}
const words = {
en: ["Hide sidebar", "Show sidebar"],
zh: ["隐藏侧边栏", "显示侧边栏"],
"zh-TW": ["隱藏側邊欄", "顯示側邊欄"],
ja: ["サイドバーを非表示", "サイドバーを表示"],
de: ["Seitenleiste ausblenden", "Seitenleiste einblenden"],
};
const frame = (page) => page.evaluate(() => new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r))));
const side = (page) => page.evaluate(() => document.body.classList.contains("side-nav"));
// the header's row: the button, the tabs and the icons apart, in the window
async function headerFits(page) {
const [b, n, a, w, sw] = await page.evaluate(() => [
document.querySelector("#sideToggle").getBoundingClientRect().toJSON(),
document.querySelector(".top #nav").getBoundingClientRect().toJSON(),
document.querySelector(".top .actions").getBoundingClientRect().toJSON(),
document.documentElement.clientWidth, document.documentElement.scrollWidth]);
assert.ok(b.right <= n.left, `the button clear of the tabs: ${b.right} ${n.left}`);
assert.ok(n.right <= a.left, `the tabs clear of the icons: ${n.right} ${a.left}`);
assert.ok(sw <= w, "the window does not scroll sideways");
}
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
test(`${engine}: the sidebar's button`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
for (const [lang, [hide, show]] of Object.entries(words)) await t.test(lang, async () => {
const context = await browser.newContext({ viewport: { width: 1280, height: 760 }, reducedMotion: "reduce" });
const page = await context.newPage();
page.setDefaultTimeout(5000);
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", serve(lang));
// whether the sidebar was ever laid out, from the first frame on
await page.addInitScript(() => {
addEventListener("DOMContentLoaded", () => {
window.sideSeen = document.body.classList.contains("side-nav");
new MutationObserver(() => { if (document.body.classList.contains("side-nav")) window.sideSeen = true; }).observe(document.body, { attributes: true, attributeFilter: ["class"] });
});
});
await page.goto("http://magpie.test/");
await page.waitForSelector("#nav button.on");
const mac = await page.evaluate(() => /^Mac/.test(navigator.platform));
const keys = mac ? "⌃⌘S" : "Ctrl+Shift+S";
// the sidebar to start with, and the button saying it hides it
const btn = page.locator("#sideToggle");
assert.ok(await side(page));
assert.ok(await btn.isVisible(), "the button in the title bar");
assert.equal(await btn.getAttribute("title"), `${hide} (${keys})`);
assert.equal(await btn.getAttribute("aria-pressed"), "true");
assert.equal(await btn.locator(".st-pane").evaluate((p) => getComputedStyle(p).opacity), "0.32", "its left pane filled");
// the header's tabs, kept
await btn.click();
await page.waitForFunction(() => !document.body.classList.contains("side-nav"));
await frame(page);
assert.ok(await page.locator(".top #nav").isVisible(), "the tabs in the header");
assert.equal(await page.locator("#side").isVisible(), false);
assert.ok(await btn.isVisible(), "the button stays to bring the sidebar back");
assert.equal(await btn.getAttribute("title"), `${show} (${keys})`);
assert.equal(await btn.getAttribute("aria-pressed"), "false");
assert.equal(await btn.locator(".st-pane").evaluate((p) => getComputedStyle(p).opacity), "0", "its pane empty");
await headerFits(page);
assert.equal(await page.evaluate(() => localStorage.getItem("magpie.navLayout")), "top");
// opened again: the header from the first frame, no sidebar on the way
await page.reload();
await page.waitForSelector("#nav button.on");
await frame(page);
assert.equal(await side(page), false);
assert.equal(await page.evaluate(() => window.sideSeen), false, "the sidebar never laid out");
await headerFits(page);
// narrower than the sidebar comes at by itself: the button stays, the
// header holds
await page.setViewportSize({ width: 1023, height: 760 });
await frame(page);
assert.ok(await btn.isVisible(), "the button at every width");
assert.equal(await side(page), false, "the header still");
await headerFits(page);
// and the sidebar picked in a narrow window: narrower, the page beside it
await page.setViewportSize({ width: 640, height: 760 });
await frame(page);
assert.ok(await btn.isVisible());
await btn.click();
await page.waitForFunction(() => document.body.classList.contains("side-nav"));
await frame(page);
assert.equal(await page.evaluate(() => localStorage.getItem("magpie.navLayout")), "side");
assert.equal(Math.round((await page.locator("#side").boundingBox()).width), 184, "a narrower sidebar");
assert.ok(await page.evaluate(() => document.documentElement.scrollWidth <= document.documentElement.clientWidth), "the window does not scroll sideways");
const [tb, cr, ac] = await page.evaluate(() => ["#sideToggle", "#crumb", ".top .actions"].map((q) => document.querySelector(q).getBoundingClientRect().toJSON()));
assert.ok(tb.right <= cr.left + 0.5 && cr.right <= ac.left + 0.5, `the header's row apart: ${tb.right} ${cr.left} ${cr.right} ${ac.left}`);
// widened, the choice holds
await page.setViewportSize({ width: 1280, height: 760 });
await frame(page);
assert.ok(await side(page), "the sidebar still");
// the shortcut trades them, and keeps what it gives
await page.keyboard.press(mac ? "Control+Meta+KeyS" : "Control+Shift+KeyS");
await page.waitForFunction(() => !document.body.classList.contains("side-nav"));
assert.equal(await page.evaluate(() => localStorage.getItem("magpie.navLayout")), "top");
await page.keyboard.press(mac ? "Control+Meta+KeyS" : "Control+Shift+KeyS");
await page.waitForFunction(() => document.body.classList.contains("side-nav"));
assert.equal(await page.evaluate(() => localStorage.getItem("magpie.navLayout")), "side");
assert.ok(await page.locator("#side .side-scroll #nav").isVisible());
assert.equal(await btn.getAttribute("title"), `${hide} (${keys})`);
assert.ok(await page.evaluate(() => document.documentElement.scrollWidth <= document.documentElement.clientWidth));
// Settings' tabs still listed under it
await page.locator("#prefs").click();
await page.locator("#side .nav-sub:not(.leaving) .nav-s").first().waitFor();
assert.deepEqual(errors, []);
await context.close();
});
await t.test("the two trade places as one motion", async () => {
const context = await browser.newContext({ viewport: { width: 1280, height: 760 } });
const page = await context.newPage();
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", serve("en"));
await page.goto("http://magpie.test/");
await page.waitForSelector("#nav button.on");
assert.ok(await page.evaluate(() => !!document.startViewTransition), "the webview has view transitions");
// what plays, read as the button is clicked: the old drawn, the new laid out
const playing = () => page.evaluate(() => new Promise((r) => {
const at = () => document.getAnimations().map((a) => a.effect?.pseudoElement).filter(Boolean);
requestAnimationFrame(() => requestAnimationFrame(() => r({ anims: at(), morph: document.documentElement.classList.contains("nav-morph") })));
}));
await page.locator("#sideToggle").click();
let p = await playing();
assert.ok(p.morph, "the layouts trade places");
assert.ok(p.anims.includes("::view-transition-old(side)"), `the sidebar slides away: ${p.anims}`);
assert.ok(p.anims.includes("::view-transition-group(nav-usage)"), "each tab flies to the header");
assert.ok(p.anims.includes("::view-transition-group(page)"), "the page moves over");
// on the sidebar's spring
const ease = await page.evaluate(() => getComputedStyle(document.documentElement, "::view-transition-group(page)").animationTimingFunction);
assert.match(ease, /cubic-bezier\(0?\.32, 0?\.72, 0, 1\)/);
await page.waitForFunction(() => !document.documentElement.classList.contains("nav-morph"));
assert.equal(await side(page), false);
await headerFits(page);
await page.locator("#sideToggle").click();
p = await playing();
assert.ok(p.anims.includes("::view-transition-new(side)"), `the sidebar slides in: ${p.anims}`);
await page.waitForFunction(() => !document.documentElement.classList.contains("nav-morph"));
assert.ok(await side(page));
assert.ok(await page.locator("#side .side-scroll #nav").isVisible());
assert.deepEqual(errors, []);
await context.close();
});
await t.test("less motion: at once", async () => {
const context = await browser.newContext({ viewport: { width: 1280, height: 760 }, reducedMotion: "reduce" });
const page = await context.newPage();
await page.route("**/*", serve("en"));
await page.goto("http://magpie.test/");
await page.waitForSelector("#nav button.on");
await page.evaluate(() => { window.morphed = false; new MutationObserver(() => { if (document.documentElement.classList.contains("nav-morph")) window.morphed = true; }).observe(document.documentElement, { attributes: true }); });
await page.locator("#sideToggle").click();
assert.equal(await side(page), false, "the header at once");
assert.equal(await page.evaluate(() => window.morphed), false);
await context.close();
});
});
}