From 5bf6af7afc7aa8868746844ec186ab19bd5b725b Mon Sep 17 00:00:00 2001 From: Maurice Date: Sat, 24 Oct 2026 01:53:58 +0000 Subject: [PATCH] fix(tours): new points of a saved hike take each saved point once and keep its shaping mark A new point within 25 m of a saved one took that point's key, name and place link, even when another new point already stood on it. A stop put in just before a stop a stage starts at took the stage's key, so the stage left its day and the undo could not bring it back. The saved points are now matched one to one, the nearest pairs first, so a point standing on a saved one keeps it. The points a hike is changed through carry no shaping mark, so an inserted or removed stop and a reshape turned every shaping point into a stop. A via point standing on a saved shaping point now shapes the line still, unless a stage ends there. --- server/src/nest/tours/hike-plan.helpers.ts | 64 ++++++++++----- .../tests/unit/nest/hike-plan.helpers.test.ts | 52 ++++++++++++- server/tests/unit/nest/tour-stops.mcp.test.ts | 78 ++++++++++++++++++- 3 files changed, 171 insertions(+), 23 deletions(-) diff --git a/server/src/nest/tours/hike-plan.helpers.ts b/server/src/nest/tours/hike-plan.helpers.ts index 87edf7c7f..80b4141e0 100644 --- a/server/src/nest/tours/hike-plan.helpers.ts +++ b/server/src/nest/tours/hike-plan.helpers.ts @@ -127,37 +127,61 @@ export function hikeCreateCommandOf( /** How near a new point of a hike stands to a saved one to be the same point, in metres. */ const SAME_POINT_M = 25; -/** The saved point nearest to `point`, where one stands within SAME_POINT_M of it. */ -function savedPointAt(point: { lat: number; lng: number }, saved: readonly TourWaypoint[]): TourWaypoint | undefined { - let nearest: TourWaypoint | undefined; - let nearestGap = Number.POSITIVE_INFINITY; - for (const old of saved) { - const gap = haversineMetres(point.lat, point.lng, old.lat, old.lng); - if (gap <= SAME_POINT_M && gap < nearestGap) { - nearest = old; - nearestGap = gap; - } +/** A new point and a saved one it may be, and how far apart they stand. */ +interface PointPair { + point: number; + saved: number; + gap: number; +} + +/** + * The saved point each new point stands on, one to one: of the pairs within + * SAME_POINT_M the nearest are matched first, so a point set where a saved one + * stood keeps it and a new point beside it takes nothing of it. Two pairs as + * near (a loop's start and end on one spot) go by the order of the points. + */ +function savedPointsAt(points: readonly HikeStop[], saved: readonly TourWaypoint[]): Array { + const pairs: PointPair[] = []; + points.forEach(({ lat, lng }, point) => { + saved.forEach((old, index) => { + const gap = haversineMetres(lat, lng, old.lat, old.lng); + if (gap <= SAME_POINT_M) pairs.push({ point, saved: index, gap }); + }); + }); + pairs.sort((a, b) => a.gap - b.gap || a.point - b.point || a.saved - b.saved); + const matched: Array = points.map(() => undefined); + const taken = new Set(); + for (const pair of pairs) { + if (matched[pair.point] || taken.has(pair.saved)) continue; + matched[pair.point] = saved[pair.saved]; + taken.add(pair.saved); } - return nearest; + return matched; } /** * New points as a hike keeps them, in order. A point that stands where a - * saved one stood (within SAME_POINT_M, the nearest) is that point still: it - * keeps its link to the trip's place it stands for, and its name where the - * new one gives none. A saved point no new one stands on goes, with its link. + * saved one stood (within SAME_POINT_M, each saved point standing for one new + * point at most) is that point still: it keeps its key, its link to the trip's + * place it stands for, its name where the new one gives none, and on the way + * a shaping point's mark where no stage ends there. A saved point no new one + * stands on goes, with its link. */ export function repointedWaypoints(points: readonly HikeStop[], saved: readonly TourWaypoint[]): TourWaypoint[] { - return hikeWaypointsOf(points).map((point) => { - const same = savedPointAt(point, saved); - if (!same) return point; - const name = point.name ?? same.name; + const same = savedPointsAt(points, saved); + return hikeWaypointsOf(points).map((point, index) => { + const old = same[index]; + if (!old) return point; + const name = point.name ?? old.name; + // A hike's new points carry no shaping mark: a spot that only steered the line steers it still. + const shaping = old.shaping === true && point.role === 'via' && point.stage_end !== true; // Its key too: a stage is put on its day by the key of the stop it starts at. return { ...point, - ...(same.key ? { key: same.key } : {}), + ...(old.key ? { key: old.key } : {}), ...(name ? { name } : {}), - ...(same.linked_place_id ? { linked_place_id: same.linked_place_id } : {}), + ...(old.linked_place_id ? { linked_place_id: old.linked_place_id } : {}), + ...(shaping ? { shaping: true } : {}), }; }); } diff --git a/server/tests/unit/nest/hike-plan.helpers.test.ts b/server/tests/unit/nest/hike-plan.helpers.test.ts index bf04473e2..917cd1400 100644 --- a/server/tests/unit/nest/hike-plan.helpers.test.ts +++ b/server/tests/unit/nest/hike-plan.helpers.test.ts @@ -36,7 +36,7 @@ import { import { describe, expect, it } from 'vitest'; -// HIKE-PLAN-001 to HIKE-PLAN-017 +// HIKE-PLAN-001 to HIKE-PLAN-019 /** A plan with its line, as the router answers it: without the router's legs and snaps unless given. */ const planned = ( @@ -443,3 +443,53 @@ describe('a hike over several days', () => { ]); }); }); + +describe('new points against the saved ones', () => { + it('HIKE-PLAN-018: each saved point stands for one new point at most, the one standing on it first', () => { + const saved = [ + { lat: 47, lng: 11, role: 'start' as const, sequence: 0, key: 'k-start' }, + { lat: 47.002, lng: 11, role: 'via' as const, sequence: 1, key: 'k-hut', stage_end: true, linked_place_id: 8 }, + { lat: 47.004, lng: 11, role: 'end' as const, sequence: 2, key: 'k-end' }, + ]; + // A spring 14 m before the hut, put in before it: the hut keeps its key, so its stage keeps its day. + const repointed = repointedWaypoints( + [START, { lat: 47.00187, lng: 11, name: 'Spring' }, { lat: 47.002, lng: 11, stage_end: true }, SUMMIT], + saved, + ); + expect(repointed.map((point) => [point.name, point.key, point.linked_place_id, point.stage_end])).toEqual([ + ['Car park', 'k-start', undefined, undefined], + ['Spring', undefined, undefined, undefined], + [undefined, 'k-hut', 8, true], + ['Summit', 'k-end', undefined, undefined], + ]); + // A loop's start and end on one spot each keep their own. + const loop = [ + { lat: 47, lng: 11, role: 'start' as const, sequence: 0, key: 'k-start' }, + { lat: 47.002, lng: 11, role: 'via' as const, sequence: 1, key: 'k-hut' }, + { lat: 47, lng: 11, role: 'end' as const, sequence: 2, key: 'k-back' }, + ]; + const points = [START, { lat: 47.002, lng: 11 }, START]; + expect(repointedWaypoints(points, loop).map((point) => point.key)).toEqual(['k-start', 'k-hut', 'k-back']); + }); + + it('HIKE-PLAN-019: a point on a saved shaping point shapes the line still, unless a stage ends there', () => { + const saved = [ + { lat: 47, lng: 11, role: 'start' as const, sequence: 0, key: 'k-start' }, + { lat: 47.002, lng: 11, role: 'via' as const, sequence: 1, key: 'k-shape', shaping: true }, + { lat: 47.004, lng: 11, role: 'end' as const, sequence: 2, key: 'k-end' }, + ]; + const kept = repointedWaypoints( + [START, { lat: 47.001, lng: 11, name: 'Spring' }, { lat: 47.002, lng: 11 }, SUMMIT], + saved, + ); + expect(kept.map((point) => [point.key, point.shaping])).toEqual([ + ['k-start', undefined], + [undefined, undefined], + ['k-shape', true], + ['k-end', undefined], + ]); + const staged = repointedWaypoints([START, { lat: 47.002, lng: 11, stage_end: true }, SUMMIT], saved); + expect(staged[1]).toMatchObject({ key: 'k-shape', stage_end: true }); + expect(staged[1]?.shaping).toBeUndefined(); + }); +}); diff --git a/server/tests/unit/nest/tour-stops.mcp.test.ts b/server/tests/unit/nest/tour-stops.mcp.test.ts index 1c98ea0e8..cc85adfed 100644 --- a/server/tests/unit/nest/tour-stops.mcp.test.ts +++ b/server/tests/unit/nest/tour-stops.mcp.test.ts @@ -6,6 +6,7 @@ * HikeRoutingService, as in the hike suite. The gates are the tours': trip * access, then place_edit, on the Tours addon. */ +import { DayAssignments } from '../../../src/db/entities/DayAssignments.entity'; import { TourWaypoints } from '../../../src/db/entities/TourWaypoints.entity'; import { Tours } from '../../../src/db/entities/Tours.entity'; import type { McpContext } from '../../../src/nest-mcp'; @@ -16,10 +17,14 @@ import { HikeRoutingService, type PlannedHike } from '../../../src/nest/tours/hi import { TourPoiSearchService } from '../../../src/nest/tours/tour-poi-search.service'; import { TourStopsMcp } from '../../../src/nest/tours/tour-stops.mcp'; import { ToursModule } from '../../../src/nest/tours/tours.module'; +import { ToursService } from '../../../src/nest/tours/tours.service'; import { WeatherService } from '../../../src/nest/weather/weather.service'; import { createSnapshotTestDb } from '../../helpers/db-mock'; +import { makeDayAssignment } from '../../helpers/factories/itinerary'; +import { makePlace } from '../../helpers/factories/places'; import { countRows, findRow } from '../../helpers/factories/rows'; -import { makeTrip } from '../../helpers/factories/trips'; +import { addTourWaypoints, makeTour } from '../../helpers/factories/tours'; +import { makeDay, makeTrip } from '../../helpers/factories/trips'; import { makeUser } from '../../helpers/factories/users'; import { resetTestDb } from '../../helpers/test-db'; import { createTestModule, type TestModule } from '../../helpers/test-module'; @@ -28,7 +33,7 @@ import type { GeometryPoint, HikePlanRequest } from '@trek/shared'; import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'; -// TOUR-STOPS-MCP-001 to TOUR-STOPS-MCP-005 +// TOUR-STOPS-MCP-001 to TOUR-STOPS-MCP-007 const db = createSnapshotTestDb(); const plan = vi.fn(); @@ -193,3 +198,72 @@ describe('TourStopsMcp', () => { expect(await countRows(orm(), TourWaypoints, { place: f.placeId })).toBe(3); }); }); + +describe('TourStopsMcp: the saved points a stop goes in among', () => { + /** A hike as the planner saves one: its line through its points, each point keyed. */ + async function savedHike(tripId: number, points: Parameters[2]): Promise { + const place = await makePlace(orm(), tripId, { + name: 'Hut to hut', + route_geometry: JSON.stringify(points.map(({ lat, lng }) => [lat, lng, 800])), + }); + await makeTour(orm(), place.id, { max_hiking_difficulty: 2 }); + await addTourWaypoints(orm(), place.id, points); + return place.id; + } + + async function member() { + const { user } = await makeUser(orm()); + return { own: ctx(user.id), trip: await makeTrip(orm(), user.id) }; + } + + it('TOUR-STOPS-MCP-006: a shaping point stays one when a stop goes in and when it is taken out again', async () => { + const { own, trip } = await member(); + const placeId = await savedHike(trip.id, [ + { lat: 47.5, lng: 11.1, station_key: 'stop-start01', name: 'Car park' }, + { lat: 47.505, lng: 11.1, station_key: 'stop-shape01', shaping: 1 }, + { lat: 47.51, lng: 11.1, station_key: 'stop-summit1', name: 'Summit' }, + ]); + const shaping = async () => + (await findRow(orm(), TourWaypoints, { place: placeId, station_key: 'stop-shape01' }))?.shaping; + const inserted = read( + await stops.insertTourStop({ tripId: trip.id, placeId, expected_revision: 1, stop: SPRING }, own), + ); + expect(await countRows(orm(), TourWaypoints, { place: placeId })).toBe(4); + expect(await shaping()).toBe(1); + await stops.removeTourStop({ tripId: trip.id, placeId, expected_revision: 2, key: inserted.stop.key }, own); + expect(await countRows(orm(), TourWaypoints, { place: placeId })).toBe(3); + expect(await shaping()).toBe(1); + }); + + it('TOUR-STOPS-MCP-007: a stop just before the stop a stage starts at leaves that stage its key, its link and its day', async () => { + const { own, trip } = await member(); + const hut = await makePlace(orm(), trip.id, { name: 'Herzogstandhaus', lat: 47.51, lng: 11.1 }); + const placeId = await savedHike(trip.id, [ + { lat: 47.5, lng: 11.1, station_key: 'stop-start01', name: 'Car park' }, + { lat: 47.51, lng: 11.1, station_key: 'stop-hut0001', name: 'Hut', stage_end: 1, linkedPlace: hut.id }, + { lat: 47.52, lng: 11.1, station_key: 'stop-end0001', name: 'Summit' }, + ]); + const first = await makeDay(orm(), trip.id, { date: '2027-07-01' }); + const second = await makeDay(orm(), trip.id, { date: '2027-07-02' }); + await makeDayAssignment(orm(), first.id, placeId, { tour_stage_key: 'stop-start01' }); + await makeDayAssignment(orm(), second.id, placeId, { tour_stage_key: 'stop-hut0001' }); + + // A spring about 14 m before the hut along the way: it goes in after the start. + const spring = { lat: 47.50987, lng: 11.1, name: 'Spring', poi_category: 'drinking_water' as const }; + const answer = read( + await stops.insertTourStop({ tripId: trip.id, placeId, expected_revision: 1, stop: spring }, own), + ); + expect(answer.stop.after_key).toBe('stop-start01'); + expect(answer.stop.key).not.toBe('stop-hut0001'); + const { waypoints } = await t.get(ToursService).getTour(String(trip.id), String(placeId)); + expect(waypoints.map((point) => [point.name, point.key === 'stop-hut0001', point.linked_place_id ?? null])).toEqual( + [ + ['Car park', false, null], + ['Spring', false, null], + ['Hut', true, hut.id], + ['Summit', false, null], + ], + ); + expect(await countRows(orm(), DayAssignments, { place: placeId })).toBe(2); + }); +});