feat(scripts): B4b-2 préparé, migrer-taxonomie-nocodb.mjs (jamais lancé contre la base)

Lecture seule par défaut : plan des 91 org:<Id> (Id, nom, type, postures)
dans taxonomie-nocodb-plan.json (dossier temporaire ou --plan). Avec
NOCODB_URL / NOCODB_TOKEN : à blanc (colonnes, Id, noms vérifiés). Avec
--ecrire : ligne à ligne, relecture, bilan (écrites, sautées, différentes).
Ni adresse ni jeton journalisés. test-migrer-taxonomie : fausse API locale,
trois lignes dont un Id absent et un nom changé (16/16).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PWGVn2kurLPzxVc5F3mQJ5
This commit is contained in:
Jules Neny
2026-11-12 09:18:57 +01:00
co-authored by Claude Opus 5.5
parent e2ce82dc7f
commit 2617c7d0f1
3 changed files with 280 additions and 1 deletions
+124
View File
@@ -0,0 +1,124 @@
#!/usr/bin/env node
/**
* Test de scripts/migrer-taxonomie-nocodb.mjs (B4b-2, préparé en B4b-1) contre une fausse
* API NocoDB v2 locale : trois lignes, dont un Id absent et un nom changé ; à blanc rien
* n'est écrit ; --ecrire écrit la seule ligne sûre et la relit ; une colonne manquante
* bloque tout ; le jeton n'apparaît jamais dans le journal. Plan réel : 91 lignes org:<Id>.
* Aucune base réelle n'est touchée.
* Usage : node scripts/test-migrer-taxonomie.mjs
*/
import { createServer } from 'node:http'
import { readFileSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import { dirname, join } from 'node:path'
import { plan, migrer } from './migrer-taxonomie-nocodb.mjs'
const racine = join(dirname(fileURLToPath(import.meta.url)), '..')
let n = 0
let fail = 0
function ok(cond, libelle, detail = '') {
n++
if (!cond) fail++
console.log(`${cond ? 'OK ' : 'FAIL'} ${libelle}${!cond && detail ? ` → ${detail}` : ''}`)
}
// ── Le plan réel ──
const mapping = JSON.parse(readFileSync(join(racine, 'data', 'taxonomie', 'organisations.json'), 'utf8'))
const p = plan(mapping)
ok(p.length === 91, `plan : 91 lignes org:<Id> (${p.length})`)
ok(p.every((l) => Number.isInteger(l.Id) && l.nom && l.type_organisation && Array.isArray(l.postures)), 'plan : Id, nom, type_organisation, postures sur chaque ligne')
ok(p.every((l, i) => i === 0 || p[i - 1].Id < l.Id), 'plan : trié par Id')
ok(!p.some((l) => 'source' in l), 'plan : rien d’autre que les quatre champs')
// ── Fausse API NocoDB ──
const JETON = 'jeton-de-test-secret'
const TABLE = 'tfaux'
function fausseBase({ colonnes = ['Id', 'nom', 'type_org', 'type_organisation', 'postures'] } = {}) {
const lignes = new Map([
[1, { Id: 1, nom: 'Collectif A', type_organisation: null, postures: null }],
[3, { Id: 3, nom: 'Nouveau nom C', type_organisation: null, postures: null }],
])
const ecritures = []
const serveur = createServer((req, res) => {
const envoyer = (statut, corps) => { res.writeHead(statut, { 'Content-Type': 'application/json' }); res.end(JSON.stringify(corps)) }
if (req.headers['xc-token'] !== JETON) return envoyer(401, { msg: 'jeton' })
const u = new URL(req.url, 'http://x')
if (req.method === 'GET' && u.pathname === `/api/v2/meta/tables/${TABLE}`) return envoyer(200, { columns: colonnes.map((title) => ({ title, column_name: title })) })
const m = u.pathname.match(new RegExp(`^/api/v2/tables/${TABLE}/records(?:/(\\d+))?$`))
if (!m) return envoyer(404, {})
if (req.method === 'GET' && m[1]) {
const l = lignes.get(Number(m[1]))
return l ? envoyer(200, l) : envoyer(404, { msg: 'Record not found' })
}
if (req.method === 'PATCH' && !m[1]) {
let corps = ''
req.on('data', (c) => (corps += c))
req.on('end', () => {
for (const w of JSON.parse(corps)) {
ecritures.push(w)
const l = lignes.get(w.Id)
if (l) Object.assign(l, w)
}
envoyer(200, JSON.parse(corps).map((w) => ({ Id: w.Id })))
})
return
}
envoyer(405, {})
})
return new Promise((r) => serveur.listen(0, '127.0.0.1', () => r({ serveur, lignes, ecritures, url: `http://127.0.0.1:${serveur.address().port}` })))
}
const trois = [
{ Id: 1, nom: 'Collectif A', type_organisation: 'association', postures: ['participatif', 'cooperative'] },
{ Id: 2, nom: 'Agence B', type_organisation: 'agence', postures: [] },
{ Id: 3, nom: 'Ancien nom C', type_organisation: 'syndicat', postures: [] },
]
// À blanc
{
const f = await fausseBase()
const journal = []
const b = await migrer({ lignes: trois, url: f.url, token: JETON, table: TABLE, ecrire: false, journal: (l) => journal.push(l), pause: 0 })
ok(f.ecritures.length === 0, 'à blanc : aucune écriture')
ok(JSON.stringify(b.a_ecrire) === '[1]', `à blanc : la ligne 1 serait écrite (${JSON.stringify(b.a_ecrire)})`)
ok(b.sautees.length === 2 && b.sautees.some((s) => s.Id === 2 && /absent/.test(s.raison)) && b.sautees.some((s) => s.Id === 3 && /nom changé/.test(s.raison)), 'à blanc : Id absent et nom changé sautés et listés')
f.serveur.close()
}
// Écriture
{
const f = await fausseBase()
const journal = []
const b = await migrer({ lignes: trois, url: f.url + '/', token: JETON, table: TABLE, ecrire: true, journal: (l) => journal.push(l), pause: 0 })
ok(f.ecritures.length === 1 && f.ecritures[0].Id === 1, 'écriture : une seule ligne écrite, la ligne sûre', JSON.stringify(f.ecritures))
ok(f.ecritures[0]?.postures === 'participatif,cooperative' && f.ecritures[0]?.type_organisation === 'association', 'écriture : choix multiple en « a,b », type tel quel')
ok(JSON.stringify(b.ecrites) === '[1]' && b.differentes.length === 0, 'écriture : relue identique, bilan 1 écrite, 0 différente')
ok(b.sautees.length === 2, 'écriture : Id absent et nom changé sautés (2)')
ok(f.lignes.get(3).type_organisation === null, 'écriture : la ligne au nom changé n’est pas touchée')
ok(journal.every((l) => !l.includes(JETON) && !l.includes('127.0.0.1')), 'journal : ni jeton ni adresse')
const b2 = await migrer({ lignes: trois, url: f.url, token: JETON, table: TABLE, ecrire: true, pause: 0 })
ok(b2.ecrites.length === 0 && JSON.stringify(b2.deja_a_jour) === '[1]' && f.ecritures.length === 1, 'relancée : rien de réécrit, ligne 1 déjà à jour')
f.serveur.close()
}
// Une colonne manquante bloque tout
{
const f = await fausseBase({ colonnes: ['Id', 'nom', 'type_org', 'type_organisation'] })
const b = await migrer({ lignes: trois, url: f.url, token: JETON, table: TABLE, ecrire: true, pause: 0 })
ok(JSON.stringify(b.colonnes_manquantes) === '["postures"]' && f.ecritures.length === 0, 'colonne postures absente : rien n’est écrit')
f.serveur.close()
}
// Relecture différente (la base ignore une colonne) → listée
{
const f = await fausseBase()
const origine = f.lignes.get(1)
Object.defineProperty(origine, 'postures', { get: () => null, set: () => {}, enumerable: true })
const b = await migrer({ lignes: trois, url: f.url, token: JETON, table: TABLE, ecrire: true, pause: 0 })
ok(b.differentes.length === 1 && b.differentes[0].Id === 1 && b.ecrites.length === 0, 'relue différente de l’écriture → listée « différente »')
f.serveur.close()
}
console.log(`\n${n - fail}/${n}`)
if (fail) process.exit(1)