Files
magpie/internal/gui/tests/api-key-usage.test.cjs
T
yetone 18e82adc62 gui: the window's pages down a sidebar in groups, the open page's tabs under it, and a button to have the header's tabs instead
From 1024 points wide (SIDE_AT) the window lays its pages down the left
as a Mac app's sidebar: Work (Agents, Sessions, Library), Models
(Providers, Routing, Gateway), Insights (Usage), Extend (Plugins), with
Settings and the gateway's address at the foot. The open page's own tabs
(Usage, Library, Plugins, Settings) are listed under it, opening and
folding on an iOS-like spring, and the page's strip is hidden. The header
becomes a breadcrumb, and pages whose head held only tools have them in
the title bar. Graphite: the open page is in the text's ink, not purple.
A pressed row darkens and never shrinks. The selection under the tabs
sets off as the row is pressed, on a 0.3s curve (it felt behind the
finger), and is the list's only shape: a sub-row hovered or pressed has
no ground of its own, which showed a block under the finger before the
selection slid there. The page's tab is clicked at once, so what is
typed next goes to it.

The title bar's button (or ⌃⌘S, Ctrl+Shift+S), there at every width,
picks the sidebar or the header's tabs. The choice is kept in
localStorage magpie.navLayout (side or top) at every width, so it is laid
out from the first frame; picked in a narrow window the sidebar is 184
points under 860. The two trade places in
a view transition: the sidebar slides, each tab flies to its place, and
the page slides as one. Without view transitions or under reduced
motion, the change is at once.

With nothing picked, narrower than 1024 the page beside a sidebar would
be narrower than Usage's requests table needs, so the header's tabs come
back. A resize
drops what a click held (heldWidth). Usage's KPI strip goes two to a line
by the room the page gives it (a container query), and Japanese counts
wrap there. Tests whose window is 1024+ were updated for the sidebar,
and docs/subsystems/gui-shell.md says how it works.

