gui: the tray panel's Usage tab is drawn again in Settings' currency picked alone, and when its read came before the state (dLdgKyRd)

dLdgKyRd on Discord, v0.1.3025 on macOS: the menu-bar panel's Usage tab
said ≈$12.36 while the window's Usage page said ≈¥82.61 for the same day.

v0.1.2913 (2ba044aa6) made the Mac panel's page hear that it was hidden
and shown, so a reopen reads the state again. Nothing since changed that
path; its redraw never came. applyPrefs drew the panel's Usage tab again
only when the number units changed. A currency change called the Usage
page's costs hook, which redraws the page and Routing, not the panel's
tab, which keeps its own read. 2913's test picked ¥ and 万 / 亿 together,
so the units' redraw hid it. And the first applyPrefs (applyPrefs.painted
false) redrew nothing: the panel opened on its Usage tab reads its usage
beside /api/state, and when that read came first it drew $, which no
reopen, focus or quiet read of the same figures drew again.

applyPrefs now redraws the panel's Usage tab, when it has drawn, on any
change of currency, rate or units, the first settings included.

Tests: panel-units-follow.test.cjs gets "follows Settings' currency
picked alone": first open with ¥ set and /api/state answering 400ms after
the usage read; ¥ picked alone, then a reopen; $ picked again, then a
focus (Settings changed while the panel was open); and the panel opened
on Agents, ¥ picked, its Usage tab picked after. zh, en, zh-TW, ja, de, at
380px and 300px, Chromium and WebKit.

Run on mote (macOS 27.2), at this tree:
- node --test panel-units-follow (the new test 20/20 subtests, the 2913
  one 12/12), panel-usage, currency, number-units, window-visibility,
  i18n-en-no-cjk: 62/62, at the rebased head (parent a2f7ed2e8)
