import { render, screen } from "@testing-library/react"; import { describe, expect, it } from "vitest"; import MarkdownTextRenderer from "@/components/MarkdownTextRenderer"; describe("MarkdownTextRenderer", () => { it("does not wrap complete fenced code blocks in an extra pre", () => { const { container } = render( {"当前目录:\n\n```text\n/Users/renxubin/.nanobot/workspace\n```"} , ); expect(screen.getByText("/Users/renxubin/.nanobot/workspace")).toBeInTheDocument(); expect(container.querySelectorAll("pre")).toHaveLength(1); expect(container.querySelector("pre div")).toBeNull(); }); it("renders bare fenced code blocks without crashing", () => { const { container } = render( {"Some text\n\n```\ncode without language\n```"} , ); expect(screen.getByText("code without language")).toBeInTheDocument(); expect(screen.getByText("text")).toBeInTheDocument(); expect(container.querySelectorAll("pre")).toHaveLength(1); }); it("keeps streaming unfinished fenced code blocks to a single shell", () => { const { container } = render( {"当前目录:\n\n```text\n/Users/renxubin/.nanobot/workspace"} , ); expect(screen.getByText("/Users/renxubin/.nanobot/workspace")).toBeInTheDocument(); expect(container.querySelectorAll("pre")).toHaveLength(1); expect(container.querySelector("pre div")).toBeNull(); }); it("renders markdown images as inline previews", () => { render(![Diagram](/api/media/sig/payload)); const image = screen.getByRole("img", { name: "Diagram" }); expect(image).toHaveAttribute("src", "/api/media/sig/payload"); expect(screen.getByRole("link", { name: "Open Diagram" })).toHaveAttribute( "href", "/api/media/sig/payload", ); }); it("renders markdown videos as inline players", () => { render(![nanobot-intro.mp4](/api/media/sig/video)); const video = screen.getByLabelText("Video attachment: nanobot-intro.mp4"); expect(video.tagName).toBe("VIDEO"); expect(video).toHaveAttribute("src", "/api/media/sig/video"); expect(video).toHaveAttribute("controls"); expect(screen.queryByRole("img", { name: "nanobot-intro.mp4" })).not.toBeInTheDocument(); }); it("renders markdown links with file-looking names as file attachments", () => { render(![index.html](/api/media/sig/html)); expect(screen.getByLabelText("File attachment")).toHaveTextContent("index.html"); expect(screen.queryByRole("img", { name: "index.html" })).not.toBeInTheDocument(); }); it("renders media attachments without an extra preview/code wrapper", () => { render(![Diagram](/api/media/sig/payload)); expect(screen.getByRole("img", { name: "Diagram" })).toHaveAttribute( "src", "/api/media/sig/payload", ); expect(screen.getByRole("link", { name: "Open Diagram" })).toHaveAttribute( "href", "/api/media/sig/payload", ); expect(screen.queryByRole("button", { name: "Code" })).not.toBeInTheDocument(); }); it("renders a safe subset of inline HTML", () => { const { container } = render( {"高亮文本\n\n上标:x2\n下标:H2O"} , ); expect(container.querySelector("mark")).toHaveTextContent("高亮文本"); expect(container.querySelector("sup")).toHaveTextContent("2"); expect(container.querySelector("sub")).toHaveTextContent("2"); }); it("keeps unsafe HTML as text", () => { const { container } = render( {"\n\nbad"} , ); expect(container.querySelector("script")).toBeNull(); expect(container.querySelector("mark")).toBeNull(); expect(container).toHaveTextContent(""); expect(container).toHaveTextContent("bad"); }); it("renders safe details blocks", () => { const { container } = render( { "
点击展开更多内容\n\n这里是被折叠的内容。\n\n- 可以放列表\n\n
" }
, ); expect(container.querySelector("details")).toBeInTheDocument(); expect(container.querySelector("summary")).toHaveTextContent("点击展开更多内容"); expect(screen.getByText("这里是被折叠的内容。")).toBeInTheDocument(); expect(screen.getByText("可以放列表")).toBeInTheDocument(); expect(container).not.toHaveTextContent(""); }); it("renders task list checkboxes as quiet status marks", () => { const { container } = render( {"- [x] 写 Markdown 示例\n- [x] 加点 emoji\n- [ ] 测试渲染效果"} , ); expect(container.querySelectorAll("input[type='checkbox']")).toHaveLength(0); expect(screen.getAllByTestId("markdown-task-checkbox")).toHaveLength(3); expect(container.querySelectorAll(".task-list-item")).toHaveLength(3); }); it("keeps dollar amounts from being parsed as inline math", () => { const { container } = render( { "VBeats mentions $24 million, while Globe states a total of $130.6 million since founding." } , ); expect(container).toHaveTextContent( "VBeats mentions $24 million, while Globe states a total of $130.6 million since founding.", ); expect(container.querySelector(".katex")).toBeNull(); }); it("still renders explicit math blocks", () => { const { container } = render( {"$$x^2 + y^2 = z^2$$"}, ); expect(container.querySelector(".katex")).toBeInTheDocument(); }); });