import { describe, it, expect } from 'vitest';
import { suggestDeterministicFix, truncateAtWordBoundary, excerptForMetaDescription, buildFixFingerprint, META_TITLE_MAX, META_DESC_MAX, } from '../../seo/issue-fix.js';
const baseEntity = {
    entityType: 'page',
    entityId: 'doc-1',
    title: 'How to Contact UpChat Support',
    url: '/support',
    content: '<p>Reach our team by email or chat. We respond within one business day and offer priority support on paid plans.</p>' +
        ' '.repeat(50),
    metaTitle: null,
    metaDescription: null,
    canonicalUrl: null,
    noindex: false,
};
const siteSeo = {
    siteName: 'UpChat',
    siteUrl: 'https://upchat.example',
    titleTemplate: '%page% — %site%',
};
describe('truncateAtWordBoundary', () => {
    it('returns short text unchanged', () => {
        expect(truncateAtWordBoundary('Hello world', 60)).toBe('Hello world');
    });
    it('truncates long text at a word boundary', () => {
        const long = 'word '.repeat(30).trim();
        const out = truncateAtWordBoundary(long, 40);
        expect(out.length).toBeLessThanOrEqual(40);
        expect(out.endsWith('word')).toBe(true);
    });
});
describe('excerptForMetaDescription', () => {
    it('strips HTML and caps length', () => {
        const out = excerptForMetaDescription(baseEntity.content ?? '', META_DESC_MAX);
        expect(out).not.toContain('<');
        expect(out.length).toBeLessThanOrEqual(META_DESC_MAX);
        expect(out.length).toBeGreaterThan(20);
    });
});
describe('suggestDeterministicFix', () => {
    const ctx = { entity: baseEntity, siteSeo, updatedAt: '2024-06-27T10:00:00.000Z' };
    it('suggests a meta title for missing-meta-title', () => {
        const s = suggestDeterministicFix({ type: 'missing-meta-title', title: 'Missing', entityId: 'doc-1', entityType: 'page' }, ctx);
        expect(s.autoFixable).toBe(true);
        expect(s.patch.metaTitle).toBe('How to Contact UpChat Support — UpChat');
        expect(s.changes[0]?.field).toBe('metaTitle');
    });
    it('truncates an over-long meta title', () => {
        const longTitle = 'A'.repeat(80);
        const s = suggestDeterministicFix({ type: 'meta-title-length', title: 'Too long', entityType: 'page', entityId: 'doc-1' }, {
            ...ctx,
            entity: { ...baseEntity, metaTitle: longTitle },
        });
        expect(s.autoFixable).toBe(true);
        expect(String(s.patch.metaTitle).length).toBeLessThanOrEqual(META_TITLE_MAX);
    });
    it('clears accidental noindex', () => {
        const s = suggestDeterministicFix({ type: 'accidental-noindex', title: 'Noindex', entityType: 'page', entityId: 'doc-1' }, { ...ctx, entity: { ...baseEntity, noindex: true } });
        expect(s.autoFixable).toBe(true);
        expect(s.patch).toEqual({ noindex: false });
    });
    it('drafts meta description from content when missing', () => {
        const s = suggestDeterministicFix({
            type: 'missing-meta-description',
            title: 'Missing desc',
            entityType: 'page',
            entityId: 'doc-1',
        }, ctx);
        expect(s.autoFixable).toBe(true);
        expect(String(s.patch.metaDescription).length).toBeGreaterThan(0);
    });
    it('truncates an over-long meta description', () => {
        const long = 'x'.repeat(200);
        const s = suggestDeterministicFix({
            type: 'meta-description-length',
            title: 'Long desc',
            entityType: 'page',
            entityId: 'doc-1',
        }, { ...ctx, entity: { ...baseEntity, metaDescription: long } });
        expect(s.autoFixable).toBe(true);
        expect(String(s.patch.metaDescription).length).toBeLessThanOrEqual(META_DESC_MAX);
    });
    it('sets canonical when site URL is configured', () => {
        const s = suggestDeterministicFix({ type: 'missing-canonical', title: 'Canonical', entityType: 'page', entityId: 'doc-1' }, ctx);
        expect(s.autoFixable).toBe(true);
        expect(s.patch.canonical).toBe('https://upchat.example/support');
    });
    it('returns manual-only for unsupported issue types', () => {
        const s = suggestDeterministicFix({ type: 'thin-content', title: 'Thin', entityType: 'page', entityId: 'doc-1' }, ctx);
        expect(s.autoFixable).toBe(false);
        expect(Object.keys(s.patch)).toHaveLength(0);
    });
    it('buildFixFingerprint changes when document updates', () => {
        const a = buildFixFingerprint('doc-1', 'missing-meta-title', '2024-01-01T00:00:00.000Z');
        const b = buildFixFingerprint('doc-1', 'missing-meta-title', '2024-06-01T00:00:00.000Z');
        expect(a).not.toBe(b);
    });
});
//# sourceMappingURL=issue-fix.test.js.map