|
1 | 1 | import { EnvironmentId } from "@t3tools/contracts"; |
2 | 2 | import { act, type ComponentProps, type ReactNode } from "react"; |
3 | | -import { renderToStaticMarkup } from "react-dom/server"; |
| 3 | +import { renderToReadableStream, renderToStaticMarkup } from "react-dom/server"; |
4 | 4 | import { create, type ReactTestRenderer } from "react-test-renderer"; |
5 | 5 | import { describe, expect, it, vi } from "vite-plus/test"; |
6 | 6 |
|
@@ -72,6 +72,80 @@ function codeButton(renderer: ReactTestRenderer, label: string) { |
72 | 72 | return button.props as ComponentProps<typeof Button>; |
73 | 73 | } |
74 | 74 |
|
| 75 | +describe("ChatMarkdown math", () => { |
| 76 | + async function renderMath(text: string, parseRawHtml = true, isStreaming = false) { |
| 77 | + const stream = await renderToReadableStream( |
| 78 | + <ChatMarkdown |
| 79 | + cwd={undefined} |
| 80 | + text={text} |
| 81 | + parseRawHtml={parseRawHtml} |
| 82 | + lineBreaks={!parseRawHtml} |
| 83 | + isStreaming={isStreaming} |
| 84 | + />, |
| 85 | + ); |
| 86 | + await stream.allReady; |
| 87 | + return new Response(stream).text(); |
| 88 | + } |
| 89 | + |
| 90 | + it.each([true, false])( |
| 91 | + "typesets accessible inline and display math with raw HTML=%s", |
| 92 | + async (parseRawHtml) => { |
| 93 | + const html = await renderMath( |
| 94 | + String.raw`Fraction $\frac{1}{2}$ and root \(\sqrt{x}\).` + |
| 95 | + "\n\n" + |
| 96 | + String.raw`\[\begin{pmatrix}1 & 2 \\ 3 & 4\end{pmatrix}\]`, |
| 97 | + parseRawHtml, |
| 98 | + ); |
| 99 | + expect(html).toContain("<mfrac>"); |
| 100 | + expect(html).toContain("<msqrt>"); |
| 101 | + expect(html).toContain("<mtable"); |
| 102 | + expect(html).toContain('<annotation encoding="application/x-tex">\\frac{1}{2}</annotation>'); |
| 103 | + expect(html).not.toContain("chat-markdown-codeblock"); |
| 104 | + }, |
| 105 | + ); |
| 106 | + |
| 107 | + it("keeps invalid math readable and renders the completed streamed formula", async () => { |
| 108 | + const partial = await renderMath("Before\n\n$$\n\\frac{1}{\n", true, true); |
| 109 | + expect(partial).toContain("Before"); |
| 110 | + expect(partial).toContain("\\frac{1}{"); |
| 111 | + const complete = await renderMath("Before\n\n$$\n\\frac{1}{2}\n$$\n\nAfter", true, true); |
| 112 | + expect(complete).toContain("<mfrac>"); |
| 113 | + expect(complete).toContain("After"); |
| 114 | + }); |
| 115 | + |
| 116 | + it("does not load external resources or execute HTML from a formula", async () => { |
| 117 | + const html = await renderMath( |
| 118 | + String.raw`$\includegraphics{https://example.com/tracker.png}$` + |
| 119 | + "\n\n" + |
| 120 | + String.raw`$\text{<img src=x onerror=alert(1)>}$`, |
| 121 | + ); |
| 122 | + expect(html).not.toContain("<img"); |
| 123 | + expect(html).toContain("<img"); |
| 124 | + expect(html).toContain("\\includegraphics"); |
| 125 | + }); |
| 126 | + |
| 127 | + it("does not typeset a completed equation again when more text streams in", async () => { |
| 128 | + const katex = (await import("katex")).default; |
| 129 | + const render = vi.spyOn(katex, "renderToString"); |
| 130 | + vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); |
| 131 | + let renderer: ReactTestRenderer | undefined; |
| 132 | + try { |
| 133 | + await act(async () => { |
| 134 | + renderer = create(<ChatMarkdown cwd={undefined} text="$x^2$" isStreaming />); |
| 135 | + }); |
| 136 | + expect(render).toHaveBeenCalledTimes(1); |
| 137 | + await act(async () => { |
| 138 | + renderer!.update(<ChatMarkdown cwd={undefined} text="$x^2$ and more text" isStreaming />); |
| 139 | + }); |
| 140 | + expect(render).toHaveBeenCalledTimes(1); |
| 141 | + } finally { |
| 142 | + await act(async () => renderer?.unmount()); |
| 143 | + render.mockRestore(); |
| 144 | + vi.unstubAllGlobals(); |
| 145 | + } |
| 146 | + }); |
| 147 | +}); |
| 148 | + |
75 | 149 | describe("ChatMarkdown context references", () => { |
76 | 150 | it("renders text and image references through the chip renderer, with readable fallback", async () => { |
77 | 151 | vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); |
|
0 commit comments