Files
magpie/internal/gui/tests/group-keys.test.cjs
T
yetone c6331b427c routing: a group bound to gateway keys is theirs alone (Xerxes754)
Xerxes754 on X asked to bind a routing group to one or two gateway
keys, so other keys neither see nor use it. Before this a key could only
be held to models from its own side (naming group/<id> in its Models);
a key with no limits reached every group, and nothing on a group could
keep it from one.

A group now has Keys (key ids). Set, only those keys, and keys issued
under one of them, may use it:
- groupBarred/groupGiven in key_models.go answer it for every route that
  reaches a group: chat, responses, messages, count_tokens, embeddings,
  images, video, the decider and the subagent and image MCP paths. Any
  other key gets 403 "bound to other gateway keys", naming the group and
  the Routing page, and nothing goes upstream.
- /v1/models and /v1/models/{id} leave it out for any other key, a key
  held to models that names it included. provider.GroupsBound keeps an
  unbound magpie's list as before.
- A keyless request from this computer uses it, as key limits never
  hold one; callerGuard now names a loopback request's key when it
  sends one, so a key used locally is held too.
- SaveGroup keeps the stored keys when a save doesn't name them (the
  CLI, the TUI, the card toggles); an empty list clears them. Ids are
  trimmed, deduped and capped. Sharing a group leaves its keys out;
  importing over one keeps them.
- GUI: Routing, a group's Edit, row "Gateway keys" (网关密钥) with a
  button "Any gateway key" (任意网关密钥) that opens the app menu of the
  user's keys (multi-pick); a bound key since deleted shows "key
  deleted" (密钥已删除). The row shows once there is a key to bind.
  /api/groups lists gatewayKeys (no browser, read-only or issued keys).
  zh, zh-TW, ja, de strings.
- Not covered: a bound group nested in another group is reached through
  the outer one (documented in gateway-routing.md).

Run (mote unless said), at this head rebased on ac4af5823:
- gofmt -l clean; go vet darwin for provider, gateway, gui; GOOS=windows
  go vet for those and access; GOOS=linux go vet -tags nogui; go build.
- go test ./internal/gateway (full package, 905s), ./internal/gui,
  ./internal/access, ./internal/provider: ok. One provider run died on
  workspaceSwitchReadsApart's concurrent map writes in its fake server,
  #5800 already open; the rerun was ok.
- New: TestGroupBoundToKeysIsTheirsAlone, TestGroupKeysKeptBySaves-
  ThatDontNameThem, TestGroupKeysListedAndSaved, and group-keys.test.cjs
  (5 languages x 2 widths x chromium + webkit, deleted key, hidden row).
- GUI on mote: group-keys, i18n*, rt-*, *routing*, *group*, *budget*
  files in both engines: ok. That run found a new group's editor
  throwing on its missing keys (group-editor-align red); the draft now
  gets keys like accounts. group-editor-fits was flaky on origin/main
  too and is fixed in the next commit.
- Whole GUI suite on the Linux box (wsl-run, concurrency 4): still
  running at push; its reds so far are in account-eye,
  accounts-own-signin and accounts-switch-alone (webkit), files this
  change doesn't reach.
- Real binary, sandbox HOME on mote, fake upstream, group bound in
  providers.json: key A lists and uses group/team; key B doesn't list
  it, gets 403 on chat (group/team and "team"), messages, count_tokens
  and responses, 404 on /v1/models/group/team, and still uses fake/m1;
  keyless local lists and uses it; upstream got 3 calls.

Broken by hand, each turned its Go test red: the issuer chain not
walked; an unheld key let through (groupGiven true); a held key's
groupAllowed ignoring the binding; SaveGroup not keeping nil keys.

Not tried: binding from the real app's window end to end (the e2e
bound by file), Windows and Linux runs of the Go tests; the CLI and TUI
can't set keys (they keep them).
2026-11-10 00:13:44 +08:00

