Files
magpie/internal/gui/tests/sessions-manage-paging.test.cjs
T
yetone 1eaf1e890a gui: Sessions is a plugin's page, its script, styles and words under assets/plugins/sessions (P2)
The first page of P2 to leave app.js's core. sessions.js and sessions.css
move to assets/plugins/sessions/ (client.js, sessions.css). The page
registers through the kernel as magpie.plugin("sessions"): mount takes its
<main>, which the kernel now makes, open and relang read it again, and
reveal is the Usage page's way in. window.loadSessionsPage and
window.openSessionOnPage are gone. app.js reaches the page only through
magpie.call, and show() takes a page's title from its part. The plugin's
manifest lists i18n.js, sessions.css and client.js.

Words: the kernel's magpie.strings(dict) adds a plugin's words to I18N.
A key that is already there is logged and not taken, since the later one
would silently win. The 65 keys only the Sessions page says (own-keys over
every asset, Go and index.html) move, as written, from i18n.js to
plugins/sessions/i18n.js. tests/strings.cjs reads i18n.js and every
plugin's i18n.js as the page has them. gui-de/ja/zh-tw use it, and
i18n-keys now checks that no key is said twice across files and that a
plugin's words are in every language. TestBuiltinClientsLoaded takes a
.css entry as a <link> in <head>, and a script between kernel.js and
app.js.

Docs: plugin-runtime.md (Words, Pages moved out: Sessions), gui-shell.md,
gateway-sessions.md.

Run:
- go test ./internal/gui/ ./internal/pluginrt/ ./internal/gateway/: ok.
- gofmt clean. go vet darwin, GOOS=linux -tags nogui and GOOS=windows on
  gui, pluginrt and plugins: clean.
- GUI, in both chromium and webkit, 40 files, all green:
  - the sessions-* and sess-* tests, session-identity, routing-sessions,
    account-mask-sessions, usage-session*, gateway-conversations;
  - plugin-kernel, the plugin-move* tests, font-cascade, win-fonts;
  - i18n-keys, gui-de, gui-ja, gui-zh-tw.
- session-terminal's red under parallel load is the same on origin/main
  591ed58b5. It is fixed at its cause in the commit before this one.
2026-10-14 08:33:24 +08:00

