Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : KA Maps Engine8 * File : studio/test/validate.test.ts9 * Module : studio10 * Repository : https://git.spboucher.ai/ka-maps-engine11 * Created : 2026-12-0112 * Last Modified : 2026-12-0113 *14 * Author : Simon-Pierre Boucher15 * Position : Professor of Finance16 * Institution : Université du Québec en Outaouais (UQO)17 * Organization : SP Boucher18 * Website : https://spboucher.ai19 * Git : https://git.spboucher.ai20 *21 * Preferred Contact: support@spboucher.ai22 * Development : dev@spboucher.ai23 * Support : support@spboucher.ai24 * Security/Admin : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142import assert from 'node:assert/strict';43import { readFileSync } from 'node:fs';44import { join } from 'node:path';45import { test } from 'node:test';46import { diffDocs, pointerToPath, validateBrandShape, validateTheme } from '../server/validate.ts';47import { THEMES_DIR } from './helpers.ts';4849const load = (f: string) => JSON.parse(readFileSync(join(THEMES_DIR, f), 'utf8'));5051test('les thèmes livrés passent le schéma et l’analyse stricte du moteur, avec les quatre marques', async () => {52 for (const name of ['signature', 'midnight', 'city3d']) {53 const r = await validateTheme(load(`${name}.json`));54 assert.deepEqual(r.errors, [], name);55 assert.ok(r.summary.layers > 50);56 for (const app of ['louka', 'immoka', 'vraiprix', 'valoplex']) {57 const b = await validateTheme(load(`${name}.json`), load(`brands/${app}.json`));58 assert.deepEqual(b.errors, [], `${name} + ${app}`);59 }60 }61});6263test('erreurs précises : chemin, message, source', async () => {64 const cases: [string, (d: any) => void, string, RegExp][] = [65 ['couleur invalide', (d) => (d.palette.land = '#12345'), 'palette.land', /couleur invalide/],66 ['clé de peinture inconnue', (d) => (d.layers[1].paint.colour = '#fff'), 'layers[1].paint.colour', /clé inconnue/],67 ['type de couche inconnu', (d) => (d.layers[2].type = 'raster'), 'layers[2].type', /valeur admise/],68 ['arrêts mal formés', (d) => (d.layers.find((l: any) => l.id === 'road-minor').paint.width = { stops: [[12, 'a']] }), 'layers[', /type attendu : number/],69 ['source inconnue', (d) => (d.layers[1].source = 'landcovr'), 'layers[1].source', /valeur admise/],70 ['filtre invalide', (d) => (d.layers[1].filter = ['~=', 'kind', 'x']), 'layers[1].filter', /valeur admise/],71 ['lumière hors bornes', (d) => (d.light.ambient = 1.4), 'light.ambient', /≤ 1/],72 ['kastyle absent', (d) => delete d.kastyle, '', /kastyle/],73 ['version non sémantique', (d) => (d.minEngine = '1.0'), 'minEngine', /format invalide/],74 ['clé racine inconnue', (d) => (d.sprites = {}), 'sprites', /clé inconnue/],75 ['expression de couleur', (d) => (d.palette.halo = ['mix', '@land', 0.5]), 'palette.halo', /./],76 ];77 for (const [name, mutate, path, msg] of cases) {78 const d = load('signature.json');79 mutate(d);80 const r = await validateTheme(d);81 assert.equal(r.ok, false, name);82 const hit = r.errors.find((e) => e.path.startsWith(path) && msg.test(e.message));83 assert.ok(hit, `${name} : ${JSON.stringify(r.errors.slice(0, 4))}`);84 }85});8687test('ce que seul le moteur voit : référence de palette inconnue, cycle, identifiant en double', async () => {88 const a = load('signature.json');89 a.palette.water = '@nexistepas';90 let r = await validateTheme(a);91 assert.ok(r.errors.some((e) => e.source === 'engine'), JSON.stringify(r.errors));92 const b = load('signature.json');93 b.palette.land = '@paper';94 b.palette.paper = '@land';95 r = await validateTheme(b);96 assert.ok(r.errors.some((e) => e.source === 'engine' && /cycle|circulaire/i.test(e.message)), JSON.stringify(r.errors));97 const c = load('signature.json');98 c.layers[5].id = c.layers[4].id;99 r = await validateTheme(c);100 assert.ok(r.errors.some((e) => e.source === 'engine'), JSON.stringify(r.errors));101 assert.equal((await validateTheme('nope')).ok, false);102 assert.equal((await validateTheme([])).ok, false);103});104105test('les commentaires (_clé) sont admis partout', async () => {106 const d = load('signature.json');107 d._note = 'x';108 d.light._note = 'y';109 d.layers[3]._note = 'z';110 d.layers[3].paint._pourquoi = 'w';111 assert.deepEqual((await validateTheme(d)).errors, []);112});113114test('forme d’une marque', () => {115 assert.deepEqual(validateBrandShape(load('brands/louka.json')), []);116 const e = validateBrandShape({ kastyle: 1, brand: 'louka', palette: { accent: 'rouge' }, light: {} });117 assert.deepEqual(e.map((x) => x.path).sort(), ['light', 'palette.accent']);118});119120test('différences : par propriété, couches appariées par identifiant', () => {121 const a = load('signature.json');122 const b = structuredClone(a);123 b.palette.water = '#000000';124 b.light.altitude = 30;125 const minor = b.layers.find((l: any) => l.id === 'road-minor');126 minor.paint.width = { stops: [[12, 1]], base: 1.2 };127 minor.minzoom = 11;128 b.layers = b.layers.filter((l: any) => l.id !== 'rail-ties');129 b.layers.splice(3, 0, { id: 'nouvelle', type: 'fill', source: 'landuse', paint: { color: '#fff' } });130 const d = diffDocs(a, b);131 const by = Object.fromEntries(d.map((x) => [x.path, x]));132 assert.equal(by['palette.water'].kind, 'changed');133 assert.equal(by['light.altitude'].after, 30);134 assert.equal(by['layers[road-minor].paint.width'].kind, 'changed');135 assert.equal(by['layers[road-minor].minzoom'].before, 12);136 assert.equal(by['layers[rail-ties]'].kind, 'removed');137 assert.equal(by['layers[nouvelle]'].kind, 'added');138 assert.equal(d.length, 6, 'insérer une couche ne décale pas les suivantes');139 const moved = structuredClone(a);140 [moved.layers[10], moved.layers[11]] = [moved.layers[11], moved.layers[10]];141 assert.deepEqual(diffDocs(a, moved).map((x) => x.kind), ['moved']);142 assert.deepEqual(diffDocs(a, structuredClone(a)), []);143 assert.equal(pointerToPath('/layers/3/paint/text-size'), 'layers[3].paint.text-size');144});145146test('format en avance sur le schéma : ce que le moteur accepte n’est pas refusé, seulement signalé', async () => {147 const { reconcile } = await import('../server/validate.ts');148 const issues = [149 { source: 'schema' as const, path: 'layers[3].paint.pattern', message: 'clé inconnue : pattern', soft: true },150 { source: 'schema' as const, path: 'light.ambient', message: 'doit être ≤ 1' },151 ];152 const ok = reconcile(issues, true);153 assert.deepEqual(ok.errors.map((e) => e.path), ['light.ambient'], 'une valeur hors bornes reste une erreur');154 assert.match(ok.late[0], /layers\[3\]\.paint\.pattern .* accepté par le moteur/);155 assert.equal(reconcile(issues, false).errors.length, 2, 'si le moteur refuse, toutes les erreurs du schéma sont montrées');156 // une clé inconnue du schéma ET du moteur reste une erreur157 const d = load('signature.json');158 d.layers[4].paint['clef-imaginaire'] = 1;159 const r = await validateTheme(d);160 assert.equal(r.ok, false);161 assert.ok(r.errors.some((e) => e.path.endsWith('paint.clef-imaginaire')));162});163