The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
// @vitest-environment jsdom
import { act, useLayoutEffect } from "react";
import { createRoot, type Root } from "react-dom/client";
import { MemoryRouter, Route, Routes, useNavigate } from "react-router";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
const apiMocks = vi.hoisted(() => ({
getSessions: vi.fn(),
getSessionMessages: vi.fn(),
getEmptySessionsCount: vi.fn(),
getStatus: vi.fn(),
searchSessions: vi.fn(),
importSessions: vi.fn(),
exportSessionUrl: vi.fn(),
renameSession: vi.fn(),
pruneSessions: vi.fn(),
deleteSession: vi.fn(),
deleteEmptySessions: vi.fn(),
bulkDeleteSessions: vi.fn(),
getProfiles: vi.fn(),
getActiveProfile: vi.fn(),
getSessionStats: vi.fn(),
}));
vi.mock("@/lib/api", () => ({
api: apiMocks,
// ProfileProvider mirrors its selection into the api module.
setManagementProfile: vi.fn(),
getManagementProfile: vi.fn(() => ""),
}));
vi.mock("@/components/PlatformsCard", () => ({ PlatformsCard: () => null }));
// Passthrough so transcript assertions can see exactly what text the
// Markdown path would put in the DOM (React escapes it, like production).
vi.mock("@/components/Markdown", () => ({
Markdown: ({ content }: { content: string }) => <div data-testid="markdown">{content}</div>,
}));
let container: HTMLDivElement;
let root: Root;
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
async function waitFor(cond: () => boolean, timeoutMs = 5000) {
const start = Date.now();
while (!cond()) {
if (Date.now() - start > timeoutMs) throw new Error("waitFor: condition never became true");
await act(async () => {
await new Promise((resolve) => setTimeout(resolve, 20));
});
}
}
function click(el: Element | null) {
if (!el) throw new Error("element not rendered");
el.dispatchEvent(new MouseEvent("click", { bubbles: true, cancelable: true }));
}
const button = (label: string) => document.querySelector(`button[aria-label="${label}"]`);
async function renderSessionsPage(rows: Record<string, unknown>[]) {
// Page list uses limit 20; the overview tab's recent-cards fetch uses 50 —
// keep the overview empty so the list view (with row actions) renders.
apiMocks.getSessions.mockImplementation(async (limit: number) => ({
sessions: limit >= 50 ? [] : rows,
total: limit >= 50 ? 0 : rows.length,
limit,
offset: 0,
}));
const [{ default: SessionsPage }, { I18nProvider }, { SystemActionsProvider }, { ProfileProvider }, { PageHeaderProvider }] =
await Promise.all([
import("./SessionsPage"),
import("@/i18n"),
import("@/contexts/SystemActions"),
import("@/contexts/ProfileProvider"),
import("@/contexts/PageHeaderProvider"),
]);
container = document.createElement("div");
document.body.append(container);
root = createRoot(container);
await act(async () =>
root.render(
<I18nProvider>
<MemoryRouter>
<SystemActionsProvider>
<ProfileProvider>
<PageHeaderProvider pluginTabs={[]}>
<SessionsPage />
</PageHeaderProvider>
</ProfileProvider>
</SystemActionsProvider>
</MemoryRouter>
</I18nProvider>,
),
);
await waitFor(() => Boolean(button("Delete session")));
}
beforeEach(() => {
for (const fn of Object.values(apiMocks)) fn.mockReset();
apiMocks.getStatus.mockResolvedValue({});
apiMocks.getEmptySessionsCount.mockResolvedValue({ count: 0 });
apiMocks.getProfiles.mockResolvedValue({ profiles: [] });
// active === current keeps the management profile "" — the precondition
// under which an unstamped request hits the process's own store.
apiMocks.getActiveProfile.mockResolvedValue({ current: "default", active: "default" });
apiMocks.getSessionStats.mockResolvedValue({ by_source: {} });
apiMocks.getSessionMessages.mockResolvedValue({ messages: [] });
apiMocks.deleteSession.mockResolvedValue({ ok: true });
apiMocks.renameSession.mockResolvedValue({ ok: true, title: "Renamed" });
apiMocks.exportSessionUrl.mockReturnValue("/api/sessions/x/export");
vi.stubGlobal("fetch", vi.fn(async () => ({ ok: false, status: 500 })));
vi.stubGlobal("ResizeObserver", class { disconnect() {} observe() {} unobserve() {} });
// gsap ticks through rAF; a synchronous callback recurses to death.
vi.stubGlobal("requestAnimationFrame", (cb: FrameRequestCallback) => setTimeout(() => cb(0), 0) as unknown as number);
vi.stubGlobal("cancelAnimationFrame", (id: number) => clearTimeout(id));
vi.stubGlobal("matchMedia", () => ({ addEventListener() {}, matches: false, media: "", removeEventListener() {} }));
sessionStorage.clear();
});
afterEach(async () => {
await act(async () => root?.unmount());
container?.remove();
vi.unstubAllGlobals();
});
describe("SessionsPage per-row profile routing (#99387)", () => {
it("sends every per-row request to the row's owning profile, not the management default", async () => {
await renderSessionsPage([
{ id: "sid-guanli", profile: "guanli", source: "cli", model: null, title: "Managed", started_at: 1, ended_at: null,
last_active: 1, is_active: false, message_count: 2, tool_call_count: 0, input_tokens: 1, output_tokens: 1, preview: "hi" },
]);
// expand → transcript read
await act(async () => click(button("Delete session")!.closest("div.cursor-pointer")));
await waitFor(() => apiMocks.getSessionMessages.mock.calls.length > 0);
expect(apiMocks.getSessionMessages).toHaveBeenCalledWith("sid-guanli", "guanli");
await act(async () => click(button("Export session")));
expect(apiMocks.exportSessionUrl).toHaveBeenCalledWith("sid-guanli", "guanli");
await act(async () => click(button("Rename session")));
const input = document.querySelector<HTMLInputElement>('input[placeholder="Session title"]');
if (!input) throw new Error("rename input not rendered");
await act(async () => {
Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")!.set!.call(input, "Renamed");
input.dispatchEvent(new Event("input", { bubbles: true }));
});
await act(async () => click(button("Save title")));
expect(apiMocks.renameSession).toHaveBeenCalledWith("sid-guanli", "Renamed", "guanli");
await act(async () => click(button("Delete session")));
await waitFor(() => Boolean(document.querySelector('[role="alertdialog"]')));
const confirm = Array.from(document.querySelectorAll('[role="alertdialog"] button')).find(
(b) => b.textContent?.trim() === "Delete",
);
await act(async () => click(confirm ?? null));
expect(apiMocks.deleteSession).toHaveBeenCalledWith("sid-guanli", "guanli");
});
it("routes a search result through the profile stamped on that result", async () => {
apiMocks.searchSessions.mockResolvedValue({
results: [
{ id: "sid-worker", session_id: "sid-worker", profile: "worker", source: "cli", model: null,
title: "Search hit", started_at: 1, ended_at: null, last_active: 1, is_active: false,
message_count: 2, tool_call_count: 0, input_tokens: 1, output_tokens: 1, preview: "found",
snippet: "found", role: "user", session_started: 1 },
],
});
await renderSessionsPage([
{ id: "sid-default", profile: "default", source: "cli", model: null, title: "Listed", started_at: 1,
ended_at: null, last_active: 1, is_active: false, message_count: 2, tool_call_count: 0,
input_tokens: 1, output_tokens: 1, preview: "listed" },
]);
const search = document.querySelector<HTMLInputElement>('input[placeholder]');
if (!search) throw new Error("search input not rendered");
await act(async () => {
Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")!.set!.call(search, "found");
search.dispatchEvent(new Event("input", { bubbles: true }));
});
await waitFor(() => document.body.textContent?.includes("Search hit") === true);
await act(async () => click(button("Delete session")));
await waitFor(() => Boolean(document.querySelector('[role="alertdialog"]')));
const confirm = Array.from(document.querySelectorAll('[role="alertdialog"] button')).find(
(b) => b.textContent?.trim() === "Delete",
);
await act(async () => click(confirm ?? null));
expect(apiMocks.deleteSession).toHaveBeenCalledWith("sid-worker", "worker");
});
});
describe("structured reasoning rendering (#59957)", () => {
const reasoningSession = () => {
apiMocks.getSessionMessages.mockResolvedValue({
session_id: "sid-reasoning",
messages: [
{ role: "user", content: "run the tool" },
{
role: "assistant",
content:
'<thinking>I will run the tool now. <action>tool_call</action><result>{"ok":true}</result></thinking> Done.',
},
],
});
return [
{ id: "sid-reasoning", profile: "default", source: "cli", model: null, title: "Reasoning", started_at: 1,
ended_at: null, last_active: 1, is_active: false, message_count: 2, tool_call_count: 0,
input_tokens: 1, output_tokens: 1, preview: "run" },
];
};
async function expandFirstSession() {
await act(async () => click(button("Delete session")!.closest("div.cursor-pointer")));
}
it("renders assistant reasoning as prose plus labeled code blocks, not raw wrapper tags", async () => {
await renderSessionsPage(reasoningSession());
await expandFirstSession();
await waitFor(() => (document.body.textContent ?? "").includes("tool_call"));
const text = document.body.textContent ?? "";
// No raw wrapper tags cluttering the transcript — the core bug.
expect(text).not.toContain("<thinking>");
expect(text).not.toContain("</thinking>");
expect(text).not.toContain("<action>");
expect(text).not.toContain("</action>");
expect(text).not.toContain("<result>");
expect(text).not.toContain("</result>");
expect(text).toContain("I will run the tool now.");
expect(text).toContain('{"ok":true}');
expect(text).toContain("Action");
expect(text).toContain("Result");
// Action and result payloads land in code blocks.
expect(document.querySelectorAll("pre code").length).toBeGreaterThanOrEqual(2);
});
it("keeps literal known tags in user messages on the Markdown path", async () => {
apiMocks.getSessionMessages.mockResolvedValue({
session_id: "sid-reasoning",
messages: [
{
role: "user",
content: 'Please echo <action>tool_call</action> and <result>{"ok":true}</result> literally.',
},
],
});
await renderSessionsPage([
{ id: "sid-reasoning", profile: "default", source: "cli", model: null, title: "Echo", started_at: 1,
ended_at: null, last_active: 1, is_active: false, message_count: 1, tool_call_count: 0,
input_tokens: 1, output_tokens: 1, preview: "echo" },
]);
await expandFirstSession();
await waitFor(() => (document.body.textContent ?? "").includes("Please echo"));
const text = document.body.textContent ?? "";
expect(text).toContain("<action>tool_call</action>");
expect(text).toContain('<result>{"ok":true}</result>');
expect(document.querySelectorAll("pre code").length).toBe(0);
});
});
describe("SessionsPage header slots across navigation", () => {
it("leaves the next page's header action in place", async () => {
apiMocks.getSessions.mockResolvedValue({ sessions: [], total: 0, limit: 20, offset: 0 });
const [{ default: SessionsPage }, { I18nProvider }, { SystemActionsProvider }, { ProfileProvider }, { PageHeaderProvider }, { usePageHeader }] =
await Promise.all([
import("./SessionsPage"),
import("@/i18n"),
import("@/contexts/SystemActions"),
import("@/contexts/ProfileProvider"),
import("@/contexts/PageHeaderProvider"),
import("@/contexts/usePageHeader"),
]);
function NextPage() {
const { setEnd } = usePageHeader();
useLayoutEffect(() => {
setEnd(<button>Next action</button>);
return () => setEnd(null);
}, [setEnd]);
return null;
}
function GoNext() {
const navigate = useNavigate();
return <button onClick={() => navigate("/next")}>Go next</button>;
}
container = document.createElement("div");
document.body.append(container);
root = createRoot(container);
await act(async () =>
root.render(
<I18nProvider>
<MemoryRouter initialEntries={["/sessions"]}>
<SystemActionsProvider>
<ProfileProvider>
<GoNext />
<PageHeaderProvider pluginTabs={[]}>
<Routes>
<Route path="/sessions" element={<SessionsPage />} />
<Route path="/next" element={<NextPage />} />
</Routes>
</PageHeaderProvider>
</ProfileProvider>
</SystemActionsProvider>
</MemoryRouter>
</I18nProvider>,
),
);
const header = () => container.querySelector("header")?.textContent ?? "";
await waitFor(() => header().includes("Prune old sessions"));
const goNext = Array.from(container.querySelectorAll("button")).find((b) => b.textContent === "Go next");
await act(async () => click(goNext ?? null));
expect(header()).toContain("Next action");
expect(header()).not.toContain("Prune old sessions");
});
});