153 lines
9.4 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// The Sessions page with a history of thousands (ChildhoodAndy on X: switching
// to it, or to another agent, and scrolling it stuttered). An open folder draws
// its latest 100 sessions and a "Show 100 more" that adds the next page under
// them, leaving what is drawn, and where the reader is, as it was; a filter and
// another agent start again at a page. Opening a session draws that session
// again, not the list. A folder's box and the bar's still pick every session,
// drawn or not, and a session opened from the Usage page is drawn however far
// down its folder it is. English and Chinese, Chromium and WebKit, wide and
// narrow.
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 N = 250; // two pages and a half in the big folder
const at = (min) => new Date(Date.now() - min * 60e3).toISOString();
const sessions = (agent) => Array.from({ length: agent === "claude" ? N + 3 : 140 }, (_, i) => ({
agent, id: `${agent}-${i}`, cwd: i < N || agent !== "claude" ? "/work/big" : "/work/small", title: `${agent} task ${i}`,
start: at(i + 5), last: at(i + 1), input: 1000, output: 100, cost: 0.1, priced: true,
models: [{ model: "claude-sonnet-5", input: 1000, output: 100, cost: 0.1, priced: true }],
messages: 6, files: 1, size: 4096, deletable: true, resume: `${agent} --resume ${agent}-${i}`,
}));
const agents = [{ agent: "claude", name: "Claude Code", icon: "claudecode-color", count: N + 3, deletable: true }, { agent: "codex", name: "Codex", icon: "openai", count: 140, deletable: true }];
const words = {
en: { more: "Show 100 more", rest: "Show 50 more", picked: `${N} selected`, all: `${N + 3} selected` },
zh: { more: "显示其余 100 个", rest: "显示其余 50 个", picked: `已选 ${N} 个`, all: `已选 ${N + 3} 个` },
};
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:false};` });
if (url.pathname === "/wails/runtime.js") return 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/plugins") return json({ plugins: [] });
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/sessions/manage") {
const agent = url.searchParams.get("agent") === "codex" ? "codex" : "claude";
return json({ agents, agent, sessions: sessions(agent), trash: [] });
}
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)];
const body = await fs.readFile(file).catch(() => null);
return body ? route.fulfill({ body, contentType }) : route.fulfill({ status: 404, body: "" });
};
}
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of ["en", "zh"]) {
test(`${engine} ${lang}: a big folder of sessions is drawn a page at a time`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
const w = words[lang];
for (const width of [1000, 380]) {
const page = await (await browser.newContext({ viewport: { width, height: 640 }, reducedMotion: "reduce" })).newPage();
page.setDefaultTimeout(10000);
const errors = [];
page.on("pageerror", (e) => errors.push(e.message + " " + e.stack));
await page.route("**/*", serve(lang));
await page.goto("http://magpie.test/");
await page.locator('#nav [data-view="sessions"]').click();
const view = page.locator("#view-sessions");
const rows = view.locator(".row.sm-sess");
const more = view.locator(".sm-more");
await rows.first().waitFor();
assert.equal(await rows.count(), 100, "the open folder draws one page");
assert.equal(await more.textContent(), w.more);
const box = await more.boundingBox();
assert(box.x >= 0 && box.x + box.width <= width, `${width}px: Show more fits`);
// the next page goes under the rows drawn; they and the scroll stay
// the reader scrolls there: a scroll by code (scrollIntoViewIfNeeded)
// is put back at the next frame, and a click before that frame now
// puts it back too (#1554)
await page.mouse.move(width / 2, 320);
const inSight = () => rows.nth(99).evaluate((e) => { const r = e.getBoundingClientRect(); return r.top >= 0 && r.bottom <= innerHeight; });
for (let i = 0; i < 300 && !(await inSight()); i++) await page.mouse.wheel(0, 200);
await page.waitForTimeout(300);
assert(await inSight(), "the reader's wheel brings the last row drawn into sight");
await rows.first().evaluate((e) => { e.kept = true; });
const y = () => rows.nth(99).evaluate((e) => Math.round(e.getBoundingClientRect().top));
const before = await y();
// clicked where it is, as the reader does, without scrolling to it
await more.evaluate((e) => e.click());
assert.equal(await rows.count(), 200);
assert.equal(await y(), before, "Show more doesn't move the rows in sight");
assert(await rows.first().evaluate((e) => e.kept === true), "the rows drawn are kept, not drawn again");
assert.equal(await more.textContent(), w.rest);
await more.evaluate((e) => e.click());
assert.equal(await rows.count(), N);
assert.equal(await more.count(), 0, "the last page leaves no Show more");
// opening a session draws it alone
// back to the top as the reader goes there: a scroll of code's own
// is put back (WebKit)
await page.mouse.move(width / 2, 320);
await page.mouse.wheel(0, -40000);
await page.waitForFunction(() => document.querySelector("#view-sessions").scrollTop === 0);
await rows.nth(5).click();
await view.locator(".sm-item.open .sess-detail").waitFor();
assert(await rows.first().evaluate((e) => e.kept === true), "opening a session leaves the other rows");
assert.equal(await view.locator(".sm-item.open .row.sm-sess").getAttribute("data-id"), "claude-5");
await rows.nth(7).evaluate((e) => e.click()); // under the details, out of sight in a short window
assert.equal(await view.locator(".sm-item.open").count(), 1, "the one opened before closes");
assert.equal(await view.locator(".sm-item.open .row.sm-sess").getAttribute("data-id"), "claude-7");
await view.locator('.row.sm-sess[data-id="claude-7"]').evaluate((e) => e.click());
assert.equal(await view.locator(".sm-item.open").count(), 0);
// a filter starts again at a page; clearing it keeps a page
const filter = view.locator(".sm-filter");
await filter.fill("task");
assert.equal(await rows.count(), 100 + 3, "every folder opens to a page while filtering");
await filter.fill("task 249");
assert.equal(await rows.count(), 1);
await filter.fill("");
assert.equal(await rows.count(), 100);
// the folder's box and the bar pick every session, drawn or not
await view.locator(".sm-folder-check").first().check();
assert.equal(await view.locator(".sm-bar .sm-count").textContent(), w.picked);
await view.locator(".sm-bar .sm-check").check();
assert.equal(await view.locator(".sm-bar .sm-count").textContent(), w.all);
await view.locator(".sm-bar .sm-check").uncheck();
// another agent starts at a page, and so does coming back
await page.evaluate(() => magpie.call("sessions", "reveal", { agent: "codex" }));
await view.locator('.row.sm-sess[data-id^="codex"]').first().waitFor();
assert.equal(await rows.count(), 100);
// a session opened from the Usage page, far down its folder, is drawn
await page.evaluate(() => magpie.call("sessions", "reveal", { agent: "claude", id: "claude-180", cwd: "/work/big" }));
await view.locator('.sm-item.open .row.sm-sess[data-id="claude-180"]').waitFor();
assert.equal(await rows.count(), 181);
assert.equal(await more.textContent(), lang === "en" ? "Show 69 more" : "显示其余 69 个");
if (process.env.ARTIFACT_DIR) {
await fs.mkdir(process.env.ARTIFACT_DIR, { recursive: true });
await page.mouse.wheel(0, 40000);
await page.waitForTimeout(300);
await page.screenshot({ path: path.join(process.env.ARTIFACT_DIR, `${engine}-${lang}-${width}-sessions-manage-paging.png`) });
}
assert.deepEqual(errors, []);
await page.context().close();
}
});
}
}