scripts/porter-taxonomie-reseaux.mjs (rejouable, --verifier) recopie tels quels type_organisation, postures et taxonomie_source (= source) depuis data/taxonomie/organisations.json. classement(id, fiche, mapping) lit le champ de la fiche d'abord, le mapping en repli ; toMini passe la structure (les Org restent sur le mapping jusqu'à B4b-2). Le mapping reste comme trace (README). test-taxonomie : égalité 120 sur 120, classement identique fiche/mapping, repli, vocabulaire (44/44). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PWGVn2kurLPzxVc5F3mQJ5
129 lines
9.9 KiB
JavaScript
129 lines
9.9 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`.
|
||
* 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')
|
||
ok(eq(A.toMini({ ...org, type_organisation: 'media' }, T.classement).etiquettes.type, ['institution']), 'toMini(org) : toujours le mapping (NocoDB sans colonne avant B4b-2)')
|
||
|
||
console.log(`\n${n - fail}/${n}`)
|
||
if (fail) process.exit(1)
|