import { describe, expect, it, vi, beforeEach } from 'vitest';
import { setActuateConfig } from '../../config/runtime.js';
import { migrateMetaFields } from '../../seo/migrate-meta.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('migrateMetaFields', () => {
    beforeEach(() => {
        updateDocumentSeoFieldsMock.mockClear();
        setActuateConfig({
            seo: {
                siteName: 'Acme',
                siteUrl: 'https://acme.com',
                titleTemplate: '%page% | %site%',
            },
            collections: {
                pages: { slug: 'pages', type: 'page', fields: [] },
            },
        });
    });
    it('stamps metaTitle from title template when missing', async () => {
        const db = {
            document: {
                findMany: vi
                    .fn()
                    .mockResolvedValueOnce([
                    {
                        id: 'p1',
                        collection: 'pages',
                        title: 'About',
                        slug: 'about',
                        status: 'PUBLISHED',
                        data: { title: 'About', slug: 'about' },
                        publishedAt: new Date(),
                    },
                ])
                    .mockResolvedValueOnce([]),
            },
        };
        const result = await migrateMetaFields(ctxFor(db), { batchSize: 10 });
        expect(result.migrated).toBe(1);
        expect(updateDocumentSeoFieldsMock).toHaveBeenCalledWith('pages', 'p1', expect.objectContaining({ metaTitle: 'About | Acme' }), expect.any(Object));
    });
    it('dry run reports without writing', async () => {
        const db = {
            document: {
                findMany: vi.fn().mockResolvedValueOnce([
                    {
                        id: 'p1',
                        collection: 'pages',
                        title: 'About',
                        slug: 'about',
                        status: 'PUBLISHED',
                        data: { title: 'About' },
                        publishedAt: new Date(),
                    },
                ]),
            },
        };
        const result = await migrateMetaFields(ctxFor(db), { dryRun: true });
        expect(result.migrated).toBe(1);
        expect(updateDocumentSeoFieldsMock).not.toHaveBeenCalled();
    });
});
//# sourceMappingURL=migrate-meta.test.js.map