Files
magpie/internal/gui/tests/popover-mac-chrome.test.cjs
T
yetone ce8b67de37 gui tests: a page's first load gets a minute, not a click's 5s (#4470)
claude-switch-only set page.setDefaultTimeout(5000) and then called
page.goto with no timeout of its own, so Playwright held the first load
of app.js, i18n.js and the plugins to the 5s bar meant for clicks. On a
busy box (a whole-suite run at load 130-160) that load took longer and
goto timed out. The same pattern is in 988 GUI test files (1174 pages):
every one of them now also sets setDefaultNavigationTimeout(60000) on
the same page, right after its default, so goto, reload and waitForURL
wait on the load itself with room, while clicks and waits keep their
short bar. group-from-model-add's own 20s navigation bar is raised to the
same minute. claude-switch-only also gives its first provider row (drawn
from the page's first reads) a minute before clicking it.

nav-timeout.test.cjs (no browser) reads every GUI test file and names
each page given a default under a minute without a navigation timeout of
at least a minute, so a new file can't bring the pattern back.

What was run (WSL magpie-desktop, Playwright 1.62.0, load 70-260 from
other sessions):
- node --check on every file in internal/gui/tests: all parse.
- nav-timeout.test.cjs against origin/main's test files: red (names the
  1174 pages); against this tree: its only reds were my own probe copies
  of the old files, since removed.
- By hand: claude-switch-only with app.js served 7s late (a busy box's
  first load), --test-name-pattern "1100 en": origin/main's file 4 of 4
  red with "page.goto: Timeout 5000ms exceeded"; this file 4 of 4 green.
- claude-switch-only.test.cjs, both engines, en/zh/ja/de, 1100 and 420:
  32 of 32. group-from-model-add, usage-access-key-card and i18n-keys:
  green. agent-plugin-agents: Chromium green; WebKit's de 420 subtest red
  on a 5s waitForFunction at line 115 (a wait after load, not the
  navigation), at load ~230.

Not tried: the whole GUI suite (about 1000 files; the change is one
mechanical line per page), Windows or mote (mote's browsers, #4125).
2026-10-31 02:09:18 +08:00

153 lines
8.9 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// lyston11 (#1573): in a short Mac window, a popover opened upward from near
// the window's foot was held only to 8px from the top, so it covered the
// traffic lights and the header they are drawn over. Every placer that opens
// upward stops under the window's chrome now: an agent's model list (Pick),
// the field picker (#pop) and the app menu (.proto-menu). On a Mac window
// each one's top edge sits under header.top's bottom; on Windows they are
// where they were, 8px from the top. Each is opened from an anchor at the
// foot of a short, narrow window; no click moves the page. English and
// Chinese, Chromium and WebKit; no backend, the API is faked here.
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");
const models = () => [
...["gpt-5.5", "gpt-5.5-mini", "gpt-5.4", "gpt-5.3-codex", "o4", "o4-mini"].map((m, i) =>
({ id: "openai/" + m, name: m.toUpperCase(), group: "OpenAI", icon: "openai", context: 400e3, inUse: i === 0 })),
...Array.from({ length: 30 }, (_, i) => ({ id: "openrouter/m" + i, name: "Router Model " + i, group: "OpenRouter", icon: "openrouter", context: 128e3 })),
];
function server(lang) {
const list = models();
const options = list.map((m) => ({ value: "magpie/" + m.id, label: m.name, ref: m.id }));
const state = () => ({
agents: [{
id: "codex", name: "Codex", path: "/test/config.toml", icon: "codex-color", wired: true,
fields: [{ key: "model", label: "model", value: options[0].value, options }],
models: { shown: list.length, listed: list.length, by: [{ name: "OpenAI", icon: "openai", n: 6 }, { name: "OpenRouter", icon: "openrouter", n: 30 }] },
}],
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:"${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/agent-models/codex") return json({ models: list });
if (url.pathname === "/api/groups") return json({ groups: [] });
if (url.pathname === "/api/plugins") return json({ plugins: [] });
if (url.pathname === "/api/providers") return json({ providers: [], gateway: { running: true, window: true } });
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)];
try { await route.fulfill({ body: await fs.readFile(file), contentType }); } catch { await route.fulfill({ status: 404, body: "" }); }
};
}
const settled = (page) => page.evaluate(() => new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r))));
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const platform of ["MacIntel", "Win32"]) for (const lang of ["en", "zh"]) {
test(`${engine} ${platform} ${lang}: a popover opened upward stays under the window's chrome`, async (t) => {
const mac = platform === "MacIntel";
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
const ctx = await browser.newContext({ viewport: { width: 480, height: 560 } });
await ctx.addInitScript((p) => { Object.defineProperty(Navigator.prototype, "platform", { get: () => p }); }, platform);
const page = await ctx.newPage();
page.setDefaultTimeout(5000);
page.setDefaultNavigationTimeout(60000);
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", server(lang));
await page.goto("http://magpie.test/?mode=window");
assert.equal(await page.evaluate(() => document.body.classList.contains("mac")), mac);
const head = await page.evaluate(() => document.querySelector("header.top").getBoundingClientRect().bottom);
if (mac) assert(head >= 49, `a Mac window's header reaches under the traffic lights: ${head}`);
// the highest a popover may start: under the chrome on a Mac, 8px elsewhere
const floor = mac ? head + 8 : 8;
const row = page.locator('.row.agent[data-id="codex"]');
await row.locator(".ag-link").click();
const entry = row.locator(".ag-exp .ag-chips .ag-quiet");
await entry.waitFor();
await page.waitForFunction(() => document.getAnimations().length === 0);
// the window made short enough that the anchor is at its foot, with
// more room over it than under it; clicked where it is, as a person
// does (Playwright's own click scrolls it into view first, and a
// scroll closes a popover)
const atFoot = async (loc, room) => {
const b = await loc.boundingBox();
await page.setViewportSize({ width: 480, height: Math.round(b.y + b.height + 24) });
await settled(page);
const r = await loc.boundingBox();
assert(r.y > room, `the anchor has room over it: ${r.y}`);
return () => page.mouse.click(r.x + Math.min(r.width / 2, 30), r.y + r.height / 2);
};
const tops = () => page.evaluate(() => [scrollY, $("#view-agents").scrollTop]);
// 1. the agent's model list (Pick), the reporter's case
let click = await atFoot(entry, 200);
let was = await tops();
await click();
const am = page.locator(".am-pop:not(.leaving)");
await am.waitFor();
await page.waitForFunction(() => document.querySelector(".am-pop:not(.leaving)")?.getAnimations().length === 0);
assert(await am.evaluate((e) => e.classList.contains("up")), "it opens upward");
let b = await am.boundingBox();
assert(b.y >= floor - 0.5, `the model list's top ${b.y} is under ${floor}`);
assert(b.y < floor + 40, `the model list still uses the room it has: ${b.y}`);
assert.deepEqual(await tops(), was, "opening it moves nothing");
await page.keyboard.press("Escape");
await page.waitForFunction(() => !document.querySelector(".am-pop:not(.leaving)"));
// 2. the field picker (#pop) from the model field at the row's foot:
// too tall to fit under or over it, it is held to the window's top
await page.setViewportSize({ width: 480, height: 560 });
const field = row.locator('.field[data-key="model"]').first();
click = await atFoot(field, 120);
was = await tops();
await click();
const pop = page.locator("#pop:not([hidden])");
await pop.locator("#list li").first().waitFor();
await page.waitForFunction(() => document.querySelector("#pop").getAnimations().length === 0);
b = await pop.boundingBox();
assert(b.y >= floor - 0.5, `the picker's top ${b.y} is under ${floor}`);
assert(b.y + b.height <= (await page.evaluate(() => innerHeight)) + 0.5, "the picker is on the screen whole");
assert.deepEqual(await tops(), was, "opening it moves nothing");
await page.keyboard.press("Escape");
await page.waitForFunction(() => document.querySelector("#pop").hidden);
// 3. an app menu (.proto-menu) taller than the room under its anchor
was = await tops();
await page.evaluate(() => {
const a = document.createElement("button");
a.id = "pm-anchor";
a.textContent = "menu";
a.style.cssText = `position:fixed;left:20px;top:${innerHeight - 40}px;height:24px`;
document.body.append(a);
openProtoMenu(a, Array.from({ length: 40 }, (_, i) => ({ v: "v" + i, name: "Choice " + i, note: "", literalName: true })), "v0", () => {});
});
const menu = page.locator(".proto-menu");
await menu.waitFor();
await page.waitForFunction(() => document.querySelector(".proto-menu").getAnimations().length === 0);
b = await menu.boundingBox();
assert(await menu.evaluate((e) => e.classList.contains("up")), "the menu opens upward");
assert(b.y >= floor - 0.5, `the menu's top ${b.y} is under ${floor}`);
assert(b.y + b.height <= (await page.evaluate(() => innerHeight)) - 30, "the menu ends over its anchor");
assert.deepEqual(await tops(), was, "opening it moves nothing");
if (process.env.ARTIFACT_DIR) {
await fs.mkdir(process.env.ARTIFACT_DIR, { recursive: true });
await page.screenshot({ path: path.join(process.env.ARTIFACT_DIR, `${engine}-${platform}-${lang}-popover.png`) });
}
assert.deepEqual(errors, []);
});
}
}