import { describe, it, expect } from 'vitest';
import { auditAccessibility, fixAccessibility } from '../a11y-fix.js';
function makePageWithBlocks(blocks) {
    return {
        id: 'page-1',
        type: 'page',
        children: [
            {
                id: 'sec-1',
                type: 'section',
                settings: {},
                children: [
                    {
                        id: 'con-1',
                        type: 'container',
                        settings: { maxWidth: '1200px' },
                        children: [
                            {
                                id: 'row-1',
                                type: 'row',
                                settings: { gap: '24px' },
                                children: [
                                    {
                                        id: 'col-1',
                                        type: 'column',
                                        settings: { width: 12 },
                                        children: blocks,
                                    },
                                ],
                            },
                        ],
                    },
                ],
            },
        ],
    };
}
describe('auditAccessibility', () => {
    it('finds missing alt text on image blocks', () => {
        const tree = makePageWithBlocks([
            {
                id: 'img-1',
                type: 'block',
                settings: { blockType: 'image' },
                data: { src: '/photo.jpg' },
            },
        ]);
        const issues = auditAccessibility(tree);
        expect(issues).toHaveLength(1);
        expect(issues[0].type).toBe('missing-alt');
        expect(issues[0].blockId).toBe('img-1');
        expect(issues[0].severity).toBe('error');
    });
    it('detects heading hierarchy skip', () => {
        const tree = makePageWithBlocks([
            {
                id: 'txt-1',
                type: 'block',
                settings: { blockType: 'text' },
                data: { heading: 'First', headingLevel: 'h2', body: 'Content' },
            },
            {
                id: 'txt-2',
                type: 'block',
                settings: { blockType: 'text' },
                data: { heading: 'Skipped', headingLevel: 'h4', body: 'Content' },
            },
        ]);
        const issues = auditAccessibility(tree);
        expect(issues).toHaveLength(1);
        expect(issues[0].type).toBe('heading-skip');
        expect(issues[0].blockId).toBe('txt-2');
        expect(issues[0].message).toContain('h2');
        expect(issues[0].message).toContain('h4');
        expect(issues[0].autoFixable).toBe(true);
    });
    it('returns empty array for well-formed tree', () => {
        const tree = makePageWithBlocks([
            {
                id: 'txt-1',
                type: 'block',
                settings: { blockType: 'text' },
                data: { heading: 'About Us', headingLevel: 'h2', body: 'We are great.' },
            },
            {
                id: 'txt-2',
                type: 'block',
                settings: { blockType: 'text' },
                data: { heading: 'Our Team', headingLevel: 'h3', body: 'Meet the team.' },
            },
            {
                id: 'img-1',
                type: 'block',
                settings: { blockType: 'image' },
                data: { src: '/photo.jpg', alt: 'Team photo' },
            },
            {
                id: 'cta-1',
                type: 'block',
                settings: { blockType: 'cta' },
                data: { heading: 'Get Started', buttonText: 'Contact Us', buttonLink: '/contact' },
            },
        ]);
        const issues = auditAccessibility(tree);
        expect(issues).toHaveLength(0);
    });
    it('detects missing alt on hero block images', () => {
        const tree = makePageWithBlocks([
            {
                id: 'hero-1',
                type: 'block',
                settings: { blockType: 'hero' },
                data: { title: 'Welcome', image: '/bg.jpg' },
            },
        ]);
        const issues = auditAccessibility(tree);
        expect(issues.some((i) => i.type === 'missing-alt' && i.blockId === 'hero-1')).toBe(true);
    });
    it('detects missing alt on gallery images', () => {
        const tree = makePageWithBlocks([
            {
                id: 'gal-1',
                type: 'block',
                settings: { blockType: 'gallery' },
                data: {
                    images: [{ src: '/a.jpg', alt: 'Photo A' }, { src: '/b.jpg' }, { src: '/c.jpg' }],
                },
            },
        ]);
        const issues = auditAccessibility(tree);
        const galleryIssues = issues.filter((i) => i.blockId === 'gal-1' && i.type === 'missing-alt');
        expect(galleryIssues).toHaveLength(2);
    });
});
describe('fixAccessibility', () => {
    it('fixes heading hierarchy', () => {
        const tree = makePageWithBlocks([
            {
                id: 'txt-1',
                type: 'block',
                settings: { blockType: 'text' },
                data: { heading: 'First', headingLevel: 'h2', body: 'Content' },
            },
            {
                id: 'txt-2',
                type: 'block',
                settings: { blockType: 'text' },
                data: { heading: 'Skipped', headingLevel: 'h4', body: 'Content' },
            },
        ]);
        const result = fixAccessibility(tree);
        const blocks = [];
        function collectBlocks(node) {
            const n = node;
            if (n.type === 'block') {
                blocks.push(n);
                return;
            }
            if (n.children) {
                for (const child of n.children) {
                    collectBlocks(child);
                }
            }
        }
        collectBlocks(result.tree);
        const fixedBlock = blocks.find((b) => b.id === 'txt-2');
        expect(fixedBlock?.data.headingLevel).toBe('h3');
    });
    it('adds placeholder alt text to images', () => {
        const tree = makePageWithBlocks([
            {
                id: 'img-1',
                type: 'block',
                settings: { blockType: 'image' },
                data: { src: '/photo.jpg' },
            },
        ]);
        const result = fixAccessibility(tree);
        const blocks = [];
        function collectBlocks(node) {
            const n = node;
            if (n.type === 'block') {
                blocks.push(n);
                return;
            }
            if (n.children) {
                for (const child of n.children) {
                    collectBlocks(child);
                }
            }
        }
        collectBlocks(result.tree);
        const fixedBlock = blocks.find((b) => b.id === 'img-1');
        expect(fixedBlock?.data.alt).toBe('Image in image section');
    });
    it('returns accurate report counts', () => {
        const tree = makePageWithBlocks([
            {
                id: 'img-1',
                type: 'block',
                settings: { blockType: 'image' },
                data: { src: '/photo.jpg' },
            },
            {
                id: 'txt-1',
                type: 'block',
                settings: { blockType: 'text' },
                data: { heading: 'First', headingLevel: 'h2', body: 'Content' },
            },
            {
                id: 'txt-2',
                type: 'block',
                settings: { blockType: 'text' },
                data: { heading: 'Skip', headingLevel: 'h4', body: 'Content' },
            },
            {
                id: 'form-1',
                type: 'block',
                settings: { blockType: 'form' },
                data: {},
            },
        ]);
        const result = fixAccessibility(tree);
        expect(result.report.fixedCount).toBe(2);
        expect(result.report.remainingCount).toBeGreaterThan(0);
        expect(result.report.issues.length).toBeGreaterThan(result.report.fixedCount);
    });
    it('does not mutate the original tree', () => {
        const tree = makePageWithBlocks([
            {
                id: 'img-1',
                type: 'block',
                settings: { blockType: 'image' },
                data: { src: '/photo.jpg' },
            },
        ]);
        const originalJson = JSON.stringify(tree);
        fixAccessibility(tree);
        expect(JSON.stringify(tree)).toBe(originalJson);
    });
});
//# sourceMappingURL=a11y-fix.test.js.map