import { beforeEach, describe, expect, it } from 'vitest';
import { handleActuateAPI } from '../../api/index.js';
import { initDB } from '../../db.js';
function createMockDB(docs) {
    const findFirst = async (args) => {
        const w = args?.where ?? {};
        const found = docs.find((d) => {
            if (w.collection && d.collection !== w.collection)
                return false;
            if (w.status && d.status !== w.status)
                return false;
            if (w.deletedAt === null && d.deletedAt !== null)
                return false;
            if (Array.isArray(w.OR)) {
                const ok = w.OR.some((cond) => {
                    if (typeof cond.slug === 'string')
                        return d.slug === cond.slug;
                    if (cond.data && 'equals' in cond.data) {
                        return d.data?.slug === cond.data.equals;
                    }
                    return false;
                });
                if (!ok)
                    return false;
            }
            return true;
        });
        return found ?? null;
    };
    return {
        document: { findFirst, findMany: async () => [], count: async () => 0 },
        user: {},
        session: {},
        media: {},
    };
}
const PAGE = {
    title: 'About Us',
    slug: 'about',
    collection: 'pages',
    status: 'PUBLISHED',
    deletedAt: null,
    data: { body: '<h2>Who we are</h2><p>We build software.</p>' },
    plainText: 'Who we are We build software.',
};
function makeHandler(docs, llms = {}) {
    initDB(createMockDB(docs));
    return handleActuateAPI({
        prismaClient: createMockDB(docs),
        config: {
            seo: { siteUrl: 'https://example.com', llms },
            collections: {
                pages: {
                    slug: 'pages',
                    type: 'page',
                    labels: { singular: 'Page', plural: 'Pages' },
                    fields: {},
                },
            },
        },
    });
}
describe('GET /md', () => {
    beforeEach(() => {
        delete globalThis.__actuateConfig;
    });
    it('400s without a path', async () => {
        const handler = makeHandler([PAGE]);
        const res = await handler(new Request('https://example.com/api/cms/md'));
        expect(res.status).toBe(400);
    });
    it('renders published page content as Markdown with a noindex header', async () => {
        const handler = makeHandler([PAGE]);
        const res = await handler(new Request('https://example.com/api/cms/md?path=/about'));
        expect(res.status).toBe(200);
        expect(res.headers.get('content-type')).toContain('text/markdown');
        expect(res.headers.get('x-robots-tag')).toBe('noindex');
        const text = await res.text();
        expect(text).toContain('# About Us');
        expect(text).toContain('## Who we are');
        expect(text).toContain('We build software.');
    });
    it('404s for an unknown path', async () => {
        const handler = makeHandler([PAGE]);
        const res = await handler(new Request('https://example.com/api/cms/md?path=/missing'));
        expect(res.status).toBe(404);
    });
    it('404s for a noindexed document', async () => {
        const hidden = { ...PAGE, data: { ...PAGE.data, noindex: true } };
        const handler = makeHandler([hidden]);
        const res = await handler(new Request('https://example.com/api/cms/md?path=/about'));
        expect(res.status).toBe(404);
    });
    it('404s for an llmsInclude NEVER document', async () => {
        const excluded = { ...PAGE, data: { ...PAGE.data, llmsInclude: 'NEVER' } };
        const handler = makeHandler([excluded]);
        const res = await handler(new Request('https://example.com/api/cms/md?path=/about'));
        expect(res.status).toBe(404);
    });
    it('404s when the llms layer is disabled', async () => {
        const handler = makeHandler([PAGE], { disabled: true });
        const res = await handler(new Request('https://example.com/api/cms/md?path=/about'));
        expect(res.status).toBe(404);
    });
});
//# sourceMappingURL=md-route.test.js.map