Files
magpie/internal/gui/tests/redact-words-import.test.cjs
T
yetone eeecd41505 privacy: masked words and never-mask values are imported in bulk and copied out (acmilan_)
acmilan_ on Discord asked to import masking words in bulk instead of adding
them one at a time. Settings > Privacy's Masked words row now has Import...
and Copy beside Show. Import... opens a box under the list: what is pasted
there, a word a line or split at commas (either width), is added after the
list's words in one save. A word the list already has, or one pasted twice,
is passed over; the box counts the new ones as you type, and once saved the
status says "Words added: 3 · already in the list: 2". Cmd/Ctrl+Enter
imports, Escape and Cancel close it without saving; a redraw keeps the box
open with what it holds; a save that fails says no count. Copy puts the
list on the clipboard a word a line, the form Import takes. Never mask
uses the same wordList, so it gets both too. The pasted text is drawn as
dots while the words are hidden. Strings in zh, zh-TW, ja and de.

Test: internal/gui/tests/redact-words-import.test.cjs, Chromium and WebKit,
en/zh/zh-TW/ja/de at 400px: 10 of 10 ok on mote. Broken by hand: with the
dedupe against the list taken out it fails ("New words: 5", expected 3);
with the saved-words check before the count taken out, the failed save's
error is written over and the test times out waiting for it.
Looked at it in both engines at 400px (screenshots, en and zh).
Run (mote, at the pushed head of this commit and the two after it,
bae625cdf on origin/main 8a8079680):
- node --test redact-words-import, i18n-en-no-cjk, zcode-site,
  zcode-site-layout, plugin-moved-signin, plugin-other-ways, zhipu-team,
  gui-de, gui-ja, gui-zh-tw, i18n-keys, agent-leave-magpie,
  agent-own-direct, checkin-now-account, checkin-account, plugin-checkin,
  usage-checkin: 215 of 215 ok.
