import { beforeEach, describe, expect, it, vi } from 'vitest';
import { handleActuateAPI } from '../../api/index.js';
import { initDB } from '../../db.js';
/**
 * Regression test for sitemap pagination. The old per-collection sitemap capped
 * at 5,000 URLs and silently dropped the rest, and the index emitted a single
 * `<sitemap>` per collection regardless of size. Collections larger than the
 * 50k protocol limit must now be split across `?page=N` files referenced from
 * the index.
 */
const SITEMAP_PAGE_SIZE = 45000;
function createMockDB(opts) {
    return {
        document: {
            count: vi.fn(async () => opts.count),
            findMany: opts.findMany,
            findFirst: vi.fn(async () => null),
        },
        user: {},
        session: {},
        media: {},
    };
}
const postsConfig = {
    seo: { siteUrl: 'https://example.com' },
    collections: {
        posts: {
            slug: 'posts',
            labels: { singular: 'Post', plural: 'Posts' },
            urlPrefix: 'blog',
            type: 'post',
            fields: {},
        },
    },
};
describe('sitemap pagination', () => {
    beforeEach(() => {
        delete globalThis.__actuateConfig;
    });
    it('emits one <sitemap> entry per 45k-doc page in the index', async () => {
        const findMany = vi.fn(async () => []);
        const db = createMockDB({ count: SITEMAP_PAGE_SIZE * 2 + 10, findMany });
        initDB(db);
        const handler = handleActuateAPI({ prismaClient: db, config: postsConfig });
        const res = await handler(new Request('https://example.com/api/cms/sitemap.xml'));
        expect(res.status).toBe(200);
        const text = await res.text();
        // 90,010 docs / 45,000 = 3 pages → page 0 (bare) + page 1 + page 2.
        expect(text).toContain('<loc>https://example.com/api/cms/sitemaps/posts.xml</loc>');
        expect(text).toContain('<loc>https://example.com/api/cms/sitemaps/posts.xml?page=1</loc>');
        expect(text).toContain('<loc>https://example.com/api/cms/sitemaps/posts.xml?page=2</loc>');
        expect(text).not.toContain('?page=3');
    });
    it('emits a single entry for a small collection', async () => {
        const findMany = vi.fn(async () => []);
        const db = createMockDB({ count: 12, findMany });
        initDB(db);
        const handler = handleActuateAPI({ prismaClient: db, config: postsConfig });
        const res = await handler(new Request('https://example.com/api/cms/sitemap.xml'));
        const text = await res.text();
        expect(text).toContain('<loc>https://example.com/api/cms/sitemaps/posts.xml</loc>');
        expect(text).not.toContain('?page=1');
    });
    it('applies skip/take when a page is requested', async () => {
        const findMany = vi.fn(async (_args) => []);
        const db = createMockDB({ count: SITEMAP_PAGE_SIZE * 2, findMany });
        initDB(db);
        const handler = handleActuateAPI({ prismaClient: db, config: postsConfig });
        await handler(new Request('https://example.com/api/cms/sitemaps/posts.xml?page=2'));
        expect(findMany).toHaveBeenCalledTimes(1);
        const arg = (findMany.mock.calls[0]?.[0] ?? {});
        expect(arg.skip).toBe(2 * SITEMAP_PAGE_SIZE);
        expect(arg.take).toBe(SITEMAP_PAGE_SIZE);
    });
    it('omits the archive URL on pages after the first', async () => {
        const findMany = vi.fn(async () => []);
        const db = createMockDB({ count: SITEMAP_PAGE_SIZE * 2, findMany });
        initDB(db);
        const handler = handleActuateAPI({
            prismaClient: db,
            config: {
                seo: { siteUrl: 'https://example.com' },
                collections: {
                    posts: {
                        slug: 'posts',
                        labels: { singular: 'Post', plural: 'Posts' },
                        urlPrefix: 'blog',
                        type: 'post',
                        seo: { archivePath: '/blog' },
                        fields: {},
                    },
                },
            },
        });
        const res = await handler(new Request('https://example.com/api/cms/sitemaps/posts.xml?page=1'));
        const text = await res.text();
        expect(text).not.toContain('<loc>https://example.com/blog</loc>');
    });
});
//# sourceMappingURL=sitemap-pagination.test.js.map