Files
magpie/internal/gui/tests/routing-session-folder.test.cjs
T
yetone 43849d51c3 routing: By session heads a session by its title, its folder quieter beside it (flandern1211_58540 on Discord)
Routing's By session headed each session "Claude Code · folder · title",
and with no title only the folder and id ("路由这里的会话名只能看到目录名和
会话ID吗"). Now the head is the agent and the session's title on one
line (whitespace folded), with its folder after it in a quieter .dir; with
no title the folder stands in, and with neither the id. The tooltip keeps
the title, the folder and the id.

The title comes from what magpie already reads, no second reader:
sessions.Titles (the agent's own title, else its first prompt) and Codex's
name index, and where both find nothing, the first prompt the gateway
recorded for that session (sessions.GatewayTitler, the title the Sessions
page shows for it), looked up under the agent the page names and the one
the gateway records it under (usage.AgentOf). A Codex thread Codex never
named now goes to sessions.Titles as a codex: key, so its rollout's first
prompt names it. withGatewayTitles and codexReadableTitles in trace.go.
Nothing is read that the Sessions page doesn't show already, and nothing
leaves the local GUI.

Tests: TestSessionTitlesFallBackToFirstPrompt (new): an unnamed Codex
thread gets its first prompt, a named one keeps its name, an unknown id
stays out, a pi session known only to the gateway gets its first words.
routing-session-folder.test.cjs follows the new head: title then .dir,
the first prompt on one line, the folder said once, the id with neither,
nothing clipped (chromium and webkit, en zh zh-TW ja de, 1100 and 420px).

Run on mote via mote-run, at origin/main e7b0c398e plus this change:
- Whole Routing GUI set (routing-*, panel-routing*) + i18n-keys,
  chromium and webkit: 2583 pass, 35 fail. Every red is a known one:
  rule-remove (#3096), rule-drag (#3789), row-swap (#3799),
  order-unknown (#4627), rules-line (#3593), many-keys, steady,
  steps-cap and story-shrinks (#5088), and under the load of another
  session's whole GUI suite (load ~200) routing-credits webkit ja 420
  (#5161) and routing-flood webkit en (#2215). Run again: credits 20/20
  and flood 12/12 here; order-unknown's webkit de 420 red the same at
  e7b0c398e alone.
- routing-session-folder: 20/20 (both engines, 5 languages, 2 widths).
- go test ./internal/gui: TestSessionTitlesFallBackToFirstPrompt ok;
  red only TestCoreScriptBudget (1534001 > 1533802, the same at
  e7b0c398e, #5139: this change adds nothing to the core scripts) and
  TestMainWindowCorners (the same at e7b0c398e, #3949).
- Rebased onto origin/main 1a67ca614 (its routing client.js changes
  merged clean), at this commit: routing-session-folder 20/20,
  routing-session-title 40/40, routing-kept-again 20/20,
  usage-compact-counts 30/30, routing-live-calm 2/2, i18n-keys 4/4, and
  go test ./internal/gui -run 'TestSession|TestCoreScriptBudget' ok
  (the budget passes at this base). The whole Routing set was not run
  again here. Then onto 0f1b40bbc (routing client.js and css again,
  merged clean): routing-session-folder 20/20, routing-live-bytes
  20/20, routing-live-calm 2/2, i18n-keys 4/4.

Broken by hand:
- withGatewayTitles a no-op: TestSessionTitlesFallBackToFirstPrompt red
  ("titles = map[]").
- client.js back to the old head: routing-session-folder red in every
  engine, language and width.

Not tried: the real app with real Codex, Claude Code and gateway-only
sessions (fixtures only), the Linux WebKitGTK app, Windows.
2026-11-04 19:43:00 +08:00

141 lines
9.2 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// Each route keeps the folder its agent works in, its last two levels only
// (#1068, emo172: two Claude Code runs in two projects read as one). The
// Routing page's By session heads a session with its title ("Claude Code ·
// title") and the folder dim beside it, the session id under the pointer; a request's
// row says it under the pointer; the stage's agent, working in two folders
// at once, names both by their last level; the tray panel's agent says them
// under the pointer. A session that says no folder reads as before. In every
// language, at 1100 and 420. No backend: the API is faked here.
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");
function fixture() {
const now = Date.now(), at = (s) => new Date(now - s * 1000).toISOString();
const seat = { id: "relay", provider: "relay", name: "Relay", who: "team", kind: "key", model: "m1" };
const req = (id, session, folder, ago, extra = {}) => ({ id, seq: id, time: at(ago), agent: "claude", session, ...(folder ? { folder } : {}),
model: "relay/m1", provider: "relay", order: [seat], tries: [{ id: seat.id, model: "m1", start: at(ago), done: true, status: 200, ms: 500 }],
done: true, status: 200, ms: 500, tokens: 1000, ...extra });
return [
req(1, "s-magpie", "workspace/magpie", 5),
req(2, "s-magpie", "workspace/magpie", 4, { sessionTitle: "Fix the gateway" }),
req(3, "s-dial", "src/dial", 3),
req(4, "s-none", "", 2),
req(5, "", "", 1),
req(6, "s-old", "", 0.5),
];
}
function serve(lang, routes) {
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(() => {});
return json({ mine: true, now: new Date().toISOString(), seq: 6, totals: { requests: 6, rerouted: 0, errors: 0 }, routes });
}
if (url.pathname === "/api/gateway/history") return json({ cut: false, days: [], routes: [] });
// a session named by its first prompt, as sessions.Titles gives it
if (url.pathname === "/api/gateway/session-titles") return json({ titles: { "claude:s-old": "Rename foo\n to bar" } });
if (url.pathname === "/api/groups") return json({ groups: [] });
if (url.pathname === "/api/providers") return json({ providers: [], gateway: { running: true, window: true } });
if (url.pathname === "/api/usage/quotas") return json([]);
if (url.pathname === "/api/window/fit") return route.fulfill({ status: 204 });
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)];
return route.fulfill({ body: await fs.readFile(file), contentType });
};
}
const words = {
en: { folder: "Folder", session: "Session id" },
zh: { folder: "文件夹", session: "会话 ID" },
"zh-TW": { folder: "資料夾", session: "會話 ID" },
ja: { folder: "フォルダ", session: "セッション ID" },
de: { folder: "Ordner", session: "Sitzungs-ID" },
};
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of Object.keys(words)) {
for (const width of [1100, 420]) {
test(`${engine} ${lang} ${width}: routing names each session's folder, never more of its path`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
const w = words[lang], errors = [];
const page = await browser.newPage({ viewport: { width, height: 900 }, reducedMotion: "reduce" });
page.setDefaultTimeout(5000);
page.setDefaultNavigationTimeout(60000);
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", serve(lang, fixture()));
try { await page.addInitScript(() => localStorage.setItem("magpie.routingBySession", "1")); } catch {}
await page.goto("http://magpie.test/?view=routing");
await page.locator(".rt-session").nth(4).waitFor();
await page.waitForFunction(() => [...document.querySelectorAll(".rt-session .nm")].some((e) => e.textContent.includes("Rename")));
// By session: the agent and the session's title, its folder dim
// beside it (flandern1211_58540 on Discord: "路由这里的会话名只能
// 看到目录名和会话ID吗"); with no title, the folder stands in, and
// with neither, the id
const names = await page.locator(".rt-session").evaluateAll((es) => es.map((e) => {
const nm = e.querySelector(".nm"), dir = e.querySelector(".dir");
return { text: nm.textContent, title: nm.title, dir: dir && !dir.hidden ? dir.textContent : "", dirTitle: dir?.title || "" };
}));
const of = (re) => names.find((n) => re.test(n.title)) || assert.fail(`no group for ${re}: ${JSON.stringify(names)}`);
assert.equal(of(/s-magpie$/).text, "Claude Code · Fix the gateway");
assert.equal(of(/s-magpie$/).dir, "workspace/magpie", "the folder beside the title");
assert.equal(of(/s-old$/).text, "Claude Code · Rename foo to bar", "the first prompt, on one line");
assert.equal(of(/s-old$/).dir, "");
assert.equal(of(/s-dial$/).text, "Claude Code · src/dial", "a folder stands in for the session id");
assert.equal(of(/s-dial$/).dir, "", "the folder said once");
assert.equal(of(/s-none$/).text, "Claude Code · s-none", "no folder: as before");
assert.match(of(/s-old$/).title, new RegExp(`^Rename foo to bar\\n${w.session}: s-old$`));
const clipped = await page.locator(".rt-session").evaluateAll((es) => es.some((e) => [...e.children].some((c) => c.getBoundingClientRect().right > e.getBoundingClientRect().right + 1)));
assert.equal(clipped, false, "each heading's parts inside it");
assert.match(of(/s-magpie$/).title, new RegExp(`${w.folder}: workspace/magpie\\n${w.session}: s-magpie$`));
assert.doesNotMatch(of(/s-none$/).title, new RegExp(w.folder));
// a request's row says its folder under the pointer
const rowTitles = await page.locator(".rt-req").evaluateAll((es) => es.map((e) => e.title));
assert(rowTitles.some((x) => x.endsWith(`\n${w.folder}: src/dial`)), JSON.stringify(rowTitles));
assert.equal(rowTitles.filter((x) => x.includes(w.folder)).length, 3, "only the rows that have a folder");
// the stage: one agent in two folders at once names both
const node = page.locator(".rt-src").first();
await page.waitForFunction(() => document.querySelector(".rt-src small")?.textContent.includes(","));
assert.equal(await node.locator("small").textContent(), "magpie, dial");
assert.match(await node.getAttribute("title"), new RegExp(`${w.folder}: workspace/magpie\\n${w.folder}: src/dial$`));
const fit = await page.evaluate(() => ({ client: document.documentElement.clientWidth, scroll: document.documentElement.scrollWidth }));
assert(fit.scroll <= fit.client + 1, `the page overflows at ${width}: ${JSON.stringify(fit)}`);
const reqs = await page.locator(".rt-reqs").evaluate((e) => ({ client: e.clientWidth, scroll: e.scrollWidth }));
assert(reqs.scroll <= reqs.client + 1, `request list overflows at ${width}: ${JSON.stringify(reqs)}`);
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}-${width}-session-folder.png`), fullPage: true });
}
// the tray panel's agent: its folders under the pointer
const panel = await browser.newPage({ viewport: { width: Math.min(width, 440), height: 600 }, reducedMotion: "reduce" });
panel.setDefaultTimeout(5000);
panel.setDefaultNavigationTimeout(60000);
panel.on("pageerror", (e) => errors.push(e.message));
await panel.route("**/*", serve(lang, fixture()));
await panel.goto("http://magpie.test/?mode=panel");
await panel.locator('[data-ptab="routing"]').click();
await panel.locator(".ps-ag").first().waitFor();
assert.match(await panel.locator(".ps-ag").first().getAttribute("title"), new RegExp(`^Claude Code\\n${w.folder}: workspace/magpie\\n${w.folder}: src/dial$`));
assert.deepEqual(errors, []);
});
}
}
}