Files
Radosław SmolińskiandClaude Opus 5.5 3645e61f7d Fixes from the reference site: 404 page unlisted, own-origin media, fillGlobal defaults, switcher docs
- sitemap, llms.txt and the site index leave out the page assigned as the 404
  page (unlistedPageIds, exported); it stays when it is also the homepage
- buildImagesConfig: in production with R2_PUBLIC_URL empty, allow the site's
  own /api/media/file/** so next/image no longer answers 400
- fillGlobal: a choice field (select, radio, checkbox, number, date) still at its
  defaultValue counts as empty; text is never overwritten
- docs: LanguageSwitcher renderLabel needs a 'use client' wrapper (i18n.md,
  ipal-site, ipal-setup, api.md)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-29 15:29:07 +01:00

118 lines
6.2 KiB
TypeScript

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, mimeType: 'image/jpeg', 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('placeholder colour only where no alpha can show it (JPEG), unless forced', () => {
const png = { ...image, mimeType: 'image/png', placeholder: { blurDataURL: 'data:x', color: '#000000' } }
expect(mediaImageProps(png)?.style).toBeUndefined()
expect(mediaImageProps(png)?.placeholder).toBe('blur')
expect(mediaImageProps(png, { placeholderColor: true })?.style).toEqual({ backgroundColor: '#000000' })
expect(mediaImageProps({ ...png, mimeType: 'image/jpeg' }, { placeholderColor: false })?.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({ production: true, publicUrl: 'https://media.acme.pl/', serverUrl: 'https://acme.pl' })).toEqual({
formats: ['image/avif', 'image/webp'],
minimumCacheTTL: 2678400,
remotePatterns: [{ hostname: 'media.acme.pl', pathname: '/**', protocol: 'https' }],
})
expect(buildImagesConfig({ production: true, publicUrl: '/media' }).remotePatterns).toEqual([])
})
test('development without R2: the app\'s own media on localhost (private IP allowed)', () => {
const config = buildImagesConfig({ production: false, publicUrl: undefined, serverUrl: 'http://localhost:3000' })
expect(config.remotePatterns).toEqual([{ hostname: 'localhost', pathname: '/api/media/file/**', port: '3000', protocol: 'http' }])
expect(config.dangerouslyAllowLocalIP).toBe(true)
expect(buildImagesConfig({ production: false, serverUrl: 'https://staging.acme.pl' }).dangerouslyAllowLocalIP).toBeUndefined()
})
test('production without R2 (the documented default): the site\'s own media are allowed', () => {
const config = buildImagesConfig({ production: true, publicUrl: '', serverUrl: 'https://acme.pl' })
expect(config.remotePatterns).toEqual([{ hostname: 'acme.pl', pathname: '/api/media/file/**', protocol: 'https' }])
expect(config.dangerouslyAllowLocalIP).toBeUndefined()
// A local production build (CI, `next start` on localhost) still never opens private IPs.
expect(buildImagesConfig({ production: true, serverUrl: 'http://localhost:3000' }).dangerouslyAllowLocalIP).toBeUndefined()
})
})
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<string, unknown> = {}
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<string, unknown> = {}
await buildMediaPlaceholderHook()({ args: { data: svgData }, operation: 'create', req: { ...req, file: { data: png, mimetype: 'image/svg+xml' } } } as never)
expect(svgData.placeholder).toBeUndefined()
})
test('transparent image: blur kept, no colour', async () => {
const logo = await sharp({ create: { background: { alpha: 0, b: 255, g: 255, r: 255 }, channels: 4, height: 100, width: 300 } }).png().toBuffer()
const data: Record<string, unknown> = {}
const req = { file: { data: logo, 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: null | string }
expect(ph.blurDataURL).toMatch(/^data:image\/webp;base64,/)
expect(ph.color).toBeNull()
})
})