#!/usr/bin/env node /** * Banc de routes (lot SURFACE) : bâtit l'application une fois, la démarre sur un port local * et joue des requêtes HTTP contre elle. Les services externes sont remplacés par le faux * serveur de scripts/banc-routes.mjs, qui enregistre ce qu'il reçoit. Aucun appel réel. * * Usage : npm run test:routes (bâtit, ~1 min) * BANC_SANS_BUILD=1 npm run test:routes (réutilise .output) */ import { demarrerBanc } from './banc-routes.mjs' let ok = 0 let ko = 0 const echecs = [] function check(nom, cond, detail = '') { if (cond) { ok++; console.log(` ✓ ${nom}`) } else { ko++; echecs.push(nom); console.log(` ✗ ${nom}${detail ? ` — ${detail}` : ''}`) } } const banc = await demarrerBanc() const { faux } = banc async function groupe(titre, fn) { console.log(`\n${titre}`) faux.reinitialiser() try { await fn() } catch (e) { ko++; echecs.push(`${titre} : exception`); console.log(` ✗ exception : ${e?.stack ?? e}`) } } try { await groupe('A. Le banc joue les routes', async () => { const r = await banc.requete('/robots.txt') check('GET /robots.txt → 200', r.status === 200, `status ${r.status}`) for (const chemin of ['/evenements/ruetz', '/evenements/ruetz/codev', '/evenements/ruetz/foret']) { const p = await banc.requete(chemin) check(`témoin Ruetz ${chemin} → 200`, p.status === 200, `status ${p.status}`) } const c = await banc.requete('/api/chatbot', { method: 'POST', json: { question: 'Qui fait de la paille à Lyon ?' } }) check('POST /api/chatbot nominal → 200', c.status === 200, `status ${c.status} ${c.texte.slice(0, 200)}`) check('… la réponse vient du faux Bifrost', c.json?.reponse_texte === 'Réponse du faux modèle.') check('… le faux Bifrost a reçu un appel', faux.recu('bifrost').length === 1) const s = await banc.requete('/api/report', { method: 'POST', json: { fiche_id: 1, message: 'Le lien est mort.', email: 'a@b.fr' } }) check('POST /api/report nominal → 200', s.status === 200, `status ${s.status} ${s.texte.slice(0, 200)}`) check('… le faux Resend a reçu le courriel', faux.recu('resend').length === 1) const inconnus = faux.recu().filter((e) => !['nocodb', 'bifrost', 'mistral', 'resend', 'rag', 'rag-emancipation'].includes(e.service)) check('aucun appel vers un service non simulé', inconnus.length === 0, inconnus.map((e) => e.service).join(', ')) }) } finally { await banc.arreter() } console.log(`\ntest-routes : ${ok} sur ${ok + ko}${ko ? ` — échecs : ${echecs.join(' · ')}` : ''}`) process.exit(ko ? 1 : 0)