|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * test-model.mjs |
| 4 | + * ────────────── |
| 5 | + * Test di integrità del dato e di comportamento di buildGraph. |
| 6 | + * Non scarica nulla: gira offline su src/data/pdnd-data.json. |
| 7 | + * |
| 8 | + * Uso: npm test |
| 9 | + * Exit: 0 se tutti i test passano, 1 al primo fallimento. |
| 10 | + * |
| 11 | + * Copre in particolare la regressione della provenienza: il dato curato (v1) |
| 12 | + * non porta il campo `archi`, quindi buildGraph applica un fallback. Quel |
| 13 | + * fallback NON deve dichiarare gli archi come "certificata", perché non sono |
| 14 | + * derivati dal campo attributes del catalogo ma ricostruiti da documentazione. |
| 15 | + */ |
| 16 | + |
| 17 | +import fs from "fs"; |
| 18 | +import path from "path"; |
| 19 | +import { fileURLToPath } from "url"; |
| 20 | + |
| 21 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 22 | +const ROOT = path.join(__dirname, ".."); |
| 23 | + |
| 24 | +const DATA = JSON.parse( |
| 25 | + fs.readFileSync(path.join(ROOT, "src/data/pdnd-data.json"), "utf8") |
| 26 | +); |
| 27 | + |
| 28 | +// buildGraph è un modulo ES con export nominato: lo importiamo direttamente. |
| 29 | +const { buildGraph } = await import( |
| 30 | + path.join(ROOT, "src/utils/buildGraph.js") |
| 31 | +); |
| 32 | + |
| 33 | +let passed = 0; |
| 34 | +const failures = []; |
| 35 | + |
| 36 | +function check(name, fn) { |
| 37 | + try { |
| 38 | + const res = fn(); |
| 39 | + if (res === true || res === undefined) { |
| 40 | + passed++; |
| 41 | + console.log(` ok ${name}`); |
| 42 | + } else { |
| 43 | + failures.push(`${name}: ${res}`); |
| 44 | + console.log(` FAIL ${name}: ${res}`); |
| 45 | + } |
| 46 | + } catch (err) { |
| 47 | + failures.push(`${name}: ${err.message}`); |
| 48 | + console.log(` FAIL ${name}: ${err.message}`); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +console.log("\nIntegrità del dato (src/data/pdnd-data.json)"); |
| 53 | + |
| 54 | +const ids = new Set(DATA.enti.map((e) => e.id)); |
| 55 | + |
| 56 | +check("ogni ente ha id, name e categoria", () => { |
| 57 | + const bad = DATA.enti.filter((e) => !e.id || !e.name || !e.categoria); |
| 58 | + return bad.length === 0 || `${bad.length} enti incompleti`; |
| 59 | +}); |
| 60 | + |
| 61 | +check("gli id degli enti sono unici", () => { |
| 62 | + return ids.size === DATA.enti.length || "id duplicati fra gli enti"; |
| 63 | +}); |
| 64 | + |
| 65 | +check("gli id degli e-service sono unici", () => { |
| 66 | + const seen = new Set(); |
| 67 | + const dup = DATA.eservices.filter((s) => (seen.has(s.id) ? true : (seen.add(s.id), false))); |
| 68 | + return dup.length === 0 || `${dup.length} id duplicati`; |
| 69 | +}); |
| 70 | + |
| 71 | +check("ogni erogatore risolve a un ente esistente", () => { |
| 72 | + const bad = DATA.eservices.filter((s) => !ids.has(s.erogatore)); |
| 73 | + return bad.length === 0 || `${bad.length} e-service con erogatore ignoto`; |
| 74 | +}); |
| 75 | + |
| 76 | +check("ogni fruitore risolve a un ente esistente", () => { |
| 77 | + const bad = []; |
| 78 | + DATA.eservices.forEach((s) => |
| 79 | + (s.fruitori || []).forEach((f) => { if (!ids.has(f)) bad.push(`${s.id}→${f}`); }) |
| 80 | + ); |
| 81 | + return bad.length === 0 || `${bad.length} riferimenti pendenti: ${bad.slice(0, 3).join(", ")}`; |
| 82 | +}); |
| 83 | + |
| 84 | +check("nessun auto-anello (erogatore uguale a fruitore)", () => { |
| 85 | + const bad = []; |
| 86 | + DATA.eservices.forEach((s) => |
| 87 | + (s.fruitori || []).forEach((f) => { if (f === s.erogatore) bad.push(s.id); }) |
| 88 | + ); |
| 89 | + return bad.length === 0 || `${bad.length} auto-anelli: ${bad.slice(0, 3).join(", ")}`; |
| 90 | +}); |
| 91 | + |
| 92 | +check("nessun fruitore ripetuto nello stesso e-service", () => { |
| 93 | + const bad = DATA.eservices.filter( |
| 94 | + (s) => new Set(s.fruitori || []).size !== (s.fruitori || []).length |
| 95 | + ); |
| 96 | + return bad.length === 0 || `${bad.length} e-service con fruitori duplicati`; |
| 97 | +}); |
| 98 | + |
| 99 | +check("nessun nodo orfano (ogni ente compare in almeno un arco)", () => { |
| 100 | + const used = new Set(); |
| 101 | + DATA.eservices.forEach((s) => { |
| 102 | + used.add(s.erogatore); |
| 103 | + (s.fruitori || []).forEach((f) => used.add(f)); |
| 104 | + }); |
| 105 | + const orfani = DATA.enti.filter((e) => !used.has(e.id)).map((e) => e.id); |
| 106 | + return orfani.length === 0 || `${orfani.length} orfani: ${orfani.join(", ")}`; |
| 107 | +}); |
| 108 | + |
| 109 | +check("il meta dichiara la fonte delle connessioni", () => { |
| 110 | + return ( |
| 111 | + (DATA.meta && DATA.meta.fonte_connessioni && DATA.meta.note_connessioni) |
| 112 | + ? true |
| 113 | + : "meta.fonte_connessioni o meta.note_connessioni mancante" |
| 114 | + ); |
| 115 | +}); |
| 116 | + |
| 117 | +console.log("\nComportamento di buildGraph"); |
| 118 | + |
| 119 | +const graph = buildGraph(DATA); |
| 120 | +const ORIGINI_VALIDE = new Set(["documentata", "certificata", "ricostruita", "inferita"]); |
| 121 | + |
| 122 | +check("restituisce nodes, links e linkCounts", () => { |
| 123 | + return ( |
| 124 | + Array.isArray(graph.nodes) && Array.isArray(graph.links) && graph.linkCounts |
| 125 | + ? true |
| 126 | + : "struttura di ritorno inattesa" |
| 127 | + ); |
| 128 | +}); |
| 129 | + |
| 130 | +check("un nodo per ogni ente", () => { |
| 131 | + return graph.nodes.length === DATA.enti.length || |
| 132 | + `${graph.nodes.length} nodi contro ${DATA.enti.length} enti`; |
| 133 | +}); |
| 134 | + |
| 135 | +check("un link per ogni istanza erogatore→fruitore", () => { |
| 136 | + const atteso = DATA.eservices.reduce((a, s) => a + (s.fruitori || []).length, 0); |
| 137 | + return graph.links.length === atteso || |
| 138 | + `${graph.links.length} link contro ${atteso} attesi`; |
| 139 | +}); |
| 140 | + |
| 141 | +check("ogni link porta un'origine valida", () => { |
| 142 | + const bad = graph.links.filter((l) => !ORIGINI_VALIDE.has(l.origine)); |
| 143 | + return bad.length === 0 || |
| 144 | + `${bad.length} link con origine non prevista (es. "${bad[0]?.origine}")`; |
| 145 | +}); |
| 146 | + |
| 147 | +// Regressione: il dato v1 non ha `archi`, quindi tutti i link nascono dal |
| 148 | +// fallback. Etichettarli "certificata" affermerebbe una derivazione dal campo |
| 149 | +// attributes del catalogo che non è mai avvenuta. |
| 150 | +check('il fallback non dichiara gli archi "certificata"', () => { |
| 151 | + const senzaArchi = DATA.eservices.filter((s) => !s.archi); |
| 152 | + if (senzaArchi.length === 0) return true; // il dato porta già la provenienza |
| 153 | + const nomi = new Set(senzaArchi.map((s) => s.nome)); |
| 154 | + const daFallback = graph.links.filter((l) => nomi.has(l.eservice)); |
| 155 | + const certificati = daFallback.filter((l) => l.origine === "certificata"); |
| 156 | + return certificati.length === 0 || |
| 157 | + `${certificati.length} archi ricostruiti dichiarati "certificata"`; |
| 158 | +}); |
| 159 | + |
| 160 | +check("nessun link punta a un nodo inesistente", () => { |
| 161 | + const bad = graph.links.filter((l) => !ids.has(l.source) || !ids.has(l.target)); |
| 162 | + return bad.length === 0 || `${bad.length} link pendenti`; |
| 163 | +}); |
| 164 | + |
| 165 | +check("il peso è simmetrico per la coppia non ordinata", () => { |
| 166 | + const bad = graph.links.filter((l) => { |
| 167 | + const key = [l.source, l.target].sort().join("--"); |
| 168 | + return l.weight !== graph.linkCounts[key]; |
| 169 | + }); |
| 170 | + return bad.length === 0 || `${bad.length} link con peso incoerente`; |
| 171 | +}); |
| 172 | + |
| 173 | +check("ogni peso è un intero positivo", () => { |
| 174 | + const bad = graph.links.filter( |
| 175 | + (l) => !Number.isInteger(l.weight) || l.weight < 1 |
| 176 | + ); |
| 177 | + return bad.length === 0 || `${bad.length} pesi non validi`; |
| 178 | +}); |
| 179 | + |
| 180 | +console.log("\nCoerenza con le metriche pubblicate"); |
| 181 | + |
| 182 | +check("la topologia corrisponde al report (51 nodi, 86 e-service, 343 archi)", () => { |
| 183 | + const distinti = new Set(); |
| 184 | + DATA.eservices.forEach((s) => |
| 185 | + (s.fruitori || []).forEach((f) => distinti.add(`${s.erogatore}->${f}`)) |
| 186 | + ); |
| 187 | + const err = []; |
| 188 | + if (DATA.enti.length !== 51) err.push(`nodi ${DATA.enti.length}≠51`); |
| 189 | + if (DATA.eservices.length !== 86) err.push(`e-service ${DATA.eservices.length}≠86`); |
| 190 | + if (distinti.size !== 343) err.push(`archi distinti ${distinti.size}≠343`); |
| 191 | + return err.length === 0 || err.join(", "); |
| 192 | +}); |
| 193 | + |
| 194 | +const totale = passed + failures.length; |
| 195 | +console.log( |
| 196 | + `\n${failures.length === 0 ? "Tutti i test superati" : "Test falliti"}: ${passed}/${totale}\n` |
| 197 | +); |
| 198 | +process.exit(failures.length === 0 ? 0 : 1); |
0 commit comments