mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 16:33:16 +00:00
32879dd528
A MapLibre user could only draw OpenFreeMap styles; a raster template was Leaflet's alone. With maplibre_style set to trek:template, MapLibre now draws the template the trip map already resolves for Leaflet (CARTO key, keyless fallback and dark default included), so both renderers show the same map. No stored row holds the keyword, so every existing user sees exactly the map they saw before, and Mapbox is untouched. - glBasemap turns the basemap spec into a raster style document (256px tiles as Leaflet requests them, the provider's deepest zoom, the credit, OpenFreeMap glyphs for the map's own labels) and resolves trek:ofm-auto to Liberty or OpenFreeMap Dark with the colour scheme. - glCanFetch decides whether a GL map may fetch a basemap here: https on a host the CSP names, the shipped ones or MAP_EXTRA_CONNECT_HOSTS from the app config. GCJ-02 tiles (Amap) and other hosts are drawn by Leaflet for that render, the setting untouched. - rasterHealth watches the first tiles of a template map: a CSP refusal or twelve failures without a success move every map drawing that template to Leaflet for the session, with a pill that says so. - useEffectiveMapEngine decides this on the engine that actually draws and runs the offline checks after it; a stored raster template keeps drawing offline where Leaflet would keep the same tiles. - The journey map uses the same hook: the same template, the same fallback and now the offline basemap with its pill as the trip map has it. - A MapLibre raster style stops at zoom 19 like Leaflet; the Mapbox options stay as they were. The offline pill moves to BasemapPill for both maps. Dropping the unused imports there lowers the eslint baseline.
97 lines
3.9 KiB
TypeScript
97 lines
3.9 KiB
TypeScript
/**
|
|
* The session watch over a raster template a MapLibre map draws (FE-MAP-RASTERHEALTH-*).
|
|
*/
|
|
import { act, renderHook } from '@testing-library/react';
|
|
import { afterEach, describe, expect, it } from 'vitest';
|
|
|
|
import { GL_BASEMAP_SOURCE, mapLibreBasemap } from './glBasemap';
|
|
import { rasterTemplateFailed, resetRasterHealth, useRasterHealth, watchRasterHealth } from './rasterHealth';
|
|
|
|
const TEMPLATE = 'https://tile.openstreetmap.de/{z}/{x}/{y}.png';
|
|
const STYLE = mapLibreBasemap('trek:template', TEMPLATE, false, []).style;
|
|
|
|
type Listener = (event: { sourceId?: string; tile?: unknown }) => void;
|
|
|
|
/** Just enough of a GL map: the events the watch listens for, fired by hand. */
|
|
function fakeMap() {
|
|
const listeners: Record<string, Listener[]> = {};
|
|
const add = (type: string, listener: Listener) => {
|
|
(listeners[type] ??= []).push(listener);
|
|
};
|
|
return {
|
|
on: add,
|
|
once: add,
|
|
fire: (type: string, event: { sourceId?: string; tile?: unknown } = {}) =>
|
|
listeners[type]?.forEach((listener) => listener(event)),
|
|
};
|
|
}
|
|
|
|
function violation(blockedURI: string): Event {
|
|
return Object.assign(new Event('securitypolicyviolation'), { blockedURI });
|
|
}
|
|
|
|
afterEach(() => resetRasterHealth());
|
|
|
|
describe('watchRasterHealth', () => {
|
|
it('FE-MAP-RASTERHEALTH-001: twelve failed tiles without a success mark the template', () => {
|
|
const map = fakeMap();
|
|
watchRasterHealth(map, STYLE);
|
|
for (let i = 0; i < 11; i++) map.fire('error', { sourceId: GL_BASEMAP_SOURCE });
|
|
expect(rasterTemplateFailed(TEMPLATE)).toBe(false);
|
|
map.fire('error', { sourceId: GL_BASEMAP_SOURCE });
|
|
expect(rasterTemplateFailed(TEMPLATE)).toBe(true);
|
|
});
|
|
|
|
it('FE-MAP-RASTERHEALTH-002: one tile that arrived ends the watch, later failures are an outage', () => {
|
|
const map = fakeMap();
|
|
watchRasterHealth(map, STYLE);
|
|
map.fire('data', { sourceId: GL_BASEMAP_SOURCE, tile: {} });
|
|
for (let i = 0; i < 20; i++) map.fire('error', { sourceId: GL_BASEMAP_SOURCE });
|
|
expect(rasterTemplateFailed(TEMPLATE)).toBe(false);
|
|
});
|
|
|
|
it("FE-MAP-RASTERHEALTH-003: errors of the map's other sources do not count", () => {
|
|
const map = fakeMap();
|
|
watchRasterHealth(map, STYLE);
|
|
for (let i = 0; i < 20; i++) map.fire('error', { sourceId: 'route' });
|
|
map.fire('data', { sourceId: GL_BASEMAP_SOURCE });
|
|
expect(rasterTemplateFailed(TEMPLATE)).toBe(false);
|
|
// Still watching: removing the map ends it, so no later test meets this one's listener.
|
|
map.fire('remove');
|
|
});
|
|
|
|
it('FE-MAP-RASTERHEALTH-004: a CSP refusal of one of its tiles marks the template at once', () => {
|
|
watchRasterHealth(fakeMap(), STYLE);
|
|
document.dispatchEvent(violation('https://tile.openstreetmap.de.evil.test/1/2/3.png'));
|
|
expect(rasterTemplateFailed(TEMPLATE)).toBe(false);
|
|
document.dispatchEvent(violation('https://tile.openstreetmap.de/5/16/10.png'));
|
|
expect(rasterTemplateFailed(TEMPLATE)).toBe(true);
|
|
});
|
|
|
|
it('FE-MAP-RASTERHEALTH-005: a removed map stops listening for refusals', () => {
|
|
const map = fakeMap();
|
|
watchRasterHealth(map, STYLE);
|
|
map.fire('remove');
|
|
document.dispatchEvent(violation('https://tile.openstreetmap.de/5/16/10.png'));
|
|
expect(rasterTemplateFailed(TEMPLATE)).toBe(false);
|
|
});
|
|
|
|
it('FE-MAP-RASTERHEALTH-006: a style URL is not watched', () => {
|
|
const map = fakeMap();
|
|
watchRasterHealth(map, 'https://tiles.openfreemap.org/styles/liberty');
|
|
for (let i = 0; i < 20; i++) map.fire('error', { sourceId: GL_BASEMAP_SOURCE });
|
|
expect(rasterTemplateFailed(TEMPLATE)).toBe(false);
|
|
});
|
|
|
|
it('FE-MAP-RASTERHEALTH-007: a mark re-renders the maps that read the health', () => {
|
|
const { result } = renderHook(() => useRasterHealth());
|
|
const before = result.current;
|
|
const map = fakeMap();
|
|
watchRasterHealth(map, STYLE);
|
|
act(() => {
|
|
for (let i = 0; i < 12; i++) map.fire('error', { sourceId: GL_BASEMAP_SOURCE });
|
|
});
|
|
expect(result.current).toBeGreaterThan(before);
|
|
});
|
|
});
|