import { describe, expect, it } from 'vitest';
import { applyMediaMeta, collectSectionImageUrls, featuredImageUrl, } from '../media-enrich.js';
describe('collectSectionImageUrls', () => {
    it('collects feature + hero + gallery image URLs, deduped, skipping non-URLs', () => {
        const sections = [
            { sectionType: 'feature', content: { imageUrl: 'https://cdn/a.jpg' } },
            { sectionType: 'feature', content: { imageUrl: 'not-a-url' } },
            { sectionType: 'hero', content: { imageUrl: 'https://cdn/hero.jpg' } },
            {
                sectionType: 'gallery',
                content: {
                    images: [{ url: 'https://cdn/a.jpg' }, { url: '/local/b.png' }, { url: '' }, 'bad'],
                },
            },
        ];
        expect(collectSectionImageUrls(sections).sort()).toEqual([
            '/local/b.png',
            'https://cdn/a.jpg',
            'https://cdn/hero.jpg',
        ]);
    });
    it('returns [] for non-arrays', () => {
        expect(collectSectionImageUrls(undefined)).toEqual([]);
        expect(collectSectionImageUrls({})).toEqual([]);
    });
});
describe('featuredImageUrl', () => {
    it('reads a plain URL string', () => {
        expect(featuredImageUrl('https://cdn/hero.jpg')).toBe('https://cdn/hero.jpg');
        expect(featuredImageUrl('/local/hero.png')).toBe('/local/hero.png');
    });
    it('reads a media object { url }', () => {
        expect(featuredImageUrl({ url: 'https://cdn/hero.jpg' })).toBe('https://cdn/hero.jpg');
    });
    it('returns null for absent or non-URL values', () => {
        expect(featuredImageUrl(undefined)).toBeNull();
        expect(featuredImageUrl('')).toBeNull();
        expect(featuredImageUrl('not-a-url')).toBeNull();
        expect(featuredImageUrl({ url: 123 })).toBeNull();
    });
});
describe('applyMediaMeta', () => {
    const lookup = new Map([
        ['https://cdn/a.jpg', { width: 800, height: 600, alt: 'A cat', title: 'cat' }],
        ['/local/b.png', { width: 1200, height: 900, alt: 'B dog', title: null }],
    ]);
    it('adds width/height to a feature and backfills empty alt', () => {
        const out = applyMediaMeta([{ sectionType: 'feature', content: { imageUrl: 'https://cdn/a.jpg', imageAlt: '' } }], lookup);
        const c = out[0].content;
        expect(c.imageWidth).toBe(800);
        expect(c.imageHeight).toBe(600);
        expect(c.imageAlt).toBe('A cat');
    });
    it('does not overwrite an author-provided alt or explicit dimensions', () => {
        const out = applyMediaMeta([
            {
                sectionType: 'feature',
                content: { imageUrl: 'https://cdn/a.jpg', imageAlt: 'Author alt', imageWidth: 100 },
            },
        ], lookup);
        const c = out[0].content;
        expect(c.imageAlt).toBe('Author alt');
        expect(c.imageWidth).toBe(100); // not clobbered
        expect(c.imageHeight).toBe(600); // still filled (was missing)
    });
    it('enriches gallery rows by url', () => {
        const out = applyMediaMeta([
            {
                sectionType: 'gallery',
                content: { images: [{ url: '/local/b.png', alt: '' }, { url: 'https://unknown/x.jpg' }] },
            },
        ], lookup);
        const imgs = out[0].content.images;
        expect(imgs[0]).toMatchObject({ width: 1200, height: 900, alt: 'B dog' });
        expect(imgs[1]).toEqual({ url: 'https://unknown/x.jpg' }); // untouched
    });
    it('returns the same reference when nothing matches (no needless clone)', () => {
        const sections = [{ sectionType: 'feature', content: { imageUrl: 'https://none/x.jpg' } }];
        expect(applyMediaMeta(sections, lookup)[0]).toBe(sections[0]);
    });
    it('short-circuits on an empty lookup', () => {
        const sections = [{ sectionType: 'feature', content: { imageUrl: 'https://cdn/a.jpg' } }];
        expect(applyMediaMeta(sections, new Map())).toBe(sections);
    });
    it('enriches a hero image like a feature image', () => {
        const out = applyMediaMeta([{ sectionType: 'hero', content: { heading: 'Hi', imageUrl: 'https://cdn/a.jpg' } }], lookup);
        const c = out[0].content;
        expect(c.imageWidth).toBe(800);
        expect(c.imageHeight).toBe(600);
        expect(c.imageAlt).toBe('A cat');
    });
    it('leaves a hero section without an image alone', () => {
        const sections = [{ sectionType: 'hero', content: { heading: 'Hi' } }];
        expect(applyMediaMeta(sections, lookup)[0]).toBe(sections[0]);
    });
});
//# sourceMappingURL=media-enrich.test.js.map