Files
magpie/internal/gui/tests/modal-morph.test.cjs
T
yetone d9d1680ab6 gui: a dialog grows out of the row that opened it and folds back into it
The owner, 10-20: a provider's editor "从右上角弹出" and should open from
the provider's row and close back into it, as iOS does. openModal scaled
the dialog from the row's rect, but a row is wider than the dialog, so
scale alone left a full-size dialog sliding in from the row's middle.

Now the dialog starts as a strip the row's size (transform plus a
clip-path inset, morphFrom), grows into the dialog on the spring, and
closeModal runs it back into the row. The backdrop fades its own
background and blur (VEIL_OFF) instead of the modal's opacity, which
faded the dialog with it, so a dialog folding back stays visible until
it lands. A ResizeObserver keeps the start on the row when an editor
settles its size after it was measured. Reduced motion still only fades.

Every page's dialogs open through the core, so app.js's budget rises
(1,271,683 bytes).

Run on origin/main 75e769a9f + this commit, macOS, playwright 1.62.0:
- modal-morph.test.cjs (new): 6/6, chromium and webkit at 1240 and 420px;
  it fails without the change in webkit.
- gofmt -l clean; go test ./internal/gui/ -run TestCoreScriptBudget ok.
- Full GUI suite (814 files, --test-concurrency=4) on this commit with
  the two after it, on 75e769a9f before the rebase: 11,385 pass, 90
  fail. The morph's own reds (modal-morph chromium 1240 and 420) were
  under load: 6/6 run alone on the rebased head. The rest are the agent
  settings commit's (fixed there) or red alone on origin/main a9304efe0
  too: codex-predictions 420px (#2624), quota-count-wrap and
  quota-name-translated (#2228), routing-bodies, routing-request-metrics,
  s3-sync (#2658), usage-agents-hide, usage-fold (#2399), usage-checkin
  webkit and rail-tip (#3091). agy-launch and name-suffix-remote were red
  only in the full run and pass alone.
- An earlier full run on this commit alone was cut short, not finished.
2026-10-21 02:06:18 +08:00

156 lines
9.7 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// A provider's editor grows out of the row that was clicked and folds back
// into it (the owner, 10-20: it came from the top right or a lower corner,
// not from the provider, unlike iOS). A row is wider than the dialog, so a
// scale alone left a full-size dialog sliding in from the row's middle; the
// dialog is clipped to the row's shape too. Held at the start of the
// opening and at the end of the closing, what shows of the dialog is a
// strip over the row: the row's height, centred on it, inside it. At rest
// nothing is clipped, the shadow included. Halfway back it still shows,
// only the backdrop fading. Reduced motion only fades. Wide and narrow,
// 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 provider = (id, name) => ({
id, name, icon: "generic", chat: `https://api.${id}.example/v1`, url: `https://api.${id}.example/v1`, host: `api.${id}.example`, responses: "", anthropic: "", catalog: "",
models: [{ id: "m1", name: "", on: true }, { id: "m2", name: "", on: true }],
agents: [], fallback: [], headers: {}, keyList: [], proxy: "", key: { set: true, masked: "sk-…1234" }, ready: true,
});
function serve() {
const settings = { theme: "light", lang: "en", tray: "panel", currency: "usd" };
const providers = { providers: ["alpha", "beta", "gamma", "delta", "epsilon", "zeta"].map((id) => provider(id, id[0].toUpperCase() + id.slice(1))), presets: [], excluded: [], gateway: { running: true, window: true } };
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:"en",theme:"light",web: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 });
if (url.pathname === "/api/settings") return json(settings);
if (url.pathname === "/api/providers") return json(providers);
if (url.pathname === "/api/usage/quotas") return json([]);
if (url.pathname === "/api/groups") return json({ groups: [], models: [] });
if (url.pathname === "/api/plugins") return json({ plugins: [] });
if (url.pathname.startsWith("/api/")) return json({});
const file = path.join(assets, url.pathname === "/" ? "index.html" : url.pathname);
try {
await route.fulfill({ body: await fs.readFile(file), contentType: { ".html": "text/html", ".js": "text/javascript", ".css": "text/css", ".svg": "image/svg+xml", ".png": "image/png" }[path.extname(file)] });
} catch {
await route.fulfill({ status: 404, body: "" });
}
};
}
// what shows of the dialog, on screen: its transformed box less its clip,
// with every animation on it and the backdrop held at time `at` (a fraction
// of each one)
const shown = (page, at) => page.evaluate((at) => {
const d = document.querySelector("#modal > .dialog");
for (const a of [...d.parentElement.getAnimations(), ...d.getAnimations()]) { a.pause(); a.currentTime = at * a.effect.getTiming().duration; }
const box = d.getBoundingClientRect(), s = box.width / d.offsetWidth;
const clip = getComputedStyle(d).clipPath;
const m = clip.match(/^inset\(([-\d.]+)px(?: ([-\d.]+)px)?(?: ([-\d.]+)px)?(?: ([-\d.]+)px)?/);
if (!m) return { clip, top: box.top, bottom: box.bottom, left: box.left, right: box.right };
const [t, r = t, b = t, l = r] = m.slice(1).map((v) => v === undefined ? undefined : +v);
return { clip, top: box.top + Math.max(0, t) * s, bottom: box.bottom - Math.max(0, b) * s, left: box.left + Math.max(0, l) * s, right: box.right - Math.max(0, r) * s };
}, at);
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const width of [1240, 420]) {
test(`${engine} ${width}: a provider's editor grows out of its row and folds back into it`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
const page = await browser.newPage({ viewport: { width, height: 760 }, reducedMotion: "no-preference" });
page.setDefaultTimeout(5000);
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", serve());
// the modal's animations are held from the moment they start, so a
// slow machine can't let one finish before it is measured
await page.addInitScript(() => {
const animate = Element.prototype.animate;
Element.prototype.animate = function () {
const a = animate.apply(this, arguments);
if (window.__hold && this.matches("#modal, #modal > .dialog")) a.pause();
return a;
};
});
await page.goto("http://magpie.test/?view=providers");
const row = page.locator('.row.provider[data-id="delta"]');
await row.waitFor();
await page.evaluate(() => { window.__hold = true; });
const r = await row.boundingBox();
// held at the start: a strip of the dialog over the row
await row.locator(".name, .who").first().click();
await page.locator("#modal:not([hidden]) > .dialog").waitFor();
const start = await shown(page, 0);
assert.match(start.clip, /^inset\(/, `${engine} ${width}: the opening clips the dialog: ${start.clip}`);
const near = (a, b, what) => assert(Math.abs(a - b) <= 3, `${engine} ${width}: ${what}: ${a} vs ${b}`);
near(start.top, r.y, "the strip's top on the row's");
near(start.bottom, r.y + r.height, "the strip's bottom on the row's");
assert(start.left >= r.x - 1 && start.right <= r.x + r.width + 1, `${engine} ${width}: the strip stays inside the row: ${JSON.stringify(start)} ${JSON.stringify(r)}`);
if (process.env.MORPH_SHOT) await page.screenshot({ path: `${process.env.MORPH_SHOT}-${engine}-${width}-open0.png` });
for (const at of [.15, .35]) {
await shown(page, at);
if (process.env.MORPH_SHOT) await page.screenshot({ path: `${process.env.MORPH_SHOT}-${engine}-${width}-open${at}.png` });
}
// at rest: nothing clipped, its shadow whole
await page.evaluate(() => { for (const a of document.querySelector("#modal").getAnimations({ subtree: true })) a.finish(); });
const rest = await page.locator("#modal > .dialog").evaluate((d) => [getComputedStyle(d).clipPath, getComputedStyle(d).transform]);
assert.deepEqual(rest, ["none", "none"], `${engine} ${width}: at rest`);
if (process.env.MORPH_SHOT) await page.screenshot({ path: `${process.env.MORPH_SHOT}-${engine}-${width}-rest.png` });
// closed: held at its end, back in the row's strip
await page.keyboard.press("Escape");
await page.locator("#modal.out").waitFor({ state: "attached" });
for (const at of [.5]) {
await shown(page, at);
if (process.env.MORPH_SHOT) await page.screenshot({ path: `${process.env.MORPH_SHOT}-${engine}-${width}-close${at}.png` });
}
// halfway back it is still there to see: the backdrop fades by
// itself, not the modal holding the dialog
const seen = await page.evaluate(() => {
const m = document.querySelector("#modal");
return +getComputedStyle(m).opacity * +getComputedStyle(m.firstElementChild).opacity;
});
assert(seen > .8, `${engine} ${width}: halfway back the dialog shows: ${seen}`);
const end = await shown(page, 1);
near(end.top, r.y, "closed: the strip's top on the row's");
near(end.bottom, r.y + r.height, "closed: the strip's bottom on the row's");
assert(end.left >= r.x - 1 && end.right <= r.x + r.width + 1, `${engine} ${width}: closed inside the row: ${JSON.stringify(end)}`);
await page.evaluate(() => { window.__hold = false; for (const a of [...document.querySelector("#modal").getAnimations({ subtree: true })]) a.play(); });
await page.locator("#modal").waitFor({ state: "hidden" });
assert.equal(await page.locator("#modal > .dialog").evaluate((d) => d.style.clipPath), "", "no clip left behind");
// opened again: it grows from the row again
await page.evaluate(() => { window.__hold = true; });
await row.locator(".name, .who").first().click();
await page.locator("#modal:not([hidden]) > .dialog").waitFor();
const again = await shown(page, 0);
near(again.top, r.y, "again: the strip's top on the row's");
assert.deepEqual(errors, []);
});
}
test(`${engine}: reduced motion only fades`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
const page = await browser.newPage({ viewport: { width: 1240, height: 760 }, reducedMotion: "reduce" });
page.setDefaultTimeout(5000);
await page.route("**/*", serve());
await page.goto("http://magpie.test/?view=providers");
const row = page.locator('.row.provider[data-id="delta"]');
await row.locator(".name, .who").first().click();
await page.locator("#modal:not([hidden]) > .dialog").waitFor();
const moving = await page.locator("#modal > .dialog").evaluate((d) => d.getAnimations().flatMap((a) => a.effect.getKeyframes().flatMap((k) => Object.keys(k))).filter((k) => k === "transform" || k === "clipPath"));
assert.deepEqual(moving, [], "nothing moves or clips under reduced motion");
});
}