mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-01 00:43:15 +00:00
fix(roadtrip): also take intermediate stops into account
A booking's intermediate stops are now part of the route drawn on the map, and they show up in the day list under their place name. They no longer need the pickup and return times to be filled in. Roadtrips that span several days work properly too. A car picked up on one day and returned on the next showed both days as empty, and only the first half of the drive was drawn.
This commit is contained in:
@@ -69,7 +69,7 @@ export const roadtripCardStopSchema = z.object({
|
||||
.object({
|
||||
reservationId: idSchema,
|
||||
type: z.string().min(1).max(20),
|
||||
role: z.enum(['departure', 'arrival', 'pickup', 'return']),
|
||||
role: z.enum(['departure', 'arrival', 'pickup', 'return', 'stop']),
|
||||
title: cardText(80),
|
||||
code: z.string().min(1).max(8).nullable(),
|
||||
at: cardClockSchema.nullable(),
|
||||
|
||||
@@ -118,10 +118,10 @@ describe('assembleRoadtrip connected days', () => {
|
||||
],
|
||||
});
|
||||
|
||||
function assembleTwoDays(connectDays: boolean) {
|
||||
function assembleTwoDays(connectDays: boolean, opensSecond: Partial<RoadtripStop> = {}) {
|
||||
const first = [stop({ ownerIndex: 0 }), stop({ ownerIndex: 1 }), stop({ ownerIndex: 2 })];
|
||||
const second = [
|
||||
stop({ ownerDayId: 2, ownerIndex: 0, assignmentId: 300, placeId: 400, lat: 60 }),
|
||||
stop({ ownerDayId: 2, ownerIndex: 0, assignmentId: 300, placeId: 400, lat: 60, ...opensSecond }),
|
||||
stop({ ownerDayId: 2, ownerIndex: 1, assignmentId: 301, placeId: 401, lat: 61 }),
|
||||
];
|
||||
const allLegs: Record<string, RoutedLeg> = {};
|
||||
@@ -201,6 +201,23 @@ describe('assembleRoadtrip connected days', () => {
|
||||
expect(routes.days[1]!.geometry[0]).toEqual([60, 10]);
|
||||
});
|
||||
|
||||
it('ROADTRIP-ASSEMBLE-028: the drive back to a hire car’s desk is drawn without one continuous drive', () => {
|
||||
// A car taken out in the morning and handed back after midnight: the desk opens the
|
||||
// day it goes back on, and the road to it is the booking's own rather than the gap a
|
||||
// day-at-a-time routing leaves. Drawn only for a connected trip, the day it set out
|
||||
// on was drawn and nothing led from there to the desk, which reads as a drive that
|
||||
// stops halfway.
|
||||
const routes = assembleTwoDays(false, {
|
||||
carrier: { reservationId: 42, type: 'car', role: 'return', title: 'Sixt Hamburg', code: null, at: '02:00' },
|
||||
});
|
||||
|
||||
expect(routes.days[1]!.arrivingFrom).toEqual(expect.objectContaining({ ownerDayId: 1, ownerIndex: 2 }));
|
||||
expect(routes.days[1]!.geometry[0]).toEqual([52, 10]);
|
||||
// Still the road leading into the day it arrives on, the way a connected one is.
|
||||
expect(routes.lineDays).toEqual([1, 1, 2, 2]);
|
||||
expect(routes.lineJoins).toEqual([false, false, true, false]);
|
||||
});
|
||||
|
||||
it('ROADTRIP-ASSEMBLE-006: that drive is marked as the connection it is, in the colour of the day it arrives on (#3123)', () => {
|
||||
const routes = assembleTwoDays(true);
|
||||
|
||||
|
||||
@@ -120,14 +120,21 @@ export function assembleRoadtrip({
|
||||
let arrivingLeg: RouteSegment | undefined;
|
||||
let arrivingLine: [number, number][] | undefined;
|
||||
let arrivingFrom: RoadtripStop | undefined;
|
||||
const first = chain.stops[0];
|
||||
// The drive back to a hire car's desk is the booking's own road rather than the gap a
|
||||
// day-at-a-time routing leaves between two cards, so it is drawn whether or not one
|
||||
// continuous drive was asked for. A car taken out in the morning and handed back after
|
||||
// midnight had the day it set out on drawn and nothing from there to the desk, which
|
||||
// reads as a drive that stops halfway.
|
||||
const ownDrive = first?.carrier?.role === 'return';
|
||||
if (connectDays && !automaticSchedule) {
|
||||
for (const spill of chain.spills) {
|
||||
inboundAt.set(spill.at, { seg: spill.leg, line: spill.line });
|
||||
}
|
||||
|
||||
}
|
||||
if ((connectDays || ownDrive) && !automaticSchedule) {
|
||||
// No band for a night spent at one hotel: nothing is driven between the evening and
|
||||
// the morning, and a band would read "0 km" between a hotel and itself.
|
||||
const first = chain.stops[0];
|
||||
const joined =
|
||||
inboundAt.has(0) || !previousStop || !first || isStationaryJoin(previousStop, first)
|
||||
? undefined
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
*/
|
||||
import { assembleRoadtrip } from './assemble';
|
||||
import {
|
||||
carrierStopAssignmentId,
|
||||
CHECK_IN_MINUTES,
|
||||
carrierClock,
|
||||
carrierLeg,
|
||||
@@ -40,6 +41,7 @@ import {
|
||||
} from './carriers';
|
||||
import { planDayWindow } from './dayWindow';
|
||||
import type { PlanDay, RoadtripStop, RoutedLeg } from './planning-types';
|
||||
import { standsAsDay } from './roadtripModel';
|
||||
import { roadtripLegKey } from './routeRun';
|
||||
|
||||
import { describe, expect, it } from 'vitest';
|
||||
@@ -1051,3 +1053,117 @@ describe('tonight’s stay at the far end of a ride within one day', () => {
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('the stops a hire car names between its desks', () => {
|
||||
const driven = (over = {}) =>
|
||||
({
|
||||
id: 42,
|
||||
type: 'car',
|
||||
title: 'Sixt Hamburg',
|
||||
day_id: 1,
|
||||
end_day_id: 1,
|
||||
reservation_time: '2026-06-01T10:00',
|
||||
endpoints: [
|
||||
{ role: 'from', sequence: 0, name: 'Hamburg', code: null, lat: 53.5511, lng: 9.9937 },
|
||||
{ role: 'stop', sequence: 1, name: 'Lüneburg', code: null, lat: 53.2464, lng: 10.4115 },
|
||||
{ role: 'to', sequence: 2, name: 'Berlin', code: null, lat: 52.52, lng: 13.405 },
|
||||
],
|
||||
...over,
|
||||
}) as CarrierBooking;
|
||||
|
||||
it('ROADTRIP-CARRIERS-038: a hire car keeps the places it is driven through, a ride keeps none', () => {
|
||||
expect(carrierSeam(driven())!.waypoints).toEqual([{ lat: 53.2464, lng: 10.4115, name: 'Lüneburg' }]);
|
||||
// A train calls at its intermediate stations; nobody drives between them.
|
||||
expect(carrierSeam(driven({ type: 'train' }))!.waypoints).toEqual([]);
|
||||
// Nothing between the two desks is nothing to drive through.
|
||||
expect(
|
||||
carrierSeam(
|
||||
driven({
|
||||
endpoints: [
|
||||
{ role: 'from', sequence: 0, name: 'Hamburg', code: null, lat: 53.5511, lng: 9.9937 },
|
||||
{ role: 'to', sequence: 1, name: 'Berlin', code: null, lat: 52.52, lng: 13.405 },
|
||||
],
|
||||
}),
|
||||
)!.waypoints,
|
||||
).toEqual([]);
|
||||
});
|
||||
|
||||
/** The day the car is taken out and handed back on, with a stop of the day's own. */
|
||||
const sameDay = (over = {}) =>
|
||||
carrierSeam(driven({ end_day_id: 1, reservation_end_time: '2026-06-01T18:00', ...over }))!;
|
||||
|
||||
it('ROADTRIP-CARRIERS-039: the places it is driven through are stops of the drive, behind the pick-up', () => {
|
||||
const seated = seatCarrierStops(1, [stop({ ownerIndex: 0, time: '12:00' })], [0], [sameDay()]);
|
||||
// Taken out, driven through Lüneburg, and the day's own stop still on the road.
|
||||
expect(seated.map((s) => (s.carrier ? `${s.carrier.role}:${s.name}` : s.name))).toEqual([
|
||||
'pickup:Hamburg',
|
||||
'stop:Lüneburg',
|
||||
'Stop 0',
|
||||
'return:Berlin',
|
||||
]);
|
||||
const through = seated[1]!;
|
||||
// A place driven through keeps no minutes of its own: the drive passes and goes on.
|
||||
expect(through).toMatchObject({
|
||||
dwellMinutes: 0,
|
||||
time: null,
|
||||
leaveAt: null,
|
||||
night: false,
|
||||
legMode: null,
|
||||
incomingLegMode: null,
|
||||
lat: 53.2464,
|
||||
lng: 10.4115,
|
||||
});
|
||||
// An id of its own, below every terminal's, so no stop of the day collides with it.
|
||||
expect(through.assignmentId).toBe(carrierStopAssignmentId(42, 0));
|
||||
expect(through.assignmentId).toBeLessThan(terminalAssignmentId(42, 'return'));
|
||||
});
|
||||
|
||||
it('ROADTRIP-CARRIERS-040: a car still out tomorrow puts them on the day it was taken out, and a ride puts none', () => {
|
||||
// The road leaves the pick-up and runs over them whether the car goes back that day
|
||||
// or days later. Kept off the day it sets off, a hire car booked across two days had
|
||||
// one desk on each and the drive ran over neither of the places it was booked through.
|
||||
const stillOut = seatCarrierStops(1, [stop({ ownerIndex: 0 })], [0], [carrierSeam(driven({ end_day_id: 3 }))!]);
|
||||
expect(stillOut.map((s) => s.carrier?.role ?? s.name)).toEqual(['pickup', 'stop', 'Stop 0']);
|
||||
// The day it goes back on holds its desk, and stands as a day on that alone.
|
||||
expect(standsAsDay(seatCarrierStops(3, [], [], [carrierSeam(driven({ end_day_id: 3 }))!]))).toBe(true);
|
||||
// A train calls at its intermediate stations and nobody drives between them.
|
||||
const ride = seatCarrierStops(
|
||||
1,
|
||||
[stop({ ownerIndex: 0 })],
|
||||
[0],
|
||||
[carrierSeam(driven({ type: 'train', end_day_id: 1, reservation_end_time: '2026-06-01T18:00' }))!],
|
||||
);
|
||||
expect(ride.map((s) => s.carrier?.role ?? s.name).sort()).toEqual(['Stop 0', 'arrival', 'departure']);
|
||||
// Two of them keep the booking's own order.
|
||||
const twice = sameDay({
|
||||
endpoints: [
|
||||
{ role: 'from', sequence: 0, name: 'Hamburg', code: null, lat: 53.5511, lng: 9.9937 },
|
||||
{ role: 'stop', sequence: 1, name: 'Lüneburg', code: null, lat: 53.2464, lng: 10.4115 },
|
||||
{ role: 'stop', sequence: 2, name: 'Wittenberge', code: null, lat: 52.9939, lng: 11.7536 },
|
||||
{ role: 'to', sequence: 3, name: 'Berlin', code: null, lat: 52.52, lng: 13.405 },
|
||||
],
|
||||
});
|
||||
expect(
|
||||
seatCarrierStops(1, [stop({ ownerIndex: 0 })], [0], [twice])
|
||||
.filter((s) => s.carrier?.role === 'stop')
|
||||
.map((s) => s.name),
|
||||
).toEqual(['Lüneburg', 'Wittenberge']);
|
||||
});
|
||||
|
||||
it('ROADTRIP-CARRIERS-041: a booking that states no hours still puts the places it is driven through on the drive', () => {
|
||||
// Reported on a car booked with neither hour filled in: the desks are seated by the
|
||||
// day's shape alone (the pick-up opens it, the hand-back closes it), and the places
|
||||
// between them are the booking's whatever the clock says. Stating the hours is what
|
||||
// made the road run over them before, which is no part of what they are.
|
||||
const noHours = carrierSeam(driven({ reservation_time: null, reservation_end_time: null, end_day_id: 1 }))!;
|
||||
expect(noHours.arrival).toMatchObject({ dayId: 1, clock: null, name: 'Berlin' });
|
||||
const seated = seatCarrierStops(1, [stop({ ownerIndex: 0 })], [0], [noHours]);
|
||||
expect(seated.map((s) => (s.carrier ? `${s.carrier.role}:${s.name}` : s.name))).toEqual([
|
||||
'pickup:Hamburg',
|
||||
'stop:Lüneburg',
|
||||
'Stop 0',
|
||||
'return:Berlin',
|
||||
]);
|
||||
expect(seated[1]).toMatchObject({ dwellMinutes: 0, time: null });
|
||||
});
|
||||
});
|
||||
|
||||
@@ -108,6 +108,7 @@ export interface CarrierSeam {
|
||||
* booking by its own clock, so the seat by geography (`rideSeatAfter`) leaves it alone.
|
||||
*/
|
||||
stopover: boolean;
|
||||
waypoints: { lat: number; lng: number; name: string }[];
|
||||
departure: CarrierEnd;
|
||||
/**
|
||||
* A ride always has both ends. A hire car has a return only when the booking says
|
||||
@@ -178,7 +179,7 @@ function isLocated(e: Endpoint): e is LocatedEndpoint {
|
||||
function seamEnds(
|
||||
booking: CarrierBooking,
|
||||
kind: CarrierSeam['kind'],
|
||||
): { from: LocatedEndpoint; to: LocatedEndpoint | undefined } | null {
|
||||
): { from: LocatedEndpoint; to: LocatedEndpoint | undefined; between: LocatedEndpoint[] } | null {
|
||||
const located = (booking.endpoints ?? []).filter(isLocated).sort((a, b) => (a.sequence ?? 0) - (b.sequence ?? 0));
|
||||
const from =
|
||||
kind === 'ride' ? (located.find((e) => e.role === 'from') ?? located[0]) : located.find((e) => e.role === 'from');
|
||||
@@ -188,7 +189,8 @@ function seamEnds(
|
||||
: [...located].reverse().find((e) => e.role === 'to');
|
||||
if (!from) return null;
|
||||
if (kind === 'ride' && (!to || from === to)) return null;
|
||||
return { from, to };
|
||||
const between = kind === 'rental' && to ? located.slice(located.indexOf(from) + 1, located.indexOf(to)) : [];
|
||||
return { from, to, between };
|
||||
}
|
||||
|
||||
/** The day a booking leaves on: its first leg's, else its own. Null when it names none. */
|
||||
@@ -213,7 +215,7 @@ export function carrierSeam(booking: CarrierBooking): CarrierSeam | null {
|
||||
if (!kind) return null;
|
||||
const ends = seamEnds(booking, kind);
|
||||
if (!ends) return null;
|
||||
const { from, to } = ends;
|
||||
const { from, to, between } = ends;
|
||||
|
||||
const legs = legsOf(booking);
|
||||
const first = legs[0];
|
||||
@@ -250,6 +252,7 @@ export function carrierSeam(booking: CarrierBooking): CarrierSeam | null {
|
||||
title: booking.title,
|
||||
kind,
|
||||
stopover: legs.length > 1,
|
||||
waypoints: between.map((e) => ({ lat: e.lat, lng: e.lng, name: e.name })),
|
||||
departure: {
|
||||
dayId: depDayId,
|
||||
position: positionOn(first?.day_positions, depDayId) ?? bookingPosition(depDayId),
|
||||
@@ -429,6 +432,18 @@ export function terminalAssignmentId(reservationId: number, role: CarrierTermina
|
||||
return TERMINAL_ID_BASE - reservationId * 2 - (role === 'arrival' || role === 'return' ? 1 : 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Below the terminals, which take two ids per booking: a booking's stops need one id each,
|
||||
* and there is no room for them between its two desks. A booking names at most
|
||||
* `MAX_CARRIER_STOPS` of them, so an id of one booking never reaches the next one's.
|
||||
*/
|
||||
const CARRIER_STOP_ID_BASE = -4_000_000_000;
|
||||
const MAX_CARRIER_STOPS = 64;
|
||||
|
||||
export function carrierStopAssignmentId(reservationId: number, sequence: number): number {
|
||||
return CARRIER_STOP_ID_BASE - reservationId * MAX_CARRIER_STOPS - (sequence % MAX_CARRIER_STOPS);
|
||||
}
|
||||
|
||||
/** The two roles of a seam, by which end of it a terminal stands at. */
|
||||
function rolesOf(seam: CarrierSeam): { start: CarrierTerminal['role']; end: CarrierTerminal['role'] } {
|
||||
return seam.kind === 'rental' ? { start: 'pickup', end: 'return' } : { start: 'departure', end: 'arrival' };
|
||||
@@ -480,6 +495,52 @@ function terminalStop(
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* A place a hire car's booking names between its desks, as a stop of the drive.
|
||||
*
|
||||
* A car taken Hamburg → Lüneburg → Berlin is driven through Lüneburg, so the drive stops
|
||||
* there on the way: the day plan has always routed through it (`orderedWaypoints`), while
|
||||
* the road trip saw only the two desks and drew the short way round, which reads as a
|
||||
* shorter journey than the one booked, on a road nobody takes. It is a stop the booking
|
||||
* makes rather than a place the trip is for, so it keeps no minutes of its own — the drive
|
||||
* passes through and goes on — and carries no number on the rail, as a desk carries none.
|
||||
*/
|
||||
function carrierWaypointStop(
|
||||
seam: CarrierSeam,
|
||||
point: CarrierSeam['waypoints'][number],
|
||||
sequence: number,
|
||||
dayId: number,
|
||||
ownerIndex: number,
|
||||
): RoadtripStop {
|
||||
return {
|
||||
carrier: {
|
||||
reservationId: seam.reservationId,
|
||||
type: seam.type,
|
||||
role: 'stop',
|
||||
title: seam.title,
|
||||
code: null,
|
||||
at: null,
|
||||
},
|
||||
assignmentId: carrierStopAssignmentId(seam.reservationId, sequence),
|
||||
ownerDayId: dayId,
|
||||
ownerIndex,
|
||||
placeId: -seam.reservationId,
|
||||
name: point.name,
|
||||
lat: point.lat,
|
||||
lng: point.lng,
|
||||
time: null,
|
||||
leaveAt: null,
|
||||
dwellMinutes: 0,
|
||||
checkInTime: null,
|
||||
night: false,
|
||||
endDay: false,
|
||||
legMode: null,
|
||||
incomingLegMode: null,
|
||||
stopType: null,
|
||||
fillPercent: null,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Tonight's stays that wait behind a ride of their check-in day, by their index among the
|
||||
* day's stops, each with the rides that land nearer it than they leave.
|
||||
@@ -674,6 +735,16 @@ export function seatCarrierStops(
|
||||
// The index of the stored stop that follows the one the terminal is seated behind.
|
||||
const insertAt = slot.after + 1;
|
||||
out.push(terminalStop(slot.seam, slot.role, dayId, insertAt));
|
||||
// The places the booking is driven through follow its pick-up, in its own order, as
|
||||
// stops of the drive: the road leaves that desk and runs over them, whether the car
|
||||
// goes back the same day or days later. Held to the day it is taken out on, because
|
||||
// that is the one day the booking names for the drive; a stop the traveller reaches
|
||||
// on a later day of a long hire is theirs to move, and moving it is what says so.
|
||||
if (slot.role === 'pickup') {
|
||||
slot.seam.waypoints.forEach((point, sequence) =>
|
||||
out.push(carrierWaypointStop(slot.seam, point, sequence, dayId, insertAt)),
|
||||
);
|
||||
}
|
||||
if (slot.role === 'departure' && slot.seam.arrival?.dayId === dayId) {
|
||||
out.push(terminalStop(slot.seam, 'arrival', dayId, insertAt));
|
||||
for (const index of waiting.get(slot.seam) ?? []) out.push(reachedFrom(stops[index]!, slot.seam));
|
||||
|
||||
@@ -19,7 +19,12 @@ export interface CarrierTerminal {
|
||||
reservationId: number;
|
||||
/** flight | train | ferry | cruise | bus for a ride, car for a hire car. */
|
||||
type: string;
|
||||
role: 'departure' | 'arrival' | 'pickup' | 'return';
|
||||
/**
|
||||
* Which end of the booking this is, or `stop` for a place it names between its ends: a
|
||||
* hire car taken Hamburg → Lüneburg → Berlin is driven through Lüneburg, and that is a
|
||||
* stop of the drive rather than a desk (`seatCarrierStops`).
|
||||
*/
|
||||
role: 'departure' | 'arrival' | 'pickup' | 'return' | 'stop';
|
||||
/** The booking's title, which is what the rail prints on the ride. */
|
||||
title: string;
|
||||
/** IATA code or station code, when the booking carries one. */
|
||||
|
||||
@@ -407,9 +407,16 @@ export function isStoredStop(stop: Pick<RoadtripStop, 'automaticNight' | 'carrie
|
||||
* used to need company, and a hotel entered under Days for the arrival day was filed
|
||||
* at the bottom of the rail under "only X so far", which reads as the hotel missing
|
||||
* from the road trip altogether.
|
||||
*
|
||||
* So does a booking's own terminal. A car taken out on one day and handed back on the
|
||||
* next leaves one desk on each of them, and a day holding a single desk read as quiet:
|
||||
* a hire car booked across two days was listed under "nothing planned yet" on both,
|
||||
* which reads as the drive missing from the road trip altogether, and the one day the
|
||||
* traveller set out on asked them for stops. A desk or a terminal is something the day
|
||||
* holds, whatever else it does not.
|
||||
*/
|
||||
export function standsAsDay(stops: readonly { night?: boolean }[]): boolean {
|
||||
return stops.length > 1 || stops.some((stop) => stop.night === true);
|
||||
export function standsAsDay(stops: readonly { night?: boolean; carrier?: unknown }[]): boolean {
|
||||
return stops.length > 1 || stops.some((stop) => stop.night === true || !!stop.carrier);
|
||||
}
|
||||
|
||||
export function splitIntoRuns<T>(stops: T[], modeOfLeg: (from: T, to: T) => string): { stops: T[]; mode: string }[] {
|
||||
|
||||
Reference in New Issue
Block a user