|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { |
| 3 | + escapeHtml, |
| 4 | + parseJsDocLinks, |
| 5 | + renderMarkdown, |
| 6 | + type SymbolLookup, |
| 7 | +} from '../../server/utils/docs' |
| 8 | + |
| 9 | +describe('escapeHtml', () => { |
| 10 | + it('should escape < and >', () => { |
| 11 | + expect(escapeHtml('<script>')).toBe('<script>') |
| 12 | + }) |
| 13 | + |
| 14 | + it('should escape &', () => { |
| 15 | + expect(escapeHtml('foo & bar')).toBe('foo & bar') |
| 16 | + }) |
| 17 | + |
| 18 | + it('should escape quotes', () => { |
| 19 | + expect(escapeHtml('"hello"')).toBe('"hello"') |
| 20 | + expect(escapeHtml("'hello'")).toBe(''hello'') |
| 21 | + }) |
| 22 | + |
| 23 | + it('should handle multiple special characters', () => { |
| 24 | + expect(escapeHtml('<a href="test?a=1&b=2">')) |
| 25 | + .toBe('<a href="test?a=1&b=2">') |
| 26 | + }) |
| 27 | + |
| 28 | + it('should return empty string for empty input', () => { |
| 29 | + expect(escapeHtml('')).toBe('') |
| 30 | + }) |
| 31 | + |
| 32 | + it('should not modify text without special characters', () => { |
| 33 | + expect(escapeHtml('hello world')).toBe('hello world') |
| 34 | + }) |
| 35 | +}) |
| 36 | + |
| 37 | +describe('parseJsDocLinks', () => { |
| 38 | + const emptyLookup: SymbolLookup = new Map() |
| 39 | + |
| 40 | + it('should convert external URLs to links', () => { |
| 41 | + const result = parseJsDocLinks('{@link https://example.com}', emptyLookup) |
| 42 | + expect(result).toContain('href="https://example.com"') |
| 43 | + expect(result).toContain('target="_blank"') |
| 44 | + expect(result).toContain('rel="noopener"') |
| 45 | + }) |
| 46 | + |
| 47 | + it('should handle external URLs with labels', () => { |
| 48 | + const result = parseJsDocLinks('{@link https://example.com Example Site}', emptyLookup) |
| 49 | + expect(result).toContain('href="https://example.com"') |
| 50 | + expect(result).toContain('>Example Site</a>') |
| 51 | + }) |
| 52 | + |
| 53 | + it('should convert internal symbol references to anchor links', () => { |
| 54 | + const lookup: SymbolLookup = new Map([['MyFunction', 'function-MyFunction']]) |
| 55 | + const result = parseJsDocLinks('{@link MyFunction}', lookup) |
| 56 | + expect(result).toContain('href="#function-MyFunction"') |
| 57 | + expect(result).toContain('docs-symbol-link') |
| 58 | + }) |
| 59 | + |
| 60 | + it('should render unknown symbols as code', () => { |
| 61 | + const result = parseJsDocLinks('{@link UnknownSymbol}', emptyLookup) |
| 62 | + expect(result).toContain('<code class="docs-symbol-ref">UnknownSymbol</code>') |
| 63 | + }) |
| 64 | + |
| 65 | + it('should escape HTML in surrounding text', () => { |
| 66 | + const result = parseJsDocLinks('Use <T> with {@link https://example.com}', emptyLookup) |
| 67 | + expect(result).toContain('<T>') |
| 68 | + }) |
| 69 | + |
| 70 | + it('should handle multiple links', () => { |
| 71 | + const result = parseJsDocLinks( |
| 72 | + 'See {@link https://a.com} and {@link https://b.com}', |
| 73 | + emptyLookup, |
| 74 | + ) |
| 75 | + expect(result).toContain('href="https://a.com"') |
| 76 | + expect(result).toContain('href="https://b.com"') |
| 77 | + }) |
| 78 | + |
| 79 | + it('should not convert non-http URLs to links', () => { |
| 80 | + const result = parseJsDocLinks('{@link javascript:alert(1)}', emptyLookup) |
| 81 | + // Should be treated as unknown symbol, not a link |
| 82 | + expect(result).not.toContain('href="javascript:') |
| 83 | + expect(result).toContain('<code') |
| 84 | + }) |
| 85 | + |
| 86 | + it('should handle http URLs (not just https)', () => { |
| 87 | + const result = parseJsDocLinks('{@link http://example.com}', emptyLookup) |
| 88 | + expect(result).toContain('href="http://example.com"') |
| 89 | + }) |
| 90 | +}) |
| 91 | + |
| 92 | +describe('renderMarkdown', () => { |
| 93 | + const emptyLookup: SymbolLookup = new Map() |
| 94 | + |
| 95 | + it('should convert inline code', () => { |
| 96 | + const result = renderMarkdown('Use `foo()` here', emptyLookup) |
| 97 | + expect(result).toContain('<code class="docs-inline-code">foo()</code>') |
| 98 | + }) |
| 99 | + |
| 100 | + it('should escape HTML inside inline code', () => { |
| 101 | + const result = renderMarkdown('Use `Array<T>` here', emptyLookup) |
| 102 | + expect(result).toContain('<T>') |
| 103 | + expect(result).not.toContain('<T>') |
| 104 | + }) |
| 105 | + |
| 106 | + it('should convert bold text', () => { |
| 107 | + const result = renderMarkdown('This is **important**', emptyLookup) |
| 108 | + expect(result).toContain('<strong>important</strong>') |
| 109 | + }) |
| 110 | + |
| 111 | + it('should convert single newlines to <br>', () => { |
| 112 | + const result = renderMarkdown('line 1\nline 2', emptyLookup) |
| 113 | + expect(result).toBe('line 1<br>line 2') |
| 114 | + }) |
| 115 | + |
| 116 | + it('should convert double newlines to <br><br>', () => { |
| 117 | + const result = renderMarkdown('paragraph 1\n\nparagraph 2', emptyLookup) |
| 118 | + expect(result).toBe('paragraph 1<br><br>paragraph 2') |
| 119 | + }) |
| 120 | + |
| 121 | + it('should handle multiple formatting in same text', () => { |
| 122 | + const result = renderMarkdown('Use `foo()` for **important** tasks', emptyLookup) |
| 123 | + expect(result).toContain('<code class="docs-inline-code">foo()</code>') |
| 124 | + expect(result).toContain('<strong>important</strong>') |
| 125 | + }) |
| 126 | + |
| 127 | + it('should process {@link} tags', () => { |
| 128 | + const lookup: SymbolLookup = new Map([['MyFunc', 'function-MyFunc']]) |
| 129 | + const result = renderMarkdown('See {@link MyFunc} for details', lookup) |
| 130 | + expect(result).toContain('href="#function-MyFunc"') |
| 131 | + }) |
| 132 | + |
| 133 | + it('should escape HTML in regular text', () => { |
| 134 | + const result = renderMarkdown('Returns <T> or null', emptyLookup) |
| 135 | + expect(result).toContain('<T>') |
| 136 | + }) |
| 137 | + |
| 138 | + it('should handle empty string', () => { |
| 139 | + expect(renderMarkdown('', emptyLookup)).toBe('') |
| 140 | + }) |
| 141 | + |
| 142 | + it('should handle text with only whitespace', () => { |
| 143 | + const result = renderMarkdown(' \n ', emptyLookup) |
| 144 | + expect(result).toBe(' <br> ') |
| 145 | + }) |
| 146 | +}) |
0 commit comments