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.
226 lines
7.3 KiB
TypeScript
226 lines
7.3 KiB
TypeScript
/**
|
|
* The construction options of every GL map (FE-MAP-GLOPTIONS-*).
|
|
*
|
|
* Mapbox GL is TREK's 3D map and its options are pinned here exactly as the
|
|
* trip map, the journey map and the settings preview built them before they
|
|
* shared this builder: every key, every value, nothing more. A change to the
|
|
* Mapbox branch has to change this test on purpose.
|
|
*/
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
import { mapLibreBasemap } from './glBasemap';
|
|
import { applyGl3d, glMapOptions } from './glMapOptions';
|
|
|
|
const env = vi.hoisted(() => ({ tileStore: false }));
|
|
const transformRequest = vi.hoisted(() => () => ({ url: '' }));
|
|
|
|
vi.mock('../../native/tileCache', () => ({
|
|
tileCacheActive: () => env.tileStore,
|
|
mapboxTileCacheRequest: transformRequest,
|
|
}));
|
|
|
|
const container = document.createElement('div');
|
|
const STREETS = 'mapbox://styles/mapbox/streets-v12';
|
|
const STANDARD = 'mapbox://styles/mapbox/standard';
|
|
const SATELLITE = 'mapbox://styles/mapbox/satellite-v9';
|
|
const LIBERTY = 'https://tiles.openfreemap.org/styles/liberty';
|
|
|
|
const RASTER = mapLibreBasemap('trek:template', 'https://tile.openstreetmap.de/{z}/{x}/{y}.png', false, []).style;
|
|
|
|
function tripMap(provider: 'mapbox-gl' | 'maplibre-gl', quality: boolean, style: unknown = STREETS) {
|
|
return glMapOptions({
|
|
provider,
|
|
container,
|
|
style,
|
|
center: [2.35, 48.85],
|
|
zoom: 12,
|
|
pitch: 45,
|
|
attributionControl: true,
|
|
quality,
|
|
appTileStore: true,
|
|
});
|
|
}
|
|
|
|
beforeEach(() => {
|
|
env.tileStore = false;
|
|
});
|
|
|
|
describe('glMapOptions: Mapbox, byte for byte as before', () => {
|
|
/** What the trip map built, key for key. */
|
|
const tripMapOptions = {
|
|
container,
|
|
style: STREETS,
|
|
center: [2.35, 48.85],
|
|
zoom: 12,
|
|
pitch: 45,
|
|
attributionControl: true,
|
|
antialias: false,
|
|
projection: 'mercator',
|
|
};
|
|
|
|
it('FE-MAP-GLOPTIONS-001: the trip map', () => {
|
|
expect(tripMap('mapbox-gl', false)).toStrictEqual(tripMapOptions);
|
|
});
|
|
|
|
it('FE-MAP-GLOPTIONS-002: the quality mode antialiases and draws the globe', () => {
|
|
expect(tripMap('mapbox-gl', true)).toStrictEqual({ ...tripMapOptions, antialias: true, projection: 'globe' });
|
|
});
|
|
|
|
it('FE-MAP-GLOPTIONS-003: in the iOS app with its tile store, Mapbox resources go through it', () => {
|
|
env.tileStore = true;
|
|
expect(tripMap('mapbox-gl', false)).toStrictEqual({ ...tripMapOptions, transformRequest });
|
|
});
|
|
|
|
it('FE-MAP-GLOPTIONS-004: the journey map, with its compact credit', () => {
|
|
env.tileStore = true;
|
|
const lngLat = { lng: 9.1, lat: 45.4 };
|
|
const options = glMapOptions({
|
|
provider: 'mapbox-gl',
|
|
container,
|
|
style: STANDARD,
|
|
center: lngLat,
|
|
zoom: 2,
|
|
pitch: 0,
|
|
attributionControl: { compact: true },
|
|
quality: false,
|
|
appTileStore: true,
|
|
});
|
|
expect(options).toStrictEqual({
|
|
...tripMapOptions,
|
|
style: STANDARD,
|
|
center: lngLat,
|
|
zoom: 2,
|
|
pitch: 0,
|
|
attributionControl: { compact: true },
|
|
transformRequest,
|
|
});
|
|
});
|
|
|
|
it('FE-MAP-GLOPTIONS-005: the settings preview never goes through the tile store', () => {
|
|
env.tileStore = true;
|
|
const options = glMapOptions({
|
|
provider: 'mapbox-gl',
|
|
container,
|
|
style: STREETS,
|
|
center: [2.3, 48.8],
|
|
zoom: 16,
|
|
pitch: 0,
|
|
attributionControl: true,
|
|
quality: true,
|
|
});
|
|
expect(options).toStrictEqual({
|
|
...tripMapOptions,
|
|
center: [2.3, 48.8],
|
|
zoom: 16,
|
|
pitch: 0,
|
|
antialias: true,
|
|
projection: 'globe',
|
|
});
|
|
});
|
|
|
|
it('FE-MAP-GLOPTIONS-006: keys come in the order they always had', () => {
|
|
env.tileStore = true;
|
|
expect(Object.keys(tripMap('mapbox-gl', false))).toEqual([...Object.keys(tripMapOptions), 'transformRequest']);
|
|
});
|
|
});
|
|
|
|
describe('glMapOptions: MapLibre', () => {
|
|
it('FE-MAP-GLOPTIONS-010: rotates around the pointer and has no projection or tile store', () => {
|
|
env.tileStore = true;
|
|
const options = tripMap('maplibre-gl', false, LIBERTY);
|
|
expect(options).toMatchObject({ style: LIBERTY, antialias: false, aroundCenter: false });
|
|
expect(Object.keys(options)).toEqual([
|
|
'container',
|
|
'style',
|
|
'center',
|
|
'zoom',
|
|
'pitch',
|
|
'attributionControl',
|
|
'antialias',
|
|
'aroundCenter',
|
|
]);
|
|
});
|
|
|
|
it('FE-MAP-GLOPTIONS-011: a raster template stops at the zoom Leaflet stops at', () => {
|
|
const options = tripMap('maplibre-gl', false, RASTER);
|
|
expect(options.style).toBe(RASTER);
|
|
expect(options.maxZoom).toBe(19);
|
|
});
|
|
|
|
it('FE-MAP-GLOPTIONS-012: a style document never moves the Mapbox ceiling', () => {
|
|
expect(tripMap('mapbox-gl', false, RASTER)).not.toHaveProperty('maxZoom');
|
|
});
|
|
});
|
|
|
|
describe('applyGl3d', () => {
|
|
function fakeMap() {
|
|
return {
|
|
getLayer: vi.fn(() => undefined),
|
|
getSource: vi.fn(() => undefined),
|
|
addSource: vi.fn(),
|
|
addLayer: vi.fn(),
|
|
setTerrain: vi.fn(),
|
|
getStyle: vi.fn(() => ({ layers: [] })),
|
|
};
|
|
}
|
|
type GlMap = Parameters<typeof applyGl3d>[0];
|
|
|
|
it('FE-MAP-GLOPTIONS-020: a satellite style in 3D gets terrain and buildings', () => {
|
|
const map = fakeMap();
|
|
applyGl3d(map as unknown as GlMap, SATELLITE, true, false);
|
|
expect(map.setTerrain).toHaveBeenCalledWith({ source: 'mapbox-dem', exaggeration: 1.2 });
|
|
expect(map.addLayer).toHaveBeenCalledWith(expect.objectContaining({ id: 'trek-3d-buildings' }), undefined);
|
|
});
|
|
|
|
it('FE-MAP-GLOPTIONS-021: a flat vector style in 3D gets buildings and no terrain', () => {
|
|
const map = fakeMap();
|
|
applyGl3d(map as unknown as GlMap, STREETS, true, true);
|
|
expect(map.setTerrain).not.toHaveBeenCalled();
|
|
expect(map.addLayer).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
id: 'trek-3d-buildings',
|
|
paint: expect.objectContaining({ 'fill-extrusion-color': '#3b3b3f' }),
|
|
}),
|
|
undefined
|
|
);
|
|
});
|
|
|
|
it('FE-MAP-GLOPTIONS-022: Mapbox Standard brings its own 3D and has its terrain flattened, 3D or not', () => {
|
|
for (const enable3d of [true, false]) {
|
|
const map = fakeMap();
|
|
applyGl3d(map as unknown as GlMap, STANDARD, enable3d, false);
|
|
expect(map.addLayer).not.toHaveBeenCalled();
|
|
expect(map.setTerrain).toHaveBeenCalledWith(null);
|
|
}
|
|
});
|
|
|
|
it('FE-MAP-GLOPTIONS-023: a style not ready for terrain does not break the load', () => {
|
|
const map = fakeMap();
|
|
map.setTerrain.mockImplementation(() => {
|
|
throw new Error('style not loaded');
|
|
});
|
|
expect(() => applyGl3d(map as unknown as GlMap, STANDARD, false, false)).not.toThrow();
|
|
});
|
|
|
|
it('FE-MAP-GLOPTIONS-024: with 3D off a non-Standard style is left as it is', () => {
|
|
const map = fakeMap();
|
|
applyGl3d(map as unknown as GlMap, SATELLITE, false, false);
|
|
expect(map.addLayer).not.toHaveBeenCalled();
|
|
expect(map.setTerrain).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('FE-MAP-GLOPTIONS-026: a raster template style is left as it is', () => {
|
|
const map = fakeMap();
|
|
applyGl3d(map as unknown as GlMap, RASTER ?? '', true, false);
|
|
expect(map.addLayer).not.toHaveBeenCalled();
|
|
expect(map.setTerrain).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('FE-MAP-GLOPTIONS-025: a MapLibre style is left as it is', () => {
|
|
const map = fakeMap();
|
|
applyGl3d(map as unknown as GlMap, LIBERTY, false, false);
|
|
expect(map.addLayer).not.toHaveBeenCalled();
|
|
expect(map.setTerrain).not.toHaveBeenCalled();
|
|
});
|
|
});
|