diff --git a/client/src/components/Map/valhallaRoute.test.ts b/client/src/components/Map/valhallaRoute.test.ts index 2182a8eec..a6acc53e1 100644 --- a/client/src/components/Map/valhallaRoute.test.ts +++ b/client/src/components/Map/valhallaRoute.test.ts @@ -519,6 +519,23 @@ describe('valhallaAlternates', () => { expect(bodies[1]).not.toHaveProperty('alternates') }) + it('VALHALLA-ALT-005: a section of a hike is offered ways on foot under its own limit and preferences (RTE-02)', async () => { + const bodies: Record[] = [] + server.use(http.post(FOSSGIS_VALHALLA, async ({ request }) => { + bodies.push(await request.json() as Record) + return HttpResponse.json(withAlternates([answer({ time: 12_000 })])) + })) + + const ways = await valhallaAlternates(HAMBURG, BERLIN, 'walking', [], 3, undefined, { max_hiking_difficulty: 4, use_hills: 0.2 }) + + expect(ways).toHaveLength(2) + expect(bodies[0]).toMatchObject({ + alternates: 3, + costing: 'pedestrian', + costing_options: { pedestrian: { max_hiking_difficulty: 4, use_hills: 0.2 } }, + }) + }) + it('VALHALLA-ALT-004: a refusal is asked once more, then is no answer; an instance with its own OSRM asks nothing', async () => { let calls = 0 server.use(http.post(FOSSGIS_VALHALLA, () => { calls++; return new HttpResponse(null, { status: 429 }) })) diff --git a/client/src/components/Map/valhallaRoute.ts b/client/src/components/Map/valhallaRoute.ts index 30cf31b91..d114047bf 100644 --- a/client/src/components/Map/valhallaRoute.ts +++ b/client/src/components/Map/valhallaRoute.ts @@ -460,6 +460,8 @@ async function requestRun( * quickest road beside a day Valhalla had routed was offered as the faster way, and * taking it changed nothing. Valhalla answers `alternates` between exactly two locations, * which is all one leg is, and weighs every one of them the way it weighed the day. + * `costingOptions` carries the rest of how that day was weighed, such as a hike's limit + * and preferences (RTE-02), so a section of a tour is offered ways under its own rules. */ export async function valhallaAlternates( from: Waypoint, @@ -468,10 +470,11 @@ export async function valhallaAlternates( avoid: readonly AvoidClass[], count: number, signal?: AbortSignal, + costingOptions: Readonly = {}, ): Promise { const base = valhallaBase() if (!base || notValhalla.has(base)) return null - const body = { ...routeBody([from, to], profile, avoid), ...(count > 0 ? { alternates: count } : {}) } + const body = { ...routeBody([from, to], profile, avoid, costingOptions), ...(count > 0 ? { alternates: count } : {}) } return askWithRetry(base, async () => { const data = await postRoute(base, body, signal) return data === null ? null : alternatesFrom(data) diff --git a/client/src/components/Tours/planner/LegVariantsCard.test.tsx b/client/src/components/Tours/planner/LegVariantsCard.test.tsx new file mode 100644 index 000000000..c056884de --- /dev/null +++ b/client/src/components/Tours/planner/LegVariantsCard.test.tsx @@ -0,0 +1,173 @@ +// FE-TOUR-LEG-VARIANTS-CARD-001 to FE-TOUR-LEG-VARIANTS-CARD-007: the card that compares the ways +// of a hike's section over the planner map (RTE-02). +import userEvent from '@testing-library/user-event'; +import { beforeEach, describe, expect, it, vi } from 'vitest'; +import { fireEvent, render, screen, within } from '../../../../tests/helpers/render'; +import { seedStore } from '../../../../tests/helpers/store'; +import { idleLegVariants } from '../../../../tests/helpers/tourPlannerStub'; +import { useSettingsStore } from '../../../store/settingsStore'; +import LegVariantsOverlay, { LegVariantsCard } from './LegVariantsCard'; +import type { LegVariant } from './legVariants'; +import type { LegComparison } from './useLegVariants'; +import type { TourPlannerController } from './useTourPlanner'; + +const POINTS = [ + { id: 'a', lat: 47, lng: 11, role: 'start' as const }, + { id: 's', lat: 47.001, lng: 11.01, role: 'via' as const, shaping: true }, + { id: 'b', lat: 47, lng: 11.02, role: 'end' as const }, +]; + +const way = (key: LegVariant['key'], overrides: Partial = {}): LegVariant => ({ + key, + current: key === 'current', + direct: false, + coordinates: [ + [47, 11], + [47, 11.02], + ], + distanceM: 1500, + gainM: 120, + lossM: 40, + minutes: 35, + heights: 'known', + ...overrides, +}); + +function comparison(overrides: Partial = {}): LegComparison { + return { + leg: { + fromId: 'a', + toId: 's', + from: { lat: 47, lng: 11 }, + to: { lat: 47.001, lng: 11.01 }, + costing: { max_hiking_difficulty: 2 }, + key: 'a>s', + }, + phase: 'choose', + variants: [way('current'), way('a', { distanceM: 1900, gainM: 180, minutes: 47 })], + selected: null, + proving: false, + notice: null, + ...overrides, + }; +} + +type CardPlanner = Pick; + +function planner(open: LegComparison | null, overrides: Partial = {}): CardPlanner { + return { + waypoints: POINTS, + legVariants: { ...idleLegVariants(), comparison: open }, + canEdit: true, + isSaving: false, + ...overrides, + } as unknown as CardPlanner; +} + +function renderCard(open: LegComparison, overrides: Partial = {}) { + const controller = planner(open, overrides); + render(); + return controller.legVariants; +} + +beforeEach(() => { + seedStore(useSettingsStore, { settings: { distance_unit: 'metric' } }); +}); + +describe('LegVariantsCard', () => { + it('FE-TOUR-LEG-VARIANTS-CARD-001: names the section by its ends and lists each way with its figures', () => { + renderCard(comparison()); + const card = screen.getByTestId('leg-variants-card'); + expect(within(card).getByRole('heading', { name: 'Other ways for this section' })).toBeInTheDocument(); + expect(within(card).getByLabelText('Section from Waypoint 1 to Shaping point')).toBeInTheDocument(); + const [current, other] = within(card).getAllByRole('radio'); + expect(current).toHaveAccessibleName('Current way'); + expect(other).toHaveAccessibleName('Way A'); + expect(within(current).getByText('1.5 km')).toBeInTheDocument(); + expect(within(current).getByText('120 m')).toBeInTheDocument(); + expect(within(current).getByText('40 m')).toBeInTheDocument(); + // How the other way differs: longer, more climb, more time; the way taken now has no difference to show. + const delta = within(other).getByTestId('leg-variant-delta'); + expect(delta).toHaveTextContent('+400 m'); + expect(delta).toHaveTextContent('+60 m'); + expect(delta).toHaveTextContent('+12 min'); + expect(within(current).queryByTestId('leg-variant-delta')).not.toBeInTheDocument(); + }); + + it('FE-TOUR-LEG-VARIANTS-CARD-002: heights still asked for turn, heights not to be had stay unknown, never a guess', () => { + renderCard( + comparison({ + variants: [ + way('current', { gainM: null, lossM: null, minutes: null, heights: 'unknown' }), + way('a', { gainM: null, lossM: null, minutes: null, heights: 'pending', distanceM: 1400 }), + ], + }) + ); + const [current, other] = screen.getAllByRole('radio'); + expect(within(current).getAllByText('Unknown')).toHaveLength(3); + expect(within(other).getAllByLabelText('Heights are being looked up')).toHaveLength(3); + // Only the length is known on both, so only it is compared. + expect(within(other).getByTestId('leg-variant-delta')).toHaveTextContent('−100 m'); + }); + + it('FE-TOUR-LEG-VARIANTS-CARD-003: choosing marks a way; taking it is a separate, explicit step', async () => { + const actions = renderCard(comparison()); + const apply = screen.getByTestId('leg-variants-apply'); + expect(apply).toBeDisabled(); + await userEvent.click(screen.getByRole('radio', { name: 'Way A' })); + expect(actions.select).toHaveBeenCalledWith('a'); + expect(actions.apply).not.toHaveBeenCalled(); + fireEvent.mouseEnter(screen.getByRole('radio', { name: 'Way A' })); + expect(actions.hover).toHaveBeenLastCalledWith('a'); + fireEvent.mouseLeave(screen.getByRole('radio', { name: 'Way A' })); + expect(actions.hover).toHaveBeenLastCalledWith(null); + }); + + it('FE-TOUR-LEG-VARIANTS-CARD-004: a chosen other way can be taken; the way taken now and a locked tour cannot', async () => { + const actions = renderCard(comparison({ selected: 'a' })); + expect(screen.getByRole('radio', { name: 'Way A' })).toHaveAttribute('aria-checked', 'true'); + await userEvent.click(screen.getByRole('button', { name: 'Take this way' })); + expect(actions.apply).toHaveBeenCalledTimes(1); + + renderCard(comparison({ selected: 'current' })); + expect(screen.getAllByTestId('leg-variants-apply').at(-1)).toBeDisabled(); + renderCard(comparison({ selected: 'a' }), { isSaving: true }); + expect(screen.getAllByTestId('leg-variants-apply').at(-1)).toBeDisabled(); + }); + + it('FE-TOUR-LEG-VARIANTS-CARD-005: while a choice is checked the list stands still and the button says what happens', async () => { + const actions = renderCard(comparison({ selected: 'a', proving: true })); + expect(screen.getByTestId('leg-variants-apply')).toHaveTextContent('Checking the way'); + expect(screen.getByTestId('leg-variants-apply')).toBeDisabled(); + await userEvent.click(screen.getByRole('radio', { name: 'Current way' })); + expect(actions.select).not.toHaveBeenCalled(); + }); + + it('FE-TOUR-LEG-VARIANTS-CARD-006: says what it is doing and why nothing changed', () => { + renderCard(comparison({ phase: 'loading', variants: [way('current')] })); + expect(screen.getByRole('status')).toHaveTextContent('Looking for other ways'); + renderCard(comparison({ phase: 'none', variants: [way('current')] })); + expect(screen.getAllByRole('status').at(-1)).toHaveTextContent('The router knows no other way'); + renderCard(comparison({ phase: 'failed', variants: [way('current')] })); + expect(screen.getAllByRole('status').at(-1)).toHaveTextContent('The router did not answer'); + renderCard(comparison({ notice: 'notHeld' })); + expect(screen.getAllByRole('status').at(-1)).toHaveTextContent('The router does not keep to this way'); + renderCard(comparison({ notice: 'stale' })); + expect(screen.getAllByRole('status').at(-1)).toHaveTextContent('The tour changed in the meantime'); + renderCard(comparison({ notice: 'failed' })); + expect(screen.getAllByRole('status').at(-1)).toHaveTextContent('This way could not be checked'); + }); + + it('FE-TOUR-LEG-VARIANTS-CARD-007: the overlay shows only while a section is compared; cancel, the cross and Escape close it', async () => { + const { rerender } = render(); + expect(screen.queryByTestId('leg-variants-overlay')).not.toBeInTheDocument(); + + const open = planner(comparison()); + rerender(); + expect(screen.getByTestId('leg-variants-overlay').style.bottom).toBe('66px'); + await userEvent.click(screen.getByRole('button', { name: 'Cancel' })); + await userEvent.click(screen.getByRole('button', { name: 'Close' })); + fireEvent.keyDown(document, { key: 'Escape' }); + expect(open.legVariants.close).toHaveBeenCalledTimes(3); + }); +}); diff --git a/client/src/components/Tours/planner/LegVariantsCard.tsx b/client/src/components/Tours/planner/LegVariantsCard.tsx new file mode 100644 index 000000000..c688ea449 --- /dev/null +++ b/client/src/components/Tours/planner/LegVariantsCard.tsx @@ -0,0 +1,402 @@ +import { + AlertTriangle, + ArrowRight, + Check, + Clock, + Loader2, + MoveHorizontal, + Route, + Shuffle, + TrendingDown, + TrendingUp, + X, +} from 'lucide-react'; +import { useEffect, type ReactNode } from 'react'; +import { useTranslation } from '../../../i18n'; +import { useSettingsStore } from '../../../store/settingsStore'; +import type { DistanceUnit } from '../../../types'; +import { formatDistance, formatElevation } from '../../../utils/units'; +import { MAP_CARD_GLASS } from '../../Map/mapControlShadow'; +import { SoftPill, WhiteBadge } from '../../Planner/planParts'; +import { fs } from '../../shared/DialogShell'; +import { Tooltip } from '../../shared/Tooltip'; +import { TourNotice } from '../tourParts'; +import { variantPaintCss } from './legVariantPaint'; +import { variantDelta, type LegVariant } from './legVariants'; +import { formatTourDuration } from './tourDurationInput'; +import { stopNumbersOf } from './tourPlannerWaypoints'; +import type { LegComparison } from './useLegVariants'; +import type { TourPlannerController } from './useTourPlanner'; + +type Translate = ReturnType['t']; + +/** A signed difference: a plus for more, a minus sign for fewer, nothing for none worth printing. */ +function signed(value: number, format: (absolute: number) => string): string | null { + const text = format(Math.abs(value)); + if (Math.abs(value) < 1e-9 || /^0\s/.test(text)) return null; + return `${value > 0 ? '+' : '−'}${text}`; +} + +/** An end of the section by name: the stop by its number, else the shaping point it is. */ +function endName(stop: number | undefined, t: Translate): string { + return stop === undefined ? t('tours.planner.shapingPoint') : t('tours.planner.waypointLabel', { n: stop }); +} + +/** + * One end of the section, as the waypoint list numbers it: a stop by its + * number, a shaping point as the small dot it is on the map. + */ +function LegEnd({ stop }: { stop: number | undefined }) { + if (stop === undefined) { + return ( +