Files
TREK/client/src/components/Map/tripRouteGeometry.test.ts
T
Maurice ed18c37903 fix: the findings of the final 4.3.0 audit
Every file the release touches was read once more, and what survived an
independent check against the code is fixed here, with a test for each.

Data and credentials
- A document sync connection edited to a new address no longer carries the
  stored token along: a blank form under another origin is refused, the same
  rule the probe route already had. Dawarich and AirTrail testConnection send
  the stored key only to the host it was stored for.
- The key rotation script now covers document_connections.secrets and
  trip_document_links.webhook_secret, which it had skipped.
- Turning a booked night into a pause in the road trip popup asks first and
  says that the booking and its expense go with it. Deleting a place says the
  same when a night is booked there, on the desktop and on the phone.
- A moved version marker over unchanged bytes no longer replaces the TREK file
  and its attachments; a real revision carries the attachments over.

Planner
- A drop in the day plan names the row it lands ahead of in the full day, so a
  hidden hotel stop no longer shifts it one slot early; the store sorts before
  it splices. Via re-anchoring, the booked-night guard and the sidebar's day
  membership read the stored day, the index space the server uses.
- A hotel stop placed mid-day re-pins the road trip vias of that day, and the
  booking session receives the new stop order too. The Public API and the iCal
  feed list a booked hotel once. A plugin can no longer write a stay against
  another trip's day.

Road trip and map
- The route overview paces its requests to the public routers and retries a
  rate limit, and it says which legs it could not route instead of quietly
  reporting a shorter trip. A click on a via handle no longer drops a second
  via; handles hide when zooming out. Read-only members cannot edit the driving
  limits. The phone shows a way back when no day is picked and prints the night
  drive departure in the chosen clock format.

Integrations and search
- Google transit keeps conventional trains when the train chip is on.
  TREK_PLACES_ENABLED=false stops the details and enrichment calls too, and
  accepts the whole boolean family. A caller-supplied description no longer
  lands in the instance-wide enrichment cache. Nominatim searches carry a
  timeout. An OSM place id is checked for shape before it reaches Overpass.
- Dawarich puts a stay on the trip whose dates hold it, the Atlas hand-off
  knows what the Atlas already shows, and a host change with a new key clears
  the old instance's state.
- Document sync: a webhook address pasted by hand works on the token alone,
  deleting a connection unregisters its subscriptions, Nextcloud under a
  sub-path connects, Synology answers a name collision before uploading and
  verifies a move into the bin, Paperless titles are capped at 128 characters,
  a DNS failure reads as unreachable rather than as a policy verdict, videos
  from the store are accepted like uploads, and remote_missing has a way out.
  Conflict buttons only for the owner, a refused choice is reported, a refused
  credential gets a Reconnect button, disconnecting asks first on the desktop
  as it does on the phone, and the folder picker no longer spins forever.

Release
- The server holds the release notice back from a bundle that cannot draw it,
  so the previous shell served from the service worker cache after an update
  no longer shows bare keys and no longer lets a click use the notice up.
- Import into an existing list gets the same body limit as a new list. Chat
  images say when the limit is reached and the new controls are translated.
  The transit backend switch reports a failed save. The Helm chart carries the
  Amap credentials in its Secret; the shipped templates document the Places
  API and Amap variables and the Overpass default. The wiki follows.
2026-09-20 13:54:31 +02:00

