Files
magpie/internal/gui/tests/codex-auto-reset-settings.test.cjs
T
yetone 00afcf97a1 provider, gui, cli: a Codex account's auto-use of its resets is two switches, its week used up and one about to expire (#2078, alanwalk)
alanwalk asked for "自动使用重置卡" to be split: they want a reset that is
about to expire used rather than lost, but a used-up week waited out rather
than spending a reset on it (and the other way round for others).

What changed:
- settings: codexAutoResetOnly narrows an account in codexAutoReset to
  "week" or "expiring". An account with no entry keeps both, so every
  existing config means what it meant: nothing to migrate, and no user
  value is rewritten. Load drops entries for accounts not in the list or
  with an unknown value.
- provider: CodexAutoResetWhen(user) reads both times. AutoUseCodexReset
  (the week used up, called from the gateway's autoReset and
  autoResetSignedIn through AutoResetsWeek) only acts when the week is on;
  SpendExpiringCodexResets and routing's resetRunsOut only count on the
  expiring reset when that is on. SetCodexAutoResetCase(user, when, on)
  sets one time and keeps the other; SetCodexAutoReset still sets both.
  Both off removes the account.
- gateway: POST /api/settings/codex-auto-reset takes an optional "when";
  POST /api/settings keeps codexAutoResetOnly, which the page doesn't send.
- GUI: the account's card (Usage page and the menu bar panel) says
  "Auto-use resets:" with each time on its own line and its own switch;
  the brief account's one switch still sets both, its title naming each
  time; Settings › Usage gives each account an Off/On per time. The status
  line says which times are on now. "· auto-used before it expires" on
  the resets held follows the expiring switch alone. Strings in zh, zh-TW,
  ja and de.
- CLI/TUI: `magpie quota auto-reset <account> week|expiring on|off`; the
  quota and accounts tables and the TUI say "auto", "auto when the week
  runs out" or "auto before one expires".

What was run (worktree on origin/main 2fbbd1694):
- gofmt -l on the changed Go files: clean. go vet ./... (darwin),
  GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...: clean.
- go test -tags nogui ./internal/provider ./internal/gateway ./internal/cli
  ./internal/tui ./internal/gui ./internal/settings: ok. The gateway
  package hit go test's 10m default once while ~15 sibling sessions were
  building on this machine (no test failed; TestRemoteMagpieModels was
  merely running); with -timeout 30m it passed in 967s.
- New Go tests: TestCodexAutoResetCases (provider), a week-only step in
  TestSpendExpiringCodexResets, an "only before one expires" case in
  TestCodexAutoResetNot (gateway), CodexAutoResetOnly in
  TestSettingsSaveKeepsWhatItDoesNotSend. Broken by hand: making
  CodexAutoResetWhen ignore the narrowing turned all three reset tests
  red (the gateway case answered 200, a week-only account read 2 and
  spent 1); dropping the keep line in api.go turned the settings test red.
- GUI (Playwright, chromium and webkit): codex-auto-reset.test.cjs, now in
  en, zh, zh-TW, ja and de at 1100 and 420 wide, flips each time's switch
  on the card and the panel and checks posts carry "when", the other time
  is kept, the status, the resets-held line, the brief switch, fit at 420,
  no left border, no select; codex-auto-reset-settings.test.cjs does the
  same for Settings › Usage. Both, with codex-resets-used, routing-auto-reset,
  quota-curve (its class list now holds ar-split) and i18n-keys: 56 + 18
  pass. Broken by hand: posting without "when" turned 20 of 21 red.
  quota-curve failed 2 of 18 once while a go test build ran beside it; it
  passed 15/15 serially and 18/18 again at concurrency 4 just after.

Not tried: a real ChatGPT account using or keeping a reset; the times are
driven with the faked usage reads the existing tests use.
2026-10-14 16:37:59 +08:00

