Files
magpie/internal/gui/tests/gateway-key-model-limit.test.cjs
T
yetone 1f439ecc5e gateway keys: a limit can count only some models or providers (zola_xynb)
zola_xynb on X asked for a cap per model or per provider under one
gateway key. A key limit can now name models (access.Limit.Models,
"<provider>/<model>" or "<provider>/*"), so one key can have, for
example, a month of $20 on openai/* next to a month for the whole key.
Limits without models still cover the whole key, as before.

- budget: sums are kept per window and scope, and count a ledger row only
  when its provider/model matches (any of the model's ids). reserve holds
  a request to a scoped limit only when the model it names is served
  there. windowsOf keeps every scope's sums, so the ledger is not read
  again on each request.
- gateway: keyLimited puts the key's spent scopes on the caller
  (withSpent), count_tokens included. A direct request for a spent model
  gets a 429 with Retry-After and no X-Magpie-Limit-Reset, so a remote
  magpie rests only that model. A routing group, a provider's fallback
  and embedding/image/video/decision groups skip spent members
  (groupKeeps, allowedCandidates) and get a 429 only when none is left.
  System One, count_tokens and magpie's own calls for the key are held
  too. Model lists are unchanged. A refusal turned away before routing
  now records its ErrType in the ledger.
- CLI: magpie gateway-key limit <id> add month --cost 20 --model openai/*
  (--model repeatable, --models a,b).
- GUI: the key limit editor has a Models field that opens the app menu
  (multi-pick: All models, every model of a provider, each model). It has
  no <select> and no new screen. The badge, line and tabs name the models.
  Strings are in every language.
- docs: reference.md and subsystems/gateway-routing.md.

Run at d52725d32 (this change on 4ef47b3f4), on mote:
- go test ./... -timeout 45m, under the shared lock, with the log kept
  whole: every package ok except three reds outside this change. None of
  the three touches what changed:
  - TestClosedBeforeSentEveryTimeSaysWhy (#5031, passes alone 3/3)
  - TestMainWindowCorners/macOS_26_SDK (#3949, red alone on macOS 27)
  - TestMoveToPlugin TempDir cleanup (filed #5524, passes alone 2/2)
- go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go
  vet ./..., go build ./...: ok. gofmt -l: nothing.
- GUI, chromium + webkit: gateway-key-*.test.cjs, gateway-keys-*,
  i18n-keys, i18n-en-no-cjk and usage-remote-key-limits passed 244/246.
  The new gateway-key-model-limit runs in every language at 1100 and
  420px. The two reds also fail on origin/main 3cbb7e042:
  - gateway-key-models en at 560px (#4081, #4616, #5233)
  - i18n-keys "a plugin's words are its own" (#5327, #5332, #5426, #5482)

Broken by hand; each one turned the named tests red:
- groupKeeps' spent skip removed: ItsModelsAlone and MediaGroups.
- modelAllowed's spent check removed: ItsModelsAlone and AFallback.
- withSpent on count_tokens removed: ItsModelsAlone.
- the scoped refusal sending X-Magpie-Limit-Reset: ItsModelsAlone and
  SystemOne.
- sums counting every model: ItsModelsAlone, TestScopedLimitCountsItsModels
  and TestScopedBesideTheWholeKey.
- embeddings' keyGroupRefusal back to keyModelRefusal: MediaGroups.
- the turned-away row's ErrType dropped: ItsModelsAlone.
- keyHolds not holding for Spent: ItsModelsAlone, AFallback and
  MediaGroups.
- decide.go's 429 back to a 403 stayed green. keyLimited's reserve
  refuses a direct decider first, so that line is a second guard.

Not tried:
- a real vendor account or a real remote magpie.
- the Codex backend path and a video group: they go through the same
  keyLimited/groupKeeps code, but no test of their own.
- the whole GUI suite: only the files above.
- Windows and Linux test runs: vet only.
2026-11-06 20:47:21 +08:00

109 lines
6.3 KiB
JavaScript

// A gateway key's limit on some models (zola_xynb on X): the limit
// editor's Models picks them in the app's menu, every model of a provider
// at once or one by one, and none picked is the whole key, as a limit set
// before there was a pick. The badge, the line and the tabs name what a
// limit counts. In every language, both engines, wide and narrow.
const assert = require("node:assert/strict");
const fs = require("node:fs");
const path = require("node:path");
const { test } = require("node:test");
const { chromium, webkit } = require("playwright");
const { fixture } = require("./fixtures/caller-keys.cjs");
const src = fs.readFileSync(path.resolve(__dirname, "../assets/i18n.js"), "utf8");
const I18N = new Function(src.slice(0, src.indexOf("\n};") + 3) + "; return I18N;")();
const day = 864e5;
const limits = () => ({
laptop: {
limit: { period: "month", cost: 20, models: ["openai/*"] },
used: { period: "month", start: new Date(Date.now() - 3 * day).toISOString(), reset: new Date(Date.now() + 20 * day).toISOString(), calls: 8,
tokens: 300000, cost: 4, costLimit: 20, tokensLeft: 0, costLeft: 16, spent: false, models: ["openai/*"] },
},
});
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of ["en", "zh", "zh-TW", "ja", "de"]) {
for (const width of [1100, 420]) {
test(`${engine} ${lang} ${width}: a key's limit on some models`, async (t) => {
const tr = (k, vars = {}) => {
const s = lang === "en" ? k : I18N[lang][k];
assert.equal(typeof s, "string", `${lang} has ${k}`);
return s.replace(/\{(\w+)\}/g, (_, v) => String(vars[v]));
};
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
const page = await (await browser.newContext({ viewport: { width, height: 760 }, 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, limits: limits(), groups: true }));
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 sent = (action) => events.filter((e) => e.action === action);
const fits = async (what) => assert.equal(await page.evaluate(() => document.documentElement.scrollWidth > window.innerWidth), false, what + " scrolls sideways");
const tap = async (loc) => {
await page.mouse.move(width / 2, 400);
for (let i = 0; i < 20 && !(await loc.evaluate((e) => { const r = e.getBoundingClientRect(); return r.top >= 0 && r.bottom <= (document.querySelector("footer.foot")?.getBoundingClientRect().top ?? innerHeight); })); i++) {
await page.mouse.wheel(0, 120);
await page.waitForTimeout(50);
}
await loc.click();
};
// the badge and the line name the models the limit counts
const badge = row("laptop").locator(".amodels.limit");
assert((await badge.textContent()).includes("openai/*"), "badge says " + await badge.textContent());
const line = row("laptop").locator(".key-limit");
assert((await line.textContent()).startsWith("openai/*"), "line says " + await line.textContent());
await fits("the rows");
// the editor's Models, the app's menu, never a native select
await badge.click();
const ed = row("laptop").locator(".key-limit-ed");
await ed.waitFor();
assert.equal(await ed.locator("select").count(), 0);
const pick = ed.getByRole("button", { name: tr("Models this limit counts"), exact: true });
assert.equal((await pick.textContent()).trim(), "openai/*");
assert.equal(await pick.evaluate((e) => getComputedStyle(e).borderLeftStyle === getComputedStyle(e).borderRightStyle), true);
await fits("the editor");
await tap(pick);
const menu = page.locator(".proto-menu");
await menu.waitFor();
assert.equal((await menu.locator(".pm-head").textContent()).trim(), tr("Models this limit counts"));
// a routing group isn't offered: a call is counted by the model that served it
assert.equal(await menu.getByText("group/coding", { exact: true }).count(), 0);
const item = (name) => menu.locator(".pm-item").filter({ has: page.getByText(name, { exact: true }) });
assert.equal(await item(tr("Every {provider} model", { provider: "OpenAI" })).getAttribute("aria-checked"), "true");
// one Relay model more, and the window as it was
await item("relay/m-mini").click();
await page.keyboard.press("Escape");
await menu.waitFor({ state: "detached" });
assert.equal((await pick.textContent()).trim(), tr("{n} models", { n: 2 }));
assert.equal(await ed.locator(".munsaved").isHidden(), false, "a pick is unsaved until Save");
await tap(ed.getByRole("button", { name: tr("Save"), exact: true }));
await ed.waitFor({ state: "detached" });
assert.deepEqual(sent("limit-key").map((e) => e.body), [{ key: "laptop", limit: { period: "month", tokens: 0, cost: 20, cacheReads: false, models: ["relay/m-mini", "openai/*"] } }]);
assert((await badge.textContent()).includes(tr("{n} models", { n: 2 })));
// All models: the whole key again, sent with no models
await badge.click();
await ed.waitFor();
await tap(pick);
await menu.waitFor();
await item(tr("All models")).click();
await menu.waitFor({ state: "detached" });
assert.equal((await pick.textContent()).trim(), tr("All models"));
await tap(ed.getByRole("button", { name: tr("Save"), exact: true }));
await ed.waitFor({ state: "detached" });
assert.deepEqual(sent("limit-key").at(-1).body, { key: "laptop", limit: { period: "month", tokens: 0, cost: 20, cacheReads: false } });
assert.equal((await badge.textContent()).includes("openai/*"), false);
assert.equal((await line.textContent()).includes(tr("{n} models", { n: 2 })), false);
assert.deepEqual(errors, []);
});
}
}
}