import { afterAll, beforeAll, describe, expect, it } from 'vitest'; import type { LiveData } from '../config/hostInjector.ts'; import { eventually, openPage, waitForHydration, type OpenedPage, } from './support/browser.ts'; import { startServer } from './support/server.ts'; // De CMS-host vult markers in de geprerenderde HTML (plan: aidlc-docs/features/ // asf-cms-host-injection). Hier doet de previewserver alsof hij die host is // (config/hostInjector.ts), met data die van de gebakken data afwijkt. Wat // getest wordt: de HTML die de host schrijft is leesbaar zonder JavaScript, // de pagina hydrateert zonder fout, toont daarna de actuele data zonder zelf // de API te vragen, en valt terug op wat gebakken is als de data niet bruikbaar // is. const AUTHORED = 'https://andreaslop.nl'; let live: { data: LiveData; publicBaseUrl?: string } | null = null; let server: Awaited>; beforeAll(async () => { server = await startServer({ inject: () => live && { data: live.data, authoredBaseUrl: AUTHORED, publicBaseUrl: live.publicBaseUrl, }, }); }); afterAll(() => server.close()); const pakket = ( slug: string, title: string, price: string, order: number, extra: Record = {}, ) => ({ slug, title, price, features: [`Kenmerk van ${title}`], displayOrder: order, ...extra, }); const FIVE = [ pakket('zwangerschap', 'Zwangerschap', '€ 200', 0), pakket('newborn', 'Newborn', '€ 210', 1, { featured: true }), pakket('gezin', 'Gezin', '€ 220', 2), pakket('loveshoot', 'Loveshoot', '€ 230', 3), pakket('portret', 'Portret', '€ 240', 4), ]; const NO_BANNERS = { top: null, bottom: null }; const html = async (path: string) => (await fetch(server.origin + path)).text(); const cards = (page: OpenedPage['page']) => page.locator('[data-testid^="price-card-"]:not([data-testid$="-book"])'); // Elke test begint zonder injectie en zet live zelf const inject = (data: LiveData, publicBaseUrl?: string) => { live = { data, publicBaseUrl }; }; describe('markers in de gebouwde pagina', () => { it('heeft ze op de plekken van het contract, en niet op andere', async () => { live = null; const home = await html('/'); for (const marker of [ 'OFFERINGS-HTML', 'BANNER-TOP-HTML', 'BANNER-BOTTOM-HTML', 'HERO-HTML', 'HERO-PRELOAD', ]) { expect(home.split(``)).toHaveLength(2); expect(home.split(``)).toHaveLength(2); } for (const path of [ '/', '/over-mij', '/contact', '/soorten-fotoshoots', ]) { const page = await html(path); expect(page.split('')).toHaveLength(2); expect(page).toMatch( /'], }, ), ], banners: { top: null, bottom: { title: 'x', position: 'bottom' }, }, }); const raw = await html('/'); expect(raw).not.toContain('window.pwned'); const { page, messages } = await openPage(server.origin + '/'); await waitForHydration(page); await eventually(() => cards(page).count()).toBe(1); expect(await page.evaluate(() => 'pwned' in window)).toBe(false); expect(await cards(page).first().textContent()).toContain( '', ); expect(messages).toEqual([]); }); }); describe('zonder host die injecteert', () => { it('toont de gebakken pakketten en vraagt de API zelf, zoals voor de injectie', async () => { live = null; const requested: string[] = []; const { page, messages } = await openPage(server.origin + '/', { prepare: async (p) => { p.on('request', (request) => { if (request.url().includes('/api/v1/offerings')) { requested.push(new URL(request.url()).pathname); } }); }, }); await waitForHydration(page); await eventually(() => cards(page).count()).toBe(3); await eventually(async () => requested.length).toBe(3); // Het blok dat de build schreef telt niet als data van de host expect(requested.sort()).toEqual([ '/api/v1/offerings', '/api/v1/offerings/banner', '/api/v1/offerings/banner', ]); expect(messages).toEqual([]); }); }); const hostHero = (extra: Record = {}) => ({ display: 'slideshow', title: 'Nieuwe titel', tagline: 'Nieuw label', buttons: [{ label: 'Boek nu', target: '#contact' }], slides: ['een', 'twee'].map((name) => ({ id: name, alt: `Alt ${name}`, decorative: false, image: { width: 100, height: 80, focus: { x: 0.25, y: 0.75 }, src: '/favicon.png', variants: [], }, })), ...extra, }); describe('de hero van de host', () => { it('schrijft de eerste foto, de titel en de preload in de HTML', async () => { inject({ offerings: FIVE, banners: NO_BANNERS, hero: hostHero() }); const home = await html('/'); const region = home.slice( home.indexOf(''), home.indexOf(''), ); expect(region).toContain('Nieuwe titel'); expect(region).toContain('alt="Alt een"'); expect(region).not.toContain('Alt twee'); expect(region).toContain('style="object-position:25% 75%"'); expect(region).toContain('href="#contact"'); expect(home).toContain( '', ); }); it('toont na hydratatie de slides van de host, zonder API-verzoek', async () => { inject({ offerings: FIVE, banners: NO_BANNERS, hero: hostHero() }); const requested: string[] = []; const { page, messages } = await openPage(server.origin + '/', { prepare: async (p) => { p.on('request', (request) => { if (request.url().includes('/api/v1/hero')) { requested.push(request.url()); } }); }, }); await waitForHydration(page); await eventually(() => page.locator('[aria-roledescription="carousel"] h1').textContent(), ).toContain('Nieuwe titel'); expect( await page .locator('[aria-roledescription="carousel"] img') .first() .getAttribute('alt'), ).toBe('Alt een'); expect(requested).toEqual([]); expect(messages).toEqual([]); }); it('laat de gebakken hero staan als de data niet bruikbaar is', async () => { inject({ offerings: FIVE, banners: NO_BANNERS, hero: hostHero({ slides: [] }), }); const home = await html('/'); // The data block still holds what the host sent; the region keeps the // baked hero const region = home.slice( home.indexOf(''), home.indexOf(''), ); expect(region).not.toContain('Nieuwe titel'); expect(region).toContain('Andrea Slop'); expect(home).toContain('