- go test -count=1 ./internal/gui/ (core and app.js budgets included,
  app.js 1,311,133 of 1,316,874): every test ok but
  TestMainWindowCorners/macOS_26_SDK, red alone at a2f7ed2e8 too (#3949)
- go vet ./internal/gui/, GOOS=linux go vet -tags nogui, GOOS=windows go
  vet: ok; gofmt -l: nothing
- every panel-*, usage-*, currency, number-units, window-visibility and
  i18n file before the rebase: 1462 of 1495; the 33 reds are in
  i18n-keys (#5327, #5332), usage-agents-hide and usage-fold (#2399) and
  usage-ledger (#5048), and the same four files are red alone on
  origin/main 2891ec7a7 (34 there)

Broken by hand: the new test against the old app.js (a copy of the
assets, MAGPIE_PANEL_UNITS_ASSETS): 0 of 10 in each engine, "first open,
the state after the usage" (≈$26.40); with the first-open part left out,
"reopened after ¥ was picked alone" red in zh and en. The tab part passes
on the old code too: it is a guard, not the bug.

Not tried: the real tray panel by hand on a Mac, nor on Windows or Linux;
dLdgKyRd's exact sequence (which of the paths they hit) isn't known.
This commit is contained in:
yetone
2026-11-05 15:09:33 +08:00
parent a2f7ed2e81
commit 5db1b07ed9
2 changed files with 90 additions and 2 deletions
+8 -2
View File
@@ -19464,13 +19464,19 @@ function applyPrefs(s, rate) {
const got = rate?.rate > 0 ? rate : s.fx?.rate > 0 ? s.fx : null;
const moved = !!got && got.rate !== fx.rate;
if (got) fx = got;
let costs = false;
if (currency !== (s.currency || "usd") || (moved && currency === "cny")) {
currency = s.currency || "usd";
if (applyPrefs.painted) magpie.call("usage", "costs");
costs = true;
}
if (chineseUnits !== !!s.chineseUnits) {
chineseUnits = !!s.chineseUnits;
if (applyPrefs.painted) { magpie.call("usage", "costs"); if (mode === "panel" && panelTab === "stats") renderPanelUse(); }
costs = true;
}
// the panel's Usage tab too, whenever drawn: its read can beat the state (dLdgKyRd)
if (costs) {
if (applyPrefs.painted) magpie.call("usage", "costs");
if (mode === "panel" && panelTab === "stats" && panelUse) renderPanelUse();
}
applyPrefs.painted = true;
if (!prefsBusy && (s.textSize || 100) !== textSize) { textSize = s.textSize || 100; newTextSize(); applyZoom(textSize); }
@@ -134,3 +134,85 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium",
}
});
}
// The currency alone (dLdgKyRd on Discord, v0.1.3025: the panel's Usage tab
// ≈$12.36 while the Usage page said ≈¥82.61). The test above picks ¥ and
// 万 / 亿 together, and the units' change redrew the panel while the
// currency's alone didn't; nor was a panel drawn before the state came
// ever redrawn. Each way the panel gets its currency: its first open, with
// /api/state slower than its usage read; a reopen; a focus, as when
// Settings changed while it was open; and its own tab picked afterwards.
// At the panel's width and a narrower one.
async function openPanel(browser, lang, store, width, tab) {
const ctx = await browser.newContext({ viewport: { width, height: 560 }, reducedMotion: "reduce" });
const p = await ctx.newPage();
const errors = [];
p.setDefaultTimeout(5000);
p.setDefaultNavigationTimeout(60000);
p.on("pageerror", (e) => errors.push(e.message));
const plain = serve(lang, store);
await p.route("**/*", async (route) => {
// the state answers after the panel's usage read
if (store.slowState && new URL(route.request().url()).pathname === "/api/state") await new Promise((r) => setTimeout(r, 400));
return plain(route);
});
await p.addInitScript((tab) => { try { localStorage.setItem("magpie.panelTab", tab); } catch {} }, tab);
await p.goto("http://magpie.test/?mode=panel");
return { ctx, p, errors };
}
const costShown = (p) => p.evaluate(() => document.querySelectorAll("#panelUsage .pu-tot .blk .v")[2]?.textContent.trim() || "");
async function costBecomes(p, want, why) {
for (let i = 0; i < 100 && (await costShown(p)) !== want; i++) await p.waitForTimeout(20);
assert.equal(await costShown(p), want, why);
}
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
test(engine + ": the tray panel's Usage tab follows Settings' currency picked alone", async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
for (const lang of LANGS) for (const width of [380, 300]) {
await t.test(lang + " " + width + "px", async () => {
const units = { lang, theme: "light", chineseUnits: false, tray: "panel" };
// first open: ¥ picked already, the state slower than the usage read
let store = { cur: { ...units, currency: "cny" }, stateReads: 0, slowState: true };
let o = await openPanel(browser, lang, store, width, "stats");
await o.p.locator("#panelUsage .pu-card .led-chart svg").waitFor();
for (let i = 0; i < 100 && store.stateReads === 0; i++) await o.p.waitForTimeout(20);
await o.p.waitForTimeout(500); // the state is in
await costBecomes(o.p, "≈¥185", lang + ": first open, the state after the usage");
assert.deepEqual(o.errors, []);
await o.ctx.close();
// reopened, then focused, after Settings picked the currency alone
store = { cur: { ...units, currency: "usd" }, stateReads: 0 };
o = await openPanel(browser, lang, store, width, "stats");
await o.p.locator("#panelUsage .pu-card .led-chart svg").waitFor();
await costBecomes(o.p, "≈$26.40", lang + ": $ at first");
store.cur = { ...store.cur, currency: "cny" };
await hideAndShow(o.p, store);
await costBecomes(o.p, "≈¥185", lang + ": reopened after ¥ was picked alone");
store.cur = { ...store.cur, currency: "usd" };
const before = store.stateReads;
await o.p.evaluate(() => window.dispatchEvent(new Event("focus")));
for (let i = 0; i < 100 && store.stateReads === before; i++) await o.p.waitForTimeout(20);
await costBecomes(o.p, "≈$26.40", lang + ": focused after $ was picked again");
assert.deepEqual(o.errors, []);
await o.ctx.close();
// open on another tab; ¥ picked; the Usage tab picked after
store = { cur: { ...units, currency: "usd" }, stateReads: 0 };
o = await openPanel(browser, lang, store, width, "agents");
for (let i = 0; i < 100 && store.stateReads === 0; i++) await o.p.waitForTimeout(20);
store.cur = { ...store.cur, currency: "cny" };
await hideAndShow(o.p, store);
await o.p.waitForTimeout(100);
await o.p.locator('[data-ptab="stats"]').click();
await o.p.locator("#panelUsage .pu-card .led-chart svg").waitFor();
await costBecomes(o.p, "≈¥185", lang + ": its Usage tab picked after ¥");
assert.deepEqual(o.errors, []);
await o.ctx.close();
});
}
});
}