Checkpoint 22/11, correction 4. Ces trois textes n'étaient bornés que par leur type. lib/extraction.js::texteBorne les coupe à 300, 200 et 100 caractères (en points de code), une chaîne vide vaut null. Antérieur au lot, mais le lot touche ces lignes. Tests : texteBorne seul, et un PATCH à blanc où le modèle rend 1 200 / 450 / 400 caractères. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
111 lines
5.6 KiB
JavaScript
111 lines
5.6 KiB
JavaScript
/**
|
||
* Lot 15 — la proposition de type et de postures reste dans le vocabulaire du site (sinon null),
|
||
* et l'extraction rendue par le modèle est bornée avant d'entrer dans R6 et R3.
|
||
*/
|
||
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 { TYPES_ORGANISATION, POSTURES, LIBELLES_TYPE, LIBELLES_POSTURE, proposerType, proposerPostures } from '../lib/taxonomie.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');
|
||
|
||
/** Lit `export const NOM = [ ... ] as const` dans le fichier du site. */
|
||
function tableauDuSite(nom) {
|
||
const m = SITE.match(new RegExp(`export const ${nom} = \\[([^\\]]*)\\]`));
|
||
assert.ok(m, `${nom} introuvable dans utils/taxonomie.ts`);
|
||
return [...m[1].matchAll(/'([^']+)'/g)].map(x => x[1]);
|
||
}
|
||
/** Lit `export const NOM: Record<…> = { clé: 'libellé', … }` dans le fichier du site. */
|
||
function libellesDuSite(nom) {
|
||
const m = SITE.match(new RegExp(`export const ${nom}[^=]*= \\{([^}]*)\\}`));
|
||
assert.ok(m, `${nom} introuvable dans utils/taxonomie.ts`);
|
||
return Object.fromEntries([...m[1].matchAll(/'?([a-z-]+)'?:\s*'([^']+)'/g)].map(x => [x[1], x[2]]));
|
||
}
|
||
|
||
describe('lot 15 · vocabulaire du worker = vocabulaire du site (utils/taxonomie.ts)', () => {
|
||
it('sept types, sept postures, mêmes valeurs dans le même ordre', () => {
|
||
assert.deepEqual(TYPES_ORGANISATION, tableauDuSite('TYPES_ORGANISATION'));
|
||
assert.deepEqual(POSTURES, tableauDuSite('POSTURES'));
|
||
assert.equal(TYPES_ORGANISATION.length, 7);
|
||
assert.equal(POSTURES.length, 7);
|
||
});
|
||
|
||
it('mêmes libellés', () => {
|
||
assert.deepEqual(LIBELLES_TYPE, libellesDuSite('LIBELLES_TYPE'));
|
||
assert.deepEqual(LIBELLES_POSTURE, libellesDuSite('LIBELLES_POSTURE'));
|
||
});
|
||
});
|
||
|
||
describe('lot 15 · type et postures proposés', () => {
|
||
it('type : valeur ou libellé du vocabulaire, sinon null', () => {
|
||
assert.equal(proposerType('association'), 'association');
|
||
assert.equal(proposerType('Média'), 'media');
|
||
assert.equal(proposerType(' Syndicat '), 'syndicat');
|
||
for (const hors of ['collectif', 'ecosysteme', 'reseau', 'Association loi 1901', '', null, undefined, 42, ['association'], { type: 'agence' }]) {
|
||
assert.equal(proposerType(hors), null, JSON.stringify(hors));
|
||
}
|
||
});
|
||
|
||
it('postures : seules celles du vocabulaire, sans doublon, dans l’ordre du site', () => {
|
||
assert.deepEqual(proposerPostures(['Low tech', 'participatif', 'décroissance', 'Coopératif', 'participatif', 7]), ['cooperative', 'low-tech', 'participatif']);
|
||
assert.deepEqual(proposerPostures('regenerative'), []);
|
||
assert.deepEqual(proposerPostures(null), []);
|
||
assert.deepEqual(proposerPostures(['Bas carbone', 'Écoconstruction', 'Régénératif']), ['regenerative', 'bas-carbone', 'ecoconstruction']);
|
||
});
|
||
});
|
||
|
||
describe('lot 15 · extraction rendue par le modèle, bornée', () => {
|
||
it('champs hors format → null ou liste vide ; indices booléens seulement', () => {
|
||
const x = extractionDepuisModele({
|
||
objet: ' Portal.Place ', variantes: ['Portal Place', 'Portal Place', 3], porteurs: 'Mike Gilliland',
|
||
point_de_vue: 'officiel', confiance: 'certaine',
|
||
designations: [
|
||
{ nom: 'Portal.Place', indices: { agit: true, peut_cesser: 'oui', methode: false, inconnu: true }, date: 'hier' },
|
||
{ indices: { agit: true } },
|
||
'texte',
|
||
],
|
||
}, { avecPage: true });
|
||
assert.equal(x.objet, 'Portal.Place');
|
||
assert.deepEqual(x.variantes, ['Portal Place']);
|
||
assert.deepEqual(x.porteurs, []);
|
||
assert.equal(x.point_de_vue, null);
|
||
assert.equal(x.confiance, 'faible', 'confiance hors vocabulaire → faible');
|
||
assert.equal(x.designations.length, 1);
|
||
assert.deepEqual(x.designations[0].indices, { agit: true, methode: false });
|
||
assert.equal(x.designations[0].date, null);
|
||
});
|
||
|
||
it('sans page, la confiance est faible quoi que dise le modèle', () => {
|
||
assert.equal(extractionDepuisModele({ objet: 'X', confiance: 'haute' }, { avecPage: false }).confiance, 'faible');
|
||
assert.equal(extractionDepuisModele({ objet: 'X', confiance: 'haute' }, { avecPage: true }).confiance, 'haute');
|
||
});
|
||
|
||
it('au plus cinq désignations, noms bornés à 200 caractères', () => {
|
||
const x = extractionDepuisModele({ objet: 'a'.repeat(500), designations: Array.from({ length: 9 }, (_, i) => ({ nom: `d${i}`, indices: {} })) }, { avecPage: true });
|
||
assert.equal(x.objet.length, 200);
|
||
assert.equal(x.designations.length, 5);
|
||
});
|
||
|
||
it('sortie absente ou illisible → extraction vide, jamais d’exception', () => {
|
||
for (const s of [null, undefined, 'texte', 42, []]) {
|
||
const x = extractionDepuisModele(s, { avecPage: true });
|
||
assert.equal(x.objet, null);
|
||
assert.deepEqual(x.designations, []);
|
||
}
|
||
});
|
||
});
|
||
|
||
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);
|
||
});
|
||
});
|