import { describe, it, expect } from 'vitest';
import { extractContent, analyzeReadability, analyzeSEO, stripHtml, countWords, countSyllables, } from '../seo-analyzer.js';
function makeTestPage() {
    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: [
                                            {
                                                id: 'blk-hero',
                                                type: 'block',
                                                settings: { blockType: 'hero' },
                                                data: {
                                                    title: 'Welcome to Our Service',
                                                    subtitle: 'The best solution for your needs',
                                                    ctaText: 'Get Started',
                                                    ctaLink: '/signup',
                                                    image: '/hero.jpg',
                                                    alt: 'Hero banner showing our service',
                                                },
                                            },
                                        ],
                                    },
                                ],
                            },
                        ],
                    },
                ],
            },
            {
                id: 'sec-2',
                type: 'section',
                settings: {},
                children: [
                    {
                        id: 'row-2',
                        type: 'row',
                        settings: { gap: '16px' },
                        children: [
                            {
                                id: 'col-2',
                                type: 'column',
                                settings: { width: 12 },
                                children: [
                                    {
                                        id: 'blk-text',
                                        type: 'block',
                                        settings: { blockType: 'text' },
                                        data: {
                                            heading: 'About Our Service',
                                            headingLevel: 2,
                                            body: '<p>We provide excellent services that help businesses grow. Our team of experts is dedicated to delivering quality results.</p>',
                                        },
                                    },
                                    {
                                        id: 'blk-image',
                                        type: 'block',
                                        settings: { blockType: 'image' },
                                        data: {
                                            src: '/about.jpg',
                                            alt: 'Team photo',
                                            caption: 'Our dedicated team',
                                            link: 'https://example.com',
                                        },
                                    },
                                ],
                            },
                        ],
                    },
                ],
            },
            {
                id: 'sec-3',
                type: 'section',
                settings: {},
                children: [
                    {
                        id: 'row-3',
                        type: 'row',
                        settings: { gap: '16px' },
                        children: [
                            {
                                id: 'col-3',
                                type: 'column',
                                settings: { width: 12 },
                                children: [
                                    {
                                        id: 'blk-cards',
                                        type: 'block',
                                        settings: { blockType: 'cards' },
                                        data: {
                                            items: [
                                                {
                                                    title: 'Card One',
                                                    description: 'First card description',
                                                    link: '/card-one',
                                                },
                                                {
                                                    title: 'Card Two',
                                                    description: 'Second card description',
                                                    link: 'https://external.com',
                                                },
                                            ],
                                        },
                                    },
                                    {
                                        id: 'blk-cta',
                                        type: 'block',
                                        settings: { blockType: 'cta' },
                                        data: {
                                            heading: 'Ready to Start?',
                                            body: '<p>Join thousands of happy customers today.</p>',
                                            buttonText: 'Sign Up Now',
                                            buttonLink: '/register',
                                        },
                                    },
                                    {
                                        id: 'blk-faq',
                                        type: 'block',
                                        settings: { blockType: 'faq' },
                                        data: {
                                            items: [
                                                {
                                                    question: 'What is this service?',
                                                    answer: '<p>It is a great service for everyone.</p>',
                                                },
                                                {
                                                    question: 'How much does it cost?',
                                                    answer: '<p>Plans start at $10/month.</p>',
                                                },
                                            ],
                                        },
                                    },
                                ],
                            },
                        ],
                    },
                ],
            },
        ],
    };
}
describe('stripHtml', () => {
    it('removes HTML tags from a string', () => {
        expect(stripHtml('<p>Hello <strong>world</strong></p>')).toBe('Hello world');
    });
    it('handles empty string', () => {
        expect(stripHtml('')).toBe('');
    });
});
describe('countWords', () => {
    it('counts words in a sentence', () => {
        expect(countWords('hello world foo bar')).toBe(4);
    });
    it('returns 0 for empty string', () => {
        expect(countWords('')).toBe(0);
    });
});
describe('countSyllables', () => {
    it('counts syllables in simple words', () => {
        expect(countSyllables('cat')).toBe(1);
        expect(countSyllables('hello')).toBe(2);
        expect(countSyllables('beautiful')).toBe(3);
    });
});
describe('extractContent', () => {
    it('pulls text from hero, text, image, cards, cta, faq blocks', () => {
        const tree = makeTestPage();
        const content = extractContent(tree);
        expect(content.plainText).toContain('Welcome to Our Service');
        expect(content.plainText).toContain('About Our Service');
        expect(content.plainText).toContain('Team photo');
        expect(content.plainText).toContain('Card One');
        expect(content.plainText).toContain('Sign Up Now');
        expect(content.plainText).toContain('What is this service?');
    });
    it('collects headings with correct levels', () => {
        const tree = makeTestPage();
        const content = extractContent(tree);
        const h1 = content.headings.find((h) => h.level === 1);
        expect(h1).toBeDefined();
        expect(h1.text).toBe('Welcome to Our Service');
        expect(h1.blockId).toBe('blk-hero');
        const h2 = content.headings.find((h) => h.level === 2);
        expect(h2).toBeDefined();
        expect(h2.text).toBe('About Our Service');
        expect(h2.blockId).toBe('blk-text');
    });
    it('collects images with src and alt', () => {
        const tree = makeTestPage();
        const content = extractContent(tree);
        expect(content.images.length).toBeGreaterThanOrEqual(2);
        const heroImage = content.images.find((i) => i.blockId === 'blk-hero');
        expect(heroImage).toBeDefined();
        expect(heroImage.src).toBe('/hero.jpg');
        expect(heroImage.alt).toBe('Hero banner showing our service');
        const aboutImage = content.images.find((i) => i.blockId === 'blk-image');
        expect(aboutImage).toBeDefined();
        expect(aboutImage.src).toBe('/about.jpg');
        expect(aboutImage.alt).toBe('Team photo');
    });
    it('collects internal and external links', () => {
        const tree = makeTestPage();
        const content = extractContent(tree);
        const internalLinks = content.links.filter((l) => l.internal);
        const externalLinks = content.links.filter((l) => !l.internal);
        expect(internalLinks.length).toBeGreaterThanOrEqual(1);
        expect(externalLinks.length).toBeGreaterThanOrEqual(1);
    });
    it('returns correct word count', () => {
        const tree = makeTestPage();
        const content = extractContent(tree);
        expect(content.wordCount).toBeGreaterThan(0);
    });
    it('builds blockTexts map', () => {
        const tree = makeTestPage();
        const content = extractContent(tree);
        expect(content.blockTexts.size).toBeGreaterThan(0);
        expect(content.blockTexts.has('blk-hero')).toBe(true);
    });
});
describe('analyzeReadability', () => {
    it('returns valid Flesch score between 0 and 100', () => {
        const text = 'The cat sat on the mat. It was a sunny day. Birds were singing in the trees. Children played in the park. Everyone was happy and content.';
        const result = analyzeReadability(text);
        expect(result.fleschScore).toBeGreaterThanOrEqual(0);
        expect(result.fleschScore).toBeLessThanOrEqual(100);
    });
    it('returns 0 for empty text', () => {
        const result = analyzeReadability('');
        expect(result.fleschScore).toBe(0);
        expect(result.wordCount).toBe(0);
    });
    it('calculates reading time', () => {
        const words = Array(400).fill('word').join(' ');
        const result = analyzeReadability(words + '.');
        expect(result.readingTime).toBe(2);
    });
    it('estimates passive voice percentage', () => {
        const text = 'The ball was kicked by the boy. The window was broken by the storm. The cat sat on the mat.';
        const result = analyzeReadability(text);
        expect(result.passiveEstimate).toBeGreaterThan(0);
    });
});
describe('analyzeSEO', () => {
    const defaultSettings = {
        title: 'Our Amazing Service',
        slug: 'our-amazing-service',
        metaTitle: 'Our Amazing Service - Best in Class',
        metaDescription: 'Discover our amazing service that helps businesses grow. Join thousands of satisfied customers and transform your workflow with our proven solution.',
        focusKeyphrase: 'amazing service',
        ogImage: '/og-image.jpg',
        schemaType: 'WebPage',
    };
    it('returns score between 0 and 100', () => {
        const tree = makeTestPage();
        const result = analyzeSEO(tree, defaultSettings);
        expect(result.score).toBeGreaterThanOrEqual(0);
        expect(result.score).toBeLessThanOrEqual(100);
    });
    it('flags missing meta title', () => {
        const tree = makeTestPage();
        const settings = { ...defaultSettings, metaTitle: '', title: '' };
        const result = analyzeSEO(tree, settings);
        const titleCheck = result.checks.find((c) => c.id === 'meta-title-exists');
        expect(titleCheck).toBeDefined();
        expect(titleCheck.status).toBe('error');
    });
    it('flags missing alt text per block', () => {
        const tree = {
            id: 'page-1',
            type: 'page',
            children: [
                {
                    id: 'sec-1',
                    type: 'section',
                    settings: {},
                    children: [
                        {
                            id: 'row-1',
                            type: 'row',
                            settings: { gap: '16px' },
                            children: [
                                {
                                    id: 'col-1',
                                    type: 'column',
                                    settings: { width: 12 },
                                    children: [
                                        {
                                            id: 'blk-img-no-alt',
                                            type: 'block',
                                            settings: { blockType: 'image' },
                                            data: { src: '/photo.jpg', alt: '' },
                                        },
                                    ],
                                },
                            ],
                        },
                    ],
                },
            ],
        };
        const result = analyzeSEO(tree, defaultSettings);
        const altCheck = result.checks.find((c) => c.id === 'images-alt');
        expect(altCheck).toBeDefined();
        expect(altCheck.status).toBe('error');
        const blockHints = result.perBlockHints.get('blk-img-no-alt');
        expect(blockHints).toBeDefined();
        expect(blockHints.some((h) => h.id === 'block-missing-alt')).toBe(true);
    });
    it('passes with complete SEO data', () => {
        const tree = makeTestPage();
        const result = analyzeSEO(tree, defaultSettings);
        expect(result.score).toBeGreaterThan(50);
        expect(result.checks.length).toBeGreaterThan(0);
        expect(result.readability.wordCount).toBeGreaterThan(0);
    });
    it('includes readability and content in results', () => {
        const tree = makeTestPage();
        const result = analyzeSEO(tree, defaultSettings);
        expect(result.readability).toBeDefined();
        expect(result.readability.fleschScore).toBeGreaterThanOrEqual(0);
        expect(result.content).toBeDefined();
        expect(result.content.wordCount).toBeGreaterThan(0);
    });
});
//# sourceMappingURL=seo-analyzer.test.js.map