Files
magpie/internal/gui/tests/account-mask.test.cjs
T
yetone 655f96ede4 gui tests: usage-fold and account-mask no longer reload the Usage page the moment it opens, so WebKit has no first reads to cancel and report as page errors (#1350)
Both tests opened the page in a new browser context, cleared their
localStorage keys and reloaded at once. A new context's storage is already
empty, and the reload lands while the page's first reads (agents/cli,
agents/install, usage, upstream, update, whatsnew) are on their way. WebKit
logs each fetch it cancels as "Fetch API cannot load … due to access
control checks." with source "javascript", which Playwright's WebKit turns
into a pageerror, and the tests' final assert.deepEqual(errors, []) failed.
The app catches every one of those reads; nothing is wrong in the page.

Instrumented runs show it: requestfailed "cancelled" for whatsnew/update
and the two page errors within 70ms after the first reload, none after.

Verified (Playwright 1.62.1, macOS arm64), WebKit "several accounts"
(en+zh), 8 copies at once, 5 rounds:
- before: 3 of 80 cases red, each with "Fetch API cannot load … due to
  access control checks" right after the first reload
- after: 80 of 80 pass, no page error
- usage-fold.test.cjs + account-mask.test.cjs, Chromium and WebKit,
  10 runs: 100/100 pass (10 cases each run)
2026-10-09 19:24:16 +08:00

