Files
magpie/internal/gui/tests/account-rest.test.cjs
T
yetone 63dc359fb4 providers: a subscription account's rest, or its model's, shows on its row (maxleebeta)
maxleebeta on Discord (#feedback): WorkBuddy answered 429
{"type":"rate_limit_error","message":"WorkBuddy: 您的使用量已超出频率限制,将在
2026-11-03 23:57:19 UTC+8 重置,您也可以切换其他模型继续使用。"} while the
provider's card said 1,934/2,350 credits left (82%), and nothing on the
Providers page said the account was held back.

Routing already handled the refusal: resetsWhen reads the time (UTC+8, the
space before 重置 included), failure() gives failQuota, and since it says
切换其他模型 the rest is that model's alone (restID), by "resets", till
15:57:19 UTC; the account's other models are still asked (#1669). What
was missing was the page: a key's rest showed on its row, an account's
never did.

- gateway.RestsUnder(prefix) lists the rests in force under a key prefix
  (provider@ for a subscription's accounts, provider@user/model for a
  model's), with Key and the rest's note (Model, By, Error).
- providerJSON.accountRests gives them by user in lower case, the model
  filled in from the key when the note has none; provider.KeyRest gains
  By, Model and Error.
- app.js: the account row in the provider editor gets a line per rest
  (accountRestLines): "<model> resting · 429 quota used up · back at
  <time>" or "Resting · …" for the whole account, the vendor's words
  beside it (shown whole when it names a time magpie couldn't read, so
  an unknown format still says why), and Try now (gateway/unrest by the
  rest's key). The key row's rest line is now the same helper
  (restLineOf), so app.js grows ~800 bytes, under the core budget.
- New strings in zh, zh-TW, ja and de. docs/subsystems/gateway-routing.md
  says where the Providers page reads them.

Not changed: the "-sg" maxleebeta saw in pi is WorkBuddy AI's own model
id (deepseek-v4.1-flash-sg, the paid Singapore id beside the free
deepseek-v4.1-flash); pi's footer and model picker show ids by design,
and the name magpie writes for it is the vendor's ("Deepseek-V4.1-Flash",
"(SG)" added by the plugin only where two ids share a name). Neither
magpie nor the plugin appends "-sg"; the CN build has no -sg ids.

Run (mote, through mote-run):
- go test ./internal/gateway ./internal/gui ./internal/provider at f0df4bb2b
  (base 06944e065): gateway ok (737s), provider ok, gui ok except
  TestMainWindowCorners/macOS_26_SDK, red on macOS 27 as on main (#3949).
- Rebased on 889767b7b: TestCoreScriptBudget, TestI18n*,
  TestAccountRestsInProviderState and the written-reset tests ok;
  gofmt -l clean. GOOS=linux and GOOS=windows go vet -tags nogui ok at
  f0df4bb2b, before the rebase. Then rebased once more onto 8ee172ca0,
  which changes only gateway search files; not re-run there.
- internal/gui/tests/account-rest.test.cjs: chromium and webkit, en, zh,
  zh-TW, ja, de, at 900 and 360px, ok. rest-try-now.test.cjs's 15 webkit
  reds fail the same on origin/main 06944e065 (#2564, #4127).
- Broken by hand: TestRateLimitedModelListedUnderAccount red with
  RestsUnder returning nothing, TestAccountRestsInProviderState red
  without accountRests, account-rest.test.cjs red without
  accountRestLines.

Not tried: a real WorkBuddy account hitting this 429, so the line has
not been seen on a live gateway; the whole GUI suite (mote under another
session's suite). The line shows in the provider editor's account rows,
not on the collapsed provider row.
2026-11-03 14:09:42 +08:00

107 lines
7.1 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// A subscription account the gateway rests says so on its row on the
// Providers page, as a key does (maxleebeta on Discord: WorkBuddy AI
// answered 429 "您的使用量已超出频率限制,将在 2026-11-03 23:57:19 UTC+8
// 重置,您也可以切换其他模型继续使用。" with 82% of its credits left, and
// the page said nothing). A rest of one model names the model and when it
// is back; the vendor's words show beside it, so a refusal whose time magpie
// couldn't read is still explained; Try now lifts it by the gateway's key.
// A rest gone by, and an account not resting, show nothing. It fits at
// 360px. 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 SAID = "WorkBuddy: 您的使用量已超出频率限制,将在 2026-11-03 23:57:19 UTC+8 重置,您也可以切换其他模型继续使用。";
const USER = "Max@Example.com", OTHER = "two@example.com", THIRD = "three@example.com";
const fresh = (until) => ({
id: "workbuddy-ai", name: "WorkBuddy AI", icon: "", chat: "", responses: "", anthropic: "", catalog: "",
models: [{ id: "gpt-6-astra", name: "GPT-6-Astra", on: true }, { id: "deepseek-v4.1-flash-sg", name: "Deepseek-V4.1-Flash", on: true }],
agents: [], fallback: [], headers: {}, keyList: [], proxy: "",
account: {
agent: "workbuddy-ai", agentName: "WorkBuddy AI", user: USER, plan: "",
logins: [{ user: USER, active: true, on: true }, { user: OTHER, on: true }, { user: THIRD, on: true }],
},
accountRests: {
"max@example.com": [{ why: "quota", status: 429, until, key: "workbuddy-ai@max@example.com/gpt-6-astra", by: "resets", model: "gpt-6-astra", error: SAID }],
// the whole account, gone by: nothing
"three@example.com": [{ why: "rate", status: 429, until: new Date(Date.now() - 60000).toISOString(), key: "workbuddy-ai@three@example.com", by: "cooldown" }],
},
});
function serve(lang, until, lifted) {
const settings = { theme: "light", lang, tray: "panel", quotaLeft: false, currency: "usd" };
const providers = { providers: [fresh(until)], presets: [], excluded: [], gateway: { running: true, window: true },
plugins: [{ id: "workbuddy-ai", pid: "workbuddy", name: "WorkBuddy AI", icon: "", spec: "@magpie-community/opencode-workbuddy-auth" }] };
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({ agents: [], profiles: [], settings });
if (url.pathname === "/api/settings") return json(settings);
if (url.pathname === "/api/providers") return json(providers);
if (url.pathname === "/api/gateway/unrest") { lifted.push(JSON.parse(route.request().postData() || "{}")); return json({}); }
if (url.pathname === "/api/usage/quotas") return json([]);
if (url.pathname === "/api/groups") return json({ groups: [], models: [] });
if (url.pathname === "/api/plugins") return json({ plugins: [] });
if (url.pathname === "/api/login/usage") return json({});
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)];
try { await route.fulfill({ body: await fs.readFile(file), contentType }); } catch { await route.fulfill({ status: 404, body: "" }); }
};
}
const LANGS = ["en", "zh", "zh-TW", "ja", "de"];
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of LANGS) {
test(`${engine} ${lang}: an account's model the gateway rests says so on its row, with the vendor's words and Try now`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
for (const width of [900, 360]) {
const until = new Date(Date.now() + 3 * 3600e3).toISOString(), lifted = [];
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));
await page.route("**/*", serve(lang, until, lifted));
await page.goto("http://magpie.test/?view=providers");
await page.locator(".row.provider", { hasText: "WorkBuddy AI" }).first().click();
const row = (id) => page.locator(`.editor .accts .acc[data-account-id="${id}"]`);
await row(USER).waitFor();
const tr = (k, v) => page.evaluate(([k, v]) => t(k, v), [k, v]);
const back = await page.evaluate((u) => restBack(u), until);
const why = "429 " + (await tr("quota used up"));
const line = row(USER).locator(".key-rest-line");
assert.equal(await line.count(), 1);
assert.equal(await line.locator(".key-rest").textContent(), await tr("{model} resting · {why} · back at {time}", { model: "gpt-6-astra", why, time: back }));
assert.equal(await line.locator(".key-rest").getAttribute("title"), await tr("Until then {model} goes to another account; its other models are used as before", { model: "gpt-6-astra" }));
assert.equal(await line.locator(".key-rest-said").textContent(), SAID);
assert.ok(await line.locator(".key-rest").isVisible());
assert.ok(await line.locator(".key-unrest").isVisible());
// an account not resting, and a rest gone by: nothing
assert.equal(await row(OTHER).locator(".key-rest-line").count(), 0);
assert.equal(await row(THIRD).locator(".key-rest-line").count(), 0);
// the row's own line is untouched, and nothing scrolls sideways
const box = await row(USER).boundingBox(), lb = await line.boundingBox();
assert.ok(lb.x >= box.x && lb.x + lb.width <= box.x + box.width + 1, `the rest line fits its row at ${width}px`);
assert.equal(await page.evaluate(() => document.documentElement.scrollWidth - document.documentElement.clientWidth), 0, `no sideways scroll at ${width}px`);
// Try now lifts it by the gateway's key
await line.locator(".key-unrest").click();
await page.waitForFunction(() => !document.querySelector(".editor .key-rest-line"));
assert.deepEqual(lifted, [{ key: "workbuddy-ai@max@example.com/gpt-6-astra" }]);
assert.deepEqual(errors, []);
await page.context().close();
}
});
}
}