- go test ./internal/gui/...: ok but TestMainWindowCorners/macOS_26_SDK,
  red the same at origin/main 8a8079680 (#3949, macOS 27 on mote);
  TestCoreScriptBudget ok.
- The whole GUI suite was not run before the push: both test boxes were
  running other sessions' GUI suites (one suite a machine). It runs after
  the tag; reds are filed.
- Rebased on origin/main d02b8a000 (agent-own-direct, app.js, i18n.js
  changed there) and run again at the pushed tree: TestCoreScriptBudget,
  i18n-en-no-cjk, i18n-keys, gui-de, gui-ja, gui-zh-tw and
  agent-own-direct, 37 of 37 ok.
2026-11-04 22:19:21 +08:00

187 lines
11 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.
// Settings › Privacy's Masked words and Never mask take many words at once
// (acmilan_ on Discord: adding them one by one was slow). Import… opens a
// box under the list; what is pasted there, a word a line or split at
// commas, is added after the words the list has, a word already in it or
// pasted twice passed over, and the status says how many were added. A
// redraw keeps the box and what it holds; Escape and Cancel close it and
// save nothing; a save that fails says no count. Copy puts the list on the
// clipboard a word a line. Every language, Chromium and WebKit, narrow.
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 { reach, watchSettingsReads, settingsDrawn } = require("./reader.cjs");
const assets = path.resolve(__dirname, "../assets");
function serve(lang, box) {
return async (r) => {
const req = r.request(), url = new URL(req.url());
const json = (data) => r.fulfill({ json: data });
if (url.pathname === "/boot.js") return r.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:true};` });
if (url.pathname === "/wails/runtime.js") return r.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
if (url.pathname === "/api/state") return json({ agents: [], profiles: [], settings: box.settings });
if (url.pathname === "/api/settings") {
if (req.method() === "POST") {
if (box.fail) return r.fulfill({ status: 500, contentType: "text/plain", body: "disk full" });
box.settings = JSON.parse(req.postData());
box.saved.push(box.settings.redactWords);
}
return json(box.settings);
}
if (url.pathname === "/api/settings/redact-allow") {
const b = req.postDataJSON();
box.allowed.push(b.allow);
box.settings = { ...box.settings, redactAllow: b.allow };
return json(box.settings);
}
if (url.pathname === "/api/copy") {
box.copied.push(req.postDataJSON().text);
return json({});
}
if (url.pathname === "/api/groups") return json({ models: [], groups: [], pools: [] });
if (url.pathname === "/api/providers") return json({ providers: [], presets: [], excluded: [], gateway: { running: true, window: true, url: "http://127.0.0.1:3999" } });
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 r.fulfill({ body: await fs.readFile(file), contentType }); } catch { await r.fulfill({ status: 404, body: "" }); }
};
}
const ROW = (key) => `.row:has(> .word-list[data-list="${key}"])`;
const LIST = (key) => `.word-list[data-list="${key}"]`;
const words = (page, key) => page.evaluate((sel) => [...document.querySelectorAll(`${sel} input[data-word]`)].map((i) => i.dataset.word), LIST(key));
const drawn = (page, key, want) => page.waitForFunction(([sel, want]) =>
JSON.stringify([...document.querySelectorAll(`${sel} input[data-word]`)].map((i) => i.dataset.word)) === want, [LIST(key), JSON.stringify(want)], { timeout: 5000 });
const until = async (ok, what) => {
for (let n = 0; n < 100 && !ok(); n++) await new Promise((r) => setTimeout(r, 50));
assert(ok(), what);
};
// the window's own word for it
const said = (page, s, vars) => page.evaluate(([s, vars]) => t(s, vars), [s, vars]);
// nothing in the list's row past its section's edge, and no sideways scroll
const fits = (page, key) => page.evaluate((sel) => {
const l = document.querySelector(sel).parentElement.getBoundingClientRect();
const over = [...document.querySelectorAll(`${sel} input, ${sel} button, ${sel} textarea, ${sel} .hint`)]
.filter((n) => n.getClientRects().length).filter((n) => { const b = n.getBoundingClientRect(); return b.left < l.left - 1 || b.right > l.right + 1; })
.map((n) => n.className);
return { over, page: document.documentElement.scrollWidth > document.documentElement.clientWidth };
}, ROW(key));
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of ["en", "zh", "zh-TW", "ja", "de"]) {
test(`${engine} ${lang} 400px: masked words and never-mask values are imported in bulk and copied out`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
const page = await (await browser.newContext({ viewport: { width: 400, height: 1400 }, reducedMotion: "reduce" })).newPage();
page.setDefaultTimeout(5000);
page.setDefaultNavigationTimeout(60000);
const errors = [];
const box = { saved: [], allowed: [], copied: [], fail: false,
settings: { lang, theme: "light", redact: true, redactPersonal: false, redactWords: ["Nightjar", "百度"], redactAllow: ["ops@company.com"] } };
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", serve(lang, box));
await watchSettingsReads(page);
await page.goto("http://magpie.test/?view=settings&tab=privacy");
await page.locator(LIST("words")).waitFor();
await settingsDrawn(page);
// closed until asked for, and nothing native
const area = page.locator(`${LIST("words")} textarea.word-import-in`);
assert.equal(await area.isVisible(), false, "the import box is closed at first");
assert.equal(await page.locator(`${ROW("words")} select`).count(), 0);
const open = page.locator(`${ROW("words")} .word-import-open`);
assert.equal((await open.textContent()).trim(), await said(page, "Import…"));
await reach(page, open);
await open.click();
await page.waitForFunction((sel) => document.activeElement?.matches(`${sel} textarea.word-import-in`), LIST("words"));
assert.deepEqual(await fits(page, "words"), { over: [], page: false }, "the open box fits a narrow window");
// pasted: lines, commas (either width), blanks, a word twice and two
// the list has; the count is of the new ones
await area.fill("Nightjar\nKestrel, Osprey,百度\n\n Kestrel \nops.example.com\n");
const count = page.locator(`${LIST("words")} .word-import-count`);
assert.equal(await count.textContent(), await said(page, "New words: {n}", { n: 3 }));
if (lang === "en") assert.equal(await count.textContent(), "New words: 3");
// a redraw keeps the box open with what it holds
await page.evaluate(() => renderSettings());
await page.locator(`${LIST("words")} textarea.word-import-in`).waitFor({ state: "visible" });
assert.equal(await area.inputValue(), "Nightjar\nKestrel, Osprey,百度\n\n Kestrel \nops.example.com\n");
assert.equal(box.saved.length, 0, "typing in the box saves nothing");
const go = page.locator(`${LIST("words")} .word-import-bar button.primary`);
await reach(page, go);
await go.click();
const five = ["Nightjar", "百度", "Kestrel", "Osprey", "ops.example.com"];
await until(() => JSON.stringify(box.saved.at(-1)) === JSON.stringify(five), `saved ${JSON.stringify(box.saved)}`);
assert.equal(box.saved.length, 1, "one save for the whole import");
await drawn(page, "words", five);
const done = await said(page, "Words added: {n} · already in the list: {m}", { n: 3, m: 2 });
await page.waitForFunction((want) => document.querySelector("#status").textContent === want, done);
if (lang === "en") assert.equal(done, "Words added: 3 · already in the list: 2");
assert.equal(await area.isVisible(), false, "the box closes once imported");
// only words the list has: nothing to import; Escape closes, saves nothing
await open.click();
await area.fill("kestrel\nKestrel");
assert.equal(await count.textContent(), await said(page, "New words: {n}", { n: 1 }), "a word in another case is a word of its own");
await area.fill("Kestrel, Osprey");
assert.equal(await count.textContent(), await said(page, "No new words: each is in the list already"));
assert.equal(await go.isDisabled(), true);
await area.press("Escape");
assert.equal(await area.isVisible(), false);
// and Cancel closes it too, emptied for next time
await open.click();
await area.fill("Plover");
await page.locator(`${LIST("words")} .word-import-bar button:not(.primary)`).click();
assert.equal(await area.isVisible(), false);
await open.click();
assert.equal(await area.inputValue(), "");
await area.press("Escape");
assert.equal(box.saved.length, 1, "closing saves nothing");
// a save that fails says no count
box.fail = true;
await open.click();
await area.fill("Plover");
await area.press(process.platform === "darwin" ? "Meta+Enter" : "Control+Enter");
await page.waitForFunction(() => document.querySelector("#status").classList.contains("err"));
assert.notEqual(await page.locator("#status").textContent(), await said(page, "Words added: {n}", { n: 1 }));
box.fail = false;
await drawn(page, "words", five);
// Copy: the list a word a line, as Import takes it
const copy = page.locator(`${ROW("words")} .word-copy`);
await reach(page, copy);
await copy.click();
await until(() => box.copied.length === 1, "copied once");
assert.equal(box.copied[0], five.join("\n"));
await page.waitForFunction((want) => document.querySelector("#status").textContent === want, await said(page, "Words copied: {n}", { n: 5 }));
// Never mask takes an import the same way
const allowOpen = page.locator(`${ROW("allow")} .word-import-open`);
await reach(page, allowOpen);
await allowOpen.click();
const allowArea = page.locator(`${LIST("allow")} textarea.word-import-in`);
await allowArea.fill("ops@company.com, build-01\nbuild-02");
const allowGo = page.locator(`${LIST("allow")} .word-import-bar button.primary`);
await reach(page, allowGo);
await allowGo.click();
await until(() => box.allowed.length === 1, "never-mask saved once");
assert.deepEqual(box.allowed[0], ["ops@company.com", "build-01", "build-02"]);
await drawn(page, "allow", ["ops@company.com", "build-01", "build-02"]);
assert.deepEqual(await fits(page, "allow"), { over: [], page: false });
// what was saved is drawn again on the next load
await page.reload();
await page.locator(LIST("words")).waitFor();
await settingsDrawn(page);
assert.deepEqual(await words(page, "words"), five);
assert.deepEqual(errors, []);
});
}
}