Forms: `values` on RenderForm / BuilderForm / useBuilderForm fills fields the
page knows (chosen product variant, category) — read-only <output>, in the
SSR HTML, sent and validated like any field. prepareBuilderForm (/rsc) is the
server half of RenderForm for client components whose values change in the
browser; publicForm is exported so projects stop copying it.
Links: buildLinkField({ relationTo: ['pages', 'categories', …] }) — a
polymorphic relation; resolveLinkHref builds /{locale}/{archive}/{entry} from
`archives`, which getLinkOptions in createContentHelpers fills from Site
Settings.
SEO: pageJsonLd.entries — the main JSON-LD node per content collection
instead of BlogPosting; buildProductJsonLd emits an Offer only for a real
price from the panel.
Pages: createPageRoute / createNotFoundPage pass isFirst to blocks, so the
first section carries the page's only h1.
Fixes: ipalEnvRules({ storage: true }) requires R2_* in production only
(development: optional, all or none); buildImagesConfig allows the app's own
media on localhost outside production.
Docs, CHANGELOG, PROGRAMISTA §6 (34–37), ipal-form and ipal-upgrade skills.
Tests: builderForm, links, drafts (entries + archives), seo, media, env.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
92 lines
4.4 KiB
TypeScript
92 lines
4.4 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, 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({ 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()
|
|
})
|
|
})
|
|
|
|
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()
|
|
})
|
|
})
|