Files
magpie/internal/gui/tests/panel-arrange.test.cjs
T
yetone 490d2ea362 gui: the tray panel's Allowances tab hides subscriptions and moves them, from an Arrange at its foot (H20 on Discord: "菜单栏的详情页能不能选择某些不显示,有点额度并不想看到或者可以调整顺序,目前的还不行") — the panel's tab already followed the Usage page's order (usageOrder, byUsageOrder), but it could be set only by dragging a card's logo on the window's Usage page, which nothing in the panel said, and there was no way to leave a subscription out of the panel at all. The tab's foot now has Arrange, and "{n} hidden" beside it when some are: Arrange lists a row a subscription (balances too) in the shared order, its logo the handle (a drag, or Alt+arrows; the grip by it on hover only, as on the Usage page) and a Hide/Show pill, under a note that hiding is the panel's only and the order is the Usage page's too; Done draws the cards again. What's hidden is kept in magpie's settings (settings.PanelUsageHidden, panelUsageHidden), not the browser: routing, caps, the Usage page and the menu bar's cells still have it, and a menu bar cell for a hidden subscription still opens its card (panelPeek) until the panel is put away or the tab left. POST /api/usage/arrange takes order and panelHidden and changes only the one it is sent (both were whole-order before; an order alone still works), and the Settings save keeps panelUsageHidden as it does usageOrder. Saves draw at once and are put back with the error if they fail; no click moves the page (the view's scroll is kept across the redraw); no native select, no left-border stripe. Which allowances the menu bar shows beside its icon stays Settings' pick (trayUsages). New t() strings have zh, ja and de. TestUsageArrangePanelHidden (each field set and cleared on its own, a Settings save between) passes -race -count=20, TestSettingsSaveKeepsWhatItDoesNotSend's fixture names the field. panel-arrange.test.cjs (440px: the hidden card left out and counted, Arrange keeping the reader's scroll, the rows and note, the hover-only grip, no stripe, no sideways overflow, Show/Hide posting panelHidden without a scroll and a failed save put back, Alt+arrow and a drag posting the order, Done's cards in it, a menu bar cell opening a hidden card until the panel hides, the Usage page keeping the hidden card) passes on Chromium and WebKit in en, zh, ja and de; click-scroll, gui-ja, gui-de, i18n-keys, panel-fold, panel-usage, quota-*, routing-quota-left, tray-cell-click, tray-inuse, tray-logos, tray-usages, usage-accounts(-fold), usage-arrange, usage-fold, usage-quota-head, usage-card-refresh, usage-stale-answer, usage-remote-cards, api-key-usage, claude-usage-asked and usage-checkin pass on both. go test -tags nogui ./internal/... under a temp HOME, go vet, gofmt -l, linux (nogui) and windows builds pass. Not tried in the real tray panel on a Mac. docs/reference.md and docs/subsystems/gui-shell.md say it.
2026-10-06 19:18:14 +08:00

