Files
magpie/internal/gui/tests/providers-reset-use.test.cjs
T
yetone c0477aa728 providers, settings: a Codex account's resets are used where they are shown, its Providers row and Settings' auto-use rows (roamlog, husky_bobo on X)
roamlog on X (使用重置卡的功能哪去了): the Providers page's Codex editor
showed "↺ 2 张重置卡" on each ChatGPT account, windows at 0% left, and no
way to use one. Nothing was removed: the use (askReset, resetask.js) was
only ever on the Usage card ("Use a reset") and the menu bar panel ("Use
one…"); the Providers row's chip (#813) was words. husky_bobo (X, Windows
10, v0.1.2597, 为什么我没有这个手动用卡按钮呢?) looked for it in Settings ›
Usage, under "Codex: auto-use resets" with both times on, where it never
was either.

- Providers editor: a Codex account's "↺ N resets" is a button (a span
  with role=button, the window meters' aq-set look and hover, no colour of
  its own; Enter and Space too) opening the same confirmation, its
  tooltip saying which reset a use spends. A GLM team's resets, counted
  by window and used on the vendor's page, stay words.
- The confirmation asked from the editor goes back to it on Cancel or
  once spent (ed.back, as Sign out's does): the editor is the modal, and
  closing would have closed it too.
- Settings › Usage › Codex: auto-use resets: each account holding resets
  says "↺ N resets · until … · auto-used before it expires" under its
  name with "Use a reset" beside it, read as the editor reads them
  (loginUsageOf, cached a minute), drawn again once spent. In
  autoresetown.js, loaded with those rows.
- No new strings. The core's scripts are 1,533,441 bytes, 305 under
  their size before, budget 1,533,802 not raised: a span-as-button helper
  (spanBtn) shared with windowCapMeter, and the reset helpers' comments
  cut down.

Run, at base 9d08993c8:
- mote (mote-run): providers-reset-use.test.cjs (new: 40 cases, Chromium
  and WebKit, en/zh/zh-TW/ja/de, 420 and 1000/1100 px; Providers row
  click and keyboard, Cancel spends nothing and keeps the editor, a use
  posts {user} alone and the row reads 1, the GLM chip inert, no page
  move; Settings as the Windows app (navigator.platform Win32, web:false)
  with husky_bobo's settings, both times on), reset-providers,
  codex-auto-reset-settings, codex-reset-lead, codex-auto-reset: 114 of
  114 pass. go test -tags nogui -run 'Budget|I18n|Asset|Script'
  ./internal/gui ok. gofmt -l clean.
- Whole GUI suite: on the Linux box (wsl-run, magpie-desktop), since
  mote had three other sessions' full GUI suites running; started before
  the push, its result follows in the report, reds filed on their own.

Broken by hand (red, then put back), on mote:
- origin/main's assets: the Providers cases red ("the resets are words
  only, nothing uses one"), the Settings cases red (no .ar-own-resets).
- origin/main's resetask.js with the rest of this change: Cancel closes
  the editor (chip gone), and Settings still says 2 resets after a use.

Not tried: a real reset was not spent (fixtures only); not run in the
real Windows app on xiaomi-gpu, only with Windows' navigator.platform.
2026-11-01 20:55:02 +08:00

198 lines
13 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.
// A Codex account's "↺ 2 resets" on its Providers editor row uses one, as
// Usage's "Use a reset" does (roamlog on X: 使用重置卡的功能哪去了 — the
// row showed the resets and offered no way to use them). Clicked, or Enter
// on it, it opens the same confirmation; Cancel spends nothing; confirmed,
// the reset is asked for that account alone and the row reads again. A
// GLM team's resets, counted by window and used on the vendor's page, stay
// words. Settings › Usage's "Codex: auto-use resets" lists the same
// accounts, and husky_bobo (X, Windows 10) looked for the use there: each
// account holding resets says so under its name with Use beside it, both
// auto-use times on as theirs were, as the Windows app (navigator.platform
// Win32, not a web page). Every language, Chromium and WebKit, a phone's
// width and a desk's; no backend, the API is faked here and nothing real is
// spent.
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 } = require("./reader.cjs");
const assets = path.resolve(process.env.ASSET_DIR || path.resolve(__dirname, "../assets"));
const later = new Date(Date.now() + 3 * 864e5).toISOString();
const user = "me@example.com";
function serve(lang, posts, web = true) {
let spent = false;
const windows = () => [{ name: "5 hours", used: spent ? 0 : 100, resetsAt: later }, { name: "7 days", used: spent ? 0 : 62, resetsAt: later }];
const prov = (id, name, agent, plan) => ({
id, name, icon: id, chat: "", responses: "", anthropic: "", catalog: "",
models: [{ id: "m-1", name: "", on: true }], agents: [], fallback: [], headers: {}, keyList: [], proxy: "",
account: { agent, agentName: name, user, plan, logins: [{ user, plan, active: true, on: true }] },
});
const providers = [prov("codex", "Codex", "codex", "PLUS"), prov("zcode", "GLM Team", "zcode", "TEAM")];
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:${web}};` });
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", quotaLeft: true, codexAutoReset: [user], codexAutoResetOnly: {} } });
// husky_bobo's: both times on for the account
if (url.pathname === "/api/settings") return json({ lang, theme: "light", quotaLeft: true, version: "0.1.2597", dir: "~/.config/magpie", gateway: "http://127.0.0.1:3425", fx: { rate: 7.2, at: new Date().toISOString(), stale: false }, codexUsers: [user], codexAutoReset: [user], codexAutoResetOnly: {}, codexResetLead: 30 });
if (url.pathname === "/api/providers") return json({ providers, presets: [], excluded: [], gateway: { running: true, window: true } });
if (url.pathname === "/api/login/usage") {
const agent = url.searchParams.get("agent");
if (agent === "codex") return json({ [user]: { provider: "codex", windows: windows(), resets: { count: spent ? 1 : 2, until: later } } });
if (agent === "zcode") return json({ [user]: { provider: "zcode", windows: windows(), resets: { byWindow: true, count: 2, fiveHour: 1, weekly: 1, team: true } } });
return json({});
}
if (url.pathname === "/api/usage/codex-reset") {
posts.push(req.postDataJSON());
spent = true;
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(url.pathname === "/api/usage/quotas" ? [] : {});
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 });
};
}
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of ["en", "zh", "zh-TW", "ja", "de"]) {
for (const width of [420, 1000]) {
test(`${engine} ${lang} ${width}px: a Codex account's resets on its Providers row use one`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
const page = await (await browser.newContext({ viewport: { width, height: 800 }, reducedMotion: "reduce" })).newPage();
page.setDefaultTimeout(5000);
page.setDefaultNavigationTimeout(60000);
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
const posts = [];
await page.route("**/*", serve(lang, posts));
await page.goto("http://magpie.test/?view=providers");
const w = await page.evaluate((lang) => {
const tr = (s) => (lang === "en" ? s : I18N[lang]?.[s]);
return { two: "↺ " + tr("{n} resets")?.replace("{n}", 2), one: "↺ " + tr("1 reset"), use: tr("Use a reset"), ask: tr("Use a Codex reset?"), cancel: tr("Cancel") };
}, lang);
for (const [k, v] of Object.entries(w)) assert.ok(v && !v.includes("undefined"), `${lang} has no words for ${k}`);
await page.locator(".row.provider", { hasText: "Codex" }).first().click();
const chip = page.locator(`.editor .acc[data-account-id="${user}"] .aq-resets`);
await chip.waitFor();
assert.equal((await chip.textContent()).trim(), w.two);
assert.equal(await chip.getAttribute("role"), "button", "the resets are words only, nothing uses one");
assert.equal(await chip.getAttribute("aria-haspopup"), "dialog");
// its tooltip says which one a use spends, before anything is asked
assert.match(await chip.getAttribute("title"), /\n\n/);
// the same quiet look as the window meters' buttons: no colour of its own
const look = await chip.evaluate((n) => { const s = getComputedStyle(n); return { color: s.color, border: s.borderLeftWidth, bg: s.backgroundColor }; });
const meterColor = await page.locator(`.editor .acc[data-account-id="${user}"] .aq`).evaluate((n) => getComputedStyle(n).color);
assert.equal(look.color, meterColor);
assert.equal(look.border, "0px");
assert.equal(look.bg, "rgba(0, 0, 0, 0)");
// the row stays one line high where the meters fit: the chip is no taller than the meters
const box = await chip.boundingBox();
assert.ok(box.height < 24, `chip is ${box.height}px high`);
// Cancel spends nothing
const y = await page.evaluate(() => document.scrollingElement.scrollTop);
await chip.click();
const ask = page.locator(".reset-ask");
await ask.waitFor();
assert.equal((await ask.locator(".ehead b").textContent()).trim(), w.ask);
assert.ok((await ask.textContent()).includes(user), "the confirmation doesn't name the account");
await ask.locator("button", { hasText: w.cancel }).click();
await ask.waitFor({ state: "detached" });
assert.deepEqual(posts, []);
assert.equal(await page.evaluate(() => document.scrollingElement.scrollTop), y, "the click moved the page");
// the editor is still open: the click didn't reach the row behind it
await chip.waitFor();
// from the keyboard, then confirmed: that account's reset, once
await chip.focus();
await page.keyboard.press("Enter");
await ask.waitFor();
await ask.locator("button.primary", { hasText: w.use }).click();
await ask.waitFor({ state: "detached" });
assert.deepEqual(posts, [{ user }]);
await page.waitForFunction((one) => document.querySelector('.editor .acc[data-account-id="me@example.com"] .aq-resets')?.textContent.trim() === one, w.one, { timeout: 3000 })
.catch(async () => assert.fail(`the row still says ${(await chip.textContent()).trim()}`));
// a GLM team's resets are used on the vendor's page: words only
await page.keyboard.press("Escape");
await page.locator(".editor").waitFor({ state: "detached" });
await page.locator(".row.provider", { hasText: "GLM Team" }).first().click();
const glm = page.locator(`.editor .acc[data-account-id="${user}"] .aq-resets`);
await glm.waitFor();
assert.equal(await glm.getAttribute("role"), null);
await glm.click();
assert.equal(await page.locator(".reset-ask").count(), 0);
assert.equal(posts.length, 1);
assert.deepEqual(errors, []);
});
}
}
}
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of ["en", "zh", "zh-TW", "ja", "de"]) {
for (const width of [420, 1100]) {
test(`${engine} ${lang} ${width}px Windows: Settings' Codex auto-use rows use a reset`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
const page = await (await browser.newContext({ viewport: { width, height: 800 }, reducedMotion: "reduce" })).newPage();
page.setDefaultTimeout(5000);
page.setDefaultNavigationTimeout(60000);
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
const posts = [];
await page.addInitScript(() => Object.defineProperty(navigator, "platform", { value: "Win32" }));
await page.route("**/*", serve(lang, posts, false));
await watchSettingsReads(page);
await page.goto("http://magpie.test/?view=settings&tab=usage");
await settingsDrawn(page);
const w = await page.evaluate((lang) => {
const tr = (s) => (lang === "en" ? s : I18N[lang]?.[s]);
return { two: "↺ " + tr("{n} resets")?.replace("{n}", 2), one: "↺ " + tr("1 reset"), use: tr("Use a reset"), cancel: tr("Cancel"), kept: tr("auto-used before it expires") };
}, lang);
for (const [k, v] of Object.entries(w)) assert.ok(v && !v.includes("undefined"), `${lang} has no words for ${k}`);
const row = page.locator(`.ar-own[data-user="${user}"]`);
const held = row.locator(".ar-own-resets");
await held.waitFor();
assert.equal((await held.locator(".resets-n").textContent()).trim(), w.two);
assert.ok((await held.textContent()).includes(w.kept), "doesn't say the one running out is auto-used");
const use = held.locator("button", { hasText: w.use });
// nothing runs past the settings view
const over = await page.evaluate(() => {
const v = document.querySelector("#view-settings").getBoundingClientRect();
return [...document.querySelectorAll(".ar-own, .ar-own-resets, .ar-own-resets button")].filter((n) => n.getBoundingClientRect().right > v.right + 1).length;
});
assert.equal(over, 0, "a row runs past the view");
await use.click();
const ask = page.locator(".reset-ask");
await ask.waitFor();
// where the click left it (Playwright scrolls the button into view)
const top = await page.locator("#view-settings").evaluate((v) => v.scrollTop);
await ask.locator("button", { hasText: w.cancel }).click();
await ask.waitFor({ state: "detached" });
assert.deepEqual(posts, []);
assert.equal(await page.locator("#view-settings").evaluate((v) => v.scrollTop), top, "Cancel moved the page");
await use.click();
await ask.waitFor();
await ask.locator("button.primary", { hasText: w.use }).click();
await ask.waitFor({ state: "detached" });
assert.deepEqual(posts, [{ user }]);
await page.waitForFunction(({ one, user }) => document.querySelector(`.ar-own[data-user="${user}"] .ar-own-resets .resets-n`)?.textContent.trim() === one, { one: w.one, user }, { timeout: 3000 })
.catch(async () => assert.fail(`Settings still says ${(await held.textContent()).trim()}`));
assert.equal(await page.locator("#view-settings").evaluate((v) => v.scrollTop), top, "a click moved the page");
assert.deepEqual(errors, []);
});
}
}
}