The Usage page's auto-refresh topped out at a minute. A page left open
all day had no slower choice than reading again every 60 s. The picker
(the same app menu) now also offers 5 min, 15 min and 30 min.
The choice lives in localStorage (magpie.usageEvery) and is read back
only if it is one of USAGE_EVERY's values, so the new ones round-trip by
being in that list. Nothing on the Go side reads or clamps it. The labels
"5 min", "15 min" and "30 min" already have zh, zh-TW, ja and de
translations in i18n.js, so no new strings.
usage-refresh.test.cjs now runs in all five languages, checks the eight
choices in order, that half an hour reads none for 29 minutes and once by
31, and that another window opens on 30 min.
Run (mote-run):
- usage-refresh.test.cjs, Chromium and WebKit, en/zh/zh-TW/ja/de: green.
- The 195 GUI files that open the Usage page or its tray tabs, Chromium
and WebKit, every language they cover (node --test, concurrency 6,
mote at load ~35): 1100 ok, 11 red in 6 files. Rerun alone, here and
on an untouched origin/main 2f660ca86 copy: account-eye, account-mask
and context-map pass in both; codex-auto-reset (webkit de) and
tray-cell-click are red the same in both, and gateway-caller-keys
(webkit en) red on main only. All filed: #4790, #5215, #5052, #3951,
#3797, #2572, #3488.
- go test -run Budget ./internal/gui (TestCoreScriptBudget): ok.
- i18n-keys.test.cjs: green except "a plugin's words are its own",
red on origin/main too and filed (#5327, #5332, #5426, #5482); none of
its words are touched here. i18n-en-no-cjk: green.
Broken by hand: usage-refresh.test.cjs against the old page.js is red in
all five languages (the menu lists five, and 30 min isn't there).
Not tried: the real app on a page left open for half an hour.
Reported-by: 114514 (Discord)
154 lines
9.6 KiB
JavaScript
154 lines
9.6 KiB
JavaScript
// Run with Node's test runner and Playwright on the module path; see README.md.
|
|
// The Usage page reads its numbers again while it is looked at, as often as
|
|
// the reader says: every 5 s to begin with, or every 10 s, 30 s, a minute, 5, 15
|
|
// or 30 minutes (114514 on Discord: a minute was the longest), or never, from
|
|
// the picker beside the cost; the button beside it reads them now,
|
|
// on the tab shown (Overview or Requests), and says when they were last read.
|
|
// On the Overview it is the allowances' Refresh too, asking for them as
|
|
// opening the page does (#486); there is no second Refresh beside them.
|
|
// The choice is remembered. A clock stands in for time here, so nothing waits;
|
|
// 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(__dirname, "../assets");
|
|
const now = Date.now();
|
|
const ROWS = [{ t: new Date(now - 60e3).toISOString(), agent: "codex", agentName: "Codex", icon: "codex-color", provider: "relay", providerName: "Relay", host: "team", req: "sol", model: "gpt-6-sol", in: 100, out: 10, ms: 900, status: 200, cost: 0.01, priced: true }];
|
|
|
|
function server(lang, calls) {
|
|
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: { rate: 7.2, at: new Date().toISOString() } });
|
|
if (url.pathname === "/api/usage/requests") {
|
|
calls.requests++;
|
|
return json({ period: "30d", rows: ROWS, offset: 0, total: 1, calls: 1, errors: 0, input: 100, output: 10, cache_read: 0, cache_write: 0, reasoning: 0, cost: 0.01, unpriced: 0, bucket: "day", series: [], by: { provider: [], agent: [], model: [] }, agents: [], providers: [] });
|
|
}
|
|
if (url.pathname === "/api/usage") {
|
|
calls.overview++;
|
|
return json({ calls: 1, errors: 0, input: 100, output: 10, cache_read: 0, cache_write: 0, reasoning: 0, unpriced: 0, cost: 0.01 + calls.overview * 0.001, bucket: "day", series: [{ label: "Mon", input: 100, output: 10, calls: 1, cost: 0.01 }], agents: [{ name: "Codex", calls: 1, cost: 0.01 }], models: [{ name: "gpt-6-sol", calls: 1, cost: 0.01 }], path: "~/.config/magpie/usage.jsonl" });
|
|
}
|
|
if (url.pathname === "/api/usage/quotas") { calls.quotas++; if (url.searchParams.get("asked") === "1") calls.asked++; if (url.searchParams.get("all") === "1") calls.all++; return json([]); }
|
|
if (url.pathname === "/api/sessions") return json({ sessions: [], dirs: [] });
|
|
if (url.pathname === "/api/sessions/stats") return json({ from: "", to: "", days: [], agents: {} });
|
|
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 L = {
|
|
en: { label: "5 s", off: "Off", min: "1 min", half: "30 min", options: ["Off", "5 s", "10 s", "30 s", "1 min", "5 min", "15 min", "30 min"], now: "Refresh now" },
|
|
zh: { label: "5 秒", off: "关闭", min: "1 分钟", half: "30 分钟", options: ["关闭", "5 秒", "10 秒", "30 秒", "1 分钟", "5 分钟", "15 分钟", "30 分钟"], now: "立即刷新" },
|
|
"zh-TW": { label: "5 秒", off: "關閉", min: "1 分鐘", half: "30 分鐘", options: ["關閉", "5 秒", "10 秒", "30 秒", "1 分鐘", "5 分鐘", "15 分鐘", "30 分鐘"], now: "立即重新整理" },
|
|
ja: { label: "5 秒", off: "オフ", min: "1 分", half: "30 分", options: ["オフ", "5 秒", "10 秒", "30 秒", "1 分", "5 分", "15 分", "30 分"], now: "今すぐ更新" },
|
|
de: { label: "5 s", off: "Aus", min: "1 Min.", half: "30 Min.", options: ["Aus", "5 s", "10 s", "30 s", "1 Min.", "5 Min.", "15 Min.", "30 Min."], now: "Jetzt aktualisieren" },
|
|
};
|
|
|
|
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
|
|
for (const lang of Object.keys(L)) {
|
|
test(`${engine} ${lang}: the Usage page's refresh period`, async (t) => {
|
|
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
|
|
t.after(() => browser.close());
|
|
const w = L[lang];
|
|
const errors = [], calls = { requests: 0, overview: 0, quotas: 0, asked: 0, all: 0 };
|
|
const context = await browser.newContext({ viewport: { width: 1180, height: 700 }, reducedMotion: "reduce" });
|
|
const open = async () => {
|
|
const p = await context.newPage();
|
|
p.setDefaultTimeout(5000);
|
|
p.setDefaultNavigationTimeout(60000);
|
|
p.on("pageerror", (e) => errors.push(e.message));
|
|
await p.clock.install({ time: now });
|
|
await p.route("**/*", server(lang, calls));
|
|
await p.goto("http://magpie.test/");
|
|
return p;
|
|
};
|
|
// time passes, and the answers to what it asked come in
|
|
const pass = async (p, ms) => { await p.clock.fastForward(ms); await p.waitForTimeout(150); };
|
|
|
|
const p = await open();
|
|
await p.locator('[data-view="usage"]').first().click();
|
|
await p.locator(":is(#usageTab .opt, .nav-s):visible").nth(1).click(); // Requests
|
|
await p.locator("#ledWrap .led tbody tr").first().waitFor();
|
|
assert.equal((await p.locator("#usageEvery").textContent()).trim(), w.label, "every 5 s to begin with");
|
|
|
|
// every 5 s: another read after a while, and none before
|
|
let n = calls.requests;
|
|
await pass(p, 2000);
|
|
assert.equal(calls.requests, n, "not yet");
|
|
await pass(p, 4000);
|
|
assert(calls.requests > n, "read again after 5 s");
|
|
|
|
// the picker offers the eight; off reads no more
|
|
await p.locator("#usageEvery").click();
|
|
assert.deepEqual(await p.locator(".proto-menu .pm-item").allTextContents().then((a) => a.map((s) => s.trim())), w.options);
|
|
await p.locator(".proto-menu .pm-item", { hasText: new RegExp("^\\s*" + w.off + "\\s*$") }).click();
|
|
assert.equal((await p.locator("#usageEvery").textContent()).trim(), w.off);
|
|
n = calls.requests;
|
|
await pass(p, 120e3);
|
|
assert.equal(calls.requests, n, "off reads no more");
|
|
|
|
// a minute: not at half of it, once by the end
|
|
await p.locator("#usageEvery").click();
|
|
const pick = (name) => p.locator(".proto-menu .pm-item", { hasText: new RegExp("^\\s*" + name + "\\s*$") }).click();
|
|
await pick(w.min);
|
|
n = calls.requests;
|
|
await pass(p, 30e3);
|
|
assert.equal(calls.requests, n, "not at half a minute");
|
|
await pass(p, 35e3);
|
|
assert(calls.requests > n, "once by the end of it");
|
|
|
|
// the button reads at once, turns while it does, and says when
|
|
n = calls.requests;
|
|
await p.locator("#usageReload").click();
|
|
await p.waitForTimeout(200);
|
|
assert(calls.requests > n, "read on the click");
|
|
assert(!(await p.locator("#usageReload").evaluate((b) => b.classList.contains("busy"))), "done turning");
|
|
assert((await p.locator("#usageReload").getAttribute("title")).startsWith(w.now), await p.locator("#usageReload").getAttribute("title"));
|
|
assert(/\d{1,2}:\d{2}:\d{2}/.test(await p.locator("#usageReload").getAttribute("title")), "when they were read");
|
|
|
|
// on Overview it reads the summary, and the allowances are read now too
|
|
await p.locator(":is(#usageTab .opt, .nav-s):visible").nth(0).click();
|
|
await p.locator("#stats .kpi:not(.loading-kpi)").first().waitFor();
|
|
const o = calls.overview, q = calls.quotas, a = calls.asked, every = calls.all;
|
|
await p.locator("#usageReload").click();
|
|
await p.waitForTimeout(200);
|
|
assert(calls.overview > o, "the summary again");
|
|
assert(calls.quotas > q, "and the allowances");
|
|
// it is the allowances' Refresh too (#486): asked, as that one was, and
|
|
// there is no second Refresh beside them
|
|
assert(calls.asked > a, "the allowances asked for, a Claude account's by its /usage");
|
|
// every account's, a saved Claude account's too (#5380)
|
|
assert(calls.all > every, "every account's allowance asked for");
|
|
assert.equal(await p.locator("#quotaRefresh").count(), 0, "one refresh on the page");
|
|
const tip = await p.locator("#usageReload").getAttribute("title");
|
|
assert(tip.startsWith(w.now) && tip.includes("/usage"), tip);
|
|
|
|
// half an hour: none for 29 minutes, once by 31
|
|
await p.locator("#usageEvery").click();
|
|
await pick(w.half);
|
|
assert.equal((await p.locator("#usageEvery").textContent()).trim(), w.half);
|
|
n = calls.overview;
|
|
await pass(p, 29 * 60e3);
|
|
assert.equal(calls.overview, n, "not before half an hour");
|
|
await pass(p, 2 * 60e3);
|
|
assert(calls.overview > n, "once by the end of it");
|
|
|
|
// remembered: another window opens on the half hour
|
|
await p.locator('[data-view="usage"]').first().waitFor();
|
|
const again = await open();
|
|
await again.locator('[data-view="usage"]').first().click();
|
|
await again.locator("#usageEvery").waitFor();
|
|
assert.equal((await again.locator("#usageEvery").textContent()).trim(), w.half);
|
|
assert.deepEqual(errors, []);
|
|
});
|
|
}
|
|
}
|