Files
magpie/internal/gui/tests/window-cap.test.cjs
T
yetone 40cbdaa7e8 provider: an account's window can be capped at a fixed amount of its unit
A window magpie reads with a total and a unit (a day's 1000 credits, a
plan's requests) can now be capped at an amount of that unit, not only
a share: 9won_25273 asked to stop an account at 300 of its day's credits.

- AccountWindowAmounts keeps it per account and window (capId). An
  amount and a window's own share replace each other.
- WindowCaps.limitShare turns it into a share of the total for the mark
  and NearCap. CapHeld and capReached hold the account once the window's
  count reaches the amount (past), and rest it as the percentage cap
  does: the next account takes the request. cappedError and the trace
  say "has used 300 credits ... past its cap of 300".
- A reading with no total ignores the amount; the account's share
  applies.
- GUI: the window meter's menu has one more item, Amount..., typed in
  place (provider/windowcap with amount). It shows only on a window
  with a unit and total. Strings in zh, zh-TW, ja, de.
- CLI: magpie provider account-cap <id> <account> --window <name>
  --amount <n|off>.

Usage outside magpie counts: every cap reads the vendor's own count, so
what the account spent in the vendor's app or on another machine counts
from the next reading on. magpie can only hold back its own requests.

Run, at this head on Linux (wsl-run, magpie-desktop):
- gofmt -l: clean; go vet ./... for linux (-tags nogui) and windows: ok (darwin
  vet needs cgo, so it runs with the full suite on mote afterwards)
- go test -run 'Lang|ToolLoop|LoopGuard|Cap|Amount' ./internal/gateway
  ./internal/provider ./internal/cli: ok (TestWindowAmountCapHeld,
  TestSetWindowAmount, TestWindowAmountHoldsTheAccount, the CLI cases)
- GUI window-cap, window-cap-pool, account-cap in chromium and webkit,
  420 and 1100 wide, en zh zh-TW ja de: 78 of 78 in each engine. An
  earlier webkit run lost the amount test's page once ("Target page,
  context or browser has been closed"); it passed in 2 more full-file
  runs and 60 of 60 alone
- i18n-keys: only "a plugin's words are its own" red, words that aren't
  this change's (#5332, #5426, #5482)
Broken by hand: limitShare ignoring amounts (of < 0): TestWindowAmountCapHeld
and TestWindowAmountHoldsTheAccount go red.
Not tried: the full go test ./... at this head (queued on mote, runs
after); a real account at an amount cap.
iFlytek: 9won's 讯飞 overshoot isn't covered. Loomy's daily window sends
used as a count and amount as what is left (plugins#889); AStudio's
wallets tell a balance only, which no cap can hold (#5770).

Reported-by: 9won_25273 (Discord)
2026-11-09 18:19:02 +08:00

314 lines
21 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// Each usage window of an account can have a cap of its own beside the
// account's (willz on Discord: a friend's account stops at 50% of its five
// hours, while its week has its own 40%). A window's meter on the account's
// row is a button: a click opens the app's own menu (no native select) of
// the account's cap, no cap on this window, a share, or Other… typed in
// place, each posted to provider/windowcap with the account, the window's
// capId and the cap (0: follow the account's; 100: none on this window). The
// meter says its own cap, its mark stands there, and the row's note says the
// account is at its cap when any window is past its own. A click moves
// nothing. Narrow and wide, in every language.
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 inHours = (h) => new Date(Date.now() + h * 3600e3 + 60e3).toISOString();
const fresh = () => ({
codex: {
id: "codex", name: "Codex", icon: "codex-color", chat: "", responses: "", anthropic: "", catalog: "",
models: [{ id: "gpt-6", name: "", on: true }],
agents: [], fallback: [], headers: {}, keyList: [], proxy: "",
account: {
agent: "codex", agentName: "Codex", user: "me@example.com", plan: "PLUS",
logins: [
{ user: "me@example.com", plan: "PLUS", active: true, on: true },
{ user: "Friend@Example.com", plan: "PLUS", on: true },
],
},
// the friend's five hours stop at 50%, its week at 40%; no account cap
// and its plan's credits at 50%, set before a plugin set that window
// aside (ferock on Discord)
accountWindowCaps: { "friend@example.com": { "5 hours": 50, weekly: 40, credits: 50 } },
},
});
const win = (name, used, h) => ({ name, capId: name.toLowerCase(), used, resetsAt: inHours(h), capped: true });
// the friend at 20% of its five hours, 30% of its week: under both
const under = () => ({
"me@example.com": { provider: "codex", windows: [win("5 hours", 10, 4), win("Weekly", 10, 100)] },
"Friend@Example.com": { provider: "codex", windows: [win("5 hours", 20, 4), win("Weekly", 30, 100)] },
});
// the friend at 60% of its five hours, past its own 50%; the week at 10%
const past = () => ({
"me@example.com": under()["me@example.com"],
"Friend@Example.com": { provider: "codex", windows: [win("5 hours", 60, 3), win("Weekly", 10, 100)] },
});
// a window set aside holds at its own cap only (ownCap): WorkBuddy's
// plan's credits and the extra ones, the account stopped by their total
const aside = (name, used) => ({ ...win(name, used, 600), ownCap: true });
const credits = (plan) => ({ provider: "codex", windows: [aside("Credits", plan), aside("Extra credits", 0), win("Total credits", plan / 4, 600)] });
const split = () => ({ "me@example.com": credits(60), "Friend@Example.com": credits(60) });
// a window counted in a unit with its total (9won_25273 on Discord: a
// day's credits) can be capped at an amount of it instead: me has used 120
// of the day's 1000 credits
const daily = () => ({
"me@example.com": { provider: "codex", windows: [win("5 hours", 10, 4), { ...win("Daily credits", 12, 20), amount: 120, limit: 1000, unit: "credits" }] },
"Friend@Example.com": under()["Friend@Example.com"],
});
function serve(lang, posts, use) {
const ps = fresh();
const providers = { providers: [ps.codex], presets: [], excluded: [], gateway: { running: true, window: true } };
const state = { agents: [], profiles: [], settings: { lang, theme: "light" } };
return async (route) => {
const url = new URL(route.request().url());
const json = (data) => route.fulfill({ json: data });
if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:true};` });
if (url.pathname === "/wails/runtime.js") return route.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
if (url.pathname === "/api/state") return json(state);
if (url.pathname === "/api/providers") return json(providers);
if (url.pathname === "/api/groups") return json({ groups: [] });
if (url.pathname === "/api/plugins") return json({ plugins: [] });
if (url.pathname === "/api/login/usage") return json(url.searchParams.get("agent") === "codex" ? use() : {});
if (url.pathname === "/api/provider/windowcap" && route.request().method() === "POST") {
const body = route.request().postDataJSON();
posts.push(body);
const p = providers.providers.find((x) => x.id === body.id);
const all = structuredClone(p.accountWindowCaps || {}), k = body.account.toLowerCase();
const amts = structuredClone(p.accountWindowAmounts || {});
const caps = all[k] || {}, amt = amts[k] || {};
// an amount and a share on one window replace each other
delete caps[body.window];
delete amt[body.window];
if (body.amount) amt[body.window] = body.amount;
else if (body.cap) caps[body.window] = body.cap;
if (Object.keys(caps).length) all[k] = caps; else delete all[k];
if (Object.keys(amt).length) amts[k] = amt; else delete amts[k];
p.accountWindowCaps = all;
p.accountWindowAmounts = amts;
return json(providers);
}
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 route.fulfill({ body: await fs.readFile(file), contentType });
};
}
const words = {
en: { cap: (n) => `Cap ${n}%`, none: "No cap", byWindow: "Caps by window", held: "At its cap · back in 3h", head: "Cap for 5 hours", account: "Account's cap · none", off: "No cap on this window", other: "Other…" , amount: "Amount…", own: "Cap 300 credits" },
zh: { cap: (n) => `上限 ${n}%`, none: "不设上限", byWindow: "按窗口设上限", held: "已达上限 · 3 小时后恢复", head: "5 小时的上限", account: "账号上限 · 无", off: "此窗口不设上限", other: "其他…" , amount: "固定数量…", own: "上限 300 积分" },
"zh-TW": { cap: (n) => `上限 ${n}%`, none: null, byWindow: "按時段設上限", held: null, head: "5 小時的上限", account: "帳號上限 · 無", off: "此時段不設上限", other: null , amount: "固定數量…", own: "上限 300 積分" },
ja: { cap: null, none: null, byWindow: "ウィンドウごとの上限", held: null, head: "5 時間の上限", account: "アカウントの上限 · なし", off: "このウィンドウは上限なし", other: null , amount: "固定量…", own: "上限 300 クレジット" },
de: { cap: null, none: null, byWindow: "Limits je Zeitfenster", held: null, head: "Limit für 5 Stunden", account: "Limit des Kontos · keins", off: "Kein Limit für dieses Zeitfenster", other: null , amount: "Menge…", own: "Limit 300 Credits" },
};
const NEW = [
"No cap on {window}: it is used to 100%, whatever this account's cap. Click to change",
"{window} has a cap of its own: magpie counts this account as used up once {window} is at {n}%, until it renews. Click to change",
"Click to give {window} a cap of its own, apart from this account's",
"{window} of {who} follows the account's cap", "{window} of {who} has no cap", "{who} stops at {n}% of {window}",
"Cap for {window}, in percent", "Account's cap · {n}%", "Account's cap · none", "As the account's other windows",
"No cap on this window", "Use this window to 100%", "Cap for {window}", "Caps by window",
"A window can have a cap of its own: click its meter below",
"Cap {n}", "{window} has a cap of its own: magpie counts this account as used up once it has used {n} of {window}, until it renews. Click to change",
"{who} stops at {n} of {window}", "Cap for {window}, in {unit}", "An amount above 0", "Amount…", "At most this much {unit}",
];
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const width of [420, 1100]) {
for (const lang of ["en", "zh", "zh-TW", "ja", "de"]) {
const w = words[lang];
const open = async (t, name, use) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
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}-${width}-${lang}-${name}-window-cap.png`) });
}
await browser.close();
});
const page = await (await browser.newContext({ viewport: { width, height: 760 }, reducedMotion: "reduce" })).newPage();
page.setDefaultTimeout(5000);
page.setDefaultNavigationTimeout(60000);
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
const posts = [];
await page.route("**/*", serve(lang, posts, use));
await page.goto("http://magpie.test/?view=providers");
await page.locator(".row.provider", { hasText: "Codex" }).first().click();
await page.locator(".editor .accts .acc .aq-w").first().waitFor();
return { page, errors, posts };
};
const row = (page, id) => page.locator(`.editor .accts .acc[data-account-id="${id}"]`);
const meter = (page, id, i) => row(page, id).locator('.aq-set[role="button"]').nth(i);
const menu = (page) => page.locator(".proto-menu");
const item = (page, name) => menu(page).locator(".pm-item", { has: page.locator(".pm-name", { hasText: new RegExp(`^${name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}$`) }) });
const scrolled = (page) => page.evaluate(() => [window.scrollY, ...[...document.querySelectorAll("*")].filter((e) => e.scrollTop > 0).map((e) => `${e.className}:${e.scrollTop}`)].join(" "));
const still = async (page, id, b) => {
await b.scrollIntoViewIfNeeded();
await page.waitForTimeout(150);
const before = await row(page, id).evaluate((e) => Math.round(e.getBoundingClientRect().top)), sc = await scrolled(page);
await b.click();
await page.waitForTimeout(200);
assert.equal(await row(page, id).evaluate((e) => Math.round(e.getBoundingClientRect().top)), before, `${id}'s row moved`);
assert.equal(await scrolled(page), sc, "a click scrolled");
};
const posted = async (posts, n) => {
for (let i = 0; i < 60 && posts.length === n; i++) await new Promise((r) => setTimeout(r, 50));
return posts.at(-1);
};
const label = `${engine} ${width} ${lang}`;
test(`${label}: a window past its own cap holds the account; one under it doesn't`, async (t) => {
const { page, errors } = await open(t, "past", past);
// 60% of the five hours, past its own 50%: held, the mark at 50 and 40
const held = row(page, "Friend@Example.com").locator(".acap-held");
if (w.held) assert.equal(await held.textContent(), w.held);
else assert.equal(await held.count(), 1);
assert.match(await held.getAttribute("title"), /60/);
assert.match(await held.getAttribute("title"), /50/);
assert.deepEqual(await row(page, "Friend@Example.com").locator(".aq-cap").evaluateAll((ms) => ms.map((m) => m.style.left)), ["50%", "40%"]);
// the pill says its caps are by window; each meter says its own
assert.equal(await row(page, "Friend@Example.com").locator(".acap").textContent(), w.byWindow);
if (w.cap) assert.deepEqual(await row(page, "Friend@Example.com").locator(".aq-own").allTextContents(), [w.cap(50), w.cap(40)]);
else assert.equal(await row(page, "Friend@Example.com").locator(".aq-own").count(), 2);
// me has no cap at all: no note, no marks, meters to set
assert.equal(await row(page, "me@example.com").locator(".acap-held, .aq-cap, .aq-own").count(), 0);
assert.equal(await row(page, "me@example.com").locator('.aq-set[role="button"]').count(), 2);
// a meter is reached by the keyboard too: Enter opens its menu
await meter(page, "me@example.com", 1).focus();
await page.keyboard.press("Enter");
await menu(page).waitFor();
assert.deepEqual(errors, []);
});
test(`${label}: 20% and 30% under their own 50% and 40% leave the account in use`, async (t) => {
const { page, errors } = await open(t, "under", under);
assert.equal(await row(page, "Friend@Example.com").locator(".acap-held").count(), 0);
assert.equal(await row(page, "Friend@Example.com").locator(".aq-cap").count(), 2);
assert.deepEqual(errors, []);
});
test(`${label}: a window set aside holds the account at the cap set on it before, and only that one`, async (t) => {
const { page, errors, posts } = await open(t, "aside", split);
// the friend's Credits at 60%, past the 50% set on it: held, marked
const held = row(page, "Friend@Example.com").locator(".acap-held");
if (w.held) assert.match(await held.textContent(), /^[^·]+·/);
assert.equal(await held.count(), 1);
assert.deepEqual(await row(page, "Friend@Example.com").locator(".aq-cap").evaluateAll((ms) => ms.map((m) => m.style.left)), ["50%"]);
// me has none on it: no note, no mark, and no new cap on a window set aside
assert.equal(await row(page, "me@example.com").locator(".acap-held, .aq-cap, .aq-own").count(), 0);
assert.equal(await row(page, "me@example.com").locator('.aq-set[role="button"]').count(), 0);
// the friend's is lifted from its meter
await still(page, "Friend@Example.com", meter(page, "Friend@Example.com", 0));
await menu(page).waitFor();
const n = posts.length;
await item(page, w.account).click();
assert.deepEqual(await posted(posts, n), { id: "codex", account: "Friend@Example.com", window: "credits", cap: 0 });
await page.waitForFunction(() => !document.querySelector('.editor .acc[data-account-id="Friend@Example.com"] .acap-held'));
assert.equal(await row(page, "Friend@Example.com").locator('.aq-set[role="button"]').count(), 0);
assert.deepEqual(errors, []);
});
test(`${label}: a window counted in a unit is capped at an amount of it, typed, and lifted`, async (t) => {
const { page, errors, posts } = await open(t, "amount", daily);
// the day's credits: Amount… last, after Other…
await still(page, "me@example.com", meter(page, "me@example.com", 1));
await menu(page).waitFor();
const names = await menu(page).locator(".pm-name").allTextContents();
assert.equal(names.length, 9);
assert.equal(names[8], w.amount);
let n = posts.length;
await item(page, w.amount).click();
const typed = row(page, "me@example.com").locator("input.acap-in");
await typed.waitFor();
await typed.fill("0");
await typed.press("Enter");
await page.waitForTimeout(150);
assert.equal(posts.length, n, "0 posted as an amount");
await still(page, "me@example.com", meter(page, "me@example.com", 1));
await item(page, w.amount).click();
await typed.waitFor();
await typed.fill("300");
await typed.press("Enter");
assert.deepEqual(await posted(posts, n), { id: "codex", account: "me@example.com", window: "daily credits", amount: 300 });
await row(page, "me@example.com").locator(".aq-own").first().waitFor();
assert.equal(await row(page, "me@example.com").locator(".aq-own").first().textContent(), w.own);
// the mark stands at 300 of 1000; 120 used is under it
assert.deepEqual(await row(page, "me@example.com").locator(".aq-cap").evaluateAll((ms) => ms.map((m) => m.style.left)), ["30%"]);
assert.equal(await row(page, "me@example.com").locator(".acap-held").count(), 0);
// the amount is current in the menu; the five hours, with no unit, has no Amount…
await still(page, "me@example.com", meter(page, "me@example.com", 1));
assert.equal(await menu(page).locator(".pm-item.on .pm-name").textContent(), w.amount);
// back to the account's: 0, the amount goes
n = posts.length;
await item(page, w.account).click();
assert.deepEqual(await posted(posts, n), { id: "codex", account: "me@example.com", window: "daily credits", cap: 0 });
await page.waitForFunction(() => !document.querySelector('.editor .acc[data-account-id="me@example.com"] .aq-own'));
await still(page, "me@example.com", meter(page, "me@example.com", 0));
assert.equal(await menu(page).locator(".pm-name").count(), 8);
assert.deepEqual(errors, []);
});
test(`${label}: a window's cap is set from its meter in the app's menu, typed, and lifted`, async (t) => {
const { page, errors, posts } = await open(t, "set", under);
// me's five hours: the menu, headed by the window, no native select
await still(page, "me@example.com", meter(page, "me@example.com", 0));
await menu(page).waitFor();
assert.equal(await menu(page).locator(".pm-head").textContent(), w.head);
const names = await menu(page).locator(".pm-name").allTextContents();
assert.equal(names[0], w.account);
assert.equal(names[1], w.off);
assert.deepEqual(names.slice(2, 7), ["50%", "60%", "70%", "80%", "90%"]);
assert.equal(names.length, 8);
assert.equal(await page.locator("select").count(), 0, "no native select");
let n = posts.length;
await item(page, "50%").click();
assert.deepEqual(await posted(posts, n), { id: "codex", account: "me@example.com", window: "5 hours", cap: 50 });
await row(page, "me@example.com").locator(".aq-own").first().waitFor();
if (w.cap) assert.equal(await row(page, "me@example.com").locator(".aq-own").first().textContent(), w.cap(50));
assert.deepEqual(await row(page, "me@example.com").locator(".aq-cap").evaluateAll((ms) => ms.map((m) => m.style.left)), ["50%"]);
// the week: Other…, 45 typed in place
await still(page, "me@example.com", meter(page, "me@example.com", 1));
await menu(page).locator(".pm-item").last().click();
const typed = row(page, "me@example.com").locator("input.acap-in");
await typed.waitFor();
await typed.fill("45");
n = posts.length;
await typed.press("Enter");
assert.deepEqual(await posted(posts, n), { id: "codex", account: "me@example.com", window: "weekly", cap: 45 });
await page.waitForFunction(() => document.querySelectorAll('.editor .acc[data-account-id="me@example.com"] .aq-own').length === 2);
// the friend's five hours: no cap on this window, 100
await still(page, "Friend@Example.com", meter(page, "Friend@Example.com", 0));
n = posts.length;
await item(page, w.off).click();
assert.deepEqual(await posted(posts, n), { id: "codex", account: "Friend@Example.com", window: "5 hours", cap: 100 });
await page.waitForFunction(() => document.querySelectorAll('.editor .acc[data-account-id="Friend@Example.com"] .aq-cap').length === 1);
if (w.none) assert.equal(await row(page, "Friend@Example.com").locator(".aq-own").first().textContent(), w.none);
// back to the account's: 0, its own goes
await still(page, "Friend@Example.com", meter(page, "Friend@Example.com", 0));
n = posts.length;
await item(page, w.account).click();
assert.deepEqual(await posted(posts, n), { id: "codex", account: "Friend@Example.com", window: "5 hours", cap: 0 });
await page.waitForFunction(() => document.querySelectorAll('.editor .acc[data-account-id="Friend@Example.com"] .aq-own').length === 1);
// the meters stay on the row's line, not wider than the page
assert.ok(await page.evaluate(() => document.documentElement.scrollWidth <= window.innerWidth), "no page scroll sideways");
const border = await page.evaluate(() => [...document.querySelectorAll(".accts .aq-set, .accts .aq-own, .accts .aq-cap, .proto-menu, .proto-menu *")].map((e) => getComputedStyle(e).borderLeftWidth).filter((b) => parseFloat(b) > 1));
assert.deepEqual(border, [], "no border stripes");
if (lang !== "en") {
const missing = await page.evaluate(([l, keys]) => keys.filter((k) => !I18N[l][k]), [lang, NEW]);
assert.deepEqual(missing, [], `every string has its ${lang}`);
}
assert.deepEqual(errors, []);
});
}
}
}