Files
magpie/internal/gui/tests/panel-page-hidden.test.cjs
T
yetone 7ea1e6a4b3 usage: the tray panel and the TUI leave out what the Usage page hides
onkashiko on X: a provider whose allowance card was hidden in the main
window (Usage > Overview, "Hide from Overview") still showed in the menu
bar's panel. The panel's Allowances tab had only its own Arrange list
(panelUsageHidden) and read the full cards.

Now the panel leaves out a card whose cardHideKey is in usageCardsHidden
and the rows in usageWindowsHidden (withoutHiddenRows), as Overview does;
a menu bar cell's card still shows (panelPeek). A subscription whose
every card the page hides counts in the foot's "{n} hidden" and shows as
hidden in Arrange; Show there
also drops its keys from usageCardsHidden, since the panel follows the
page. All of it is panelPageCut in the Usage plugin's page.js, which every
window loads, so the core scripts get no bigger (TestCoreScriptBudget
unchanged). Hiding from the panel alone still leaves the page as it is. The
panel reads settings again each time it is shown (loadBack), so a hide or
unhide in the main window reaches it with no restart.

Siblings: magpie tui's Usage page leaves out the same cards and rows
(usageShown, read at every quota read). The menu bar's cells and tooltip
show only the cards picked in Settings, so they keep a hidden card as
before; routing, caps and counting are unchanged.

Run:
- panel-page-hidden.test.cjs (new): chromium + webkit, en/zh/zh-TW/ja/de,
  10/10 green; red on the old app.js/panelarrange.js (cards still drawn).
- TestUsagePageLeavesOutWhatTheAppHides (new): green; red on the old
  pages.go (balance, account and row still listed).
