import { beforeEach, describe, expect, it } from 'vitest';
import { handleActuateAPI } from '../../api/index.js';
import { initDB } from '../../db.js';
function createMockDB(doc) {
    return {
        document: {
            findFirst: async () => doc,
        },
        user: {},
        session: {},
        media: {},
    };
}
const CONFIG = { collections: {}, globals: {} };
function navDoc(data) {
    return { id: 'nav-1', collection: 'navigations', title: 'Main', data, deletedAt: null };
}
describe('public navigations API', () => {
    beforeEach(() => {
        delete globalThis.__actuateConfig;
    });
    it('returns a clean { name, slug, location, items } tree without auth', async () => {
        const db = createMockDB(navDoc({
            name: 'Main Menu',
            slug: 'main',
            location: 'header',
            items: [
                { id: 'x1', label: 'Home', url: '/' },
                {
                    id: 'x2',
                    label: 'Products',
                    url: '/products',
                    target: '_self',
                    children: [{ id: 'x3', label: 'Widgets', url: '/products/widgets' }],
                },
            ],
        }));
        initDB(db);
        const handler = handleActuateAPI({ prismaClient: db, config: CONFIG });
        const res = await handler(new Request('https://example.com/api/cms/public/navigations/main'));
        expect(res.status).toBe(200);
        const body = (await res.json());
        expect(body.data).toEqual({
            name: 'Main Menu',
            slug: 'main',
            location: 'header',
            items: [
                { label: 'Home', url: '/' },
                {
                    label: 'Products',
                    url: '/products',
                    target: '_self',
                    children: [{ label: 'Widgets', url: '/products/widgets' }],
                },
            ],
        });
    });
    it('drops items without a label and strips internal ids', async () => {
        const db = createMockDB(navDoc({
            slug: 'main',
            items: [{ id: 'a', label: '', url: '/x' }, { label: 'Ok', url: '/ok' }, 'garbage', null],
        }));
        initDB(db);
        const handler = handleActuateAPI({ prismaClient: db, config: CONFIG });
        const res = await handler(new Request('https://example.com/api/cms/public/navigations/main'));
        const body = (await res.json());
        expect(body.data.items).toEqual([{ label: 'Ok', url: '/ok' }]);
    });
    it('404s when no navigation matches the slug', async () => {
        const db = createMockDB(null);
        initDB(db);
        const handler = handleActuateAPI({ prismaClient: db, config: CONFIG });
        const res = await handler(new Request('https://example.com/api/cms/public/navigations/missing'));
        expect(res.status).toBe(404);
    });
});
//# sourceMappingURL=public-navigations.test.js.map