import { describe, expect, it } from 'vitest'
import { decodeHtmlEntities, encodeHtmlEntities } from '../utilities/htmlEntities.js'
describe('decodeHtmlEntities', () => {
it('decodes < to <', () => {
expect(decodeHtmlEntities('<div>')).toBe('
')
})
it('decodes & to &', () => {
expect(decodeHtmlEntities('a & b')).toBe('a & b')
})
it('decodes " to "', () => {
expect(decodeHtmlEntities('"hello"')).toBe('"hello"')
})
it('handles doubly-encoded sequences like <', () => {
expect(decodeHtmlEntities('<')).toBe('<')
})
it('returns plain text unchanged', () => {
expect(decodeHtmlEntities('hello world')).toBe('hello world')
})
})
describe('encodeHtmlEntities', () => {
it('encodes < to <', () => {
expect(encodeHtmlEntities('
')).toBe('<div>')
})
it('encodes & to &', () => {
expect(encodeHtmlEntities('a & b')).toBe('a & b')
})
it('does not encode " (quotes are valid in markdown)', () => {
expect(encodeHtmlEntities('"hello"')).toBe('"hello"')
})
it('returns plain text unchanged', () => {
expect(encodeHtmlEntities('hello world')).toBe('hello world')
})
})
describe('roundtrip', () => {
it.each(['
', 'a & b', 'x < y & y > z'])('encode then decode roundtrips: %s', input => {
expect(decodeHtmlEntities(encodeHtmlEntities(input))).toBe(input)
})
it('decode is a superset of encode – " decodes but " is not encoded', () => {
// " passes through encode unchanged, " decodes to "
expect(encodeHtmlEntities('"hello"')).toBe('"hello"')
expect(decodeHtmlEntities('"hello"')).toBe('"hello"')
})
})