import { afterEach, describe, expect, it } from 'vitest';
import { computeSiteIndexStatus, getSiteIndexStatus, robotsTxtBlocksEntireSite, } from '../../seo/index-status.js';
import { DEFAULT_SEO_MODULE_SETTINGS, SEO_CONFIG_COLLECTION } from '../../seo/config-store.js';
import { setActuateConfig } from '../../config/runtime.js';
/** Minimal config stub — index-status only reads `seo`. */
function stubConfig(seo) {
    setActuateConfig({ collections: {}, ...(seo ? { seo } : {}) });
}
function inputs(overrides = {}) {
    return {
        environment: 'production',
        seo: { siteUrl: 'https://example.com' },
        module: { ...DEFAULT_SEO_MODULE_SETTINGS },
        hasSeoConfig: true,
        lastAuditAt: null,
        ...overrides,
    };
}
describe('robotsTxtBlocksEntireSite', () => {
    it('detects a blanket Disallow: /', () => {
        expect(robotsTxtBlocksEntireSite('User-agent: *\nDisallow: /')).toBe(true);
        expect(robotsTxtBlocksEntireSite('User-agent: *\nDisallow: /  # block all')).toBe(true);
    });
    it('does not flag scoped disallow rules', () => {
        expect(robotsTxtBlocksEntireSite('User-agent: *\nDisallow: /admin\nDisallow: /api/')).toBe(false);
        expect(robotsTxtBlocksEntireSite('')).toBe(false);
    });
});
describe('computeSiteIndexStatus', () => {
    it('reports indexable in production with a site URL and no blocks', () => {
        const result = computeSiteIndexStatus(inputs());
        expect(result.status).toBe('indexable');
        expect(result.severity).toBe('success');
        expect(result.label).toBe('Indexable');
    });
    it('reports blocked in production when global noindex is on', () => {
        const result = computeSiteIndexStatus(inputs({ seo: { siteUrl: 'https://example.com', robots: { defaultNoIndex: true } } }));
        expect(result.status).toBe('blocked');
        expect(result.severity).toBe('danger');
        expect(result.checks.find((c) => c.key === 'globalNoindex')?.status).toBe('fail');
    });
    it('reports blocked in production when robots.txt disallows everything', () => {
        const result = computeSiteIndexStatus(inputs({
            module: { ...DEFAULT_SEO_MODULE_SETTINGS, robotsTxt: 'User-agent: *\nDisallow: /' },
        }));
        expect(result.status).toBe('blocked');
        expect(result.severity).toBe('danger');
    });
    it('reports blocked in production when the robots route is disabled', () => {
        const result = computeSiteIndexStatus(inputs({ module: { ...DEFAULT_SEO_MODULE_SETTINGS, robotsEnabled: false } }));
        expect(result.status).toBe('blocked');
    });
    it('reports protected when non-production noindex guards a preview env', () => {
        const result = computeSiteIndexStatus(inputs({
            environment: 'preview',
            seo: { siteUrl: 'https://preview.example.com', robots: { noIndexNonProduction: true } },
        }));
        expect(result.status).toBe('protected');
        expect(result.severity).toBe('warning');
        expect(result.label).toBe('Protected');
    });
    it('warns (partially blocked) when a non-production env is NOT protected', () => {
        const result = computeSiteIndexStatus(inputs({ environment: 'preview', seo: { siteUrl: 'https://preview.example.com' } }));
        expect(result.status).toBe('partially_blocked');
        expect(result.severity).toBe('warning');
        expect(result.checks.find((c) => c.key === 'nonProdNoindex')?.status).toBe('warning');
    });
    it('flags a missing site URL as needs-attention', () => {
        const result = computeSiteIndexStatus(inputs({ seo: { siteUrl: '' } }));
        expect(result.status).toBe('unknown');
        expect(result.severity).toBe('warning');
        expect(result.checks.find((c) => c.key === 'siteUrl')?.status).toBe('warning');
    });
    it('reports partially blocked when the sitemap is disabled but the site is indexable', () => {
        const result = computeSiteIndexStatus(inputs({ module: { ...DEFAULT_SEO_MODULE_SETTINGS, sitemapEnabled: false } }));
        expect(result.status).toBe('partially_blocked');
        expect(result.checks.find((c) => c.key === 'sitemap')?.status).toBe('warning');
    });
    it('reports unknown when no SEO config exists at all', () => {
        const result = computeSiteIndexStatus(inputs({ hasSeoConfig: false, seo: null }));
        expect(result.status).toBe('unknown');
        expect(result.severity).toBe('neutral');
    });
    it('always exposes the three SEO actions and a full set of checks', () => {
        const result = computeSiteIndexStatus(inputs());
        expect(result.actions.map((a) => a.label)).toEqual([
            'Run SEO Audit',
            'View robots.txt',
            'View sitemap.xml',
        ]);
        expect(result.checks.map((c) => c.key)).toEqual([
            'environment',
            'siteUrl',
            'robotsBlockAll',
            'globalNoindex',
            'nonProdNoindex',
            'sitemap',
            'lastAudit',
        ]);
    });
    it('surfaces the last audit date', () => {
        const result = computeSiteIndexStatus(inputs({ lastAuditAt: '2026-01-02T03:04:05.000Z' }));
        expect(result.checks.find((c) => c.key === 'lastAudit')?.value).toBe('2026-01-02');
        expect(result.checks.find((c) => c.key === 'lastAudit')?.status).toBe('pass');
    });
});
describe('getSiteIndexStatus', () => {
    const origVercel = process.env.VERCEL_ENV;
    const origNode = process.env.NODE_ENV;
    afterEach(() => {
        process.env.VERCEL_ENV = origVercel;
        process.env.NODE_ENV = origNode;
        stubConfig();
    });
    it('reads effective config + last audit from the DB', async () => {
        process.env.VERCEL_ENV = 'production';
        stubConfig({ siteUrl: 'https://example.com' });
        const db = {
            document: {
                findFirst: async ({ where }) => where.collection === SEO_CONFIG_COLLECTION ? { data: { site: {}, module: {} } } : null,
            },
            seoAuditRun: {
                findFirst: async () => ({ finishedAt: new Date('2026-02-03T00:00:00.000Z') }),
            },
        };
        const result = await getSiteIndexStatus(db);
        expect(result.status).toBe('indexable');
        expect(result.environment).toBe('production');
        expect(result.lastAuditAt).toBe('2026-02-03T00:00:00.000Z');
    });
    it('tolerates a missing audit table', async () => {
        process.env.VERCEL_ENV = 'production';
        stubConfig({ siteUrl: 'https://example.com' });
        const db = { document: { findFirst: async () => null } };
        const result = await getSiteIndexStatus(db);
        expect(result.lastAuditAt).toBeNull();
        expect(result.status).toBe('indexable');
    });
});
//# sourceMappingURL=index-status.test.js.map