Files
magpie/internal/gui/tests/library-mcp-on.test.cjs
T
yetone d7cda856ad library: MCP servers turned off fold away below the ones on (hzlxb on X)
hzlxb on X asked for Library -> MCP servers to keep the servers turned off
apart, folded, as Providers keeps the providers turned off. The list now
holds the servers on. The ones off sit below it, above "+ Add server",
under a "Servers turned off (N)" fold drawn with Providers' Turned off
markup (row-head off-head, .fold with the chevron, the count in .note).
It is folded at first and remembered in localStorage
magpie.offServersOpen, as magpie.offProvidersOpen is. A server switched
off moves under the fold, and one switched on comes back up. While
picking (Select) the fold is held open and disabled, so every server
picked is in sight, as Providers holds its fold open while searching.
The MCP tab has no filter of its own, so there is nothing else for it to
hide from. With every server off only the fold shows. With none off
there is no fold. Strings are in en, zh, zh-TW, ja and de.

Run:
- library-mcp-off-fold.test.cjs (new): Chromium and WebKit, 1100 and
  420 wide, en/zh/zh-TW/ja/de. It checks order, label, count, title,
  aria-expanded, that opening by a wheel-scrolled click leaves the head
  where it is and opens no editor, the reload memory, moving on a
  switch, picking, all off and none off. It is red on the old library
  files (no fold; checked on mica in chromium).
