Files
magpie/internal/gui/tests/settings-proxy-auth.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

217 lines
12 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// A proxy's optional username and password, in fields of their own for
// every kind of proxy (SOCKS, and HTTP for 微雨南山 on Discord), survive
// saving/reloading, never appear in the address or status, and typing
// between fields cannot save a partial login. A login saved in the address
// before loads into the fields.
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 server(lang, posts, proxy = "") {
let cur = {
theme: "light", lang, tray: "panel", currency: "usd", textSize: 100,
proxy, proxyNow: proxy, proxySource: proxy ? "settings" : "none",
trayUsageEvery: 3, version: "test", dir: "/test", lanURLs: [],
visionModels: [], imageGenModels: [], workbuddyCheckins: [],
fx: { rate: 7.2, at: new Date().toISOString(), stale: false },
};
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 = ${JSON.stringify({ lang, 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: { lang, theme: "light" }, fx: cur.fx });
if (url.pathname === "/api/settings") {
if (req.method() === "POST") {
const body = req.postDataJSON();
posts.push(body);
cur = { ...cur, ...body, proxyNow: body.proxy, proxySource: body.proxy === "direct" ? "off" : body.proxy ? "settings" : "none" };
}
return json(cur);
}
if (url.pathname === "/api/plugins") return json({ plugins: [] });
if (url.pathname === "/api/usage/quotas") return json([]);
if (url.pathname === "/api/groups") return json({ groups: [], models: [] });
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)];
return route.fulfill({ body: await fs.readFile(file), contentType });
};
}
const labels = {
en: { user: "Username (optional)", pass: "Password (optional)", custom: "Custom", save: "Save", off: "Off", auto: "Auto", saved: "Saved" },
zh: { user: "用户名(可选)", pass: "密码(可选)", custom: "自定义", save: "保存", off: "关闭", auto: "自动", saved: "已保存" },
"zh-TW": { user: "使用者名稱(選填)", pass: "密碼(選填)", custom: "自定義", save: "儲存", off: "關閉", auto: "自動", saved: "已儲存" },
ja: { user: "ユーザー名(任意)", pass: "パスワード(任意)", custom: "カスタム", save: "保存", off: "オフ", auto: "自動", saved: "保存しました" },
de: { user: "Benutzername (optional)", pass: "Passwort (optional)", custom: "Benutzerdefiniert", save: "Speichern", off: "Aus", auto: "Automatisch", saved: "Gespeichert" },
};
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of Object.keys(labels)) {
const l = labels[lang];
test(`${engine} ${lang}: optional proxy credentials in settings`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
const context = await browser.newContext({ viewport: { width: 1100, height: 760 }, reducedMotion: "reduce" });
const page = await context.newPage();
page.setDefaultTimeout(4000);
page.setDefaultNavigationTimeout(60000);
const posts = [], errors = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", server(lang, posts));
await page.goto("http://magpie.test/?view=settings&tab=network");
const box = page.locator("#proxySegs");
const address = box.locator("input.proxy");
const user = box.getByRole("textbox", { name: l.user, exact: true });
const pass = box.getByLabel(l.pass, { exact: true });
const saved = async (action, want) => {
const count = posts.length;
const response = page.waitForResponse((r) => r.url().endsWith("/api/settings") && r.request().method() === "POST");
await action();
await response;
await page.waitForFunction((word) => document.querySelector("#status")?.textContent.includes(word), l.saved);
assert.equal(posts.length, count + 1, "save once for the whole proxy");
assert.equal(posts.at(-1).proxy, want);
};
await box.getByRole("button", { name: l.custom, exact: true }).click();
await address.fill("socks5://127.0.0.1:1080");
await user.fill("a@b:/% 用户");
await user.press("Tab");
assert.equal(posts.length, 0, "tabbing from username to password must not save half a login");
await pass.fill("p@ss:/% #密码");
assert.equal(await pass.getAttribute("type"), "password");
const encoded = "socks5://a%40b%3A%2F%25%20%E7%94%A8%E6%88%B7:p%40ss%3A%2F%25%20%23%E5%AF%86%E7%A0%81@127.0.0.1:1080";
await saved(async () => {
// WebKit may skip Save when tabbing, leaving the editor and saving
// now. If Tab stays inside, clicking outside saves the same login.
await pass.press("Tab");
await page.locator("#proxySub").click();
}, encoded);
await page.reload();
assert.equal(await address.inputValue(), "socks5://127.0.0.1:1080");
assert.equal(await user.inputValue(), "a@b:/% 用户");
assert.equal(await pass.inputValue(), "p@ss:/% #密码");
assert(!((await page.locator("#proxySub").innerText()).includes("p%40ss")), "status exposes the encoded password");
assert(!((await page.locator("#proxySub").innerText()).includes("p@ss")), "status exposes the password");
for (const width of [1100, 560, 375]) {
await page.setViewportSize({ width, height: 760 });
for (const field of [address, user, pass]) {
const rect = await field.boundingBox();
assert(rect && rect.x >= 0 && rect.x + rect.width <= width, `proxy field outside ${width}px window`);
}
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}-${lang}-proxy-${width}.png`) });
}
}
await user.fill("");
await pass.fill("");
await saved(() => pass.press("Enter"), "socks5://127.0.0.1:1080");
await page.reload();
assert.equal(await user.inputValue(), "");
assert.equal(await pass.inputValue(), "");
// Pasting an existing URL separates and decodes the login, including IPv6.
await address.fill("socks5h://name:p%40ss@[::1]:1080");
assert.equal(await address.inputValue(), "socks5h://[::1]:1080");
assert.equal(await user.inputValue(), "name");
assert.equal(await pass.inputValue(), "p@ss");
await saved(() => pass.press("Enter"), "socks5h://name:p%40ss@[::1]:1080");
await pass.fill("discard-me");
const beforeEscape = posts.length;
await pass.press("Escape");
assert.equal(await pass.inputValue(), "p@ss");
assert.equal(posts.length, beforeEscape, "Escape must discard, not save, the draft");
// Arbitrary encoded bytes in an existing password remain masked and intact.
await address.fill("socks5://name:p%FFss@127.0.0.1:1080");
assert.equal(await address.inputValue(), "socks5://127.0.0.1:1080");
assert.equal(await pass.inputValue(), "p%FFss");
await saved(() => box.getByRole("button", { name: l.save, exact: true }).click(), "socks5://name:p%FFss@127.0.0.1:1080");
// An HTTP proxy has the same fields (微雨南山 on Discord): its login
// is typed as it is, @ : / % # and spaces included, and each part is
// encoded into the address, which keeps no slash or port of URL's.
await address.fill("http://127.0.0.1:7890");
assert.equal(await user.isVisible(), true, "an HTTP proxy has a username field");
assert.equal(await pass.isVisible(), true, "an HTTP proxy has a password field");
await user.fill("");
await pass.fill("");
await saved(() => address.press("Enter"), "http://127.0.0.1:7890");
await user.fill("me@corp:x");
await pass.fill("p@ss:w/rd% #1 x");
// the bytes internal/netproxy's TestProxyLoginWithReservedCharacters signs in with
const httpLogin = "http://me%40corp%3Ax:p%40ss%3Aw%2Frd%25%20%231%20x@127.0.0.1:7890";
await saved(() => pass.press("Enter"), httpLogin);
await page.reload();
assert.equal(await address.inputValue(), "http://127.0.0.1:7890");
assert.equal(await user.inputValue(), "me@corp:x");
assert.equal(await pass.inputValue(), "p@ss:w/rd% #1 x");
assert.equal(await pass.getAttribute("type"), "password");
for (const secret of ["p@ss", "p%40ss", "corp"]) assert(!(await page.locator("#proxySub").innerText()).includes(secret), `status shows ${secret}`);
for (const width of [560, 375]) {
await page.setViewportSize({ width, height: 760 });
for (const field of [address, user, pass]) {
const rect = await field.boundingBox();
assert(rect && rect.x >= 0 && rect.x + rect.width <= width, `HTTP proxy field outside ${width}px window`);
}
if (process.env.ARTIFACT_DIR) await page.screenshot({ path: path.join(process.env.ARTIFACT_DIR, `${engine}-${lang}-http-proxy-${width}.png`) });
}
await page.setViewportSize({ width: 1100, height: 760 });
// A bare host:port stays as typed, with or without a login.
await user.fill("");
await pass.fill("");
await address.fill("127.0.0.1:7891");
await saved(() => page.locator("#proxySub").click(), "127.0.0.1:7891");
await user.fill("u");
await pass.fill("p w");
await saved(() => pass.press("Enter"), "u:p%20w@127.0.0.1:7891");
await page.reload();
assert.equal(await address.inputValue(), "127.0.0.1:7891");
assert.equal(await pass.inputValue(), "p w");
await address.fill("socks5://127.0.0.1:1080");
await user.fill("unsaved-user");
await pass.fill("unsaved-password");
await saved(() => box.getByRole("button", { name: l.off, exact: true }).click(), "direct");
await saved(() => box.getByRole("button", { name: l.auto, exact: true }).click(), "");
assert.deepEqual(errors, []);
});
test(`${engine} ${lang}: an HTTP proxy saved with its login in the address loads into the fields`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
const context = await browser.newContext({ viewport: { width: 375, height: 760 }, reducedMotion: "reduce" });
const page = await context.newPage();
page.setDefaultTimeout(4000);
page.setDefaultNavigationTimeout(60000);
const posts = [], errors = [];
page.on("pageerror", (e) => errors.push(e.message));
// as one typed whole before: a raw @ in the password, as Go reads it
await page.route("**/*", server(lang, posts, "https://al%20ice:se@cr%23et@proxy.example:8443"));
await page.goto("http://magpie.test/?view=settings&tab=network");
const box = page.locator("#proxySegs");
assert.equal(await box.locator("input.proxy").inputValue(), "https://proxy.example:8443");
assert.equal(await box.getByRole("textbox", { name: l.user, exact: true }).inputValue(), "al ice");
const pass = box.getByLabel(l.pass, { exact: true });
assert.equal(await pass.inputValue(), "se@cr#et");
assert.equal(await pass.getAttribute("type"), "password");
const sub = await page.locator("#proxySub").innerText();
assert(!sub.includes("se@cr") && !sub.includes("al%20ice"), `status shows the login: ${sub}`);
// left as it was, it is saved as it was
await pass.press("Enter");
assert.equal(posts.length, 0, "an unchanged login is not saved again");
assert.deepEqual(errors, []);
});
}
}