Files
magpie/internal/gui/tests/gateway-key-groups.test.cjs
T
yetone b9d20856ef gateway, gui: a gateway key can be held to routing groups (Magic_zero on Discord) — a key's models menu on the Gateway page listed providers' models only, so a key couldn't be limited to the routing groups an agent was meant to use. The menu now lists the routing groups first, with Every routing group (group/*), and a key may keep group/<id>. A key that names a group may use it with every member in it, the next member too when one fails, but not those members asked for by name, a group it doesn't name, or a fallback outside the group; a group named inside one it doesn't name counts for its own members. A group the key doesn't name still passes only when it may use every member, as before. /v1/models and the Anthropic and Gemini lists show the key its groups. en/zh/ja/de; docs/reference.md. TestGatewayKeyModelsNameAGroup (group/both lists and serves only the group, falls to the next member, members by name and other groups refused; group/* every group; a nested named group; a member's own fallback outside the group refused) fails without the change; TestCallerKeyModelsRoutes lists the groups. gateway-key-groups.test.cjs (WebKit, en and zh: groups first after All models, Every routing group, a group picked sends group/coding, a held key's group ticked, no select, no scroll, no left border) fails without the change; gateway-key-models, gateway-key-limit and click-scroll pass in WebKit. go vet, go build (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui ./... under a temp HOME pass.
2026-10-05 18:44:27 +08:00

65 lines
4.2 KiB
JavaScript

// A gateway key can be held to routing groups (Magic_zero on Discord):
// the key's models menu lists the groups, first, with "Every routing
// group", and a group picked is sent as "group/<id>".
const assert = require("node:assert/strict");
const { test } = require("node:test");
const { chromium, webkit } = require("playwright");
const { fixture } = require("./fixtures/caller-keys.cjs");
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of ["en", "zh"]) {
test(`${engine} ${lang}: a gateway key is held to the routing groups picked for it`, 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: 1000, height: 600 }, reducedMotion: "reduce" })).newPage();
page.setDefaultTimeout(6000);
const events = [], errors = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", fixture(lang, "light", events, { lan: true, groups: true, limits: { server: { models: ["group/fast"] } } }));
const zh = lang === "zh";
const w = zh
? { label: "此密钥可用的模型", all: "全部模型", groups: "全部路由组", relay: "Relay 的全部模型" }
: { label: "Models this key may use", all: "All models", groups: "Every routing group", relay: "Every Relay model" };
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 });
// a key held to one group says which
assert.equal((await badge("server").textContent()).trim(), "group/fast");
const view = page.locator("#view-gateway");
const scrolled = async () => [await view.evaluate((v) => v.scrollTop), await page.evaluate(() => window.scrollY)];
const at = await scrolled();
await row("work").hover();
await badge("work").click();
const menu = page.locator(".proto-menu");
await menu.waitFor();
assert.equal(await page.locator("select").count(), 0, "no native select");
// the groups come first, after All models, then each provider's models
const names = (await menu.locator(".pm-item").allTextContents()).map((s) => s.trim());
const at0 = (s) => names.findIndex((n) => n.startsWith(s));
assert(at0(w.all) === 0, names.join(" | "));
assert(at0(w.groups) === 1, names.join(" | "));
assert(at0("Coding") > at0(w.groups) && at0("Fast") > at0("Coding"), names.join(" | "));
assert(at0(w.relay) > at0("Fast"), names.join(" | "));
assert.match(await menu.locator(".pm-item", { hasText: "Coding" }).textContent(), /group\/coding/);
await menu.locator(".pm-item", { hasText: "Coding" }).click();
await menu.locator(".pm-item", { hasText: "GPT-5" }).click();
await page.keyboard.press("Escape");
await menu.waitFor({ state: "detached" });
assert.deepEqual(events.filter((e) => e.action === "models-key").map((e) => e.body), [{ key: "work", models: ["group/coding", "openai/gpt-5"] }]);
// the key held to group/fast has it ticked; Every routing group picks group/*
await badge("server").click();
await menu.waitFor();
assert.deepEqual((await menu.locator(".pm-item.on").allTextContents()).map((s) => s.trim()), ["Fastgroup/fast"]);
await menu.locator(".pm-item", { hasText: w.groups }).click();
await page.keyboard.press("Escape");
await menu.waitFor({ state: "detached" });
assert.deepEqual(events.filter((e) => e.action === "models-key").at(-1).body, { key: "server", models: ["group/*", "group/fast"] });
assert.deepEqual(await scrolled(), at, "a click moved the page");
const lefts = await page.locator("#gatewayKeys .key-models, .proto-menu .pm-item").evaluateAll((els) => els.map((e) => getComputedStyle(e).borderLeftStyle));
assert(lefts.every((s) => s === "none"), "a coloured left border");
assert.deepEqual(errors, []);
});
}
}