137 lines
8.9 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// Hide accounts hides every account, not only an email address (#568: a
// WorkBuddy account's nickname stayed in sight while 邮箱已打码): a name
// the page was told of — an allowance's user, a route's account — is
// blurred in its row, a sentence and a tooltip, on Usage and Routing; a
// card's own name ("ZCode" signed in with no name of its own, WorkBuddy)
// stays, as does a name inside a longer word. The tray panel follows the
// window's setting, as it is turned. English and Chinese; 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 quotas = () => [
{ provider: "workbuddy", name: "WorkBuddy", icon: "workbuddy-color", plan: "Free", user: "李雷", windows: [{ name: "Credits", used: 8.9, display: "500.52 / 5600" }] },
{ provider: "workbuddy", name: "WorkBuddy", icon: "workbuddy-color", plan: "Free", user: "138****5678", windows: [{ name: "Credits", used: 1, display: "56 / 5600" }] },
{ provider: "zcode", name: "ZCode", icon: "zcode", plan: "Start", user: "ZCode", windows: [{ name: "5 hours", used: 10 }] },
{ provider: "qoder", name: "Qoder", icon: "qoder", plan: "Pro", user: "Lee", windows: [{ name: "Credits", used: 3 }] },
{ provider: "codex", name: "Codex", icon: "openai", plan: "Plus", user: "dev.one@example.com", windows: [{ name: "5 hours", used: 20 }] },
];
function serve(lang, panel) {
const settings = { theme: "light", lang, tray: "panel", quotaLeft: false, currency: "usd" };
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:${!panel}};` });
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: [], presets: [], excluded: [], gateway: { running: true, window: true } });
if (url.pathname === "/api/usage/quotas") return json(quotas());
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);
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 words = {
en: { hide: "Hide accounts", hidden: "Accounts hidden", tip: /^Mask the accounts/, back: /click to show them/ },
zh: { hide: "账号打码", hidden: "账号已打码", tip: /^给本页的账号/, back: /点击显示/ },
};
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of ["en", "zh"]) {
const w = words[lang];
test(`${engine} ${lang}: every account is hidden, not only an address`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
const context = await browser.newContext({ viewport: { width: 900, height: 760 }, reducedMotion: "reduce" });
const errors = [];
const open = async (url) => {
const page = await context.newPage();
page.setDefaultTimeout(5000);
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", serve(lang, url.includes("mode=panel")));
await page.goto(url);
return page;
};
// a new context's storage is empty: no reload to clear it, which
// would cancel the page's first reads, each a page error in WebKit (#1350)
const page = await open("http://magpie.test/?view=usage");
await page.waitForSelector(".subscription-account .user");
const btn = page.locator("#usageMask");
assert.equal((await btn.textContent()).trim(), w.hide);
// the button has a tooltip, and once on it says how to have them back (#653)
assert.match(await btn.getAttribute("title"), w.tip);
// a sentence and a tooltip naming the accounts, and a word with one inside
await page.evaluate(() => {
const d = document.createElement("div");
d.id = "sample";
d.textContent = "李雷 checked in · Sleeper Lee · ZCode";
d.title = "WorkBuddy · 138****5678";
document.querySelector("#view-usage").append(d);
});
await btn.click();
assert.equal((await btn.textContent()).trim(), w.hidden);
assert.match(await btn.getAttribute("title"), w.back);
assert.match(await page.locator("#rtMask").getAttribute("title"), w.back);
await page.waitForFunction(() => document.querySelectorAll("#sample .pii").length === 2);
const got = await page.evaluate(() => ({
users: [...document.querySelectorAll(".subscription-account .user")].map((u) => ({ raw: u.querySelector(".pii")?.dataset.raw ?? null, text: u.textContent, title: u.title })),
heads: [...document.querySelectorAll(".subscription-head b")].map((b) => b.textContent),
sample: document.querySelector("#sample").textContent,
sampleRaws: [...document.querySelectorAll("#sample .pii")].map((e) => e.dataset.raw),
sampleTitle: document.querySelector("#sample").title,
}));
const byRaw = Object.fromEntries(got.users.map((u) => [u.raw, u]));
for (const name of ["李雷", "138****5678", "Lee", "dev.one@example.com"]) {
assert.ok(byRaw[name], `${name} is hidden: ${JSON.stringify(got.users)}`);
assert.ok(!byRaw[name].text.includes(name), `${name} isn't in sight`);
assert.ok(!byRaw[name].title.includes(name), `${name} isn't in its tooltip: ${byRaw[name].title}`);
}
assert.equal(byRaw["138****5678"].title, "•••••••••••");
// ZCode signed in with no name of its own is the card's name: kept
assert.ok(got.users.some((u) => u.raw === null && u.text === "ZCode"), "ZCode's own name stays");
assert.ok(got.heads.includes("WorkBuddy") && got.heads.includes("ZCode"), got.heads.join());
assert.deepEqual(got.sampleRaws, ["李雷", "Lee"]);
assert.match(got.sample, /^\S+ checked in · Sleeper \S+ · ZCode$/);
assert.equal(got.sampleTitle, "WorkBuddy · •••••••••••");
// Routing: a route's account, learnt from what the gateway answered
await page.evaluate(() => {
noteAccounts({ order: [{ kind: "account", who: "韩梅梅", name: "WorkBuddy" }, { kind: "key", who: "team key" }] });
const d = document.createElement("div");
d.id = "route";
d.textContent = "韩梅梅 answered; team key waited";
document.querySelector("#view-routing").append(d);
});
await page.waitForFunction(() => document.querySelector("#route .pii")?.dataset.raw === "韩梅梅");
assert.match(await page.locator("#route").textContent(), /^\S+ answered; team key waited$/);
// the tray panel follows the window's setting, as it is turned
const panel = await open("http://magpie.test/?mode=panel");
await panel.evaluate(() => setPanelTab("usage"));
await panel.waitForFunction(() => [...document.querySelectorAll("#panelQuota .pq-user .pii")].some((e) => e.dataset.raw === "李雷"));
const pq = await panel.evaluate(() => [...document.querySelectorAll("#panelQuota .pq-card")].map((c) => ({ text: c.querySelector(".pq-user").textContent, title: c.title })));
for (const c of pq) assert.ok(!/李雷|138\*|dev\.one|Lee\b/.test(c.text + c.title), JSON.stringify(c));
await btn.click();
assert.equal((await btn.textContent()).trim(), w.hide);
await panel.waitForFunction(() => !document.querySelector("#panelQuota .pii"));
assert.ok(await panel.evaluate(() => [...document.querySelectorAll("#panelQuota .pq-user")].some((e) => e.textContent === "李雷")));
// and back as they were in the window
assert.equal(await page.locator("#sample").textContent(), "李雷 checked in · Sleeper Lee · ZCode");
assert.equal(await page.locator("#sample").getAttribute("title"), "WorkBuddy · 138****5678");
assert.equal(await page.locator(".subscription-account .user", { hasText: "李雷" }).getAttribute("title"), "李雷");
assert.deepEqual(errors, []);
});
}
}