Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

text-part.test.tsx

47 lines1.5 KB

1import { renderToStaticMarkup } from 'react-dom/server'
2import { describe, expect, it } from 'vitest'
3import { TextPart } from '../../src/chat-ui/text-part'
4​
5describe('TextPart', () => {
6 it('renders markdown and escapes raw HTML', () => {
7 const html = renderToStaticMarkup(
8 <TextPart
9 role="user"
10 className="base"
11 userClassName="user"
12 content={'**bold** <script>alert(1)</script>'}
13 />,
14 )
15 expect(html).toContain('class="base user"')
16 expect(html).toContain('<strong>bold</strong>')
17 expect(html).not.toContain('<script>')
18 expect(html).toContain('&lt;script&gt;')
19 })
20​
21 it('applies a custom highlighter to fenced code', () => {
22 const html = renderToStaticMarkup(
23 <TextPart
24 content={'```ts\nconst x = 1\n```'}
25 highlighter={(code, lang) => `<span data-lang="${lang}">${code}</span>`}
26 />,
27 )
28 expect(html).toContain('<span data-lang="ts">const x = 1</span>')
29 })
30})
31​
32describe('TextPart streaming', () => {
33 it('keeps partial code verbatim at every character boundary', () => {
34 const full =
35 '```json\n{ name: "john", age: 3 }\n```\n\nInline `{ name: "john" }`.'
36 for (let i = 1; i <= full.length; i++) {
37 const src = full.slice(0, i)
38 const html = renderToStaticMarkup(<TextPart content={src} />)
39 const partial = src.match(/\{ name: "[a-z"]*/)?.[0]
40 if (partial) {
41 expect(html.replace(/<[^>]+>/g, '').replace(/&quot;/g, '"')).toContain(
42 partial,
43 )
44 }
45 }
46 })
47})