/**
 * @jest-environment node
 */

const {
	escapeHtml,
	buildQuickAnswerInnerHtml,
	buildQuickAnswerClassList,
} = require('./serialize');

describe('Quick Answer serialize utils', () => {
	it('escapes XSS in label and answer', () => {
		const html = buildQuickAnswerInnerHtml({
			label: '<script>alert(1)</script>',
			answer: 'Use <img src=x onerror=alert(1)>.',
		});
		expect(html).toContain('&lt;script&gt;');
		expect(html).toContain('&lt;img src=x');
		expect(html).not.toContain('<script>');
		expect(html).not.toContain('<img src=x');
	});

	it('returns empty when answer is missing', () => {
		expect(buildQuickAnswerInnerHtml({ label: 'Quick answer', answer: '' })).toBe('');
		expect(buildQuickAnswerInnerHtml({ label: 'Quick answer', answer: '   ' })).toBe('');
	});

	it('never emits JSON-LD', () => {
		const html = buildQuickAnswerInnerHtml({
			label: 'Quick answer',
			answer: 'Pruning keeps trees healthy.',
		});
		expect(html).not.toContain('application/ld+json');
		expect(html).not.toContain('@type');
		expect(html).not.toContain('FAQPage');
	});

	it('builds style classes', () => {
		const classes = buildQuickAnswerClassList({
			stylePreset: 'callout',
			colorPreset: 'green',
			density: 'compact',
		});
		expect(classes).toContain('seovault-quick-answer-block');
		expect(classes).toContain('seovault-quick-answer-block--style-callout');
		expect(classes).toContain('seovault-quick-answer-block--color-green');
		expect(classes).toContain('seovault-quick-answer-block--density-compact');
	});

	it('escapeHtml encodes entities', () => {
		expect(escapeHtml(`a&b<"'>`)).toBe('a&amp;b&lt;&quot;&#039;&gt;');
	});
});
