Files
magpie/internal/gui/tests/routing-steady.test.cjs
T
yetone 98efd30a2e routing: requests coming in leave the routing groups where they are on the screen, a group being edited too (Jerell.OvO on Discord #general: on the window's Routing page the request log refreshes live and each refresh scrolls the page up, so the routing groups further down can't be read or edited; a switch to pause it or moving the log to another tab was suggested) — every request redraws what sits above the groups (the stage gains or loses agent and account rows, the story a line, the requests and accounts lists theirs), and app.js's backToReader kept the view's scrollTop as a fixed number, putting it back after any scroll the reader didn't make: WebKit has no scroll anchoring to make up for it and in Chromium backToReader undid the browser's own, so the groups slid up under the reader as the parts above shrank and down as they grew. In WebKit the accounts list, emptied and refilled every second for its countdowns, also made the view scroll on its own. Now a view can name a part of itself to keep in place (keepInView): its position on the screen is taken when the reader leaves it (their own scroll, or a click held), and backToReader follows it wherever the parts above take it, from a ResizeObserver so before paint, with the browser's own anchoring off for that view so the two don't fight; a view at its top stays at its top. The Routing page keeps the groups in place while they're in the upper half of the view or a field in them has the focus, else the lists, and with neither (the stage in sight) nothing moves. The accounts list is patched, keeping each row that's unchanged (rows with a button are always replaced, so their handler is the new one), not emptied and refilled. No live-update switch was needed and no new strings; no border stripe. Tested with a new internal/gui/tests/routing-steady.test.cjs in Chromium and WebKit: with the view wheeled down to the groups, one open in its editor and text typed into its name, six requests streamed in from the trace (new agents and accounts, failures and retries): the part above the groups really changed (more than 50 px), the section's head, the editor and the last group stayed within 1.5 px on the screen on every layout and scroll seen (WebKit scrolls by whole pixels while the parts above are laid out in fractions), the view's scrollTop moved by just what grew above them, and the field kept its focus and its text and went on taking typing. On origin/main it fails in both engines (the head moves 233 → 171 px). go build -tags nogui (and GOOS=linux, GOOS=windows), go test -tags nogui ./... and the whole GUI suite (126 tests) pass. Not tried in the real window with a live gateway.
2026-09-29 23:52:30 +08:00

180 lines
10 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// Requests coming in never move the Routing page (Jerell.OvO on Discord
// #general: as the request log refreshed, the page kept scrolling up, so
// the routing groups further down could not be read or edited). With the
// view scrolled down to the groups and one open in its editor, a name being
// typed, new requests stream in from the trace — new agents and accounts on
// the stage, a failure and a retry in the story, the list growing — and the
// groups stay where they are on the screen, the view's scrollTop with them,
// and the field keeps its focus and what was typed.
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 now = new Date();
const iso = (ms) => new Date(ms).toISOString();
const acct = (n) => ({ id: `acct-${n}`, provider: `prov${n}`, name: `Provider ${n}`, who: `user${n}@example.com`, kind: "account", model: `model-${n}`, known: true, used: 10 * n, plan: "Pro" });
const agentsOf = ["codex", "claude", "opencode", "kimi"];
// a request: from an agent, weighing some accounts, answered by the last
// of them after the others failed (more tries, a longer story)
function req(id, agent, ids, fails) {
const t0 = now.getTime() - (200 - id) * 1000;
const order = ids.map(acct);
const tries = order.slice(0, fails + 1).map((w, i) => ({
id: w.id, model: w.model, start: iso(t0 + i * 100), done: true, ms: 800,
...(i < fails ? { status: 429, fail: "rate", again: true, error: "rate limited, try again later" } : { status: 200 }),
}));
return { id, seq: id, time: iso(t0), agent, model: order[0].model, provider: order[0].provider, order, tries, done: true, status: 200, ms: 900 + fails * 800, tokens: 1500 };
}
const first = [req(100, "codex", [1], 0), req(101, "codex", [1, 2], 0)];
const later = [
req(102, "claude", [3, 4, 5], 2),
req(103, "opencode", [6, 1, 2, 3], 3),
req(104, "kimi", [7, 8], 1),
req(105, "codex", [1, 2, 3, 4, 5, 6], 4),
req(106, "claude", [2], 0),
req(107, "opencode", [8, 7, 6, 5, 4], 3),
];
const models = [1, 2, 3].map((n) => ({ id: `prov${n}/model-${n}`, name: `model-${n}`, providerName: `Provider ${n}`, icon: "generic" }));
const groups = {
models,
groups: [
{ id: "opus-anywhere", name: "Opus anywhere", members: [models[0].id, models[1].id], routing: "", ready: true },
{ id: "cheap", name: "Cheap", members: [models[2].id], routing: "order", ready: true },
],
pools: [1, 2, 3].map((n) => ({ provider: `prov${n}`, name: `Provider ${n}`, kind: "account", who: ["a", "b"], routing: "", affinity: "" })),
};
function serve(feed) {
const state = { agents: agentsOf.map((id) => ({ id, name: id[0].toUpperCase() + id.slice(1), path: `/test/${id}`, fields: [] })), profiles: [], settings: { lang: "en", theme: "light" } };
return async (route) => {
const url = new URL(route.request().url());
const json = (data) => route.fulfill({ json: data });
if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: 'window.bootPrefs = {lang:"en",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/gateway/trace") {
const trace = (routes) => json({ mine: true, now: new Date().toISOString(), seq: routes.length ? routes[routes.length - 1].seq : Number(url.searchParams.get("after")), totals: { requests: 0, rerouted: 0, errors: 0 }, routes });
if (!url.searchParams.get("wait")) return trace(first);
// the long poll: answered when the test feeds a request
const r = await new Promise((res) => { feed.next = res; });
return trace([r]);
}
if (url.pathname === "/api/gateway/history") return json({ cut: false, days: [], routes: [] });
if (url.pathname === "/api/groups") return json(groups);
if (url.pathname === "/api/providers") return json({ providers: [], excluded: [], gateway: { running: true, window: true, url: "http://127.0.0.1:3999" } });
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 view = "#view-routing";
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
test(`${engine}: requests coming in leave the routing groups where they are`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
const context = await browser.newContext({ viewport: { width: 1100, height: 760 } });
const page = await context.newPage();
page.setDefaultTimeout(5000);
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
const feed = { next: null };
await page.route("**/*", serve(feed));
t.after(async () => {
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}-steady.png`) });
}
feed.next?.(req(999, "codex", [1], 0));
await browser.close();
});
await page.goto("http://magpie.test/?view=routing");
await page.locator(".rt-req").nth(first.length - 1).waitFor();
await page.locator(".rt-group").first().waitFor();
await page.waitForTimeout(2000); // the first request's play
// down to the groups with the wheel (the view moves only for the
// reader), the second group in the middle of the view; then the first
// opened in its editor and a name typed into it
const dy = await page.locator(view).evaluate((v) => {
const g = [...document.querySelectorAll(".rt-group")].pop();
return Math.round(g.getBoundingClientRect().top - v.getBoundingClientRect().top - v.clientHeight / 2);
});
await page.mouse.move(550, 400);
await page.mouse.wheel(0, dy);
await page.waitForTimeout(400);
const scrolled = await page.locator(view).evaluate((v) => v.scrollTop);
assert(scrolled > 200, `the view must be scrolled down to the groups (${scrolled})`);
await page.locator(".rt-group").first().locator("button", { hasText: "Edit" }).click();
const name = page.locator(".rt-gedit input").first();
await name.click();
await name.press("End");
await page.keyboard.type(" and more");
const typed = await name.inputValue();
assert.equal(typed, "Opus anywhere and more");
await page.waitForTimeout(300);
// where things are: on the screen, and the groups' place in the page
// (which moves as what's above them grows or shrinks)
const where = () => page.evaluate((sel) => {
const v = document.querySelector(sel), vt = v.getBoundingClientRect().top;
const top = (e) => e.getBoundingClientRect().top - vt;
const gsec = document.querySelector(".rt-gsec");
return {
scrollTop: v.scrollTop,
inPage: gsec.getBoundingClientRect().top - vt + v.scrollTop,
onScreen: { head: top(gsec), editor: top(document.querySelector(".rt-gedit")), group: top([...document.querySelectorAll(".rt-group")].pop()) },
};
}, view);
const before = await where();
// every layout and every scroll while they come, after the page has
// had its say (these observers and listeners come after the page's):
// what the reader could see, not only where it ends
await page.evaluate((sel) => {
// (to a pixel and a half: WebKit scrolls by whole pixels, the
// parts above are laid out in fractions of one)
const v = document.querySelector(sel), ed = () => document.querySelector(".rt-gedit").getBoundingClientRect().top;
const want = ed();
window.__moves = [];
const look = (why) => { const at = ed(); if (Math.abs(at - want) > 1.5) window.__moves.push(`${why}: the editor at ${at}, not ${want}`); };
window.__ro = new ResizeObserver(() => look("layout"));
for (const e of [v, ...v.children, ...document.querySelector("#rtMore").children]) window.__ro.observe(e);
v.addEventListener("scroll", () => look("scroll"));
}, view);
for (const r of later) {
for (let i = 0; i < 50 && !feed.next; i++) await page.waitForTimeout(50);
const next = feed.next;
feed.next = null;
next(r);
await page.waitForTimeout(900);
}
await page.waitForTimeout(2500); // the plays end, the countdowns tick
assert.equal(await page.locator(".rt-req").count(), first.length + later.length, "every request must be listed");
const moves = await page.evaluate(() => { window.__ro.disconnect(); return window.__moves; });
const after = await where();
// what's above the groups did change: new agents and accounts on the
// stage, a longer story, more rows in the lists
assert(Math.abs(after.inPage - before.inPage) > 50, `the part above the groups must have changed (${before.inPage} → ${after.inPage})`);
// and the groups stayed where they were on the screen, the view
// scrolling by just what grew above them, on every frame
for (const k of Object.keys(before.onScreen)) assert(Math.abs(after.onScreen[k] - before.onScreen[k]) <= 1.5, `the ${k} moved: ${JSON.stringify([before, after])}`);
assert(Math.abs((after.scrollTop - before.scrollTop) - (after.inPage - before.inPage)) < 2, `scrollTop must follow the groups: ${JSON.stringify([before, after])}`);
assert.deepEqual(moves, [], "the page moved while the requests came");
// the field kept its focus and what was typed, and goes on taking it
assert.equal(await name.evaluate((e) => e === document.activeElement), true, "the field lost its focus");
assert.equal(await name.inputValue(), typed);
await page.keyboard.type("!");
assert.equal(await name.inputValue(), typed + "!");
assert.deepEqual(errors, []);
});
}