trails/e2e/fixtures/brouter-mock.ts
Ullrich Schäfer 1e2f6beded
Add road type color mode to route visualization
Extract highway=* tags from BRouter tiledesc messages alongside surface
data and add a new "Road Type" color mode that colors the route polyline
and elevation chart by OSM highway classification (cycleway, residential,
path, etc.). Includes color palette, legend, hover labels, i18n (EN+DE),
unit tests for tag extraction, and E2E tests.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-11 11:13:43 +02:00

109 lines
3.3 KiB
TypeScript

import type { Page } from "@playwright/test";
/**
* Canned enriched route for 3 nearby Berlin waypoints:
* [52.520, 13.405] → [52.516, 13.377] → [52.510, 13.390]
*
* ~3.2km total, 6 track points with elevation.
*/
const MOCK_ROUTE_3WP = {
geojson: {
type: "FeatureCollection",
features: [{
type: "Feature",
geometry: {
type: "LineString",
coordinates: [
[13.405, 52.520, 34], [13.398, 52.519, 36], [13.391, 52.518, 38],
[13.384, 52.517, 40], [13.377, 52.516, 42],
[13.379, 52.514, 40], [13.382, 52.513, 38],
[13.385, 52.512, 36], [13.388, 52.511, 35], [13.390, 52.510, 34],
],
},
properties: {},
}],
},
coordinates: [
[13.405, 52.520, 34], [13.398, 52.519, 36], [13.391, 52.518, 38],
[13.384, 52.517, 40], [13.377, 52.516, 42],
[13.379, 52.514, 40], [13.382, 52.513, 38],
[13.385, 52.512, 36], [13.388, 52.511, 35], [13.390, 52.510, 34],
],
segmentBoundaries: [0, 4],
totalLength: 3200,
totalAscend: 8,
surfaces: [
"asphalt", "asphalt", "cobblestone", "cobblestone", "gravel",
"gravel", "asphalt", "asphalt", "asphalt", "asphalt",
],
highways: [
"residential", "residential", "cycleway", "cycleway", "path",
"path", "tertiary", "tertiary", "residential", "residential",
],
};
/**
* Canned enriched route for 2 nearby Berlin waypoints:
* [52.520, 13.405] → [52.515, 13.351]
*
* ~3.8km total, 6 track points with elevation.
*/
const MOCK_ROUTE_2WP = {
geojson: {
type: "FeatureCollection",
features: [{
type: "Feature",
geometry: {
type: "LineString",
coordinates: [
[13.405, 52.520, 34], [13.394, 52.519, 36], [13.383, 52.518, 40],
[13.372, 52.517, 38], [13.361, 52.516, 36], [13.351, 52.515, 35],
],
},
properties: {},
}],
},
coordinates: [
[13.405, 52.520, 34], [13.394, 52.519, 36], [13.383, 52.518, 40],
[13.372, 52.517, 38], [13.361, 52.516, 36], [13.351, 52.515, 35],
],
segmentBoundaries: [0],
totalLength: 3800,
totalAscend: 6,
surfaces: [
"asphalt", "asphalt", "gravel", "gravel", "asphalt", "asphalt",
],
highways: [
"secondary", "secondary", "cycleway", "cycleway", "residential", "residential",
],
};
/**
* Mock the BRouter route API to return instant, deterministic responses.
* Selects the appropriate canned response based on waypoint count.
*/
export async function mockBRouter(page: Page) {
await page.route("**/api/route", async (route) => {
const body = route.request().postDataJSON();
const wpCount = body?.waypoints?.length ?? 0;
const mock = wpCount >= 3 ? MOCK_ROUTE_3WP : MOCK_ROUTE_2WP;
await route.fulfill({
status: 200,
contentType: "application/json",
body: JSON.stringify(mock),
});
});
}
/**
* Convert a lat/lon to pixel coordinates on the Leaflet map.
* Requires MapExposer to have set window.__leafletMap.
*/
export async function latLngToPixel(page: Page, lat: number, lon: number): Promise<{ x: number; y: number }> {
return page.evaluate(([la, ln]) => {
const map = (window as any).__leafletMap;
if (!map) throw new Error("__leafletMap not available");
const point = map.latLngToContainerPoint([la, ln]);
return { x: point.x, y: point.y };
}, [lat, lon]);
}