import { fireEvent, render, screen, within } from "@testing-library/react"; import { afterEach, describe, expect, it, vi } from "vitest"; import { MessageRenderer } from "./message-renderer"; import { sessionId as toSessionId, taskId as toTaskId, type Message } from "@/lib/types/http"; const REMOVED_PAYLOAD = "removed secret"; const fallbackOpenFile = vi.hoisted(() => vi.fn()); const useOpenFileAtLine = vi.hoisted(() => vi.fn((onOpenFile: ((path: string) => void) | undefined) => (path: string) => onOpenFile?.(path)), ); vi.mock("@/hooks/use-panel-actions", () => ({ usePanelActions: () => ({ openFile: fallbackOpenFile }), })); vi.mock("@/hooks/use-file-editors", () => ({ useOpenFileAtLine, })); vi.mock("@/components/state-provider", () => ({ useAppStore: () => null, })); function message(overrides: Partial): Message { return { id: "msg-1", session_id: toSessionId("sess-1"), task_id: toTaskId("task-1"), author_type: "agent", type: "message", content: "hello", created_at: "2026-06-16T12:00:00Z", ...overrides, }; } describe("MessageRenderer markdown file links", () => { afterEach(() => { fallbackOpenFile.mockClear(); useOpenFileAtLine.mockClear(); }); it("forwards the renderer session through edit file links", () => { const onOpenFile = vi.fn(); render( , ); expect(useOpenFileAtLine).toHaveBeenCalledWith( onOpenFile, 7, "/workspace", "requested-session", ); }); it("opens agent plan file links with the renderer file opener", () => { const onOpenFile = vi.fn(); render( , ); fireEvent.click(screen.getByRole("link", { name: "AGENTS" })); expect(onOpenFile).toHaveBeenCalledWith("apps/web/AGENTS.md"); expect(fallbackOpenFile).not.toHaveBeenCalled(); }); it("opens thinking file links with the renderer worktree context", () => { const onOpenFile = vi.fn(); const { container } = render( , ); const thinking = within(container); fireEvent.click(thinking.getByText("Thinking", { exact: true })); fireEvent.click(thinking.getByRole("link", { name: "AGENTS" })); expect(onOpenFile).toHaveBeenCalledWith("apps/web/AGENTS.md"); expect(fallbackOpenFile).not.toHaveBeenCalled(); }); }); it.each(["tool_read", "tool_edit", "tool_search", "tool_call"])( "renders removed %s details without old payloads", (type) => { const view = render( , ); expect(view.getByText("Retained title")).toBeTruthy(); expect(view.getByText("Completed")).toBeTruthy(); expect(view.getByTestId("tool-payload-removed").textContent).toMatch(/Tool details removed on/); expect(view.queryByText(REMOVED_PAYLOAD)).toBeNull(); view.unmount(); }, );