Skip to content

The agent that grows with you

test(desktop): add behavior tests for .md preview math/table/img/link rendering

Peter034589fa2242
diff --git a/apps/desktop/src/app/chat/right-rail/preview-file.test.tsx b/apps/desktop/src/app/chat/right-rail/preview-file.test.tsx
new file mode 100644
index 00000000000..5171c3a2a34
--- /dev/null
+++ b/apps/desktop/src/app/chat/right-rail/preview-file.test.tsx
@@ -0,0 +1,53 @@
+import { cleanup, render, screen } from '@testing-library/react'
+import { afterEach, describe, expect, it } from 'vitest'
+
+import { MarkdownPreview } from './preview-file'
+
+// Behavior tests for the .md file preview renderer: input markdown goes
+// through preprocessMarkdown -> Streamdown (+ KaTeX math plugin) and must
+// come out as real rendered elements, matching what the chat transcript
+// renderer produces. Guards the regression where the preview was a bare
+// Streamdown pass with no math plugin and no table/img/a components.
+describe('MarkdownPreview', () => {
+ afterEach(() => {
+ cleanup()
+ })
+
+ it('renders block and inline math through KaTeX', () => {
+ // KaTeX marks its output; raw "$" delimiters must be gone.
+ const { container } = render(
+ <MarkdownPreview
+ text={'Formula:\n\n$$\nx = \\frac{-b \\pm \\sqrt{b^2-4ac}}{2a}\n$$\n\nInline $a^2 + b^2 = c^2$ too.'}
+ />
+ )
+
+ expect(container.querySelector('.katex')).not.toBeNull()
+ expect(screen.queryByText(/\$\$/)).toBeNull()
+ })
+
+ it('renders GFM tables with header and body cells', () => {
+ const { container } = render(<MarkdownPreview text={'| h1 | h2 |\n| --- | --- |\n| a | b |'} />)
+
+ const table = container.querySelector('table')
+ expect(table).not.toBeNull()
+ expect(table?.querySelector('thead th')?.textContent).toBe('h1')
+ expect(table?.querySelector('tbody td')?.textContent).toBe('a')
+ })
+
+ it('renders images with alt text', () => {
+ const { container } = render(<MarkdownPreview text={'![a chart](https://example.com/chart.png)'} />)
+
+ const img = container.querySelector('img')
+ expect(img?.getAttribute('alt')).toBe('a chart')
+ expect(img?.getAttribute('src')).toBe('https://example.com/chart.png')
+ })
+
+ it('renders external links to open in a new tab safely', () => {
+ const { container } = render(<MarkdownPreview text={'[docs](https://example.com/docs)'} />)
+
+ const anchor = container.querySelector('a')
+ expect(anchor?.getAttribute('href')).toBe('https://example.com/docs')
+ expect(anchor?.getAttribute('target')).toBe('_blank')
+ expect(anchor?.getAttribute('rel')).toBe('noopener noreferrer')
+ })
+})
diff --git a/apps/desktop/src/app/chat/right-rail/preview-file.tsx b/apps/desktop/src/app/chat/right-rail/preview-file.tsx
index 6fd2286519e..ffbc96d7e22 100644
--- a/apps/desktop/src/app/chat/right-rail/preview-file.tsx
+++ b/apps/desktop/src/app/chat/right-rail/preview-file.tsx
@@ -428,7 +428,7 @@ const MARKDOWN_COMPONENTS = {
a: MarkdownLink
}
-function MarkdownPreview({ text }: { text: string }) {
+export function MarkdownPreview({ text }: { text: string }) {
const mathText = useMemo(() => normalizeFilePreviewMath(text), [text])
return (