Files
magpie/internal/gui/tests/gateway-key-kept-out.test.cjs
T
yetone 0842bc517e gateway keys: a key may keep some accounts out and use every other, later ones too (LeiWang_2000 on X)
A gateway key could only be held to accounts picked one by one, so an
account signed in after the key was saved was never used. An entry
"<provider>/!<account>" (access.ExcludeMark) now keeps that account out:
the key uses every other account of the provider, one added later too,
and never the one kept out.

- access: AllowsAccount refuses a kept-out account first, and counts only
  the entries naming accounts to use for "the providers it names", so a
  provider with only kept-out entries serves every other account.
  HeldModels and AccountModels ignore kept-out entries (keeping one out
  holds the key to no model). An issued key keeps out what its issuer
  does (foldAccounts). CleanAccounts resolves the ref after the mark and
  refuses an account both used and kept out. ReplaceAccount moves
  kept-out entries too. AccountsShown renders "X; every account but Z"
  for the CLI and the gateway's refusals.
- CLI: `magpie gateway-key accounts <id> relay/!Second` prints "every
  account but relay/Second"; list and models read it.
- GUI: the key row's menu has "Keep accounts out", which opens the app's
  menu (openProtoMenu, no native select); a badge names what is kept out
  and opens the same menu; an account the key was held to and is now kept
  out leaves the held ones. Strings in zh, zh-TW, ja and de.
- docs: reference.md and providers-accounts.md.

Run (mote, at this tree):
- gofmt -l internal/ clean; go vet darwin, GOOS=linux -tags nogui and
  GOOS=windows of access, cli and gateway: clean.
- go test -count=1 ./internal/access ./internal/cli ./internal/gateway: ok.
  New: TestKeptOutAccounts, TestKeptOutFolded, TestKeptOutReplaced;
  TestGatewayKeyKeptOutAccountsKeyedRoutes (chat, messages, responses,
  count_tokens, images, videos, System One and /v1/models over a key
  provider, a remote magpie and a Decide provider: the kept-out key never
  asked, every account kept out asks nothing, a key added after the
  gateway key was saved is used); TestGatewayKeyKeptOutCodexAccounts
  (Codex sign-ins, one signed in later used);
  TestGatewayKeyKeptOutQuotaCards; TestGatewayKeyAccountsCLI extended.
- GUI: gateway-key-kept-out.test.cjs, chromium and webkit, 1100 and 440
  px, en zh zh-TW ja de: 20 of 20. Every gateway-*.test.cjs and
  i18n-*.test.cjs file in both engines: 496 of 516, the 20 red all
  gateway-card's count of the row menu's items (5, now 6), updated here;
  then gateway-card, api-key-theme, gateway-connect-keys, gui-de, gui-ja
  and gui-zh-tw: 39 of 39.