- Library/MCP/i18n/click-scroll/providers-off-fold GUI files (102 files,
  both engines) on mote at the pre-rebase head 29a5f10f9: 2304 of 2310 pass. The 6
  reds are library-open-again's 1100px cases, red the same alone on the
  old tree (filed as #6751).
- go test -count=1 ./internal/gui/ on mica at this head: ok (143s).
  gofmt clean.
- The whole GUI suite on mote at 29a5f10f9, in two parts (mote rebooted
  mid-run; the rest ran after it came back, under suite.lock). Part 1:
  411 not ok in 75 files. Part 2 (640 files): 11355 tests, 10911 pass,
  444 fail. No red is in the files this change touches except
  library-open-again (#6751). Every red file has an open issue saying it
  is red on main: skill-fold-all/skill-manual (#6667, #6668, the
  window's /api/update/using), skill-group-shared (#6687),
  view-open-wheel (#4723), view-trackpad-coast (#4724),
  view-wheel-unheard (#5158), usage-* (#6743, #4263), the rest in #6709
  or by name; icon-tile was unfiled and is now #6759 (dim-color from
  2649d5d72, not in app.css's tile rule).
- After rebasing on origin/main 923cc8747 (1f04ed219 touched
  library.css): Library/i18n/click-scroll/providers-off/gui-* files on
  mote: 1837 of 1847 pass. Reds: library-open-again's 1100px
  cases (#6751), library-env de (#6675, NO_PROXYlocalhost) and
  library-chip-fan de no-preference (#3072), both chromium only; those
  two pass 3 of 3 alone at this head and at origin/main 923cc8747; go test -count=1 ./internal/gui/ on mica: ok (154s).

Not tried: the real app, including WebKitGTK on Linux. The fold has no
motion of its own (it uses Providers' chevron rotation).
2026-11-15 21:22:45 +08:00

150 lines
9.1 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.
// swang20201 on X (v0.1.3375, Docker gateway mode): an installed MCP server
// in Library could only be removed. Each server row has a switch: off
// posts servers/on {on:false}, says the settings are kept, and the row
// shows dimmed with its agent chips out of reach, under the fold of the
// ones turned off; on posts {on:true} and
// the row is as it was. The switch doesn't open the editor. In the app
// with an agent at 1100px and a browser in gateway mode at 420px, every
// language, both engines: no sideways scroll, no native select.
// 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 { strings } = require("./strings.cjs");
const assets = path.resolve(__dirname, "../assets");
const HOME = "/srv/mg";
const fixture = (st) => ({
dir: `${HOME}/.magpie/library`, backups: `${HOME}/.magpie/backups`, home: HOME,
mcpHub: st.gateway, hubPath: "/mcp/magpie/hub/",
agents: [{ id: "claude", name: "Claude Code", icon: "claudecode-color", skills: `${HOME}/.claude/skills`, mcp: `${HOME}/.claude.json`, ...(st.gateway ? { hub: "on" } : {}) }],
instructions: { agents: [], sets: [] }, foundSkills: [], skills: [], problems: [], foundServers: [], projects: [],
servers: [
st.off
? { name: "notes", transport: "stdio", command: "uvx", args: ["notes-mcp"], env: { TOKEN: "x" }, agents: [], hidden: true, paused: ["claude"] }
: { name: "notes", transport: "stdio", command: "uvx", args: ["notes-mcp"], env: { TOKEN: "x" }, agents: ["claude"] },
{ name: "docs", transport: "http", url: "https://docs.example/mcp", agents: ["claude"] },
],
});
let I18N;
function tr(lang, key, vars = {}) {
let s = key;
if (lang !== "en") {
assert.equal(typeof I18N[lang][key], "string", `${lang} has no ${JSON.stringify(key)}`);
s = I18N[lang][key];
}
return s.replace(/\{(\w+)\}/g, (m, k) => (k in vars ? String(vars[k]) : m));
}
function server(lang, st) {
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:${st.gateway}${st.gateway ? ",gateway: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", gateway: "http://127.0.0.1:3425" } });
if (url.pathname === "/api/library") return json(fixture(st));
if (url.pathname === "/api/library/servers/on") {
const body = req.postDataJSON();
st.sent.push(body);
st.off = !body.on;
return json({ ...fixture(st), result: { changed: [] } });
}
if (url.pathname === "/api/library/mcp/check") {
const names = req.postDataJSON().names;
st.checked.push(...names);
return json({ servers: Object.fromEntries(names.map((n) => [n, { state: "ok", tools: 2 }])) });
}
if (url.pathname === "/api/plugins") return json({ plugins: [] });
if (url.pathname === "/api/usage/quotas") return json([]);
if (url.pathname === "/api/groups") return json({ groups: [] });
if (url.pathname === "/api/providers") return json({ providers: [], gateway: { running: true } });
if (url.pathname.startsWith("/api/")) return json({});
if (url.host !== "magpie.test") return route.fulfill({ status: 404, body: "" });
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 sideways = (page) => page.evaluate(() => {
const over = [];
const w = document.documentElement.clientWidth;
if (document.documentElement.scrollWidth > w) over.push("page " + document.documentElement.scrollWidth);
for (const e of document.querySelectorAll("#view-library .lib-server, #view-library .lib-server > .lib-switch, #view-library .lib-server .lib-agents")) {
const r = e.getBoundingClientRect();
if (r.width && (r.right > w + 0.5 || r.left < -0.5)) over.push((e.className || e.tagName) + " " + Math.round(r.left) + "–" + Math.round(r.right));
}
return over;
});
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
test(engine + ": an MCP server switched off and on", async (t) => {
assert(["chromium", "webkit"].includes(engine), "BROWSER must be chromium or webkit");
I18N = strings().I18N;
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: process.env.PLAYWRIGHT_CHANNEL || "chromium" }));
t.after(() => browser.close());
for (const width of [1100, 420]) for (const lang of ["en", "zh", "zh-TW", "ja", "de"]) {
await t.test(`${lang} ${width}px`, async () => {
const w = (key, vars) => tr(lang, key, vars);
const gateway = width < 800;
const where = gateway ? "gateway mode" : "the app";
const st = { off: false, sent: [], checked: [], gateway };
const errors = [];
const ctx = await browser.newContext({ viewport: { width, height: 900 }, reducedMotion: "reduce" });
await ctx.addInitScript(() => { try { localStorage.setItem("magpie.libTab", "mcp"); } catch {} });
const page = await ctx.newPage();
page.setDefaultTimeout(5000);
page.setDefaultNavigationTimeout(60000);
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", server(lang, st));
await page.goto("http://magpie.test/");
await page.locator('button[data-view="library"]').click();
const v = page.locator("#view-library");
const row = v.locator('.lib-server[data-server="notes"]');
await row.waitFor();
const sw = row.locator(":scope > .lib-switch");
assert.equal(await sw.count(), 1, `${where}: no switch on the row`);
assert.equal(await sw.getAttribute("aria-checked"), "true");
assert.equal(await sw.getAttribute("aria-label"), w("Turn {name} on or off", { name: "notes" }));
assert.equal(await row.evaluate((r) => r.classList.contains("off")), false);
assert.equal(await v.locator('.lib-server[data-server="docs"] > .lib-switch').getAttribute("aria-checked"), "true");
assert.equal(await page.locator("select").count(), 0);
assert.deepEqual(await sideways(page), [], `${where}: sideways scroll, on`);
// off: kept, dimmed, the editor not opened
await sw.click();
await page.waitForFunction((x) => document.querySelector("#status")?.textContent === x, w("{name} is off; its settings are kept", { name: "notes" }));
assert.deepEqual(st.sent, [{ name: "notes", on: false }]);
await page.waitForFunction(() => document.querySelector('#view-library .lib-server[data-server="notes"]')?.classList.contains("off"));
// turned off, it is under the fold below the ones on, folded at first
// (library-mcp-off-fold): opened to see it
assert.equal(await v.locator(".lib-offhead .fold").getAttribute("aria-expanded"), "false");
await v.locator(".lib-offhead .fold").click();
const offRow = v.locator('.lib-server[data-server="notes"]');
assert.equal(await offRow.locator(":scope > .lib-switch").getAttribute("aria-checked"), "false");
assert.equal(await page.locator("#modal").evaluate((m) => !m.hidden && m.getBoundingClientRect().height > 0).catch(() => false), false, `${where}: the switch opened the editor`);
if (!gateway) assert.equal(await offRow.locator(".lib-agents").evaluate((e) => getComputedStyle(e).pointerEvents), "none", "the chips of a server off still take clicks");
assert.ok(Number(await offRow.locator(".name").evaluate((e) => getComputedStyle(e).opacity)) < 1, `${where}: not dimmed`);
assert.equal(await v.locator('.lib-server[data-server="docs"]').evaluate((r) => r.classList.contains("off")), false);
assert.deepEqual(await sideways(page), [], `${where}: sideways scroll, off`);
// on again, as it was
await offRow.locator(":scope > .lib-switch").click();
await page.waitForFunction((x) => document.querySelector("#status")?.textContent === x, w("{name} is on again", { name: "notes" }));
assert.deepEqual(st.sent, [{ name: "notes", on: false }, { name: "notes", on: true }]);
await page.waitForFunction(() => !document.querySelector('#view-library .lib-server[data-server="notes"]')?.classList.contains("off"));
assert.equal(await v.locator('.lib-server[data-server="notes"] > .lib-switch').getAttribute("aria-checked"), "true");
assert.deepEqual(errors, []);
await ctx.close();
});
}
});
}