Files
magpie/internal/gui/tests/gateway-key-admin.test.cjs
T
yetone 4e24994a6e gateway: a key let use Magpie Memory reaches it from another computer
Niapyaya on X reaches their own MCP servers through the MCP hub from a
remote machine, but not magpie-memory: memory answered the agents on
this computer alone, on /mcp/magpie/memory (547585cd9) and in the hub
(HereOnly), whatever key came. That was on purpose: a friend's key
could read every memory or plant a global rule the digest writes into
every agent's instructions here.

So it stays off for every key, and the user can let one key of their
own use it:

- access.Key.Memory, set by "memory-key" (`magpie gateway-key memory
  <id> on|off`, or the key's ... > Advanced > Magpie Memory). As Admin,
  only a key the user made can have it (memoryOf): never the sharing
  key, a read-only or Browser key, nor an issued or expiring one.
  Read-only takes it away; the management API can't set it.
- memoryMCP lets a caller from elsewhere in only while magpie is
  shared, by such a key, and never from a page by hubFromPage (Origin,
  Sec-Fetch-Site, a Mozilla/ agent; Node's fetch sends Sec-Fetch-Mode,
  #5522). This computer's check is unchanged.
- The hub gives magpie-memory (HubServer.Memory, from isMemory) to an
  Away visitor only by such a key (HubVisitor.Memory), listed and run,
  in the gateway and again in the library's Call.
- Advanced in the key's menu now opens Management API and Magpie
  Memory, each its own pick; a key with it says so in its row. Strings
  in zh, zh-TW, ja, de. Docs: memory.md, mcp-router.md, and the site's
  remote page (en, zh, ja) with its index rebuilt.

Image already answers a key directly (/mcp/magpie/image, held to the
key); sessions stays this computer's. There is no skills MCP server.

Tests: TestMemoryMCPToAKeyGivenIt, TestHubGivesMemoryToAKeyLetUseIt,
TestMemoryKey, the hubServerOf Memory column, and gateway-key-admin's
GUI file now goes through Advanced > Management API and covers Memory.

Run (mote unless said; base d01e0a465, rebased onto c4bae0724, which
only touches the Settings keep guard):
- Red on the old code (mica): TestMemoryMCPToAKeyGivenIt (403 to the
  key) and TestHubGivesMemoryToAKeyLetUseIt (hub listed [docs] only).
- go test -count=1 -timeout 45m ./internal/access ./internal/gateway
  ./internal/library ./internal/cli ./internal/gui: access, library ok.
  Reds, each the same at a clean origin/main c4bae0724 and filed:
  gateway TestOpenCodeGoOwnFreeModelGetsNothingOfOpenCodes (#7117) and
  the doneHold panic log (#6505); cli TestHealthcheck…/TestUpdate…
  WithoutMagpieAddr, TestUpdateFindsAMagpieThatWroteNoRecord (#6471,
  #6223); gui TestSettingsSaveKeepsWhatItDoesNotSend (fixed by
  c4bae0724).
- GUI, both engines, every language, at this head: 64 files (every
  caller-keys / gateway-plugin file and i18n-*): 1008 of 1024 pass;
  api-key-theme (#6444) and connect-address-kept (#6755) fail the same
  at clean origin/main c4bae0724.
- gofmt -l clean; go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./... ok; builds for all three OSes ok.
- Broken by hand: drop !hubFromPage -> the Sec-Fetch-Site case gets 200,
  red; drop the key's Memory check -> another key gets 200, red. Drop
  sharedWith -> still green: serveMCP's share guard refuses first, so
  memoryReaches' check is a second line, not the only one.

Not tried: a real remote agent against a real shared magpie (tests use
httptest with 192.0.2.7 as the remote address); the full suite runs
after the push.
2026-11-19 02:31:18 +08:00

143 lines
10 KiB
JavaScript

// A gateway key is given the management API (/v1/magpie/admin/, #6307,
// ljayic on X) from its row's menu, under Advanced: off, from this
// computer or from any. A key that can manage magpie says so in its row.
// Beside it, Advanced lets a key's agents on other computers read and
// save Magpie Memory (Niapyaya on X), and such a key says so in its row.
// The sharing key, a read-only key and Magpie Browser's can't hold
// either, so their menus don't offer them.
const assert = require("node:assert/strict");
const { test } = require("node:test");
const { chromium, webkit } = require("playwright");
const { fixture } = require("./fixtures/caller-keys.cjs");
const words = {
en: { more: "Key actions", adv: "Advanced", head: "Management API", off: "Off", local: "From this computer", any: "From any computer", tag: "Manages magpie", madeLocal: "Server can manage magpie from this computer", madeOff: "Server can't manage magpie", mem: "Magpie Memory", memTag: "Memory from any computer", memOn: "Server can read and save Magpie Memory from any computer", memOff: "Server uses Magpie Memory from this computer only" },
zh: { more: "密钥操作", adv: "高级", head: "管理 API", off: "关闭", local: "仅限本机", any: "任何电脑均可", tag: "可管理 magpie", madeLocal: "Server 现在可在本机管理 magpie", madeOff: "Server 不再能管理 magpie", mem: "Magpie Memory", memTag: "任何电脑可用记忆", memOn: "Server 现在可在任何电脑上读写 Magpie Memory", memOff: "Server 只在本机使用 Magpie Memory" },
"zh-TW": { more: "金鑰操作", adv: "進階", head: "管理 API", off: "關閉", local: "僅限本機", any: "任何電腦均可", tag: "可管理 magpie", madeLocal: "Server 現在可在本機管理 magpie", madeOff: "Server 不再能管理 magpie", mem: "Magpie Memory", memTag: "任何電腦可用記憶", memOn: "Server 現在可在任何電腦上讀寫 Magpie Memory", memOff: "Server 只在本機使用 Magpie Memory" },
ja: { more: "キーの操作", adv: "詳細設定", head: "管理 API", off: "オフ", local: "このコンピュータから", any: "どのコンピュータからでも", tag: "magpie を管理", madeLocal: "Server はこのコンピュータから magpie を管理できます", madeOff: "Server は magpie を管理できなくなりました", mem: "Magpie Memory", memTag: "どこからでもメモリー", memOn: "Server はどのコンピュータからでも Magpie Memory を読み書きできます", memOff: "Server はこのコンピュータでのみ Magpie Memory を使います" },
de: { more: "Schlüsselaktionen", adv: "Erweitert", head: "Verwaltungs-API", off: "Aus", local: "Von diesem Computer", any: "Von jedem Computer", tag: "Verwaltet magpie", madeLocal: "Server kann magpie von diesem Computer aus verwalten", madeOff: "Server kann magpie nicht mehr verwalten", mem: "Magpie Memory", memTag: "Memory von jedem Computer", memOn: "Server kann Magpie Memory von jedem Computer aus lesen und speichern", memOff: "Server nutzt Magpie Memory nur von diesem Computer aus" },
};
const keys = () => [
{ id: "ops", name: "Ops", masked: "sk-magpie-key-…000000", admin: "any", memory: true },
{ id: "server", name: "Server", masked: "sk-magpie-key-…222222" },
{ id: "laptop", name: "Laptop", masked: "sk-magpie-key-…111111", readOnly: true },
{ id: "magpie", name: "Magpie", masked: "sk-magpie-key-…444444", lan: true },
{ id: "ext", name: "Magpie Browser", masked: "sk-magpie-key-…555555", readOnly: true, browser: true },
];
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const width of [1100, 420]) {
for (const [lang, w] of Object.entries(words)) {
test(`${engine} ${width} ${lang}: a gateway key is given the management API and has it taken away`, 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, height: 700 }, reducedMotion: "reduce" })).newPage();
page.setDefaultTimeout(6000);
page.setDefaultNavigationTimeout(60000);
const events = [], errors = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", fixture(lang, "light", events, { lan: true, keys: keys() }));
await page.goto("http://magpie.test/?view=gateway");
await page.locator("#gatewayKeys .acc[data-key]").last().waitFor();
const row = (id) => page.locator(`#gatewayKeys .acc[data-key="${id}"]`);
const tag = (id) => row(id).locator(".key-admin");
const item = (name) => page.locator(".row-menu [role=menuitem]").filter({ hasText: name });
const level = (name) => page.locator(".proto-menu .pm-item").filter({ has: page.locator(".pm-name", { hasText: new RegExp(`^${name}$`) }) });
const menu = async (id) => {
await row(id).hover();
await row(id).getByRole("button", { name: w.more, exact: true }).click();
await page.locator(".row-menu").waitFor();
};
const levels = async (id, head = w.head) => {
await menu(id);
await item(w.adv).click();
await item(head).click();
await page.locator(".proto-menu").waitFor();
assert.equal((await page.locator(".proto-menu .pm-head").textContent()).trim(), head);
};
const memTag = (id) => row(id).locator(".key-memory");
const memories = () => events.filter((e) => e.action === "memory-key").map((e) => e.body);
const admins = () => events.filter((e) => e.action === "admin-key").map((e) => e.body);
// a key that can manage magpie says so always, not only on hover
await page.mouse.move(0, 0);
assert.equal((await tag("ops").textContent()).trim(), w.tag);
assert.ok((await tag("ops").boundingBox()).width > 0, "the tag is hidden");
assert.ok((await tag("ops").getAttribute("title")).length > 10, "the tag says what it means");
assert.equal(await tag("server").count(), 0);
const view = page.locator("#view-gateway");
const scrolled = async () => [await view.evaluate((v) => v.scrollTop), await page.evaluate(() => window.scrollY)];
const before = await scrolled();
// folded: the levels aren't in the row menu itself
await menu("server");
assert.equal(await page.locator(".row-menu").getByText(w.local, { exact: true }).count(), 0);
await page.keyboard.press("Escape");
await levels("server");
assert.equal(await level(w.off).getAttribute("aria-checked"), "true");
assert.equal(await level(w.any).getAttribute("aria-checked"), "false");
await level(w.local).click();
await page.waitForFunction(() => !!document.querySelector('#gatewayKeys .acc[data-key="server"] .key-admin'));
assert.deepEqual(admins(), [{ key: "server", admin: "local" }]);
assert.equal((await page.locator("#status").textContent()).trim(), w.madeLocal);
await levels("server");
assert.equal(await level(w.local).getAttribute("aria-checked"), "true");
await level(w.off).click();
await page.waitForFunction(() => !document.querySelector('#gatewayKeys .acc[data-key="server"] .key-admin'));
assert.deepEqual(admins().at(-1), { key: "server", admin: "" });
assert.equal((await page.locator("#status").textContent()).trim(), w.madeOff);
// the level a key has is ticked
await levels("ops");
assert.equal(await level(w.any).getAttribute("aria-checked"), "true");
await page.keyboard.press("Escape");
// Magpie Memory from other computers: off unless given, and said
// in the row while on
assert.equal((await memTag("ops").textContent()).trim(), w.memTag);
assert.ok((await memTag("ops").getAttribute("title")).length > 10, "the memory tag says what it means");
assert.equal(await memTag("server").count(), 0);
await levels("server", w.mem);
assert.equal(await level(w.local).getAttribute("aria-checked"), "true");
await level(w.any).click();
await page.waitForFunction(() => !!document.querySelector('#gatewayKeys .acc[data-key="server"] .key-memory'));
assert.deepEqual(memories(), [{ key: "server", memory: true }]);
assert.equal((await page.locator("#status").textContent()).trim(), w.memOn);
await levels("server", w.mem);
assert.equal(await level(w.any).getAttribute("aria-checked"), "true");
await level(w.local).click();
await page.waitForFunction(() => !document.querySelector('#gatewayKeys .acc[data-key="server"] .key-memory'));
assert.deepEqual(memories().at(-1), { key: "server", memory: false });
assert.equal((await page.locator("#status").textContent()).trim(), w.memOff);
// keys that can't hold it aren't offered it
for (const id of ["laptop", "magpie", "ext"]) {
await menu(id);
assert.equal(await item(w.adv).count(), 0, `${id} offers the management API`);
await page.keyboard.press("Escape");
assert.equal(await tag(id).count(), 0);
assert.equal(await memTag(id).count(), 0);
}
assert.equal(admins().length, 2);
assert.equal(memories().length, 2);
assert.deepEqual(await scrolled(), before, "a click moved the page");
const over = await page.evaluate(() => document.documentElement.scrollWidth - document.documentElement.clientWidth);
assert.ok(over <= 0, `the page scrolls sideways by ${over}px`);
const rowBox = await row("ops").boundingBox(), b = await tag("ops").boundingBox();
assert.ok(b.x >= rowBox.x && b.x + b.width <= rowBox.x + rowBox.width + 1, "the tag runs out of its row");
assert.equal(await page.locator("select").count(), 0, "no native select");
const borders = await page.locator("#gatewayKeys .key-admin, #gatewayKeys .key-memory").evaluateAll((els) => els.map((e) => {
const c = getComputedStyle(e);
return { left: c.borderLeftStyle, right: c.borderRightStyle, leftColor: c.borderLeftColor, rightColor: c.borderRightColor };
}));
for (const b of borders) assert(b.left === "none" || (b.left === b.right && b.leftColor === b.rightColor), `a coloured left border: ${JSON.stringify(b)}`);
assert.deepEqual(errors, []);
});
}
}
}