- Again after rebasing onto 4869d0c96 (this commit's tree): gofmt clean,
  vet darwin/linux nogui/windows clean, go test access, cli, gateway and
  gui (TestCoreScriptBudget among them) ok; gateway-key-kept-out,
  gateway-card, gateway-key-accounts, i18n-keys, i18n-en-no-cjk, gui-zh-tw,
  gui-ja, gui-de in both engines: 61 of 61.
- Rebased onto b922e5168 (nanobot, which also adds i18n strings): go test
  gui and access ok; gateway-key-kept-out, i18n-keys, i18n-en-no-cjk,
  gui-zh-tw, gui-ja, gui-de in both engines: 37 of 37.

Broken by hand:
- AllowsAccount without its kept-out refusal: TestKeptOutAccounts,
  TestKeptOutFolded and all three TestGatewayKeyKeptOut* tests red.
- gatewayKeptOutPick keeping a held account it was told to keep out:
  gateway-key-kept-out red (the server key's accounts-key body).

Not tried: a real account kept out on a running magpie (the tests use
fake upstreams and sign-ins); Windows (no OS-specific code).
2026-11-10 10:50:26 +08:00

114 lines
7.6 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// A gateway key may keep some accounts out instead of naming the ones it
// uses (LeiWang_2000 on X), so an account signed in later is used too:
// picked in the app's own menu from the key row's menu, sent as
// "accounts-key" with "<provider>/!<account>" entries when the menu
// closes, shown in a badge that opens the same menu; an account the key
// was held to and is now kept out leaves the held ones, and "No account
// kept out" takes them all off. In every language, Chromium and WebKit,
// wide and narrow.
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: { item: "Keep accounts out", label: "Accounts this key keeps out", none: "No account kept out", one: "Not relay/spare@example.com", two: "2 accounts kept out", title: /Every account but: relay\/me@example\.com, relay\/spare@example\.com/ },
zh: { item: "排除账号", label: "这个密钥排除的账号", none: "不排除账号", one: "排除 relay/spare@example.com", two: "排除 2 个账号", title: /除这些账号外都可用:/ },
"zh-TW": { item: "排除帳號", label: "這個金鑰排除的帳號", none: "不排除帳號", one: "排除 relay/spare@example.com", two: "排除 2 個帳號", title: /除這些帳號外都可用:/ },
ja: { item: "アカウントを除外", label: "このキーが除外するアカウント", none: "除外なし", one: "relay/spare@example.com を除外", two: "2 件のアカウントを除外", title: /これら以外のすべてのアカウント:/ },
de: { item: "Konten ausschließen", label: "Konten, die dieser Schlüssel ausschließt", none: "Kein Konto ausgeschlossen", one: "Ohne relay/spare@example.com", two: "2 Konten ausgeschlossen", title: /Alle Konten außer:/ },
};
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const width of [1100, 440]) {
for (const [lang, w] of Object.entries(words)) {
test(`${engine} ${width} ${lang}: a gateway key keeps out the accounts picked, and uses every other`, 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, keyHolds: { server: { accounts: ["relay/me@example.com"] } } }));
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 badge = (id) => row(id).getByRole("button", { name: w.label, exact: true });
const sent = () => events.filter((e) => e.action === "accounts-key").map((e) => e.body);
const until = async (fn) => {
for (let i = 0; i < 150 && !fn(); i++) await new Promise((r) => setTimeout(r, 20));
};
const view = page.locator("#view-gateway");
const scrolled = async () => [await view.evaluate((v) => v.scrollTop), await page.evaluate(() => window.scrollY)];
const at = await scrolled();
// a key that keeps none out has no badge; the row's menu picks
await page.mouse.move(0, 0);
assert.equal(await row("work").locator(".key-out").count(), 0);
await row("work").hover();
await row("work").locator(".gw-key-more").click();
await page.locator(".row-menu").getByRole("menuitem", { name: w.item, exact: true }).click();
const menu = page.locator(".proto-menu");
await menu.waitFor();
assert.equal(await page.locator("select").count(), 0, "no native select");
assert.equal((await menu.locator(".pm-head").textContent()).trim(), w.label);
assert.deepEqual((await menu.locator(".pm-item.on").allTextContents()).map((s) => s.trim()), [w.none]);
const box = await menu.boundingBox();
assert(box.x >= 0 && box.x + box.width <= width + 1, "the menu inside the window");
await menu.locator(".pm-item", { hasText: "spare@example.com" }).click();
assert.equal(sent().length, 0, "a pick was sent before the menu closed");
await page.keyboard.press("Escape");
await menu.waitFor({ state: "detached" });
await until(() => sent().length);
assert.deepEqual(sent(), [{ key: "work", accounts: ["relay/!spare@example.com"] }]);
await page.waitForFunction(() => document.querySelector('#gatewayKeys .acc[data-key="work"] .key-out'));
assert.equal((await badge("work").textContent()).trim(), w.one);
// kept out is not held to: the models badge still says every model
assert.equal(await row("work").locator(".key-models.set").count(), 0, "keeping one out held the key's models");
// the badge opens the same menu, the one kept out ticked; a second
events.length = 0;
await badge("work").click();
await menu.waitFor();
assert.equal(await menu.locator(".pm-item.on").count(), 1);
await menu.locator(".pm-item", { hasText: "me@example.com" }).first().click();
await page.keyboard.press("Escape");
await menu.waitFor({ state: "detached" });
await until(() => sent().length);
assert.deepEqual(sent(), [{ key: "work", accounts: ["relay/!me@example.com", "relay/!spare@example.com"] }]);
await page.waitForFunction((two) => document.querySelector('#gatewayKeys .acc[data-key="work"] .key-out')?.textContent.trim() === two, w.two);
assert.match(await badge("work").getAttribute("title"), w.title);
// a key held to an account and now keeping it out uses it no more
events.length = 0;
await row("server").hover();
await row("server").locator(".gw-key-more").click();
await page.locator(".row-menu").getByRole("menuitem", { name: w.item, exact: true }).click();
await menu.waitFor();
await menu.locator(".pm-item", { hasText: "me@example.com" }).first().click();
await page.keyboard.press("Escape");
await menu.waitFor({ state: "detached" });
await until(() => sent().length);
assert.deepEqual(sent(), [{ key: "server", accounts: ["relay/!me@example.com"] }]);
// "No account kept out" takes them all off
events.length = 0;
await badge("work").click();
await menu.waitFor();
await menu.locator(".pm-item", { hasText: w.none }).click();
await menu.waitFor({ state: "detached" });
await until(() => sent().length);
assert.deepEqual(sent(), [{ key: "work", accounts: [] }]);
await page.waitForFunction(() => !document.querySelector('#gatewayKeys .acc[data-key="work"] .key-out'));
assert.deepEqual(await scrolled(), at, "a click moved the page");
assert(!(await page.evaluate(() => document.documentElement.scrollWidth > document.documentElement.clientWidth)), "the page scrolls sideways");
const lefts = await page.locator("#gatewayKeys .key-out").evaluateAll((els) => els.map((e) => getComputedStyle(e).borderLeftStyle));
assert(lefts.every((s) => s === "none"), "a coloured left border");
assert.deepEqual(errors, []);
});
}
}
}