fix(roadtrip): a hotel visited again on its check-in day is the night only at its last visit

A stop counted as the booked night whenever a stay began at its place on its
day, so a hotel on its check-in day twice (the booking's own stop and a rest
the traveller planned there) was read as two nights. Both were held to the
check-in: the rest waited for the room or was set back to it, and with a check-in
far from its arrival the rest and every stop behind it moved onto the next day's
card.

oneNightPerStay in shared keeps the night and its check-in on the last visit of
the place and turns the earlier ones into ordinary stops, still a hotel. The
browser and calculate_roadtrip both run it, the stored ride slot times only that
row by the check-in, and opening an earlier visit in road trip mode opens the
stop rather than the night.

Refs #2636
This commit is contained in:
Maurice
2026-10-12 11:05:41 +02:00
parent e3bf18f9a6
commit 565333a90c
12 changed files with 229 additions and 33 deletions
@@ -1405,6 +1405,34 @@ describe('a booked night at both ends of its days', () => {
expect(runs).toContainEqual(['driving', [HOTEL[0], P[4][0], P[5][0], HOTEL[0]]])
})
it('FE-ROADTRIP-ROUTES-065: a hotel visited again on its check-in day is the night only at its last visit (#2636)', async () => {
switchTo(false)
const days = [day(1, 1), day(2, 2)]
// A rest at the hotel at noon, a place, and back to the hotel for the night.
const assignments = map(1, [
{ id: 1, at: P[0], time: '09:00', dwell: 0 },
{ id: 2, at: HOTEL, dwell: 60 },
{ id: 3, at: P[1], dwell: 30 },
{ id: 4, at: HOTEL },
]) as AssignmentsMap
Object.assign(assignments['1'][3], { place_id: 20 })
const stays = [stayOf(5, 2, 1, 2, HOTEL, { check_in: '15:00' })]
const { result } = renderHook(() => useRoadtripRoutes(7, days, assignments, 'driving', {}, [], stays))
await waitFor(() => expect(result.current.loading).toBe(false))
const [card] = result.current.days
expect(card.stops.map(s => [s.name, s.night, s.checkInTime])).toEqual([
['Stop 1', false, null],
['Stop 2', false, null],
['Stop 3', false, null],
['Stop 4', true, '15:00'],
])
// The rest is reached when the drive gets there, the night waits for the room.
expect(card.schedule.entries.map(e => e.arrival)).toEqual(['09:00', '10:00', '12:00', '15:00'])
expect(card.schedule.warnings).toEqual([])
expect(result.current.days.map(d => d.dayId)).toEqual([1])
})
it('FE-ROADTRIP-ROUTES-064: the stop tonight’s booking put first on its day gives way to the evening at the stay (#2546)', async () => {
const { days } = cam()
const stays = [stayOf(1, 1, 1, 2, GETAWAY), stayOf(2, 4, 2, 3, WALLINGA)]
@@ -1,5 +1,5 @@
import { useRoadtripSettings } from '../../hooks/useRoadtripSettings'
import { assembleRoadtrip, bookendStaysOf, carrierLegsFor, carrierSeam, foldRouteRun, hotelBookendsOn, isCarrierMode, isStationaryJoin, mergeRouteSegments, seatCarrierStops, seatNightBookends, standsAsDay, terminalAssignmentId, viasOnLeg, type CarrierSeam, type RoadtripStop, type RoadtripRoutes, type PlanDay, type QuietDay, type RoutedLeg } from '@trek/shared/roadtrip'
import { assembleRoadtrip, bookendStaysOf, carrierLegsFor, carrierSeam, foldRouteRun, hotelBookendsOn, isCarrierMode, isStationaryJoin, mergeRouteSegments, oneNightPerStay, seatCarrierStops, seatNightBookends, standsAsDay, terminalAssignmentId, viasOnLeg, type CarrierSeam, type RoadtripStop, type RoadtripRoutes, type PlanDay, type QuietDay, type RoutedLeg } from '@trek/shared/roadtrip'
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { avoidedClasses, calculateRouteWithLegs, routeEngineFor, RoutingRefusedError, type RouteEngine } from '../Map/RouteCalculator'
import { resolveLegMode } from '../Planner/legMode'
@@ -319,8 +319,9 @@ export function useRoadtripRoutes(
.filter((x): x is { assignment: Assignment; stop: RoadtripStop } => x.stop !== null)
// The index is filled in after the drop, because it is the index into THIS
// list: an assignment whose place has no coordinates never becomes a stop, and
// counting before the filter would name a row the rail does not draw.
const stops = located.map((x, i) => ({ ...x.stop, ownerIndex: i }))
// counting before the filter would name a row the rail does not draw. A hotel
// visited twice on its check-in day is the night at its last visit only.
const stops = oneNightPerStay(located.map((x, i) => ({ ...x.stop, ownerIndex: i })))
return {
dayId: d.id,
dayNumber: d.day_number ?? 0,
@@ -362,6 +362,22 @@ describe('usePlaceFormOpeners', () => {
expect(result.current.form.editingPlace?.id).toBe(6)
})
it('FE-TP-OPENERS-014b: a hotel visited twice on its check-in day opens as the night only at its last visit (#2636)', () => {
const tripAccommodations = [stay({ id: 70, place_id: 3, start_day_id: 14, end_day_id: 12, check_in: '15:00', check_out: '10:00' })]
const twice: AssignmentsMap = {
...assignments,
14: [buildAssignment({ id: 141, place: hotel, order_index: 0 }), buildAssignment({ id: 142, place: hotel, order_index: 2 })],
}
const { result } = renderOpeners({ roadtripActive: true, tripAccommodations, assignments: twice })
act(() => { result.current.openPlaceEditor(hotel, 141) })
expect(result.current.stopDraft).toMatchObject({ dayId: 14, position: 0, editing: { accommodationId: undefined, checkIn: '' } })
expect(result.current.stopDraft?.overnight).toBeUndefined()
act(() => { result.current.openPlaceEditor(hotel, 142) })
expect(result.current.stopDraft).toMatchObject({ dayId: 14, position: 2, editing: { accommodationId: 70, checkIn: '15:00' }, overnight: { defaultEndDayId: 12 } })
})
it('FE-TP-OPENERS-015: an ordinary place opens the form on its one visit, a tour on the phone selects it instead', () => {
const { result } = renderOpeners()
@@ -295,7 +295,9 @@ export function usePlaceFormOpeners(options: PlaceFormOpenersOptions) {
if (entry) {
const dayId = Number(entry[0])
const visit = entry[1].find(visit => visit.id === visitId)!
const stay = tripAccommodations.find(stay => stay.place_id === place.id && stay.start_day_id === dayId)
// The night is the hotel's last visit of the day; an earlier one opens as a stop.
const lastVisit = entry[1].filter(other => other.place_id === place.id).every(other => (other.order_index ?? 0) <= (visit.order_index ?? 0))
const stay = lastVisit ? tripAccommodations.find(stay => stay.place_id === place.id && stay.start_day_id === dayId) : undefined
const category = place.stop_type ?? (stay ? 'hotel' : '')
const routedDay = roadtripRoutes.days.find(day => day.stops.some(stop => stop.assignmentId === visitId))
const arrivalTime = routedDay?.schedule.entries[routedDay.stops.findIndex(stop => stop.assignmentId === visitId)]?.arrival ?? null
+9
View File
@@ -413,6 +413,15 @@ describe('storedRideSlot', () => {
expect(storedRideSlot(ferry(), [amsterdam, hotel], [{ ...checkIn[0], start_day_id: 4 }], 5)).toBe(0.5)
})
it('times only the last visit of the hotel by its check-in, an earlier one is a stop (#2636)', () => {
// Checked in at six, after the 17:30 sailing: the night is behind the crossing, and so
// is the rest at the same hotel before it. Read as a second check-in it timed the rest
// at six too, and the slot went behind both, so the drive reached them overland.
const checkIn = [{ place_id: 30, start_day_id: 5, check_in: '18:00' }]
const night = { ...hotel, id: 4, order_index: 2 }
expect(storedRideSlot(ferry(), [amsterdam, hotel, night], checkIn, 5)).toBe(0.5)
})
it('opens the day, closes it, or sits behind a timed row, measured on order indexes', () => {
expect(storedRideSlot(ferry(), [row(2, 3, 54.9783, -1.6178)], [], 5)).toBe(2.5)
expect(storedRideSlot(ferry(), [amsterdam], [], 5)).toBe(0.5)
+9 -3
View File
@@ -375,8 +375,9 @@ interface StoredStop {
* number among their order indexes all the same, and worked out over the rows the list
* shows it could land behind a hidden stop on the far shore, which the drive then went to
* overland before the crossing (#2461). Each row counts with its start, else the check-in
* of the stay that begins there that day (`stayStartingOn`), and with its coordinates; a
* row without them is no stop of the drive. Where the rule keeps the clock's seat, the
* of the stay that begins there that day (`stayStartingOn`) on the place's last row of the
* day, and with its coordinates; a row without them is no stop of the drive. Where the
* rule keeps the clock's seat, the
* clock is read over the same rows: behind the last one timed at or before the departure,
* else at the end of the day.
*/
@@ -388,11 +389,16 @@ export function storedRideSlot(
): number | null {
const ride = sameDayRide(r)
if (!ride || !day.length) return null
// The night is the last visit of its hotel on the day (`oneNightPerStay`), so only that
// row is timed by the check-in; an earlier visit of the same place is a stop like any other.
const lastOfPlace = new Map<number | null | undefined, number>()
for (const row of day) lastOfPlace.set(row.place_id, Math.max(lastOfPlace.get(row.place_id) ?? -Infinity, row.order_index))
const items: SeatItem[] = day.flatMap(row => {
const lat = row.place?.lat
const lng = row.place?.lng
if (typeof lat !== 'number' || typeof lng !== 'number' || !Number.isFinite(lat) || !Number.isFinite(lng)) return []
const start = row.place?.place_time ?? stayStartingOn(stays, row.place_id, dayId)?.check_in
const night = lastOfPlace.get(row.place_id) === row.order_index
const start = row.place?.place_time ?? (night ? stayStartingOn(stays, row.place_id, dayId)?.check_in : null)
return [{ key: row.order_index, minutes: parseTimeToMinutes(start), point: { lat, lng } }]
})
const keys = day.map(row => row.order_index)