fix(planner): keep timed stops in their local zone when flights take their clock slots

The zoned flights traded clock times among themselves while stops kept
theirs, so a dinner timed after a flight landed could be drawn before it.
A timed stop near one of the day's zoned endpoints now joins the swap in
that endpoint's zone.
This commit is contained in:
Maurice
2026-11-05 13:02:25 +01:00
parent c3357ebea4
commit 7b5a05afee
2 changed files with 114 additions and 17 deletions
+53 -4
View File
@@ -716,14 +716,63 @@ describe('getMergedItems: bookings in different time zones (#2929)', () => {
}
})
it('keeps the stops of the day where their clock puts them', () => {
it('keeps a stop with no location, and a note, where their clock puts them', () => {
const dayAssignments = [
{ id: 10, order_index: 0, place: { place_time: '09:00' } },
{ id: 11, order_index: 1, place: { place_time: '18:00' } },
]
const result = getMergedItems({ dayAssignments, dayNotes: [], dayTransports: [toSeattle, toLA], dayId: DAY })
// The flights swap the slots they hold between them (17:00 and 20:20); the stops stay put.
expect(result.map(i => `${i.type}:${i.data.id}`)).toEqual(['place:10', 'transport:1', 'place:11', 'transport:2'])
const dayNotes = [{ id: 30, sort_order: 2, time: '18:30' }]
const result = getMergedItems({ dayAssignments, dayNotes, dayTransports: [toSeattle, toLA], dayId: DAY })
// The flights swap the slots they hold between them (17:00 and 20:20). Without
// coordinates there is no zone to read 18:00 or 18:30 in, so those keep their clock.
expect(result.map(i => `${i.type}:${i.data.id}`)).toEqual(['place:10', 'transport:1', 'place:11', 'note:30', 'transport:2'])
})
describe('a stop near an endpoint reads the zone of that endpoint', () => {
const AKL = { lat: -37.0, lng: 174.8, timezone: 'Pacific/Auckland' }
const LAX = { lat: 33.9, lng: -118.4, timezone: 'America/Los_Angeles' }
const SEA = { lat: 47.4, lng: -122.3, timezone: 'America/Los_Angeles' }
const ends = (from: typeof AKL, to: typeof AKL) => [
{ role: 'from', sequence: 0, name: 'A', code: null, local_time: null, local_date: null, ...from },
{ role: 'to', sequence: 1, name: 'B', code: null, local_time: null, local_date: null, ...to },
]
const aklLax = { ...toLA, endpoints: ends(AKL, LAX) }
const laxSea = { ...toSeattle, endpoints: ends(LAX, SEA) }
const stopAt = (id: number, time: string, lat: number, lng: number) =>
({ id, order_index: id, place: { place_time: time, lat, lng } })
const drawn = (dayAssignments: any[], dayTransports: any[]) =>
getMergedItems({ dayAssignments, dayNotes: [], dayTransports, dayId: DAY })
const labels = (items: MergedItem[]) => items.map(i => `${i.type}:${i.data.id}`)
it('draws a Seattle dinner after the flight to Seattle, and an Auckland breakfast first', () => {
const stops = [stopAt(10, '09:00', -36.85, 174.76), stopAt(12, '19:45', 47.61, -122.33)]
const items = drawn(stops, [laxSea, aklLax])
expect(labels(items)).toEqual(['place:10', 'transport:1', 'transport:2', 'place:12'])
// The move checks read the same keys, so the drawn order reads as in order.
const keys = items.map(chronoKeyOf(items, DAY, getDisplayTimeForDay))
expect(keys).toEqual([9 * 60, 17 * 60, 19 * 60 + 45, 20 * 60 + 20])
})
it('draws a dinner in Auckland at the same clock ahead of the flight out', () => {
expect(labels(drawn([stopAt(11, '19:45', -36.85, 174.76)], [laxSea, aklLax])))
.toEqual(['place:11', 'transport:1', 'transport:2'])
})
it('draws a lunch in Los Angeles after the Auckland flight that lands there', () => {
expect(labels(drawn([stopAt(13, '13:00', 34.05, -118.24)], [aklLax]))).toEqual(['transport:1', 'place:13'])
})
it('leaves a stop far from every endpoint on its clock', () => {
// Hawaii is no day out from Auckland, Los Angeles or Seattle, so its 19:45 reads no zone.
expect(labels(drawn([stopAt(14, '19:45', 21.3, -157.85)], [laxSea, aklLax])))
.toEqual(['transport:1', 'place:14', 'transport:2'])
})
it('changes nothing on a day whose stops and bookings share one zone', () => {
const hop = { ...laxSea, id: 4, reservation_time: '2026-11-20T08:00', endpoints: ends(LAX, SEA) }
const stops = [stopAt(15, '07:00', 34.05, -118.24), stopAt(16, '12:00', 47.61, -122.33), stopAt(17, '18:00', 47.61, -122.33)]
expect(labels(drawn(stops, [laxSea, hop]))).toEqual(['place:15', 'transport:4', 'place:16', 'transport:2', 'place:17'])
})
})
it('reads the zone from the endpoints when the metadata names none', () => {
+61 -13
View File
@@ -4,6 +4,7 @@ import { nightVisits, rideSeatAfter, sameDayRide, type CarrierBooking, type Seat
// that documents the multi-leg model. Sorting endpoints a second time here is how
// the two drift.
import { orderedEndpoints, parseReservationMetadata } from './flightLegs'
import { haversineKm, MAX_DAY_TRIP_KM } from './geo'
import { zonedInstant } from './zonedTime'
export const TRANSPORT_TYPES = new Set(['flight', 'train', 'bus', 'car', 'taxi', 'bicycle', 'cruise', 'ferry', 'cable_car', 'transit', 'transport_other'])
@@ -361,9 +362,10 @@ function applyChronoOrder(
/**
* The minutes the day orders an item by: its own clock, or for a booking in a known
* zone the clock slot it takes among the day's zoned bookings (#2929). The checks
* that ask whether a move keeps the day in order read the same key, or a day whose
* flights were put in the order they happen would read as out of order on every move.
* zone and a stop near one of its endpoints the clock slot it takes among the day's
* zoned items (#2929, see zonedClockSlots). The checks that ask whether a move keeps the
* day in order read the same key, or a day whose flights and stops were put in the order
* they happen would read as out of order on every move.
*/
export function chronoKeyOf(
items: readonly MergedItem[],
@@ -399,13 +401,22 @@ function transportTimeZone(r: MergedItem['data'], dayId: number): string | null
}
/**
* The clock slots of the day's bookings whose time names an instant: a dated time in a
* known zone. Each local time reads in its own zone, so across zones the clock can run
* backwards: a flight leaving Auckland at 20:20 lands in Los Angeles at 12:25 the same
* date, and the 17:00 flight on from there sorted ahead of it (#2929). Such bookings
* keep the clock slots they hold between them and take them in the order they happen,
* so stops, notes and bookings without a zone stay where the clock puts them. With fewer
* than two the map is empty and the day orders by the clock as before.
* The clock slots of the day's items whose time names an instant: a booking's dated time
* in a known zone, and a stop near one of those bookings' endpoints. Each local time reads
* in its own zone, so across zones the clock can run backwards: a flight leaving Auckland
* at 20:20 lands in Los Angeles at 12:25 the same date, and the 17:00 flight on from there
* sorted ahead of it (#2929). Such items keep the clock slots they hold between them and
* take them in the order they happen.
*
* A stop has to join them, or swapping the flights' slots moves every stop timed between
* the two clocks to the wrong side of the flight it belongs after: a 19:45 dinner in
* Seattle was drawn above the flight to Seattle. A stop carries no zone of its own, so it
* reads the zone of the nearest endpoint within a day's reach, which tells the 19:45
* dinner in Seattle from a 19:45 dinner in Auckland before the flight out. Notes, stops
* without coordinates or far from every endpoint, and bookings without a zone stay where
* their clock puts them. With fewer than two such items the map is empty and the day
* orders by the clock as before. Where every zone is the same, the order by instant is
* the order by clock and nothing moves.
*/
function zonedClockSlots(
items: MergedItem[],
@@ -413,12 +424,14 @@ function zonedClockSlots(
getDisplayTime: DisplayTimeOf,
clockOf: (it: MergedItem) => number | null
): Map<MergedItem, number> {
const zoned = items.flatMap((it, index) => {
const bookings = items.flatMap((it, index) => {
if (it.type !== 'transport') return []
const clock = clockOf(it)
const instant = zonedInstant(getDisplayTime(it.data, dayId), transportTimeZone(it.data, dayId))
return clock === null || instant === null ? [] : [{ it, index, clock, instant }]
const time = getDisplayTime(it.data, dayId)
const instant = zonedInstant(time, transportTimeZone(it.data, dayId))
return clock === null || instant === null ? [] : [{ it, index, clock, instant, date: (time as string).slice(0, 10) }]
})
const zoned = [...bookings, ...stopsInZone(items, bookings, clockOf)]
const slots = new Map<MergedItem, number>()
if (zoned.length < 2) return slots
const clocks = zoned.map(z => z.clock).sort((a, b) => a - b)
@@ -472,6 +485,41 @@ export function yieldTimedBookings(
return drawnAsIs(result, dayId, getDisplayTime) ? result : null
}
/**
* The timed stops of the day that sit within a day's reach of an endpoint of the zoned
* bookings, with the instant their clock names in that endpoint's zone. The date is the
* one the bookings' times carry on this day; bookings that disagree on it leave every
* stop on its clock.
*/
function stopsInZone(
items: MergedItem[],
bookings: readonly { it: MergedItem; date: string }[],
clockOf: (it: MergedItem) => number | null
): { it: MergedItem; index: number; clock: number; instant: number }[] {
const dates = new Set(bookings.map(b => b.date))
if (dates.size !== 1) return []
const [date] = dates
const anchors = bookings.flatMap(b => orderedEndpoints(b.it.data))
.filter(e => Number.isFinite(e.lat) && Number.isFinite(e.lng) && !!e.timezone)
if (anchors.length === 0) return []
return items.flatMap((it, index) => {
if (it.type !== 'place') return []
const clock = clockOf(it)
const place = it.data?.place
if (clock === null || !Number.isFinite(place?.lat) || !Number.isFinite(place?.lng)) return []
let zone: string | null = null
let nearest = MAX_DAY_TRIP_KM
for (const e of anchors) {
const km = haversineKm({ lat: place.lat, lng: place.lng }, { lat: e.lat, lng: e.lng })
if (km <= nearest) { nearest = km; zone = e.timezone as string }
}
const hh = String(Math.floor(clock / 60)).padStart(2, '0')
const mm = String(clock % 60).padStart(2, '0')
const instant = zone ? zonedInstant(`${date}T${hh}:${mm}`, zone) : null
return instant === null ? [] : [{ it, index, clock, instant }]
})
}
/**
* A day order with a dragged booking or note at `index`, moved to the nearest slot the
* list draws it in. A row with a time is drawn by it, and the untimed rows behind it