/** * Supporto ai test: elenco delle pagine letto dalla sitemap generata in dist/ (nessuna lista da * tenere allineata a mano), URL del sito e utilità comuni. */ import { existsSync, readFileSync } from 'node:fs'; import { resolve } from 'node:path'; import type { Page } from '@playwright/test'; export const SITE = (process.env.SITE_URL || 'https://www.grassoezio.it').replace(/\/+$/, ''); const DIST = resolve(import.meta.dirname, '..', '..', 'dist'); function locs(xml: string): string[] { return [...xml.matchAll(/([^<]+)<\/loc>/g)].map((m) => m[1].trim()); } /** Percorsi delle pagine elencate nella sitemap (es. «/», «/studio/»). */ export function sitemapPaths(): string[] { const index = resolve(DIST, 'sitemap-index.xml'); if (!existsSync(index)) { throw new Error('Sitemap non trovata in dist/: eseguire `npm run build` prima dei test.'); } const paths: string[] = []; for (const sitemap of locs(readFileSync(index, 'utf8'))) { const file = resolve(DIST, new URL(sitemap).pathname.replace(/^\//, '')); for (const url of locs(readFileSync(file, 'utf8'))) paths.push(new URL(url).pathname); } return [...new Set(paths)].sort(); } /** Pagine generate ma escluse dalla sitemap (noindex). */ export const EXTRA_PAGES = ['/404.html', '/contatti/grazie/']; export const PAGES = sitemapPaths(); export const ALL_PAGES = [...PAGES, ...EXTRA_PAGES]; export const SERVICE_PAGES = PAGES.filter((p) => /^\/servizi\/[^/]+\/$/.test(p)); /** Vero per le richieste che restano sul server locale di test. */ export function isLocalRequest(url: string): boolean { const u = new URL(url); if (u.protocol === 'data:' || u.protocol === 'blob:' || u.protocol === 'about:') return true; return u.hostname === '127.0.0.1' || u.hostname === 'localhost'; } /** Raccoglie errori della console e della pagina. */ export function collectConsoleErrors(page: Page): string[] { const errors: string[] = []; page.on('console', (msg) => { if (msg.type() === 'error') errors.push(msg.text()); }); page.on('pageerror', (err) => errors.push(err.message)); return errors; } /** Risposta finta per Google Maps: nei test nessuna richiesta esce dalla macchina. */ export async function stubGoogle(page: Page): Promise { const requested: string[] = []; await page.route('https://www.google.com/**', (route) => { requested.push(route.request().url()); return route.fulfill({ status: 200, contentType: 'text/html', body: 'Mappa', }); }); return requested; }