import assert from 'node:assert/strict'
import test from 'node:test'
import { runInNewContext } from 'node:vm'
import { stripSearchExcerptMarkdown } from '../src/components/docs/search-excerpt.mjs'
test('unterminated escaped link destinations do not backtrack exponentially', () => {
for (const prefix of [' {
const input = prefix + '\\('.repeat(32_768)
const result = runInNewContext(
'strip(input)',
{ strip: stripSearchExcerptMarkdown, input },
{
timeout: 250
}
)
assert.equal(result, input.replace(/\\\(/g, '('))
}
})
test('link destinations retain escaped closing parentheses and literal escapes', () => {
for (const prefix of [' {
assert.equal(stripSearchExcerptMarkdown(prefix + 'https://example.com/a\\)b)'), 'label')
assert.equal(stripSearchExcerptMarkdown(prefix + 'https://example.com/a\\(b)'), 'label')
assert.equal(stripSearchExcerptMarkdown(prefix + 'https://example.com/a\\)'), 'label')
assert.equal(stripSearchExcerptMarkdown(prefix + 'https://example.com/a\\\\))'), 'label)')
assert.equal(stripSearchExcerptMarkdown(prefix + 'https://example.com/a\\\nb)'), 'label')
}
})
test('search excerpts omit inline Markdown syntax', () => {
assert.equal(
stripSearchExcerptMarkdown(
'Use **bold**, *emphasis*, `inline code`, and [linked text](https://example.com).'
),
'Use bold, emphasis, inline code, and linked text.'
)
})
test('search excerpts preserve highlighted matches inside Markdown', () => {
assert.equal(
stripSearchExcerptMarkdown('**Privacy controls**'),
'Privacy controls'
)
assert.equal(
stripSearchExcerptMarkdown('[Open settings](https://example.com/settings)'),
'Open settings'
)
assert.equal(
stripSearchExcerptMarkdown('Use `git **status**`'),
'Use git **status**'
)
assert.equal(
stripSearchExcerptMarkdown('**Privacy controls**'),
'Privacy controls'
)
assert.equal(stripSearchExcerptMarkdown('`git status`'), 'git status')
assert.equal(
stripSearchExcerptMarkdown('[Privacy controls](https://example.com/privacy)'),
'Privacy controls'
)
})
test('unclosed angles finish within the excerpt budget without changing text', () => {
const input = '<'.repeat(131_072)
const result = runInNewContext(
'strip(input)',
{ strip: stripSearchExcerptMarkdown, input },
{ timeout: 250 }
)
assert.equal(result, input)
})
test('tag guards preserve malformed markup, protected code and highlighted matches', () => {
const cases = [
['**bold**', 'bold'],
['<> **bold**', '<> bold'],
['<bold', 'bold'],
['hello', 'hello'],
['`**literal**`', '**literal**'],
['**highlight**', 'highlight'],
['**highlight**', 'highlight'],
['[