Files
TREK/client/src/components/Tours/planner/useLegVariants.test.tsx
T
Maurice 64445a5204 feat(tours): compare other ways for one section of a hike (RTE-02)
A routed section of a hike between two neighbouring points can now be
compared with the other ways the router knows between the same two points,
under the same limit and preferences. "Compare ways for the next leg" in a
waypoint's menu opens a card over the planner map: the way taken now and up
to three others, each drawn on the map in its own colour, with length,
climb, descent and walking time by the signposts' rule. Figures without
heights stay unknown, and the differences to the current way are shown
only where both are known. When the router knows no other way, or does not
answer, the card says so.

Showing, hovering and choosing change nothing. Only "Take this way" does:
the router is first asked whether a few shaping points hold the chosen way
(the check the road trip's picker already uses), and only then are they set
into that one leg as a single step of the history, with a "Way taken" note
and its undo at the foot of the map. Other legs, fixed lines, days and
points stay as they were, the totals follow from the replanned route, and
the shaping points can be dragged like any other. A section that changed
while the router was asked, or a way it does not keep to, is not written,
and the card says why. The ways are drawn in 3D too.
2026-10-20 15:18:38 +02:00

264 lines
12 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// FE-TOUR-USE-LEG-VARIANTS-001 to FE-TOUR-USE-LEG-VARIANTS-010: comparing the ways of a hike's
// section, choosing one on purpose and holding it by shaping points as one step (RTE-02).
import { act, renderHook, waitFor } from '@testing-library/react';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import type { OtherWaysAnswer } from './legVariantRouting';
import type { LegVariant } from './legVariants';
import type { TourPlannerWaypoint } from './tourPlannerWaypoints';
const { askOtherWays, routeSection, pinAlternative } = vi.hoisted(() => ({
askOtherWays: vi.fn(),
routeSection: vi.fn(),
pinAlternative: vi.fn(),
}));
vi.mock('./legVariantRouting', () => ({ askOtherWays, routeSection }));
vi.mock('../../Roadtrip/alternativePins', () => ({ pinAlternative }));
import { useLegVariants, type LegVariantsOptions } from './useLegVariants';
const A: TourPlannerWaypoint = { id: 'a', lat: 47.0, lng: 11.0, role: 'start' };
const B: TourPlannerWaypoint = { id: 'b', lat: 47.0, lng: 11.02, role: 'via' };
const C: TourPlannerWaypoint = { id: 'c', lat: 47.01, lng: 11.02, role: 'end' };
const ROUTE: [number, number][] = [
[47.0, 11.0],
[47.001, 11.01],
[47.0, 11.02],
[47.005, 11.021],
[47.01, 11.02],
];
const OTHER: LegVariant = {
key: 'a',
current: false,
direct: false,
coordinates: [
[47.0, 11.0],
[47.03, 11.01],
[47.0, 11.02],
],
distanceM: 6800,
gainM: null,
lossM: null,
minutes: null,
heights: 'pending',
};
/** What a test answers for the comparison it opens: the router's reports, sent in order. */
let answers: OtherWaysAnswer[] = [];
function options(overrides: Partial<LegVariantsOptions> = {}): LegVariantsOptions {
return {
waypoints: [A, B, C],
route: ROUTE,
enrichedGeometry: null,
routeCurrent: true,
tourType: 'hike',
maxHikingDifficulty: 2,
hikingPreferences: null,
insertShapingPoints: vi.fn(() => ['s1']),
recomputeLeg: vi.fn(),
undo: vi.fn(),
mayEdit: () => true,
...overrides,
};
}
function setup(initial: LegVariantsOptions = options()) {
return renderHook((props: LegVariantsOptions) => useLegVariants(props), { initialProps: initial });
}
/** Opens the comparison of the leg after `a` and lets the router's answers arrive. */
async function compared(view: ReturnType<typeof setup>) {
act(() => view.result.current.compare('a'));
await waitFor(() => expect(view.result.current.comparison?.phase).not.toBe('loading'));
}
beforeEach(() => {
vi.clearAllMocks();
answers = [];
askOtherWays.mockImplementation(
async (_leg: unknown, current: LegVariant, _signal: AbortSignal, report: (answer: OtherWaysAnswer) => void) => {
// The router answers later, never inside the render that asked.
await Promise.resolve();
for (const answer of answers) {
report({ ...answer, variants: answer.variants.map((variant) => (variant.current ? current : variant)) });
}
}
);
});
describe('useLegVariants', () => {
it('FE-TOUR-USE-LEG-VARIANTS-001: only a routed leg of a hike whose route is planned for the points can be compared', () => {
const view = setup();
expect(view.result.current.canCompare('a')).toBe(true);
expect(view.result.current.canCompare('c')).toBe(false);
view.rerender(options({ routeCurrent: false }));
expect(view.result.current.canCompare('a')).toBe(false);
view.rerender(options({ tourType: 'bike' }));
expect(view.result.current.canCompare('a')).toBe(false);
view.rerender(options({ route: null }));
expect(view.result.current.canCompare('a')).toBe(false);
});
it('FE-TOUR-USE-LEG-VARIANTS-002: opens on the section with the way taken now, asked under the hike’s rules', async () => {
answers = [{ phase: 'choose', variants: [{ ...OTHER, key: 'current', current: true }, OTHER] }];
const view = setup(options({ maxHikingDifficulty: 4, hikingPreferences: { avoid_steps: true } }));
act(() => view.result.current.compare('a'));
expect(view.result.current.comparison).toMatchObject({ phase: 'loading', selected: null, proving: false });
expect(view.result.current.comparison?.variants.map((v) => v.key)).toEqual(['current']);
await waitFor(() => expect(view.result.current.comparison?.phase).toBe('choose'));
const [leg] = askOtherWays.mock.calls[0];
expect(leg).toMatchObject({ fromId: 'a', toId: 'b', costing: { max_hiking_difficulty: 4, step_penalty: 600 } });
expect(view.result.current.comparison?.variants.map((v) => v.key)).toEqual(['current', 'a']);
// The way taken now is the route's own stretch between the two points.
expect(view.result.current.comparison?.variants[0].coordinates).toEqual(ROUTE.slice(0, 3));
});
it('FE-TOUR-USE-LEG-VARIANTS-003: hovering and choosing light a way up; closing changes nothing', async () => {
answers = [{ phase: 'choose', variants: [{ ...OTHER, key: 'current', current: true }, OTHER] }];
const insertShapingPoints = vi.fn(() => ['s1']);
const view = setup(options({ insertShapingPoints }));
await compared(view);
act(() => view.result.current.select('a'));
expect(view.result.current.comparison?.selected).toBe('a');
expect(view.result.current.focus).toBe('a');
act(() => view.result.current.hover('current'));
expect(view.result.current.focus).toBe('current');
act(() => view.result.current.hover(null));
act(() => view.result.current.close());
expect(view.result.current.comparison).toBeNull();
expect(view.result.current.focus).toBeNull();
expect(insertShapingPoints).not.toHaveBeenCalled();
expect(pinAlternative).not.toHaveBeenCalled();
});
it('FE-TOUR-USE-LEG-VARIANTS-004: a chosen way the router keeps to is held by its pins as one step, and the card closes', async () => {
answers = [{ phase: 'choose', variants: [{ ...OTHER, key: 'current', current: true }, OTHER] }];
pinAlternative.mockResolvedValue({ held: true, pins: [{ lat: 47.03, lng: 11.01 }], fellBack: false, last: {} });
routeSection.mockResolvedValue({ coordinates: OTHER.coordinates, distance: 6800, duration: 0 });
const insertShapingPoints = vi.fn(() => ['s1']);
const view = setup(options({ insertShapingPoints }));
await compared(view);
act(() => view.result.current.select('a'));
await act(async () => view.result.current.apply());
const [{ offer, current, route }] = pinAlternative.mock.calls[0];
expect(offer).toMatchObject({ coordinates: OTHER.coordinates, direct: false });
expect(current.coordinates).toEqual(ROUTE.slice(0, 3));
await route([{ lat: 1, lng: 2 }], new AbortController().signal);
expect(routeSection).toHaveBeenCalledWith(
expect.objectContaining({ fromId: 'a', toId: 'b' }),
[{ lat: 1, lng: 2 }],
expect.any(AbortSignal)
);
expect(insertShapingPoints).toHaveBeenCalledWith('a', 'b', [{ lat: 47.03, lng: 11.01 }]);
expect(view.result.current.comparison).toBeNull();
});
it('FE-TOUR-USE-LEG-VARIANTS-005: a way the router does not keep to changes nothing and says so', async () => {
answers = [{ phase: 'choose', variants: [{ ...OTHER, key: 'current', current: true }, OTHER] }];
pinAlternative.mockResolvedValue({ held: false, pins: [], fellBack: false, last: {} });
const insertShapingPoints = vi.fn(() => ['s1']);
const view = setup(options({ insertShapingPoints }));
await compared(view);
act(() => view.result.current.select('a'));
await act(async () => view.result.current.apply());
expect(view.result.current.comparison).toMatchObject({ notice: 'notHeld', proving: false, selected: 'a' });
expect(insertShapingPoints).not.toHaveBeenCalled();
});
it('FE-TOUR-USE-LEG-VARIANTS-006: a router that fails the check changes nothing; the way taken now is no choice to apply', async () => {
answers = [{ phase: 'choose', variants: [{ ...OTHER, key: 'current', current: true }, OTHER] }];
pinAlternative.mockRejectedValue(new Error('No route found'));
const view = setup();
await compared(view);
act(() => view.result.current.select('current'));
await act(async () => view.result.current.apply());
expect(pinAlternative).not.toHaveBeenCalled();
act(() => view.result.current.select('a'));
await act(async () => view.result.current.apply());
expect(view.result.current.comparison?.notice).toBe('failed');
});
it("FE-TOUR-USE-LEG-VARIANTS-007: the router's own way needs no pin: the leg is only planned again", async () => {
answers = [
{ phase: 'choose', variants: [{ ...OTHER, key: 'current', current: true }, { ...OTHER, direct: true }] },
];
pinAlternative.mockResolvedValue({ held: true, pins: [], fellBack: false, last: {} });
const recomputeLeg = vi.fn();
const insertShapingPoints = vi.fn(() => ['s1']);
const view = setup(options({ recomputeLeg, insertShapingPoints }));
await compared(view);
act(() => view.result.current.select('a'));
await act(async () => view.result.current.apply());
expect(recomputeLeg).toHaveBeenCalledWith('a');
expect(insertShapingPoints).not.toHaveBeenCalled();
expect(view.result.current.comparison).toBeNull();
});
it('FE-TOUR-USE-LEG-VARIANTS-008: a choice checked while the section changed is not written; a refused write says stale too', async () => {
answers = [{ phase: 'choose', variants: [{ ...OTHER, key: 'current', current: true }, OTHER] }];
let release: (value: unknown) => void = () => undefined;
pinAlternative.mockImplementation(() => new Promise((resolve) => (release = resolve)));
const insertShapingPoints = vi.fn(() => null);
const view = setup(options({ insertShapingPoints }));
await compared(view);
act(() => view.result.current.select('a'));
let applying: Promise<void> = Promise.resolve();
act(() => {
applying = view.result.current.apply();
});
expect(view.result.current.comparison?.proving).toBe(true);
// The limit changes while the router is asked: the answer is for another section now.
view.rerender(options({ insertShapingPoints, maxHikingDifficulty: 5 }));
await act(async () => {
release({ held: true, pins: [{ lat: 47.03, lng: 11.01 }], fellBack: false, last: {} });
await applying;
});
expect(view.result.current.comparison).toBeNull();
expect(insertShapingPoints).not.toHaveBeenCalled();
pinAlternative.mockResolvedValue({ held: true, pins: [{ lat: 47.03, lng: 11.01 }], fellBack: false, last: {} });
await compared(view);
act(() => view.result.current.select('a'));
await act(async () => view.result.current.apply());
expect(insertShapingPoints).toHaveBeenCalledTimes(1);
expect(view.result.current.comparison?.notice).toBe('stale');
});
it('FE-TOUR-USE-LEG-VARIANTS-009: a moved point closes the comparison; an answer arriving after it is dropped', async () => {
let late: ((answer: OtherWaysAnswer) => void) | null = null;
askOtherWays.mockImplementationOnce(
async (_leg: unknown, _current: unknown, _signal: unknown, report: (answer: OtherWaysAnswer) => void) => {
late = report;
}
);
const view = setup();
act(() => view.result.current.compare('a'));
view.rerender(options({ waypoints: [A, { ...B, lat: 47.002 }, C] }));
expect(view.result.current.comparison).toBeNull();
act(() => late?.({ phase: 'choose', variants: [OTHER] }));
expect(view.result.current.comparison).toBeNull();
// Nothing to compare without a planned stretch for the leg.
view.rerender(options({ route: null }));
act(() => view.result.current.compare('a'));
expect(view.result.current.comparison).toBeNull();
});
it('FE-TOUR-USE-LEG-VARIANTS-010: nothing is checked while the tour may not change; the way taken now follows its heights', async () => {
answers = [{ phase: 'choose', variants: [{ ...OTHER, key: 'current', current: true }, OTHER] }];
const view = setup(options({ mayEdit: () => false }));
await compared(view);
act(() => view.result.current.select('a'));
await act(async () => view.result.current.apply());
expect(pinAlternative).not.toHaveBeenCalled();
expect(view.result.current.comparison?.variants[0].heights).toBe('unknown');
const heights = ROUTE.map(([lat, lng], i): [number, number, number] => [lat, lng, 1000 + i * 10]);
view.rerender(options({ mayEdit: () => false, enrichedGeometry: heights }));
expect(view.result.current.comparison?.variants[0]).toMatchObject({ heights: 'known', gainM: 20 });
});
});