import { describe, expect, it, vi, beforeEach } from 'vitest';
import { setActuateConfig } from '../../config/runtime.js';
import { migrateStructuredDataFields } from '../../seo/migrate-schema.js';
const { updateDocumentSeoFieldsMock } = vi.hoisted(() => ({
    updateDocumentSeoFieldsMock: vi.fn(async () => ({})),
}));
vi.mock('../../actions.js', async (importOriginal) => {
    const actual = await importOriginal();
    return {
        ...actual,
        updateDocumentSeoFields: updateDocumentSeoFieldsMock,
    };
});
function ctxFor(db) {
    return { userId: 'admin-1', role: 'ADMIN', db, locale: 'en' };
}
describe('migrateStructuredDataFields', () => {
    beforeEach(() => {
        updateDocumentSeoFieldsMock.mockClear();
        setActuateConfig({
            collections: {
                pages: { slug: 'pages', type: 'page', fields: [] },
            },
        });
    });
    it('stamps schema types on published pages missing structuredDataType', async () => {
        const update = vi.fn(async () => ({}));
        const db = {
            document: {
                findMany: vi
                    .fn()
                    .mockResolvedValueOnce([
                    {
                        id: 'p1',
                        collection: 'pages',
                        slug: 'home',
                        status: 'PUBLISHED',
                        data: { title: 'Home', slug: 'home' },
                        structuredData: null,
                        publishedAt: new Date(),
                        updatedAt: new Date(),
                    },
                ])
                    .mockResolvedValueOnce([]),
                update,
            },
        };
        const result = await migrateStructuredDataFields(ctxFor(db), { batchSize: 10 });
        expect(result.scanned).toBe(1);
        expect(result.migrated).toBe(1);
        expect(updateDocumentSeoFieldsMock).toHaveBeenCalledWith('pages', 'p1', expect.objectContaining({ structuredDataType: 'WebPage', schemaType: 'WebPage' }), expect.any(Object));
    });
    it('skips documents that already have schema type and JSON-LD', async () => {
        const db = {
            document: {
                findMany: vi.fn().mockResolvedValueOnce([
                    {
                        id: 'p1',
                        collection: 'pages',
                        slug: 'home',
                        status: 'PUBLISHED',
                        data: { title: 'Home', structuredDataType: 'WebPage', schemaType: 'WebPage' },
                        structuredData: { '@context': 'https://schema.org', '@type': 'WebPage' },
                        publishedAt: new Date(),
                        updatedAt: new Date(),
                    },
                ]),
            },
        };
        const result = await migrateStructuredDataFields(ctxFor(db));
        expect(result.migrated).toBe(0);
        expect(result.skipped).toBe(1);
        expect(updateDocumentSeoFieldsMock).not.toHaveBeenCalled();
    });
    it('dry run reports without writing', async () => {
        const db = {
            document: {
                findMany: vi.fn().mockResolvedValueOnce([
                    {
                        id: 'p1',
                        collection: 'pages',
                        slug: 'home',
                        status: 'DRAFT',
                        data: { title: 'Draft' },
                        structuredData: null,
                        publishedAt: null,
                        updatedAt: new Date(),
                    },
                ]),
            },
        };
        const result = await migrateStructuredDataFields(ctxFor(db), { dryRun: true });
        expect(result.migrated).toBe(1);
        expect(updateDocumentSeoFieldsMock).not.toHaveBeenCalled();
    });
});
//# sourceMappingURL=migrate-schema.test.js.map