Files
magpie/internal/gui/tests/routing-pooled-card.test.cjs
T
yetone 1106a33a89 routing: every account row in Accounts and keys shows all its windows
zwm_vincent on X: with two subscription accounts, Accounts and keys
showed one window per row, 5H on one and 7D on the other, so neither
row said what was left in the other window or when it reset.

A subscription account's row now has one line per window Usage read:
the short name (5h, 7d), a small bar, what is used or left, and when
it resets. Each part's column is as wide as the widest down the list
(in ch, CJK counted double), so the names, bars, values and resets
start at the same place on every row. Where a row is too narrow (the
list in columns at a phone's width, under 215px) the bar is left out
and the reset wraps under, alike on every row. The hover still has
each window in full with the day and time it resets and when it was
read. The single-figure status, the windows line and the bottom bar
are not drawn for such a row; wallets and accounts with no windows
keep their one line.

Tests: routing-account-windows.test.cjs adds a second account whose
binding window is the week and checks both rows show both windows with
their bars (40%/12%), the hover text, every column's left (and the
reset's right) equal across lines and rows, nothing cut, no sideways
scroll; en, zh, zh-TW, ja, de at 1100px and 420px, Chromium and WebKit.
routing-pooled-card and routing-read-allowances read the new lines.

Run at this head, rebased on origin/main aa358555f, on mote:
- The 7 routing GUI files (routing-answer-figures, routing-account-windows,
  routing-pooled-card, routing-read-allowances and the routing files they
  share helpers with): 108/108 ok, Chromium and WebKit, en/zh/zh-TW/ja/de,
  1100px and 420px.
- Both new tests on the old code: 86 fail, 2 pass.
- Every GUI file that loads routing or app.css (313 files): chromium 51
  and webkit 50 reds, each red on origin/main aa358555f too (#5872,
  #5262 zero-priced-free, #6561 text-size webkit, red 3 of 3 on main
  on mica). ctx-route-align was red here only, from a fractional
  min-height; fixed (54px) and green.
- go vet ./internal/gui, go test ./internal/gui: ok (160.8s).
Rebased on origin/main 171fce511 (app.js, i18n.js changed): ctx-route-align,
  i18n-keys, quota-credits and the 4 routing files above, 108/108 ok,
  Chromium and WebKit, on mote.
Broken by hand: the old client.js/routing.css under the new tests (above).
Not run: the whole GUI suite, the Go suite (no Go change), the real app
with a real account's windows (fixtures carry the reporters' figures).
2026-11-14 23:45:23 +08:00

