Files
magpie/internal/gui/tests/routing-fold-all.test.cjs
T
yetone 326e72f4c5 routing: the "No session ID" group folds like the others, and one button folds or opens every group (zzzkl on Discord)
zzzkl: the Routing page's 未提供会话标识 group couldn't be collapsed. Its
head was a div with no click handler, no arrow and no aria-expanded,
because only a group with a key could be folded (`g.key ? "button" :
"div"`). closedSessions is keyed by the group's key, and "" is a key a Set
holds, so the no-session group now uses the same button, arrow and
handler as the rest.

Also, as asked: an icon button in the request list's existing header row
(.rt-fold-all, beside Replay) folds every group while any is open, and
opens them all otherwise. It shows only when the list is grouped (by
session or by key) into two or more groups. It has a tooltip and an
aria-label only, no text: "Collapse all" / "Expand all". Those two words
moved from plugins/library/i18n.js to the core i18n.js (zh, zh-TW, ja,
de), since Library and Routing now both say them (i18n-keys: a word used
by two plugins is the core's). The core script budget doesn't count
i18n.js or plugins/, so it isn't raised.

routing-sessions and inner-scroll-contain found group heads by
`button.rt-session` with a key; they now filter .rt-session by text, as
the no-session head is a button too.

Run (mote, node --test, Chromium + WebKit):
- routing-fold-all.test.cjs (new): 2 engines x 1100/420 x en, zh,
  zh-TW, ja, de. It covers the no-session group folding on click, the
  fold-all button folding then opening every group, its tooltip in each
  language, and it being hidden with one group.
- i18n-keys, i18n-en-no-cjk, routing-sessions, inner-scroll-contain,
  skill-fold-all, routing-row-swap, routing-rule-drag,
  routing-order-unknown, routing-many-keys, in one run at concurrency
  3 with routing-fold-all: 170 tests, 157 ok, 13 red. Every red is also
  red on a clean origin/main 8bfc551ee checkout run alone (concurrency
  1, 42 tests, 14 red), and each already has an issue:
  i18n-keys "a plugin's words are its own" ("View routing", "No session
  ID" said by Go tests, #5332), routing-many-keys "2 keys in sight"
  (#4082), routing-order-unknown webkit de 420 (#4627),
  routing-row-swap (#3799), routing-rule-drag chromium (#3789, #5088).
  routing-fold-all, routing-sessions, inner-scroll-contain,
  skill-fold-all and i18n-en-no-cjk are all ok.

Broken by hand: on the old client.js, routing-fold-all fails with "the
no-session group can't be folded" (and finds no fold-all button).

Not tried: the real app's WebKitGTK on Linux, or Windows WebView2.
2026-11-05 22:07:46 +08:00

141 lines
8.3 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// zzzkl on Discord: grouped by session, the "No session ID" group couldn't be
// folded while every other group could. Now it folds like the rest, and one
// icon button in the list's head folds every group, or opens them all when
// none is open. Only shown while the list is grouped and has two groups or
// more. In Chromium and WebKit, at 1100 and 420 wide, in every language.
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 { reach } = require("./reader.cjs");
const assets = path.resolve(__dirname, process.env.MAGPIE_ROUTING_ASSETS || "../assets");
const now = new Date();
const day = [now.getFullYear(), now.getMonth() + 1, now.getDate()].map((n) => String(n).padStart(2, "0")).join("-");
const stamp = (d) => {
const o = -d.getTimezoneOffset(), p = (n) => String(Math.floor(Math.abs(n))).padStart(2, "0");
return new Date(d.getTime() + o * 60e3).toISOString().slice(0, 19) + (o < 0 ? "-" : "+") + p(o / 60) + ":" + p(o % 60);
};
function req(id, session) {
const time = stamp(new Date(now.getTime() - (10 - id) * 60e3));
const seat = { id: "relay", provider: "relay", name: "Relay", who: "Test account", kind: "account", model: "model-a" };
return { id, seq: id, time, agent: "claude", session, model: "relay/model-a", provider: "relay", cost: 0.01, priced: true,
order: [seat], tries: [{ id: seat.id, model: seat.model, start: time, done: true, status: 200, ms: 4500 }],
done: true, status: 200, ms: 4500, tokens: 2000, ttft: 1200 };
}
const routes = [req(1, "chat-a"), req(2, ""), req(3, "chat-b"), req(4, "chat-a"), req(5, "")];
function serve(lang) {
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:"${lang}",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({ agents: [{ id: "claude", name: "Claude Code", fields: [] }], profiles: [], settings: { lang, theme: "light" } });
if (url.pathname === "/api/gateway/trace") {
if (url.searchParams.has("wait")) return new Promise(() => {}); // no live update
return json({ mine: true, now: now.toISOString(), seq: 5, totals: { requests: 5, rerouted: 0, errors: 0 }, routes });
}
if (url.pathname === "/api/gateway/history") return json({ cut: false, days: [{ day, requests: routes.length }], routes: [] });
if (url.pathname === "/api/gateway/session-titles") return json({});
if (url.pathname === "/api/clis") return json({ agents: [], providers: [] });
if (url.pathname === "/api/groups") return json({ groups: [] });
if (url.pathname === "/api/providers") return json({ providers: [], gateway: { running: true, window: true } });
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 KEYS = ["No session ID", "Collapse all", "Expand all"];
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const width of [1100, 420]) {
for (const lang of ["en", "zh", "zh-TW", "ja", "de"]) {
test(`${engine} ${width} ${lang}: every session group folds, the one with no session id too, and all fold at once`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
const page = await browser.newPage({ viewport: { width, height: 800 }, reducedMotion: "reduce" });
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}-${width}-${lang}-routing-fold-all.png`) });
}
await browser.close();
});
page.setDefaultTimeout(5000);
page.setDefaultNavigationTimeout(60000);
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", serve(lang));
await page.goto("http://magpie.test/?view=routing");
const view = page.locator("#view-routing");
await view.locator(".rt-req").nth(4).waitFor();
await view.locator(".rt-group-by button").nth(1).click();
await view.locator(".rt-session").nth(2).waitFor();
const w = await page.evaluate((keys) => Object.fromEntries(keys.map((k) => [k, t(k)])), KEYS);
if (lang !== "en") assert.deepEqual(KEYS.filter((k) => w[k] === k), [], `${lang} lacks these`);
const rows = () => view.locator(".rt-req").count();
const groups = view.locator(".rt-session");
const bare = groups.filter({ hasText: w["No session ID"] });
const fold = view.locator(".rt-req-head .rt-fold-all");
const open = () => groups.evaluateAll((gs) => gs.map((g) => g.getAttribute("aria-expanded")));
// where the view is scrolled, and the furthest it can be: a fold
// that makes the page shorter than where it was can't stay put
const y = () => view.evaluate((v) => [v.scrollTop, v.scrollHeight - v.clientHeight, document.scrollingElement.scrollTop]);
// the reporter's case: the group with no session id folds and opens
assert.equal(await groups.count(), 3);
assert.equal(await bare.count(), 1);
assert.equal(await bare.getAttribute("aria-expanded"), "true", "the no-session group can't be folded");
await reach(page, bare);
await bare.click();
assert.equal(await bare.getAttribute("aria-expanded"), "false");
assert.equal(await rows(), 3, "its two requests are still shown");
await bare.click();
assert.equal(await rows(), 5);
// one button folds them all, then opens them all; it says which
await fold.waitFor();
assert.equal(await fold.getAttribute("aria-label"), w["Collapse all"]);
assert.equal(await fold.getAttribute("title"), w["Collapse all"]);
assert.equal((await fold.textContent()).trim(), "", "the button has words beside its icon");
const box = await fold.boundingBox(), head = await view.locator(".rt-req-head").boundingBox();
assert(box.x >= head.x - 1 && box.x + box.width <= head.x + head.width + 1, "the button is out of the head row");
await reach(page, fold);
const before = await y();
await fold.click();
assert.deepEqual(await open(), ["false", "false", "false"]);
assert.equal(await rows(), 0);
assert.equal(await fold.getAttribute("aria-label"), w["Expand all"]);
const [top, most, doc] = await y();
assert(top === before[0] || top < before[0] && top >= most - 1, `the click scrolled: ${before} to ${[top, most]}`);
assert.equal(doc, before[2], "the click scrolled the window");
await reach(page, fold);
await fold.click();
assert.deepEqual(await open(), ["true", "true", "true"]);
assert.equal(await rows(), 5);
// one open is enough for it to fold them all
await reach(page, groups.first());
await groups.first().click();
await reach(page, bare);
await bare.click();
assert.equal(await fold.getAttribute("aria-label"), w["Collapse all"]);
await reach(page, fold);
await fold.click();
assert.deepEqual(await open(), ["false", "false", "false"]);
// by request there are no groups, and no button
await reach(page, view.locator(".rt-group-by button").first());
await view.locator(".rt-group-by button").first().click();
assert.equal(await rows(), 5);
assert(await fold.isHidden(), "the button shows with no groups");
assert.deepEqual(errors, []);
});
}
}
}