diff --git a/internal/gui/assets/plugins/routing/client.js b/internal/gui/assets/plugins/routing/client.js index a3bb7756b..fcfc9195b 100644 --- a/internal/gui/assets/plugins/routing/client.js +++ b/internal/gui/assets/plugins/routing/client.js @@ -6581,6 +6581,23 @@ function routingPage(part) { } return lines; } + // the quota lines folded away from every group, as kept in this window + // (Sdsi134a on X: with many plans the list grew long); shown, as before, + // until the toolbar's Hide plans is clicked + let gPlansHid = false; + try { gPlansHid = localStorage.getItem("magpie.routingGroupPlans") === "hidden"; } catch {} + function plansButton() { + const b = el("button", "text rt-gplans", t(gPlansHid ? "Show plans" : "Hide plans")); + b.type = "button"; + b.title = t("What is left of each provider's accounts, under every group"); + b.onclick = () => { + gPlansHid = !gPlansHid; + try { localStorage.setItem("magpie.routingGroupPlans", gPlansHid ? "hidden" : "shown"); } catch {} + b.textContent = t(gPlansHid ? "Show plans" : "Hide plans"); + gList.classList.toggle("rt-noplans", gPlansHid); + }; + return b; + } function drawGroups() { groupsQuotas = typeof quotas === "undefined" ? undefined : quotas; // the ones switched off are listed after the ones on, each part in the @@ -6615,6 +6632,9 @@ function routingPage(part) { // in a click (provider.Templates). Cards while the user has no group of // their own, a menu by New group after const own = all.some((g) => !g.auto), tpls = groups.templates || []; + // shown when a group has a line to fold, after the rows are drawn + const plans = gSel ? null : plansButton(); + if (plans) head.push(plans); if (!gSel) { if (own && tpls.length) head.push(templateButton(tpls)); const imp = el("button", "text rt-gimportbtn", t("Import")); @@ -6673,6 +6693,8 @@ function routingPage(part) { rows.push(h); } // the import box's text stays in the page as the list is drawn again + if (plans) plans.hidden = !rows.some((r) => r.querySelector?.(".rt-gquota")); + gList.classList.toggle("rt-noplans", gPlansHid); const was = slotsOf(); replaceKeeping(gList, rows); morphSlots(was); diff --git a/internal/gui/assets/plugins/routing/i18n.js b/internal/gui/assets/plugins/routing/i18n.js index 1ba6e983b..1631da63a 100644 --- a/internal/gui/assets/plugins/routing/i18n.js +++ b/internal/gui/assets/plugins/routing/i18n.js @@ -7,6 +7,9 @@ magpie.strings({ "sign-in refused (answered since)": "登录曾被拒(之后已应答)", "Free first uses it before paid ones, and Cheapest first counts it as costing nothing": "免费优先会先用它再用付费的,最便宜优先把它算作不花钱", "{name}, {n} accounts: {windows}": "{name}({n} 个账号):{windows}", + "Hide plans": "隐藏套餐", + "Show plans": "显示套餐", + "What is left of each provider's accounts, under every group": "每个供应商的账号还剩多少,显示在每个组下面", "What is left of this provider's accounts the group can use, together: amounts of one unit added up, a share the mean of the accounts on one plan. Each account is on the Usage page.": "本组可用的这家供应商账号合起来还剩多少:同一单位的额度相加,百分比取同一套餐账号的平均值。各账号详情在用量页。", "Each session's context window": "各会话的上下文窗口", "Show this session's newest request": "显示这个会话最新的请求", @@ -759,6 +762,9 @@ magpie.strings({ "sign-in refused (answered since)": "登入曾被拒(之後已應答)", "Free first uses it before paid ones, and Cheapest first counts it as costing nothing": "免費優先會先用它再用付費的,最便宜優先把它算作不花錢", "{name}, {n} accounts: {windows}": "{name}({n} 個帳號):{windows}", + "Hide plans": "隱藏套餐", + "Show plans": "顯示套餐", + "What is left of each provider's accounts, under every group": "每個供應商的帳號還剩多少,顯示在每個組下面", "What is left of this provider's accounts the group can use, together: amounts of one unit added up, a share the mean of the accounts on one plan. Each account is on the Usage page.": "本組可用的這家供應商帳號合起來還剩多少:同一單位的額度相加,百分比取同一方案帳號的平均值。各帳號詳情在用量頁。", "Each session's context window": "各會話的上下文視窗", "Show this session's newest request": "顯示這個會話最新的請求", @@ -1511,6 +1517,9 @@ magpie.strings({ "sign-in refused (answered since)": "サインイン拒否(その後は応答あり)", "Free first uses it before paid ones, and Cheapest first counts it as costing nothing": "無料優先は有料より先にこれを使い、最安優先はこれを費用ゼロとして数えます", "{name}, {n} accounts: {windows}": "{name}({n} アカウント):{windows}", + "Hide plans": "プランを隠す", + "Show plans": "プランを表示", + "What is left of each provider's accounts, under every group": "各プロバイダのアカウントの残り。各グループの下に表示", "What is left of this provider's accounts the group can use, together: amounts of one unit added up, a share the mean of the accounts on one plan. Each account is on the Usage page.": "このグループが使えるこのプロバイダーのアカウントの残りの合計:同じ単位の量は足し合わせ、割合は同じプランのアカウントの平均。各アカウントは使用量ページにあります。", "Each session's context window": "セッションごとのコンテキストウィンドウ", "Show this session's newest request": "このセッションの最新のリクエストを表示", @@ -2263,6 +2272,9 @@ magpie.strings({ "sign-in refused (answered since)": "Anmeldung abgelehnt (seitdem beantwortet)", "Free first uses it before paid ones, and Cheapest first counts it as costing nothing": "„Kostenlos zuerst“ nutzt es vor bezahlten, „Günstigste zuerst“ zählt es als kostenlos", "{name}, {n} accounts: {windows}": "{name}, {n} Konten: {windows}", + "Hide plans": "Tarife ausblenden", + "Show plans": "Tarife zeigen", + "What is left of each provider's accounts, under every group": "Was von den Konten jedes Anbieters übrig ist, unter jeder Gruppe", "What is left of this provider's accounts the group can use, together: amounts of one unit added up, a share the mean of the accounts on one plan. Each account is on the Usage page.": "Was von den Konten dieses Anbieters, die die Gruppe nutzen kann, zusammen übrig ist: Mengen derselben Einheit addiert, ein Anteil als Mittelwert der Konten mit demselben Tarif. Die einzelnen Konten stehen auf der Seite Nutzung.", "Each session's context window": "Kontextfenster jeder Sitzung", "Show this session's newest request": "Die neueste Anfrage dieser Sitzung zeigen", diff --git a/internal/gui/assets/plugins/routing/routing.css b/internal/gui/assets/plugins/routing/routing.css index c90194760..b42882fe4 100644 --- a/internal/gui/assets/plugins/routing/routing.css +++ b/internal/gui/assets/plugins/routing/routing.css @@ -300,6 +300,8 @@ :is(#view-routing, #view-groups) .row-head .note { font-size: 11px; color: var(--faint); margin-left: 8px; } #view-routing .row-head .rt-livedot + .note { margin-left: 8px; } .rt-gnew { display: inline-flex; align-items: center; gap: 4px; } +/* the toolbar's Hide plans: every group's quota lines folded */ +.rt-groups.rt-noplans .rt-gquota { display: none; } /* the groups page's toolbar: the filter first, then how many */ .rt-gsec > .row-head:first-child .rt-gfilter { flex: 0 1 300px; width: auto; margin: 0; } .rt-gsec > .row-head:first-child .rt-gcount { margin-left: 10px; white-space: nowrap; } diff --git a/internal/gui/tests/routing-group-plans.test.cjs b/internal/gui/tests/routing-group-plans.test.cjs new file mode 100644 index 000000000..db6fe3439 --- /dev/null +++ b/internal/gui/tests/routing-group-plans.test.cjs @@ -0,0 +1,168 @@ +// Run with Node's test runner and Playwright on the module path; see README.md. +// Routing groups' quota lines fold (Sdsi134a on X): each group listed every +// provider's "WorkBuddy AI, 4 accounts: …" under its members, so with many +// plans the page grew long. The toolbar has Hide plans, before Templates: +// it folds those lines under every group (the name, its id and the members' +// line stay), says Show plans then, and the choice is kept across a +// reload. Shown, as before, until it is clicked; no button when no group +// has such a line. Every language, Chromium and WebKit, wide and a phone's +// width. +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(process.env.MAGPIE_ROUTING_ASSETS || path.join(__dirname, "../assets")); +const models = [ + { id: "a/m", name: "m", providerName: "A", icon: "generic" }, + { id: "b/m", name: "m", providerName: "B", icon: "generic" }, + { id: "a/n", name: "n", providerName: "A", icon: "generic" }, +]; +const info = (ids) => ids.map((id) => ({ id, ready: true, provider: id.split("/")[0], name: id.split("/")[0].toUpperCase() })); +const groups = [ + { id: "auto-m", name: "Model M", members: ["a/m", "b/m"], auto: true, ready: true, memberInfo: info(["a/m", "b/m"]) }, + { id: "mine", name: "Mine", members: ["a/n", "b/m"], ready: true, memberInfo: info(["a/n", "b/m"]) }, +]; +// the whole toolbar: Templates too, beside Import, Select and New group +const templates = [{ kind: "fast", group: { id: "fastest-first", name: "Fastest first", members: ["a/m", "b/m"], routing: "fast" } }]; +// two providers, two accounts each: a line for each under both groups +const quotas = ["a", "b"].flatMap((provider) => ["1@example.com", "2@example.com"].map((user) => ({ + provider, name: provider.toUpperCase(), kind: "subscription", icon: "generic", plan: "Pro", user, readAt: new Date().toISOString(), + windows: [{ name: "Credits", amount: 400, limit: 500, unit: "credits", used: 20, resetsAt: new Date(Date.now() + 9 * 864e5).toISOString() }], +}))); + +const words = { + en: { hide: "Hide plans", show: "Show plans" }, + zh: { hide: "隐藏套餐", show: "显示套餐" }, + "zh-TW": { hide: "隱藏套餐", show: "顯示套餐" }, + ja: { hide: "プランを隠す", show: "プランを表示" }, + de: { hide: "Tarife ausblenden", show: "Tarife zeigen" }, +}; + +function serve(lang, web, withQuotas) { + const state = { agents: [{ id: "claude", name: "Claude Code", path: "/test/claude", fields: [] }], profiles: [], settings: { lang, theme: "light" } }; + 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:${web}};` }); + 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/plugins") return json({ plugins: [] }); + 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: 0, totals: { requests: 0, rerouted: 0, errors: 0 }, routes: [] }); + } + if (url.pathname === "/api/gateway/history") return json({ cut: false, days: [], routes: [] }); + if (url.pathname === "/api/groups") return json({ models, pools: [], deciders: [], found: true, groups, templates }); + if (url.pathname === "/api/usage/quotas") return json(withQuotas ? quotas : []); + 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 }); + }; +} + +// the page's boot under the whole suite's load (#3478) +const boot = 15000; + +for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { + for (const lang of Object.keys(words)) { + for (const { width, web } of [{ width: 1280, web: false }, { width: 420, web: true }]) { + const w = words[lang]; + test(`${engine} ${lang} ${width}px: Hide plans folds every group's quota lines, kept across a reload`, async (t) => { + const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" })); + const page = await (await browser.newContext({ viewport: { width, height: 700 }, reducedMotion: "reduce" })).newPage(); + t.after(async () => { + if (process.env.ARTIFACT_DIR) { + await fs.mkdir(process.env.ARTIFACT_DIR, { recursive: true }); + await page.screenshot({ path: path.join(process.env.ARTIFACT_DIR, `${engine}-${lang}-${width}-routing-group-plans.png`) }); + } + await browser.close(); + }); + page.setDefaultTimeout(5000); + page.setDefaultNavigationTimeout(60000); + const errors = []; + page.on("pageerror", (e) => errors.push(e.message)); + await page.route("**/*", serve(lang, web, true)); + await page.goto("http://magpie.test/?view=groups"); + const view = page.locator("#view-groups"); + const lines = view.locator(".rt-group .rt-gquota"); + await lines.first().waitFor({ timeout: boot }); + assert.equal(await lines.count(), 4, "a line a provider under each group"); + const head = view.locator(".rt-gsec > .row-head").first(); + const btn = head.locator(".rt-gplans"); + + // shown at first, as before; the button says what a click does, + // before Templates, Import and Select + assert.equal(await btn.isVisible(), true); + assert.equal((await btn.textContent()).trim(), w.hide); + assert.equal(await btn.evaluate((b) => b.nextElementSibling?.classList.contains("rt-gtplbtn")), true, "before Templates"); + for (const c of ["rt-gtplbtn", "rt-gimportbtn", "rt-gselect", "rt-gnew"]) assert.equal(await head.locator("." + c).isVisible(), true, c); + for (const l of await lines.all()) assert.equal(await l.isVisible(), true); + assert.equal(await head.locator("select").count(), 0, "no native select"); + const headOver = await head.evaluate((h) => h.scrollWidth - h.clientWidth); + assert(headOver <= 0, `nothing overflows the toolbar (${headOver}px)`); + const over = await page.evaluate(() => document.documentElement.scrollWidth - innerWidth); + assert(over <= 0, `no sideways scroll (${over}px)`); + const border = await btn.evaluate((e) => { const c = getComputedStyle(e); return c.borderLeftWidth !== c.borderRightWidth || c.borderLeftColor !== c.borderRightColor; }); + assert.equal(border, false, "no left-border accent"); + + // folded: every line gone, the rest of each card stays; the button + // where it was + const at = await btn.boundingBox(); + await btn.click(); + assert.equal((await btn.textContent()).trim(), w.show); + for (const l of await lines.all()) assert.equal(await l.isVisible(), false); + const mine = view.locator('.rt-group[data-id="mine"]'); + assert.equal(await mine.locator(".nm b").isVisible(), true, "its name"); + assert.equal(await mine.locator(".nm code.mdl").isVisible(), true, "its id"); + assert.equal(await mine.locator(".main > .mem:not(.rt-gquota)").first().isVisible(), true, "its members"); + const after = await btn.boundingBox(); + assert(Math.abs(after.y - at.y) < 1, "the button stayed put"); + + // new quotas draw the list again: still folded + await page.evaluate(() => { quotas = quotas.map((q) => ({ ...q })); }); + await page.waitForTimeout(1300); + for (const l of await lines.all()) assert.equal(await l.isVisible(), false); + + // kept across a reload + await page.reload(); + await lines.first().waitFor({ state: "attached", timeout: boot }); + assert.equal((await btn.textContent()).trim(), w.show); + for (const l of await lines.all()) assert.equal(await l.isVisible(), false); + + // and shown again + await btn.click(); + assert.equal((await btn.textContent()).trim(), w.hide); + for (const l of await lines.all()) assert.equal(await l.isVisible(), true); + await page.reload(); + await lines.first().waitFor({ timeout: boot }); + assert.equal((await btn.textContent()).trim(), w.hide); + assert.deepEqual(errors, []); + }); + } + } +} + +// no line under any group: nothing to fold, no button +for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { + test(`${engine}: no quota line, no Hide plans`, async (t) => { + const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" })); + t.after(() => browser.close()); + const page = await (await browser.newContext({ viewport: { width: 1280, height: 700 }, reducedMotion: "reduce" })).newPage(); + page.setDefaultTimeout(5000); + page.setDefaultNavigationTimeout(60000); + const errors = []; + page.on("pageerror", (e) => errors.push(e.message)); + await page.route("**/*", serve("en", false, false)); + await page.goto("http://magpie.test/?view=groups"); + await page.locator('#view-groups .rt-group[data-id="mine"]').waitFor({ timeout: boot }); + await page.waitForFunction(() => Array.isArray(quotas), null, { timeout: boot }); + await page.waitForTimeout(1300); + assert.equal(await page.locator("#view-groups .rt-gquota").count(), 0); + assert.equal(await page.locator("#view-groups .rt-gplans").isVisible(), false); + assert.deepEqual(errors, []); + }); +}