Files
magpie/internal/gui/tests/text-size.test.cjs
T
yetone 575e694ed7 settings: a Text size preference (文字大小) that makes everything in magpie's windows larger, 100/110/125/150%, with Ctrl/Cmd + = − 0 in the window and the tray panel, and Windows' text drawn in its own UI faces (Discord, Clarence Dan: the font size is hard on the eyes on Windows) — Settings → Preferences gains a Text size row under Language whose pick posts to a new POST /api/settings/text-size (saved as settings.TextSize, validated against 100/110/125/150, 0 read as 100, and kept by the Settings page's general save, which never sends it). The size is each webview's own zoom, as a browser zooms, not CSS zoom: CSS zoom leaves getBoundingClientRect and clientX in zoomed pixels while offsets, scrollTop and 100vh are not, and app.js, routing.js and library.js place menus, popovers and the panel's height by those measures; under the webview's zoom the page is laid out again in fewer, larger CSS pixels and every measure agrees. Windows and Linux use Wails' SetZoom (WebView2's zoom factor, WebKitGTK's zoom level; the Zoom window option at creation, so the pages open at the saved size), the Mac WKWebView.pageZoom through cgo (macOS 11+), since Wails' Mac zoom is magnification, which lays nothing out again. None is under 100% because Wails clamps SetZoom below 1 on Windows and Linux. The windows grow around the pages: the panel's width and the height its page asks for (still in CSS pixels) are multiplied by the zoom, capped at the screen's work area (textsize.go panelFrame), and resized at once when the size changes; the main window's minimum is 560×420 CSS pixels at the size, capped by the screen, so a window smaller than that grows. Runtime changes wait for the app to be ready (whenReady), and at startup the Mac applies the zoom once its webviews exist. The Mac header, drawn under the native traffic lights which do not grow, divides its height, left padding and the modal's top by --zoom (set from boot.js before the first paint and from the settings), so it stays 50 points tall and level with them. The shortcuts are Cmd on the Mac and Ctrl elsewhere (the other modifier does nothing), step through the sizes and 0 goes back to 100%, with a "Text size 125%" toast; in magpie web the row is hidden and the keys are left to the browser's own zoom. On Windows the page names its faces (html.win): Segoe UI Variable Text, Segoe UI, then Microsoft YaHei UI for Chinese rather than whatever WebView2 falls back to; the Mac and Linux stack is unchanged, and there is no automatic size bump. New strings in en and zh. Tested: internal/gui/tests/text-size.test.cjs in Chromium and WebKit, en and zh, light and dark — a pick posts and does not scroll the settings page, stays picked after a reload with --zoom from boot.js, the Settings page's other saves leave it, the shortcuts step and reset in the window and the panel, and at 150% (the smallest window, 840×630 points, as 560×420 CSS pixels at device scale 1.5) no view, the header or the panel runs off to the side, the Mac header is 50 points; screenshots at 100% and 150% looked at. currency.test.cjs's viewport is 480 tall instead of 420, as settings-groups' is: the new row put the Currency control under the footer, where WebKit's hover could not reach it. Go: TestTextSizeFrames (panel and window sizes) and TestTextSizeSetting (the endpoint saves, zooms the windows, rejects 133, survives the general save, boot.js carries it) in internal/gui, and the settings round trip. go build -tags nogui (and GOOS=linux, GOOS=windows, plain darwin), go test -tags nogui ./... (TestUserPath flaked once under load and passes alone) and the whole GUI suite with --test-concurrency=2 pass. Not tried in the real Wails window on any platform: the Mac pageZoom, WebView2's zoom factor with the resized panel and the Windows font are untested there.
2026-09-30 02:51:17 +08:00

236 lines
13 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Run with Node's test runner and Playwright on the module path; see README.md.
// The Text size setting (Settings → Preferences, Ctrl/Cmd + − 0): a pick
// posts to /api/settings/text-size and stays picked after a reload (boot.js
// carries it); the shortcuts step through 100/110/125/150% and back, in the
// window and the tray panel, and a click on the control never scrolls the
// page. The zoom itself is the webview's (Go), so here it is as a browser
// zoomed: the window's points over the zoom in CSS pixels, deviceScaleFactor
// the zoom — and at 150% nothing in any view runs off to the side, the Mac
// header still level with the traffic lights. English and Chinese; no
// backend, the API is faked here. ARTIFACT_DIR gets screenshots at 100% and
// 150%, light and dark.
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 usage = {
calls: 5, errors: 0, input: 12000, output: 4000, cache_read: 500, cache_write: 100, reasoning: 200,
unpriced: 0, cost: 12.34, bucket: "day",
series: [
{ label: "Mon", input: 6000, output: 2000, calls: 3, cost: 7 },
{ label: "Tue", input: 6000, output: 2000, calls: 2, cost: 5.34 },
],
agents: [{ name: "Claude Code", calls: 5, cost: 12.34 }],
models: [{ name: "claude-opus", calls: 5, cost: 12.34 }],
};
const calls = Array.from({ length: 5 }, (_, i) => ({
time: new Date(Date.now() - (i + 1) * 60e3).toISOString(), agent: "fixture", model: "fixture/model-a", status: 200, ms: 900,
}));
const providers = {
providers: [],
presets: [], excluded: [],
gateway: { running: true, window: true, mine: true, url: "http://127.0.0.1:3999", calls, groups: [] },
};
function settingsPayload(over) {
return {
theme: "light", lang: "en", tray: "panel", quotaLeft: false, currency: "usd", textSize: 100,
dock: false, dockWindow: false, proxy: "", redact: false, redactPersonal: false, redactWords: [],
codexWarmup: "", claudeWarmup: "", codexWarmAt: "", claudeWarmAt: "", workbuddyCheckin: false, noStats: false,
trayUsage: "", trayUsageEvery: 3, vision: "", imageGen: "",
version: "0.1.400", dir: "~/.config/magpie", gateway: "http://127.0.0.1:3425",
proxyNow: "none", proxySource: "none", login: false,
visionModels: [], imageGenModels: [], workbuddyCheckins: [], lanURLs: [],
fx: { rate: 7.2, at: new Date().toISOString(), stale: false },
...over,
};
}
// one fake magpie per test: its settings live across reloads, as the real
// one's do, and every text size posted is kept in posts
function server(lang, theme, size, posts) {
let cur = settingsPayload({ lang, theme, textSize: size });
return async (route) => {
const req = route.request(), url = new URL(req.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:"${theme}",web:false,textSize:${cur.textSize}};` });
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, textSize: cur.textSize }, fx: cur.fx });
if (url.pathname === "/api/settings/text-size") {
const { size: n } = req.postDataJSON();
posts.push(n);
cur = { ...cur, textSize: n };
return json(cur);
}
if (url.pathname === "/api/settings") {
// the Settings page's own save never carries the text size
if (req.method() === "POST") cur = { ...cur, ...req.postDataJSON(), textSize: cur.textSize };
return json(cur);
}
if (url.pathname === "/api/usage") return json(usage);
if (url.pathname === "/api/library") return json({ dir: "~/.config/magpie/library", backups: "", home: "~", agents: [], instructions: { agents: [], sets: [] }, servers: [], foundServers: [], skills: [] });
if (url.pathname === "/api/usage/quotas") return json([]);
if (url.pathname === "/api/groups") return json({ groups: [], models: [] });
if (url.pathname === "/api/providers") return json(providers);
if (url.pathname === "/api/gateway/trace") {
if (url.searchParams.get("wait")) await new Promise((r) => setTimeout(r, 20e3));
return json({ mine: true, now: new Date().toISOString(), seq: 0, totals: { requests: 0, rerouted: 0, errors: 0 }, routes: [] });
}
if (url.pathname === "/api/gateway/history") return json({ cut: false, days: [], routes: [] });
if (url.pathname === "/api/window/fit") return route.fulfill({ status: 204 });
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 zoomVar = (page) => page.evaluate(() => getComputedStyle(document.documentElement).getPropertyValue("--zoom").trim());
const settingsTop = (page) => page.locator("#view-settings").evaluate((v) => v.scrollTop);
// what runs off to the side: the page, the view, and anything in the view
// wider than it
const overflow = (page) => page.evaluate(() => {
const out = [];
const de = document.documentElement;
if (de.scrollWidth > de.clientWidth) out.push("page " + de.scrollWidth + ">" + de.clientWidth);
const v = [...document.querySelectorAll("main.view")].find((m) => !m.hidden);
if (v && v.scrollWidth > v.clientWidth) out.push(v.id + " " + v.scrollWidth + ">" + v.clientWidth);
const top = document.querySelector(".top");
if (top && top.scrollWidth > top.clientWidth) out.push("header " + top.scrollWidth + ">" + top.clientWidth);
return out;
});
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
test(engine + ": the text size", async (t) => {
assert(["chromium", "webkit"].includes(engine), "BROWSER must be chromium or webkit");
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
const shots = [];
t.after(async () => {
if (process.env.ARTIFACT_DIR) {
await fs.mkdir(process.env.ARTIFACT_DIR, { recursive: true });
for (const [name, buf] of shots) await fs.writeFile(path.join(process.env.ARTIFACT_DIR, `${engine}-text-size-${name}.png`), buf);
}
await browser.close();
});
const open = async (lang, theme, size, posts, { width = 900, height = 520, zoom = 1, url = "/" } = {}) => {
const context = await browser.newContext({ viewport: { width, height }, deviceScaleFactor: zoom, reducedMotion: "reduce", colorScheme: theme });
const page = await context.newPage();
page.setDefaultTimeout(5000);
page.errors = [];
page.on("pageerror", (e) => page.errors.push(e.message));
await page.route("**/*", server(lang, theme, size, posts));
await page.goto("http://magpie.test" + url);
return page;
};
for (const [lang, name, toast] of [["en", "Text size", "Text size 125%"], ["zh", "文字大小", "文字大小 125%"]]) {
await t.test(lang + ": picked, kept, and stepped with the keys", async () => {
const posts = [];
const page = await open(lang, "light", 100, posts);
const mod = await page.evaluate(() => /^Mac/.test(navigator.platform) ? "Meta" : "Control");
assert.equal(await zoomVar(page), "1");
await page.locator("#prefs").click();
const segs = page.locator("#textSizeSegs .opt");
await segs.first().waitFor();
assert.equal((await page.locator("#textSizeRow .name").textContent()).trim(), name);
assert.deepEqual((await segs.allTextContents()).map((s) => s.trim()), ["100%", "110%", "125%", "150%"]);
assert.equal(await segs.nth(0).evaluate((b) => b.classList.contains("on")), true, "100% starts picked");
const sub = await page.locator("#textSizeSub").textContent();
assert(sub.includes(mod === "Meta" ? "⌘0" : "Ctrl+0"), sub);
// scrolled a little (a real wheel), picking 125% moves nothing
await page.locator("#textSizeSegs").hover();
for (let i = 0; i < 20 && !(await settingsTop(page)); i++) { await page.mouse.wheel(0, 30); await page.waitForTimeout(20); }
const before = await settingsTop(page);
assert(before > 0, "the settings list must scroll");
await segs.nth(2).click();
await page.locator("#textSizeSegs .opt.on", { hasText: "125%" }).waitFor();
await page.waitForTimeout(300);
assert.equal(await settingsTop(page), before, "picking a text size must not scroll the settings page");
assert.deepEqual(posts, [125]);
assert.equal(await zoomVar(page), "1.25");
// kept: a reload has it from boot.js before the first paint
await page.reload();
assert.equal(await zoomVar(page), "1.25");
await page.locator("#prefs").click();
await page.locator("#textSizeSegs .opt.on", { hasText: "125%" }).waitFor();
// the keys: + to 150, + again stays there, − to 125, 0 back to 100
await page.keyboard.press(mod + "+Equal");
await page.locator("#textSizeSegs .opt.on", { hasText: "150%" }).waitFor();
await page.keyboard.press(mod + "+Equal");
await page.keyboard.press(mod + "+Minus");
await page.locator("#textSizeSegs .opt.on", { hasText: "125%" }).waitFor();
assert.equal((await page.locator("#status").textContent()).trim(), toast);
await page.keyboard.press(mod + "+Digit0");
await page.locator("#textSizeSegs .opt.on", { hasText: "100%" }).waitFor();
assert.deepEqual(posts, [125, 150, 125, 100]);
assert.equal(await zoomVar(page), "1");
// the other modifier is not the shortcut
await page.keyboard.press((mod === "Meta" ? "Control" : "Alt") + "+Equal");
await page.waitForTimeout(100);
assert.deepEqual(posts, [125, 150, 125, 100]);
// a save of the page's other settings leaves it be
await page.locator("#themeSegs .opt").nth(2).click();
await page.waitForTimeout(200);
await page.reload();
await page.locator("#prefs").click();
await page.locator("#textSizeSegs .opt.on", { hasText: "100%" }).waitFor();
assert.deepEqual(page.errors, []);
await page.context().close();
});
await t.test(lang + ": the panel", async () => {
const posts = [];
const page = await open(lang, "light", 110, posts, { width: 440, height: 560, url: "/?mode=panel" });
const mod = await page.evaluate(() => /^Mac/.test(navigator.platform) ? "Meta" : "Control");
await page.locator("#view-agents").waitFor();
assert.equal(await zoomVar(page), "1.1");
await page.keyboard.press(mod + "+Equal");
await page.waitForTimeout(200);
await page.keyboard.press(mod + "+Digit0");
await page.waitForTimeout(200);
assert.deepEqual(posts, [125, 100]);
assert.deepEqual(page.errors, []);
await page.context().close();
});
for (const theme of ["light", "dark"]) {
await t.test(lang + ", " + theme + ": nothing runs off the side at 150%", async () => {
// the smallest window at 150% is 840×630 points, 560×420 CSS pixels
for (const [zoom, size] of [[1, 100], [1.5, 150]]) {
const page = await open(lang, theme, size, [], { width: Math.round(840 / zoom), height: Math.round(630 / zoom), zoom });
for (const v of ["agents", "providers", "gateway", "routing", "usage", "library", "settings"]) {
await page.locator(v === "settings" ? "#prefs" : `[data-view="${v}"]`).click();
await page.waitForTimeout(250);
assert.deepEqual(await overflow(page), [], `${v} at ${size}%`);
assert.equal(await page.locator("#status.err").count(), 0, `${v}: ${await page.locator("#status").textContent()}`);
if (v === "agents" || v === "settings") shots.push([`${lang}-${theme}-${size}-${v}`, await page.screenshot()]);
}
if (await page.evaluate(() => document.body.classList.contains("mac"))) {
// the header in points stays 50 tall, as the traffic lights want
const h = await page.locator(".top").evaluate((e) => e.getBoundingClientRect().height);
assert(Math.abs(h * zoom - 50) < 1, `header ${h}px at ${size}%`);
}
assert.deepEqual(page.errors, []);
await page.context().close();
}
// the panel, as wide as it always is in CSS pixels
const panel = await open(lang, theme, 150, [], { width: 440, height: 560, zoom: 1.5, url: "/?mode=panel" });
await panel.locator("#view-agents").waitFor();
await panel.waitForTimeout(250);
assert.deepEqual(await overflow(panel), [], "the panel at 150%");
shots.push([`${lang}-${theme}-150-panel`, await panel.screenshot()]);
await panel.context().close();
});
}
}
});
}