"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const report_generator_1 = require("../report-generator");
const promises_1 = __importDefault(require("fs/promises"));
const path_1 = __importDefault(require("path"));
// Mock fs module
jest.mock('fs/promises', () => ({
    mkdir: jest.fn(),
    writeFile: jest.fn(),
}));
// Mock logger
jest.mock('@bluefly/llmcli/utils/logger', () => ({
    logger: {
        info: jest.fn(),
        error: jest.fn(),
    },
}));
describe('ReportGenerator', () => {
    let reportGenerator;
    const mockResults = [
        {
            criterion: '1.1.1',
            severity: 'error',
            message: 'Image missing alt text',
            details: 'The image element has no alt attribute',
            code: '<img src="logo.png" />',
            fix: '<img src="logo.png" alt="Company Logo" />',
            aiSuggestions: ['Add descriptive alt text', 'Consider using aria-label if alt is not appropriate'],
        },
        {
            criterion: '2.1.1',
            severity: 'warning',
            message: 'Custom button missing keyboard support',
            details: 'The div with role="button" is not keyboard accessible',
            code: '<div role="button" onClick={handleClick}>Click me</div>',
            fix: '<div role="button" onClick={handleClick} onKeyDown={handleKeyDown} tabIndex={0}>Click me</div>',
        },
        {
            criterion: '4.1.2',
            severity: 'info',
            message: 'Form control missing label',
            details: 'The input element has no associated label',
            code: '<input type="text" />',
            fix: '<label htmlFor="username">Username</label><input type="text" id="username" />',
        },
    ];
    beforeEach(() => {
        reportGenerator = report_generator_1.ReportGenerator.getInstance();
        jest.clearAllMocks();
    });
    describe('generateHtmlReport', () => {
        it('should generate HTML report with default options', async () => {
            const html = await reportGenerator.generateHtmlReport(mockResults);
            // Check basic structure
            expect(html).toContain('<!DOCTYPE html>');
            expect(html).toContain('<html lang="en">');
            expect(html).toContain('Accessibility Report');
            // Check content
            expect(html).toContain('Image missing alt text');
            expect(html).toContain('Custom button missing keyboard support');
            expect(html).toContain('Form control missing label');
            // Check AI suggestions
            expect(html).toContain('AI Suggestions');
            expect(html).toContain('Add descriptive alt text');
            // Check fixes
            expect(html).toContain('Suggested Fix');
            expect(html).toContain('alt="Company Logo"');
            // Check severity badges
            expect(html).toContain('severity-error');
            expect(html).toContain('severity-warning');
            expect(html).toContain('severity-info');
        });
        it('should generate report without AI suggestions when includeAI is false', async () => {
            const html = await reportGenerator.generateHtmlReport(mockResults, { includeAI: false });
            expect(html).not.toContain('AI Suggestions');
            expect(html).not.toContain('Add descriptive alt text');
        });
        it('should generate report without fixes when includeFixes is false', async () => {
            const html = await reportGenerator.generateHtmlReport(mockResults, { includeFixes: false });
            expect(html).not.toContain('Suggested Fix');
            expect(html).not.toContain('alt="Company Logo"');
        });
        it('should generate dark theme report', async () => {
            const html = await reportGenerator.generateHtmlReport(mockResults, { theme: 'dark' });
            expect(html).toContain('--bg-color: #1a1a1a');
            expect(html).toContain('--text-color: #ffffff');
        });
        it('should write report to file when outputPath is provided', async () => {
            const outputPath = 'reports/accessibility.html';
            await reportGenerator.generateHtmlReport(mockResults, { outputPath });
            expect(promises_1.default.mkdir).toHaveBeenCalledWith(path_1.default.dirname(outputPath), { recursive: true });
            expect(promises_1.default.writeFile).toHaveBeenCalledWith(outputPath, expect.any(String), 'utf-8');
        });
        it('should handle file write errors', async () => {
            const error = new Error('Write failed');
            promises_1.default.writeFile.mockRejectedValueOnce(error);
            const outputPath = 'reports/accessibility.html';
            await expect(reportGenerator.generateHtmlReport(mockResults, { outputPath })).rejects.toThrow('Failed to write report to file');
        });
    });
    describe('report content', () => {
        it('should group results by criterion', async () => {
            const html = await reportGenerator.generateHtmlReport(mockResults);
            // Check criterion sections
            expect(html).toContain('id="criterion-1.1.1"');
            expect(html).toContain('id="criterion-2.1.1"');
            expect(html).toContain('id="criterion-4.1.2"');
            // Check criterion titles
            expect(html).toContain('Non-text Content');
            expect(html).toContain('Keyboard');
            expect(html).toContain('Name, Role, Value');
        });
        it('should include summary statistics', async () => {
            const html = await reportGenerator.generateHtmlReport(mockResults);
            // Check summary grid
            expect(html).toContain('class="summary-grid"');
            expect(html).toContain('Total Issues');
            expect(html).toContain('Errors');
            expect(html).toContain('Warnings');
            expect(html).toContain('Info');
            // Check counts
            expect(html).toContain('<span class="count">3</span>'); // Total
            expect(html).toContain('<span class="count">1</span>'); // Errors
            expect(html).toContain('<span class="count">1</span>'); // Warnings
            expect(html).toContain('<span class="count">1</span>'); // Info
        });
        it('should include interactive features', async () => {
            const html = await reportGenerator.generateHtmlReport(mockResults);
            // Check copy button script
            expect(html).toContain('copy-button');
            expect(html).toContain('navigator.clipboard.writeText');
            // Check filter buttons
            expect(html).toContain('filter-buttons');
            expect(html).toContain('data-severity="all"');
            expect(html).toContain('data-severity="error"');
            expect(html).toContain('data-severity="warning"');
            expect(html).toContain('data-severity="info"');
        });
        it('should escape HTML in content', async () => {
            const resultsWithHtml = [{
                    criterion: '1.1.1',
                    severity: 'error',
                    message: 'Image with <script>alert("xss")</script>',
                    details: 'Contains <script> tags',
                    code: '<img src="xss.png" onerror="alert(1)" />',
                    fix: '<img src="xss.png" alt="Safe image" />',
                }];
            const html = await reportGenerator.generateHtmlReport(resultsWithHtml);
            // Check escaped content
            expect(html).toContain('&lt;script&gt;alert("xss")&lt;/script&gt;');
            expect(html).toContain('Contains &lt;script&gt; tags');
            expect(html).toContain('&lt;img src="xss.png" onerror="alert(1)" /&gt;');
            expect(html).not.toContain('<script>alert("xss")</script>');
        });
    });
});
//# sourceMappingURL=report-generator.test.js.map