trails/e2e/fixtures/brouter-mock.ts
Ullrich Schäfer e6c68fb71b
Add BRouter mock for deterministic E2E tests
- brouter-mock.ts: Canned route responses for 2 and 3 waypoint sessions,
  plus latLngToPixel helper for pixel-precise map interactions
- planner.test.ts: Route split, map zoom, and overnight tests now use
  mockBRouter() for instant deterministic responses instead of real
  BRouter — fixes all 3 flaky tests
- Used .first() for sidebar km locator to avoid strict mode violations
  when both header summary and stats footer match

All 38 E2E tests pass in 9.7s (was 34s+ with real BRouter).

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

97 lines
2.8 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: [],
};
/**
* 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: [],
};
/**
* 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]);
}