feat(routing): let the admin pick FOSSGIS, the TREK services or an own Valhalla

routing_valhalla_provider (fossgis, trek or custom) sits beside the routing
URLs under Admin, Default user settings, admin only and locked on a managed
install. Without a choice an own Valhalla is custom, an own OSRM alone asks
no Valhalla, and otherwise the TREK services answer.

The planner asks them through the same-origin relay, so no connect-src
changes. A relay that answers 401 (a shared page without a sign-in), 403,
413, 429 or 5xx sends the question to FOSSGIS without a word and is left
alone for ten minutes. A long line to describe goes as a polyline.
This commit is contained in:
Maurice
2026-10-29 10:44:41 +01:00
committed by Maurice
parent 80183bc986
commit 091ebed8e4
45 changed files with 618 additions and 107 deletions
@@ -5,6 +5,8 @@ import { useSettingsStore } from '../../store/settingsStore'
import { valhallaBase, valhallaAvailable, valhallaTurn, resetValhallaTurns, valhallaRouteAvoiding, valhallaRun, valhallaRunOnFoot, valhallaAlternates, alternatesFrom, runFrom, legAvoids, valhallaTraceAttributes } from './valhallaRoute'
const FOSSGIS_VALHALLA = 'https://valhalla1.openstreetmap.de/route'
/** The relay to the TREK services' Valhalla, the base asked when nothing else is configured. */
const TREK_VALHALLA = '/api/routing/valhalla'
/**
* A real polyline6, encoded from three known points and verified by decoding it back.
@@ -36,7 +38,7 @@ const setSettings = (patch: Record<string, string>) =>
useSettingsStore.setState(st => ({ settings: { ...st.settings, ...patch } }))
afterEach(() => {
setSettings({ routing_base_url: '', valhalla_base_url: '' })
setSettings({ routing_base_url: '', valhalla_base_url: '', routing_valhalla_provider: '' })
resetValhallaTurns()
})
@@ -92,11 +94,20 @@ const HAMBURG = { lat: 53.5511, lng: 9.9937 }
const BERLIN = { lat: 52.52, lng: 13.405 }
describe('valhallaBase', () => {
it('VALHALLA-BASE-001: falls back to the public instance when nothing is configured', () => {
expect(valhallaBase()).toBe('https://valhalla1.openstreetmap.de')
it('VALHALLA-BASE-001: asks the TREK services through the relay when nothing is configured', () => {
expect(valhallaBase()).toBe(TREK_VALHALLA)
expect(valhallaAvailable()).toBe(true)
})
it('VALHALLA-BASE-005: keeps the admin choice of a routing service', () => {
setSettings({ routing_valhalla_provider: 'fossgis', valhalla_base_url: 'https://v.example.org' })
expect(valhallaBase()).toBe('https://valhalla1.openstreetmap.de')
setSettings({ routing_valhalla_provider: 'custom' })
expect(valhallaBase()).toBe('https://v.example.org')
setSettings({ routing_valhalla_provider: 'trek', valhalla_base_url: '', routing_base_url: 'https://osrm.example.org' })
expect(valhallaBase()).toBe(TREK_VALHALLA)
})
it('VALHALLA-BASE-002: uses the configured instance and drops its trailing slashes', () => {
setSettings({ valhalla_base_url: 'https://valhalla.example.org//' })
expect(valhallaBase()).toBe('https://valhalla.example.org')
@@ -705,3 +716,81 @@ describe('crossings by boat on a walk (CW-12)', () => {
expect(await valhallaRunOnFoot(points, undefined, { use_ferry: 0 }, { exclude_ferry: true })).toBeNull()
})
})
describe('through the TREK services (routing relay)', () => {
const RELAY = '/api/routing/valhalla/:action'
it('VALHALLA-RELAY-001: asks the relay first, and FOSSGIS is not asked while it answers', async () => {
const relayed: Array<{ action: string; body: Record<string, unknown> }> = []
let fossgis = 0
server.use(
http.post(RELAY, async ({ params, request }) => {
relayed.push({ action: String(params.action), body: await request.json() as Record<string, unknown> })
return HttpResponse.json(answer({ has_toll: false }))
}),
http.post(FOSSGIS_VALHALLA, () => { fossgis++; return HttpResponse.json(answer()) }),
)
const leg = await valhallaRouteAvoiding(HAMBURG, BERLIN, 'driving', 'toll')
expect(leg).not.toBeNull()
expect(leg!.distance).toBeCloseTo(295700, 0)
expect(relayed).toHaveLength(1)
expect(relayed[0].action).toBe('route')
expect(relayed[0].body).toMatchObject({ costing: 'auto', costing_options: { auto: { use_tolls: 0 } } })
expect(fossgis).toBe(0)
})
it('VALHALLA-RELAY-002: turns to FOSSGIS without a word where the relay cannot answer, and leaves it alone a while', async () => {
for (const status of [401, 429, 503]) {
resetValhallaTurns()
let relayCalls = 0
let fossgis = 0
server.use(
http.post(RELAY, () => { relayCalls++; return HttpResponse.json({ error: 'no' }, { status }) }),
http.post(FOSSGIS_VALHALLA, () => { fossgis++; return HttpResponse.json(answer()) }),
)
await expect(valhallaRouteAvoiding(HAMBURG, BERLIN, 'driving', 'toll')).resolves.not.toBeNull()
expect(valhallaBase()).toBe('https://valhalla1.openstreetmap.de')
vi.useFakeTimers({ toFake: ['Date'] })
vi.setSystemTime(Date.now() + 2000)
await valhallaRouteAvoiding(HAMBURG, BERLIN, 'driving', 'ferry')
vi.useRealTimers()
expect([status, relayCalls, fossgis]).toEqual([status, 1, 2])
}
})
it('VALHALLA-RELAY-003: asks the relay again once the rest is over', async () => {
let relayCalls = 0
server.use(
http.post(RELAY, () => { relayCalls++; return HttpResponse.json({ error: 'no' }, { status: 503 }) }),
http.post(FOSSGIS_VALHALLA, () => HttpResponse.json(answer())),
)
await valhallaRouteAvoiding(HAMBURG, BERLIN, 'driving', 'toll')
vi.useFakeTimers({ toFake: ['Date'] })
vi.setSystemTime(Date.now() + 11 * 60_000)
expect(valhallaBase()).toBe(TREK_VALHALLA)
vi.useRealTimers()
})
it("VALHALLA-RELAY-004: reads Valhalla's own refusal through the relay as no way, without asking FOSSGIS", async () => {
let fossgis = 0
server.use(
http.post(RELAY, () => HttpResponse.json({ error_code: 442, error: 'No path could be found for input' }, { status: 400 })),
http.post(FOSSGIS_VALHALLA, () => { fossgis++; return HttpResponse.json(answer()) }),
)
const points = [HAMBURG, BERLIN]
expect(await valhallaRunOnFoot(points, undefined, { use_ferry: 0 }, { exclude_ferry: true })).toEqual({ noPath: true })
expect(fossgis).toBe(0)
expect(valhallaBase()).toBe(TREK_VALHALLA)
})
it('VALHALLA-RELAY-005: describes a line through the relay as a polyline', async () => {
const bodies: Record<string, unknown>[] = []
server.use(http.post(RELAY, async ({ request }) => {
bodies.push(await request.json() as Record<string, unknown>)
return HttpResponse.json({ edges: [] })
}))
await expect(valhallaTraceAttributes(SHAPE_POINTS)).resolves.toEqual({ edges: [] })
expect(bodies[0]).not.toHaveProperty('shape')
expect(bodies[0]).toMatchObject({ encoded_polyline: expect.any(String), costing: 'bicycle', shape_match: 'map_snap' })
})
})