Files
TREK/client/src/components/Roadtrip/roadtripModel.test.ts
T
Maurice e11ae6f056 feat(roadtrip): shape the drive — vias, alternatives and stops along the way
The road-trip addon's second half: the parts that let a planned drive be
argued with rather than only read.

Reshaping. A via records which stop it follows by position, and that position
is resolved fresh every time the day is routed — so inserting, reordering or
removing a stop moved the ground under every via after it, and the drive
silently snapped back to the road it had been steered away from while the
handle still sat on the map where it was left. The correction is computed
against the day as it stands and written with the same change, in one
transaction: a half-applied re-anchoring mixes two numbering schemes in one
day. The one judgement that needs geometry — which half of a split leg a via
belongs to when a stop lands in the middle of it — is measured on the road
actually driven, so a fuel stop added to a bent leg keeps the bend on the side
it belongs to.

Alternatives. OSRM answers most long legs with exactly one route, which read as
"there is no other way" when a perfectly good slower road existed. When it
offers nothing, the same drive is asked for again without the motorway, and
failing that without the tolls — one question at a time, stopping as soon as
one lands. The offer says what it is rather than how much slower.

Stops along the way. Everything the corridor search can find is a pause, not a
destination: fuel, charging, rest areas, campsites, food and sights all sit
inside the leg they were found on, carry their own colour and icon, and are
left out of every stop count. That colour is defined once and read by the rail,
the corridor list, the map pin and the picker — and it replaced the chains'
logos, which turned a corridor full of petrol stations into a row of
advertisements and left the brands with no logo on file looking like something
else entirely.

How long a stop takes was readable in the rail and writable nowhere; new places
were given an invented hour that nobody had entered. It is now set from the
rail, cleared as a zero because the update statement folds a null into "leave
it alone", and new places start with nothing.

Both columns are rebuilt on one type scale, anchored to the day plan's own
sizes so switching tabs does not change the size of the text beside it. The
rail reads as trip totals, day cards and a chain of stops with the drives
between them; the corridor column as what to look for, how the search is
getting on, and what it found. Six category pills over three lines became one
dropdown, which is the space the results wanted.

Also: the offered routes are framed on the map when the picker opens (`fitKey`
carries no coordinates and each renderer reads it as "the selected day"), road
trip mode does not cluster at all so the whole drive stays readable zoomed out,
and the drive-time labels finally render in the app's own typeface instead of
Leaflet's Helvetica. All of it in both renderers.
2026-09-07 18:36:54 +02:00

