import { beforeEach, describe, expect, it } from 'vitest';
import { handleActuateAPI } from '../../api/index.js';
import { initDB } from '../../db.js';
function createMockDB(data) {
    return {
        document: {
            findFirst: async () => data
                ? {
                    id: 'global-1',
                    collection: 'site-settings',
                    data,
                    deletedAt: null,
                }
                : null,
        },
        user: {},
        session: {},
        media: {},
    };
}
describe('public globals API', () => {
    beforeEach(() => {
        delete globalThis.__actuateConfig;
    });
    it('returns readable global data without a session', async () => {
        initDB(createMockDB({ siteName: 'MaidPro' }));
        const handler = handleActuateAPI({
            prismaClient: createMockDB({ siteName: 'MaidPro' }),
            config: {
                collections: {},
                globals: {
                    'site-settings': {
                        slug: 'site-settings',
                        label: 'Site Settings',
                        fields: {},
                        access: { read: () => true },
                    },
                },
            },
        });
        const response = await handler(new Request('https://example.com/api/cms/public/globals/site-settings'));
        await expect(response.json()).resolves.toEqual({ data: { siteName: 'MaidPro' } });
        expect(response.status).toBe(200);
    });
    it('denies public global reads when access.read returns false', async () => {
        initDB(createMockDB({ siteName: 'Private' }));
        const handler = handleActuateAPI({
            prismaClient: createMockDB({ siteName: 'Private' }),
            config: {
                collections: {},
                globals: {
                    'site-settings': {
                        slug: 'site-settings',
                        label: 'Site Settings',
                        fields: {},
                        access: { read: () => false },
                    },
                },
            },
        });
        const response = await handler(new Request('https://example.com/api/cms/public/globals/site-settings'));
        expect(response.status).toBe(403);
    });
    it('returns declared globals as public when access.read is omitted', async () => {
        // Globals served from `/public/globals/:slug` are public site data by
        // definition. When no access policy is provided, the route should grant
        // read access — gating requires an explicit `access.read` returning false.
        initDB(createMockDB({ siteName: 'Implicit Public' }));
        const handler = handleActuateAPI({
            prismaClient: createMockDB({ siteName: 'Implicit Public' }),
            config: {
                collections: {},
                globals: {
                    'site-settings': {
                        slug: 'site-settings',
                        label: 'Site Settings',
                        fields: {},
                    },
                },
            },
        });
        const response = await handler(new Request('https://example.com/api/cms/public/globals/site-settings'));
        expect(response.status).toBe(200);
        await expect(response.json()).resolves.toEqual({ data: { siteName: 'Implicit Public' } });
    });
    it('does not expose documents for undeclared globals', async () => {
        initDB(createMockDB({ siteName: 'Hidden' }));
        const handler = handleActuateAPI({
            prismaClient: createMockDB({ siteName: 'Hidden' }),
            config: { collections: {}, globals: {} },
        });
        const response = await handler(new Request('https://example.com/api/cms/public/globals/site-settings'));
        expect(response.status).toBe(404);
    });
});
//# sourceMappingURL=public-globals.test.js.map