Files
magpie/internal/gui/tests/codex-reset-lead.test.cjs
T
yetone b03a374763 gui: a subscription's resets, warm-up, check-in and Claude run are its editor's own fields under Advanced
The owner (11-09): the block of cards these lists made under Routing took
most of the provider editor. Each list is now a field of the editor's
Advanced, beside its own label (Resets, Warm up, Check in, Claude Code),
first in it, and a field with nothing to show is hidden from the first
draw, before the prefs have loaded. Settings' search still finds them:
the hit opens the editor with Advanced open, brings the row into sight
and lights it.

In the editor's narrower column a row's name and control share the first
line and its description goes under both (a grid), so a description that
grows after a click moves nothing under the pointer. An account's own
warm-up time goes under its Same/Own/Off picks, not before them, so
picking Own at 440 wide no longer pushes the picks a line down.

Tests:
- In the editor a click holds what was pressed where it was (modalHold),
  and the editor may scroll to do it, so checkin-account, warm-at-own and
  codex-reset-lead check the pressed row's place on the screen, and that
  the page itself never scrolls, instead of every scrollTop. Each target
  is brought into sight first, so Playwright's own scroll before a click
  isn't counted.
- reader.cjs's inEditor scrolls the editor's body with the wheel until
  the element is inside it and not covered, and checks again after the
  editor draws anew.
- settings-groups: Claude's editor has Claude Code's run row too; the
  Usage part names alertHead and alertList (missed by 7d177985f).
- codex-reset-lead's "lead set outside the choices" brings the pick into
  sight before opening it (red on origin/main too, #5054).

Paid for in the core budget by seven long comments cut to two lines each,
no raise.

Run (mote, at this tree):
- the 42 GUI files this reaches, plus settings-back, i18n-keys and gui-de,
  Chromium and WebKit: 855 pass, 8 red, each red on origin/main too and
  filed: plugin-move-unserved "Try again takes the models unticked"
  (#4392, #4410) and tencent-cloud's region strip (#4321, #4399).
- go test ./internal/gui -run 'TestCoreScriptBudget|TestI18n': ok.
Seen red on origin/main 53b2773d7 while checking and filed:
claude-tui-setting's "Route is already handled" (#5836) and
claude-lean-prompt-setting's WebKit page error (#5832).

Again after the rebase onto da8480326, at this commit: TestCoreScriptBudget
and TestI18n ok; provider-prefs, warm-at-own, checkin-account,
codex-reset-lead, settings-groups and i18n-keys 120 of 120.

Broken by hand, each red, then put back:
- the editor rows' grid rule removed: 16 red, warm-at-own 11 (both
  engines) and checkin-account 5 (WebKit), the pressed control moved.
- the own time's wrap-reverse removed: warm-at-own 8 red at 440 (Chromium
  and WebKit, a pick moved a line down).
- the search hit not opening Advanced: provider-prefs and settings-search
  32 of 38 red.
- the fields put before Advanced instead of in it: provider-prefs
  20 of 20 red.

Not tried: the real app window.
2026-11-10 07:08:02 +08:00

