Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : KA Maps Engine8 * File : studio/test/static.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 */4142// Cartes statiques de bout en bout : rendu réel par le moteur (kmr + km-tiler local), cache, débit.43import assert from 'node:assert/strict';44import { readFileSync } from 'node:fs';45import { join } from 'node:path';46import { after, before, test } from 'node:test';47import { importThemes } from '../server/themes.ts';48import { CLI, makeDb, startMaps, THEMES_DIR } from './helpers.ts';4950let db: Awaited<ReturnType<typeof makeDb>>;51let m: Awaited<ReturnType<typeof startMaps>>;52const load = (f: string) => JSON.parse(readFileSync(join(THEMES_DIR, f), 'utf8'));53before(async () => {54 db = await makeDb();55 await importThemes(db, CLI, ['midnight', 'signature'].map((n) => ({ name: `${n}.json`, doc: load(`${n}.json`) })), [{ app: 'louka', doc: load('brands/louka.json') }], 0);56 m = await startMaps(db, { KM_RATE_STATIC: '6' });57});58after(async () => {59 await m.close();60 await db.drop();61});62const png = async (r: Response) => {63 const b = Buffer.from(await r.arrayBuffer());64 return { b, w: b.readUInt32BE(16), h: b.readUInt32BE(20) };65};6667test('rendu, dimensions, densité 2, cache disque, ETag', async () => {68 const url = `${m.base}/v1/static?center=-73.5674,45.5019&zoom=15&size=400x260&theme=signature&app=louka&marker=-73.5674,45.5019`;69 const t0 = performance.now();70 const r1 = await fetch(url);71 const cold = performance.now() - t0;72 assert.equal(r1.status, 200);73 assert.equal(r1.headers.get('content-type'), 'image/png');74 assert.match(r1.headers.get('cache-control')!, /max-age=86400/);75 const a = await png(r1);76 assert.deepEqual([a.w, a.h], [400, 260]);77 assert.ok(a.b.length > 20_000, `image réelle (${a.b.length} octets)`);78 const t1 = performance.now();79 const r2 = await fetch(url);80 const warm = performance.now() - t1;81 assert.ok((await png(r2)).b.equals(a.b), 'servie du cache, identique');82 assert.ok(warm < cold, `cache plus rapide (${warm.toFixed(0)} ms contre ${cold.toFixed(0)} ms)`);83 assert.match(m.app.metricsText(), /km_maps_static_total\{result="hit"\} 1/);84 assert.equal((await fetch(url, { headers: { 'if-none-match': r1.headers.get('etag')! } })).status, 304);85 const hi = await png(await fetch(url.replace('size=400x260', 'size=400x260&scale=2')));86 assert.deepEqual([hi.w, hi.h], [800, 520]);87 const dark = await png(await fetch(url.replace('theme=signature', 'theme=midnight')));88 assert.ok(!dark.b.equals(a.b));89});9091test('paramètres invalides : 400 avec message ; thème inconnu : 404', async () => {92 const r = await fetch(`${m.base}/v1/static?center=-73.5,45.5&size=9000x9000`);93 assert.equal(r.status, 400);94 assert.match(((await r.json()) as any).error, /size/);95 assert.equal((await fetch(`${m.base}/v1/static?center=-73.5,45.5&theme=inconnu`)).status, 404);96 assert.equal((await fetch(`${m.base}/v1/static?center=-73.5,45.5&evil=1`)).status, 400);97});9899test('limitation des rendus par client (les images en cache restent servies)', async () => {100 const codes: number[] = [];101 for (let i = 0; i < 12; i++) codes.push((await fetch(`${m.base}/v1/static?center=-73.6,45.52&zoom=${10 + i * 0.5}&size=64x64`)).status);102 assert.ok(codes.filter((c) => c === 429).length >= 4, codes.join());103 assert.equal(codes[0], 200);104 assert.equal((await fetch(`${m.base}/v1/static?center=-73.6,45.52&zoom=10&size=64x64`)).status, 200, 'déjà en cache');105});106