Files
magpie/internal/gui/tests/reset-pick-card.test.cjs
T
yetone 323159f1cc usage: a Codex reset's confirmation picks which of the account's resets is used (01huadalang)
01huadalang on Discord: resets were used only automatically, or by hand
the one that runs out first. Which account was already the user's to pick
(each account's card has its own Use); which of its resets was not.

- Usage card's "Use a reset" and the menu bar panel's "Use one...": when
  the account holds two or more resets listed with their ids, the
  confirmation has a "Reset to use" pop-up (the app's menu, not <select>),
  soonest first and picked unless another is. Once another is picked it
  names the account and that reset ("The one used is me@'s reset that
  runs out ..."), and says the sooner one is kept and lost if unused.
  Escape closes the open menu first, not the confirmation.
- POST /api/usage/codex-reset takes an optional credit. The server reads
  the account's resets again and spends that credit only if it is still
  held; a gone one is refused and no other is spent in its place. With no
  credit it is as before (the soonest). Auto-use is unchanged.
- ResetCard carries the credit id; `magpie quota reset --card n` is the
  CLI's sibling, its prompt naming the reset.
- askReset's dialog moved out to resetask.js, imported when Use is
  pressed (like signsheets.js), so the core's scripts are 1,530,733 bytes,
  under the 1,533,105 budget with no raise (the pick alone took it 2,068
  over). Five keys in zh, zh-TW, ja, de.

Run on mote (mote-run), at the base 2597c9636:
- gofmt -l clean; go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./... ok.
- go test -tags nogui ./internal/provider ./internal/cli ok;
  ./internal/gui: only TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and
  TestColdTurnsTaken (#4510) red, both filed and the same on main;
  TestCoreScriptBudget ok after the move.
- GUI, Chromium and WebKit, 78/78 each: reset-pick-card (new: en, zh,
  zh-TW, ja, de; Usage at 900 and 390px, panel at 440px), codex-resets-used,
  account-every-reset, codex-reset-lead, reset-each-title, reset-providers,
  reset-reminder, panel-usage, old-webkit, i18n-keys, codex-auto-reset,
  codex-auto-reset-settings, warm-reset-asleep, quota-credits,
  usage-accounts.
- Red on the old code: reset-pick-card against origin/main's assets fails
  at "an account with three resets offers the pick"; TestUseCodexResetCard
  and TestCodexResetsCounted fail without the change.
- Broken by hand, each red then restored: the chosen credit ignored
  (spent [soon]); the held check skipped (a used credit spent); ids dropped
  from each; the credit left out of the POST; the Escape guard removed.

Again at the pushed head (rebased on 88372a39d): go vet ./... ok;
provider, cli ok; gui only #4514 red (TestColdTurnsTaken passed this
time); reset-pick-card, old-webkit, i18n-keys in Chromium 15/15.

Not run: the full go test ./... and the whole GUI suite. mote had two to
three other full Go suites and several whole GUI suites running the whole
time (load 120 to 250), and the WSL box too; one suite per machine.
Not tried: a real account's reset spent (none was, by design: fakes only),
so that Codex's consume takes a credit_id other than the soonest is
inferred from it being the same field the soonest is sent in.
2026-11-01 04:51:55 +08:00

173 lines
11 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// Which of a Codex account's resets is used is the user's to pick
// (01huadalang on Discord: there was only auto-use, and by hand the one
// that runs out first). The confirmation offers each reset the account
// holds in the app's menu, the soonest first and picked unless another is;
// once another is picked it names the account and that reset, says the
// sooner one is kept, and the use names that reset's credit. Picking none
// uses the one that runs out first as before, and an account with one
// reset, or whose resets weren't listed one by one, is offered no pick.
// Escape closes the open menu, not the confirmation. 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(), late = new Date(base + 20 * day).toISOString();
const windows = [{ name: "5 hours", used: 40, resetsAt: new Date(base + 3600e3).toISOString() }, { name: "7 days", used: 100, resetsAt: new Date(base + 3 * day).toISOString() }];
const quotas = [
{ provider: "codex", name: "Codex", icon: "codex-color", user: "me@example.com", plan: "Plus", windows,
resets: { count: 3, until: soon, each: [{ until: soon, id: "cr-soon" }, { until: late, id: "cr-late" }, { id: "cr-never" }] } },
{ provider: "codex", name: "Codex", icon: "codex-color", user: "one@example.com", plan: "Plus", windows,
resets: { count: 1, until: soon, each: [{ until: soon, id: "cr-only" }] } },
// resets counted, not listed with their ids (an older magpie's reading)
{ provider: "codex", name: "Codex", icon: "codex-color", user: "old@example.com", plan: "Plus", windows,
resets: { count: 2, until: soon, each: [{ until: soon }, { until: late }] } },
];
// serve answers with the one account asked for: several of one vendor's
// fold to a line each until opened, which isn't what this is about
function serve(lang, posted, user) {
const settings = { theme: "light", lang, tray: "panel", quotaLeft: false, currency: "usd", trayUsages: ["codex"], trayUsageEvery: 3 };
return async (route) => {
const req = route.request(), url = new URL(req.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: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.filter((q) => q.user === user));
if (url.pathname === "/api/usage/codex-reset") { posted.push(JSON.parse(req.postData() || "{}")); return 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 = ["Reset to use", "Used unless you pick another", "The one used is {who}'s reset that runs out {when}.",
"The one used is {who}'s reset that never runs out.", "The one that runs out first, {when}, is kept, and is lost if it isn't used by then.",
"Reset {n} runs out {when}", "Reset {n} never runs out", "The one used is the one that runs out first, {when}."];
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}: the confirmation picks which of the account's resets is used`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
const errors = [];
const open = async (url, viewport, posted, user) => {
const page = await (await browser.newContext({ viewport, reducedMotion: "reduce" })).newPage();
page.setDefaultTimeout(5000);
page.setDefaultNavigationTimeout(60000);
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", serve(lang, posted, user));
await page.goto(url);
return page;
};
// the words as the page says them, each one translated for lang
const say = (page, key, vars) => page.evaluate(([lang, key, vars]) => {
if (lang !== "en" && !I18N[lang]?.[key]) return "untranslated: " + key;
for (const k of ["when"]) if (vars?.[k]) vars[k] = new Date(vars[k]).toLocaleString(intlLang());
return t(key, vars);
}, [lang, key, vars]);
const run = async (url, viewport, useSel) => {
let posted = [];
const load = async (user) => {
posted = [];
const page = await open(url, viewport, posted, user);
if (url.includes("panel")) await page.locator('#ptabs [data-ptab="usage"]').click();
return page;
};
let page = await load("me@example.com");
for (const k of KEYS) assert.notEqual(await say(page, k, { who: "x", when: soon, n: 1 }), "untranslated: " + k);
const card = () => page.locator(useSel);
let ask = page.locator(".reset-ask");
let picker = ask.locator(".reset-pick .reset-card");
// picked: the later one, named with its account, the sooner one kept
await card().first().click();
await ask.waitFor();
assert.equal(await picker.count(), 1, "an account with three resets offers the pick");
assert.equal((await picker.textContent()).trim(), await page.evaluate(([d]) => t("Reset {n} runs out {when}", { n: 1, when: resetClock(new Date(d)) }), [soon]));
assert.equal((await ask.locator(".reset-which").textContent()).trim(), await say(page, "The one used is the one that runs out first, {when}.", { when: soon }));
assert.equal(await ask.locator(".reset-sooner").isVisible(), false);
// it fits the confirmation, however narrow
const fits = await page.evaluate(() => {
const ed = document.querySelector(".reset-ask").getBoundingClientRect(), p = document.querySelector(".reset-ask .reset-card").getBoundingClientRect();
return p.right <= ed.right + 0.5 && p.left >= ed.left - 0.5 && document.documentElement.scrollWidth <= innerWidth;
});
assert(fits, "the pick fits the confirmation");
await picker.click();
const items = page.locator(".proto-menu .pm-item");
assert.equal(await items.count(), 3, "each reset the account holds");
const names = await page.locator(".proto-menu .pm-item .pm-name").allTextContents();
assert.deepEqual(names, [
await page.evaluate(([d]) => t("Reset {n} runs out {when}", { n: 1, when: resetClock(new Date(d)) }), [soon]),
await page.evaluate(([d]) => t("Reset {n} runs out {when}", { n: 2, when: resetClock(new Date(d)) }), [late]),
await page.evaluate(() => t("Reset {n} never runs out", { n: 3 })),
], "soonest first, the one that never runs out last");
assert.equal(await page.locator(".proto-menu .pm-item.on").count(), 1);
assert.match(await page.locator(".proto-menu .pm-item.on").textContent(), new RegExp(await say(page, "Used unless you pick another")));
// Escape closes the menu and leaves the confirmation open
await page.keyboard.press("Escape");
await page.locator(".proto-menu").waitFor({ state: "detached" });
assert.equal(await ask.isVisible(), true, "Escape on the menu closed the confirmation");
await picker.click();
await items.nth(1).click();
assert.equal((await picker.locator("span").textContent()).trim(), await page.evaluate(([d]) => t("Reset {n} runs out {when}", { n: 2, when: resetClock(new Date(d)) }), [late]));
assert.equal((await ask.locator(".reset-which").textContent()).trim(), await say(page, "The one used is {who}'s reset that runs out {when}.", { who: "me@example.com", when: late }));
assert.equal((await ask.locator(".reset-sooner").textContent()).trim(), await say(page, "The one that runs out first, {when}, is kept, and is lost if it isn't used by then.", { when: soon }));
assert.equal(await ask.locator(".reset-sooner").isVisible(), true);
assert.equal(posted.length, 0, "nothing spent before the user says so");
await ask.locator("button.primary").click();
await ask.waitFor({ state: "detached" });
assert.deepEqual(posted, [{ user: "me@example.com", credit: "cr-late" }], "the picked reset is the one spent");
// the one that never runs out
await card().first().click();
await picker.click();
await items.nth(2).click();
assert.equal((await ask.locator(".reset-which").textContent()).trim(), await say(page, "The one used is {who}'s reset that never runs out.", { who: "me@example.com" }));
await ask.locator("button.primary").click();
await ask.waitFor({ state: "detached" });
assert.deepEqual(posted[1], { user: "me@example.com", credit: "cr-never" });
// none picked: the one that runs out first, as before
await card().first().click();
await ask.locator("button.primary").click();
await ask.waitFor({ state: "detached" });
assert.deepEqual(posted[2], { user: "me@example.com" }, "no pick names no credit");
// one reset, or resets not listed by id: nothing to pick
assert.equal(posted.length, 3);
await page.context().close();
for (const user of ["one@example.com", "old@example.com"]) {
page = await load(user);
ask = page.locator(".reset-ask");
picker = ask.locator(".reset-pick .reset-card");
await card().first().click();
await ask.waitFor();
assert.equal(await picker.count(), 0, user + " offers no pick");
assert.equal((await ask.locator(".reset-which").textContent()).trim(), await say(page, "The one used is the one that runs out first, {when}.", { when: soon }));
await page.keyboard.press("Escape");
await ask.waitFor({ state: "detached" });
assert.equal(posted.length, 0);
await page.context().close();
}
};
for (const width of [900, 390]) await run("http://magpie.test/?view=usage", { width, height: 800 }, ".quota-resets button");
await run("http://magpie.test/?mode=panel", { width: 440, height: 700 }, ".pq-use");
assert.deepEqual(errors, []);
});
}
}