* usage: Requests lists the agents' own session-file calls, put to the vendor and account their files prove, charts them by provider, agent or model, shows what was said, and the tray panel gets a Usage tab The Usage page's Requests tab now covers requests an agent sends with its own sign-in (Claude Desktop's Code tab and Cowork, Claude Code, Codex), which never pass the gateway, and shows more of every request: why it failed, what was said in it, who it was of. Requests tab: a strip of four totals (tokens, requests, cost, cache hit rate); the trend of one metric — tokens, cost or requests — as columns by the hour, day or week, told apart by provider, agent or model (both remembered), beside a ranking of the same that is its legend and a way in (a click lists only that provider's or agent's requests; a provider picker sits beside the agent's). The ranking is as tall as the chart and scrolls in itself, with no scrollbar: a slim rail in the card's margin shows how much there is and where it is (its thumb dragged, the rail clicked), and the edge with more beyond it fades; under the chart (860px and narrower) it shows whole. A row opens to its details: the vendor's error type and words, request id, endpoint, and what was said in it (input and output, read from the agent's session file when opened, the call that ended nearest the request — nothing stored). A refresh period (off / 5 s / 10 s / 30 s / 1 min) and refresh-now. The Overview says it counts gateway calls only; Requests and the panel say they count both. The tray panel gets a Usage tab (today / 7 / 30 days, provider picker, four totals, a small chart, top five, "Open Usage"), and is as tall as its tallest tab (#124). Provider and account of a session-file call: never magpie's "claude" or "codex" subscription. Anthropic's when Claude Desktop's session metadata (not Claude-3p) names the account and organization, with that account's email; OpenAI's when Codex's rollout names a provider that is OpenAI's in config.toml — the built-in "openai", or a named one at OpenAI's address or signed in with the ChatGPT account and no base_url (requires_openai_auth) — with the creator account's email when the file records it. Anything else is "Local session", its recorded provider id and session account kept (CSV: session_provider, session_account). The config read is today's: a file names the provider id only. Gateway: a failed call keeps the vendor's error text and type, request id and endpoint; a Claude subscription's failure uses the status Claude Code reports (api_error_status). A request turned away before any vendor was asked is logged, marked rejected, listed in Requests and left out of every total. The client's own session header is kept as native_session beside X-Magpie-Session, and a session-file call is matched to a gateway record by request id, then by that session, equal tokens and an end within 2 s, only when unique both ways. Reading: session-file calls live in the session index main keeps on disk, parsed incrementally and outside its lock (Claude Code, Cowork in both Claude folders, Codex); the usage log is read on from where it was; a page builds the ledger once. Days are counted on calendar dates (DST). CSV gains request_id, endpoint, error_message, error_type, source, session_provider, session_account. i18n: one "Endpoint" (终结点), which a second key had turned for Azure's field. Tested: go vet ./..., go build (also GOOS=linux/windows, -tags nogui) and go test -tags nogui ./... pass. The whole GUI suite in Chromium and WebKit, en and zh (Playwright 1.62.1): all 393 tests pass. On this computer's files (7 days): 3,330 Claude Desktop calls resolve to Anthropic with the account, 1,991 Codex calls on a named ChatGPT-signed-in provider to OpenAI (620 with the account; older rollouts don't record it), 9 Claude Code CLI calls stay Local session. Co-authored-by: yetone <yetoneful@gmail.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * usage: a session-file call of one of magpie's own Codex or Claude accounts is told as that provider, beside the gateway's calls on it — "OpenAI" and "Codex" (or "Anthropic" and "Claude Code") read as two vendors when they were one subscription Codex: an official route (above) whose creator account and user are one of magpie's Codex sign-ins (the CLI's own or a saved one) is magpie's "codex", with that account's email as the gateway's rows have it. Claude Desktop (not Claude-3p): an account and organization that are one of magpie's Claude sign-ins is magpie's "claude". Only while magpie has that provider signed in; another account, a rollout with no account recorded, or an ambiguous workspace stays OpenAI or Anthropic, and a relay stays Local session. Tested: TestSessionCallsOfMagpiesAccountsAreItsProviders (the account matched, another account, none recorded, magpie's account through a relay, Desktop in magpie's and another organization, Claude-3p, magpie signed out) and TestLedgerTellsCodexSessionCallsAsMagpiesCodex (a gateway Codex call and a session call of its account one provider of two calls, another account's OpenAI). On this computer (7 days): 3,439 Claude Desktop calls are magpie's Claude, 626 Codex calls its Codex, 1,322 older Codex rollouts with no account stay OpenAI. go test -tags nogui ./... and the usage GUI tests pass. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: 修复请求统计缓存膨胀与会话账号误归属 #239 调用明细改为按会话文件分片保存并限制内存缓存,汇总索引仅保留统计数据;网关日志按需读取。 严格匹配订阅账号与用户 ID,排除自定义渠道、非官方端点覆盖、外部 CODEX_HOME 及关闭的供应商。补齐零 token 失败去重、Codex 原始会话 ID、拒绝标记导出和面板显示修复。 通过全量 Go 测试、静态检查、受影响模块的并发检查、52 项浏览器测试及 macOS/Linux/Windows 构建;使用 35 万条合成调用验证索引体积与内存改善。 * fix: 降低请求统计刷新与写入开销并明确本地账号展示 #239 请求分片改为追加帧和字符串去重,分页复用未变化的紧凑快照,采用 LRU 并移出全局锁内的磁盘读取。缓存 Cowork 目录,补齐同前缀改写检测及崩溃临时文件清理。 本地记录仅显示匹配账号或本地会话,明确官方登录标记与请求路由的区别。默认今日与模型维度,Auto Review 复用共享目录定价并单独标注估价参考。 完整 Go 测试、vet、相关 race 和 52 项浏览器回归通过;提供 2000 文件及四个大会话连续追加的可复现性能对照。 * fix: 优化用量徽标与耗时分级并保护分页边界 #239 官方登录、API、订阅和本地会话采用独立颜色与柔和底色,兼容明暗主题。耗时按 10 秒及 30 秒阈值着色,保留估算标识与原始毫秒提示;无有效耗时显示短横线。 限制分页选取范围,避免超大偏移整数溢出及越界空页的无效堆构造。补充阈值、徽标对比度和极端分页参数回归。 * fix: 将账号与来源徽标分为两行展示 #239 * fix: 提高用量表格模型文字对比度 #239 发送模型和已知实际模型使用主题正文次级色,表头与时间同步加深。保留缺失值弱化、转发提示和本地日志来源说明。Chromium 与 WebKit 的中英文、浅深色主题 10 项界面检查通过。 --------- Co-authored-by: yetone <yetoneful@gmail.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
281 lines
20 KiB
JavaScript
281 lines
20 KiB
JavaScript
// Run with Node's test runner and Playwright on the module path; see README.md.
|
|
// The tray panel's Usage tab (the window's Requests, made small): what the
|
|
// requests of a period add up to — four totals, a small chart of them by the
|
|
// hour or day, and who they were of, seven at most — for today, seven or thirty
|
|
// days, over one provider when one is picked (the chart then tells its models
|
|
// apart); a click on a provider in the ranking, or the picker beside the
|
|
// period, switches to it; the metric can be tokens, cost or requests. Open
|
|
// Usage takes the window to the Requests of the provider shown, and the window
|
|
// opened so has that provider and agent picked. Nothing runs out of a narrow
|
|
// panel and a click leaves the panel where it is; the allowances keep their own
|
|
// tab. English and Chinese; no backend, the API is faked.
|
|
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 midnight = new Date();
|
|
midnight.setHours(0, 0, 0, 0);
|
|
|
|
const MODELS = { relay: ["gpt-6-sol"], anthropic: ["claude-sonnet-5", "claude-opus-5"] };
|
|
const WHO = [
|
|
{ id: "anthropic", name: "Claude", icon: "claudecode-color", hours: [8, 20], tokens: 900000, calls: 2, cost: 0.9 },
|
|
{ id: "relay", name: "Relay", icon: "generic", hours: [10, 14], tokens: 400000, calls: 3, cost: 0.4 },
|
|
];
|
|
const shareOf = (id, name, icon, calls, tokens, cost, errors = 0) => ({ id, name, icon, calls, errors, input: tokens * 0.01, output: tokens * 0.005, cache_write: tokens * 0.05, cache_read: tokens * 0.935, cost });
|
|
|
|
function page(q) {
|
|
const only = q.get("provider");
|
|
const who = WHO.filter((w) => !only || w.id === only);
|
|
const days = q.get("period") === "today" ? 1 : q.get("period") === "7d" ? 7 : 30;
|
|
const hourly = days === 1;
|
|
const n = hourly ? 24 : days;
|
|
const series = Array.from({ length: n }, (_, i) => {
|
|
const time = new Date(hourly ? midnight.getTime() + i * 3600e3 : midnight.getTime() - (n - 1 - i) * 864e5);
|
|
const on = (w) => (hourly ? i >= w.hours[0] && i <= w.hours[1] : i % 2 === 0) ? 1 : 0;
|
|
const by = { provider: {}, agent: {}, model: {} };
|
|
let calls = 0, tokens = 0, cost = 0;
|
|
for (const w of who) {
|
|
if (!on(w)) continue;
|
|
const models = MODELS[w.id];
|
|
for (const m of models) {
|
|
const part = { calls: w.calls / models.length, tokens: w.tokens / models.length, cost: w.cost / models.length };
|
|
by.model[m] = part;
|
|
}
|
|
by.provider[w.id] = { calls: w.calls, tokens: w.tokens, cost: w.cost };
|
|
calls += w.calls; tokens += w.tokens; cost += w.cost;
|
|
}
|
|
return { label: String(i), time: time.toISOString(), calls, errors: 0, input: tokens * 0.01, output: tokens * 0.005, cache_write: tokens * 0.05, cache_read: tokens * 0.935, cost, by };
|
|
});
|
|
const sum = (k) => series.reduce((a, p) => a + p[k], 0);
|
|
const tot = (pick) => { const m = {}; for (const p of series) for (const [k, v] of Object.entries(pick(p))) { m[k] = m[k] || { calls: 0, tokens: 0, cost: 0 }; m[k].calls += v.calls; m[k].tokens += v.tokens; m[k].cost += v.cost; } return m; };
|
|
const facet = (w) => {
|
|
const hours = hourly ? w.hours[1] - w.hours[0] + 1 : Math.ceil(n / 2);
|
|
return shareOf(w.id, w.name, w.icon, w.calls * hours, w.tokens * hours, w.cost * hours, w.id === "relay" ? 3 : 0);
|
|
};
|
|
const models = Object.entries(tot((p) => p.by.model)).map(([id, x]) => shareOf(id, id, "", x.calls, x.tokens, x.cost));
|
|
models.sort((a, b) => b.cache_read - a.cache_read);
|
|
return {
|
|
period: q.get("period"), rows: [], offset: 0, total: sum("calls") ? 1 : 0, calls: sum("calls"), errors: only === "relay" || !only ? 3 : 0,
|
|
input: sum("input"), output: sum("output"), cache_write: sum("cache_write"), cache_read: sum("cache_read"), reasoning: 0, cost: sum("cost"), unpriced: 0,
|
|
bucket: hourly ? "hour" : "day", series,
|
|
by: { provider: WHO.map(facet).sort((a, b) => b.cache_read - a.cache_read), agent: [], model: models },
|
|
agents: [{ id: "codex", name: "Codex", icon: "codex-color" }], providers: WHO.map((w) => ({ id: w.id, name: w.name, icon: w.icon })),
|
|
};
|
|
}
|
|
|
|
function serve(lang, asked, opened, fitted = [], resize) {
|
|
const state = { agents: [{ id: "codex", name: "Codex", path: "/test/config.toml", fields: [] }], profiles: [], settings: { lang, theme: "light" } };
|
|
return async (route) => {
|
|
const req = route.request(), url = new URL(req.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:true};` });
|
|
if (url.pathname === "/wails/runtime.js") return route.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
|
|
if (url.pathname === "/api/state") return json(state);
|
|
if (url.pathname === "/api/usage/requests") { asked.push(url.searchParams); return json(page(url.searchParams)); }
|
|
if (url.pathname === "/api/gateway/trace") 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/usage") return json({ calls: 1, input: 1, output: 1, cache_read: 0, cache_write: 0, reasoning: 0, cost: 0, unpriced: 0, bucket: "day", series: [], agents: [], models: [] });
|
|
if (url.pathname === "/api/usage/quotas") return json([]);
|
|
if (url.pathname === "/api/groups") return json({ groups: [] });
|
|
if (url.pathname === "/api/providers") return json({ providers: [], gateway: { running: true, window: true } });
|
|
if (url.pathname === "/api/window/main") { opened.push(url.search); return route.fulfill({ status: 204 }); }
|
|
if (url.pathname === "/api/window/fit") {
|
|
const h = Number(url.searchParams.get("h"));
|
|
fitted.push(h);
|
|
if (resize) await resize(h);
|
|
return route.fulfill({ status: 204 });
|
|
}
|
|
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 route.fulfill({ body: await fs.readFile(file), contentType });
|
|
};
|
|
}
|
|
|
|
const want = {
|
|
en: { tab: "Usage", quota: "Allowances", periods: ["Today", "7 days", "30 days"], blocks: ["Tokens", "Requests", "Cost", "Cache hit rate"], all: "All providers", open: "Open Usage", metrics: ["Tokens", "Cost", "Requests"] },
|
|
zh: { tab: "用量", quota: "额度", periods: ["今天", "7 天", "30 天"], blocks: ["Token", "请求", "费用", "缓存命中率"], all: "全部供应商", open: "打开用量", metrics: ["Token", "费用", "请求"] },
|
|
};
|
|
|
|
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
|
|
for (const lang of ["en", "zh"]) {
|
|
test(`${engine} ${lang}: the tray panel's Usage tab`, async (t) => {
|
|
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
|
|
const errors = [], pages = [];
|
|
t.after(async () => {
|
|
if (process.env.ARTIFACT_DIR) {
|
|
await fs.mkdir(process.env.ARTIFACT_DIR, { recursive: true });
|
|
for (const [i, p] of pages.entries()) await p.screenshot({ path: path.join(process.env.ARTIFACT_DIR, `${engine}-${lang}-panel-usage-${i}.png`) });
|
|
}
|
|
await browser.close();
|
|
});
|
|
const w = want[lang];
|
|
const fitted = [];
|
|
const open = async (url, viewport, asked, opened) => {
|
|
const p = await (await browser.newContext({ viewport, reducedMotion: "reduce" })).newPage();
|
|
pages.push(p);
|
|
p.setDefaultTimeout(5000);
|
|
p.on("pageerror", (e) => errors.push(e.message));
|
|
await p.route("**/*", serve(lang, asked, opened, fitted, (h) => p.setViewportSize({width: p.viewportSize().width, height:Math.max(220,Math.min(560,h))})));
|
|
await p.goto(url);
|
|
return p;
|
|
};
|
|
const settled = async (asked, ok) => {
|
|
for (let i = 0; i < 60 && !(asked.length && ok(asked.at(-1))); i++) await new Promise((r) => setTimeout(r, 40));
|
|
assert(asked.length && ok(asked.at(-1)), "asked: " + (asked.at(-1) || ""));
|
|
};
|
|
|
|
const asked = [], opened = [];
|
|
const p = await open("http://magpie.test/?mode=panel", { width: 440, height: 340 }, asked, opened);
|
|
assert.equal((await p.locator('[data-ptab="stats"]').textContent()).trim(), w.tab);
|
|
assert.equal((await p.locator('[data-ptab="usage"]').textContent()).trim(), w.quota, "the allowances keep their tab");
|
|
await p.locator('[data-ptab="usage"][hidden]').waitFor({state:"attached"});
|
|
assert.equal(await p.locator("#ptabs button:not([hidden])").count(), 3, "no allowances means no allowances tab");
|
|
await p.locator('[data-ptab="stats"]').click();
|
|
await p.locator("#panelUsage .pu-tot").waitFor();
|
|
assert(await p.locator("#panelUsage").isVisible());
|
|
assert.equal(await p.locator("#panelUsage > .usage-note").textContent(), lang === "zh" ? "统计网关调用与会话日志调用;本地拒绝的请求不计入汇总。" : "Gateway and session-log calls; local rejections excluded from totals.");
|
|
assert(!(await p.locator("#agents").isVisible()) && !(await p.locator("#panelRouting").isVisible()), "the others are other tabs");
|
|
assert.equal(asked[0].get("period"), "today");
|
|
assert.equal(asked[0].get("limit"), "1", "a page of one row is all it asks for");
|
|
|
|
// the period, the four totals, the chart, and the ranking of who
|
|
assert.deepEqual(await p.locator("#panelUsage .pu-bar .segs .opt").allTextContents(), w.periods);
|
|
assert.equal(await p.locator("#panelUsage .pu-bar .segs .opt.on").textContent(), w.periods[0]);
|
|
assert.deepEqual(await p.locator("#panelUsage .pu-tot .k").allTextContents(), w.blocks);
|
|
assert((await p.locator("#panelUsage .pu-tot .blk").nth(1).locator(".sub").textContent()).includes("3"), "the failures under the requests");
|
|
assert(await p.locator("#panelUsage rect.col").count() > 10, "columns");
|
|
assert.deepEqual(await p.locator("#panelUsage .led-rank .rk-nm").allTextContents(), ["Claude", "Relay"]);
|
|
assert.equal((await p.locator("#panelUsage .sess-pick").textContent()).trim(), w.all);
|
|
assert.deepEqual(await p.locator("#panelUsage .pu-card .segs .opt").allTextContents(), w.metrics);
|
|
// Labels in the 440px panel must be readable, not clipped by ellipsis.
|
|
assert(await p.locator("#panelUsage .pu-tot .blk").first().locator(".sub").evaluate(e =>
|
|
e.scrollWidth <= e.clientWidth + 1 && e.scrollHeight <= e.clientHeight + 1 && getComputedStyle(e).textOverflow !== "ellipsis"), "token input/output is fully visible");
|
|
// nothing runs out of the panel
|
|
const over = await p.evaluate(() => [...document.querySelectorAll("#panelUsage *")].filter((e) => e.scrollWidth > e.clientWidth + 1 && getComputedStyle(e).overflow === "visible" && e.children.length === 0 && e.tagName !== "text").length);
|
|
assert.equal(over, 0, "an element wider than itself");
|
|
assert(await p.evaluate(() => document.documentElement.scrollWidth <= innerWidth + 1), "the panel doesn't scroll sideways");
|
|
|
|
// a column under the pointer
|
|
const svg = await p.locator("#panelUsage .led-chart svg").boundingBox();
|
|
await p.mouse.move(svg.x + svg.width * (12.5 / 24), svg.y + svg.height * 0.5);
|
|
assert(await p.locator("#panelUsage .tip").isVisible());
|
|
const tip = await p.locator("#panelUsage .tip").innerText();
|
|
assert(tip.includes("Claude") && tip.includes("Relay"), tip);
|
|
await p.mouse.move(2, 2);
|
|
|
|
// Native fit includes the Usage tab, and all controls fit without scrolling.
|
|
const height = await p.evaluate(() => {
|
|
const body = document.body, tab = body.dataset.ptab;
|
|
delete body.dataset.ptab;
|
|
const h = document.querySelector('.top').offsetHeight + document.querySelector('#ptabs').offsetHeight + Math.max(...['agents','panelQuota','panelRouting','panelUsage'].map(id => document.getElementById(id).offsetHeight)) + document.querySelector('.foot').offsetHeight + 4;
|
|
body.dataset.ptab = tab;
|
|
return h;
|
|
});
|
|
for (let i=0;i<40 && fitted.at(-1) !== height;i++) await p.waitForTimeout(25);
|
|
assert.equal(fitted.at(-1),height,"the panel fits its tallest tab including Usage");
|
|
const scroll = () => p.locator("#view-agents").evaluate(v => v.scrollTop);
|
|
const before = await scroll();
|
|
await p.locator("#panelUsage .pu-card .segs .opt").nth(1).click();
|
|
assert((await p.locator("#panelUsage svg .axis").allTextContents()).some(s => /^[$¥]/.test(s)), "the cost's axis");
|
|
assert.equal(await scroll(),before,"a click moved the panel");
|
|
await p.locator("#panelUsage .pu-card .segs .opt").nth(0).click();
|
|
assert.equal(await scroll(),before,"a click moved the panel");
|
|
|
|
// the period: seven days is by the day
|
|
await p.locator("#panelUsage .pu-bar .segs .opt").nth(1).click();
|
|
await settled(asked, (q) => q.get("period") === "7d");
|
|
await p.locator("#panelUsage .pu-bar .segs .opt.on", { hasText: w.periods[1] }).waitFor();
|
|
await p.locator("#panelUsage:not(.pu-loading)").waitFor(); // the answer is in, not only asked for
|
|
assert((await p.locator("#panelUsage svg .axis").allTextContents()).some((s) => /\d\/\d|\d月/.test(s)), "days along the bottom");
|
|
|
|
// a click on a provider switches to it: its requests, its models in the chart
|
|
await p.locator("#panelUsage .pu-bar .segs .opt").nth(0).click();
|
|
await settled(asked, (q) => q.get("period") === "today");
|
|
await p.locator("#panelUsage:not(.pu-loading)").waitFor();
|
|
await p.locator("#panelUsage .led-rank .rk").first().click();
|
|
await settled(asked, (q) => q.get("provider") === "anthropic");
|
|
await p.locator("#panelUsage .sess-pick", { hasText: "Claude" }).waitFor();
|
|
await p.locator("#panelUsage:not(.pu-loading)").waitFor();
|
|
assert.deepEqual(await p.locator("#panelUsage .led-rank .rk-nm").allTextContents(), ["claude-sonnet-5", "claude-opus-5"], "its models");
|
|
assert.equal(await p.locator("#panelUsage rect.col").evaluateAll((r) => [...new Set(r.map((x) => x.dataset.k))].sort().join()), "claude-opus-5,claude-sonnet-5");
|
|
|
|
// Open Usage takes the window to that provider's requests
|
|
await p.locator("#panelUsage .pu-bar .text").click();
|
|
for (let i = 0; i < 50 && !opened.length; i++) await p.waitForTimeout(40);
|
|
assert.equal(opened.at(-1), "?view=usage&tab=requests&provider=anthropic");
|
|
assert.equal((await p.locator("#panelUsage .pu-bar .text").textContent()).trim(), w.open);
|
|
|
|
// the picker lists the providers, and all of them again
|
|
await p.locator("#panelUsage .sess-pick").click();
|
|
await p.locator(".sess-menu button, .sess-menu [role=option], .sess-menu .opt").filter({ hasText: w.all }).first().click();
|
|
await settled(asked, (q) => !q.get("provider"));
|
|
await p.locator("#panelUsage .sess-pick", { hasText: w.all }).waitFor();
|
|
await p.locator("#panelUsage:not(.pu-loading)").waitFor();
|
|
assert.deepEqual(await p.locator("#panelUsage .led-rank .rk-nm").allTextContents(), ["Claude", "Relay"]);
|
|
|
|
// a narrow panel
|
|
await p.setViewportSize({ width: 320, height: 640 });
|
|
await p.waitForTimeout(250);
|
|
assert(await p.evaluate(() => document.documentElement.scrollWidth <= innerWidth + 1), "no sideways scroll at 320");
|
|
const narrow = await p.locator("#panelUsage .led-chart svg").boundingBox(), card = await p.locator("#panelUsage .pu-card").boundingBox();
|
|
assert(narrow.x + narrow.width <= card.x + card.width + 1, "the chart fits at 320");
|
|
// nothing is cut off at the panel's edge: every control of the bar is whole, and the totals two to a row
|
|
const edge = await p.evaluate(() => [...document.querySelectorAll("#panelUsage .pu-bar > *")].filter((e) => e.getBoundingClientRect().right > innerWidth - 4).length);
|
|
assert.equal(edge, 0, "a control of the bar runs out of the panel");
|
|
assert.equal(await p.locator("#panelUsage .pu-tot").evaluate((k) => getComputedStyle(k).gridTemplateColumns.split(" ").length), 2, "two totals to a row when narrow");
|
|
|
|
// a panel that opens on the Usage tab, as the reader left it: the page draws it
|
|
// as it loads, before what it draws with is all set, and must not fail on it
|
|
const again = [];
|
|
const q = await (await browser.newContext({ viewport: { width: 440, height: 500 }, reducedMotion: "reduce" })).newPage();
|
|
pages.push(q);
|
|
q.setDefaultTimeout(5000);
|
|
q.on("pageerror", (e) => again.push(e.message));
|
|
await q.addInitScript(() => { try { localStorage.setItem("magpie.panelTab", "stats"); } catch {} });
|
|
await q.route("**/*", serve(lang, [], []));
|
|
await q.goto("http://magpie.test/?mode=panel");
|
|
await q.locator("#panelUsage .pu-tot").waitFor();
|
|
assert.equal(await q.locator('#ptabs [data-ptab="stats"]').getAttribute("aria-selected"), "true");
|
|
assert.deepEqual(again, [], "the panel failed to load on the Usage tab");
|
|
// and its other tabs still work from there
|
|
await q.locator('#ptabs [data-ptab="agents"]').click();
|
|
assert(await q.locator("#agents").isVisible() && !(await q.locator("#panelUsage").isVisible()));
|
|
// whichever tab the reader left it on, it loads without an error
|
|
for (const tab of ["agents", "usage", "stats", "routing", "profiles"]) {
|
|
const failed = [];
|
|
const x = await (await browser.newContext({ viewport: { width: 440, height: 500 }, reducedMotion: "reduce" })).newPage();
|
|
pages.push(x);
|
|
x.on("pageerror", (e) => failed.push(e.message));
|
|
await x.addInitScript((t) => { try { localStorage.setItem("magpie.panelTab", t); } catch {} }, tab);
|
|
await x.route("**/*", serve(lang, [], []));
|
|
await x.goto("http://magpie.test/?mode=panel");
|
|
await x.waitForTimeout(400);
|
|
assert.deepEqual(failed, [], `the panel failed to load on the ${tab} tab`);
|
|
assert.equal(await x.locator("#ptabs button:not([hidden])").count(), 3, `the tabs are there on ${tab}`);
|
|
}
|
|
|
|
// the window opened from the panel: the Requests tab, on that provider and agent
|
|
const w2 = await open("http://magpie.test/?view=usage&tab=requests&provider=relay&agent=codex", { width: 1100, height: 700 }, (asked.length = 0, asked), opened);
|
|
await w2.locator("#ledWrap, #ledDash").first().waitFor({ state: "attached" });
|
|
await settled(asked, (q) => q.get("provider") === "relay" && q.get("agent") === "codex");
|
|
assert.equal(await w2.locator("#usageTab .opt.on").textContent(), lang === "zh" ? "请求" : "Requests");
|
|
assert(!/provider=|tab=/.test(w2.url()), "the address is clean: " + w2.url());
|
|
// Every distinct chart color has a named legend row, with the rest grouped.
|
|
const ranked = await p.evaluate(() => {
|
|
const rows = Array.from({length:9}, (_,i) => ({id:"p"+i,name:"Provider "+i,calls:9-i,input:(9-i)*100,output:0,cache_read:0,cache_write:0,cost:0}));
|
|
const rank = document.createElement("div");
|
|
drawLedRank(rank,{by:{provider:rows}},"provider","tokens","",()=>{},true);
|
|
return {names:[...rank.querySelectorAll(".rk-nm")].map(x=>x.textContent), colors:[...rank.querySelectorAll(".rk-sw")].map(x=>x.style.background)};
|
|
});
|
|
assert.deepEqual(ranked.names.slice(0,7),Array.from({length:7},(_,i)=>"Provider "+i));
|
|
assert.equal(ranked.names.length,8);
|
|
assert.equal(new Set(ranked.colors.slice(0,7)).size,7);
|
|
assert.deepEqual(errors, []);
|
|
});
|
|
}
|
|
}
|