- GUI: the 128 files that read the panel, Usage or tray code, chromium +
  webkit, every language, on mote: 17 files red, rerun alone at
  concurrency 1 here 123/449 red and at origin/main 015860f2d 122/449,
  the same tests but tray-cell-click's chromium en, which is red alone on
  main too (#5215); the rest noted on #5475. tray-cell-click + panel-arrange +
  panel-page-hidden together before the last rebase (no GUI file changed
  in it): 21 of 22, the one red tray-cell-click's (#5215). At this head
  (after the move into page.js): panel-page-hidden, panel-arrange,
  panel-balance-order and i18n-keys green; the reds in key-balance-card
  (de), panel-fold, tray-cell-click (zh) and usage-cards-hide are each
  in the origin/main 015860f2d record above.
- Full suite (go test -tags nogui -count=1 -timeout 45m ./...) on mica
  under suite.lock at 734f06b64 (this change before the move into
  page.js): 97 packages ok; internal/gui red only on TestCoreScriptBudget
  (fixed by the move), internal/gateway red from "too many open files in
  system" at mica's load ~65. At this head on mote: internal/gateway,
  internal/gui, internal/tui, internal/settings and internal/redact (the
  one commit rebased over) ok; gofmt -l clean; go vet, GOOS=linux go vet
  -tags nogui and GOOS=windows go vet ./... clean; go build ok.
- Rebased onto origin/main 667ac8bd6 (5faed634d): internal/gui (budget
  included), internal/tui and internal/settings ok on mote;
  panel-page-hidden + panel-arrange 18 of 18.
Not tried: the real tray panel by hand on a live install.
2026-11-14 14:10:24 +08:00

109 lines
7.4 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// What the Usage page hides is left out of the tray panel's Allowances tab
// too (onkashiko on X: a provider hidden in the main window still showed in
// the menu bar's panel): a hidden card, a hidden row, and a subscription
// hidden whole, which Arrange calls hidden and whose Show brings it back on
// the page too. A hide in the main window reaches the panel when it is
// shown again, with no restart. APIs are mocked.
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 quotas = [
{ provider: "claude", name: "Claude Code", icon: "claude-color", user: "a@b.c", windows: [{ name: "Weekly", used: 17.6 }, { name: "5-hour", used: 42.2 }] },
{ provider: "codex", name: "Codex", icon: "codex-color", user: "x@y.z", windows: [{ name: "5-hour", used: 8 }] },
{ provider: "codex", name: "Codex", icon: "codex-color", user: "other@例子.test", windows: [{ name: "Weekly", used: 25 }] },
{ provider: "deepseek", name: "DeepSeek", icon: "deepseek-color", windows: [], balance: "¥12.30" },
{ provider: "kimi", name: "Kimi Code", icon: "kimi-color", windows: [{ name: "Weekly", used: 30 }] },
];
function serve(lang, posts, settings) {
return async (route) => {
const url = new URL(route.request().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 });
if (url.pathname === "/api/settings") return json(settings);
if (url.pathname === "/api/usage/arrange") {
const body = route.request().postDataJSON();
posts.push(body);
if (body.panelHidden) settings.panelUsageHidden = body.panelHidden;
if (body.cardsHidden) settings.usageCardsHidden = body.cardsHidden;
return json(settings);
}
if (url.pathname === "/api/providers") return json({ providers: [], presets: [], excluded: [], gateway: { running: true, window: true } });
if (url.pathname === "/api/usage/quotas") return json(quotas);
if (url.pathname === "/api/usage") return json({ days: [] });
if (url.pathname === "/api/groups") return json({ groups: [], models: [] });
if (url.pathname === "/api/plugins") return json({ plugins: [] });
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 route.fulfill({ body: await fs.readFile(file), contentType }); } catch { await route.fulfill({ status: 404, body: "" }); }
};
}
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of ["en", "zh", "zh-TW", "ja", "de"]) {
test(`${engine} ${lang}: the panel leaves out what the Usage page hides`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(async () => { await browser.close(); });
const errors = [], posts = [];
const settings = {
theme: "light", lang, tray: "panel", quotaLeft: false, currency: "usd", usageOrder: [], panelUsageHidden: [],
usageCardsHidden: ["deepseek|balance", "codex|x@y.z|plan"], usageWindowsHidden: ["claude|5-hour"],
};
const context = await browser.newContext({ viewport: { width: 440, height: 600 }, reducedMotion: "reduce" });
await context.addInitScript(() => { try { localStorage.setItem("magpie.panelTab", "usage"); } catch {} });
const page = await context.newPage();
page.setDefaultTimeout(5000);
page.setDefaultNavigationTimeout(60000);
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", serve(lang, posts, settings));
await page.goto("http://magpie.test/?mode=panel");
await page.waitForFunction(() => document.querySelectorAll("#panelQuota .pq-card").length >= 2);
const tr = (s, v) => page.evaluate(([s, v]) => t(s, v), [s, v]);
const cards = () => page.evaluate(() => [...document.querySelectorAll("#panelQuota .pq-card")].map((c) => c.dataset.card));
const rings = (card) => page.locator(`#panelQuota .pq-card[data-card="${card}"] .pq-ring`).count();
// the hidden card, the account and the row are left out
assert.deepEqual(await cards(), ["claude|a@b.c", "codex|other@例子.test", "kimi"]);
assert.equal(await rings("claude|a@b.c"), 1, "the row the page hides is left out");
assert.equal(await page.evaluate(() => document.querySelector("#panelQuota .pq-bals")), null, "no balance for the hidden DeepSeek card");
assert.equal(await page.locator("#panelQuota .pq-foot .pq-hidden").textContent(), await tr("{n} hidden", { n: 1 }));
// hidden in the main window while the panel was put away: shown again,
// the panel has it gone, and one shown there comes back
settings.usageCardsHidden = ["deepseek|balance", "kimi|plan"];
settings.usageWindowsHidden = [];
await page.evaluate(() => document.dispatchEvent(new Event("visibilitychange")));
await page.waitForFunction(() => !document.querySelector('#panelQuota .pq-card[data-card="kimi"]'));
assert(!(await cards()).includes("kimi") && (await cards()).includes("claude|a@b.c"));
assert.equal(await rings("claude|a@b.c"), 2, "the row shown in the main window again is back");
assert.equal(await page.locator("#panelQuota .pq-foot .pq-hidden").textContent(), await tr("{n} hidden", { n: 2 }));
// Arrange calls them hidden; Show brings DeepSeek back on the page too
await page.locator("#panelQuota .pq-arrange").click();
await page.waitForSelector("#panelQuota .pq-arow");
for (const key of ["deepseek", "kimi"]) {
assert(await page.locator(`#panelQuota .pq-arow[data-key="${key}"]`).evaluate((r) => r.classList.contains("put-away")), key + " shows as hidden");
assert.equal(await page.locator(`#panelQuota .pq-arow[data-key="${key}"] .pq-show`).textContent(), await tr("Show"));
}
assert.equal(await page.locator('#panelQuota .pq-arow[data-key="claude"] .pq-show').textContent(), await tr("Hide"));
await page.locator('#panelQuota .pq-arow[data-key="deepseek"] .pq-show').click();
await page.waitForFunction(() => !document.querySelector('#panelQuota .pq-arow[data-key="deepseek"]').classList.contains("put-away"));
assert.deepEqual(posts.splice(0), [{ panelHidden: [], cardsHidden: ["kimi|plan"] }]);
// hiding from the panel alone leaves the page's list as it is
await page.locator('#panelQuota .pq-arow[data-key="claude"] .pq-show').click();
await page.waitForFunction(() => document.querySelector('#panelQuota .pq-arow[data-key="claude"]').classList.contains("put-away"));
assert.deepEqual(posts.splice(0), [{ panelHidden: ["claude"] }]);
assert.deepEqual(settings.usageCardsHidden, ["kimi|plan"]);
assert.deepEqual(errors, []);
});
}
}