Files
magpie/internal/gui/tests/api-key-usage.test.cjs
T
yetone 07a62b3739 providers: Copy all keys, and magpie provider keys, carry a provider's keys to another magpie
@emilochen on X asked to batch-export one provider's keys, to sync them
between a local and a cloud magpie. Each key row could copy only its own
key (#1480), and nothing printed them all.

- A provider with two or more keys gets a "Copy all N keys" row under
  Add another key. It asks provider/key with allKeys (provider.AllKeys:
  the main key and every listed key, deduplicated, turned-off ones too)
  and copies them one a line, for the other magpie's Paste several
  (keys/import). The secret goes only to the clipboard on the click,
  through the page's copy(); nothing logs it, and the route sits behind
  the GUI API's existing local/origin guards. A signed-in account has
  no keys and is refused.
- The row is drawn by keyrates.js (copyAllKeysRow), already imported
  when a provider with two or more keys opens; keyRateField now takes
  which of its parts to draw. It has its own class, keys-copy-all, not
  .add, since ~21 GUI tests take .acc.add as the one adding row.
  TestCoreScriptBudget's core budget goes up by the 72 bytes left in
  app.js (the placing line and the helper's new argument); app.js stays
  under its own budget.
- CLI: `magpie provider keys <id>` prints the keys one a line;
  `magpie provider keys <id> add [key… | -]` adds them from words or
  stdin (SplitKeys + AddKeys), so
  `magpie provider keys relay | ssh cloud magpie provider keys relay add`
  works. A word starting with "-" is refused, never taken as a key.
- Strings in en, zh, zh-TW, ja, de.

Related: #5387 (sharing saved providers through magpie:// links), not
closed by this.

Run on mote (mote-run) at this tree on 11ddf076b (debc80271, its new parent, changes LESSONS.md only):
- gofmt -l internal/: clean. go vet (darwin) ./internal/provider
  ./internal/gui ./internal/cli; GOOS=linux go vet -tags nogui ./...;
  GOOS=windows go vet ./...; GOOS=linux go build -tags nogui and
  GOOS=windows go build: ok.
- go test -count=1 ./internal/provider ./internal/cli: ok;
  ./internal/gui: ok (TestCoreScriptBudget included, after the budget
  change).
- GUI, Chromium + WebKit: key-copy-all (en/zh/zh-TW/ja/de × 900/420px),
  api-key-usage, i18n-keys, key-price-rate, key-copy, key-weight,
  click-scroll: 104/104. key-copy-all again in WebKit twice: 15/15 each.
- GUI, 43 key/provider/i18n files at the change vs the same files at
  untouched 11ddf076b: the only new reds were api-key-usage's key-row
  count (it counted .acc:not(.add); now leaves keys-copy-all out). The
  other 67 reds are the same on the base and filed: #5835 key-switch,
  #6414 key-balance-card de, #6424 key-box-once, #5866 signin-proxy
  380, #6444 api-key-theme, #4126 key-replace 420, #4673
  provider-typed, #6416 key-import Paste several, and #6628 (filed now)
  login-export's Export row count.

Broken by hand: key-copy-all.test.cjs at untouched 11ddf076b assets
fails 10 of 15 in WebKit (the 5 that pass are "one key has no Copy
all"). The Go tests can't build on the old code (AllKeys, keysCmd).

Not tried: the real app and the system clipboard, the Windows and
Linux boxes, a real cloud/headless magpie over ssh, and the full Go
suite (mote and mica were both over their load limits; the three
touched packages and cross-OS vet ran instead).
2026-11-14 16:34:10 +08:00

179 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);
page.setDefaultNavigationTimeout(60000);
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):not(.keys-copy-all)").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, []);
});
}
}