Règle posée (CKPT-AEP-1211-B4b-RECAP §4 point 4) : une posture lue dans NocoDB s'affiche sans source par posture ; la source reste au mapping. Une ligne au type vide retombe sur le mapping pour les deux colonnes. Recoupé sur la base réelle : 91 lignes, 0 écart avec le mapping. Suite taxonomie 41 → 56. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
156 lines
13 KiB
JavaScript
156 lines
13 KiB
JavaScript
#!/usr/bin/env node
|
||
/**
|
||
* Test de la taxonomie de la carte (B4a-M3) : vocabulaire (taxonomie N), mapping
|
||
* `data/taxonomie/organisations.json` complet (91 org + 120 structures = 211 identifiants),
|
||
* aucune valeur hors vocabulaire, aucune posture sans source, repli `autre` pour un
|
||
* identifiant absent, et les étiquettes `type` / `postures` posées par `toMini`.
|
||
* B4b-3 : les Org lisent leurs colonnes NocoDB (postures sans source), le mapping en repli.
|
||
* Charge les VRAIS fichiers via jiti (même patron que test-vue-fiches.mjs).
|
||
* Usage : node scripts/test-taxonomie.mjs
|
||
*/
|
||
|
||
import { createJiti } from 'jiti'
|
||
import { fileURLToPath } from 'node:url'
|
||
import { dirname, join } from 'node:path'
|
||
import { readFileSync } from 'node:fs'
|
||
|
||
const __dirname = dirname(fileURLToPath(import.meta.url))
|
||
const racine = join(__dirname, '..')
|
||
const jiti = createJiti(import.meta.url)
|
||
const T = await jiti.import(join(racine, 'utils', 'taxonomie.ts'))
|
||
const A = await jiti.import(join(racine, 'composables', 'useFicheAdapter.ts'))
|
||
const reseaux = JSON.parse(readFileSync(join(racine, 'public', 'data', 'reseaux-bifurcation.json'), 'utf8'))
|
||
|
||
let n = 0
|
||
let fail = 0
|
||
function ok(cond, libelle, detail = '') {
|
||
n++
|
||
if (!cond) fail++
|
||
console.log(`${cond ? 'OK ' : 'FAIL'} ${libelle}${!cond && detail ? ` → ${detail}` : ''}`)
|
||
}
|
||
const eq = (a, b) => JSON.stringify(a) === JSON.stringify(b)
|
||
|
||
// ── Vocabulaire : les sept types et les sept postures de N, un libellé chacun ──
|
||
ok(eq([...T.TYPES_ORGANISATION], ['institution', 'association', 'syndicat', 'agence', 'formation', 'media', 'autre']), 'types : les sept valeurs de N, dans l’ordre')
|
||
ok(eq([...T.POSTURES], ['regenerative', 'bas-carbone', 'cooperative', 'patrimoine', 'ecoconstruction', 'low-tech', 'participatif']), 'postures : les sept valeurs de N, dans l’ordre')
|
||
ok(T.TYPES_ORGANISATION.every((t) => T.LIBELLES_TYPE[t]?.length > 0) && Object.keys(T.LIBELLES_TYPE).length === 7, 'un libellé par type, pas un de plus')
|
||
ok(T.POSTURES.every((p) => T.LIBELLES_POSTURE[p]?.length > 0) && Object.keys(T.LIBELLES_POSTURE).length === 7, 'un libellé par posture, pas un de plus')
|
||
ok(T.LIBELLES_TYPE.media === 'Média' && T.LIBELLES_POSTURE.ecoconstruction === 'Écoconstruction', 'libellés accentués')
|
||
|
||
// ── Mapping complet : 211 identifiants ──
|
||
const ids = Object.keys(T.MAPPING.entrees)
|
||
const orgIds = ids.filter((id) => id.startsWith('org:'))
|
||
const structIds = ids.filter((id) => id.startsWith('structure:'))
|
||
ok(ids.length === 211, `211 entrées (${ids.length})`)
|
||
ok(orgIds.length === 91, `91 structures d'entraide org:<Id> (${orgIds.length})`)
|
||
ok(structIds.length === 120, `120 réseaux et agences structure:<id> (${structIds.length})`)
|
||
const attendues = reseaux.structures.map((s) => T.idStructure(s.id))
|
||
const manquantes = attendues.filter((id) => !T.MAPPING.entrees[id])
|
||
ok(manquantes.length === 0, 'chaque structure de reseaux-bifurcation.json a son entrée', manquantes.join(', '))
|
||
ok(structIds.every((id) => attendues.includes(id)), 'aucune entrée structure: sans structure dans le JSON')
|
||
ok(orgIds.every((id) => /^org:\d+$/.test(id)), 'identifiants org: numériques (Id NocoDB)')
|
||
|
||
// ── La règle : vocabulaire, sources ──
|
||
const ecarts = T.ecartsMapping(T.MAPPING)
|
||
ok(ecarts.length === 0, 'mapping conforme : types et postures dans le vocabulaire, une source par valeur', ecarts.slice(0, 5).join(' | '))
|
||
const valeursType = new Set(Object.values(T.MAPPING.entrees).map((e) => e.type_organisation))
|
||
ok([...valeursType].every((v) => T.TYPES_ORGANISATION.includes(v)), `aucun type hors vocabulaire (${[...valeursType].join(', ')})`)
|
||
const sansSource = Object.entries(T.MAPPING.entrees).flatMap(([id, e]) => e.postures.filter((p) => !e.source.postures[p]).map((p) => `${id}/${p}`))
|
||
ok(sansSource.length === 0, 'aucune posture sans source', sansSource.join(', '))
|
||
ok(Object.values(T.MAPPING.entrees).every((e) => typeof e.nom === 'string' && e.nom.length > 0), 'chaque entrée porte le nom de la fiche (pour qui corrige à la main)')
|
||
|
||
// L'outil de contrôle lui-même attrape les fautes
|
||
const faux = {
|
||
version: 1,
|
||
entrees: {
|
||
'org:1': { type_organisation: 'ong', postures: [], source: { type_organisation: 'x', postures: {} } },
|
||
'org:2': { type_organisation: 'agence', postures: ['low-tech'], source: { type_organisation: 'x', postures: {} } },
|
||
'org:3': { type_organisation: 'agence', postures: ['verte'], source: { type_organisation: 'x', postures: { verte: 'y' } } },
|
||
'org:4': { type_organisation: 'agence', postures: [], source: { type_organisation: '', postures: {} } },
|
||
'Org 5': { type_organisation: 'agence', postures: [], source: { type_organisation: 'x', postures: {} } },
|
||
},
|
||
}
|
||
const e = T.ecartsMapping(faux)
|
||
ok(e.some((x) => x.startsWith('org:1') && x.includes('hors vocabulaire')), 'contrôle : type hors vocabulaire attrapé')
|
||
ok(e.some((x) => x.startsWith('org:2') && x.includes('sans source')), 'contrôle : posture sans source attrapée')
|
||
ok(e.some((x) => x.startsWith('org:3') && x.includes('hors vocabulaire')), 'contrôle : posture hors vocabulaire attrapée')
|
||
ok(e.some((x) => x.startsWith('org:4') && x.includes('type sans source')), 'contrôle : type sans source attrapé')
|
||
ok(e.some((x) => x.startsWith('Org 5') && x.includes('hors forme')), 'contrôle : identifiant hors forme attrapé')
|
||
|
||
// ── Lecture : classement et repli ──
|
||
ok(eq(T.classement('org:999999'), { type: 'autre', postures: [] }), 'identifiant absent → autre, sans posture')
|
||
ok(eq(T.classement('structure:inconnue'), { type: 'autre', postures: [] }), 'structure absente → autre')
|
||
ok(eq(T.classement('org:1', null, faux), { type: 'autre', postures: [] }), 'type hors vocabulaire → autre')
|
||
ok(eq(T.classement('org:2', null, faux).postures, []), 'posture sans source → pas affichée')
|
||
ok(eq(T.classement('org:3', null, faux).postures, []), 'posture hors vocabulaire → ignorée')
|
||
const rotor = T.classement('structure:f1-rotor')
|
||
ok(T.TYPES_ORGANISATION.includes(rotor.type) && rotor.postures.every((p) => T.POSTURES.includes(p)), `structure:f1-rotor lue (${rotor.type} · ${rotor.postures.join(', ')})`)
|
||
ok(T.classement('org:106').type === 'institution', 'org:106 (CNOA) → institution (nom : Ordre)')
|
||
ok(T.classement('org:37').type === 'syndicat', 'org:37 (UNSFA) → syndicat (nom)')
|
||
ok(eq(T.valeursPresentes(T.POSTURES, T.LIBELLES_POSTURE, ['participatif', 'bas-carbone', 'x']).map((v) => v.valeur), ['bas-carbone', 'participatif']), 'valeursPresentes : ordre du vocabulaire, inconnues ignorées')
|
||
|
||
// ── useFicheAdapter : étiquettes type et postures ──
|
||
const s = reseaux.structures.find((x) => x.id === 'f1-rotor')
|
||
const mS = A.toMini(s, T.classement)
|
||
ok(eq(mS.etiquettes.type, [rotor.type]) && eq(mS.etiquettes.postures, rotor.postures), 'toMini(structure, classement) : étiquettes type et postures')
|
||
ok(mS.etiquettes.tags.length > 0 && mS.etiquettes.famille.length > 0, 'toMini : les étiquettes d’avant restent (tags, famille)')
|
||
const org = { Id: 106, nom: 'CNOA', echelle: 'National', tags_fonction: 'Juridique', territoire: 'Métropole' }
|
||
const mO = A.toMini(org, T.classement)
|
||
ok(eq(mO.etiquettes.type, ['institution']) && eq(mO.etiquettes.fonctions, ['Juridique']), 'toMini(org, classement) : type posé, fonctions inchangées')
|
||
ok(eq(A.toMini({ ...org, Id: 424242 }, T.classement).etiquettes.type, ['autre']), 'toMini : fiche absente du mapping → type autre')
|
||
ok(!('type' in A.toMini(org).etiquettes) && !('type' in [org].map(A.toMini)[0].etiquettes), 'toMini sans classeur (ou avec l’index de map) : rien d’ajouté')
|
||
|
||
// ── B4b-M3 : la taxonomie portée par les structures, un déplacement sans changement ──
|
||
const S = reseaux.structures
|
||
const egales = S.filter((x) => {
|
||
const ent = T.MAPPING.entrees[T.idStructure(x.id)]
|
||
return ent && x.type_organisation === ent.type_organisation && eq(x.postures, ent.postures) && eq(x.taxonomie_source, ent.source)
|
||
})
|
||
ok(S.length === 120 && egales.length === 120, `égalité mapping ↔ JSON des réseaux : type, postures et source recopiés tels quels (${egales.length} sur ${S.length})`,
|
||
S.filter((x) => !egales.includes(x)).slice(0, 5).map((x) => x.id).join(', '))
|
||
const memes = S.filter((x) => eq(T.classement(T.idStructure(x.id), x), T.classement(T.idStructure(x.id))))
|
||
ok(memes.length === 120, `classement lu sur la fiche = classement lu dans le mapping (${memes.length} sur 120)`)
|
||
ok(S.every((x) => T.TYPES_ORGANISATION.includes(x.type_organisation) && x.postures.every((p) => T.POSTURES.includes(p) && x.taxonomie_source.postures[p])), 'champs portés : type et postures dans le vocabulaire, une source par posture')
|
||
// Le champ de la fiche passe avant le mapping ; sans champ, repli sur le mapping ; ni l'un ni l'autre → autre
|
||
const porte = { type_organisation: 'media', postures: ['low-tech', 'verte', 'participatif'], taxonomie_source: { type_organisation: 'test', postures: { 'low-tech': 'test', verte: 'test' } } }
|
||
ok(eq(T.classement('structure:f1-rotor', porte), { type: 'media', postures: ['low-tech'] }), 'champ de la fiche d’abord : type lu, posture hors vocabulaire ou sans source ignorée')
|
||
ok(eq(T.classement('structure:f1-rotor', { nom: 'Rotor' }), rotor), 'fiche sans champ → repli sur le mapping')
|
||
ok(eq(T.classement('structure:ajoutee-apres', { nom: 'Nouvelle' }), { type: 'autre', postures: [] }), 'fiche ajoutée plus tard, sans champ ni entrée → autre')
|
||
ok(eq(T.classement('structure:f1-rotor', { type_organisation: 'ong', postures: [] }), { type: 'autre', postures: [] }), 'champ de la fiche hors vocabulaire → autre (pas de repli silencieux vers le mapping)')
|
||
const sansChamp = { ...s }
|
||
delete sansChamp.type_organisation
|
||
delete sansChamp.postures
|
||
delete sansChamp.taxonomie_source
|
||
ok(eq(A.toMini(sansChamp, T.classement).etiquettes.type, [rotor.type]), 'toMini : structure sans champ → étiquette du mapping')
|
||
ok(eq(A.toMini({ ...s, type_organisation: 'media' }, T.classement).etiquettes.type, ['media']), 'toMini : structure avec champ → étiquette du champ')
|
||
|
||
// ── B4b-3 : les Org lisent leurs colonnes NocoDB, le mapping en repli ──
|
||
// Fausse réponse NocoDB = les 91 lignes telles que la migration du 12/11 les a écrites
|
||
// (type depuis le mapping, postures en texte « a,b »).
|
||
const lignes = orgIds.map((id) => {
|
||
const ent = T.MAPPING.entrees[id]
|
||
return { Id: Number(id.slice(4)), nom: ent.nom, territoire: 'Métropole', type_organisation: ent.type_organisation, postures: ent.postures.join(',') }
|
||
})
|
||
const typeDe = (fiches) => fiches.map((f) => f.etiquettes.type[0])
|
||
const chips = (fiches, dim) => T.valeursPresentes(dim === 'type' ? T.TYPES_ORGANISATION : T.POSTURES, dim === 'type' ? T.LIBELLES_TYPE : T.LIBELLES_POSTURE, fiches.flatMap((f) => f.etiquettes[dim])).map((v) => v.libelle)
|
||
const avant = lignes.map(({ type_organisation, postures, ...reste }) => A.toMini(reste, T.classement))
|
||
const apres = lignes.map((l) => A.toMini(l, T.classement))
|
||
ok(eq(typeDe(apres), typeDe(avant)) && eq(apres.map((f) => f.etiquettes.postures), avant.map((f) => f.etiquettes.postures)), `NocoDB rempli comme le 12/11 : type et postures identiques au mapping, fiche par fiche (${apres.length})`)
|
||
ok(eq(chips(apres, 'type'), chips(avant, 'type')) && chips(apres, 'type').length === 7, `les sept chips Type inchangées (${chips(apres, 'type').join(', ')})`)
|
||
ok(eq(chips(apres, 'postures'), chips(avant, 'postures')), 'chips Posture inchangées')
|
||
const change = lignes.map((l) => (l.Id === 106 ? { ...l, type_organisation: 'media', postures: 'low-tech' } : l)).map((l) => A.toMini(l, T.classement))
|
||
const cnoa = change.find((f) => f.id === 106)
|
||
ok(eq(cnoa.etiquettes.type, ['media']) && eq(cnoa.etiquettes.postures, ['low-tech']), 'une valeur changée dans NocoDB change la chip (org:106 institution → Média, posture Low-tech sans source)')
|
||
ok(change.filter((f) => f.id !== 106).every((f, i) => eq(f.etiquettes.type, apres.filter((x) => x.id !== 106)[i].etiquettes.type)), 'les 90 autres fiches ne bougent pas')
|
||
ok(eq(T.classement('org:106', { type_organisation: 'agence', postures: ['participatif', 'verte'] }), { type: 'agence', postures: ['participatif'] }), 'postures en liste acceptées ; hors vocabulaire ignorée')
|
||
ok(eq(T.classement('org:106', { type_organisation: 'agence', postures: ' participatif , bas-carbone ' }).postures, ['bas-carbone', 'participatif']), 'postures en texte : espaces retirés, ordre du vocabulaire')
|
||
ok(eq(T.classement('org:106', { type_organisation: 'ong', postures: 'low-tech' }), { type: 'autre', postures: ['low-tech'] }), 'type NocoDB hors vocabulaire → autre (pas de repli silencieux)')
|
||
ok(eq(T.classement('org:106', { type_organisation: 'agence', postures: null }).postures, []), 'postures vides dans NocoDB → aucune (le mapping ne revient pas)')
|
||
ok(eq(T.classement('org:106', { type_organisation: '', postures: 'low-tech' }), T.classement('org:106')), 'type vide dans NocoDB → repli sur le mapping pour les deux colonnes')
|
||
ok(eq(T.classement('org:106', { type_organisation: null }), T.classement('org:106')), 'colonnes absentes ou nulles → mapping')
|
||
ok(eq(T.classement('structure:f1-rotor', { type_organisation: 'media', postures: 'low-tech' }).postures, []), 'une structure garde sa règle : posture sans source non affichée')
|
||
ok(eq(T.posturesNocodb(undefined), []) && eq(T.posturesNocodb(42), []) && eq(T.posturesNocodb(''), []), 'posturesNocodb : vide ou forme inconnue → aucune')
|
||
|
||
console.log(`\n${n - fail}/${n}`)
|
||
if (fail) process.exit(1)
|