SPB Git

Repository Public

pure.test.ts 10.3 KB 156 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : KA Maps Engine8 * File             : server/km-maps/test/pure.test.ts9 * Module           : server10 * 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 { test } from 'node:test';44import { applyLayerSwitches, defaultAppConfig, validateAppConfig } from '../lib/brand.ts';45import { markdownToHtml } from '../lib/markdown.ts';46import { inRollout, resolveTheme, themeSalt } from '../lib/rollout.ts';47import { compareSemver, parseSemver } from '../lib/semver.ts';48import { buildManifest, channelKey, type Snapshot } from '../lib/snapshot.ts';49import { parseStaticQuery, staticKey } from '../src/static-map.ts';5051test('semver', () => {52  assert.deepEqual(parseSemver('1.20.3'), [1, 20, 3]);53  assert.deepEqual(parseSemver('0.1.0-beta.2'), [0, 1, 0]);54  for (const bad of ['1.0', 'v1.0.0', '1.0.0.0', '', 'a.b.c', 1 as any]) assert.equal(parseSemver(bad), null);55  assert.ok(compareSemver('0.10.0', '0.9.9') > 0);56  assert.ok(compareSemver('1.0.0', '1.0.1') < 0);57  assert.equal(compareSemver('2.3.4', '2.3.4'), 0);58});5960test('tranches de diffusion : exactes, monotones, décalées par thème, sans tranche = stable', () => {61  for (const theme of ['signature', 'midnight', 'city3d', 'x']) {62    let prev = new Set<number>();63    for (const pct of [0, 1, 10, 25, 50, 99, 100]) {64      const cur = new Set(Array.from({ length: 100 }, (_, b) => b).filter((b) => inRollout(theme, b, pct)));65      assert.equal(cur.size, pct, `${theme} à ${pct} %`);66      for (const b of prev) assert.ok(cur.has(b), 'une installation servie le reste quand le pourcentage monte');67      prev = cur;68    }69    assert.equal(inRollout(theme, null, 99), false);70    assert.equal(inRollout(theme, null, 100), true);71  }72  assert.notEqual(themeSalt('signature'), themeSalt('midnight'));73  assert.ok(themeSalt('signature') >= 0 && themeSalt('signature') < 100);74});7576test('choix de version : candidate, stable, compatibilité du moteur', () => {77  const versions = [{ version: 1, minEngine: '0.1.0' }, { version: 2, minEngine: '0.1.0' }, { version: 3, minEngine: '0.4.0' }, { version: 4, minEngine: '0.5.0' }];78  const salt = themeSalt('t');79  const inB = (100 - salt) % 100; // tranche servie dès 1 %80  const outB = (inB + 60) % 100;81  const ch = { stable: 3, candidate: 4, percent: 20 };82  assert.deepEqual(resolveTheme('t', versions, ch, '0.5.0', inB), { version: 4, channel: 'candidate', target: 4, downgraded: false });83  assert.deepEqual(resolveTheme('t', versions, ch, '0.5.0', outB), { version: 3, channel: 'stable', target: 3, downgraded: false });84  assert.deepEqual(resolveTheme('t', versions, ch, '0.4.2', inB), { version: 3, channel: 'candidate', target: 4, downgraded: true });85  assert.deepEqual(resolveTheme('t', versions, ch, '0.3.9', outB), { version: 2, channel: 'stable', target: 3, downgraded: true });86  assert.deepEqual(resolveTheme('t', versions, ch, '0.0.1', outB), { version: null, reason: 'engine' });87  assert.deepEqual(resolveTheme('t', versions, ch, null, null), { version: 3, channel: 'stable', target: 3, downgraded: false });88  assert.deepEqual(resolveTheme('t', versions, { stable: null, candidate: null, percent: 0 }, '1.0.0', 1), { version: null, reason: 'unpublished' });89  assert.deepEqual(resolveTheme('t', versions, { stable: null, candidate: 2, percent: 50 }, '1.0.0', outB), { version: null, reason: 'unpublished' });90  // un retour arrière ne sert jamais une version postérieure à la cible, même compatible91  assert.deepEqual(resolveTheme('t', versions, { stable: 1, candidate: null, percent: 0 }, '9.0.0', 5), { version: 1, channel: 'stable', target: 1, downgraded: false });92});9394test('manifeste : thèmes de l’application seulement, thème par défaut de repli, adresses', () => {95  const cfg = { ...defaultAppConfig(['a', 'b']), defaultTheme: 'b' };96  const snap: Snapshot = {97    revision: 7,98    themes: new Map([['a', { id: 'a', title: 'A', versions: [{ version: 1, minEngine: '0.1.0', sha256: 'aa', bytes: 10, createdAt: '2026-12-01T00:00:00Z' }] }], ['b', { id: 'b', title: 'B', versions: [{ version: 1, minEngine: '0.9.0', sha256: 'bb', bytes: 20, createdAt: '2026-12-01T00:00:00Z' }] }], ['c', { id: 'c', title: 'C', versions: [] }]]),99    channels: new Map([[channelKey('a', 'louka', 'web'), { stable: 1, candidate: null, percent: 0 }], [channelKey('b', 'louka', 'web'), { stable: 1, candidate: null, percent: 0 }]]),100    apps: new Map([['louka', { rev: 3, config: cfg, brandText: '{}', brandSha: 'cc' }]]),101    icons: [], datasets: [], dataVersion: '20261130-qc',102  };103  const ep = { tiles: 'https://t', terrain: 'https://r', geo: 'https://g', maps: 'https://m' };104  const m = buildManifest(snap, { app: 'louka', platform: 'web', engine: '0.2.0', bucket: 4 }, ep, { version: '0.2.0' })!;105  assert.deepEqual(m.themes.map((t) => t.id), ['a']);106  assert.deepEqual(m.unavailable, [{ id: 'b', reason: 'engine' }]);107  assert.equal(m.config.defaultTheme, 'a', 'le thème par défaut illisible par ce moteur est remplacé');108  assert.equal(m.themes[0].url, 'https://m/v1/themes/a/1.json');109  assert.equal(m.themes[0].effectiveUrl, 'https://m/v1/themes/a/1.json?app=louka&brand=3');110  assert.deepEqual(m.endpoints, ep);111  assert.equal(m.sdk!.web.url, 'https://m/sdk/0.2.0/');112  assert.equal(buildManifest(snap, { app: 'immoka', platform: 'web', engine: null, bucket: null }, ep, null), null);113  assert.equal(buildManifest(snap, { app: 'louka', platform: 'ios', engine: null, bucket: null }, ep, null)!.themes.length, 0);114});115116test('carte statique : validation stricte des paramètres', () => {117  const q = (s: string) => parseStaticQuery(new URLSearchParams(s));118  const ok = q('center=-73.5674,45.5019&zoom=15.5&size=640x360&scale=2&theme=signature&app=louka&marker=-73.56,45.5&marker=-73.57,45.51,1 450 $&pitch=40&bearing=-20&mode=3d');119  assert.deepEqual([ok.lon, ok.lat, ok.zoom, ok.width, ok.height, ok.scale, ok.pitch, ok.bearing, ok.mode], [-73.5674, 45.5019, 15.5, 640, 360, 2, 40, -20, '3d']);120  assert.deepEqual(ok.markers, [{ lon: -73.56, lat: 45.5 }, { lon: -73.57, lat: 45.51, label: '1 450 $' }]);121  assert.deepEqual(q('center=-71.2,46.8').zoom, 14);122  for (const [bad, msg] of [123    ['', /center/], ['center=abc,1', /nombre invalide/], ['center=-73.5,95', /hors limites/], ['center=-73.5,45.5,3', /center/], ['center=-73.5,45.5&zoom=21', /zoom/], ['center=-73.5,45.5&zoom=1e1', /zoom/],124    ['center=-73.5,45.5&size=4000x300', /size/], ['center=-73.5,45.5&size=10x10', /size/], ['center=-73.5,45.5&size=600', /size/], ['center=-73.5,45.5&scale=3', /scale/],125    ['center=-73.5,45.5&theme=../x', /theme/], ['center=-73.5,45.5&app=autre', /app/], ['center=-73.5,45.5&mode=4d', /mode/], ['center=-73.5,45.5&pitch=80', /pitch/],126    ['center=-73.5,45.5&marker=1', /marker 1/], ['center=-73.5,45.5&marker=-73.5,45.5,<script>', /étiquette/], ['center=-73.5,45.5&marker=-73.5,45.5,' + 'x'.repeat(25), /étiquette/],127    ['center=-73.5,45.5&' + Array(21).fill('marker=-73.5,45.5').join('&'), /20 au plus/], ['center=-73.5,45.5&callback=x', /paramètre inconnu/], ['center=-73.5,45.5&zoom=3&zoom=4', /une seule/],128  ] as [string, RegExp][]) assert.throws(() => q(bad), msg, bad);129  const k = (s: string, v = 1, rev: number | null = 2, data = 'd1') => staticKey(q(s), 'signature', v, rev, data);130  const base = 'center=-73.5,45.5&zoom=12';131  assert.equal(k(base), k('zoom=12&center=-73.5,45.5&size=600x400&scale=1'));132  assert.equal(k(base + '&bearing=-90'), k(base + '&bearing=270'));133  for (const other of [k(base, 2), k(base, 1, 3), k(base, 1, 2, 'd2'), k(base + '&scale=2'), k(base + '&marker=-73.5,45.5')]) assert.notEqual(k(base), other);134});135136test('configuration d’application et couches coupées', () => {137  const c = defaultAppConfig(['signature', 'midnight']);138  assert.deepEqual(validateAppConfig(c, ['signature', 'midnight']), []);139  assert.equal(validateAppConfig({ ...c, themes: [] }, ['signature']).length > 0, true);140  assert.equal(validateAppConfig({ ...c, extra: 1 } as any, ['signature', 'midnight'])[0].path, 'extra');141  const doc = { layers: [{ id: 'bg', type: 'background' }, { id: 'p', type: 'symbol', source: 'poi' }, { id: 'r', type: 'line', source: 'road' }, { id: 'h', type: 'hillshade' }] };142  assert.deepEqual(applyLayerSwitches(doc, { poi: false, hillshade: false, road: true }).layers.map((l: any) => l.id), ['bg', 'r']);143  assert.equal(applyLayerSwitches(doc, { poi: true }), doc);144});145146test('markdown : titres, tableaux, listes, liens — et échappement', () => {147  const h = markdownToHtml('# Titre\n\nTexte **gras** et `code`, voir [ODbL](https://opendatacommons.org/licenses/odbl/).\n\n| A | B |\n|---|---|\n| 1 | <b>2</b> |\n\n- un\n- deux\n\n<script>alert(1)</script>\n');148  assert.match(h, /<h2>Titre<\/h2>/);149  assert.match(h, /<strong>gras<\/strong>/);150  assert.match(h, /<a href="https:\/\/opendatacommons.org\/licenses\/odbl\/" rel="noopener">ODbL<\/a>/);151  assert.match(h, /<td>&lt;b&gt;2&lt;\/b&gt;<\/td>/);152  assert.match(h, /<ul><li>un<\/li><li>deux<\/li><\/ul>/);153  assert.ok(!h.includes('<script>'));154  assert.ok(!markdownToHtml('[x](javascript:alert(1))').includes('href'));155});156
Files main
Menu

Theme