/** * Lot 15a — le message ntfy « Fiche enrichie » ne porte jamais de texte de contributeur. * Runner : node:test, zéro dépendance, aucune requête. Depuis la racine du repo : * node --test "worker/test/*.test.js" * Valeurs fictives seulement (courriel d'essai : essai@exemple.invalid). */ import { describe, it } from 'node:test'; import assert from 'node:assert/strict'; import { readFileSync } from 'node:fs'; import { dirname, join } from 'node:path'; import { fileURLToPath } from 'node:url'; import { messageFicheEnrichie, hoteDuLien } from '../lib/messageNtfy.js'; const __dirname = dirname(fileURLToPath(import.meta.url)); // Le texte libre tel que le formulaire assoupli le range (utils/submitLibre.ts, nomSuggere : 60 caractères). const TEXTE = 'Je m appelle Personne Essai, joignable au 00 00 00 00 00, essai@exemple.invalid'; const sansLien = { Id: 42, nom: `[à qualifier] ${TEXTE.slice(0, 60)}`, url: null, description_user: `Type : non précisé\n${TEXTE}`, submitted_by_email: 'essai@exemple.invalid', submission_type: 'ecosysteme', }; // Ce que le modèle propose, tiré du texte du contributeur. const enrichi = { nom: 'Collectif de Personne Essai', confiance: 'moyenne', type_suggere: 'reseau' }; const misAJour = { nom: enrichi.nom, submission_type: 'reseau' }; const fragments = ['Personne Essai', 'Je m appelle', '00 00 00 00 00', 'essai@exemple.invalid', 'à qualifier', 'Collectif']; describe('15a · message ntfy du worker', () => { it('contribution sans lien : ni son texte, ni le nom tiré du texte, ni le courriel', () => { const m = messageFicheEnrichie(sansLien, misAJour, enrichi); for (const f of fragments) assert.ok(!m.includes(f), `« ${f} » sort sur ntfy : ${m}`); }); it('contribution sans lien : dit « sans titre » et le numéro', () => { const m = messageFicheEnrichie(sansLien, misAJour, enrichi); assert.match(m, /^Id NocoDB : 42$/m); assert.match(m, /^Nom : sans titre /m); assert.match(m, /^Type : reseau$/m); assert.match(m, /^Confiance : moyenne$/m); }); it('contribution sans lien, sans proposition du modèle : même chose', () => { const m = messageFicheEnrichie(sansLien, {}, {}); for (const f of fragments) assert.ok(!m.includes(f), `« ${f} » sort sur ntfy : ${m}`); assert.match(m, /^Nom : sans titre /m); assert.match(m, /^Confiance : nc$/m); }); it('contribution avec lien : l’hôte du lien, jamais le nom saisi ni celui du modèle', () => { const avecLien = { ...sansLien, url: 'https://www.exemple.invalid/page?x=1', nom: 'Nom saisi par Personne Essai' }; const m = messageFicheEnrichie(avecLien, misAJour, enrichi); assert.match(m, /^Nom : exemple\.invalid /m); for (const f of fragments) assert.ok(!m.includes(f), `« ${f} » sort sur ntfy : ${m}`); assert.ok(!m.includes('page?x=1'), 'le chemin du lien ne sort pas'); }); it('lien qui n’est pas http(s) ou illisible : « sans titre »', () => { for (const url of ['javascript:alert(1)', 'pas un lien Personne Essai', '', ' ', 'mailto:essai@exemple.invalid', 42]) { const m = messageFicheEnrichie({ ...sansLien, url }, {}, {}); assert.match(m, /^Nom : sans titre /m, `url ${JSON.stringify(url)}`); for (const f of fragments) assert.ok(!m.includes(f), `« ${f} » sort sur ntfy (url ${JSON.stringify(url)}) : ${m}`); } }); it('type et confiance hors vocabulaire (sortie du modèle) : « nc », jamais le texte', () => { const m = messageFicheEnrichie( { ...sansLien, submission_type: TEXTE }, { submission_type: 'Personne Essai' }, { confiance: `haute — ${TEXTE}` }, ); for (const f of fragments) assert.ok(!m.includes(f), `« ${f} » sort sur ntfy : ${m}`); assert.match(m, /^Type : nc$/m); assert.match(m, /^Confiance : nc$/m); }); it('numéro absent ou non entier : « nc »', () => { assert.match(messageFicheEnrichie({ ...sansLien, Id: 'Personne Essai' }), /^Id NocoDB : nc$/m); assert.match(messageFicheEnrichie({}), /^Id NocoDB : nc$/m); }); it('hoteDuLien : http(s) seulement, sans « www. »', () => { assert.equal(hoteDuLien('https://www.exemple.invalid/a'), 'exemple.invalid'); assert.equal(hoteDuLien('http://sous.exemple.invalid'), 'sous.exemple.invalid'); assert.equal(hoteDuLien('ftp://exemple.invalid'), null); assert.equal(hoteDuLien(null), null); }); it('enrich.js passe par messageFicheEnrichie et n’écrit plus « Nom suggéré »', () => { const src = readFileSync(join(__dirname, '..', 'enrich.js'), 'utf-8'); assert.match(src, /notifyNtfy\('\[AEP\] Fiche enrichie', messageFicheEnrichie\(row, updateData, enriched\)\)/); assert.ok(!src.includes('Nom suggéré : ${'), 'l’ancienne ligne est encore là'); }); });