"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("@testing-library/react");
const vitest_1 = require("vitest");
const ComplianceReportGenerator_1 = require("../ComplianceReportGenerator");
const react_2 = require("@chakra-ui/react");
// Mock Chakra UI's useToast
vitest_1.vi.mock('@chakra-ui/react', async () => {
    const actual = await vitest_1.vi.importActual('@chakra-ui/react');
    return {
        ...actual,
        useToast: vitest_1.vi.fn(),
    };
});
// Mock fetch
const mockFetch = vitest_1.vi.fn();
global.fetch = mockFetch;
// Mock clipboard API
Object.assign(navigator, {
    clipboard: {
        writeText: vitest_1.vi.fn(),
    },
});
(0, vitest_1.describe)('ComplianceReportGenerator', () => {
    const mockToast = vitest_1.vi.fn();
    const defaultProps = {
        drupalSettings: {
            baseUrl: 'http://example.com',
            csrfToken: 'test-token',
            userId: '1',
            defaultReportSettings: {
                timeRange: 'month',
                format: 'pdf',
                includeTrends: true,
                includeRecommendations: true,
                includeDetails: true,
                groupBy: 'severity',
                filters: {
                    severities: [],
                    categories: [],
                    regions: [],
                },
            },
        },
        experienceBuilder: {
            onAction: vitest_1.vi.fn(),
            isEditing: false,
            blockId: 'test-block',
        },
    };
    const mockReportData = {
        summary: {
            totalControls: 100,
            compliantControls: 80,
            nonCompliantControls: 20,
            complianceScore: 80,
            severityDistribution: {
                critical: 5,
                high: 10,
                medium: 15,
                low: 70,
            },
        },
        trends: {
            dates: ['2024-01-01', '2024-01-02', '2024-01-03'],
            scores: [75, 78, 80],
            compliantCounts: [70, 75, 80],
            nonCompliantCounts: [30, 25, 20],
        },
        details: [
            {
                id: '1',
                name: 'Test Control 1',
                category: 'Security',
                severity: 'high',
                status: 'compliant',
                score: 100,
                region: 'US',
                lastChecked: '2024-01-03T00:00:00Z',
                description: 'Test control description',
                recommendations: ['Test recommendation 1'],
            },
        ],
    };
    (0, vitest_1.beforeEach)(() => {
        vitest_1.vi.clearAllMocks();
        react_2.useToast.mockReturnValue(mockToast);
        mockFetch.mockReset();
    });
    (0, vitest_1.it)('renders in edit mode', () => {
        const props = {
            ...defaultProps,
            experienceBuilder: {
                ...defaultProps.experienceBuilder,
                isEditing: true,
            },
        };
        (0, react_1.render)((0, jsx_runtime_1.jsx)(ComplianceReportGenerator_1.ComplianceReportGenerator, { ...props }));
        (0, vitest_1.expect)(react_1.screen.getByText('Compliance Report Generator')).toBeInTheDocument();
        (0, vitest_1.expect)(react_1.screen.getByText('This component will generate and display compliance reports when placed on a page.')).toBeInTheDocument();
    });
    (0, vitest_1.it)('loads report data on mount', async () => {
        mockFetch.mockResolvedValueOnce({
            ok: true,
            json: () => Promise.resolve(mockReportData),
        });
        (0, react_1.render)((0, jsx_runtime_1.jsx)(ComplianceReportGenerator_1.ComplianceReportGenerator, { ...defaultProps }));
        await (0, react_1.waitFor)(() => {
            (0, vitest_1.expect)(mockFetch).toHaveBeenCalledWith('http://example.com/api/compliance/reports/generate', vitest_1.expect.objectContaining({
                method: 'POST',
                headers: vitest_1.expect.objectContaining({
                    'X-CSRF-Token': 'test-token',
                }),
            }));
        });
        (0, vitest_1.expect)(react_1.screen.getByText('80%')).toBeInTheDocument();
        (0, vitest_1.expect)(react_1.screen.getByText('80')).toBeInTheDocument();
        (0, vitest_1.expect)(react_1.screen.getByText('20')).toBeInTheDocument();
    });
    (0, vitest_1.it)('handles report generation error', async () => {
        mockFetch.mockRejectedValueOnce(new Error('Failed to generate report'));
        (0, react_1.render)((0, jsx_runtime_1.jsx)(ComplianceReportGenerator_1.ComplianceReportGenerator, { ...defaultProps }));
        await (0, react_1.waitFor)(() => {
            (0, vitest_1.expect)(mockToast).toHaveBeenCalledWith(vitest_1.expect.objectContaining({
                title: 'Error',
                description: 'Failed to generate compliance report. Please try again.',
                status: 'error',
            }));
        });
        (0, vitest_1.expect)(react_1.screen.getByText('Failed to generate compliance report')).toBeInTheDocument();
    });
    (0, vitest_1.it)('changes time range and updates report', async () => {
        mockFetch
            .mockResolvedValueOnce({
            ok: true,
            json: () => Promise.resolve(mockReportData),
        })
            .mockResolvedValueOnce({
            ok: true,
            json: () => Promise.resolve(mockReportData),
        });
        (0, react_1.render)((0, jsx_runtime_1.jsx)(ComplianceReportGenerator_1.ComplianceReportGenerator, { ...defaultProps }));
        // Wait for initial load
        await (0, react_1.waitFor)(() => {
            (0, vitest_1.expect)(react_1.screen.getByText('80%')).toBeInTheDocument();
        });
        // Change time range
        react_1.fireEvent.change(react_1.screen.getByLabelText('Time Range'), {
            target: { value: 'week' },
        });
        // Verify new report request
        await (0, react_1.waitFor)(() => {
            (0, vitest_1.expect)(mockFetch).toHaveBeenCalledTimes(2);
            (0, vitest_1.expect)(mockFetch).toHaveBeenLastCalledWith('http://example.com/api/compliance/reports/generate', vitest_1.expect.objectContaining({
                method: 'POST',
                body: vitest_1.expect.stringContaining('"timeRange":"week"'),
            }));
        });
    });
    (0, vitest_1.it)('downloads report in different formats', async () => {
        mockFetch
            .mockResolvedValueOnce({
            ok: true,
            json: () => Promise.resolve(mockReportData),
        })
            .mockResolvedValueOnce({
            ok: true,
            blob: () => Promise.resolve(new Blob(['test'], { type: 'application/pdf' })),
        });
        (0, react_1.render)((0, jsx_runtime_1.jsx)(ComplianceReportGenerator_1.ComplianceReportGenerator, { ...defaultProps }));
        // Wait for initial load
        await (0, react_1.waitFor)(() => {
            (0, vitest_1.expect)(react_1.screen.getByText('80%')).toBeInTheDocument();
        });
        // Click download button and select PDF
        react_1.fireEvent.click(react_1.screen.getByText('Download'));
        react_1.fireEvent.click(react_1.screen.getByText('PDF'));
        await (0, react_1.waitFor)(() => {
            (0, vitest_1.expect)(mockFetch).toHaveBeenCalledWith('http://example.com/api/compliance/reports/download', vitest_1.expect.objectContaining({
                method: 'POST',
                body: vitest_1.expect.stringContaining('"format":"pdf"'),
            }));
        });
        (0, vitest_1.expect)(mockToast).toHaveBeenCalledWith(vitest_1.expect.objectContaining({
            title: 'Report downloaded',
            description: 'Your compliance report has been downloaded in PDF format.',
            status: 'success',
        }));
    });
    (0, vitest_1.it)('shares report and copies link to clipboard', async () => {
        const mockShareUrl = 'http://example.com/share/abc123';
        mockFetch
            .mockResolvedValueOnce({
            ok: true,
            json: () => Promise.resolve(mockReportData),
        })
            .mockResolvedValueOnce({
            ok: true,
            json: () => Promise.resolve({ shareUrl: mockShareUrl }),
        });
        (0, react_1.render)((0, jsx_runtime_1.jsx)(ComplianceReportGenerator_1.ComplianceReportGenerator, { ...defaultProps }));
        // Wait for initial load
        await (0, react_1.waitFor)(() => {
            (0, vitest_1.expect)(react_1.screen.getByText('80%')).toBeInTheDocument();
        });
        // Click share button
        react_1.fireEvent.click(react_1.screen.getByLabelText('Share report'));
        await (0, react_1.waitFor)(() => {
            (0, vitest_1.expect)(navigator.clipboard.writeText).toHaveBeenCalledWith(mockShareUrl);
            (0, vitest_1.expect)(mockToast).toHaveBeenCalledWith(vitest_1.expect.objectContaining({
                title: 'Report shared',
                description: 'The report link has been copied to your clipboard.',
                status: 'success',
            }));
        });
        (0, vitest_1.expect)(defaultProps.experienceBuilder.onAction).toHaveBeenCalledWith('report_shared', { shareUrl: mockShareUrl });
    });
    (0, vitest_1.it)('updates settings and triggers report generation', async () => {
        mockFetch
            .mockResolvedValueOnce({
            ok: true,
            json: () => Promise.resolve(mockReportData),
        })
            .mockResolvedValueOnce({
            ok: true,
            json: () => Promise.resolve(mockReportData),
        });
        (0, react_1.render)((0, jsx_runtime_1.jsx)(ComplianceReportGenerator_1.ComplianceReportGenerator, { ...defaultProps }));
        // Wait for initial load
        await (0, react_1.waitFor)(() => {
            (0, vitest_1.expect)(react_1.screen.getByText('80%')).toBeInTheDocument();
        });
        // Go to settings tab
        react_1.fireEvent.click(react_1.screen.getByText('Settings'));
        // Toggle include trends
        react_1.fireEvent.click(react_1.screen.getByLabelText('Include Trends'));
        // Verify new report request
        await (0, react_1.waitFor)(() => {
            (0, vitest_1.expect)(mockFetch).toHaveBeenCalledTimes(2);
            (0, vitest_1.expect)(mockFetch).toHaveBeenLastCalledWith('http://example.com/api/compliance/reports/generate', vitest_1.expect.objectContaining({
                method: 'POST',
                body: vitest_1.expect.stringContaining('"includeTrends":false'),
            }));
        });
    });
    (0, vitest_1.it)('displays trend data in charts', async () => {
        mockFetch.mockResolvedValueOnce({
            ok: true,
            json: () => Promise.resolve(mockReportData),
        });
        (0, react_1.render)((0, jsx_runtime_1.jsx)(ComplianceReportGenerator_1.ComplianceReportGenerator, { ...defaultProps }));
        // Wait for initial load
        await (0, react_1.waitFor)(() => {
            (0, vitest_1.expect)(react_1.screen.getByText('80%')).toBeInTheDocument();
        });
        // Go to trends tab
        react_1.fireEvent.click(react_1.screen.getByText('Trends'));
        // Verify trend data is displayed
        (0, vitest_1.expect)(react_1.screen.getByText('Compliance Score')).toBeInTheDocument();
        (0, vitest_1.expect)(react_1.screen.getByText('Compliant Controls')).toBeInTheDocument();
        (0, vitest_1.expect)(react_1.screen.getByText('Non-Compliant Controls')).toBeInTheDocument();
    });
    (0, vitest_1.it)('displays detailed control information', async () => {
        mockFetch.mockResolvedValueOnce({
            ok: true,
            json: () => Promise.resolve(mockReportData),
        });
        (0, react_1.render)((0, jsx_runtime_1.jsx)(ComplianceReportGenerator_1.ComplianceReportGenerator, { ...defaultProps }));
        // Wait for initial load
        await (0, react_1.waitFor)(() => {
            (0, vitest_1.expect)(react_1.screen.getByText('80%')).toBeInTheDocument();
        });
        // Go to details tab
        react_1.fireEvent.click(react_1.screen.getByText('Details'));
        // Verify control details are displayed
        (0, vitest_1.expect)(react_1.screen.getByText('Test Control 1')).toBeInTheDocument();
        (0, vitest_1.expect)(react_1.screen.getByText('Security')).toBeInTheDocument();
        (0, vitest_1.expect)(react_1.screen.getByText('high')).toBeInTheDocument();
        (0, vitest_1.expect)(react_1.screen.getByText('compliant')).toBeInTheDocument();
        (0, vitest_1.expect)(react_1.screen.getByText('Test control description')).toBeInTheDocument();
        (0, vitest_1.expect)(react_1.screen.getByText('Test recommendation 1')).toBeInTheDocument();
    });
    (0, vitest_1.it)('handles custom date range selection', async () => {
        mockFetch
            .mockResolvedValueOnce({
            ok: true,
            json: () => Promise.resolve(mockReportData),
        })
            .mockResolvedValueOnce({
            ok: true,
            json: () => Promise.resolve(mockReportData),
        });
        (0, react_1.render)((0, jsx_runtime_1.jsx)(ComplianceReportGenerator_1.ComplianceReportGenerator, { ...defaultProps }));
        // Wait for initial load
        await (0, react_1.waitFor)(() => {
            (0, vitest_1.expect)(react_1.screen.getByText('80%')).toBeInTheDocument();
        });
        // Go to settings tab
        react_1.fireEvent.click(react_1.screen.getByText('Settings'));
        // Select custom time range
        react_1.fireEvent.change(react_1.screen.getByLabelText('Time Range'), {
            target: { value: 'custom' },
        });
        // Set custom dates
        react_1.fireEvent.change(react_1.screen.getByLabelText('Start Date'), {
            target: { value: '2024-01-01' },
        });
        react_1.fireEvent.change(react_1.screen.getByLabelText('End Date'), {
            target: { value: '2024-01-31' },
        });
        // Verify new report request with custom dates
        await (0, react_1.waitFor)(() => {
            (0, vitest_1.expect)(mockFetch).toHaveBeenCalledTimes(2);
            (0, vitest_1.expect)(mockFetch).toHaveBeenLastCalledWith('http://example.com/api/compliance/reports/generate', vitest_1.expect.objectContaining({
                method: 'POST',
                body: vitest_1.expect.stringContaining('"startDate":"2024-01-01"'),
            }));
        });
    });
});
//# sourceMappingURL=ComplianceReportGenerator.test.js.map