import { test } from 'node:test'; import assert from 'node:assert/strict'; import { readFileSync } from 'node:fs'; import { lireEpingles, filtrerParCouche, statutAffiche, decalages, ErreurEpingles, RECTANGLE } from '../xl/epingles.ts'; import { construireStyle, ZOOM_MAX, VUE_DEPART } from '../xl/style.ts'; const md = readFileSync(new URL('../content/xl-epingles.md', import.meta.url), 'utf8'); const ENTETE = '| id | lon | lat | couche | titre | base | statut | precision | phrase | ouvre |\n|---|---|---|---|---|---|---|---|---|---|\n'; const ligne = (id: string, lon: string, lat: string, couche = 'existant') => `| ${id} | ${lon} | ${lat} | ${couche} | Titre | Entrée | partiel | Tarbes | Une phrase. | |\n`; test('la table du dépôt se lit : six à dix épingles au plus, toutes dans le rectangle', () => { const e = lireEpingles(md); assert.ok(e.length >= 1 && e.length <= 10, `${e.length} épingles`); for (const p of e) { assert.ok(p.lon >= RECTANGLE.ouest && p.lon <= RECTANGLE.est && p.lat >= RECTANGLE.sud && p.lat <= RECTANGLE.nord, p.id); assert.ok(p.base.length > 0 && p.titre.length > 0, p.id); } assert.deepEqual(new Set(e.map((p) => p.couche)), new Set(['existant', 'propose'])); }); test('une coordonnée hors du rectangle est refusée, avec la ligne', () => { assert.throws(() => lireEpingles(ENTETE + ligne('a', '0.06', '43.23') + ligne('b', '1.44', '43.60')), (err: unknown) => err instanceof ErreurEpingles && err.ligne === 4 && /longitude/.test(err.message)); assert.throws(() => lireEpingles(ENTETE + ligne('a', '0.06', '41.38')), /latitude/); }); test('une couche inconnue est refusée', () => { assert.throws(() => lireEpingles(ENTETE + ligne('a', '0.06', '43.23', 'futur')), /couche « futur » inconnue/); }); test('un identifiant en double est refusé', () => { assert.throws(() => lireEpingles(ENTETE + ligne('a', '0.06', '43.23') + ligne('a', '0.10', '43.20')), /en double/); }); test('le filtre par couche garde les seules couches allumées', () => { const e = lireEpingles(ENTETE + ligne('a', '0.06', '43.23') + ligne('b', '0.10', '43.20', 'propose') + ligne('c', '0.12', '43.10', 'propose')); assert.deepEqual(filtrerParCouche(e, new Set(['propose'])).map((p) => p.id), ['b', 'c']); assert.deepEqual(filtrerParCouche(e, new Set(['existant', 'propose'])).length, 3); assert.deepEqual(filtrerParCouche(e, new Set()), []); }); test('un lien sortant non https est refusé ; le statut de réserve s’affiche', () => { assert.throws(() => lireEpingles(ENTETE + '| a | 0.06 | 43.23 | existant | T | E | partiel | Tarbes | P. | http://exemple.fr |\n'), /https/); assert.throws(() => lireEpingles(ENTETE + '| a | 0.06 | 43.23 | existant | T | E | partiel | Tarbes | P. | https://[x |\n'), /mal formée/); assert.equal(statutAffiche('à vérifier'), 'à vérifier'); assert.equal(statutAffiche('vérifié'), null); }); test('deux épingles au même centre de commune s’écartent à l’affichage, la donnée ne bouge pas', () => { const e = lireEpingles(ENTETE + ligne('a', '0.1298', '42.9686') + ligne('b', '0.1298', '42.9686', 'propose') + ligne('c', '0.06', '43.23')); const d = decalages(e); assert.notDeepEqual(d.get('a'), d.get('b')); assert.deepEqual(d.get('c'), [0, 0]); assert.equal(e[0]!.lon, e[1]!.lon); }); test('le style ne nomme aucun hôte : glyphes et tuiles viennent de la page', () => { const s = construireStyle('http://127.0.0.1:4319/xl-proto.html'); const texte = JSON.stringify(s); const hotes = new Set([...texte.matchAll(/https?:\/\/([^/"\\]+)/g)].map((m) => m[1]).filter((h) => h !== '127.0.0.1:4319' && h !== 'www.openstreetmap.org' && h !== 'protomaps.com' && h !== 'github.com')); assert.deepEqual([...hotes], []); assert.equal('sprite' in s, false); assert.match(String(s.glyphs), /^http:\/\/127\.0\.0\.1:4319\/xl\/glyphs\/\{fontstack\}\/\{range\}\.pbf$/); assert.ok(ZOOM_MAX >= 12 && ZOOM_MAX <= 14); assert.equal(VUE_DEPART.pitch, 45); });