#!/usr/bin/env node /** * Test de l'indexation (B23) : canonique, robots, redirections, robots.txt, sitemap. * Charge le VRAI fichier utils/seo.ts via jiti (même patron que test-echelles-nav.mjs). * Usage : node scripts/test-seo.mjs * node scripts/test-seo.mjs http://localhost:3000 (+ contrôles HTTP sur un serveur lancé) */ import { createJiti } from 'jiti' import { fileURLToPath } from 'node:url' import { dirname, join } from 'node:path' const __dirname = dirname(fileURLToPath(import.meta.url)) const jiti = createJiti(import.meta.url) const { canonique, robots, redirection, robotsTxt, cheminsSitemap, sitemapXml, dateSitemap } = await jiti.import( join(__dirname, '..', 'utils', 'seo.ts'), ) let echecs = 0 function egal(nom, obtenu, attendu) { const ok = JSON.stringify(obtenu) === JSON.stringify(attendu) if (!ok) echecs++ console.log(`${ok ? 'ok ' : 'ÉCHEC'} ${nom}${ok ? '' : `\n obtenu ${JSON.stringify(obtenu)}\n attendu ${JSON.stringify(attendu)}`}`) } const AEP = 'aep.trans-former.fr' const EVT = 'event.trans-former.fr' const A = 'https://aep.trans-former.fr' const E = 'https://event.trans-former.fr' // ── Canonique ─────────────────────────────────────────────────── const canoniques = [ [AEP, '/', {}, `${A}/`], [AEP, '/', { fiche: '108' }, `${A}/fiche/108`], [AEP, '/', { fiche: '108', vue: 'fiches', q: 'bois' }, `${A}/fiche/108`], [AEP, '/', { vue: 'fiches', tags: 'eau', echelle: 'M' }, `${A}/`], [AEP, '/agences', { fiche: 'f1-rotor', mode: 'carte' }, `${A}/agences`], [AEP, '/media', { tab: 'projets', q: 'x' }, `${A}/media?tab=projets`], [AEP, '/outils', { tab: 'simulateurs', random: '1' }, `${A}/outils?tab=simulateurs`], [AEP, '/outils/', {}, `${A}/outils`], [AEP, '/entree', { porte: 'S' }, `${A}/entree`], [AEP, '/fiche/108', { territoire: 'idf' }, `${A}/fiche/108`], [AEP, '/evenements/ruetz', {}, `${E}/ruetz`], [AEP, '/evenements', {}, `${E}/`], [EVT, '/ruetz', { utm: 'deck' }, `${E}/ruetz`], [EVT, '/', {}, `${E}/`], [EVT, '/evenements/ruetz', {}, `${E}/ruetz`], [EVT, '/agences', {}, `${A}/agences`], ['localhost:3000', '/media', { tab: 'projets' }, `${A}/media?tab=projets`], ] for (const [h, p, q, att] of canoniques) egal(`canonique ${h}${p} ${JSON.stringify(q)}`, canonique(h, p, q), att) // ── Robots (balise) ───────────────────────────────────────────── const NI = 'noindex, follow' const robotsCas = [ [AEP, '/', null], [AEP, '/agences', null], [AEP, '/fiche/12', null], [AEP, '/entree', null], [AEP, '/signaler', NI], [AEP, '/ajouter', NI], [AEP, '/proposer', NI], [AEP, '/dev/fiche-mini-card', NI], [AEP, '/codev', NI], [AEP, '/codev/demo', NI], [AEP, '/codevelopper', null], [AEP, '/evenements/ruetz', NI], [EVT, '/ruetz', null], [EVT, '/evenements/ruetz', null], ] for (const [h, p, att] of robotsCas) egal(`robots ${h}${p}`, robots(h, p), att) // ── Redirections ──────────────────────────────────────────────── const redirs = [ ['/pratiques-regeneratives', '/agences'], ['/pratiques-regeneratives/', '/agences'], ['/proposer-pratique', '/proposer'], ['/pratique/42', '/agences'], ['/proposer', null], ['/chatbot-v2', null], ['/agences', null], ] for (const [p, att] of redirs) egal(`301 ${p}`, redirection(p), att) // ── robots.txt ────────────────────────────────────────────────── const txt = robotsTxt(AEP) egal('robots.txt interdit /api/', txt.includes('Disallow: /api/'), true) egal('robots.txt laisse /_nuxt/ assets', /Disallow: \/_nuxt\/\n/.test(txt), false) egal('robots.txt sitemap aep', txt.includes(`Sitemap: ${A}/sitemap.xml`), true) egal('robots.txt sitemap event', robotsTxt(EVT).includes(`Sitemap: ${E}/sitemap.xml`), true) // ── Sitemap ───────────────────────────────────────────────────── const routes = ['/', '/a-propos', '/agences', '/ajouter', '/codev', '/codev/carto', '/codev/demo', '/contribuer', '/dev/fiche-mini-card', '/entree', '/fiche/:id()', '/manifeste', '/media', '/outils', '/proposer', '/rag', '/signaler', '/trouver-du-taf', '/evenements', '/evenements/ruetz', '/evenements/:slug()'] egal('sitemap aep', cheminsSitemap(routes, AEP), ['/', '/a-propos', '/agences', '/entree', '/manifeste', '/media', '/outils', '/trouver-du-taf']) egal('sitemap event', cheminsSitemap(routes, EVT), ['/', '/ruetz']) const xml = sitemapXml([{ loc: `${A}/` }, { loc: `${A}/fiche/1?a=1&b=2`, lastmod: '2026-10-01' }]) egal('sitemap XML échappé', xml.includes('fiche/1?a=1&b=2'), true) egal('sitemap XML en-tête', xml.startsWith('\n'), true) egal('dateSitemap', [dateSitemap('2026-10-13 08:12:00+00:00'), dateSitemap(''), dateSitemap('n/a')], ['2026-10-13', undefined, undefined]) // ── HTTP (optionnel, serveur lancé) ───────────────────────────── const base = process.argv[2] if (base) { const get = (p, opt = {}) => fetch(base + p, { redirect: 'manual', ...opt }) const r1 = await get('/robots.txt') egal('HTTP robots.txt 200 text/plain', [r1.status, r1.headers.get('content-type')?.startsWith('text/plain')], [200, true]) const r2 = await get('/sitemap.xml') const corps = await r2.text() egal('HTTP sitemap 200 xml', [r2.status, r2.headers.get('content-type')?.startsWith('application/xml')], [200, true]) const locs = [...corps.matchAll(/([^<]+)<\/loc>/g)].map(m => m[1]) egal('HTTP sitemap : ni /rag, ni /signaler, ni /contribuer', locs.filter(l => /\/(rag|signaler|contribuer)$/.test(l)), []) console.log(` (${locs.length} URL, dont ${locs.filter(l => l.includes('/fiche/')).length} fiches)`) for (const [p, cible] of [['/pratiques-regeneratives', '/agences'], ['/proposer-pratique', '/proposer'], ['/pratique/3', '/agences']]) { const r = await get(p) egal(`HTTP 301 ${p}`, [r.status, new URL(r.headers.get('location') || '', base).pathname], [301, cible]) } const page = async (p, host) => (await get(p, host ? { headers: { 'x-forwarded-host': host } } : {})).text() const canon = html => html.match(/]*rel="canonical"[^>]*>/)?.[0].match(/href="([^"]+)"/)?.[1] ?? null const rob = html => html.match(/]*name="robots"[^>]*content="([^"]+)"/)?.[1] ?? null const hAcc = await page('/') egal('HTTP / : canonique, pas de robots', [canon(hAcc), rob(hAcc)], [`${A}/`, null]) const hFiche = await page('/?fiche=108&vue=fiches') egal('HTTP /?fiche=108 → /fiche/108', canon(hFiche), `${A}/fiche/108`) for (const p of ['/signaler', '/ajouter', '/proposer', '/codev']) egal(`HTTP ${p} noindex`, rob(await page(p)), NI) const rApi = await get('/api/stats') egal('HTTP /api/stats X-Robots-Tag', rApi.headers.get('x-robots-tag'), 'noindex') } console.log(echecs ? `\n${echecs} échec(s)` : '\nTout est vert.') process.exit(echecs ? 1 : 0)