Files
magpie/internal/gui/tests/sessions-talk.test.cjs
T
yetone b96d55fc99 sessions: a session's conversation can be read in magpie, and a Pi session carried on in oh-my-pi (sxwedo, #845) — the Sessions page could resume a session only in the agent that wrote it, and showed nothing of what was said. A session's details now have Show conversation: magpie reads the agent's own session file (Claude Code, Qoder, Qoder CN, Codex, Pi, oh-my-pi; found by agent and id, never a path from the page; read only, no copy kept, 400 KB at most with "There was more than is shown here") and lists the user's words, the model's, each tool call by name and its result, thinking and summaries folded. For Pi and oh-my-pi it follows the branch the session is on (parentId from the last entry), not the turns left on an abandoned one. A Pi session also has Continue in, the app's menu (no <select>): oh-my-pi's omp --fork <file> in the session's folder, copied or opened in the session terminal (the server makes the command from the session it finds; the page sends agent, id, in), and the details say it. omp --fork loads the Pi file, migrates it and writes a new oh-my-pi session, leaving Pi's untouched; with the owner's Pi sessions copied into a sandbox HOME, oh-my-pi's own SessionManager forked them whole (4→5 and 46→47 messages, its system line added; text, thinking and tool calls kept) and the source files' sha256 didn't change. Only that pair is offered: Pi can't read an oh-my-pi file (it wants the session header first, oh-my-pi writes a title line first), oh-my-pi takes Claude Code and Codex only from its own import picker, not a given session, and the other agents' formats differ, so carrying them on would mean converting them, which isn't done. zh, ja, de strings. TestTranscriptClaude, TestTranscriptCodex, TestTranscriptPiBranch, TestTranscriptOmp, TestTranscriptUnsupported, TestTranscriptCut, TestCarryPiToOmp; sessions-talk.test.cjs (WebKit, en and zh) fails on the old code. sessions-*, click-scroll, gui-ja and gui-de pass on WebKit; session-terminal and session-identity fail the same on origin/main. go vet, builds (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass.
2026-10-05 10:08:35 +08:00

200 lines
12 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// A session's conversation and carrying it on in another agent (sxwedo,
// #845). A session opened to its details has Show conversation, which reads
// sessions/transcript for it (agent and id; the server finds the file) and
// shows each part as the Usage page's calls do: the user's words, the
// model's, a tool's call by its name and its result, thinking folded. An
// agent whose sessions can't be read has no such button. A Pi session that
// oh-my-pi can carry on has Continue in, an app menu (never a <select>)
// that copies the command or opens it in the session terminal, which posts
// its agent, id and in: "omp" (the command is made by the server); the
// details say the command too. No click moves the page, nothing has a
// left-border accent. In English and Chinese, Chromium and WebKit.
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 at = (min) => new Date(Date.now() - min * 60e3).toISOString();
const FORK = "cd '/work/pi' && omp --fork '/Users/me/.pi/agent/sessions/--work-pi--/2026-10-01T08-00-00-000Z_p-1.jsonl'";
const sess = (agent, id, title, min, extra) => ({
agent, id, cwd: "/work/pi", title, start: at(min + 5), last: at(min), models: [],
resume: `cd '/work/pi' && ${agent} --session ${id}`, path: `~/.pi/agent/sessions/--work-pi--/${id}.jsonl`, size: 4096, messages: 6, files: 1, deletable: true, ...extra,
});
const PARTS = [
{ role: "user", kind: "text", text: "Rename foo to bar" },
{ role: "assistant", kind: "thinking", text: "grep for it first" },
{ role: "assistant", kind: "tool_use", name: "bash", text: '{\n "command": "grep -rn foo"\n}' },
{ role: "tool", kind: "tool_result", name: "bash", text: "a.go:3: foo()" },
{ role: "assistant", kind: "text", text: "Renamed foo to bar in a.go." },
];
function serve(lang, calls) {
const store = {
pi: [
sess("pi", "p-1", "Rename foo", 5, { transcript: true, carry: [{ agent: "omp", command: FORK }] }),
sess("pi", "p-2", "an older one", 50, { transcript: true, carry: [{ agent: "omp", command: FORK.replace("p-1", "p-2") }] }),
],
opencode: [{ ...sess("opencode", "o-1", "an opencode chat", 8), deletable: false, resume: "opencode -s o-1" }],
};
const state = { agents: [], profiles: [], settings: { lang, 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:"${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(state);
if (url.pathname === "/api/plugins") return json({ plugins: [] });
if (url.pathname === "/api/groups") return json({ groups: [] });
if (url.pathname === "/api/providers") return json({ providers: [], presets: [], excluded: [], gateway: { running: true, window: true } });
if (url.pathname === "/api/sessions/manage") {
const agent = url.searchParams.get("agent") === "opencode" ? "opencode" : "pi";
return json({
agents: [
{ agent: "pi", count: store.pi.length, deletable: true, name: "Pi", icon: "pi" },
{ agent: "opencode", count: store.opencode.length, deletable: false, name: "OpenCode", icon: "opencode" },
],
agent, sessions: store[agent], terminal: true, trash: [], trashDir: "~/trash",
});
}
if (url.pathname === "/api/sessions/transcript") {
calls.push({ path: "transcript", agent: url.searchParams.get("agent"), id: url.searchParams.get("id") });
return json({ parts: PARTS });
}
if (url.pathname === "/api/sessions/terminal") {
calls.push({ path: "terminal", body: route.request().postDataJSON() });
return route.fulfill({ status: 204 });
}
if (url.pathname === "/api/copy") {
calls.push({ path: "copy", body: route.request().postDataJSON() });
return json({});
}
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 words = {
en: { nav: "Sessions", carry: "Continue in", head: "Continue in another agent", copyNote: "Copy the command", termNote: "Open in session terminal",
line: "Continue in omp", show: "Show conversation", hide: "Hide conversation", talk: "Conversation", you: "You", asst: "Assistant", call: "Tool call", result: "Tool result", thinking: "Thinking" },
zh: { nav: "会话", carry: "换 Agent 继续", head: "换个 Agent 继续", copyNote: "复制命令", termNote: "在所选终端中打开",
line: "用 omp 继续", show: "查看会话", hide: "收起会话", talk: "会话内容" },
};
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of ["en", "zh"]) {
const w = words[lang];
test(`${engine} ${lang}: a session's conversation is shown, and a Pi session is carried on in omp`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
const page = await (await browser.newContext({ viewport: { width: 900, height: 600 }, reducedMotion: "reduce" })).newPage();
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}-${lang}-sessions-talk.png`) });
}
await browser.close();
});
page.setDefaultTimeout(5000);
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
page.on("dialog", (d) => { errors.push("a browser dialog: " + d.message()); d.dismiss(); });
const calls = [];
await page.route("**/*", serve(lang, calls));
await page.goto("http://magpie.test/?view=providers");
await page.locator("#nav").getByRole("button", { name: w.nav, exact: true }).click();
const view = page.locator("#view-sessions");
const row = (id) => view.locator(`.row.sm-sess[data-id="${id}"]`);
await row("p-1").waitFor();
const top = (l) => l.evaluate((e) => e.getBoundingClientRect().top);
const still = async (l, what, click) => {
const before = await top(l);
await click();
await page.waitForTimeout(250);
assert.equal(await top(l), before, `${what} moved the page`);
};
// Continue in: the app's menu, with omp's copy and terminal
const carry = row("p-1").locator(".sess-carry");
assert.equal(await carry.count(), 1, "a Pi session can be carried on in omp");
assert.equal((await carry.textContent()).trim(), w.carry);
await still(row("p-1"), "opening Continue in", () => carry.click());
const menu = page.locator(".proto-menu.sess-carry-menu");
await menu.waitFor();
assert.equal((await menu.locator(".pm-head").textContent()).trim(), w.head);
assert.deepEqual(await menu.locator(".pm-item .pm-name").allTextContents(), ["omp", "omp"]);
assert.deepEqual(await menu.locator(".pm-item .pm-note").allTextContents(), [w.copyNote, w.termNote]);
assert.equal(await page.locator("select").count(), 0, "no native select");
assert.equal(await carry.getAttribute("aria-expanded"), "true");
await still(row("p-1"), "opening it in the terminal", () => menu.locator(".pm-item").nth(1).click());
await menu.waitFor({ state: "detached" });
for (let i = 0; i < 40 && !calls.some((c) => c.path === "terminal"); i++) await page.waitForTimeout(50);
assert.deepEqual(calls.find((c) => c.path === "terminal"), { path: "terminal", body: { agent: "pi", id: "p-1", in: "omp" } });
assert.equal(calls.filter((c) => c.path === "transcript").length, 0, "the row didn't open");
await carry.click();
await menu.waitFor();
await menu.locator(".pm-item").nth(0).click();
for (let i = 0; i < 40 && !calls.some((c) => c.path === "copy"); i++) await page.waitForTimeout(50);
assert.deepEqual(calls.find((c) => c.path === "copy"), { path: "copy", body: { text: FORK } });
// the details say the command; Show conversation reads the session
await still(row("p-1"), "opening the row", () => row("p-1").locator(".who").click());
const detail = view.locator(".sess-detail");
await detail.waitFor();
const keys = await detail.locator(".sess-line .k").allInnerTexts();
assert(keys.includes(w.line), `the details have ${w.line}: ${keys}`);
assert((await detail.textContent()).includes(FORK));
assert(keys.includes(w.talk));
const show = detail.locator(".sess-talk-btn");
assert.equal((await show.textContent()).trim(), w.show);
assert.equal(await detail.locator(".sess-talk").isVisible(), false, "the conversation waits to be asked for");
await still(row("p-1"), "showing the conversation", () => show.click());
const talk = detail.locator(".sess-talk");
await talk.locator(".cx-part").first().waitFor();
assert.deepEqual(calls.filter((c) => c.path === "transcript"), [{ path: "transcript", agent: "pi", id: "p-1" }]);
assert.equal(await talk.locator(".cx-part").count(), PARTS.length);
if (lang === "en") {
assert.deepEqual(await talk.locator(".cx-part .cx-role").allTextContents(), [w.you, w.thinking, w.call, w.result, w.asst]);
}
assert.deepEqual(await talk.locator(".cx-name").allTextContents(), ["bash", "bash"]);
assert.equal(await talk.locator("details.cx-part").getAttribute("open"), null, "thinking is folded");
assert((await talk.locator(".cx-t").first().textContent()).includes("Rename foo to bar"));
assert.equal((await show.textContent()).trim(), w.hide);
// hidden, and shown again from what was read
await show.click();
assert.equal(await talk.isVisible(), false);
await show.click();
await talk.locator(".cx-part").first().waitFor();
assert.equal(calls.filter((c) => c.path === "transcript").length, 1, "read once");
const border = await page.evaluate(() => [...document.querySelectorAll("#view-sessions, #view-sessions *")]
.filter((e) => parseFloat(getComputedStyle(e).borderLeftWidth) > 1 && getComputedStyle(e).borderLeftColor !== getComputedStyle(e).borderRightColor).map((e) => e.className));
assert.deepEqual(border, [], "no left-border accent");
if (process.env.SCREENSHOT_DIR) {
await fs.mkdir(process.env.SCREENSHOT_DIR, { recursive: true });
await page.screenshot({ path: path.join(process.env.SCREENSHOT_DIR, `${engine}-${lang}-sessions-talk.png`), fullPage: true });
}
// an agent whose sessions can't be read or carried has neither
await view.locator(".sm-agents .opt", { hasText: "OpenCode" }).click();
const o = view.locator('.row.sm-sess[data-id="o-1"]');
await o.waitFor();
assert.equal(await o.locator(".sess-carry").count(), 0);
await o.locator(".who").click();
await view.locator(".sess-detail").waitFor();
assert.equal(await view.locator(".sess-talk-btn").count(), 0);
if (lang === "zh") {
const missing = await page.evaluate(() => ["Continue in {agent}", "Continue in", "Continue this session in another agent", "Continue in another agent",
"Copy the command", "Conversation", "Show conversation", "Hide conversation", "Reading…"].filter((k) => !I18N.zh[k] || !I18N.ja[k] || !I18N.de[k]));
assert.deepEqual(missing, [], "every string has its Chinese, Japanese and German");
}
assert.deepEqual(errors, []);
});
}
}