Files
magpie/internal/gui/tests/mask-alert.test.cjs
T
yetone 847f0c4703 gui: Masking alert lives on the Masking page
nb5p on Discord (#feedback): "这个脱敏提醒被放到额度设置里了,害得我找半天没找到".
The Masking alert row sat in Settings › Usage. It now closes Insights ›
Masking, after the list of requests, in the page's own `.list.prefs` rows
(same Off/On segs, same ids #maskAlertRow/#maskAlertSegs, same i18n keys
and the same maskAlert setting). Nothing is left in Usage. The page draws
it from page.js after loadSettings, hides it while one request is open,
and gateway mode leaves it out as Settings did. Placed after the list so
the requests still start in the first screen (masking-records at 640px)
and the demo keeps its place between the card and the list.

Run at base 07a3312c5 with this change:
- mask-alert.test.cjs, Chromium (WSL magpie-desktop and mote) and WebKit
  (WSL), en/zh/zh-TW/ja/de at 900 and 380px: all pass.
- WebKit (WSL): mask-alert, masking-records, masking-demo,
  masking-demo-unblurred: 87 pass, 0 fail (tree differed from the pushed
  one by a client.js comment reflow only). Earlier WebKit run: turn-done,
  quota-reads, routing-masking, gateway-mode, builtins, sidebar pass.
- Chromium (WSL) at the final tree: i18n-keys, mask-alert, masking-records,
  masking-demo, masking-demo-unblurred, turn-done, quota-reads,
  routing-masking, gateway-mode, builtins, sidebar: 131 pass, 1 fail. The
  fail is i18n-keys "a plugin's words are its own" on "Unmarked"
  (plugins/routing/i18n.js), red on main, #4524.
- mote: go test -timeout 45m ./internal/gui/...: TestCoreScriptBudget and
  the budgets pass (app.js only shrinks). Red: TestSettingsSaveKeepsWhat-
  ItDoesNotSend (SearchAll, red on origin/main 93b31f5b7 too, #4514),
  TestColdTurnsTaken (passes alone 3 of 3, #4510), and the AppKit window
  tests killed at their 30s bar on mote over ssh (no window session).

Broken by hand: with internal/gui/assets back as on 07a3312c5 (row in
Settings › Usage, none on the page), mask-alert.test.cjs went 11 of 12
red in Chromium; the feature-off test is the one that still passes.

Not tried: the real app (desktop window) and WebKit on mote, whose
Playwright WebKit launch hangs there.
2026-10-31 14:05:02 +08:00

150 lines
8.9 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 masking alert (Must2333 on X: 我还希望能即时的知道发生了替换,但不是
// 通过随时监控网关的方式): Insights › Masking has "Masking alert" after its
// list of requests, and Settings › Usage no longer does (nb5p on Discord: 这个脱敏提醒被
// 放到额度设置里了,害得我找半天没找到). Off to begin with; On saves
// maskAlert, another setting saved later keeps it, Off saves false, and no
// click moves the page. With masking switched off in Plugins › Built-in the
// row goes with it. Every language, wide and narrow, both engines; 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 { watchSettingsReads, settingsDrawn, reach } = require("./reader.cjs");
const assets = path.resolve(__dirname, "../assets");
function server(lang, posts, featuresOff) {
const fixed = {
theme: "light", lang, tray: "panel", quotaLeft: false, currency: "usd", dock: false, dockWindow: false, proxy: "",
redact: true, redactPersonal: false, redactWords: [], codexWarmup: "", claudeWarmup: "", codexWarmAt: "", claudeWarmAt: "",
trayUsage: "", trayUsageEvery: 3, vision: "", imageGen: "", version: "0.1.900", dir: "~/.config/magpie",
gateway: "http://127.0.0.1:3425", proxyNow: "none", proxySource: "none", visionModels: [], imageGenModels: [], lanURLs: [],
fx: { rate: 7.2, at: new Date().toISOString(), stale: false }, turnDoneAlert: 0, featuresOff,
};
let cur = fixed;
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:"light",web:false,featuresOff:${JSON.stringify(featuresOff)}};` });
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: fixed.fx });
if (url.pathname === "/api/settings") {
if (req.method() === "POST") {
const body = req.postDataJSON();
posts.push(body);
cur = { ...fixed, ...body };
}
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)];
await route.fulfill({ body: await fs.readFile(file), contentType });
};
}
const words = {
en: { name: "Masking alert", off: "Off", on: "On",
sub: "A notification when masking swaps a value for the first time: its kind and how many, never the value itself" },
zh: { name: "脱敏提醒", off: "关闭", on: "开启", sub: "首次脱敏某个值时通知:只说类型和个数,从不显示值本身" },
"zh-TW": { name: "脫敏提醒", off: "關閉", on: "開啟", sub: "首次脫敏某個值時通知:只說類型和個數,從不顯示值本身" },
ja: { name: "マスク通知", off: "オフ", on: "オン", sub: "値を初めてマスクしたときに通知:種類と件数だけで、値そのものは表示しません" },
de: { name: "Benachrichtigung beim Maskieren", off: "Aus", on: "An",
sub: "Eine Benachrichtigung, wenn ein Wert zum ersten Mal maskiert wird: Art und Anzahl, nie der Wert selbst" },
};
async function open(engine, width, lang, posts, featuresOff, t, name, where = "?view=masking") {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
const page = await (await browser.newContext({ viewport: { width, height: 700 }, reducedMotion: "reduce" })).newPage();
t.after(async () => {
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}-${width}-${name}.png`) });
}
await browser.close();
});
page.setDefaultTimeout(5000);
page.setDefaultNavigationTimeout(60000);
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", server(lang, posts, featuresOff));
await watchSettingsReads(page);
await page.goto("http://magpie.test/" + where);
if (where.includes("settings")) await settingsDrawn(page); // rows found are the ones that stay (#1659)
return { page, errors };
}
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of Object.keys(words)) {
for (const width of [900, 380]) {
test(`${engine} ${lang} ${width}px: the masking alert is turned on and off on the Masking page`, async (t) => {
const posts = [];
const { page, errors } = await open(engine, width, lang, posts, [], t, "mask-alert");
const posted = async (n) => { for (let i = 0; i < 100 && posts.length < n; i++) await page.waitForTimeout(20); assert.equal(posts.length, n, "posts"); return posts[n - 1]; };
const w = words[lang];
const opts = page.locator("#maskAlertSegs .opt");
await opts.first().waitFor();
assert.equal((await page.locator("#maskAlertRow .name").textContent()).trim(), w.name);
assert.equal((await page.locator("#maskAlertRow .sub").textContent()).trim(), w.sub);
assert.deepEqual(await opts.allTextContents(), [w.off, w.on]);
assert.equal(await page.locator("#maskAlertSegs .opt.on").textContent(), w.off, "off until turned on");
// it is the Masking page's, after its list of requests
assert.equal(await page.locator("#view-masking #maskList + #maskPrefs > #maskAlertRow").count(), 1);
assert.equal(await page.locator("#view-settings #maskAlertRow").count(), 0, "no copy in Settings");
const over = await page.evaluate(() => {
const row = document.querySelector("#maskAlertRow").getBoundingClientRect();
return [...document.querySelectorAll("#maskAlertRow *")].filter((e) => e.getBoundingClientRect().right > Math.max(row.right, innerWidth) + 1).map((e) => e.className || e.id);
});
assert.deepEqual(over, [], "nothing past the edge");
const scrolled = () => page.locator("#view-masking").evaluate((v) => v.scrollTop);
await reach(page, opts.nth(1));
let top = await scrolled();
await opts.nth(1).click();
assert.equal((await posted(1)).maskAlert, true);
await page.waitForTimeout(300);
assert.equal(await page.locator("#maskAlertSegs .opt.on").textContent(), w.on);
assert.equal(await scrolled(), top, "the click moved the page");
// another setting saved later keeps it: the card's Turn off masking
const offBtn = page.locator("#maskCard .msk-head button.text");
await reach(page, offBtn);
top = await scrolled();
await offBtn.click();
const second = await posted(2);
assert.equal(second.redact, false, "the card's switch saved");
assert.equal(second.maskAlert, true, "kept by another save");
await page.waitForTimeout(300);
assert.equal(await scrolled(), top, "no click moved the page");
await reach(page, opts.nth(0));
await opts.nth(0).click();
assert.equal((await posted(3)).maskAlert, false, "Off saves false");
assert.equal(await page.locator("#maskAlertRow").evaluate((e) => getComputedStyle(e).borderLeftWidth), "0px");
assert.deepEqual(errors, []);
});
}
}
test(`${engine}: Settings › Usage has no masking alert`, async (t) => {
const { page, errors } = await open(engine, 900, "en", [], [], t, "mask-alert-usage", "?view=settings&tab=usage");
await page.locator("#turnDoneSegs .opt").first().waitFor();
assert.equal(await page.locator("#view-settings #maskAlertRow").count(), 0);
assert.equal(await page.locator("#turnDoneRow").isVisible(), true);
assert.deepEqual(errors, []);
});
test(`${engine}: with masking switched off, its alert goes too`, async (t) => {
const { page, errors } = await open(engine, 900, "en", [], ["redact"], t, "mask-alert-off", "?view=settings&tab=usage");
await page.locator("#turnDoneSegs .opt").first().waitFor();
assert.equal(await page.locator("#maskAlertRow").isVisible(), false);
assert.equal(await page.locator("#turnDoneRow").isVisible(), true);
assert.deepEqual(errors, []);
});
}