import { describe, it, expect } from 'vitest';
import { validateTitleTemplate, applySiteTitleTemplate, DEFAULT_SITE_TITLE_TEMPLATE, } from '../../seo/title-templates.js';
import { buildVerificationMetaEntries, renderVerificationMetaHtml, buildNextVerification, } from '../../seo/meta-tags.js';
import { mergeSiteSeoConfig } from '../../seo/config-store.js';
import { composePageMeta } from '../../seo/page-meta.js';
describe('validateTitleTemplate', () => {
    it('accepts a template with known tokens', () => {
        const r = validateTitleTemplate('%page% — %site%');
        expect(r.valid).toBe(true);
        expect(r.unknownTokens).toEqual([]);
    });
    it('treats an empty template as valid (falls back to default)', () => {
        expect(validateTitleTemplate('').valid).toBe(true);
        expect(validateTitleTemplate('   ').valid).toBe(true);
    });
    it('rejects unsupported tokens', () => {
        const r = validateTitleTemplate('%page% %bogus%');
        expect(r.valid).toBe(false);
        expect(r.unknownTokens).toContain('bogus');
        expect(r.errors[0]).toMatch(/%bogus%/);
    });
    it('accepts literal-only text with no tokens', () => {
        expect(validateTitleTemplate('My Site').valid).toBe(true);
    });
    it('rejects a template that is only an unknown token', () => {
        expect(validateTitleTemplate('%nope%').valid).toBe(false);
    });
});
describe('applySiteTitleTemplate', () => {
    it('renders the standard page/site template', () => {
        expect(applySiteTitleTemplate('%page% — %site%', { page: 'About', site: 'Acme' })).toBe('About — Acme');
    });
    it('falls back to the default template when blank', () => {
        expect(applySiteTitleTemplate('', { page: 'About', site: 'Acme' })).toBe('About — Acme');
        expect(DEFAULT_SITE_TITLE_TEMPLATE).toBe('%page% — %site%');
    });
    it('collapses dangling separators left by empty tokens', () => {
        expect(applySiteTitleTemplate('%page% — %site%', { page: 'About' })).toBe('About');
        expect(applySiteTitleTemplate('%page% — %site%', { site: 'Acme' })).toBe('Acme');
    });
    it('supports the %type%, %author% and %separator% tokens', () => {
        expect(applySiteTitleTemplate('%page% %separator% %type% %separator% %site%', {
            page: 'Hello',
            type: 'Post',
            site: 'Acme',
            separator: '·',
        })).toBe('Hello · Post · Acme');
    });
    it('falls back to the page title when nothing renders', () => {
        expect(applySiteTitleTemplate('%site%', { page: 'Only Page' })).toBe('Only Page');
    });
});
describe('mergeSiteSeoConfig', () => {
    it('overrides scalar defaults and deep-merges verification', () => {
        const base = {
            siteName: 'Base',
            titleTemplate: '%page%',
            verification: { googleSearchConsole: 'g-base', bingWebmasterTools: 'b-base' },
        };
        const merged = mergeSiteSeoConfig(base, {
            titleTemplate: '%page% — %site%',
            verification: { bingWebmasterTools: 'b-new' },
        });
        expect(merged.siteName).toBe('Base');
        expect(merged.titleTemplate).toBe('%page% — %site%');
        // google preserved from base, bing overridden
        expect(merged.verification?.googleSearchConsole).toBe('g-base');
        expect(merged.verification?.bingWebmasterTools).toBe('b-new');
    });
    it('drops empty-string overrides so they fall back to the base', () => {
        const merged = mergeSiteSeoConfig({ defaultMetaDescription: 'Base desc' }, { defaultMetaDescription: '' });
        expect(merged.defaultMetaDescription).toBe('Base desc');
    });
});
describe('verification meta tags', () => {
    const tokens = {
        googleSearchConsole: 'goog-token',
        bingWebmasterTools: 'bing-token',
        yandex: 'yndx-token',
        pinterest: 'pin-token',
        facebookDomain: 'fb-token',
    };
    it('builds the standard provider meta entries', () => {
        const entries = buildVerificationMetaEntries(tokens);
        const byName = Object.fromEntries(entries.map((e) => [e.name, e.content]));
        expect(byName['google-site-verification']).toBe('goog-token');
        expect(byName['msvalidate.01']).toBe('bing-token');
        expect(byName['yandex-verification']).toBe('yndx-token');
        expect(byName['p:domain_verify']).toBe('pin-token');
        expect(byName['facebook-domain-verification']).toBe('fb-token');
    });
    it('skips empty/whitespace tokens', () => {
        expect(buildVerificationMetaEntries({ googleSearchConsole: '   ' })).toEqual([]);
        expect(buildVerificationMetaEntries(null)).toEqual([]);
    });
    it('renders only safe escaped meta tags (no script injection)', () => {
        const html = renderVerificationMetaHtml({
            googleSearchConsole: '"><script>alert(1)</script>',
        });
        expect(html).not.toContain('<script>');
        expect(html).toContain('&lt;script&gt;');
        expect(html.startsWith('<meta name="google-site-verification"')).toBe(true);
    });
    it('renders custom tags but requires content + name/property', () => {
        const entries = buildVerificationMetaEntries({
            customMetaTags: [
                { id: '1', name: 'custom-verify', content: 'abc' },
                { id: '2', property: 'og:custom', content: 'xyz' },
                { id: '3', content: 'no-name' }, // dropped
                { id: '4', name: 'empty' }, // dropped (no content)
            ],
        });
        expect(entries).toHaveLength(2);
        expect(entries[0]).toEqual({ name: 'custom-verify', content: 'abc' });
        expect(entries[1]).toEqual({ property: 'og:custom', content: 'xyz' });
    });
    it('maps google/yandex to first-class Next keys and the rest under other', () => {
        const v = buildNextVerification(tokens);
        expect(v?.google).toBe('goog-token');
        expect(v?.yandex).toBe('yndx-token');
        expect(v?.other?.['msvalidate.01']).toBe('bing-token');
        expect(v?.other?.['p:domain_verify']).toBe('pin-token');
        expect(buildNextVerification(null)).toBeUndefined();
    });
});
describe('composePageMeta — site defaults', () => {
    const baseConfig = {
        seo: {
            siteName: 'Acme',
            siteUrl: 'https://acme.test',
            titleTemplate: '%page% — %site%',
            defaultMetaDescription: 'The Acme default description.',
            verification: { googleSearchConsole: 'goog-token' },
        },
    };
    it('applies the title template when no explicit SEO title is set', () => {
        const meta = composePageMeta({
            doc: { id: '1', collection: 'pages', slug: 'about', data: { title: 'About Us' } },
            collection: {
                slug: 'pages',
                labels: { singular: 'Page', plural: 'Pages' },
                fields: [],
            },
            config: baseConfig,
        });
        expect(meta.title).toBe('About Us — Acme');
    });
    it('uses the explicit metaTitle over the template', () => {
        const meta = composePageMeta({
            doc: {
                id: '1',
                collection: 'pages',
                slug: 'about',
                data: { title: 'About Us', metaTitle: 'Custom Title' },
            },
            config: baseConfig,
        });
        expect(meta.title).toBe('Custom Title');
    });
    it('falls back to the default meta description', () => {
        const meta = composePageMeta({
            doc: { id: '1', collection: 'pages', slug: 'about', data: { title: 'About Us' } },
            config: baseConfig,
        });
        expect(meta.description).toBe('The Acme default description.');
    });
    it('prefers a per-document description over the default', () => {
        const meta = composePageMeta({
            doc: {
                id: '1',
                collection: 'pages',
                slug: 'about',
                data: { title: 'About Us', metaDescription: 'Page-specific.' },
            },
            config: baseConfig,
        });
        expect(meta.description).toBe('Page-specific.');
    });
    it('injects verification meta into the rendered output and Next metadata', () => {
        const meta = composePageMeta({
            doc: { id: '1', collection: 'pages', slug: 'about', data: { title: 'About Us' } },
            config: baseConfig,
        });
        expect(meta.meta['google-site-verification']).toBe('goog-token');
        expect(meta.metaHtml).toContain('name="google-site-verification"');
        expect(meta.metadata.verification?.google).toBe('goog-token');
    });
});
//# sourceMappingURL=settings-defaults.test.js.map