117 lines
7.7 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// X (@apple_test_src): a Cursor account signed in through its plugin showed
// "what's left not known yet" in Routing's Accounts and keys while the Usage
// page read its allowance. Every window of a pooled Cursor plan is set aside
// (it can't stop the account), so routing weighs no figure for it, and the
// row said nothing was known. It now says the card's windows, a line each
// (zwm_vincent on X), as the Usage page read them, in full on hover; the
// unknown line stays for an account with no card. Every language, 1100 and 420px, both engines.
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 iso = (ms) => new Date(ms).toISOString();
const accts = [
{ id: "cursor", provider: "cursor", name: "Cursor", who: "work@example.com", kind: "account", agent: "cursor", model: "claude-opus-5", known: false, plan: "Pro" },
{ id: "cursor@spare", provider: "cursor", name: "Cursor", who: "spare@example.com", kind: "account", agent: "cursor", model: "claude-opus-5", known: false, plan: "Pro" },
];
const route = {
id: 7, seq: 7, time: iso(now - 2000), agent: "cursor", model: "claude-opus-5", provider: "cursor", done: true, status: 200, ms: 900, tokens: 1500,
order: accts, tries: [{ id: "cursor", model: "claude-opus-5", start: iso(now - 2000), done: true, ms: 800, status: 200 }],
};
const cards = [
{ provider: "cursor", name: "Cursor", kind: "subscription", user: "Work@Example.com", plan: "Max", windows: [
{ name: "5 hours", used: 40, aside: true, resetsAt: iso(now + 130 * 60e3) },
{ name: "7 days", used: 12, aside: true, resetsAt: iso(now + 73 * 3600e3) },
] },
// another computer's reading of the spare account: not this one's
{ provider: "cursor", name: "Cursor · Home", kind: "subscription", user: "spare@example.com", from: "Home", windows: [{ name: "5 hours", used: 90 }] },
];
function serve(lang, asks) {
const state = { agents: [{ id: "cursor", name: "Claude Code", path: "/test/claude", fields: [] }], profiles: [], settings: { lang, theme: "light", quotaLeft: false } };
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(state);
if (url.pathname === "/api/gateway/trace") {
if (url.searchParams.get("wait")) return new Promise(() => {}); // nothing more comes
return json({ mine: true, now: new Date().toISOString(), seq: 7, totals: { requests: 1, rerouted: 0, errors: 0 }, routes: [route] });
}
if (url.pathname === "/api/usage/quotas") { asks.push(url.search); return json(cards); }
if (url.pathname === "/api/usage/quotas/history") return json([]);
if (url.pathname === "/api/gateway/history") return json({ cut: false, days: [], routes: [] });
if (url.pathname === "/api/groups") return json({ models: [], groups: [], pools: [] });
if (url.pathname === "/api/providers") return json({ providers: [], presets: [], excluded: [], gateway: { running: true, window: true, url: "http://127.0.0.1:3999" } });
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 r.fulfill({ body: await fs.readFile(file), contentType });
};
}
const words = {
en: ["5 hours 40% used · resets in 2h", "7 days 12% used · resets in 3d"],
zh: ["5 小时 已用 40% · 2 小时后重置", "7 天 已用 12% · 3 天后重置"],
"zh-TW": ["5 小時 已用 40% · 2 小時後重置", "7 天 已用 12% · 3 天后重置"],
ja: ["5 時間 使用済み 40% · 2 時間後にリセット", "7 日 使用済み 12% · 3 日後にリセット"],
de: ["5 Stunden 40% verbraucht · Reset in 2 Std.", "7 Tage 12% verbraucht · Reset in 3 T."],
};
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
test(`${engine}: a plugin account whose windows are all aside shows them, not unknown`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
for (const lang of Object.keys(words)) {
for (const width of [1100, 420]) {
await t.test(`${lang} at ${width}px`, async () => {
const ctx = await browser.newContext({ viewport: { width, height: 760 }, reducedMotion: "reduce" });
try {
const page = await ctx.newPage();
page.setDefaultTimeout(5000);
page.setDefaultNavigationTimeout(60000);
const errors = [], asks = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", serve(lang, asks));
await page.goto("http://magpie.test/?view=routing");
const act = page.locator(".rt-act", { hasText: "work@example.com" }).first();
// the one figure routing went by, the windows each on hover over
// it: a card keeps one line of what is left (ferock4669 on
// Discord), and when it was read comes last on hover (danzi002)
await act.waitFor();
// the card's windows, a line each, in full on hover
await page.waitForFunction(() => [...document.querySelectorAll(".rt-act")].some((e) => e.textContent.includes("work@example.com") && e.querySelectorAll(".wq > div").length === 2));
const lines = await act.locator(".wq > div").evaluateAll((ls) => ls.map((l) => [l.querySelector(".v").textContent, l.querySelector(".r").textContent].join(" · ")));
// what is used and when it resets, the window's name ("5 hours")
// shortened to its column
assert.deepEqual(lines, words[lang].map((x) => x.split(" ").slice(2).join(" ")));
assert.equal((await act.locator(".wq").getAttribute("title")).split("\n").slice(0, 2).map((x) => x.split(" · ")[0]).join("\n"), words[lang].map((x) => x.split(" · ")[0]).join("\n"));
assert.equal(await act.locator(".wins").count(), 0);
assert.equal(await act.locator(".st").isVisible(), false, "not unknown");
// no card of its own (another computer's isn't this one's): unknown
const spare = page.locator(".rt-act", { hasText: "spare@example.com" }).first();
await spare.waitFor();
assert.ok(await spare.locator(".st").isVisible(), "unknown, as before");
assert.equal(await spare.locator(".wq").count(), 0);
assert.equal(await spare.locator(".wins").count(), 0);
// nothing in the row runs past the list
const over = await act.evaluate((e) => Math.max(e.scrollWidth - e.clientWidth, document.documentElement.scrollWidth - innerWidth));
assert.ok(over <= 1, `${lang} at ${width}: ${over}px past`);
assert.ok(asks.length >= 1, "the readings were asked for");
assert.ok(asks.every((s) => !s.includes("asked")), "never an asked read: " + asks.join(", "));
assert.deepEqual(errors, []);
} finally {
await ctx.close();
}
});
}
}
});
}