gui: a reset's time is said by a formatter kept per language, not made each call (#5310)
resetClock and resetText called toLocaleTimeString/toLocaleDateString/ toLocaleString, which build a new Intl formatter on every call. The Usage page says every account window's reset on each render, so 600 accounts made thousands of formatters per render. dateFmt(opts) in i18n.js keeps one Intl.DateTimeFormat per intlLang() and options. resetText's past time gives toLocaleString's default fields (year/month/day/hour/minute/second numeric) by name, which is what toLocaleString uses when given no options. Probe (mote, 600 accounts open, W=520, names wrapping, 8 renders, median sync ms, before → after): WebKit ~431 → ~377 Chromium ~327 → ~279 Test: reset-clock-formats.test.cjs compares resetClock and resetText with what the old toLocale*String code said word for word. It covers a time every 5h from a week back to eight days ahead, in en, zh, zh-TW, ja, de and then system (a language switch after the cache is warm), Chromium and WebKit, in a Tokyo time zone with an en-GB system. 2/2 green. Broken by hand: a cache key without the language is red in Chromium (zh said "02/11/2026, 20:29:01" for "2026/11/2 20:29:01"). Run at this change on74017e0b8(mote): TestCoreScriptBudget ok. The 40 GUI files that say a reset time ("Resets", "Reset time", "tomorrow", 重置) plus i18n-keys: 770 tests, 3 red. The reds are codex-auto-reset webkit de 1100/420 (red the same at its base40cfae5d3, #4790) and i18n-keys 'a plugin's words are its own' (#5327 #5332 #5426 #5482). Not tried: the other ~37 toLocale*String calls in app.js. They run once per page or row, not once per account window.
This commit is contained in:
@@ -14551,14 +14551,13 @@ async function setQuotaLeft(on) {
|
||||
// "tomorrow 09:00", "Thu 14:30" later this week (Monday to Sunday), else
|
||||
// "Oct 12 08:05": a bare weekday in next week read as this week's (#181).
|
||||
function resetClock(at, now = new Date()) {
|
||||
const lang = intlLang();
|
||||
const time = at.toLocaleTimeString(lang, { hour: "2-digit", minute: "2-digit", hour12: false });
|
||||
const time = dateFmt({ hour: "2-digit", minute: "2-digit", hour12: false }).format(at);
|
||||
const day = (d) => new Date(d.getFullYear(), d.getMonth(), d.getDate());
|
||||
const days = Math.round((day(at) - day(now)) / 864e5);
|
||||
if (days <= 0) return time;
|
||||
if (days === 1) return t("tomorrow {time}", { time });
|
||||
if (days <= 6 - ((now.getDay() + 6) % 7)) return at.toLocaleDateString(lang, { weekday: "short" }) + " " + time;
|
||||
return at.toLocaleDateString(lang, { month: "short", day: "numeric" }) + " " + time;
|
||||
if (days <= 6 - ((now.getDay() + 6) % 7)) return dateFmt({ weekday: "short" }).format(at) + " " + time;
|
||||
return dateFmt({ month: "short", day: "numeric" }).format(at) + " " + time;
|
||||
}
|
||||
|
||||
function untilText(at) {
|
||||
@@ -14570,7 +14569,7 @@ function untilText(at) {
|
||||
return t("in {n}d", { n: Math.round(h / 24) });
|
||||
}
|
||||
|
||||
function resetText(at, when = at <= Date.now() ? at.toLocaleString(intlLang()) : resetClock(at)) {
|
||||
function resetText(at, when = at <= Date.now() ? dateFmt({ year: "numeric", month: "numeric", day: "numeric", hour: "numeric", minute: "numeric", second: "numeric" }).format(at) : resetClock(at)) {
|
||||
return t(at <= Date.now() ? "Reset time passed {when}" : "Resets {when}", { when });
|
||||
}
|
||||
|
||||
|
||||
@@ -19216,6 +19216,17 @@ function intlLang() {
|
||||
return /^en(-|$)/i.test(sys) ? sys : "en";
|
||||
}
|
||||
|
||||
// dateFmt is intlLang()'s date formatter for opts, made once: a
|
||||
// toLocale*String call makes a new one each time, and a card of hundreds
|
||||
// of accounts formats every account's resets on each render (#5310).
|
||||
const dateFmts = new Map();
|
||||
function dateFmt(opts) {
|
||||
const lang = intlLang(), key = lang + JSON.stringify(opts);
|
||||
let f = dateFmts.get(key);
|
||||
if (!f) dateFmts.set(key, (f = new Intl.DateTimeFormat(lang, opts)));
|
||||
return f;
|
||||
}
|
||||
|
||||
function t(s, vars) {
|
||||
let out = I18N[locale]?.[s] ?? s;
|
||||
if (vars) for (const k in vars) out = out.split("{" + k + "}").join(vars[k]);
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
// Run with Node's test runner and Playwright on the module path; see README.md.
|
||||
// A reset's time is said the same as it was before its date formatters were
|
||||
// kept (#5310). resetClock and resetText called toLocale*String, which makes
|
||||
// a formatter each call; with hundreds of accounts a render made thousands.
|
||||
// dateFmt keeps one per language and options now. This holds every reset
|
||||
// time a week either side, for every language, to what toLocale*String
|
||||
// says, in both engines, and to the language picked after the first call.
|
||||
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");
|
||||
|
||||
function serve(lang) {
|
||||
const settings = { lang, theme: "light", currency: "usd" };
|
||||
return async (r) => {
|
||||
const url = new URL(r.request().url());
|
||||
const json = (data) => r.fulfill({ json: data });
|
||||
if (url.pathname === "/boot.js") return r.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:true};` });
|
||||
if (url.pathname === "/wails/runtime.js") return r.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/providers") return json({ providers: [], presets: [], excluded: [], gateway: { running: true, window: true } });
|
||||
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)];
|
||||
try { await r.fulfill({ body: await fs.readFile(file), contentType }); } catch { await r.fulfill({ status: 404, body: "" }); }
|
||||
};
|
||||
}
|
||||
|
||||
for (const engine of ["chromium", "webkit"]) {
|
||||
test(`${engine}: a reset's time reads as toLocale*String says it, in every language`, async (t) => {
|
||||
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
|
||||
t.after(() => browser.close());
|
||||
const context = await browser.newContext({ locale: "en-GB", timezoneId: "Asia/Tokyo" });
|
||||
const page = await context.newPage();
|
||||
const errors = [];
|
||||
page.on("pageerror", (e) => errors.push(e.message));
|
||||
await page.route("**/*", serve("en"));
|
||||
await page.goto("http://magpie.test/");
|
||||
await page.waitForFunction(() => typeof resetClock === "function" && typeof dateFmt === "function");
|
||||
const wrong = await page.evaluate(() => {
|
||||
// what resetClock and resetText said before dateFmt, word for word
|
||||
const was = (at, now) => {
|
||||
const lang = intlLang();
|
||||
const time = at.toLocaleTimeString(lang, { hour: "2-digit", minute: "2-digit", hour12: false });
|
||||
const day = (d) => new Date(d.getFullYear(), d.getMonth(), d.getDate());
|
||||
const days = Math.round((day(at) - day(now)) / 864e5);
|
||||
if (days <= 0) return time;
|
||||
if (days === 1) return t("tomorrow {time}", { time });
|
||||
if (days <= 6 - ((now.getDay() + 6) % 7)) return at.toLocaleDateString(lang, { weekday: "short" }) + " " + time;
|
||||
return at.toLocaleDateString(lang, { month: "short", day: "numeric" }) + " " + time;
|
||||
};
|
||||
const out = [];
|
||||
let seen = 0;
|
||||
// "system" last: the system's English after a language was kept
|
||||
for (const lang of ["en", "zh", "zh-TW", "ja", "de", "system"]) {
|
||||
setLocale(lang);
|
||||
const now = new Date();
|
||||
for (let h = -24 * 7; h <= 24 * 8; h += 5) {
|
||||
const at = new Date(now.getTime() + h * 36e5 + 17 * 6e4);
|
||||
const pairs = [[resetClock(at, now), was(at, now)]];
|
||||
if (at <= Date.now()) pairs.push([resetText(at), t("Reset time passed {when}", { when: at.toLocaleString(intlLang()) })]);
|
||||
for (const [got, want] of pairs) {
|
||||
seen++;
|
||||
if (got !== want) out.push(`${lang} ${at.toISOString()}: ${got} ≠ ${want}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
return { out, seen };
|
||||
});
|
||||
await context.close();
|
||||
assert.deepEqual(errors, []);
|
||||
assert.ok(wrong.seen > 400, `times compared: ${wrong.seen}`);
|
||||
assert.deepEqual(wrong.out, []);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user