Files
TREK/client/src/sync/glTileCache.test.ts
T
Maurice 54c54d5e65 fix(ci): repair map fixtures and keep renderer dependencies layered
Use the engine write lock for map defaults and clean the metadata-free test snapshot with Kysely. Model the Leaflet bridge option, keep the renderer failure state below the settings store, and make the tile tests independent of the transpiler quote style.
2026-11-30 16:34:55 +01:00

82 lines
3.8 KiB
TypeScript

/**
* The service worker rule for raster tiles a GL map fetches (FE-SYNC-GLTILECACHE-*).
*/
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import { RASTER_TILE_HOSTS } from '@trek/shared';
import { describe, expect, it } from 'vitest';
import { GL_TILE_CACHE, isGlRasterTileRequest } from './glTileCache';
import { MAX_TILES } from './tilePrefetcher';
function ask(match: typeof isGlRasterTileRequest, url: string, mode: RequestMode) {
return match({ url: new URL(url), request: { mode } as Request });
}
/** The function as the service worker gets it: its source text, evaluated with nothing around it. */
const serialized = new Function(`return (${isGlRasterTileRequest.toString()})`)() as typeof isGlRasterTileRequest;
describe('isGlRasterTileRequest', () => {
it('FE-SYNC-GLTILECACHE-001: takes the CORS fetches of the cached raster presets', () => {
for (const url of [
'https://a.basemaps.cartocdn.com/light_all/5/16/10@2x.png?key=k',
'https://basemaps.cartocdn.com/light_all/5/16/10.png',
'https://tile.openstreetmap.org/5/16/10.png',
'https://tile.openstreetmap.de/5/16/10.png',
'https://tiles.stadiamaps.com/tiles/alidade_smooth/5/16/10.png',
// The GL tour layer that broke on Leaflet's opaque copies.
'https://b.tile.opentopomap.org/5/16/10.png',
]) {
expect(ask(isGlRasterTileRequest, url, 'cors'), url).toBe(true);
}
});
it("FE-SYNC-GLTILECACHE-002: leaves Leaflet's images and the no-cors prefetch to map-tiles", () => {
expect(ask(isGlRasterTileRequest, 'https://tile.openstreetmap.de/5/16/10.png', 'no-cors')).toBe(false);
expect(ask(isGlRasterTileRequest, 'https://tile.openstreetmap.de/5/16/10.png', 'same-origin')).toBe(false);
});
it('FE-SYNC-GLTILECACHE-003: never a host whose policy forbids an offline copy, a GCJ-02 host or a look-alike', () => {
for (const url of [
'https://tile.openmaps.fr/openhikingmap/5/16/10.png',
'https://a.tile-cyclosm.openstreetmap.fr/cyclosm/5/16/10.png',
'https://webrd01.is.autonavi.com/appmaptile?x=1&y=2&z=3',
'https://tile.openstreetmap.de.example.com/5/16/10.png',
'http://tile.openstreetmap.de/5/16/10.png',
]) {
expect(ask(isGlRasterTileRequest, url, 'cors'), url).toBe(false);
}
});
it('FE-SYNC-GLTILECACHE-004: reaches nothing outside its body, so the copy in the service worker matches alike', () => {
for (const url of ['https://tile.openstreetmap.de/5/16/10.png', 'https://tile.openmaps.fr/x/5/16/10.png']) {
for (const mode of ['cors', 'no-cors'] as const) {
expect(ask(serialized, url, mode)).toBe(ask(isGlRasterTileRequest, url, mode));
}
}
});
it('FE-SYNC-GLTILECACHE-005: every host in the inline list is one the CSP lets a GL map fetch', () => {
const inline = [...isGlRasterTileRequest.toString().matchAll(/(['"])([^'"]+\.[a-z]+)\1/g)].map((m) => m[2]);
expect(inline.length).toBeGreaterThan(0);
for (const host of inline) expect(RASTER_TILE_HOSTS, host).toContain(host);
});
});
describe('the map-tiles-gl rule in vite.config.js', () => {
const config = readFileSync(resolve(process.cwd(), 'vite.config.js'), 'utf8');
const rule = /urlPattern: isGlRasterTileRequest,[\s\S]*?cacheableResponse: \{ statuses: \[200\] \}/.exec(config)?.[0];
it('FE-SYNC-GLTILECACHE-006: keeps only readable answers, as many as the prefetch budget', () => {
expect(GL_TILE_CACHE).toBe('map-tiles-gl');
expect(rule).toBeDefined();
expect(rule).toContain('cacheName: GL_TILE_CACHE');
expect(rule).toContain(`maxEntries: ${MAX_TILES}`);
});
it('FE-SYNC-GLTILECACHE-007: comes before the Leaflet rules for the same hosts, since the first match wins', () => {
expect(config.indexOf('urlPattern: isGlRasterTileRequest')).toBeLessThan(config.indexOf('basemaps\\.cartocdn'));
});
});