fix(roadtrip): a booked ferry is driven to, and one without a date says so

A ferry between two stops without a clock was seated by the clock alone, which files a
ride with nothing timed before it at the end of the day. The desktop day list stored that
slot, and the road trip drove overland to the far shore and back to the pier before the
crossing. A ride that leaves and lands on the same day, with both terminals located, now
takes the seat between the clocks that adds the fewest straight-line kilometres. One rule
in shared seats it for the day list, the slot the desktop stores, the phone and PDF lists
and the road trip in the browser and on the server. Slots already stored are left alone.

A flight, train, ferry, cruise or bus on no day made no seam and said nothing, while the
map still drew its arc. The rail and the phone tab now name such a booking with a way to
open it, and calculate_roadtrip lists it under undatedRides.
This commit is contained in:
Maurice
2026-09-23 10:21:00 +02:00
parent e6daed9403
commit ab3a81a9eb
37 changed files with 823 additions and 41 deletions
+54 -1
View File
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest'
import { parseTimeToMinutes, getSpanPhase, hidesOnMiddleDay, getTransportRouteEndpoints, getDisplayTimeForDay, getTransportForDay, getAssignmentReservations, getMergedItems, timedSlot } from './dayMerge'
import { parseTimeToMinutes, getSpanPhase, hidesOnMiddleDay, getTransportRouteEndpoints, getDisplayTimeForDay, getTransportForDay, getAssignmentReservations, getMergedItems, rideSeatKey, timedSlot } from './dayMerge'
describe('parseTimeToMinutes', () => {
it('parses HH:MM string', () => {
@@ -304,6 +304,59 @@ describe('getMergedItems', () => {
})
})
// The crossing from the report behind #2461: Amsterdam and Newcastle on the day, the
// ferry from IJmuiden to the Port of Tyne in the evening. The road trip seats its
// terminals by the same rule (`rideSeatAfter` in @trek/shared), so list and drive agree.
describe('getMergedItems: a ride within one day, seated by where it goes (#2461)', () => {
const place = (id: number, order_index: number, lat: number, lng: number, place_time: string | null = null) =>
({ id, order_index, place: { lat, lng, place_time } })
const amsterdam = (at: string | null = null) => place(1, 0, 52.3731, 4.8926, at)
const newcastle = (at: string | null = null) => place(2, 1, 54.9783, -1.6178, at)
const ferry = (over: Record<string, unknown> = {}) => ({
id: 69, type: 'ferry', title: 'IJmuiden to Newcastle', day_id: 5, end_day_id: 5,
reservation_time: '2026-10-06T17:30', reservation_end_time: '2026-10-06T23:00',
endpoints: [
{ role: 'from', sequence: 0, name: 'IJmuiden', lat: 52.4581, lng: 4.5879 },
{ role: 'to', sequence: 1, name: 'Port of Tyne', lat: 54.9925, lng: -1.4522 },
],
...over,
})
const read = (dayAssignments: unknown[], dayTransports: unknown[], dayNotes: unknown[] = []) =>
getMergedItems({ dayAssignments, dayNotes, dayTransports, dayId: 5 }).map(i => `${i.type}:${i.data.id}`)
it('puts a ferry between the stops on its two shores rather than at the end of the day', () => {
expect(read([amsterdam(), newcastle()], [ferry()])).toEqual(['place:1', 'transport:69', 'place:2'])
})
it('keeps it behind a stop timed before its departure, and leaves a ferry without terminals to the clock', () => {
expect(read([amsterdam(), newcastle('10:00')], [ferry()])).toEqual(['place:1', 'place:2', 'transport:69'])
expect(read([amsterdam(), newcastle()], [ferry({ endpoints: [] })])).toEqual(['place:1', 'place:2', 'transport:69'])
// One that lands tomorrow has no seat of its own on this day either.
expect(read([amsterdam(), newcastle()], [ferry({ end_day_id: 6 })])).toEqual(['place:1', 'place:2', 'transport:69'])
})
it('opens the day with a ride that lands next to its first stop, ahead of a note too', () => {
expect(read([newcastle()], [ferry()], [{ id: 10, sort_order: 0 }])).toEqual(['transport:69', 'place:2', 'note:10'])
})
it('leaves a slot somebody stored where it is', () => {
expect(read([amsterdam(), newcastle()], [ferry({ day_positions: { 5: 1.5 } })])).toEqual(['place:1', 'place:2', 'transport:69'])
})
it('reads a note as nothing and another booking as its clock only', () => {
const rows = [
{ type: 'place' as const, sortKey: 0, data: amsterdam() },
{ type: 'note' as const, sortKey: 0.5, data: { id: 10, time: '20:00' } },
{ type: 'place' as const, sortKey: 1, data: newcastle() },
]
expect(rideSeatKey(ferry(), rows)).toBe(0)
// A bus at 18:00 between the two is a clock after the departure: the ferry stays ahead of it.
const bus = { type: 'transport' as const, sortKey: 0.5, data: { id: 11, type: 'bus', reservation_time: '18:00' } }
expect(rideSeatKey(ferry(), [rows[0], bus, rows[2]])).toBeNull()
expect(rideSeatKey({ ...ferry(), type: 'taxi' }, rows)).toBeNull()
})
})
// A stop with a start is drawn by it, and the road trip drives the stored order, so a
// stop joining a day from elsewhere is stored where the list will draw it.
describe('timedSlot', () => {
+44 -3
View File
@@ -1,4 +1,5 @@
import { chronoOrder } from '@trek/shared'
import { rideSeatAfter, sameDayRide, type CarrierBooking, type SeatItem } from '@trek/shared/roadtrip'
// `orderedEndpoints` is the geometry order's single source of truth, in the module
// that documents the multi-leg model. Sorting endpoints a second time here is how
// the two drift.
@@ -289,6 +290,38 @@ function applyChronoOrder(
})
}
/** A row of the day list as the seat of a ride reads it; a note is none (see rideSeatKey). */
function seatItemsOf(row: MergedItem): SeatItem[] {
if (row.type === 'place') {
const place = row.data?.place
const located = Number.isFinite(place?.lat) && Number.isFinite(place?.lng)
return [{
key: row.sortKey,
minutes: parseTimeToMinutes(place?.place_time),
point: located ? { lat: place.lat, lng: place.lng } : null,
}]
}
if (row.type === 'transport') {
return [{ key: row.sortKey, minutes: parseTimeToMinutes(row.data?.reservation_time), point: null }]
}
return []
}
/**
* Where a booking without a saved slot goes among the day's rows by where it goes: the
* key of the row it goes behind, -Infinity to open the day, or null to leave it to the
* clock. Only a ride that leaves and lands on this day, with both terminals located,
* has a say (`sameDayRide`); the rule itself is `rideSeatAfter` in @trek/shared, which
* the road trip seats its terminals with, so the list and the drive keep one order.
*
* A place counts with its time and its coordinates, another booking with its time, a
* note not at all: the clock rule this refines never read a note's time either.
*/
export function rideSeatKey(r: CarrierBooking, rows: readonly MergedItem[]): number | null {
const ride = sameDayRide(r)
return ride ? rideSeatAfter(rows.flatMap(seatItemsOf), ride) : null
}
/** Merge places, notes, and transports into a single ordered day timeline. */
export function getMergedItems(opts: {
dayAssignments: any[]
@@ -345,11 +378,19 @@ export function getMergedItems(opts: {
if (tm !== null && tm <= minutes) insertAfterKey = item.sortKey
}
}
// A ride that lands today may sit elsewhere between the same clocks, where it adds
// the least road. Without that, a ferry between two untimed stops closed the day and
// the drive went to the far shore overland before the crossing (#2461).
const seat = rideSeatKey(timed.data, result)
if (seat !== null) insertAfterKey = seat
const lastKey = result.length > 0 ? Math.max(...result.map(i => i.sortKey)) : 0
const sortKey = insertAfterKey === -Infinity
? lastKey + 0.5 + ti * 0.01
: insertAfterKey + 0.01 + ti * 0.001
const firstKey = result.length > 0 ? Math.min(...result.map(i => i.sortKey)) : 0
const sortKey = seat === -Infinity
? firstKey - 0.5 - ti * 0.01
: insertAfterKey === -Infinity
? lastKey + 0.5 + ti * 0.01
: insertAfterKey + 0.01 + ti * 0.001
result.push({ type: timed.type, sortKey, data: timed.data })
}