178 lines
13 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// The tray panel's Allowances tab hides subscriptions and moves them (H20 on
// Discord): Arrange lists them, a pill hides or shows one, the logo moves it
// (Alt+arrows, drag) in the order the Usage page shares; what's hidden stays
// on the Usage page, and a menu bar cell still opens its card. 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 }, { name: "Weekly", used: 100 }] },
{ 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 的一个名字很长很长的订阅 with a long name", icon: "kimi-color", windows: [{ name: "Weekly", used: 30 }] },
];
function serve(lang, posts, fail) {
const settings = { theme: "light", lang, tray: "panel", quotaLeft: false, currency: "usd", usageOrder: [], panelUsageHidden: ["kimi"] };
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 (fail.next) { fail.next = false; return route.fulfill({ status: 500, body: "disk full" }); }
if (body.order) settings.usageOrder = body.order;
if (body.panelHidden) settings.panelUsageHidden = body.panelHidden;
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", "ja", "de"]) {
test(`${engine} ${lang}: the panel's Allowances tab hides and moves subscriptions`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(async () => { await browser.close(); });
const errors = [], posts = [], fail = { next: false };
const context = await browser.newContext({ viewport: { width: 440, height: 300 }, reducedMotion: "reduce" });
await context.addInitScript(() => {
try { localStorage.setItem("magpie.panelTab", "usage"); } catch {}
window.__scrolled = 0;
const scroll = Element.prototype.scrollIntoView;
Element.prototype.scrollIntoView = function (o) { window.__scrolled++; scroll.call(this, o); };
});
const page = await context.newPage();
page.setDefaultTimeout(5000);
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", serve(lang, posts, fail));
await page.goto("http://magpie.test/?mode=panel");
await page.waitForFunction(() => document.querySelectorAll("#panelQuota .pq-card").length >= 3);
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 rows = () => page.evaluate(() => [...document.querySelectorAll("#panelQuota .pq-arow")].map((r) => r.dataset.key));
const view = page.locator("#view-agents");
const noSideways = async () => assert(await page.evaluate(() => {
const v = document.querySelector("#view-agents");
return v.scrollWidth <= v.clientWidth && document.scrollingElement.scrollWidth <= innerWidth
&& [...document.querySelectorAll("#panelQuota .pq-arow, #panelQuota .pq-foot")].every((r) => r.scrollWidth <= r.clientWidth + 1 && r.getBoundingClientRect().right <= innerWidth);
}), "nothing runs off sideways at 440px");
// what the settings hide is left out, and the foot says how many
assert(!(await cards()).includes("kimi"), "the hidden subscription has no card");
assert.equal(await page.locator("#panelQuota .pq-foot .pq-hidden").textContent(), await tr("{n} hidden", { n: 1 }));
assert.equal(await page.locator("#panelQuota .pq-arrange").textContent(), await tr("Arrange"));
await noSideways();
// Arrange: a click that doesn't move the page; a row each, in order
// the reader scrolls to the foot, as a reader does: by the wheel
await page.mouse.move(220, 150);
await page.mouse.wheel(0, 1000);
await page.waitForFunction(() => { const v = document.querySelector("#view-agents"); return v.scrollTop > 0 && v.scrollTop >= v.scrollHeight - v.clientHeight - 1; });
const keep = await view.evaluate((v) => v.scrollTop);
await page.locator("#panelQuota .pq-arrange").click();
await page.waitForSelector("#panelQuota .pq-arow");
const after = await view.evaluate((v) => [v.scrollTop, v.scrollHeight - v.clientHeight]);
assert.equal(after[0], Math.min(keep, after[1]), "Arrange keeps the reader where they were");
assert.deepEqual(await rows(), ["claude", "codex", "deepseek", "kimi"]);
assert.equal(await page.locator("#panelQuota .pq-anote").textContent(), await tr("Hiding is for this panel only: routing, the Usage page and the menu bar still use what's hidden. The order is the Usage page's too."));
assert(await page.locator('#panelQuota .pq-arow[data-key="kimi"]').evaluate((r) => r.classList.contains("put-away")));
assert.equal(await page.locator('#panelQuota .pq-arow[data-key="kimi"] .pq-show').textContent(), await tr("Show"));
assert.equal(await page.locator('#panelQuota .pq-arow[data-key="claude"] .pq-show').textContent(), await tr("Hide"));
for (const w of await page.evaluate(() => [...document.querySelectorAll("#panelQuota .pq-arow")].map((r) => getComputedStyle(r).borderLeftWidth))) assert.equal(w, "0px", "no stripe down a row's left");
await noSideways();
// the grip by the logo is there on hover only
await page.mouse.move(5, 5);
const grip = (key) => page.locator(`#panelQuota .pq-arow[data-key="${key}"] .pq-ahandle`).evaluate((h) => getComputedStyle(h, "::before").opacity);
await page.waitForFunction(() => getComputedStyle(document.querySelector('#panelQuota .pq-arow[data-key="codex"] .pq-ahandle'), "::before").opacity === "0");
await page.locator('#panelQuota .pq-arow[data-key="codex"] .pq-aname').hover();
await page.waitForFunction(() => getComputedStyle(document.querySelector('#panelQuota .pq-arow[data-key="codex"] .pq-ahandle'), "::before").opacity === "1");
assert.equal(await grip("claude"), "0");
// hide and show: the pill posts what's hidden, and doesn't scroll
const top = await view.evaluate((v) => v.scrollTop);
await page.locator('#panelQuota .pq-arow[data-key="kimi"] .pq-show').click();
await page.waitForFunction(() => !document.querySelector('#panelQuota .pq-arow[data-key="kimi"]').classList.contains("put-away"));
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: [] }, { panelHidden: ["deepseek"] }]);
assert.equal(await view.evaluate((v) => v.scrollTop), top, "a pill's click doesn't move the page");
assert(await page.evaluate(() => document.activeElement?.closest(".pq-arow")?.dataset.key === "deepseek"), "the keyboard stays on the pill");
// a save that fails is put back
fail.next = true;
await page.locator('#panelQuota .pq-arow[data-key="claude"] .pq-show').click();
await page.waitForFunction(() => document.querySelector("#status")?.textContent.includes("disk full") || document.body.textContent.includes("disk full"));
assert(!(await page.locator('#panelQuota .pq-arow[data-key="claude"]').evaluate((r) => r.classList.contains("put-away"))), "the failed hide is undone");
posts.splice(0);
// Alt+arrow moves a row, the keyboard staying on its logo
await page.locator('#panelQuota .pq-arow[data-key="claude"] .pq-ahandle').focus();
await page.keyboard.press("Alt+ArrowDown");
await page.waitForFunction(() => document.querySelector("#panelQuota .pq-arow")?.dataset.key === "codex");
assert.deepEqual(await rows(), ["codex", "claude", "deepseek", "kimi"]);
assert.deepEqual(posts.splice(0), [{ order: ["codex", "claude", "deepseek", "kimi"] }]);
assert(await page.evaluate(() => document.activeElement?.matches('.pq-arow[data-key="claude"] .pq-ahandle')));
// a drag by the logo: DeepSeek to the top
const from = await page.locator('#panelQuota .pq-arow[data-key="deepseek"] .pq-ahandle').boundingBox();
const to = await page.locator('#panelQuota .pq-arow[data-key="codex"]').boundingBox();
await page.mouse.move(from.x + from.width / 2, from.y + from.height / 2);
await page.mouse.down();
for (let i = 1; i <= 8; i++) await page.mouse.move(from.x + from.width / 2, from.y + from.height / 2 + (to.y - from.y - 4) * i / 8);
await page.mouse.up();
await page.waitForFunction(() => document.querySelector("#panelQuota .pq-arow")?.dataset.key === "deepseek");
assert.deepEqual(posts.splice(0), [{ order: ["deepseek", "codex", "claude", "kimi"] }]);
// Done: the cards in the new order, DeepSeek left out
await page.locator("#panelQuota .pq-done").click();
await page.waitForFunction(() => !document.querySelector("#panelQuota .pq-arow"));
assert.deepEqual(await cards(), ["codex|x@y.z", "claude|a@b.c", "kimi"]);
assert.equal(await page.locator("#panelQuota .pq-foot .pq-hidden").textContent(), await tr("{n} hidden", { n: 1 }));
await noSideways();
assert.equal(await page.evaluate(() => window.__scrolled), 0, "nothing scrolled itself");
// a menu bar cell for the hidden one opens its card all the same
await page.evaluate(() => panelQuotaFocus("deepseek"));
await page.waitForFunction(() => document.querySelector('#panelQuota .pq-card[data-card="deepseek"]')?.classList.contains("flash"));
assert.equal(await page.locator("#panelQuota .pq-foot .pq-hidden").count(), 0, "the one asked for isn't counted as hidden");
// put away, the panel hides it again
await page.evaluate(() => {
Object.defineProperty(document, "hidden", { configurable: true, get: () => true });
document.dispatchEvent(new Event("visibilitychange"));
});
await page.waitForFunction(() => !document.querySelector('#panelQuota .pq-card[data-card="deepseek"]'));
// the Usage page keeps every card, in the shared order
const win = await context.newPage();
win.on("pageerror", (e) => errors.push(e.message));
await win.route("**/*", serve(lang, [], {}));
await win.goto("http://magpie.test/?view=usage&tab=usage");
await win.evaluate((s) => { state.settings = { ...state.settings, ...s }; renderQuotas(); }, { usageOrder: ["deepseek", "codex", "claude", "kimi"], panelUsageHidden: ["deepseek"] });
await win.waitForFunction(() => document.querySelectorAll("#subscriptionUsage > [data-key]").length >= 4);
assert.deepEqual(await win.evaluate(() => [...new Set([...document.querySelectorAll("#subscriptionUsage > [data-key]")].map((c) => c.dataset.key))]), ["deepseek", "codex", "claude", "kimi"]);
assert.deepEqual(errors, []);
});
}
}