import { describe, it, expect, vi, beforeEach } from 'vitest';
import { suggestAiFix, pendingFixFromMetadata, pendingFixToMetadata, } from '../../seo/issue-fix-ai.js';
const generateTextForFeatureMock = vi.fn();
vi.mock('../../ai/index.js', () => ({
    generateTextForFeature: (...args) => generateTextForFeatureMock(...args),
}));
vi.mock('../../security/audit.js', () => ({
    logEvent: vi.fn(async () => { }),
}));
vi.mock('../../config/runtime.js', () => ({
    getActuateConfig: () => ({
        collections: { posts: { seo: { defaultSchemaType: 'BlogPosting' } } },
    }),
}));
const entity = {
    entityType: 'post',
    entityId: 'doc-1',
    title: 'How to Contact Support',
    url: '/support',
    content: '<p>Reach our team by email or chat for help with billing and technical issues.</p>',
    metaTitle: 'Duplicate Title Here',
    metaDescription: 'Same description on two pages about support.',
    canonicalUrl: 'https://example.com/support',
    noindex: false,
};
const ctx = {
    entity,
    siteSeo: { siteName: 'Example', siteUrl: 'https://example.com' },
    updatedAt: '2024-06-27T10:00:00.000Z',
};
const deps = { db: {}, userId: 'user-1', collection: 'posts' };
describe('suggestAiFix', () => {
    beforeEach(() => {
        generateTextForFeatureMock.mockReset();
    });
    it('generates a unique meta title for duplicate-meta-title', async () => {
        generateTextForFeatureMock.mockResolvedValue({
            ok: true,
            text: 'Contact UpChat Support — Help & Billing',
        });
        const result = await suggestAiFix({ type: 'duplicate-meta-title', title: 'Duplicate', entityType: 'post', entityId: 'doc-1' }, ctx, deps);
        expect(result.ok).toBe(true);
        if (!result.ok)
            return;
        expect(result.suggestion.autoFixable).toBe(true);
        expect(result.suggestion.source).toBe('ai');
        expect(result.suggestion.patch.metaTitle).toBe('Contact UpChat Support — Help & Billing');
    });
    it('returns aiUnavailable when provider is not configured', async () => {
        generateTextForFeatureMock.mockResolvedValue({
            ok: false,
            code: 'no_provider',
            message: 'none',
        });
        const result = await suggestAiFix({ type: 'missing-meta-title', title: 'Missing', entityType: 'post', entityId: 'doc-1' }, ctx, deps);
        expect(result.ok).toBe(true);
        if (!result.ok)
            return;
        expect(result.suggestion.aiUnavailable).toBe(true);
        expect(result.suggestion.autoFixable).toBe(false);
    });
    it('parses JSON-LD for missing-structured-data', async () => {
        generateTextForFeatureMock.mockResolvedValue({
            ok: true,
            text: '{"@type":"BlogPosting","headline":"Support","description":"Help page"}',
        });
        const result = await suggestAiFix({
            type: 'missing-structured-data',
            title: 'No schema',
            entityType: 'post',
            entityId: 'doc-1',
        }, ctx, deps);
        expect(result.ok).toBe(true);
        if (!result.ok)
            return;
        expect(result.suggestion.patch.structuredDataType).toBe('BlogPosting');
        expect(result.suggestion.patch.structuredData).toMatchObject({ '@type': 'BlogPosting' });
    });
});
describe('pendingFix metadata', () => {
    it('round-trips through metadata helpers', () => {
        const suggestion = {
            autoFixable: true,
            source: 'ai',
            fingerprint: 'doc-1:1:duplicate-meta-title',
            patch: { metaTitle: 'Unique Title' },
            changes: [],
            justification: 'AI fix',
        };
        const meta = pendingFixToMetadata(suggestion);
        const cached = pendingFixFromMetadata(meta);
        expect(cached?.fingerprint).toBe(suggestion.fingerprint);
        expect(cached?.patch).toEqual(suggestion.patch);
    });
});
//# sourceMappingURL=issue-fix-ai.test.js.map