Files
magpie/internal/gui/tests/routing-session-folder.test.cjs
T
yetone 2fbbd16945 gateway, gui: routing history keeps the last two levels of the folder an agent works in, and By session, the stage and the panel name it (#1068, emo172)
Routes now carry Folder: the last two levels of the working directory the
agent states (Claude Code's "Primary working directory", Codex's <cwd>,
Pi, OpenCode), cut by shortFolder before the route is stored, so no full
path reaches the routing history, /api, or the GUI. It is kept in the
routing history for as long as the history itself (30 days / 200 MB).
A request that states no folder (title, side calls) takes the folder its
session (or parent session) last stated; the per-session map is capped
at 512 entries.

GUI: By session headings read "Agent · folder · title", their tooltip
and every request row's tooltip (page and tray panel) name the folder,
the stage's agent subtitle lists the last level of each folder when an
agent works in more than one, and the panel's agent node tooltip lists
them.

Run:
- TestShortFolder (unix, trailing slash, C:\, D:\, UNC, CJK with spaces)
  and TestRouteKeepsItsFolderShort (real Handler, fake upstream: session
  inheritance, a moved Windows path, nothing of the full path in the
  marshalled routes, saveRoute -> History round trip), TestTurnDone.
  Broke it by hand: without the session lookup the title request has no
  folder; with the folder unshortened the full path shows. Both go red.
- routing-session-folder.test.cjs: chromium + webkit x en, zh, zh-TW,
  ja, de x 1100 and 420 px, 20/20. Red on the old routing.js and with
  the stage's live routes left out.
- GUI suites routing-*, panel-routing*, purpose-filter, reasoning-speed,
  gui-de/ja/zh-tw, click-scroll, number-units: 951/951.
- gofmt -l clean; go vet darwin, linux (nogui), windows; builds for all
  three. go test -tags nogui ./...: all ok except internal/gateway, which
  hit go test's default 10m timeout at 611s on a loaded machine (#2108);
  alone with -timeout 30m it passes in 356s.
- Real use: nogui build in a sandbox HOME, fake upstream, real Claude Code
  2.1.299 (claude -p) run from .../workspace/magpie and .../src/dial: the
  day's routing jsonl has folder "workspace/magpie" and "src/dial" and no
  part of the sandbox path above them.

Not tried: title/side requests' inheritance end to end with a real agent
(claude -p made none), Codex, OpenCode and Pi with real binaries, Windows.
2026-10-14 14:03:38 +08:00

123 lines
7.8 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 it ("Claude Code ·
// workspace/magpie · title"), 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),
];
}
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: 5, totals: { requests: 5, rerouted: 0, errors: 0 }, routes });
}
if (url.pathname === "/api/gateway/history") return json({ cut: false, days: [], routes: [] });
if (url.pathname === "/api/gateway/session-titles") return json({ titles: {} });
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.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(3).waitFor();
// By session: the folder after the agent, before the title
const names = await page.locator(".rt-session .nm").evaluateAll((es) => es.map((e) => ({ text: e.textContent, title: e.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 · workspace/magpie · Fix the gateway");
assert.equal(of(/s-dial$/).text, "Claude Code · src/dial", "a folder stands in for the session id");
assert.equal(of(/s-none$/).text, "Claude Code · s-none", "no folder: as before");
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.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, []);
});
}
}
}