|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { describe, expect, it } from 'vitest' |
| 5 | +import { HtmlComplexityError, HtmlParser } from '@/lib/file-parsers/html-parser' |
| 6 | + |
| 7 | +const parser = new HtmlParser() |
| 8 | + |
| 9 | +describe('HtmlParser', () => { |
| 10 | + describe('resource limits', () => { |
| 11 | + it('rejects a document above the input byte cap', async () => { |
| 12 | + const sparse = Buffer.concat([ |
| 13 | + Buffer.from('<html><body><p>'), |
| 14 | + Buffer.alloc(32 * 1024 * 1024, 0x61), |
| 15 | + Buffer.from('</p></body></html>'), |
| 16 | + ]) |
| 17 | + |
| 18 | + await expect(parser.parseBuffer(sparse)).rejects.toThrow( |
| 19 | + /above the maximum of 33554432 bytes/ |
| 20 | + ) |
| 21 | + }) |
| 22 | + |
| 23 | + it('rejects a tag-dense document above the markup-token cap', async () => { |
| 24 | + const dense = Buffer.from(`<html><body>${'<p>a</p>'.repeat(300_000)}</body></html>`) |
| 25 | + |
| 26 | + const error = await parser.parseBuffer(dense).catch((e) => e) |
| 27 | + |
| 28 | + expect(error).toBeInstanceOf(HtmlComplexityError) |
| 29 | + expect(error.message).toMatch(/exceeds the maximum of 500000 markup tokens/) |
| 30 | + }) |
| 31 | + |
| 32 | + it('accepts a byte-heavy document whose markup stays under the token cap', async () => { |
| 33 | + const paragraph = `<p>${'word '.repeat(200)}</p>` |
| 34 | + const buffer = Buffer.from(`<html><body>${paragraph.repeat(2000)}</body></html>`) |
| 35 | + |
| 36 | + const result = await parser.parseBuffer(buffer) |
| 37 | + |
| 38 | + expect(result.content).toContain('word') |
| 39 | + }) |
| 40 | + |
| 41 | + /** |
| 42 | + * Deep nesting overflows the stack inside cheerio's own recursive `.text()`, |
| 43 | + * which the caps cannot pre-empt. A `RangeError` is catchable, so it must |
| 44 | + * surface as a rejected promise rather than take the process down. |
| 45 | + */ |
| 46 | + it('surfaces deeply nested markup as a catchable error, not a crash', async () => { |
| 47 | + const depth = 15_000 |
| 48 | + const buffer = Buffer.from( |
| 49 | + `<html><body>${'<div>'.repeat(depth)}deep${'</div>'.repeat(depth)}</body></html>` |
| 50 | + ) |
| 51 | + |
| 52 | + await expect(parser.parseBuffer(buffer)).rejects.toThrow(/Failed to parse HTML buffer/) |
| 53 | + }) |
| 54 | + }) |
| 55 | + |
| 56 | + describe('extraction', () => { |
| 57 | + it('extracts structured text, headings, links, and metadata', async () => { |
| 58 | + const buffer = Buffer.from( |
| 59 | + `<html><head><title>Doc</title><meta name="description" content="About"></head>` + |
| 60 | + `<body><h1>Title</h1><p>Body text</p>` + |
| 61 | + `<ul><li>one</li><li>two</li></ul>` + |
| 62 | + `<table><tr><th>h</th></tr><tr><td>c</td></tr></table>` + |
| 63 | + `<a href="https://example.com">Example</a>` + |
| 64 | + `<script>alert(1)</script></body></html>` |
| 65 | + ) |
| 66 | + |
| 67 | + const result = await parser.parseBuffer(buffer) |
| 68 | + |
| 69 | + expect(result.metadata?.title).toBe('Doc') |
| 70 | + expect(result.metadata?.metaDescription).toBe('About') |
| 71 | + expect(result.content).toContain('Title') |
| 72 | + expect(result.content).toContain('Body text') |
| 73 | + expect(result.content).toContain('• one') |
| 74 | + expect(result.content).toContain('| h |') |
| 75 | + expect(result.content).toContain('Example (https://example.com)') |
| 76 | + expect(result.content).not.toContain('alert(1)') |
| 77 | + expect(result.metadata?.headings).toEqual([{ level: 1, text: 'Title' }]) |
| 78 | + expect(result.metadata?.links).toEqual([{ text: 'Example', href: 'https://example.com' }]) |
| 79 | + expect(result.metadata?.listCount).toBe(1) |
| 80 | + expect(result.metadata?.tableCount).toBe(1) |
| 81 | + }) |
| 82 | + }) |
| 83 | +}) |
0 commit comments