File size: 1,844 Bytes
21ad36a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { renderToStaticMarkup } from 'react-dom/server';
import { describe, expect, it } from 'vitest';
import { codeThemeName, getCodeHighlighter, normalizeCodeLanguage } from '../../lib/code-highlighter';
import { MarkdownContent } from './MarkdownContent';

describe('MarkdownContent code blocks', () => {
  it('renders an incomplete streamed fence immediately with a stable language label', () => {
    const html = renderToStaticMarkup(
      <MarkdownContent
        content={'```ts\nconst answer: number = 42'}
        theme="dark"
        streaming
      />,
    );

    expect(html).toContain('data-language="typescript"');
    expect(html).toContain('data-code-complete="false"');
    expect(html).toContain('streaming');
    expect(html).toContain('const answer: number = 42');
  });

  it('marks a closed code fence complete and omits the streaming marker', () => {
    const html = renderToStaticMarkup(
      <MarkdownContent
        content={'```python\nprint("bonsai")\n```'}
        theme="light"
        streaming
      />,
    );

    expect(html).toContain('data-language="python"');
    expect(html).toContain('data-code-complete="true"');
    expect(html).not.toContain('code-stream-state');
  });

  it('loads curated grammars and falls back safely for unknown labels', async () => {
    expect(normalizeCodeLanguage('tsx')).toBe('tsx');
    expect(normalizeCodeLanguage('c++')).toBe('cpp');
    expect(normalizeCodeLanguage('unknown-future-lang')).toBe('text');
    expect(codeThemeName('dark')).toBe('github-dark');

    const highlighter = await getCodeHighlighter('typescript');
    expect(highlighter.getLoadedLanguages()).toContain('typescript');
    expect(highlighter.codeToTokens('const value = 1', {
      lang: 'typescript',
      theme: 'github-light',
    }).tokens[0]?.length).toBeGreaterThan(1);
  });
});