259 lines
16 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// How long before a Codex reset about to run out unused it is used (the
// owner, 10-30: 把提前用卡的时间做成可配置的): one pick under "Auto-use resets"
// in Codex's provider editor, for every account, from the app's menu
// (no native select). Unset it reads half an hour, the default; a pick
// posts settings/codex-reset-lead with its minutes and the row, and the
// "before one expires" titles, say the new lead. A value set outside the
// choices (the CLI's 45m) is shown and kept; one out of range (written by
// hand) reads as the half hour, as the gateway reads it. Custom… after the
// choices opens a field of minutes in the row (shangyuan0818 on Discord):
// Enter or leaving it posts the number, Escape posts nothing, and one out
// of range is said and not posted. 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, provPrefsProviders, openProvPrefs, inEditor } = require("./reader.cjs");
// scrolls: the page's scroll and where the lead's row is on the screen. In
// the editor a click holds what was pressed where it was (modalHold), so
// the editor's own scrollTop may change to keep the row still.
const scrolls = (page) => page.evaluate(() => [scrollY, Math.round(document.querySelector("#codexResetLeadRow .name").getBoundingClientRect().top)].join(","));
const assets = path.resolve(__dirname, "../assets");
function server(lang, users, posts, lead) {
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: users.slice(0, 1).map((u) => u.toLowerCase()), codexAutoResetOnly: {},
...(lead === undefined ? {} : { codexResetLead: lead }),
};
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-reset-lead") {
const body = req.postDataJSON();
posts.push(body);
cur = { ...cur, codexResetLead: body.minutes };
return json(cur);
}
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/providers") return json(provPrefsProviders(["codex"], { codex: "Codex" }));
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 langs = ["en", "zh", "zh-TW", "ja", "de"];
const users = ["alice@example.com", "a-much-longer-team-seat-address@some-company.example.org"];
const launch = (engine) => engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" });
async function open(t, engine, lang, width, lead, posts) {
const browser = await launch(engine);
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-reset-lead.png`) });
}
await browser.close();
});
page.setDefaultTimeout(5000);
page.setDefaultNavigationTimeout(60000);
const errors = [];
page.on("pageerror", (e) => { if (engine === "webkit" && /due to access control checks/.test(e.message)) return; errors.push(e.message); }); // a goto in WebKit can cut off a fetch (#1350)
await page.route("**/*", server(lang, users, posts, lead));
await watchSettingsReads(page);
await openProvPrefs(page, "codex");
await settingsDrawn(page);
await page.locator(".editor .prov-prefs #codexResetLeadRow").waitFor();
return { page, errors };
}
// what the page says a lead of m minutes is, in lang
const say = (page, lang, m) => page.evaluate(([lang, m]) => {
const n = m < 60 || m % 60 ? I18N[lang]?.["{n} min"] || "{n} min" : I18N[lang]?.["{n} h"] || "{n} h";
const lead = n.replace("{n}", m < 60 || m % 60 ? m : m / 60);
return (I18N[lang]?.["{lead} before"] || "{lead} before").replace("{lead}", lead);
}, [lang, m]);
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of langs) {
for (const width of [1100, 420]) {
test(`${engine} ${lang} ${width}: Codex's editor picks how early a reset about to expire is used`, async (t) => {
const posts = [];
const { page, errors } = await open(t, engine, lang, width, undefined, posts);
const row = page.locator("#codexResetLeadRow");
assert.equal(await row.isVisible(), true);
// translated, its name and its words
if (lang !== "en") {
const name = (await row.locator(".name").textContent()).trim();
assert.notEqual(name, "How early before one expires", "name translated");
assert.notEqual((await row.locator(".sub").textContent()).trim().slice(0, 20), "For every account wi", "words translated");
}
const pick = row.locator("button.reset-lead");
assert.equal((await pick.locator(".rl-say > .on").textContent()).trim(), await say(page, lang, 30), "unset is half an hour");
// under the accounts' switches, not between them
assert.equal(await page.evaluate(() => {
const own = [...document.querySelectorAll(".ar-own")].pop();
return !!(own.compareDocumentPosition(document.querySelector("#codexResetLeadRow")) & Node.DOCUMENT_POSITION_FOLLOWING);
}), true);
// the "before one expires" title says the lead
const exp = page.locator(".ar-own").first().locator('.ar-own-case[data-case="expiring"] .ar-own-say');
const half = await page.evaluate((lang) => (I18N[lang]?.["{n} min"] || "{n} min").replace("{n}", 30), lang);
assert((await exp.getAttribute("title")).includes(half), "the title says half an hour");
await inEditor(page, pick);
const top = await scrolls(page);
await pick.click();
const items = page.locator(".proto-menu .pm-item");
assert.equal(await items.count(), 12, "3 min to 24 h, then Custom…");
assert.equal(await page.locator(".proto-menu .pm-item.on .pm-name").textContent(), await say(page, lang, 30));
await page.locator(".proto-menu .pm-item", { hasText: await say(page, lang, 180) }).click();
for (let i = 0; i < 100 && !posts.length; i++) await page.waitForTimeout(20);
assert.deepEqual(posts, [{ minutes: 180 }]);
await page.waitForFunction((want) => document.querySelector("#codexResetLeadRow button.reset-lead .rl-say > .on")?.textContent.trim() === want, await say(page, lang, 180));
const title = await page.locator(".ar-own").first().locator('.ar-own-case[data-case="expiring"] .ar-own-say').getAttribute("title");
const hours = await page.evaluate((lang) => (I18N[lang]?.["{n} h"] || "{n} h").replace("{n}", 3), lang);
assert(title.includes(hours), `the title says 3 h: ${title}`);
assert.equal(await scrolls(page), top, "no click moved the page");
// the shortest, three minutes (shangyuan0818 on Discord asked for less than 15)
await pick.click();
await page.locator(`.proto-menu .pm-item .pm-name:text-is(${JSON.stringify(await say(page, lang, 3))})`).click(); // not "3 h before"
for (let i = 0; i < 100 && posts.length < 2; i++) await page.waitForTimeout(20);
assert.deepEqual(posts, [{ minutes: 180 }, { minutes: 3 }]);
await page.waitForFunction((want) => document.querySelector("#codexResetLeadRow button.reset-lead .rl-say > .on")?.textContent.trim() === want, await say(page, lang, 3));
assert.equal(await scrolls(page), top, "no click moved the page");
// Custom…: the last item, a field of minutes in the row, focused
const custom = await page.evaluate((lang) => I18N[lang]?.["Custom…"] || "Custom…", lang);
const own = page.locator("#codexResetLeadRow input.reset-lead-own");
const ownField = async () => {
await page.locator("#codexResetLeadRow button.reset-lead").click();
const last = page.locator(".proto-menu .pm-item").last();
assert.equal((await last.locator(".pm-name").textContent()).trim(), custom);
await last.click();
await own.waitFor();
assert.equal(await own.evaluate((i) => i === document.activeElement), true, "the field has the keys");
};
await ownField();
assert.equal(await own.inputValue(), "3", "it starts at the lead");
assert.equal(await own.getAttribute("inputmode"), "numeric");
const r = await own.boundingBox(), v = await page.locator(".editor .prov-prefs[data-list=codexResetList]").boundingBox();
assert(r.x >= v.x && r.x + r.width <= v.x + v.width + 1, "the field fits");
assert.equal(await scrolls(page), top, "opening the field moved nothing");
await own.fill("7");
await own.press("Enter");
for (let i = 0; i < 100 && posts.length < 3; i++) await page.waitForTimeout(20);
assert.deepEqual(posts.at(-1), { minutes: 7 });
await page.waitForFunction((want) => document.querySelector("#codexResetLeadRow button.reset-lead .rl-say > .on")?.textContent.trim() === want, await say(page, lang, 7));
// under the least: said, nothing posted, the lead kept
await ownField();
await own.fill("2");
await own.press("Enter");
await page.locator("#codexResetLeadRow button.reset-lead").waitFor();
assert.equal(posts.length, 3, "2 isn't posted");
assert.equal(await page.locator("#status.err").textContent(), await page.evaluate((lang) => I18N[lang]?.["Minutes is a whole number from 3 to 1440"] || "Minutes is a whole number from 3 to 1440", lang));
assert.equal((await page.locator("#codexResetLeadRow .rl-say > .on").textContent()).trim(), await say(page, lang, 7));
assert(await page.evaluate(() => RESET_LEADS[0] === 3 && RESET_LEADS.at(-1) === 1440), "the words say the range RESET_LEADS has");
// Escape: nothing posted
await ownField();
await own.fill("9");
await own.press("Escape");
await page.locator("#codexResetLeadRow button.reset-lead").waitFor();
assert.equal(posts.length, 3, "Escape posts nothing");
// leaving the field saves it
await ownField();
await own.fill("12");
const name = page.locator("#codexResetLeadRow .name");
await inEditor(page, name); // in sight first, so the click itself scrolls nothing
const before = await scrolls(page);
await name.click();
for (let i = 0; i < 100 && posts.length < 4; i++) await page.waitForTimeout(20);
assert.deepEqual(posts.at(-1), { minutes: 12 });
await page.waitForFunction((want) => document.querySelector("#codexResetLeadRow button.reset-lead .rl-say > .on")?.textContent.trim() === want, await say(page, lang, 12));
assert.equal(await scrolls(page), before, "no click moved the page");
// fits, no sideways scroll, no native select, no left border
const over = await page.evaluate(() => {
const view = document.querySelector(".editor .prov-prefs[data-list=codexResetList]").getBoundingClientRect();
return [...document.querySelectorAll("#codexResetLeadRow, #codexResetLeadRow *")]
.filter((e) => { const r = e.getBoundingClientRect(); return r.width && (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 row.evaluate((e) => getComputedStyle(e).borderLeftWidth), "0px");
assert.equal(await page.locator(".editor select").count(), 0, "no native select");
assert.deepEqual(errors, []);
});
}
}
test(`${engine}: a lead set outside the choices is shown and kept; one out of range reads as half an hour`, async (t) => {
const posts = [];
const { page, errors } = await open(t, engine, "en", 900, 45, posts);
const pick = page.locator("#codexResetLeadRow button.reset-lead");
assert.equal((await pick.locator(".rl-say > .on").textContent()).trim(), "45 min before");
await inEditor(page, pick);
await pick.click();
await page.locator(".proto-menu .pm-item").first().waitFor();
assert.equal(await page.locator(".proto-menu .pm-item").count(), 13);
assert.equal(await page.locator(".proto-menu .pm-item.on .pm-name").textContent(), "45 min before");
await page.keyboard.press("Escape");
assert.deepEqual(posts, [], "nothing written by looking");
assert.deepEqual(errors, []);
const bad = await open(t, engine, "en", 900, 2, []);
assert.equal((await bad.page.locator("#codexResetLeadRow button.reset-lead .rl-say > .on").textContent()).trim(), "30 min before");
assert.deepEqual(bad.errors, []);
});
test(`${engine}: with no Codex account the lead isn't there`, async (t) => {
const browser = await launch(engine);
const page = await (await browser.newContext({ viewport: { width: 900, height: 600 }, reducedMotion: "reduce" })).newPage();
t.after(() => browser.close());
page.setDefaultTimeout(5000);
page.setDefaultNavigationTimeout(60000);
await page.route("**/*", server("en", [], [], undefined));
await watchSettingsReads(page);
await openProvPrefs(page, "codex");
await settingsDrawn(page);
await page.locator(".editor .prov-prefs #warmSegs .opt").first().waitFor();
assert.equal(await page.locator("#codexResetLeadRow").isVisible(), false);
});
}