import { expect, type APIRequestContext, type Page } from '@playwright/test'; import { createHash } from 'node:crypto'; import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'; import path from 'node:path'; import { day, monthsAhead, PICTURE_DAY_ISO } from '../dates'; import { setTripCover } from '../screenshots/photos'; import { clearNotices } from '../screenshots/shot'; import { seededTrip } from './fixtures'; import { dismissReleaseNotice, settle } from './guide'; import { closeDayDetails, selectDay } from './trip-shared'; /** * The demo data every Tours help picture runs on. * * Two trips of their own and two tours on the seeded "Autumn in Japan": * * - "Summer in the Dolomites", ten days next July: a three-stage hut trek * with its huts on the days, a day hike over a via ferrata, a road bike * loop over the four passes, an MTB loop kept in the collection * "Dolomites ideas" and on no day, a run, a boat tour on Lago di Braies, a * horse ride on the Seiser Alm, a city walk through Bolzano and one * imported GPX track, recorded last summer. * - "Ski week in Val Gardena", a week next February: the Sella Ronda on * skis where the piste router can plan it, and a winter walk up the * Vallunga. * - In Japan, a city walk through Higashiyama on day 9 and the Fushimi Inari * mountain hike on day 5, both with a start time. * * Every line follows real ways. The legs on land come from the public * Valhalla the app itself asks (`src/components/Map/valhallaRoute.ts`), with * the costing the planner sends for each type (`planner/tourRouting.ts`), and * the heights from its /height stage, as the planner's save does. The huts and * hotels are placed where OpenStreetMap has them (Nominatim), the lake's * shore and the via ferrata come from OpenStreetMap too. Each answer is kept * under `e2e/.tmp/tour-routes/`, so a second run asks the network nothing. * * Everything is found by its name, so a re-run changes nothing. A tour that * already exists is never rebuilt: a guide that edits one has to put it back * itself (or delete it, and the next call makes it again). */ // ── Names the screen scripts look for ───────────────────────────────────────── export const TOUR_TRIP = 'Summer in the Dolomites'; export const SKI_TRIP = 'Ski week in Val Gardena'; export const JAPAN_TRIP = 'Autumn in Japan'; /** Every tour by its role, as its row, card and day plan show it. */ export const TOUR_NAMES = { /** Hike, three stages, days 4 to 6, linked to the packing category "Hut trek". */ hutToHut: 'Puez-Odle hut trek', /** Hike with a via ferrata leg (when OpenStreetMap answers), day 7, 07:30. */ ferrata: 'Pisciadù via ferrata', /** Bike, road profile, day 3, 08:00. */ roadBike: 'Sella Ronda by road bike', /** Bike, MTB profile, on no day, kept in "Dolomites ideas". */ mtb: 'Monte Pana and Saltria MTB loop', /** Run, running profile, day 8, 07:00. */ run: 'Val Gardena railway trail run', /** Paddle tour on a lake, day 9, 10:00. */ paddle: 'Rowing round Lago di Braies', /** Horse ride, day 2, 10:00. */ horse: 'Seiser Alm horse ride', /** City walk with seven stations, day 1, 15:00. */ cityWalk: 'Bolzano old town walk', /** Imported GPX hike with recorded times, on no day. */ recorded: 'Seceda ridge walk', /** Japan: city walk, day 9, 09:00. */ kyotoWalk: 'Higashiyama walk', /** Japan: hike, day 5, 07:30. */ inariHike: 'Fushimi Inari mountain hike', /** Ski trip: alpine ski tour on the piste router, day 2, 09:00 (only when the piste data answers). */ skiRonda: 'Sella Ronda on skis', /** Ski trip: winter hike, day 4, 10:00. */ winterWalk: 'Vallunga winter walk', /** Japan, only after `ensureTourRecording`: the recorded track of the Fushimi Inari hike. */ inariRecording: 'Fushimi Inari, recorded', } as const; export type TourRole = keyof typeof TOUR_NAMES; /** The collection that keeps the MTB loop. */ export const TOUR_COLLECTION = 'Dolomites ideas'; /** The packing category the hut trek is linked to, and its items. */ export const TOUR_PACKING = { category: 'Hut trek', items: ['Sleeping bag liner', 'Headlamp', 'Rain shell', 'Cash for the huts', 'Earplugs'], }; /** The huts and hotels on the Dolomites days, by role. */ export const TOUR_PLACES = { bolzanoHotel: 'Hotel Greif', selvaHotel: 'Hotel Alpenroyal', puez: 'Rifugio Puez', genova: 'Rifugio Genova', firenze: 'Rifugio Firenze', pisciadu: 'Rifugio Pisciadù', braies: 'Hotel Lago di Braies', } as const; /** The GPX files a guide can import through the UI; `seceda` is imported by the fixtures already. */ export const TOUR_GPX = { seceda: { name: TOUR_NAMES.recorded, file: 'seceda-ridge-walk.gpx' }, rasciesa: { name: 'Rasciesa balcony walk', file: 'rasciesa-balcony-walk.gpx' }, } as const; // ── The calendar ────────────────────────────────────────────────────────────── /** Whole months from the picture day's month to the next `month` (1 to 12), never 0. */ function monthsUntil(month: number): number { const current = Number(PICTURE_DAY_ISO.slice(5, 7)); return (month - current + 12) % 12 || 12; } function addDays(iso: string, n: number): string { const d = new Date(`${iso}T12:00:00Z`); d.setUTCDate(d.getUTCDate() + n); return d.toISOString().slice(0, 10); } function offsetOf(iso: string): number { return Math.round((Date.parse(`${iso}T12:00:00Z`) - Date.parse(`${PICTURE_DAY_ISO}T12:00:00Z`)) / 86_400_000); } const SUMMER_START = monthsAhead(monthsUntil(7), 11); const SKI_START = monthsAhead(monthsUntil(2), 6); /** The trips' dates, as `YYYY-MM-DD`. */ export const TOUR_TRIP_DATES = { start: SUMMER_START, end: addDays(SUMMER_START, 9) }; export const SKI_TRIP_DATES = { start: SKI_START, end: addDays(SKI_START, 6) }; /** * The offset from the picture day of day `n` (from 1) of the Dolomites trip, * for the date helpers of `e2e/dates.ts`: `short(tourDayOffset(4))`. */ export function tourDayOffset(n: number): number { return offsetOf(SUMMER_START) + n - 1; } /** The same for the ski trip. */ export function skiDayOffset(n: number): number { return offsetOf(SKI_START) + n - 1; } // ── What the call leaves behind ─────────────────────────────────────────────── export interface ToursFixtures { tripId: number; skiTripId: number; japanTripId: number; /** Day ids of the Dolomites trip, day 1 first. */ dayIds: number[]; skiDayIds: number[]; /** Place id of every tour that exists, by role. A role whose data could not be had is missing (see the warnings). */ tours: Partial>; /** Place ids of the huts and hotels, by role. */ places: Partial>; collectionId: number | null; } // ── The network: Valhalla, Nominatim, Overpass, all kept on disk ────────────── const CACHE_DIR = path.join(process.cwd(), 'e2e', '.tmp', 'tour-routes'); const VALHALLA = process.env.E2E_VALHALLA_URL?.replace(/\/+$/, '') || 'https://valhalla1.openstreetmap.de'; const NOMINATIM = 'https://nominatim.openstreetmap.org'; const OVERPASS = 'https://overpass-api.de/api/interpreter'; const USER_AGENT = 'TREK help-media fixtures (github.com/liketrek/TREK)'; /** The public hosts allow about a request a second each. */ const lastCall = new Map(); async function turn(host: string): Promise { const wait = (lastCall.get(host) ?? 0) + 1150 - Date.now(); if (wait > 0) await new Promise((r) => setTimeout(r, wait)); lastCall.set(host, Date.now()); } async function fetchJson(url: string, init: RequestInit, timeoutMs: number): Promise { const host = new URL(url).host; let lastError = ''; for (let attempt = 0; attempt < 3; attempt++) { await turn(host); try { const res = await fetch(url, { ...init, headers: { 'User-Agent': USER_AGENT, 'X-Client-Id': 'trek', ...(init.headers ?? {}) }, signal: AbortSignal.timeout(timeoutMs), }); const text = await res.text(); if (text.length > 20_000_000) throw new Error('answer too large'); if (res.ok) return JSON.parse(text); lastError = `${res.status} ${text.slice(0, 300)}`; // A refusal about the request itself does not get better by asking again. if (res.status === 400 || res.status === 404) break; } catch (err) { lastError = err instanceof Error ? err.message : String(err); } await new Promise((r) => setTimeout(r, 2000 * (attempt + 1))); } throw new Error(`${url}: ${lastError}`); } /** An answer kept under `e2e/.tmp/tour-routes/