216 lines
9.2 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest'
import { dayColor } from '../Roadtrip/dayColors'
import { dayRouteColor, planTripRoute, routeTrip, summariseTripRoute } from './tripRouteGeometry'
import { buildAssignment, buildDay, buildPlace } from '../../../tests/helpers/factories'
import type { AssignmentsMap, RouteSegment } from '../../types'
vi.mock('./RouteCalculator', async (importActual) => {
const actual = await importActual<typeof import('./RouteCalculator')>()
return { ...actual, calculateRouteWithLegs: vi.fn() }
})
const { calculateRouteWithLegs, RoutingRefusedError } = await import('./RouteCalculator')
const leg = (distance: number): RouteSegment => ({
mid: [0, 0], from: [0, 0], to: [0, 0], distance, duration: 600,
distanceText: '10 km', durationText: '10 min', walkingText: '2 h', drivingText: '10 min',
})
/** A router that never answers but honours its signal, exactly as a real fetch does —
* without that the abort has nothing to cut and the pool waits forever. */
const neverAnswers = () => vi.mocked(calculateRouteWithLegs).mockImplementation(
(_waypoints, opts) => new Promise((_resolve, reject) => {
opts?.signal?.addEventListener(
'abort',
() => reject(new DOMException('Aborted', 'AbortError')),
{ once: true },
)
}) as ReturnType<typeof calculateRouteWithLegs>,
)
const at = (lat: number, lng: number, order: number, dayId: number) =>
buildAssignment({ day_id: dayId, order_index: order, place: buildPlace({ lat, lng }) })
const DAYS = [buildDay({ id: 1, day_number: 1 }), buildDay({ id: 2, day_number: 2 })]
const ASSIGNMENTS: AssignmentsMap = {
'1': [at(48.86, 2.35, 0, 1), at(48.90, 2.42, 1, 1)],
'2': [at(45.76, 4.83, 0, 2), at(45.80, 4.90, 1, 2)],
}
const input = { days: DAYS, assignments: ASSIGNMENTS, reservations: [], accommodations: [], optimizeFromAccommodation: false }
beforeEach(() => {
vi.mocked(calculateRouteWithLegs).mockReset()
vi.mocked(calculateRouteWithLegs).mockResolvedValue({
coordinates: [[48.86, 2.35], [48.90, 2.42]],
distance: 10000, duration: 600,
legs: [leg(10000)],
})
})
describe('tripRouteGeometry', () => {
it('FE-MAP-TRG-001: plans one request per day and skips days with nothing to drive', () => {
const plan = planTripRoute({ ...input, assignments: { ...ASSIGNMENTS, '2': [at(45.76, 4.83, 0, 2)] } }, 'driving')
expect(plan).toHaveLength(1)
expect(plan[0].day.id).toBe(1)
expect(plan[0].runs[0]).toHaveLength(1)
})
it('FE-MAP-TRG-002: a day is drawn in the colour the road trip gives it', () => {
// One palette for both readings of the trip, so a day never changes colour.
expect(dayRouteColor(buildDay({ day_number: 1 }))).toEqual(dayColor(1))
expect(dayRouteColor(buildDay({ day_number: 2 }))).toEqual(dayColor(2))
})
it('FE-MAP-TRG-003: routes every day and sums the trip', async () => {
const summary = await routeTrip(input, { profile: 'driving', tripId: 7 })
expect(summary.days).toHaveLength(2)
expect(summary.totalDistance).toBe(20000)
expect(summary.lines).toHaveLength(2)
expect(summary.lineColors).toHaveLength(2)
})
it('FE-MAP-TRG-004: an empty trip summarises to nothing rather than throwing', async () => {
const summary = await routeTrip({ ...input, assignments: {} }, { profile: 'driving', tripId: 7 })
expect(summary).toEqual(summariseTripRoute([]))
expect(calculateRouteWithLegs).not.toHaveBeenCalled()
})
it('FE-MAP-TRG-005: the deadline gives back what answered and leaves the rest as straight lines', async () => {
neverAnswers()
vi.useFakeTimers()
try {
const pending = routeTrip(input, { profile: 'driving', tripId: 7, timeoutMs: 8000 })
await vi.advanceTimersByTimeAsync(8100)
const summary = await pending
// The shape of the trip survives...
expect(summary.lines).toHaveLength(2)
expect(summary.lines[0]).toEqual([[48.86, 2.35], [48.90, 2.42]])
// ...and nothing that never answered is counted.
expect(summary.totalDistance).toBe(0)
} finally {
vi.useRealTimers()
}
})
it('FE-MAP-TRG-006: a caller\'s own abort cuts the routing short too', async () => {
neverAnswers()
const controller = new AbortController()
const pending = routeTrip(input, { profile: 'driving', tripId: 7, timeoutMs: 60_000, signal: controller.signal })
controller.abort()
await expect(pending).resolves.toMatchObject({ totalDistance: 0 })
})
// The public OSRM hosts allow about one request a second. Every leg of every day at
// once is exactly the burst they answer with 429 from the second second on, and each
// refused leg used to stay a straight line with no distance and no word about it.
it('FE-MAP-TRG-007: asks the router one leg at a time, a second apart', async () => {
vi.useFakeTimers()
try {
// A round trip takes time; only a network answer is paced, a cache hit is not.
vi.mocked(calculateRouteWithLegs).mockImplementation(() => new Promise(resolve => {
setTimeout(() => resolve({ coordinates: [[48.86, 2.35], [48.90, 2.42]], distance: 10000, duration: 600, legs: [leg(10000)] }), 100)
}))
const pending = routeTrip(input, { profile: 'driving', tripId: 7 })
// The second day waits for the first to answer...
expect(calculateRouteWithLegs).toHaveBeenCalledTimes(1)
await vi.advanceTimersByTimeAsync(100)
// ...and then for the gap the shared hosts need between two requests.
expect(calculateRouteWithLegs).toHaveBeenCalledTimes(1)
await vi.advanceTimersByTimeAsync(1100)
expect(calculateRouteWithLegs).toHaveBeenCalledTimes(2)
await vi.advanceTimersByTimeAsync(100)
const summary = await pending
expect(summary.totalDistance).toBe(20000)
expect(summary.unroutedLegs).toBe(0)
} finally {
vi.useRealTimers()
}
})
it('FE-MAP-TRG-008: a rate limit is waited out and the leg asked for again', async () => {
vi.useFakeTimers()
try {
vi.mocked(calculateRouteWithLegs)
.mockRejectedValueOnce(new RoutingRefusedError(429, null))
.mockResolvedValue({ coordinates: [[48.86, 2.35], [48.90, 2.42]], distance: 10000, duration: 600, legs: [leg(10000)] })
const pending = routeTrip(input, { profile: 'driving', tripId: 7 })
await vi.advanceTimersByTimeAsync(1500)
expect(calculateRouteWithLegs).toHaveBeenCalledTimes(2)
await vi.advanceTimersByTimeAsync(1100)
const summary = await pending
// Both days routed: the refused leg answered on its second try.
expect(calculateRouteWithLegs).toHaveBeenCalledTimes(3)
expect(summary.totalDistance).toBe(20000)
expect(summary.unroutedLegs).toBe(0)
} finally {
vi.useRealTimers()
}
})
it('FE-MAP-TRG-009: a host that says how long to wait is given that long', async () => {
vi.useFakeTimers()
try {
vi.mocked(calculateRouteWithLegs)
.mockRejectedValueOnce(new RoutingRefusedError(429, 6000))
.mockResolvedValue({ coordinates: [[48.86, 2.35], [48.90, 2.42]], distance: 10000, duration: 600, legs: [leg(10000)] })
const pending = routeTrip(input, { profile: 'driving', tripId: 7 })
// The own first backoff would be 1500 ms; asking then is just a second refusal.
await vi.advanceTimersByTimeAsync(2000)
expect(calculateRouteWithLegs).toHaveBeenCalledTimes(1)
await vi.advanceTimersByTimeAsync(4000)
expect(calculateRouteWithLegs).toHaveBeenCalledTimes(2)
await vi.advanceTimersByTimeAsync(1100)
await expect(pending).resolves.toMatchObject({ totalDistance: 20000 })
} finally {
vi.useRealTimers()
}
})
it('FE-MAP-TRG-010: a refusal the host meant is not repeated, and the leg is counted as unrouted', async () => {
// A 400 objects to these coordinates; the same request earns the same answer.
vi.mocked(calculateRouteWithLegs)
.mockRejectedValueOnce(new RoutingRefusedError(400, null))
.mockResolvedValue({ coordinates: [[45.76, 4.83], [45.80, 4.90]], distance: 10000, duration: 600, legs: [leg(10000)] })
const summary = await routeTrip(input, { profile: 'driving', tripId: 7 })
expect(calculateRouteWithLegs).toHaveBeenCalledTimes(2)
// The first day keeps its straight line and adds nothing to the sum...
expect(summary.lines[0]).toEqual([[48.86, 2.35], [48.90, 2.42]])
expect(summary.days[0].distance).toBe(0)
expect(summary.totalDistance).toBe(10000)
// ...and says so, per day and for the trip, so the total is not mistaken for complete.
expect(summary.days.map(d => d.unroutedLegs)).toEqual([1, 0])
expect(summary.unroutedLegs).toBe(1)
})
it('FE-MAP-TRG-011: a rate limit that never lifts gives up after the retries and is counted', async () => {
vi.useFakeTimers()
try {
vi.mocked(calculateRouteWithLegs).mockRejectedValue(new RoutingRefusedError(429, null))
const pending = routeTrip(input, { profile: 'driving', tripId: 7 })
// Two backoffs per leg, a gap between the legs, then it stops asking.
await vi.advanceTimersByTimeAsync(20000)
const summary = await pending
expect(calculateRouteWithLegs).toHaveBeenCalledTimes(6)
expect(summary.totalDistance).toBe(0)
expect(summary.unroutedLegs).toBe(2)
} finally {
vi.useRealTimers()
}
})
})