import sharp from 'sharp' import { describe, expect, test, vi } from 'vitest' import { MEDIA_SIZES, mediaImageProps, videoProps } from '../src/modules/media/mediaProps.js' import { buildMediaPlaceholderHook } from '../src/modules/media/placeholder.js' import { buildImagesConfig } from '../src/modules/storage/buildImagesConfig.js' const image = { alt: 'Ekipa przy pracy', height: 800, mimeType: 'image/webp', url: '/media/a.webp', width: 1200 } describe('mediaImageProps', () => { test('lazy by default, dimensions from the document', () => { expect(mediaImageProps(image, { sizes: MEDIA_SIZES.half })).toEqual({ alt: 'Ekipa przy pracy', height: 800, loading: 'lazy', sizes: '(min-width: 768px) 50vw, 100vw', src: '/media/a.webp', width: 1200, }) }) test('LCP image: preload + high priority + eager', () => { expect(mediaImageProps(image, { priority: true })).toMatchObject({ fetchPriority: 'high', loading: 'eager', preload: true }) }) test('placeholder and focal point', () => { const p = mediaImageProps({ ...image, focalX: 30, focalY: 70, placeholder: { blurDataURL: 'data:x', color: '#123456' } }) expect(p).toMatchObject({ blurDataURL: 'data:x', placeholder: 'blur', style: { backgroundColor: '#123456', objectPosition: '30% 70%' } }) expect(mediaImageProps({ ...image, focalX: 50, focalY: 50 })?.style).toBeUndefined() }) test('video, PDF and bare ids give null; decorative alt override', () => { expect(mediaImageProps({ ...image, mimeType: 'video/mp4' })).toBeNull() expect(mediaImageProps({ mimeType: 'application/pdf', url: '/x.pdf' })).toBeNull() expect(mediaImageProps(42)).toBeNull() expect(mediaImageProps(image, { alt: '' })?.alt).toBe('') }) }) describe('videoProps', () => { const video = { mimeType: 'video/mp4', url: '/media/v.mp4' } test('player: controls, nothing downloaded before play', () => { expect(videoProps(video, { poster: { url: '/media/p.webp' } })).toEqual({ controls: true, playsInline: true, poster: '/media/p.webp', preload: 'none', src: '/media/v.mp4', }) }) test('background: muted loop, no autoplay with reduced motion', () => { expect(videoProps(video, { mode: 'background' })).toMatchObject({ autoPlay: true, loop: true, muted: true }) expect(videoProps(video, { mode: 'background', reducedMotion: true })?.autoPlay).toBe(false) expect(videoProps(image)).toBeNull() }) }) describe('buildImagesConfig', () => { test('AVIF/WebP and the R2 domain', () => { expect(buildImagesConfig({ publicUrl: 'https://media.acme.pl/' })).toEqual({ formats: ['image/avif', 'image/webp'], minimumCacheTTL: 2678400, remotePatterns: [{ hostname: 'media.acme.pl', pathname: '/**', protocol: 'https' }], }) expect(buildImagesConfig({ publicUrl: '/media' }).remotePatterns).toEqual([]) }) }) describe('buildMediaPlaceholderHook', () => { test('stores a tiny blur image and the dominant colour; skips non-raster files', async () => { const png = await sharp({ create: { background: { b: 40, g: 120, r: 200 }, channels: 3, height: 300, width: 400 } }).png().toBuffer() const data: Record = {} const req = { file: { data: png, mimetype: 'image/png' }, payload: { config: { sharp }, logger: { warn: vi.fn() } } } await buildMediaPlaceholderHook()({ args: { data }, operation: 'create', req } as never) const ph = data.placeholder as { blurDataURL: string; color: string } expect(ph.blurDataURL).toMatch(/^data:image\/webp;base64,/) expect(ph.blurDataURL.length).toBeLessThan(600) expect(ph.color).toBe('#c87828') const svgData: Record = {} await buildMediaPlaceholderHook()({ args: { data: svgData }, operation: 'create', req: { ...req, file: { data: png, mimetype: 'image/svg+xml' } } } as never) expect(svgData.placeholder).toBeUndefined() }) })