Files
pi2d/web/tests/chat.tree.test.tsx
T
yuez 06e70204e7 fix(web): render the chat session tree from parent/sub-session lineage
The sidebar treated every workspace session as a root and only nested the
manifest sub-agents, so sub-session files showed up as siblings (the same child
twice); sub-sessions the plugin marked hidden were listed anyway.

- build the tree from `parentId` in a pure `buildSessionTree` model: orphans are
  promoted to roots, cycles and duplicate ids are guarded, real child-session
  rows win over the manifest projection, and the routed row force-opens its
  ancestors
- honour the plugin's `:PiSessions` visibility (`visible: false`) with a "show
  hidden sub-sessions" toggle, the routed-row exception and a dedicated
  all-hidden empty state
- keep tree metadata across WS snapshots, reducer events, id migration and
  parent removal; prefer a root row for the `/chat` index and add a "back to
  parent" action
- document the session-row and `pi2:subagents` list metadata in the protocol
2026-10-22 16:45:09 +08:00

426 lines
18 KiB
TypeScript

import { fireEvent, render, screen, waitFor, within } from "@testing-library/react";
import type { ComponentProps } from "react";
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import { setLanguage } from "@/lib/i18n";
import { SessionTree } from "@/features/chat/session-tree";
import { useChatStore } from "@/features/chat/store";
import { ChatPage } from "@/pages/chat";
import { useSessionsStore } from "@/stores/sessions";
import { sessionFixture, subagentFixture } from "./chat-fixtures";
import { mockApi } from "./helpers/api-mock";
import { renderPage } from "./helpers/render-page";
beforeAll(async () => {
await setLanguage("en");
});
const defaultSessions = () => [
sessionFixture({ sessionId: "s1", name: "Alpha", subagents: [subagentFixture()] }),
sessionFixture({ sessionId: "s2", name: "Beta", subagents: [] }),
];
/** The tree element with inert handlers, so a test can rerender with new props. */
function treeElement(props: Partial<ComponentProps<typeof SessionTree>> = {}) {
return (
<SessionTree
sessions={defaultSessions()}
selectedId="s1"
filter=""
collapsed={{}}
showHidden={false}
connected
onFilter={vi.fn()}
onToggleCollapse={vi.fn()}
onToggleAll={vi.fn()}
onToggleShowHidden={vi.fn()}
onSelectSession={vi.fn()}
onNewSession={vi.fn()}
onRenameSession={vi.fn()}
onCloseSession={vi.fn()}
{...props}
/>
);
}
function renderTree(props: Partial<ComponentProps<typeof SessionTree>> = {}) {
const onSelectSession = vi.fn();
const onToggleCollapse = vi.fn();
const onToggleAll = vi.fn();
const onToggleShowHidden = vi.fn();
const sessions = props.sessions ?? defaultSessions();
const utils = render(
treeElement({ sessions, onSelectSession, onToggleCollapse, onToggleAll, onToggleShowHidden, ...props }),
);
return { ...utils, sessions, onSelectSession, onToggleCollapse, onToggleAll, onToggleShowHidden };
}
/** `SessionState` for a sub-session row (`parentId` set, `visible` defaults true). */
function childSession(overrides: Parameters<typeof sessionFixture>[0] = {}) {
return sessionFixture({ sessionId: "c1", name: "Child session", parentId: "s1", ...overrides });
}
describe("session tree", () => {
it("selects a session and shows the nested subagent", () => {
const { onSelectSession } = renderTree();
fireEvent.click(screen.getByRole("button", { name: "Beta" }));
expect(onSelectSession).toHaveBeenCalledWith("s2");
expect(screen.getByText("researcher")).toBeInTheDocument();
expect(screen.getByText("claude-sonnet-4-5 · low")).toBeInTheDocument();
});
it("folds a parent without drawing vertical guides", () => {
const { onToggleCollapse, rerender, sessions } = renderTree();
const row = screen.getByRole("button", { name: "Alpha" });
fireEvent.click(within(row).getByLabelText("Collapse"));
expect(onToggleCollapse).toHaveBeenCalledWith("s1");
rerender(treeElement({ sessions, collapsed: { s1: true } }));
expect(screen.queryByText("researcher")).not.toBeInTheDocument();
});
it("filters sessions by name and toggles all rows", () => {
const { onToggleAll } = renderTree({ filter: "Beta" });
expect(screen.queryByText("Alpha")).not.toBeInTheDocument();
expect(screen.getByText("Beta")).toBeInTheDocument();
fireEvent.click(screen.getByRole("button", { name: "Collapse or expand all" }));
expect(onToggleAll).toHaveBeenCalledTimes(1);
});
it("nests a child session row under its parent and opens its own transcript", () => {
const { onSelectSession } = renderTree({
sessions: [sessionFixture({ sessionId: "s1", name: "Alpha", subagents: [] }), childSession()],
});
const parentRow = document.querySelector('[data-session-id="s1"]');
const childRow = document.querySelector('[data-session-id="c1"]');
expect(childRow).not.toBeNull();
expect(childRow).toHaveClass("tree-row", "tree-row--sub");
// Nested under the parent row instead of a flat sibling.
expect(parentRow?.closest("li")).toContainElement(childRow as HTMLElement);
expect(parentRow?.parentElement?.querySelector("ul")).toContainElement(childRow as HTMLElement);
expect(screen.getByText("(1)")).toBeInTheDocument();
fireEvent.click(childRow as HTMLElement);
expect(onSelectSession).toHaveBeenCalledWith("c1");
});
it("hides hidden sub-sessions by default and reveals them with the toggle", () => {
const sessions = [
sessionFixture({ sessionId: "s1", name: "Alpha", subagents: [] }),
childSession({ visible: false }),
];
const { rerender, onToggleShowHidden } = renderTree({ sessions });
expect(document.querySelector('[data-session-id="c1"]')).toBeNull();
fireEvent.click(screen.getByRole("button", { name: "Show hidden sub-sessions" }));
expect(onToggleShowHidden).toHaveBeenCalledTimes(1);
rerender(treeElement({ sessions, showHidden: true }));
const childRow = document.querySelector('[data-session-id="c1"]');
expect(childRow).not.toBeNull();
expect(childRow).toHaveAttribute("data-hidden", "true");
});
it("renders a sub-session once when the manifest lists it too", () => {
renderTree({
sessions: [
sessionFixture({
sessionId: "s1",
name: "Alpha",
subagents: [subagentFixture({ id: "c1" })],
}),
childSession(),
],
});
expect(document.querySelectorAll('[data-session-id="c1"]')).toHaveLength(1);
expect(document.querySelectorAll("[data-subagent-id]")).toHaveLength(0);
});
it("keeps the routed sub-session visible under a folded parent", () => {
renderTree({
sessions: [sessionFixture({ sessionId: "s1", name: "Alpha", subagents: [] }), childSession()],
collapsed: { s1: true },
selectedId: "c1",
});
// A fold must never hide the row the route points at.
const childRow = document.querySelector('[data-session-id="c1"]');
expect(childRow).not.toBeNull();
expect(childRow).toHaveAttribute("aria-current", "true");
});
it("explains an all-hidden list instead of reporting no matches", () => {
renderTree({
sessions: [sessionFixture({ sessionId: "s1", name: "Alpha", subagents: [] }), childSession({ visible: false })],
filter: "",
});
// The only child is hidden by policy; the parent is still shown, so the
// "no matching sessions" copy would be wrong. (Parent row present.)
expect(document.querySelector('[data-session-id="s1"]')).not.toBeNull();
renderTree({ sessions: [childSession({ visible: false, parentId: null })] });
expect(screen.getByText("All sub-sessions are hidden")).toBeInTheDocument();
// Both the toolbar toggle and the empty-state action expose the label.
expect(screen.getAllByRole("button", { name: /Show hidden sub-sessions/i }).length).toBeGreaterThan(0);
});
});
/* ------------------------------------------------------------------ */
/* Deep links: an unknown session must never borrow another session. */
/* ------------------------------------------------------------------ */
const BOGUS = "ffffffffffffffffffffffff";
const TS = 1_733_234_400_000;
/** The chat page opens a socket on mount; a silent socket keeps it inert. */
function silentSocket(): void {
class SilentSocket {
readyState = 0;
onopen: ((event: unknown) => void) | null = null;
onmessage: ((event: { data: unknown }) => void) | null = null;
onclose: ((event: { code?: number }) => void) | null = null;
onerror: ((event: unknown) => void) | null = null;
send(): void {}
close(): void {
this.readyState = 3;
}
}
vi.stubGlobal("WebSocket", SilentSocket);
}
function workspaceBody() {
return [
{
workspaceId: "W1",
cwd: "/work/w1",
name: "W1",
trusted: true,
exists: true,
sessionCount: 2,
liveCount: 0,
lastActiveAt: 0,
instance: null,
},
];
}
function sessionsBody() {
return {
sessions: [
{ sessionId: "s1", name: "Alpha", workspaceId: "W1", cwd: "/work/w1", status: "idle" },
{ sessionId: "s2", name: "Beta", workspaceId: "W1", cwd: "/work/w1", status: "busy" },
],
};
}
function subSessionBody() {
// The child row arrives first and carries the sub-session metadata.
return {
sessions: [
{
sessionId: "c1",
name: "Child session",
workspaceId: "W1",
cwd: "/work/w1",
status: "busy",
parentId: "s1",
visible: false,
},
{ sessionId: "s1", name: "Alpha", workspaceId: "W1", cwd: "/work/w1", status: "idle", visible: true },
],
};
}
function entriesBody(text: string) {
return {
entries: [
{
type: "message",
id: "u1",
timestamp: new Date(TS).toISOString(),
message: { role: "user", content: text, timestamp: TS },
},
],
cursor: { hasMore: false },
};
}
function chatRoutes() {
return [
{ path: "/chat/$sessionId", component: ChatPage },
{ path: "/chat", component: ChatPage },
{ path: "/workspaces", component: () => <div>Workspaces destination</div> },
];
}
function streamPane(): HTMLElement {
const pane = document.querySelector(".stream-pane");
if (!pane) throw new Error("the chat layout did not render a stream pane");
return pane as HTMLElement;
}
function requestedUrls(fetchMock: ReturnType<typeof mockApi>): string[] {
return fetchMock.mock.calls.map(([input]) => String(input));
}
describe("chat session deep link", () => {
beforeEach(() => {
useSessionsStore.getState().reset();
useChatStore.setState({ history: {}, sending: null, preview: null });
silentSocket();
});
afterEach(() => {
vi.unstubAllGlobals();
});
it("shows an error + retry for an unknown session instead of another session", async () => {
const fetchMock = mockApi([
{ path: "/api/v1/workspaces", handler: () => ({ body: workspaceBody() }) },
{ path: "/api/v1/workspaces/W1/sessions", handler: () => ({ body: sessionsBody() }) },
{ path: "/api/v1/models", handler: () => ({ body: [] }) },
{ path: /^\/api\/v1\/sessions\/s1\/entries/, handler: () => ({ body: entriesBody("Alpha first message") }) },
]);
await renderPage(chatRoutes(), `/chat/${BOGUS}?workspace=W1`);
const error = await screen.findByTestId("session-not-found");
expect(within(error).getByText("Session not found")).toBeInTheDocument();
expect(within(error).getByRole("button", { name: /retry/i })).toBeInTheDocument();
// A way back to the workspace list, so the dead end is not a dead end.
expect(within(error).getByRole("link", { name: /back to workspaces/i })).toHaveAttribute(
"href",
"/workspaces",
);
// The main area never borrows the first session of the workspace.
expect(within(streamPane()).queryByText("Alpha")).toBeNull();
expect(within(streamPane()).queryByText("Alpha first message")).toBeNull();
expect(streamPane().querySelector(".stream-name")?.textContent ?? "").not.toContain("Alpha");
// ... and no composer for a session that does not exist.
expect(within(streamPane()).queryByLabelText("Message input")).toBeNull();
// No tree row claims to be the current session either.
expect(document.querySelector('[data-session-id][aria-current="true"]')).toBeNull();
// The unknown id is never used to fetch entries.
expect(requestedUrls(fetchMock).some((url) => url.includes(BOGUS))).toBe(false);
// Retry refetches the session list.
const listCalls = () =>
requestedUrls(fetchMock).filter((url) => url.includes("/api/v1/workspaces/W1/sessions")).length;
const before = listCalls();
fireEvent.click(within(error).getByTestId("session-not-found-retry"));
await waitFor(() => expect(listCalls()).toBeGreaterThan(before));
});
it("selects the deep-linked session and renders its own history", async () => {
const fetchMock = mockApi([
{ path: "/api/v1/workspaces", handler: () => ({ body: workspaceBody() }) },
{ path: "/api/v1/workspaces/W1/sessions", handler: () => ({ body: sessionsBody() }) },
{ path: "/api/v1/models", handler: () => ({ body: [] }) },
{ path: /^\/api\/v1\/sessions\/s2\/entries/, handler: () => ({ body: entriesBody("Beta first message") }) },
]);
await renderPage(chatRoutes(), "/chat/s2?workspace=W1");
await waitFor(() => expect(document.querySelector('[data-session-id="s2"]')).not.toBeNull());
expect(document.querySelector('[data-session-id="s2"]')).toHaveAttribute("aria-current", "true");
expect(document.querySelector('[data-session-id="s1"]')).not.toHaveAttribute("aria-current", "true");
expect(await screen.findByText("Beta first message")).toBeInTheDocument();
expect(screen.queryByTestId("session-not-found")).toBeNull();
expect(streamPane().querySelector(".stream-name")).toHaveTextContent("Beta");
expect(requestedUrls(fetchMock)).toContainEqual(
expect.stringContaining("/api/v1/sessions/s2/entries"),
);
});
it("keeps the first-session fallback for the /chat index", async () => {
mockApi([
{ path: "/api/v1/workspaces", handler: () => ({ body: workspaceBody() }) },
{ path: "/api/v1/workspaces/W1/sessions", handler: () => ({ body: sessionsBody() }) },
{ path: "/api/v1/models", handler: () => ({ body: [] }) },
{ path: /^\/api\/v1\/sessions\/s1\/entries/, handler: () => ({ body: entriesBody("Alpha first message") }) },
]);
await renderPage(chatRoutes(), "/chat?workspace=W1");
expect(await screen.findByText("Alpha first message")).toBeInTheDocument();
expect(screen.queryByTestId("session-not-found")).toBeNull();
expect(streamPane().querySelector(".stream-name")).toHaveTextContent("Alpha");
});
it("renders a deep-linked hidden sub-session and offers the way back to its parent", async () => {
mockApi([
{ path: "/api/v1/workspaces", handler: () => ({ body: workspaceBody() }) },
{ path: "/api/v1/workspaces/W1/sessions", handler: () => ({ body: subSessionBody() }) },
{ path: "/api/v1/models", handler: () => ({ body: [] }) },
{ path: /^\/api\/v1\/sessions\/c1\/entries/, handler: () => ({ body: entriesBody("Child first message") }) },
{ path: /^\/api\/v1\/sessions\/s1\/entries/, handler: () => ({ body: entriesBody("Parent first message") }) },
]);
await renderPage(chatRoutes(), "/chat/c1?workspace=W1");
await waitFor(() => {
expect(document.querySelector('[data-session-id="c1"]')).toHaveAttribute("aria-current", "true");
});
// Hidden by the plugin policy, but the routed row still renders (muted).
expect(document.querySelector('[data-session-id="c1"]')).toHaveAttribute("data-hidden", "true");
expect(await screen.findByText("Child first message")).toBeInTheDocument();
expect(streamPane().querySelector(".stream-name")).toHaveTextContent("Child session");
fireEvent.click(screen.getByRole("button", { name: "Back to parent" }));
expect(await screen.findByText("Parent first message")).toBeInTheDocument();
expect(streamPane().querySelector(".stream-name")).toHaveTextContent("Alpha");
await waitFor(() => {
expect(document.querySelector('[data-session-id="s1"]')).toHaveAttribute("aria-current", "true");
});
});
it("prefers a root session for the /chat index when a child row comes first", async () => {
mockApi([
{ path: "/api/v1/workspaces", handler: () => ({ body: workspaceBody() }) },
{ path: "/api/v1/workspaces/W1/sessions", handler: () => ({ body: subSessionBody() }) },
{ path: "/api/v1/models", handler: () => ({ body: [] }) },
{ path: /^\/api\/v1\/sessions\/s1\/entries/, handler: () => ({ body: entriesBody("Parent first message") }) },
]);
await renderPage(chatRoutes(), "/chat?workspace=W1");
expect(await screen.findByText("Parent first message")).toBeInTheDocument();
expect(screen.queryByTestId("session-not-found")).toBeNull();
expect(streamPane().querySelector(".stream-name")).toHaveTextContent("Alpha");
});
it("does not flash the error state while the first session fetch is in flight", async () => {
let release = () => {};
const gate = new Promise<void>((resolve) => {
release = resolve;
});
mockApi([
{ path: "/api/v1/workspaces", handler: () => ({ body: workspaceBody() }) },
{
path: "/api/v1/workspaces/W1/sessions",
handler: async () => {
await gate;
return { body: sessionsBody() };
},
},
{ path: "/api/v1/models", handler: () => ({ body: [] }) },
{ path: /^\/api\/v1\/sessions\/s1\/entries/, handler: () => ({ body: entriesBody("Alpha first message") }) },
]);
await renderPage(chatRoutes(), `/chat/${BOGUS}?workspace=W1`);
// Still loading: no error surface and no borrowed session.
expect(screen.queryByTestId("session-not-found")).toBeNull();
expect(streamPane().querySelector(".stream-name")?.textContent ?? "").not.toContain("Alpha");
release();
expect(await screen.findByTestId("session-not-found")).toBeInTheDocument();
});
});