252 lines
8.1 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import {
computeSchedule,
formatClock,
formatDurationShort,
insertIndexForAlong,
parseClock,
splitIntoRuns,
sumLegSeconds,
} from './roadtripModel'
describe('formatDurationShort', () => {
it('prints minutes below an hour', () => {
expect(formatDurationShort(45 * 60)).toBe('45 min')
expect(formatDurationShort(0)).toBe('0 min')
})
it('drops the zero minutes on a whole hour', () => {
expect(formatDurationShort(3600)).toBe('1 h')
expect(formatDurationShort(2 * 3600)).toBe('2 h')
})
it('prints hours and minutes together', () => {
expect(formatDurationShort(2 * 3600 + 10 * 60)).toBe('2 h 10 min')
})
it('carries instead of printing sixty minutes', () => {
// 1 h 59 min 40 s rounds the minutes to 60, which must become 2 h.
expect(formatDurationShort(3600 + 59 * 60 + 40)).toBe('2 h')
})
it('treats nonsense as nothing rather than throwing', () => {
expect(formatDurationShort(Number.NaN)).toBe('0 min')
expect(formatDurationShort(-90)).toBe('0 min')
})
})
describe('parseClock', () => {
it('reads wall-clock times', () => {
expect(parseClock('09:45')).toBe(9 * 60 + 45)
expect(parseClock('9:05')).toBe(9 * 60 + 5)
expect(parseClock('00:00')).toBe(0)
})
it('ignores anything trailing, as stored times sometimes carry seconds', () => {
expect(parseClock('14:30:00')).toBe(14 * 60 + 30)
})
it('rejects what is not a time', () => {
expect(parseClock(null)).toBeNull()
expect(parseClock('')).toBeNull()
expect(parseClock('later')).toBeNull()
expect(parseClock('25:00')).toBeNull()
expect(parseClock('12:75')).toBeNull()
})
})
describe('formatClock', () => {
it('pads both halves', () => {
expect(formatClock(9 * 60 + 5)).toBe('09:05')
expect(formatClock(0)).toBe('00:00')
})
it('wraps past midnight instead of printing a 25th hour', () => {
expect(formatClock(25 * 60)).toBe('01:00')
expect(formatClock(-30)).toBe('23:30')
})
})
describe('computeSchedule', () => {
const hours = (h: number): number => h * 3600
it('walks arrival and departure forward from the first pinned time', () => {
// The example from discussion #1797: leave at 8:00, drive 1 h 45, stay 2 h, drive 1 h 10.
const { entries } = computeSchedule(
[
{ anchor: '08:00', dwellMinutes: 0 },
{ anchor: null, dwellMinutes: 120 },
{ anchor: null, dwellMinutes: 45 },
],
[hours(1.75), hours(1) + 10 * 60],
)
expect(entries[0]).toMatchObject({ arrival: '08:00', departure: '08:00', anchored: true })
expect(entries[1]).toMatchObject({ arrival: '09:45', departure: '11:45', anchored: false })
expect(entries[2]).toMatchObject({ arrival: '12:55', departure: '13:40', anchored: false })
})
it('leaves stops before the first pinned time blank rather than inventing one', () => {
const { entries } = computeSchedule(
[
{ anchor: null, dwellMinutes: 60 },
{ anchor: '10:00', dwellMinutes: 30 },
],
[hours(1)],
)
expect(entries[0]).toMatchObject({ arrival: null, departure: null })
expect(entries[1]).toMatchObject({ arrival: '10:00', anchored: true })
})
it('restarts the cascade at a pinned stop instead of pushing it', () => {
// The drive would arrive at 09:00, but the museum ticket says 11:00: everything
// after it counts from 11:00, not from the drive.
const { entries } = computeSchedule(
[
{ anchor: '08:00', dwellMinutes: 0 },
{ anchor: '11:00', dwellMinutes: 60 },
{ anchor: null, dwellMinutes: 0 },
],
[hours(1), hours(1)],
)
expect(entries[1].arrival).toBe('11:00')
expect(entries[2].arrival).toBe('13:00')
})
it('reports a stop the drive cannot reach in time', () => {
const { warnings } = computeSchedule(
[
{ anchor: '08:00', dwellMinutes: 0 },
{ anchor: '09:00', dwellMinutes: 0 },
],
[hours(3)],
)
expect(warnings).toEqual([{ index: 1, code: 'late', minutes: 120 }])
})
it('does not cry about a minute of rounding', () => {
const { warnings } = computeSchedule(
[
{ anchor: '08:00', dwellMinutes: 0 },
{ anchor: '09:00', dwellMinutes: 0 },
],
[hours(1) + 30],
)
expect(warnings).toEqual([])
})
it('breaks the chain at a leg that never routed', () => {
const { entries } = computeSchedule(
[
{ anchor: '08:00', dwellMinutes: 30 },
{ anchor: null, dwellMinutes: 30 },
{ anchor: null, dwellMinutes: 0 },
],
[undefined, hours(1)],
)
expect(entries[0].arrival).toBe('08:00')
expect(entries[1].arrival).toBeNull()
expect(entries[2].arrival).toBeNull()
})
it('flags the day rolling past midnight and keeps counting', () => {
const { entries, warnings } = computeSchedule(
[
{ anchor: '22:00', dwellMinutes: 0 },
{ anchor: null, dwellMinutes: 0 },
],
[hours(4)],
)
expect(entries[1].arrival).toBe('02:00')
expect(entries[1].dayOffset).toBe(1)
expect(warnings).toEqual([{ index: 1, code: 'overnight' }])
})
it('handles an empty chain', () => {
expect(computeSchedule([], [])).toEqual({ entries: [], warnings: [] })
})
it('treats a missing dwell as no time spent', () => {
const { entries } = computeSchedule(
[
{ anchor: '08:00', dwellMinutes: null },
{ anchor: null, dwellMinutes: null },
],
[hours(1)],
)
expect(entries[0].departure).toBe('08:00')
expect(entries[1].arrival).toBe('09:00')
})
})
describe('splitIntoRuns', () => {
const drive = (): string => 'driving'
it('asks for a whole day travelled one way in a single run', () => {
const runs = splitIntoRuns(['a', 'b', 'c', 'd'], drive)
expect(runs).toEqual([{ stops: ['a', 'b', 'c', 'd'], mode: 'driving' }])
})
it('splits where the travel mode changes, and repeats the stop on both sides', () => {
// Drive to b, walk to c, drive on to d: the walk is its own request, and b and c
// each belong to two runs because they are the ends of neighbouring legs.
const modes = ['driving', 'walking', 'driving']
const runs = splitIntoRuns(['a', 'b', 'c', 'd'], (from) => modes[['a', 'b', 'c'].indexOf(from)])
expect(runs).toEqual([
{ stops: ['a', 'b'], mode: 'driving' },
{ stops: ['b', 'c'], mode: 'walking' },
{ stops: ['c', 'd'], mode: 'driving' },
])
})
it('keeps consecutive legs of the same mode together across a change and back', () => {
const modes = ['driving', 'driving', 'walking', 'walking']
const runs = splitIntoRuns(['a', 'b', 'c', 'd', 'e'], (from) => modes[['a', 'b', 'c', 'd'].indexOf(from)])
expect(runs).toEqual([
{ stops: ['a', 'b', 'c'], mode: 'driving' },
{ stops: ['c', 'd', 'e'], mode: 'walking' },
])
})
it('has nothing to ask for when there is no leg', () => {
expect(splitIntoRuns([], drive)).toEqual([])
expect(splitIntoRuns(['a'], drive)).toEqual([])
})
})
describe('sumLegSeconds', () => {
it('adds only the legs that routed', () => {
expect(sumLegSeconds([600, undefined, 1200])).toBe(1800)
expect(sumLegSeconds([])).toBe(0)
})
})
describe('insertIndexForAlong', () => {
// Hamburg 0, Lueneburg 50, Berlin 290 — the day's stops as distances driven.
const stops = [0, 50, 290]
it('puts a hit between the two stops it falls between', () => {
expect(insertIndexForAlong(stops, 120)).toBe(2)
expect(insertIndexForAlong(stops, 20)).toBe(1)
})
it('never lands before the stop the day starts from', () => {
expect(insertIndexForAlong(stops, 0)).toBe(1)
// A corridor is wider than the road, so a hit can project just behind the start.
expect(insertIndexForAlong(stops, -3)).toBe(1)
})
it('never lands after the stop the day ends at', () => {
expect(insertIndexForAlong(stops, 290)).toBe(2)
expect(insertIndexForAlong(stops, 400)).toBe(2)
})
it('puts a hit exactly on a stop after it, not before', () => {
expect(insertIndexForAlong(stops, 50)).toBe(2)
})
it('has nowhere to insert on a day that is not a drive', () => {
expect(insertIndexForAlong([], 10)).toBe(0)
expect(insertIndexForAlong([0], 10)).toBe(1)
})
})