import { describe, it, expect, vi, beforeEach } from 'vitest';
import { generateContentBrief, supportsContentBrief, contentBriefFromMetadata, renderContentBriefMarkdown, CONTENT_BRIEF_ISSUE_TYPES, } from '../../seo/issue-content-brief.js';
const generateTextForFeatureMock = vi.fn();
vi.mock('../../ai/index.js', () => ({
    generateTextForFeature: (...args) => generateTextForFeatureMock(...args),
}));
vi.mock('../../security/audit.js', () => ({
    logEvent: vi.fn(async () => { }),
}));
const entity = {
    entityType: 'page',
    entityId: 'page-1',
    title: 'Contact Support',
    url: '/support',
    content: '<p>Email us for help.</p>',
    wordCount: 180,
};
const ctx = { entity, updatedAt: '2024-06-27T10:00:00.000Z' };
const deps = { db: {}, userId: 'user-1', collection: 'pages' };
describe('supportsContentBrief', () => {
    it('includes thin content and heading issues', () => {
        expect(CONTENT_BRIEF_ISSUE_TYPES.has('thin-content')).toBe(true);
        expect(supportsContentBrief('missing-h1')).toBe(true);
        expect(supportsContentBrief('broken-internal-link')).toBe(false);
    });
});
describe('generateContentBrief', () => {
    beforeEach(() => {
        generateTextForFeatureMock.mockReset();
    });
    it('parses structured JSON from the model', async () => {
        generateTextForFeatureMock.mockResolvedValue({
            ok: true,
            text: JSON.stringify({
                summary: 'Expand the page with support channels and response times.',
                suggestedH1: 'How to Contact UpChat Support',
                targetWordCount: 450,
                topics: ['Email support', 'Live chat hours'],
                sections: [{ heading: 'Support channels', bullets: ['Email', 'Chat'] }],
                keyTakeawayDraft: 'Contact UpChat support via email or live chat on business days.',
                geoNote: null,
            }),
        });
        const result = await generateContentBrief({
            id: 'issue-1',
            type: 'thin-content',
            title: 'Content below 300 words',
            entityId: 'page-1',
        }, ctx, deps);
        expect(result.ok).toBe(true);
        if (!result.ok)
            return;
        expect(result.brief.suggestedH1).toBe('How to Contact UpChat Support');
        expect(result.brief.topics).toHaveLength(2);
        expect(result.brief.fingerprint).toContain('page-1');
    });
    it('returns unavailable when AI is not configured', async () => {
        generateTextForFeatureMock.mockResolvedValue({
            ok: false,
            code: 'no_provider',
            message: 'none',
        });
        const result = await generateContentBrief({ id: 'i1', type: 'missing-h1', title: 'Missing H1', entityId: 'page-1' }, ctx, deps);
        expect(result.ok).toBe(false);
        if (result.ok)
            return;
        expect(result.code).toBe('unavailable');
    });
});
describe('contentBriefFromMetadata', () => {
    it('returns null when fingerprint is stale', () => {
        const brief = {
            fingerprint: 'page-1:1:thin-content',
            generatedAt: '2024-01-01T00:00:00.000Z',
            summary: 'Old brief',
            suggestedH1: null,
            targetWordCount: null,
            topics: [],
            sections: [],
            keyTakeawayDraft: null,
            geoNote: null,
        };
        const loaded = contentBriefFromMetadata({ contentBrief: brief }, 'thin-content', 'page-1', ctx.updatedAt);
        expect(loaded).toBeNull();
    });
});
describe('renderContentBriefMarkdown', () => {
    it('formats brief sections for export', () => {
        const md = renderContentBriefMarkdown({
            issueType: 'thin-content',
            entityId: 'page-1',
            fingerprint: 'x',
            generatedAt: '2024-01-01T00:00:00.000Z',
            summary: 'Expand support content.',
            suggestedH1: 'Contact Support',
            targetWordCount: 400,
            topics: ['Hours', 'Channels'],
            sections: [],
            keyTakeawayDraft: 'Reach us by email.',
            geoNote: null,
        });
        expect(md).toContain('Suggested H1');
        expect(md).toContain('Contact Support');
        expect(md).toContain('Reach us by email.');
    });
});
//# sourceMappingURL=issue-content-brief.test.js.map