Files
magpie/internal/gui/tests/api-key-theme.test.cjs
T
yetone 10da2936da gui: Settings is a page per part, under a tab each, the part kept in the address (#471: Settings is one long scroll of some 45 rows in 9 sections) — the page now opens with a tab list in the warm-ups' look (role=tablist, a tab each with aria-controls/aria-labelledby): General (appearance, language, text size, the icons, model names, login, Dock, session terminal), Usage (the menu bar's usage, refresh, logos, what usage shows, currency, number units, the two alerts, then the warm-up and check-in tabs), Network and sharing (proxy, local network), Models (images, web search), Privacy, Observability, Sync and backup, About; the part picked shows its rows alone. Every row, its id and what it saves are as they were; the duplicate heading of a one-section part and the Preferences heading are gone. The part is kept as ?view=settings&tab=<part> (dropped on any other view), so a reload or a link lands there, and remembered (magpie.settingsTab) for Settings opened again without one, as the tray's window/main?view=settings; an unknown one falls back to it. A part whose rows are all hidden on this system has no tab, and one picked falls back to the first shown. The arrows, Home and End move along the tabs, one in the Tab order; a click on a tab moves nothing on the page, the tabs staying where they were; a pill drawn while its part was hidden has its thumb put under its pick when shown (the warm-ups' fix, now shared). The tabs wrap to a line more when they don't fit (English in the 560x420 window, at 110 and 125%, a phone), no left-border stripe. The click hold also holds a click not yet held before taking the next (keys pressed within a frame on a tab list whose part shrank to the window: the second click was taken at the clamped top and the page stayed there). New strings General and Network and sharing are in i18n.js (常规, 网络与共享). Tests that reach a row now open or click its part: api-key-theme, currency, gateway-caller-keys, gateway-connect-keys, lan-docker, number-units, otel, redact-rule-row, s3-sync, search-api, settings-groups, sync-other-kind, tray-inuse, tray-logos, tray-usages, update-check, update-hide, usage-alert, webdav-settings-refresh, whats-new; those needing a scrolled page wheel a shorter window or in smaller steps, as a part is shorter than the whole page was. settings-sections.test.cjs (Chromium, WebKit, en, zh) fails without the change (10 of 10). go vet ./internal/..., go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./... pass (TestAccountProxy as on main); settings-sections, settings-groups, click-scroll, old-webkit, text-size and every settings test listed above pass.
2026-10-02 05:50:24 +08:00

135 lines
7.3 KiB
JavaScript

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 { fixture } = require("./fixtures/caller-keys.cjs");
async function palette(page, selectors) {
return page.evaluate((selectors) => {
const root = getComputedStyle(document.documentElement);
const rgb = (name) => {
const hex = root.getPropertyValue(name).trim();
return `rgb(${[1, 3, 5].map((i) => parseInt(hex.slice(i, i + 2), 16)).join(", ")})`;
};
const colors = Object.fromEntries(["bg", "card", "card-2", "fg", "fg-2", "muted", "pill", "line", "accent", "accent-fg"]
.map((name) => [name, rgb("--" + name)]));
const elements = Object.fromEntries(Object.entries(selectors).map(([name, selector]) => {
const s = getComputedStyle(document.querySelector(selector));
return [name, { color: s.color, bg: s.backgroundColor, border: s.borderTopColor }];
}));
return { colors, elements };
}, selectors);
}
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const theme of (process.env.THEME ? [process.env.THEME] : ["light", "dark", "system"])) {
test(`${engine} ${theme}: API key controls follow the global palette`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
const context = await browser.newContext({ viewport: { width: 1000, height: 760 }, reducedMotion: "reduce", colorScheme: "light" });
const page = await context.newPage();
page.setDefaultTimeout(6000);
const events = [], errors = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", fixture("en", theme, events, { lan: true }));
await page.goto("http://magpie.test/?view=gateway");
await page.locator('#gatewayKeys .acc[data-key="server"] .dot').click();
await page.locator('#gatewayKeys .acc.off[data-key="server"]').waitFor();
await page.locator("#addGatewayKey").click();
await page.locator('#gatewayKeys .acc[data-key="laptop"] .rename').click();
await page.locator("#gatewayKeys .rename-in").waitFor();
await page.mouse.move(500, 20);
const gatewaySelectors = { card: "#gatewayKeys .accts", enabled: '#gatewayKeys .acc[data-key="work"] .dot',
disabled: "#gatewayKeys .acc.off .dot", masked: "#gatewayKeys .acc .plan", form: "#gatewayKeys .adding",
input: "#gatewayKeys .adding input", rename: "#gatewayKeys .rename-in", create: "#gatewayKeys .primary", rotate: '#gatewayKeys .acc[data-key="work"] .text', keyPick: "#connectKey" };
const gatewayPaints = [];
for (const scheme of ["light", "dark"]) {
await page.emulateMedia({ colorScheme: scheme });
await page.waitForTimeout(200);
const { colors: c, elements: e } = await palette(page, gatewaySelectors);
assert.equal(e.card.bg, c.card);
assert.equal(e.card.border, c.line);
assert.equal(e.enabled.bg, c.fg);
assert.equal(e.enabled.color, c.card);
assert.equal(e.disabled.color, c.muted);
assert.equal(e.masked.bg, c.pill);
assert.equal(e.masked.color, c.muted);
assert.equal(e.form.bg, c["card-2"]);
for (const input of [e.input, e.rename]) {
assert.equal(input.bg, c.card, "key inputs must not use the browser's default background");
assert.equal(input.color, c.fg);
}
assert.equal(e.rotate.color, c.muted);
assert.equal(e.keyPick.bg, "rgba(0, 0, 0, 0)");
assert.equal(e.create.bg, c.accent);
assert.equal(e.create.color, c["accent-fg"]);
gatewayPaints.push(c.bg);
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}-${theme}-${scheme}-keys.png`) });
}
}
assert.equal(gatewayPaints[0] === gatewayPaints[1], theme !== "system", "only System follows an OS palette change");
await page.locator('#gatewayKeys .acc[data-key="work"]').getByRole("button", { name: "Rotate key", exact: true }).click();
await page.locator("#modal .lib-confirm").waitFor();
for (const scheme of ["light", "dark"]) {
await page.emulateMedia({ colorScheme: scheme });
await page.waitForTimeout(200);
const { colors: c, elements: e } = await palette(page, {
dialog: "#modal .dialog", message: "#modal .lib-confirm", confirm: "#modal .primary",
});
assert.equal(e.dialog.bg, c.card);
assert.equal(e.message.color, c["fg-2"]);
assert.equal(e.confirm.bg, c.accent);
assert.equal(e.confirm.color, c["accent-fg"]);
if (process.env.ARTIFACT_DIR) {
await page.screenshot({ path: path.join(process.env.ARTIFACT_DIR, `${engine}-${theme}-${scheme}-confirm.png`) });
}
}
await page.locator("#modal").getByRole("button", { name: "Cancel", exact: true }).click();
await page.locator("#modal").waitFor({ state: "hidden" });
assert.equal(events.filter((e) => e.action === "rotate-key").length, 0);
await page.goto("http://magpie.test/?view=settings&tab=network");
const on = page.locator("#lanList").getByRole("button", { name: "On", exact: true });
await on.waitFor();
const bounds = await on.boundingBox();
await page.mouse.move(500, 400);
await page.mouse.wheel(0, bounds.y - 250);
await on.click();
await page.locator("#lanList .lan-url").waitFor();
await page.mouse.move(500, 20);
for (const scheme of ["light", "dark"]) {
await page.emulateMedia({ colorScheme: scheme });
await page.waitForTimeout(200);
const { colors: c, elements: e } = await palette(page, { card: "#lanList", text: "#lanList .row.pref:last-child .name",
key: "#lanList .lan-url", copy: "#lanList .row.pref:last-child .copy" });
assert.equal(e.card.bg, c.card);
assert.equal(e.text.color, c.fg);
assert.equal(e.key.color, c["fg-2"]);
assert.equal(e.copy.color, c.muted);
}
if (theme === "system") {
await page.mouse.move(500, 400);
await page.mouse.wheel(0, -5000);
for (const choice of ["Dark", "Light", "System"]) {
await page.locator("#setTab-general").click();
await page.locator("#themeSegs").getByRole("button", { name: choice, exact: true }).click();
await page.waitForFunction((want) => (document.documentElement.dataset.theme || "system") === want, choice.toLowerCase());
await page.waitForFunction(() => !document.documentElement.classList.contains("theming"));
await page.locator("#setTab-network").click();
const { colors: c, elements: e } = await palette(page, { card: "#lanList", key: "#lanList .lan-url" });
assert.equal(e.card.bg, c.card);
assert.equal(e.key.color, c["fg-2"]);
}
await page.reload();
await page.locator("#themeSegs button.on").waitFor({ state: "attached" });
assert.equal(await page.locator("#themeSegs button.on").textContent(), "System");
}
assert.deepEqual(errors, []);
});
}
}