Run: sidebar.test.cjs and side-toggle.test.cjs 16/16 in chromium and
webkit, in en, zh, zh-TW, ja and de. Each was broken by hand (press
shrink, address cut, press glide, sub-row ground, view transition, kept
choice) and went red. Every page at 560 points with the sidebar picked,
zh and de, both engines: nothing scrolls sideways. session-identity's
ledger row sits under the footer beside the sidebar; WebKit's click
doesn't scroll to it, so the test wheels it up first. Animation frames were checked from a recorded video. The 105
affected GUI files in both engines; their reds were rerun alone and fixed
(filter-ime: the sub-row's click now at once; session-identity, above) or
fail the same on main (tray-logos). The full GUI suite in chromium, on
the rebase onto d5cae267a, was cut short at 352 tests on the owner's
"ship": 6 reds (account-alias, account-models, codex-auto-review,
codex-credits, context-text) each pass alone, here and on main d5cae267a,
so they are timing under the full run's load and are to be filed. After
rebasing onto e4e22b23e: sidebar, side-toggle, session-identity,
filter-ime, library-instr-busy, usage-speed-count and view-scrollbar,
50/50 in chromium and webkit. go build ./... passes.
2026-10-12 00:41:34 +08:00

178 lines
12 KiB
JavaScript

// The real provider editor and Usage page, with isolated API fixtures.
// Provider keys stay manageable; Usage's gateway keys identify the calling client.
const assert = require("node:assert/strict");
const fs = require("node:fs/promises");
const path = require("node:path");
const crypto = require("node:crypto");
const { test } = require("node:test");
const { chromium, webkit } = require("playwright");
const assets = path.resolve(__dirname, "../assets");
const fingerprint = (key) => crypto.createHash("sha256").update(key).digest("hex").slice(0, 10);
const PERSONAL = fingerprint("test-personal"), TEAM = fingerprint("test-team");
const totals = (rows) => ({
calls: rows.length, errors: rows.filter((r) => r.status >= 400).length,
input: rows.reduce((n, r) => n + r.in, 0), output: rows.reduce((n, r) => n + r.out, 0),
cache_read: rows.reduce((n, r) => n + (r.cache_read || 0), 0), cache_write: 0, reasoning: 0,
cost: rows.reduce((n, r) => n + r.cost, 0), unpriced: 0,
});
function fixture(lang, theme, posts, requests) {
const relay = {
id: "relay", name: "Relay", icon: "generic", chat: "https://relay.example/v1", responses: "", anthropic: "",
models: [{ id: "m", name: "Model", on: true }], agents: [], fallback: [], headers: {}, routing: "", catalog: "",
key: { set: true, masked: "test-…onal" }, balanceToken: { takes: false, set: false },
keyList: [
{ id: PERSONAL, name: "Personal", masked: "test-…onal", active: true, on: true },
{ id: TEAM, name: "Team", masked: "test-…team", active: false, on: true },
],
};
const providers = { providers: [relay], presets: [], excluded: [], gateway: { running: true, window: true } };
const rows = [
{ callerKeyId: "server", callerKeyName: "Server", in: 2000, out: 200, cost: 0.4, cache_read: 1000, status: 200, agent: "claude", agentName: "Claude Code" },
{ callerKeyId: "laptop", callerKeyName: "Laptop", in: 1000, out: 100, cost: 0.2, status: 200, agent: "codex", agentName: "Codex" },
{ callerKeyId: "laptop", callerKeyName: "Laptop", in: 0, out: 0, cost: 0, status: 429, agent: "codex", agentName: "Codex" },
{ in: 50, out: 5, cost: 0.01, status: 200, agent: "codex", agentName: "Codex" },
].map((r, i) => ({ t: new Date(Date.now() - i * 60e3).toISOString(), provider: "relay", providerName: "Relay", host: "relay.example",
model: "m", req: "relay/m", ms: 1000, priced: true, icon: "generic", ...r }));
const ledger = (q) => {
let filtered = rows;
if (q.get("callerKey")) filtered = filtered.filter((r) => r.callerKeyId === q.get("callerKey"));
if (q.get("agent")) filtered = filtered.filter((r) => r.agent === q.get("agent"));
if (q.get("failed") === "1") filtered = filtered.filter((r) => r.status >= 400);
if (q.get("q")) filtered = filtered.filter((r) => (r.callerKeyName || r.model).toLowerCase().includes(q.get("q").toLowerCase()));
const offset = +q.get("offset") || 0;
return { ...totals(filtered), total: filtered.length, offset, period: q.get("period"),
rows: filtered.slice(offset, offset + (+q.get("limit") || 100)),
callerKeys: callerKeys(), agents: [{ id: "codex", name: "Codex" }, { id: "claude", name: "Claude Code" }] };
};
const callerKeys = () => ["server", "laptop"].map((id) => {
const rs = rows.filter((r) => r.callerKeyId === id);
return { id, callerKeyId: id, name: rs[0].callerKeyName, icon: "generic", ...totals(rs) };
});
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 = ${JSON.stringify({ lang, theme, 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: { lang, theme } });
if (url.pathname === "/api/providers") return json(providers);
if (url.pathname === "/api/groups") return json({ groups: [], models: [] });
if (url.pathname.startsWith("/api/keys/")) {
const action = url.pathname.split("/").at(-1), body = req.postDataJSON();
posts.push({ action, body });
const key = relay.keyList.find((k) => k.id === body.ref);
if (action === "add") relay.keyList.push({ id: fingerprint(body.key), name: body.name, masked: "test-…new", on: true, active: false });
if (action === "rename") key.name = body.name;
if (action === "off" || action === "on") key.on = action === "on";
if (action === "use") { for (const k of relay.keyList) k.active = k === key; }
if (action === "remove") relay.keyList = relay.keyList.filter((k) => k !== key);
return json(providers);
}
if (url.pathname === "/api/usage/quotas") return json([]);
if (url.pathname === "/api/usage") return json({ ...totals(rows), callerKeys: callerKeys(), agents: [], models: [], series: [], bucket: "day", path: "~/.config/magpie/usage.jsonl" });
if (url.pathname === "/api/usage/requests") { requests.push(url.searchParams); return json(ledger(url.searchParams)); }
if (url.pathname === "/api/usage/requests/export") {
requests.push(Object.assign(url.searchParams, { method: req.method() }));
return json({ path: "~/Downloads/keys.csv", rows: ledger(url.searchParams).total });
}
if (url.pathname.startsWith("/api/")) return json({});
const file = path.join(assets, url.pathname === "/" ? "index.html" : url.pathname);
await route.fulfill({ body: await fs.readFile(file), contentType: { ".html": "text/html", ".js": "text/javascript", ".css": "text/css", ".svg": "image/svg+xml" }[path.extname(file)] });
};
}
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of ["en", "zh"]) {
test(`${engine} ${lang}: manage keys and inspect their separate usage`, 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: 1000, height: 740 }, reducedMotion: "reduce" })).newPage();
page.setDefaultTimeout(5000);
const errors = [], posts = [], requests = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", fixture(lang, "light", posts, requests));
const w = lang === "zh"
? { addButton: "添加", first: "设为首选", remove: "移除", failed: "失败", all: "全部网关密钥" }
: { addButton: "Add", first: "Make first", remove: "Remove", failed: "Failed", all: "All gateway keys" };
await page.goto("http://magpie.test/?view=providers");
await page.locator(".row.provider", { hasText: "Relay" }).click();
assert.equal(await page.locator(".accts .acc:not(.add)").count(), 2);
await page.locator(".accts .acc.add").click();
await page.locator(".acc.adding .kf input").nth(0).fill("Backup");
await page.locator(".acc.adding .kf input").nth(1).fill("test-new");
await page.locator(".acc.adding").getByRole("button", { name: w.addButton, exact: true }).click();
await page.locator(".accts .acc", { hasText: "Backup" }).waitFor();
assert.equal(posts.at(-1).action, "add");
assert.equal(posts.at(-1).body.key, "test-new");
const backup = () => page.locator(".accts .acc", { hasText: "Backup" });
await backup().locator(".dot.tick").click();
await page.waitForFunction(() => [...document.querySelectorAll(".accts .acc.off")].some((r) => r.textContent.includes("Backup")));
assert.equal(posts.at(-1).action, "off");
await backup().locator(".dot.tick").click();
await page.waitForFunction(() => [...document.querySelectorAll(".accts .acc.in-use")].some((r) => r.textContent.includes("Backup")));
const team = () => page.locator(".accts .acc", { hasText: "Team" });
await team().getByRole("button", { name: w.first, exact: true }).click();
await team().locator(".using").waitFor();
assert.equal(posts.at(-1).body.ref, TEAM);
await team().locator(".rename").click();
await page.locator(".rename-in").fill("Workspace");
await page.locator(".rename-in").press("Enter");
await page.locator(".accts .acc .rename", { hasText: "Workspace" }).waitFor();
assert.equal(posts.at(-1).action, "rename");
await backup().hover();
await backup().getByRole("button", { name: w.remove, exact: true }).click();
const confirmation = page.getByRole("alertdialog");
assert.match(await confirmation.textContent(), /Backup/);
await confirmation.getByRole("button", { name: w.remove, exact: true }).click();
await page.waitForFunction(() => ![...document.querySelectorAll(".accts .acc")].some((r) => r.textContent.includes("Backup")));
await page.locator("#modal").getByRole("button", { name: lang === "zh" ? "取消" : "Cancel", exact: true }).click();
await page.locator("#modal").waitFor({ state: "hidden" });
await page.locator('nav [data-view="usage"]').click();
await page.locator("#usageKeys .row").first().waitFor();
assert.deepEqual(await page.locator("#usageKeys .name").allTextContents(), ["Server", "Laptop"]);
assert.deepEqual(await page.locator("#usageKeys .num b").allTextContents(), ["2.2K", "1.1K"]);
assert.deepEqual(await page.locator("#usageKeys .cost").allTextContents(), ["≈$0.400", "≈$0.200"]);
assert(!/Workspace|Personal/.test(await page.locator("#usageKeys").textContent()));
await page.locator(":is(#usageTab .opt, .nav-s):visible").nth(1).click();
await page.locator(".led tbody tr").first().waitFor();
assert.equal(await page.locator(".led tbody tr").first().locator("td").nth(3).textContent(), "Relay · relay.example");
assert((await page.locator(".led tbody tr").first().locator("td").nth(3).getAttribute("title")).includes("relay.example"));
await page.locator("#ledKey").click();
await page.locator(".sess-menu .pm-item", { hasText: "Laptop" }).click();
await page.waitForFunction(() => document.querySelectorAll(".led tbody tr").length === 2);
assert.equal(requests.at(-1).get("callerKey"), "laptop");
assert(!requests.at(-1).has("key"));
assert.equal(requests.at(-1).get("offset"), "0");
await page.locator("#ledStatus .opt", { hasText: w.failed }).click();
await page.waitForFunction(() => document.querySelectorAll(".led tbody tr").length === 1);
assert.equal(requests.at(-1).get("callerKey"), "laptop");
assert.equal(requests.at(-1).get("failed"), "1");
await page.locator("#ledExport").click();
await page.waitForFunction(() => document.querySelector("#status").textContent.includes("keys.csv"));
const exported = requests.findLast((q) => q.method === "POST");
assert.equal(exported.get("callerKey"), "laptop");
assert(!exported.has("key"));
assert.equal(exported.get("failed"), "1");
assert(!exported.has("offset") && !exported.has("limit"));
await page.locator("#ledStatus .opt").first().click();
await page.locator("#ledKey").click();
await page.locator(".sess-menu .pm-item", { hasText: w.all }).click();
await page.waitForFunction(() => document.querySelectorAll(".led tbody tr").length === 4);
assert(!requests.at(-1).has("key") && !requests.at(-1).has("callerKey"));
assert.equal(await page.locator(".led tbody tr").last().locator("td").nth(3).textContent(), "Relay · relay.example");
await page.setViewportSize({ width: 560, height: 700 });
// WebKit applies the sidebar's media rule to the new width at the next
// frame (matchMedia already answers for it), so measure after that frame
await page.waitForFunction(() => !document.body.classList.contains("side-nav"));
await page.evaluate(() => new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r))));
assert(await page.locator("#view-usage").evaluate((v) => v.scrollWidth <= v.clientWidth));
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}-api-key-usage.png`) });
}
assert.deepEqual(errors, []);
});
}
}