import { describe, expect, it, vi } from 'vitest';
import { createSiteClient, normalizeMedia, resolveMedia } from '../site.js';
describe('site helpers', () => {
    it('fetches globals through the public CMS API', async () => {
        const fetchMock = vi.fn(async () => new Response(JSON.stringify({ title: 'MaidPro', phone: '555-0100' }), {
            status: 200,
            headers: { 'Content-Type': 'application/json' },
        }));
        const site = createSiteClient({
            baseUrl: 'https://example.com',
            fetch: fetchMock,
        });
        await expect(site.getGlobal('site-settings')).resolves.toEqual({
            title: 'MaidPro',
            phone: '555-0100',
        });
        expect(fetchMock).toHaveBeenCalledWith('https://example.com/api/cms/public/globals/site-settings', expect.objectContaining({ headers: { 'Content-Type': 'application/json' } }));
    });
    it('resolves documents through the public CMS API', async () => {
        const fetchMock = vi.fn(async () => new Response(JSON.stringify({ data: { id: 'doc-1', collection: 'pages', data: { title: 'Home' } } }), { status: 200, headers: { 'Content-Type': 'application/json' } }));
        const site = createSiteClient({
            baseUrl: 'https://example.com/',
            fetch: fetchMock,
        });
        await expect(site.resolveDocument('/services/cleaning')).resolves.toEqual({
            data: { id: 'doc-1', collection: 'pages', data: { title: 'Home' } },
        });
        expect(fetchMock).toHaveBeenCalledWith('https://example.com/api/cms/resolve?path=%2Fservices%2Fcleaning', expect.any(Object));
    });
    it('normalizes public media values to a consistent object shape', () => {
        expect(normalizeMedia('https://cdn.example.com/hero.jpg')).toEqual({
            url: 'https://cdn.example.com/hero.jpg',
            alt: null,
            title: null,
            width: null,
            height: null,
            storageKey: null,
        });
        expect(normalizeMedia({
            id: 'media-1',
            url: '/uploads/hero.jpg',
            alt: 'Hero image',
            width: 1200,
        })).toEqual({
            id: 'media-1',
            url: '/uploads/hero.jpg',
            alt: 'Hero image',
            title: null,
            width: 1200,
            height: null,
            storageKey: null,
        });
    });
    it('resolves media IDs with a caller-provided resolver', async () => {
        const mediaById = {
            'media-1': {
                id: 'media-1',
                url: '/uploads/hero.jpg',
                alt: 'Hero image',
                title: null,
                width: 1200,
                height: 800,
                storageKey: 'actuate/media/hero.jpg',
            },
        };
        await expect(resolveMedia('media-1', {
            resolveById: async (id) => mediaById[id] ?? null,
        })).resolves.toEqual(mediaById['media-1']);
    });
});
//# sourceMappingURL=site.test.js.map