Files
magpie/internal/gui/tests/provider-mask.test.cjs
T
yetone 84da86ebd5 gui: Hide accounts is one switch for every page, window and the panel (paultian)
paultian on Discord (#feedback): Routing, Usage and Providers each kept
their own "Hide accounts" state, so turning it on in one page left the
others showing emails. Now window.hideAccounts is one switch, one
localStorage value (magpie.maskEmails), behind #rtMask, #usageMask and
#pvMask alike. Flipping it in any page applies to every page at once,
and a storage event carries it to other windows and the tray panel.
The old magpie.maskProviders value is folded in on first load (hidden
if either value was on) and then removed, so nobody who hid accounts
on one page sees them shown after updating. The tooltip and the
Settings line now say "on every page" instead of "on this page", in
every language.

Tests: provider-mask.test.cjs checks that one switch hides accounts on
every page and in a second window, that the state survives a reload,
and that an old maskProviders=1 comes through as hidden.
account-mask's expected tooltip text is updated.

Run on mote (GUI files at base 05aba5ca4; the Go asset/budget tests again
at this head after rebasing on a92cc3f33):
- go test -run 'Budget|I18n|Asset|Script|Strings|Lang' ./internal/gui: ok
- The masking/privacy GUI files at --test-concurrency=2, chromium and
  webkit, every language (account-mask-alias-plan, account-eye,
  window-mask, usage-fold, masking-demo-unblurred, account-alias,
  provider-mask, account-eye-provider, account-mask,
  privacy-hides-accounts, account-mask-sessions, mask-emails,
  plugin-name-unmasked, i18n-keys, masking-records): 403 of 408 pass.
  The 5 reds:
  - provider-mask "chromium en 420" (a click timed out). Rerun with
    usage-fold at concurrency 1: provider-mask is all green, and that
    run is 146 of 150.
  - usage-fold, 4 subtests: "17 subscriptions and 29 accounts" in
    chromium and webkit, and "several accounts" in webkit en and zh.
    They fail the same 4 at origin/main 625938a2f at concurrency 1
    (6 of 10 pass). That is #3415 / #4790, not this change.
- Not touched: Go code, so no go vet.

Broken by hand: with the old assets, provider-mask's one-switch tests
go red; with the maskProviders migration taken out, the
old-value-comes-through test goes red.

Not tried: the whole GUI suite (the owner's rule is to ship each
verified fix right away), and the real tray panel on Windows/Linux
following a switch flipped in the main window. Only the browser
storage event is tested.
2026-11-03 11:32:47 +08:00

176 lines
12 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// Hide accounts is one switch for every page (paultian in #feedback: the
// Providers page had a switch of its own, so Providers could be hidden
// while Routing wasn't). Routing's, Usage's and Providers' buttons are the
// same switch: turned on any page, every page is hidden and every button
// says so, live in another window and the tray panel too. What an earlier
// version stored for either page is folded into it: hidden on either then
// is hidden everywhere now. Every language, wide and narrow; no backend.
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 MAIL = "dev.one@example.com";
function serve(lang, panel) {
const base = { icon: "generic", chat: "", responses: "", anthropic: "", catalog: "", agents: [], fallback: [], headers: {}, keyList: [], proxy: "" };
const providers = { providers: [
{ ...base, id: "codex", name: "Codex", icon: "codex-color", models: [{ id: "gpt-6", name: "GPT-6", on: true }], account: { agent: "codex", agentName: "Codex", user: MAIL, plan: "PLUS", logins: [{ agent: "codex", user: MAIL, plan: "PLUS", active: true, on: true }] } },
], presets: [], excluded: [], gateway: { running: true, window: true } };
const quotas = [{ provider: "codex", name: "Codex", icon: "openai", plan: "Plus", user: MAIL, windows: [{ name: "5 hours", used: 20 }] }];
const settings = { lang, theme: "light", 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);
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 KEYS = ["Hide accounts", "Accounts hidden", "Mask the accounts on every page — email addresses and account names — for a screenshot to share", "Accounts are masked for a screenshot — click to show them", "Email addresses and account names on every page are blurred, for a screenshot to share"];
const BTNS = { routing: "#rtMask", usage: "#usageMask", providers: "#pvMask" };
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of ["en", "zh", "zh-TW", "ja", "de"]) {
for (const width of [1000, 420]) {
// stored: what an earlier version left, [maskEmails, maskProviders]
const launch = async (t, stored = [null, null]) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
const context = await browser.newContext({ viewport: { width, height: 800 }, reducedMotion: "reduce" });
await context.addInitScript((v) => { try { if (!localStorage.getItem("seeded")) { if (v[0] !== null) localStorage.setItem("magpie.maskEmails", v[0]); if (v[1] !== null) localStorage.setItem("magpie.maskProviders", v[1]); localStorage.setItem("seeded", "1"); } } catch {} }, stored);
const errors = [];
const open = async (url) => {
const page = await context.newPage();
page.setDefaultTimeout(5000);
page.setDefaultNavigationTimeout(60000);
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", serve(lang, url.includes("mode=panel")));
await page.goto(url);
return page;
};
return { open, errors };
};
const provText = (page) => page.evaluate(() => document.querySelector("#providers").innerText);
const listed = (page) => page.waitForFunction(() => document.querySelector("#providers")?.innerText.includes("Codex"));
// every page's button says the one state, in its words and its tooltip
const says = async (page, on, where) => {
for (const [view, sel] of Object.entries(BTNS)) {
const b = page.locator(sel);
if (!(await b.count())) continue;
assert.equal(await b.getAttribute("aria-pressed"), String(on), `${view}'s button ${where}`);
assert.equal(await b.locator("[data-t]").textContent(), await page.evaluate((k) => t(k), KEYS[on ? 1 : 0]), `${view}'s words ${where}`);
assert.equal(await b.getAttribute("title"), await page.evaluate((k) => t(k), KEYS[on ? 3 : 2]), `${view}'s tooltip ${where}`);
}
};
test(`${engine} ${lang} ${width}: Hide accounts is one switch on every page`, async (t) => {
const { open, errors } = await launch(t);
const page = await open("http://magpie.test/?view=routing");
await page.locator("#rtMask").waitFor();
await page.evaluate((m) => { const d = document.createElement("div"); d.id = "sample"; d.textContent = "Codex as " + m; document.querySelector("#view-routing").append(d); }, MAIL);
// a second window on the same storage, on Providers, and the panel
const other = await open("http://magpie.test/?view=providers");
await listed(other);
const panel = await open("http://magpie.test/?mode=panel");
await panel.evaluate(() => setPanelTab("usage"));
await panel.waitForFunction((m) => document.querySelector("#panelQuota")?.innerText.includes(m), MAIL);
// turned on Routing: Routing hidden, every button on
await page.locator("#rtMask").click();
await page.waitForFunction(() => document.querySelector("#sample .pii"));
await says(page, true, "after Routing's was turned on");
// and Providers hidden too, its own button saying so
await page.evaluate(() => show("providers"));
await listed(page);
await page.waitForFunction(() => document.querySelector("#view-providers .pii"));
assert.ok(!(await provText(page)).includes(MAIL), "Providers shows the account while Routing's switch is on");
await says(page, true, "on Providers");
// and Usage
await page.evaluate(() => show("usage"));
await page.locator("#usageMask").waitFor();
await page.waitForFunction((m) => { const u = document.querySelector("#view-usage .subscription-account .user"); return u && u.querySelector(".pii") && !u.textContent.includes(m); }, MAIL);
await says(page, true, "on Usage");
// live in the other window and the panel
await other.waitForFunction(() => document.querySelector("#view-providers .pii"));
assert.ok(!(await provText(other)).includes(MAIL));
await says(other, true, "in another window");
await panel.waitForFunction((m) => !document.querySelector("#panelQuota").innerText.includes(m), MAIL);
// turned off from Providers, in the other window: every page back
await other.locator("#pvMask").click();
await other.waitForFunction(() => !document.querySelector(".pii"));
assert.ok((await provText(other)).includes(MAIL));
await says(other, false, "after Providers' was turned off");
await page.waitForFunction(() => !document.querySelector(".pii"));
await says(page, false, "in the first window, after the other turned it off");
await page.evaluate(() => show("routing"));
assert.equal(await page.locator("#sample").textContent(), "Codex as " + MAIL);
await panel.waitForFunction((m) => document.querySelector("#panelQuota").innerText.includes(m), MAIL);
// turned on from Usage: Providers hidden again, after a reload too
await page.evaluate(() => show("usage"));
await page.locator("#usageMask").click();
await page.waitForFunction(() => document.querySelector("#view-usage .pii"));
await page.reload();
await page.evaluate(() => show("providers"));
await listed(page);
await page.waitForFunction(() => document.querySelector("#view-providers .pii"));
await says(page, true, "after a reload");
assert.equal(await page.evaluate(() => localStorage.getItem("magpie.maskEmails")), "1");
assert.equal(await page.evaluate(() => localStorage.getItem("magpie.maskProviders")), null);
// placed as Routing's: in the page's head row, at its right end
const box = await page.locator("#pvMask").boundingBox(), q = await page.locator("#pvQ").boundingBox();
assert.ok(Math.abs((box.y + box.height / 2) - (q.y + q.height / 2)) < 4, `beside the search box: ${JSON.stringify([box, q])}`);
assert.ok(box.x >= q.x + q.width - 1, `at the right end: ${JSON.stringify([box, q])}`);
assert.ok(box.x + box.width <= width, "inside the window");
assert.equal(await page.locator("select").count(), 0, "no native select");
assert.equal(await page.evaluate(() => document.documentElement.scrollWidth - document.documentElement.clientWidth), 0, `no sideways scroll at ${width}px`);
const missing = await page.evaluate(([keys, lang]) => lang === "en" ? [] : keys.filter((k) => !I18N[lang]?.[k]), [KEYS, lang]);
assert.deepEqual(missing, [], `every string has its ${lang}`);
assert.deepEqual(errors, []);
});
// what an earlier version stored for Routing (maskEmails) and for
// Providers (maskProviders): hidden on either is hidden everywhere
for (const [stored, on] of [[[null, "1"], true], [["0", "1"], true], [["1", "0"], true], [["1", null], true], [["0", "0"], false], [["0", null], false]]) {
test(`${engine} ${lang} ${width}: stored ${JSON.stringify(stored)} before the update is ${on ? "hidden" : "shown"} on every page`, async (t) => {
const { open, errors } = await launch(t, stored);
const page = await open("http://magpie.test/?view=providers");
await listed(page);
await page.locator("#pvMask").waitFor();
if (on) await page.waitForFunction(() => document.querySelector("#view-providers .pii"));
assert.equal(await page.locator("#view-providers .pii").count() > 0, on);
assert.equal((await provText(page)).includes(MAIL), !on);
await says(page, on, "on Providers");
await page.evaluate(() => show("routing"));
await says(page, on, "on Routing");
assert.equal(await page.evaluate(() => localStorage.getItem("magpie.maskEmails")), on ? "1" : "0");
assert.equal(await page.evaluate(() => localStorage.getItem("magpie.maskProviders")), null);
await page.evaluate((v) => hideAccounts.follow(v), !on);
await says(page, on, "after Mask personal data changed: a default never turns the user's choice");
assert.deepEqual(errors, []);
});
}
}
}
}