169 lines
10 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Run with Node's test runner and Playwright on the module path; see README.md.
// Codex resets used by themselves (foobra12 on Discord: 自动给我使用了一张, 这个
//设置在哪儿找呢; Avalon: only the tray's quota showed the switch). Settings ›
// Usage lists each signed-in Codex account under "Codex: auto-use resets"
// with an Off/On for each of the two times it uses one, its week used up
// and one about to expire (#2078, alanwalk): Off for every account to begin
// with, a pick posts settings/codex-auto-reset for that account and that
// time and turns that switch alone, and with no Codex account the row isn't
// there. Five languages, 1100 and 420 wide,
// both engines; the API is faked here.
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 { watchSettingsReads, settingsDrawn, reach } = require("./reader.cjs");
const assets = path.resolve(__dirname, "../assets");
function server(lang, users, posts) {
const fixed = {
theme: "light", lang, tray: "panel", quotaLeft: false, currency: "usd", dock: false, dockWindow: false, proxy: "",
redact: false, redactPersonal: false, redactWords: [], codexWarmup: "", claudeWarmup: "", codexWarmAt: "", claudeWarmAt: "",
trayUsage: "", trayUsageEvery: 3, vision: "", imageGen: "", version: "0.1.900", dir: "~/.config/magpie",
gateway: "http://127.0.0.1:3425", proxyNow: "none", proxySource: "none", visionModels: [], imageGenModels: [], lanURLs: [],
fx: { rate: 7.2, at: new Date().toISOString(), stale: false },
codexUsers: users, codexAutoReset: [], codexAutoResetOnly: {},
};
let cur = fixed;
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: { lang, theme: "light" }, fx: fixed.fx });
if (url.pathname === "/api/settings/codex-auto-reset") {
const body = req.postDataJSON();
posts.push(body);
// as the settings keep it: on, and narrowed to one time or not
const u = body.user.toLowerCase(), only = { ...cur.codexAutoResetOnly };
const was = cur.codexAutoReset.includes(u) ? { week: only[u] !== "expiring", expiring: only[u] !== "week" } : { week: false, expiring: false };
const w = body.when ? { ...was, [body.when]: body.on } : { week: body.on, expiring: body.on };
const on = cur.codexAutoReset.filter((x) => x !== u);
delete only[u];
if (w.week || w.expiring) on.push(u);
if (w.week !== w.expiring) only[u] = w.week ? "week" : "expiring";
cur = { ...cur, codexAutoReset: on, codexAutoResetOnly: only };
return json(cur);
}
if (url.pathname === "/api/settings") {
if (req.method() === "POST") cur = { ...cur, ...req.postDataJSON() };
return json(cur);
}
if (url.pathname === "/api/plugins") return json({ plugins: [] });
if (url.pathname === "/api/usage/quotas") return json([]);
if (url.pathname === "/api/groups") return json({ groups: [], models: [] });
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: { name: "Codex: auto-use resets", off: "Off", on: "On", week: "when its week runs out", expiring: "before one expires" },
zh: { name: "Codex:自动使用重置卡", off: "关闭", on: "开启", week: "周额度用完时", expiring: "重置卡到期前" },
"zh-TW": { name: "Codex:自動使用重置卡", off: "關閉", on: "開啟" },
ja: { name: "Codex:リセットを自動使用", off: "オフ", on: "オン" },
de: { name: "Codex: Resets automatisch einlösen", off: "Aus", on: "An" },
};
const users = ["alice@example.com", "a-much-longer-team-seat-address@some-company.example.org"];
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of Object.keys(words)) {
for (const width of [1100, 420]) {
test(`${engine} ${lang} ${width}: Settings › Usage turns each Codex account's auto-use resets on and off`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
const page = await (await browser.newContext({ viewport: { width, height: 700 }, reducedMotion: "reduce" })).newPage();
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}-${lang}-${width}-codex-auto-reset-settings.png`) });
}
await browser.close();
});
page.setDefaultTimeout(5000);
const errors = [], posts = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", server(lang, users, posts));
const posted = async (n) => { for (let i = 0; i < 100 && posts.length < n; i++) await page.waitForTimeout(20); assert.equal(posts.length, n, "posts"); return posts[n - 1]; };
const w = words[lang];
await watchSettingsReads(page);
await page.goto("http://magpie.test/?view=settings&tab=usage");
await settingsDrawn(page);
const head = page.locator("#codexAutoResetRow");
await head.waitFor();
assert.equal((await head.locator(".name").textContent()).trim(), w.name);
const w2 = { ...w, ...(w.week ? {} : await page.evaluate((lang) => ({ week: I18N[lang]["when its week runs out"], expiring: I18N[lang]["before one expires"] }), lang)) };
assert(w2.week && w2.expiring, "both times are translated");
const rows = page.locator(".ar-own");
const at = (i, c) => page.locator(".ar-own").nth(i).locator(`.ar-own-case[data-case="${c}"]`);
assert.deepEqual(await rows.locator(".name").allTextContents(), users);
for (let i = 0; i < users.length; i++) {
assert.deepEqual(await rows.nth(i).locator(".ar-own-say").allTextContents(), [w2.week, w2.expiring], "each time its own line");
for (const c of ["week", "expiring"]) {
assert.deepEqual(await at(i, c).locator(".opt").allTextContents(), [w.off, w.on]);
assert.equal(await at(i, c).locator(".opt.on").textContent(), w.off, "off to begin with");
}
}
// the reader wheels down to the second account's before-it-expires On,
// past the window's foot: that time alone turns, its week kept off (#2078)
await reach(page, at(1, "expiring").locator(".opt", { hasText: w.on }));
const top = await page.locator("#view-settings").evaluate((v) => v.scrollTop);
await at(1, "expiring").locator(".opt", { hasText: w.on }).click();
assert.deepEqual(await posted(1), { user: users[1], on: true, when: "expiring" });
await page.waitForTimeout(300);
assert.equal(await at(1, "expiring").locator(".opt.on").textContent(), w.on);
assert.equal(await at(1, "week").locator(".opt.on").textContent(), w.off, "its week kept off");
for (const c of ["week", "expiring"]) assert.equal(await at(0, c).locator(".opt.on").textContent(), w.off, "only that account");
assert.equal(await page.locator("#view-settings").evaluate((v) => v.scrollTop), top, "no click moved the page");
// every row fits: nothing runs past the settings view
const over = await page.evaluate(() => {
const view = document.querySelector("#view-settings").getBoundingClientRect();
return [...document.querySelectorAll("#codexAutoResetRow, .ar-own, .ar-own-case, .ar-own-say, .ar-own .opt")]
.filter((e) => { const r = e.getBoundingClientRect(); return r.right > view.right + 1 || r.left < view.left - 1; })
.map((e) => e.className + " " + e.textContent.slice(0, 30));
});
assert.deepEqual(over, []);
assert.equal(await page.evaluate(() => document.documentElement.scrollWidth <= window.innerWidth), true, "no sideways scroll");
assert.equal(await head.evaluate((e) => getComputedStyle(e).borderLeftWidth), "0px");
assert.equal(await page.locator("#view-settings select").count(), 0, "no native select");
// the week on too, then the expiring off: the week kept
await reach(page, at(1, "week").locator(".opt", { hasText: w.on }));
await at(1, "week").locator(".opt", { hasText: w.on }).click();
assert.deepEqual(await posted(2), { user: users[1], on: true, when: "week" });
await page.waitForTimeout(300);
await reach(page, at(1, "expiring").locator(".opt", { hasText: w.off }));
await at(1, "expiring").locator(".opt", { hasText: w.off }).click();
assert.deepEqual(await posted(3), { user: users[1], on: false, when: "expiring" });
await page.waitForTimeout(300);
assert.equal(await at(1, "expiring").locator(".opt.on").textContent(), w.off);
assert.equal(await at(1, "week").locator(".opt.on").textContent(), w.on, "the week kept on");
assert.deepEqual(errors, []);
});
}
}
test(`${engine}: with no Codex account the row isn't there`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
const page = await (await browser.newContext({ viewport: { width: 900, height: 600 }, reducedMotion: "reduce" })).newPage();
t.after(() => browser.close());
page.setDefaultTimeout(5000);
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", server("en", [], []));
await watchSettingsReads(page);
await page.goto("http://magpie.test/?view=settings&tab=usage");
await settingsDrawn(page);
await page.locator("#resetReminderSegs .opt").first().waitFor();
assert.equal(await page.locator("#codexAutoResetRow").isVisible(), false);
assert.equal(await page.locator(".ar-own").count(), 0);
assert.deepEqual(errors, []);
});
}