prgrmrwy runs magpie on a home server and uses it from work through a remote magpie (and DSH). The remote Codex card said "1 reset", but the reset could only be used from the GUI of the computer that holds the sign-in. What changed: - Gateway: POST /v1/magpie/quotas/codex-reset?user=<email>[&credit=<id>] (Server.codexReset) spends one of that account's resets with provider.SpendCodexReset. That is the Usage page's own code (UseCodexResetCard, then AwaitAllowance), moved out of internal/gui/usage.go so both use it. A reset can't be undone, so the route needs an enabled gateway key with full access, from loopback too. No key gets 401, and that covers every corsGuard exemption that has no key: Origin null, app schemes, *.localhost and a missing Origin. A read-only key gets 403 gateway_key_read_only (readOnlyGuard, which readOnlyRefused now lists, and again in the handler). A held key gets 403 gateway_key_held, even one told quotas. The account must be named (400 otherwise). One ask runs per account at a time (409). When the reset is used, that card is read again. - Remote client: UseRemoteCodexReset (remote_quotas.go) asks the remote with the provider's key. remoteJSON's status mapping is shared through remoteBody. A read-only key there is told as such, and a 404 as a remote too old. With no answer in time it says the reset may have been used, and it never asks again by itself. A remote Codex card now keeps its Resets; a GLM team's (byWindow) and every check-in are still dropped. - GUI: "Use a reset" / "Use one..." show on a remote Codex card (codexResetCard). They open the same in-app confirmation sheet, with a line saying which computer's magpie uses it, and send the card's provider to /api/usage/codex-reset. The remote's errors are put in the user's words. New strings are in all 5 languages. - DSH: no code here. Its provider plugin reads /v1/magpie/quotas/cards and can call the new route with a full key. - resetOutcome (a reset's result in words) moved from app.js into resetask.js, the only place that says it, so the core's scripts stay in TestCoreScriptBudget's 1,533,802 with no raise (1,533,003 after; origin/mainb967b1620is 1,533,756). - Docs: reference.md (remote quotas, gateway keys), gateway-routing.md (read-only list, the route's admission), providers-accounts.md. What was run (rebased on origin/mainb967b1620, then on2e4735f8f): - mote, at the head before the rebase and the resetOutcome move: go test -count=1 -timeout 45m ./internal/provider ./internal/gateway ./internal/gui at load ~300-450. provider ok. gateway: only TestCompactionOverByteCapEveryTime (#4862). gui: TestCoreScriptBudget (this change, 158 bytes over; fixed by the move), TestMainWindowCorners (#3949) and TestGatewaySkewOfAnotherIsSaidNotQuit/own_build (#5053, passes alone here and atb967b1620). - mote, same run: 155 GUI files (reset|quota|usage-|panel-|i18n|budget| remote|gui-|old-webkit|core-script|script-size), 1856 of 1904 pass. - WSL (magpie-desktop), the 9 red files alone, at this change and at origin/mainb967b1620: this change 147 pass / 43 fail, main 146 / 44; every test red here is red on main too: codex-auto-reset webkit de (#4790), remote-magpie (#4398), usage-agents-hide (#3414), usage-fold (#3415, #4790), usage-heatmap (#4718), usage-ledger (#5048). codex-reset-lead (#5054) and panel-routing-providers (#5055) were red only under mote's load and pass alone on both. - mote, at this head: TestCoreScriptBudget and TestGatewaySkewOfAnotherIsSaidNotQuit ok. - WSL, at this head: account-every-reset, codex-resets-used, i18n-keys, providers-reset-use, remote-codex-reset, reset-each-title, reset-pick-card, reset-providers, quota-used-no-reset: 99 of 102 pass. The 3 reds are providers-reset-use's 420px Windows cases ("a click moved the page", #4873); that file alone is red on origin/mainb967b1620too (3 of 40 there, 2 of 40 here). - WSL, rebased on2e4735f8f(main had changed internal/gateway): the new tests and the read-only, CORS, LAN-guard, held-key, remote-magpie and UseCodexResetCard tests in ./internal/gateway ./internal/provider ok; core scripts 1,532,960 under 1,533,802; gofmt clean. - Earlier on mote: TestCodexResetNeedsAFullKey, TestRemoteCodexResetThroughTheGateway, TestRemoteCodexResetAnswers, the read-only, CORS, LAN-guard, held-key and remote-magpie tests ok; remote-codex-reset and reset-pick-card 20 of 20 (chromium and webkit, 5 languages, 900/390/440px). gofmt clean; go vet darwin, windows ok, linux for provider and gateway. Broken by hand: - With the handler's key check turned off, TestCodexResetNeedsAFullKey went red: "this computer, no key: 200 {code: reset}" and a reset was spent. - With codexResetCard put back to provider === "codex", remote-codex-reset.test.cjs went red ("one Use, on the remote Codex card"). On the old code the route doesn't exist, so the full-key case gets 404. Not tried: - A real Codex reset spent between two real magpies: none was spent, to keep the owner's resets. The ChatGPT backend is the fake from codex_autoreset_test. - DSH's plugin calling the route. - GOOS=linux vet of internal/gui: it needs gtk4/webkitgtk, which neither mote nor magpie-desktop has. Its change is usage.go only, and that was vetted on darwin and windows. Thanks to prgrmrwy for the report and the proposal.
133 lines
8.4 KiB
JavaScript
133 lines
8.4 KiB
JavaScript
// Run with Node's test runner and Playwright on the module path; see README.md.
|
|
// #3850 (prgrmrwy): magpie on a home server, and the office's remote magpie
|
|
// card said "1 reset" with no way to use it there. A remote magpie's Codex
|
|
// card now offers the reset as this computer's does: the same in-app
|
|
// confirmation (no confirm()), which says the magpie on that computer uses
|
|
// it, and nothing is spent before the user says so. The use names the
|
|
// card's remote provider and the account. A GLM team's resets (byWindow),
|
|
// spent on the vendor's page, and a remote's other cards offer none. What
|
|
// the remote answered goes in the user's words (a read-only key, no answer
|
|
// in time). The Usage page at 900px and 390px and the menu bar panel at
|
|
// 440px; English, Chinese (both), Japanese and German; Chromium and WebKit.
|
|
// No backend, the API is faked.
|
|
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(process.env.ASSET_DIR || path.resolve(__dirname, "../assets"));
|
|
const day = 864e5, base = Date.now();
|
|
const soon = new Date(base + 2 * day).toISOString();
|
|
const windows = [{ name: "5 hours", used: 100, resetsAt: new Date(base + 3600e3).toISOString() }, { name: "7 days", used: 60, resetsAt: new Date(base + 3 * day).toISOString() }];
|
|
const quotas = [
|
|
{ provider: "office/codex", name: "Codex · Office", icon: "codex-color", from: "Office", user: "me@example.com", plan: "Plus", kind: "subscription", windows,
|
|
resets: { count: 1, until: soon } },
|
|
// a GLM team's, read on the remote: spent on bigmodel.cn, not from here
|
|
{ provider: "office/zhipu-team", name: "GLM Team · Office", icon: "zhipu", from: "Office", user: "team@example.com", kind: "subscription", windows,
|
|
resets: { count: 2, byWindow: true } },
|
|
];
|
|
|
|
const READ_ONLY = "this key is read-only on the remote magpie; give the provider a key with full access to use a reset from here";
|
|
const LATE = "Office didn't say in time whether the reset was used; it may have been. Refresh the card to see before asking again";
|
|
|
|
function serve(lang, posted, answer) {
|
|
const settings = { theme: "light", lang, tray: "panel", quotaLeft: false, currency: "usd", trayUsages: ["office/codex", "office/zhipu-team"], trayUsageEvery: 3 };
|
|
return async (route) => {
|
|
const req = route.request(), url = new URL(req.url());
|
|
const json = (data, status = 200) => route.fulfill({ status, json: data });
|
|
if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:false};` });
|
|
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/usage/quotas") return json(quotas);
|
|
if (url.pathname === "/api/usage/codex-reset") {
|
|
posted.push(JSON.parse(req.postData() || "{}"));
|
|
return answer.error ? json({ error: answer.error }, 400) : json({ code: "reset", windows: 2 });
|
|
}
|
|
if (url.pathname === "/api/groups") return json({ groups: [], models: [] });
|
|
if (url.pathname === "/api/plugins") return json({ plugins: [] });
|
|
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).catch(() => ""), contentType });
|
|
};
|
|
}
|
|
|
|
const KEYS = ["The magpie on {computer}, where this account is signed in, uses it.",
|
|
"This key is read-only on that computer's magpie — give this provider a key with full access to use a reset from here",
|
|
"That computer didn't say in time whether the reset was used — it may have been. Refresh the card before asking again",
|
|
"That computer's magpie can't use a reset from here yet — update magpie there",
|
|
"That computer's magpie holds this key to a budget, models or accounts, so it uses none of its resets — give this provider a key with full access"];
|
|
|
|
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
|
|
for (const lang of ["en", "zh", "zh-TW", "ja", "de"]) {
|
|
test(`${engine} ${lang}: a remote magpie's Codex reset is used from here, asked first`, async (t) => {
|
|
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
|
|
t.after(() => browser.close());
|
|
const errors = [];
|
|
const say = (page, key, vars) => page.evaluate(([lang, key, vars]) => {
|
|
if (lang !== "en" && !I18N[lang]?.[key]) return "untranslated: " + key;
|
|
return t(key, vars);
|
|
}, [lang, key, vars]);
|
|
|
|
const run = async (url, viewport, useSel, cardSel) => {
|
|
const posted = [], answer = {};
|
|
const page = await (await browser.newContext({ viewport, reducedMotion: "reduce" })).newPage();
|
|
page.setDefaultTimeout(5000);
|
|
page.setDefaultNavigationTimeout(60000);
|
|
page.on("pageerror", (e) => errors.push(e.message));
|
|
page.on("dialog", (d) => { errors.push("a browser dialog: " + d.message()); d.dismiss(); });
|
|
await page.route("**/*", serve(lang, posted, answer));
|
|
await page.goto(url);
|
|
if (url.includes("panel")) await page.locator('#ptabs [data-ptab="usage"]').click();
|
|
for (const k of KEYS) assert.notEqual(await say(page, k, { computer: "x" }), "untranslated: " + k);
|
|
|
|
// the remote's Codex card offers its reset; the GLM team's doesn't
|
|
await page.locator(cardSel).first().waitFor();
|
|
assert.equal(await page.locator(useSel).count(), 1, "one Use, on the remote Codex card");
|
|
assert.equal(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth), true, "nothing wider than the page");
|
|
const ask = page.locator(".reset-ask");
|
|
await page.locator(useSel).click();
|
|
await ask.waitFor();
|
|
assert.equal((await ask.locator(".reset-remote").textContent()).trim(), await say(page, "The magpie on {computer}, where this account is signed in, uses it.", { computer: "Office" }));
|
|
assert.equal(await page.evaluate(() => { const e = document.querySelector(".reset-ask").getBoundingClientRect(); return e.right <= innerWidth && e.left >= 0; }), true, "the confirmation fits");
|
|
// Cancel spends nothing
|
|
await page.keyboard.press("Escape");
|
|
await ask.waitFor({ state: "detached" });
|
|
assert.equal(posted.length, 0, "nothing spent before the user says so");
|
|
|
|
// a read-only key there: said in the user's words, the sheet stays
|
|
answer.error = READ_ONLY;
|
|
await page.locator(useSel).click();
|
|
await ask.locator("button.primary").click();
|
|
await page.locator("#status.err").waitFor();
|
|
assert.equal((await page.locator("#status").textContent()).trim(), await say(page, KEYS[1]));
|
|
assert.equal(await ask.isVisible(), true, "the confirmation stays to try again");
|
|
// no answer in time: it may have been used
|
|
answer.error = LATE;
|
|
await ask.locator("button.primary").click();
|
|
await page.waitForFunction((want) => document.querySelector("#status")?.textContent.trim() === want, await say(page, KEYS[2]));
|
|
await page.keyboard.press("Escape");
|
|
await ask.waitFor({ state: "detached" });
|
|
|
|
// used: the remote card's provider and account are named
|
|
delete answer.error;
|
|
await page.locator(useSel).click();
|
|
await ask.locator("button.primary").click();
|
|
await ask.waitFor({ state: "detached" });
|
|
assert.deepEqual(posted, [
|
|
{ user: "me@example.com", provider: "office/codex" },
|
|
{ user: "me@example.com", provider: "office/codex" },
|
|
{ user: "me@example.com", provider: "office/codex" },
|
|
]);
|
|
await page.context().close();
|
|
};
|
|
|
|
for (const width of [900, 390]) await run("http://magpie.test/?view=usage", { width, height: 800 }, ".quota-resets button", ".quota-resets");
|
|
await run("http://magpie.test/?mode=panel", { width: 440, height: 700 }, ".pq-use", ".pq-use");
|
|
assert.deepEqual(errors, []);
|
|
});
|
|
}
|
|
}
|