Files
renjue f1502ed08c
CI / docker (push) Successful in 11m42s
初始化 ToolBox 开发者工具箱。
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-25 11:15:38 +08:00

42 lines
1.3 KiB
JavaScript

import { describe, it, expect } from 'vitest'
import { escapeHtml, highlightDiff } from '../../../src/utils/comparator/html.js'
describe('comparator/html', () => {
describe('escapeHtml', () => {
it('escapes special HTML characters', () => {
expect(escapeHtml('<script>"\'&</script>')).toBe(
'&lt;script&gt;&quot;&#39;&amp;&lt;/script&gt;'
)
})
it('handles empty string', () => {
expect(escapeHtml('')).toBe('')
})
it('coerces non-string values', () => {
expect(escapeHtml(123)).toBe('123')
})
})
describe('highlightDiff', () => {
it('returns escaped text when no ranges', () => {
expect(highlightDiff('hello', [])).toBe('hello')
expect(highlightDiff('a<b', null)).toBe('a&lt;b')
})
it('wraps diff ranges with highlight span', () => {
const result = highlightDiff('hello', [{ start: 1, end: 4 }])
expect(result).toBe('h<span class="diff-highlight">ell</span>o')
})
it('handles multiple ranges', () => {
const result = highlightDiff('abcdef', [
{ start: 0, end: 1 },
{ start: 4, end: 6 },
])
expect(result).toContain('<span class="diff-highlight">a</span>bcd')
expect(result).toContain('<span class="diff-highlight">ef</span>')
})
})
})