import { describe, expect, it } from 'vitest'

import { decodeHtmlEntities } from './decode-html-entities'

describe('decodeHtmlEntities', () => {
	it('decodes named entities (with semicolon)', () => {
		expect(decodeHtmlEntities('&quot;&apos;&amp;&lt;&gt;')).toBe(`"'&<>`)
	})

	it('tolerates missing semicolons', () => {
		expect(decodeHtmlEntities('&quot')).toBe('"')
		expect(decodeHtmlEntities('&amp')).toBe('&')
	})

	it('decodes numeric entities', () => {
		expect(decodeHtmlEntities('&#34;')).toBe('"')
		expect(decodeHtmlEntities('&#x22;')).toBe('"')
	})

	it('leaves unknown entities untouched', () => {
		expect(decodeHtmlEntities('&thisisnotanentity;')).toBe('&thisisnotanentity;')
		expect(decodeHtmlEntities('&thisisnotanentity')).toBe('&thisisnotanentity')
	})

	it('handles nested encodings', () => {
		expect(decodeHtmlEntities('&amp;quot;')).toBe('"')
	})
})
