diff --git a/worker/enrich.js b/worker/enrich.js index 6e2408a..7beb9d7 100644 --- a/worker/enrich.js +++ b/worker/enrich.js @@ -35,7 +35,7 @@ import { dirname, join } from 'path'; import { fileURLToPath } from 'url'; import { messageFicheEnrichie, hoteDuLien } from './lib/messageNtfy.js'; import { recupererPage, controlerAdresse, tableRedirections } from './lib/recupererPage.js'; -import { extractionDepuisModele } from './lib/extraction.js'; +import { extractionDepuisModele, texteBorne, MAX_COLONNES } from './lib/extraction.js'; import { CHAMPS_BASE, CHAMPS_ECRITS, VERSION_SORTIE, baseDepuisFiches, passeAdresse, resoudreLot, propositionTaxonomie, statutApresEnrichissement, relireScrape, lienPrincipal, lirePagine, @@ -683,7 +683,8 @@ async function run() { const normalizedTags = [...new Set(rawTags.map(normalizeTag).filter(Boolean))]; const updateData = { - description_enrichie: typeof enriched.description === 'string' ? enriched.description : null, + // Checkpoint 22/11 : les textes du modèle écrits en colonne sont bornés (300, 200, 100). + description_enrichie: texteBorne(enriched.description, MAX_COLONNES.description_enrichie), tags_fonction: normalizedTags.join(','), moderation_status: statutApresEnrichissement(resolu?.r6), ai_processed: true, @@ -700,11 +701,13 @@ async function run() { }; // Nom : ne remplacer que le placeholder posé par le formulaire assoupli (B5-M1). - if (typeof enriched.nom === 'string' && enriched.nom && typeof row.nom === 'string' && row.nom.startsWith('[à qualifier]')) { - updateData.nom = enriched.nom; + const nomPropose = texteBorne(enriched.nom, MAX_COLONNES.nom); + if (nomPropose && typeof row.nom === 'string' && row.nom.startsWith('[à qualifier]')) { + updateData.nom = nomPropose; } - if (typeof enriched.ville === 'string' && enriched.ville && !row.localisation_ville) { - updateData.localisation_ville = enriched.ville; + const villeProposee = texteBorne(enriched.ville, MAX_COLONNES.localisation_ville); + if (villeProposee && !row.localisation_ville) { + updateData.localisation_ville = villeProposee; } // submission_type : ne réajuster que si l'utilisateur n'avait pas choisi de chip // (marqueur posé par utils/submitLibre.ts::toOrgaPayload — "Type : non précisé"). diff --git a/worker/lib/extraction.js b/worker/lib/extraction.js index 765850f..c304309 100644 --- a/worker/lib/extraction.js +++ b/worker/lib/extraction.js @@ -24,6 +24,21 @@ function texte(v, max = MAX_NOM) { return s ? s.slice(0, max) : null; } +/** + * Champs du modèle écrits tels quels dans une colonne (checkpoint 22/11) : bornés en longueur. + * Coupés en points de code (jamais au milieu d'un caractère) ; seuls les bords sont rognés. + */ +export const MAX_COLONNES = { description_enrichie: 300, nom: 200, localisation_ville: 100 }; + +/** Texte du modèle destiné à une colonne → chaîne bornée à `max` caractères, ou null (vide compris). */ +export function texteBorne(v, max) { + if (typeof v !== 'string') return null; + const s = v.trim(); + if (!s) return null; + const car = Array.from(s); + return car.length > max ? car.slice(0, max).join('').trimEnd() : s; +} + function listeTextes(v, max = MAX_LISTE) { if (!Array.isArray(v)) return []; return [...new Set(v.map(x => texte(x)).filter(Boolean))].slice(0, max); diff --git a/worker/test/branchement.test.js b/worker/test/branchement.test.js index f3088ef..ce4f98e 100644 --- a/worker/test/branchement.test.js +++ b/worker/test/branchement.test.js @@ -365,3 +365,18 @@ describe('checkpoint 22/11 · pagination de la base de R6 (correction 3)', () => assert.equal(r.finale(401).sortie.r6.cible.id, 'fiche:2000'); }); }); + +describe('checkpoint 22/11 · sortie du modèle bornée avant la colonne (correction 4)', () => { + it('description 300, nom 200, ville 100 : jamais plus long dans le PATCH', () => { + const r = jouer({ + lignes: [ficheDepuis({ soumis: { url: 'https://exemple.org' } }, 501)], + base: [], + modele: { 501: { nom: 'N'.repeat(450), description: 'D'.repeat(1200), ville: 'V'.repeat(400), objet: 'Exemple', porteurs: [], designations: [], confiance: 'haute' } }, + }); + const f = r.finale(501); + assert.equal(f.description_enrichie.length, 300); + assert.equal(f.nom.length, 200); + assert.equal(f.localisation_ville.length, 100); + champsTenus(r.patchs); + }); +}); diff --git a/worker/test/proposition.test.js b/worker/test/proposition.test.js index 1fb62fc..4f38d4d 100644 --- a/worker/test/proposition.test.js +++ b/worker/test/proposition.test.js @@ -8,7 +8,7 @@ import { readFileSync } from 'node:fs'; import { dirname, join } from 'node:path'; import { fileURLToPath } from 'node:url'; import { TYPES_ORGANISATION, POSTURES, LIBELLES_TYPE, LIBELLES_POSTURE, proposerType, proposerPostures } from '../lib/taxonomie.js'; -import { extractionDepuisModele } from '../lib/extraction.js'; +import { extractionDepuisModele, texteBorne, MAX_COLONNES } from '../lib/extraction.js'; const ici = dirname(fileURLToPath(import.meta.url)); const SITE = readFileSync(join(ici, '..', '..', 'utils', 'taxonomie.ts'), 'utf-8'); @@ -98,3 +98,13 @@ describe('lot 15 · extraction rendue par le modèle, bornée', () => { } }); }); + +describe('checkpoint 22/11 · textes du modèle écrits en colonne, bornés (correction 4)', () => { + it('300, 200 et 100 caractères ; coupe en points de code ; vide et non-texte → null', () => { + assert.deepEqual(MAX_COLONNES, { description_enrichie: 300, nom: 200, localisation_ville: 100 }); + assert.equal(texteBorne('a'.repeat(500), 300).length, 300); + assert.equal(texteBorne(' Lyon ', 100), 'Lyon'); + assert.equal(texteBorne('é'.repeat(99) + '😀😀', 100), 'é'.repeat(99) + '😀', 'jamais la moitié d’un émoji'); + for (const v of [null, undefined, 42, {}, ['x'], '', ' ']) assert.equal(texteBorne(v, 100), null); + }); +});