Files
magpie/internal/gui/tests/web-page.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

157 lines
9.5 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// The page `magpie web` serves to a browser (Jorben on Discord: the remote
// web UI had no request archive switch and no usage chart icons, and
// /wails/runtime.js was a 404 in DevTools). In a browser there is no Wails
// runtime, so the page never asks for /wails/runtime.js; the app's window
// still does, for its close and maximise. Neither the request archive's
// switch, over the Usage page's requests, nor that page's chart and its
// ranking's icons wait on it: in magpie web the switch is there and posts
// settings/archive, and the chart draws its columns with each provider's icon
// beside it. In English and
// Chinese, Chromium and WebKit, with the API 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 WHO = [
{ id: "anthropic", name: "Claude", icon: "claudecode-color", agent: "claude", model: "claude-sonnet-5", tokens: 900000, cost: 0.9 },
{ id: "codex", name: "Codex", icon: "codex-color", agent: "codex", model: "gpt-6-luna", tokens: 400000, cost: 0.4 },
];
const share = (id, name, icon, tokens, cost) => ({ id, name, icon, calls: 2, errors: 0, input: tokens * 0.5, output: tokens * 0.5, cache_write: 0, cache_read: 0, cost });
const SERIES = Array.from({ length: 24 }, (_, h) => {
const on = h >= 9 && h <= 18;
const by = { provider: {}, agent: {}, model: {} };
for (const w of on ? WHO : []) {
by.provider[w.id] = { calls: 2, tokens: w.tokens, cost: w.cost };
by.agent[w.agent] = { calls: 2, tokens: w.tokens, cost: w.cost };
by.model[w.model] = { calls: 2, tokens: w.tokens, cost: w.cost };
}
const tokens = on ? WHO.reduce((a, w) => a + w.tokens, 0) : 0;
return { label: String(h).padStart(2, "0"), time: new Date(midnight.getTime() + h * 3600e3).toISOString(), calls: on ? 4 : 0, errors: 0, input: tokens / 2, output: tokens / 2, cache_write: 0, cache_read: 0, cost: on ? 1.3 : 0, by };
});
const LEDGER = {
period: "today", rows: [{ t: new Date().toISOString(), agent: "claude", agentName: "Claude Code", icon: "claudecode-color", provider: "anthropic", providerName: "Claude", model: "claude-sonnet-5", req: "sonnet", in: 2, out: 600, cache_write: 0, cache_read: 0, ms: 2380, status: 200, cost: 0.087, priced: true }],
offset: 0, total: 1, calls: 40, errors: 0, input: 6.5e6, output: 6.5e6, cache_write: 0, cache_read: 0, reasoning: 0, cost: 13, unpriced: 0, bucket: "hour", series: SERIES,
by: {
provider: WHO.map((w) => share(w.id, w.name, w.icon, w.tokens * 10, w.cost * 10)),
agent: WHO.map((w) => share(w.agent, w.agent, w.icon, w.tokens * 10, w.cost * 10)),
model: WHO.map((w) => share(w.model, w.model, "", w.tokens * 10, w.cost * 10)),
},
agents: [{ id: "claude", name: "Claude Code", icon: "claudecode-color" }, { id: "codex", name: "Codex", icon: "codex-color" }],
providers: WHO.map((w) => ({ id: w.id, name: w.name, icon: w.icon })),
};
function serve(lang, web, seen) {
const gateway = { running: true, window: true, mine: true, url: "http://127.0.0.1:3999", groups: [], calls: [], archive: { on: false, bucket: "bkt/team on https://s3.example.com" } };
const providers = { providers: [], presets: [], excluded: [], gateway };
return async (route) => {
const url = new URL(route.request().url());
const json = (data, status = 200) => route.fulfill({ status, json: data });
seen.paths.push(url.pathname);
if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:${web}};` });
// magpie web serves no Wails runtime: a 404, as it answers
if (url.pathname === "/wails/runtime.js") return web ? route.fulfill({ status: 404, contentType: "text/plain", body: "404 page not found\n" }) : route.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
if (url.pathname === "/api/state") return json({ agents: [], profiles: [], settings: { lang, theme: "light" } });
if (url.pathname === "/api/providers") return json(providers);
if (url.pathname === "/api/groups") return json({ groups: [], models: [] });
if (url.pathname === "/api/plugins") return json({ plugins: [] });
if (url.pathname === "/api/gateway/trace") {
if (url.searchParams.get("wait")) await new Promise((r) => setTimeout(r, 20e3));
return json({ mine: true, now: new Date().toISOString(), seq: 1, totals: { requests: 0, rerouted: 0, errors: 0 }, routes: [] });
}
if (url.pathname === "/api/settings/archive") {
const body = route.request().postDataJSON();
seen.archive.push(body);
gateway.archive = { ...gateway.archive, on: body.on };
return json(gateway.archive);
}
if (url.pathname === "/api/usage/requests") return json(LEDGER);
if (url.pathname === "/api/usage/quotas") return json([]);
if (url.pathname === "/api/usage") return json({ calls: 1, errors: 0, input: 1, output: 1, cache_read: 0, cache_write: 0, reasoning: 0, unpriced: 0, cost: 1, bucket: "day", series: [], agents: [], models: [], path: "~/.config/magpie/usage.jsonl" });
if (url.pathname === "/api/sessions") return json({ sessions: [], dirs: [] });
if (url.pathname === "/api/sessions/stats") return json({ from: "", to: "", days: [], agents: {} });
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)];
try {
await route.fulfill({ body: await fs.readFile(file), contentType });
} catch {
await route.fulfill({ status: 404, body: "" });
}
};
}
const words = {
en: { name: "Request archive", provider: "Provider" },
zh: { name: "请求存档", provider: "供应商" },
};
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of ["en", "zh"]) {
const w = words[lang];
test(`${engine} ${lang}: magpie web asks for no Wails runtime, and has the archive switch and the usage chart's icons`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
const open = async (web) => {
const page = await (await browser.newContext({ viewport: { width: 1180, height: 800 }, reducedMotion: "reduce" })).newPage();
page.setDefaultTimeout(5000);
const errors = [], failed = [];
page.on("pageerror", (e) => errors.push(e.message));
page.on("response", (r) => r.status() >= 400 && failed.push(new URL(r.url()).pathname));
const seen = { paths: [], archive: [] };
await page.route("**/*", serve(lang, web, seen));
await page.goto("http://magpie.test/?view=gateway");
return { page, errors, failed, seen };
};
const { page, errors, failed, seen } = await open(true);
await page.locator("#gateway .dot").waitFor();
// the request archive's switch, over the Usage page's requests
await page.locator('[data-view="usage"]').first().click();
await page.locator(":is(#usageTab .opt, .nav-s):visible").nth(1).click();
const sw = page.locator("#ledArchive .led-arch-sw");
await sw.waitFor();
assert.equal(await sw.textContent(), w.name);
assert.equal(await sw.getAttribute("aria-checked"), "false");
const y = () => page.locator("#view-usage").evaluate((v) => v.scrollTop);
await sw.scrollIntoViewIfNeeded();
const before = await y();
await sw.click();
for (let i = 0; i < 50 && !seen.archive.length; i++) await page.waitForTimeout(40);
assert.deepEqual(seen.archive, [{ on: true }]);
await page.locator('#ledArchive .led-arch-sw[aria-checked="true"]').waitFor();
assert.equal(await y(), before, "the click moved the page");
// the Usage page's chart, its ranking by provider with their icons
await page.locator("#ledRank .rk").first().waitFor();
await page.locator("#ledSplit").getByText(w.provider, { exact: true }).click();
assert((await page.locator("#ledChart rect.col").count()) > 0, "the chart drew no columns");
const icons = page.locator("#ledRank .rk .ic img");
await page.waitForFunction(() => {
const imgs = [...document.querySelectorAll("#ledRank .rk .ic img")];
return imgs.length >= 2 && imgs.every((i) => i.complete && i.naturalWidth > 0);
});
assert.deepEqual((await icons.evaluateAll((is) => is.map((i) => new URL(i.src).pathname))).sort(), ["/icons/claudecode-color.svg", "/icons/codex-color.svg"]);
// and never the desktop's runtime, so nothing failed to load
assert(!seen.paths.includes("/wails/runtime.js"), "magpie web asked for /wails/runtime.js");
assert.deepEqual(failed, []);
assert.deepEqual(errors, []);
// the app's window still takes its runtime, for close and maximise
const app = await open(false);
await app.page.locator("#gateway .dot").waitFor();
for (let i = 0; i < 50 && !app.seen.paths.includes("/wails/runtime.js"); i++) await app.page.waitForTimeout(40);
assert(app.seen.paths.includes("/wails/runtime.js"), "the app's window no longer loads its runtime");
assert.deepEqual(app.errors, []);
});
}
}