Files
magpie/internal/gui/tests/gateway-caller-keys.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

138 lines
10 KiB
JavaScript

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 { fixture, confirmKeyAction } = require("./fixtures/caller-keys.cjs");
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of ["en", "zh"]) {
test(`${engine} ${lang}: manage named caller keys and filter their 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: 760 }, reducedMotion: "reduce" })).newPage();
page.setDefaultTimeout(6000);
await page.addInitScript(() => {
window.callerFocus = [];
const focus = HTMLElement.prototype.focus;
HTMLElement.prototype.focus = function (options) {
if (this.closest("#gatewayKeys") || this.closest("#modal")) {
callerFocus.push({ input: this.tagName === "INPUT", preventScroll: options?.preventScroll === true });
}
return focus.call(this, options);
};
});
const events = [], errors = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", fixture(lang, "light", events, { lan: true }));
const w = lang === "zh"
? { create: "创建", name: "网关密钥名称", remove: "移除", copy: "复制网关密钥", disable: "停用密钥", enable: "启用密钥", requests: "请求", usage: "用量" }
: { create: "Create", name: "Gateway key name", remove: "Remove", copy: "Copy gateway key", disable: "Disable key", enable: "Enable key", requests: "Requests", usage: "Usage" };
await page.goto("http://magpie.test/?view=gateway");
await page.locator("#gatewayKeys .acc[data-key]").last().waitFor();
assert.equal(await page.locator("#gatewayKeys .accts").count(), 1);
assert.equal(await page.locator("#gatewayKeysBlock").isVisible(), true);
assert.equal(await page.locator("#gatewayKeys .acc.add").count(), 0, "creation is offered once, in the header");
await page.locator("#addGatewayKey").click();
assert.equal(await page.evaluate(() => callerFocus.at(-1)?.preventScroll), true, "creating a key focuses without scrolling");
await page.getByRole("button", { name: w.create, exact: true }).click();
await page.waitForFunction(() => document.querySelector("#gatewayKeys .adding .primary")?.disabled === false);
await page.getByRole("textbox", { name: w.name, exact: true }).fill("Tablet");
await page.getByRole("textbox", { name: w.name, exact: true }).press("Enter");
let tablet = page.locator("#gatewayKeys .acc[data-key]", { hasText: "Tablet" });
await tablet.waitFor();
assert.equal(await page.locator("#gatewayKeys .acc[data-key]").count(), 4);
await tablet.getByRole("button", { name: w.copy, exact: true }).click();
await page.waitForFunction(() => document.querySelector("#gatewayKeys .copy.done"));
assert(events.some((e) => e.action === "clipboard" && e.body.text === "fixture-created-1"));
await tablet.getByRole("button", { name: w.disable, exact: true }).click();
await tablet.getByRole("button", { name: w.enable, exact: true }).waitFor();
await tablet.getByRole("button", { name: w.enable, exact: true }).click();
await tablet.getByRole("button", { name: w.disable, exact: true }).waitFor();
await tablet.getByRole("button", { name: "Tablet", exact: true }).click();
assert.equal(await page.evaluate(() => callerFocus.at(-1)?.preventScroll), true, "renaming a key focuses without scrolling");
await page.locator("#gatewayKeys .rename-in").fill("Travel");
await page.locator("#gatewayKeys .rename-in").press("Enter");
tablet = page.locator("#gatewayKeys .acc[data-key]", { hasText: "Travel" });
await tablet.waitFor();
await page.locator("#nav").getByRole("button", { name: w.usage, exact: true }).click();
await page.locator("#usageKeys .row").last().waitFor();
assert.equal(await page.locator("#usageKeys .row").count(), 3);
assert.match(await page.locator("#usageKeys").textContent(), /Laptop/);
assert.equal(await page.locator("#usageKeysHead").textContent(), lang === "zh" ? "网关密钥" : "Gateway keys");
assert.equal(await page.locator("#usageCallerKeys").count(), 0, "there is only one API key section");
await page.locator(":is(#usageTab .opt, .nav-s):visible", { hasText: new RegExp("^" + w.requests + "(?!\\p{L})", "u") }).click();
await page.locator("#ledKey").click();
await page.locator(".sess-menu .pm-item", { hasText: "Laptop" }).click();
await page.waitForFunction(() => document.querySelectorAll("#ledWrap tbody tr").length === 1);
assert.match(await page.locator("#ledWrap").textContent(), /Laptop/);
await page.locator("#ledExport").click();
await page.waitForTimeout(150);
assert(events.some((e) => e.action === "export" && e.query.includes("callerKey=laptop") && !e.query.includes("key=") && !e.query.includes("user=")));
await page.reload();
await page.locator("#prefs").click();
await page.locator(":is(#setTab-network, .nav-s[data-of=setTab-network]):visible").click();
const share = page.locator("#lanList").getByRole("button", { name: lang === "zh" ? "开启" : "On", exact: true });
await share.waitFor();
// Real wheel input lets the page remember the reader's scroll position.
const bounds = await share.boundingBox();
await page.mouse.move(500, 400);
await page.mouse.wheel(0, bounds.y - 250);
await share.click();
await page.locator("#lanList .lan-address-row").waitFor();
assert.equal(await page.locator("#lanList").getByRole("button", { name: w.copy, exact: true }).count(), 0,
"Settings does not duplicate key management");
await page.locator("#nav").getByRole("button", { name: lang === "zh" ? "网关" : "Gateway", exact: true }).click();
await page.waitForFunction(() => document.querySelectorAll("#gatewayKeys .acc[data-key]").length === 5);
let lanRow = page.locator("#gatewayKeys .acc[data-key]", { has: page.locator(".rename", { hasText: /^Magpie$/ }) });
const lanID = await lanRow.getAttribute("data-key");
const before = await lanRow.locator(".plan").textContent();
await lanRow.getByRole("button", { name: w.copy, exact: true }).click();
await page.waitForFunction(() => document.querySelector("#gatewayKeys .copy.done"));
assert(events.some((e) => e.action === "clipboard" && e.body.text === "fixture-lan-1"));
const rotateLabel = lang === "zh" ? "轮换密钥" : "Rotate key";
const cancelLabel = lang === "zh" ? "取消" : "Cancel";
await lanRow.getByRole("button", { name: rotateLabel, exact: true }).click();
assert.equal(await page.evaluate(() => callerFocus.at(-1)?.preventScroll), true, "confirmation focuses without scrolling");
await page.locator("#modal").getByRole("button", { name: cancelLabel, exact: true }).click();
await page.locator("#modal").waitFor({ state: "hidden" });
assert.equal(events.filter((e) => e.action === "rotate-key").length, 0);
await confirmKeyAction(page, lanRow, rotateLabel);
await page.waitForFunction((masked) => document.querySelector('#gatewayKeys .acc[data-key^="lan-key"] .plan')?.textContent !== masked, before);
assert.equal(await lanRow.getAttribute("data-key"), lanID, "rotation keeps the key's identity");
await lanRow.getByRole("button", { name: w.copy, exact: true }).click();
await page.waitForFunction(() => document.querySelector("#gatewayKeys .copy.done"));
assert(events.some((e) => e.action === "clipboard" && e.body.text === "fixture-rotated-2"));
await lanRow.getByRole("button", { name: w.remove, exact: true }).click();
await page.keyboard.press("Escape");
await page.locator("#modal").waitFor({ state: "hidden" });
assert.equal(events.filter((e) => e.action === "remove-key").length, 0);
await confirmKeyAction(page, lanRow, w.remove);
await page.waitForFunction(() => document.querySelectorAll("#gatewayKeys .acc[data-key]").length === 4);
await page.locator("#prefs").click();
await page.locator(":is(#setTab-network, .nav-s[data-of=setTab-network]):visible").click();
await page.locator("#lanList .lan-address-row").waitFor();
await page.locator("#lanList").getByRole("button", { name: lang === "zh" ? "关闭" : "Off", exact: true }).click();
await page.locator("#lanList .lan-address-row").waitFor({ state: "detached" });
await share.click();
await page.locator("#lanList .lan-address-row").waitFor();
await page.locator("#nav").getByRole("button", { name: lang === "zh" ? "网关" : "Gateway", exact: true }).click();
await page.waitForFunction(() => document.querySelectorAll("#gatewayKeys .acc[data-key]").length === 5);
lanRow = page.locator("#gatewayKeys .acc[data-key]", { has: page.locator(".rename", { hasText: /^Magpie$/ }) });
assert.notEqual(await lanRow.getAttribute("data-key"), lanID, "recreated LAN key appears without a page reload");
assert.equal(await page.locator(`#gatewayKeys .acc[data-key="${lanID}"]`).count(), 0);
tablet = page.locator("#gatewayKeys .acc[data-key]", { hasText: "Travel" });
await tablet.waitFor();
await confirmKeyAction(page, tablet, w.remove);
await page.waitForFunction(() => document.querySelectorAll("#gatewayKeys .acc[data-key]").length === 4);
await page.setViewportSize({ width: 560, height: 740 });
assert.equal(await page.evaluate(() => document.documentElement.scrollWidth > window.innerWidth), false);
assert.deepEqual(errors, []);
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}-gateway-keys.png`) });
}
});
}
}