190 lines
11 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// A routing group can be bound to gateway keys (Xerxes754 on X), so other
// keys neither see nor use it (Group.Keys). The group editor shows a
// Gateway keys row once there is a key to bind. Its button says "Any
// gateway key" until keys are ticked in a menu that stays open; one ticked
// shows its name, two show how many. Save sends the picks as Group.Keys. A
// bound key since deleted is listed as deleted and can be unticked. With no
// keys at all and none bound, the row is hidden. Nothing on the page moves.
// In every language, Chromium and WebKit, at a wide and a narrow window.
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 models = [{ id: "rl/glm-5", name: "glm-5", provider: "rl", providerName: "Relay", icon: "generic" }];
const gatewayKeys = [
{ id: "k-laptop", name: "Laptop", masked: "mg-…a1b2" },
{ id: "k-team", name: "Team server", masked: "mg-…c3d4" },
];
const groups = (keys, bound) => ({
models, pools: [], accounts: {}, gatewayKeys: keys ? gatewayKeys : [],
groups: [{ id: "work", name: "Work", members: ["rl/glm-5"], routing: "order", ready: true, accounts: [], ...(bound ? { keys: bound } : {}),
memberInfo: [{ id: "rl/glm-5", ready: true }] }],
});
const words = {
en: { edit: "Edit", save: "Save", label: "Gateway keys", any: "Any gateway key", two: "2 gateway keys", removed: "key deleted" },
zh: { edit: "编辑", save: "保存", label: "网关密钥", any: "任意网关密钥", two: "2 个网关密钥", removed: "密钥已删除" },
"zh-TW": { edit: "編輯", save: "儲存", label: "閘道金鑰", any: "任意閘道金鑰", two: "2 個閘道金鑰", removed: "金鑰已刪除" },
ja: { edit: "編集", save: "保存", label: "ゲートウェイキー", any: "どのゲートウェイキーでも", two: "ゲートウェイキー 2 個", removed: "削除されたキー" },
de: { edit: "Bearbeiten", save: "Speichern", label: "Zugangsschlüssel", any: "Jeder Zugangsschlüssel", two: "2 Gateway-Schlüssel", removed: "Schlüssel entfernt" },
};
function serve(lang, posts, keys, bound) {
const state = { agents: [{ id: "codex", name: "Codex", path: "/test/codex", fields: [] }], profiles: [], settings: { lang, theme: "light" } };
return async (r) => {
const url = new URL(r.request().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(state);
if (url.pathname === "/api/plugins") return json({ plugins: [] });
if (url.pathname === "/api/gateway/trace") {
if (url.searchParams.get("wait")) return new Promise(() => {}); // nothing more comes
return json({ mine: true, now: new Date().toISOString(), seq: 1, totals: { requests: 0, rerouted: 0, errors: 0 }, routes: [] });
}
if (url.pathname === "/api/gateway/history") return json({ cut: false, days: [], routes: [] });
if (url.pathname === "/api/groups") return json(groups(keys, bound));
if (url.pathname.startsWith("/api/groups/")) {
posts.push({ path: url.pathname, body: JSON.parse(r.request().postData() || "{}") });
return json(groups(keys, bound));
}
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)];
await r.fulfill({ body: await fs.readFile(file), contentType });
};
}
async function open(engine, lang, width, keys, bound, t) {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
const page = await (await browser.newContext({ viewport: { width, height: 1600 }, 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}-group-keys.png`) });
}
await browser.close();
});
page.setDefaultTimeout(5000);
page.setDefaultNavigationTimeout(60000);
const errors = [], posts = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", serve(lang, posts, keys, bound));
await page.goto("http://magpie.test/?view=groups");
const card = page.locator(".rt-group", { hasText: "Work" });
await card.waitFor();
return { page, card, errors, posts };
}
const option = (page, text) => page.locator("#pop:not([hidden]) #list li:not(.group)", { has: page.locator(".v", { hasText: new RegExp("^" + text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") + "$") }) });
async function save(page, w) {
const b = page.locator(".rt-gedit button.primary", { hasText: w.save });
await b.scrollIntoViewIfNeeded();
await b.click();
await page.waitForFunction(() => !document.querySelector(".rt-gedit"));
}
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of Object.keys(words)) {
for (const width of [1100, 420]) {
const w = words[lang];
test(`${engine} ${lang} ${width}px: a group is bound to chosen gateway keys`, async (t) => {
const { page, card, errors, posts } = await open(engine, lang, width, true, null, t);
await card.locator("button", { hasText: w.edit }).click();
const ed = page.locator(".rt-gedit");
const label = ed.locator("> label.rt-gkeys");
await label.waitFor();
assert.equal((await label.textContent()).trim(), w.label);
const chip = ed.locator("button.rt-gkey");
const said = async () => (await chip.textContent()).trim();
assert.equal(await said(), w.any);
assert.doesNotMatch((await chip.getAttribute("class")) || "", /\bon\b/);
// the reader scrolls the button into view with the wheel
await page.mouse.move(width / 2, 800);
for (let i = 0; i < 20 && (await chip.boundingBox()).y > 1200; i++) await page.mouse.wheel(0, 200), await page.waitForTimeout(100);
await page.waitForTimeout(200);
const box = await chip.boundingBox();
assert(box.y >= 0 && box.y + box.height <= 1600, `the button is in view before the click: ${box.y}`);
const at = () => page.evaluate(() => [...document.querySelectorAll("*")].filter((e) => e.scrollTop && !e.closest("#pop")).map((e) => [e.id || e.className, e.scrollTop]).join(";") + "|" + Math.round(document.querySelector(".rt-gedit").getBoundingClientRect().top));
const before = await at();
// the app's menu (never a native select) lists each key, none
// ticked, and stays open as one is
assert.equal(await ed.locator("select").count(), 0);
await chip.click();
await option(page, "Team server").waitFor();
assert.deepEqual(await page.locator("#pop #list li:not(.group) .v").allTextContents(), ["Laptop", "Team server"]);
assert.doesNotMatch((await option(page, "Team server").getAttribute("class")) || "", /\bcur\b/);
await option(page, "Team server").click();
await page.waitForFunction(() => !document.querySelector("#pop").hidden);
assert.match((await option(page, "Team server").getAttribute("class")) || "", /\bcur\b/);
assert.equal(await said(), "Team server");
assert.match((await chip.getAttribute("class")) || "", /\bon\b/);
// two ticked say how many; unticking one says its name again
await option(page, "Laptop").click();
assert.equal(await said(), w.two);
await option(page, "Laptop").click();
assert.equal(await said(), "Team server");
await page.keyboard.press("Escape");
await page.waitForFunction(() => document.querySelector("#pop").hidden);
assert.equal(await at(), before, "the picks moved nothing");
// the row fits a narrow window, the page doesn't scroll sideways
const over = await page.evaluate(() => {
const ed = document.querySelector(".rt-gedit"), b = ed.querySelector(".rt-gkey");
return { page: document.documentElement.scrollWidth - document.documentElement.clientWidth, row: b.getBoundingClientRect().right - ed.getBoundingClientRect().right };
});
assert(over.page <= 0, `the page scrolls sideways by ${over.page}px`);
assert(over.row <= 1, `the key button sticks out of the editor by ${over.row}px`);
await save(page, w);
const sent = posts.filter((p) => p.path === "/api/groups/save").at(-1).body;
assert.deepEqual(sent.keys, ["k-team"]);
assert.deepEqual(errors, []);
});
if (width === 1100) {
test(`${engine} ${lang}: a bound key since deleted is listed so it can be unticked`, async (t) => {
const { page, card, errors, posts } = await open(engine, lang, width, true, ["k-laptop", "k-gone"], t);
await card.locator("button", { hasText: w.edit }).click();
const chip = page.locator(".rt-gedit button.rt-gkey");
await chip.waitFor();
assert.equal((await chip.textContent()).trim(), w.two);
await chip.click();
await option(page, "k-gone").waitFor();
assert((await option(page, "k-gone").textContent()).includes(w.removed));
assert.match((await option(page, "k-gone").getAttribute("class")) || "", /\bcur\b/);
await option(page, "k-gone").click();
assert.equal((await chip.textContent()).trim(), "Laptop");
await page.keyboard.press("Escape");
await page.waitForFunction(() => document.querySelector("#pop").hidden);
await save(page, w);
const sent = posts.filter((p) => p.path === "/api/groups/save").at(-1).body;
assert.deepEqual(sent.keys, ["k-laptop"]);
assert.deepEqual(errors, []);
});
test(`${engine} ${lang}: with no gateway keys the row is hidden`, async (t) => {
const { page, card, errors, posts } = await open(engine, lang, width, false, null, t);
await card.locator("button", { hasText: w.edit }).click();
const ed = page.locator(".rt-gedit");
await ed.locator("button.primary", { hasText: w.save }).waitFor();
assert.equal(await ed.locator("> .rt-gkeys:visible").count(), 0);
await save(page, w);
const sent = posts.filter((p) => p.path === "/api/groups/save").at(-1).body;
assert.deepEqual(sent.keys, []);
assert.deepEqual(errors, []);
});
}
}
}
}