mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-23 21:33:19 +00:00
feat(tours): plan a multi-day tour as one route split into stages
A hut-to-hut hike or a paddle trip over several days is now planned in the Tours planner as one tour: the whole route is planned as before, and stops along it are marked as stage ends (MDT-01). The stages are read off that one route in its order, never kept as copies, so a change inside a stage changes the whole tour and every figure on it. - A Day tour / Multi-day tour switch beside the tour type; a stop's menu ends a stage there or lets it go on. Back to a day tour removes every stage end after a confirmation, as one step undo brings back. - The list of points is headed per stage with its distance and moving time; a stop that ends a stage carries a moon at its number, on the map too. - The overview lists the stages from stop to stop with distance, climb, descent and time (hiking rule per stage, paddle estimate per stage, the router's time shared by distance otherwise), marks where each ends across the elevation profile, and a stage of more than a day on the move warns. - A stage end a change cannot keep (its stop removed, moved to an end or made a shaping point) is dropped and said so, with undo, never moved to another point. - A multi-day tour may take a day a stage, so its time limit grows with its stages; the tour keeps its one type, portages included. - The list and the MCP tools show the stage count and where stages end. Stage ends are a flag on the waypoint (migration 046000); a tour without one is a day tour, as every tour saved before.
This commit is contained in:
@@ -1205,6 +1205,50 @@ describe('Tours e2e (real guards + temp SQLite)', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('multi-day tours (MDT-01)', () => {
|
||||
/** Two days over the ridge: the middle stop ends the first stage. */
|
||||
const twoDays = (stageAt: number) =>
|
||||
tourBody({
|
||||
waypoints: [
|
||||
{ lat: 47, lng: 11, role: 'start', sequence: 0 },
|
||||
{ lat: 47.01, lng: 11.01, role: 'via', sequence: 1 },
|
||||
{ lat: 47.02, lng: 11.02, role: 'end', sequence: 2 },
|
||||
].map((point) => (point.sequence === stageAt ? { ...point, stage_end: true } : point)),
|
||||
});
|
||||
|
||||
it('saves one route with its stage ends, lists its stages, and an edit joins them again', async () => {
|
||||
const placeId = await createViaApi(5, twoDays(1));
|
||||
const detail = await request(server).get(`/api/trips/5/tours/${placeId}`).set('Cookie', sessionCookie(OWNER));
|
||||
expect(detail.status).toBe(200);
|
||||
expect(detail.body.tour).toMatchObject({ stage_count: 2, tour_type: 'hike' });
|
||||
expect(detail.body.waypoints.map((point: { stage_end?: boolean }) => point.stage_end ?? false)).toEqual([
|
||||
false,
|
||||
true,
|
||||
false,
|
||||
]);
|
||||
const list = await request(server).get('/api/trips/5/tours').set('Cookie', sessionCookie(OWNER));
|
||||
expect(list.body.tours[0]).toMatchObject({ place_id: placeId, stage_count: 2 });
|
||||
|
||||
const joined = await request(server)
|
||||
.put(`/api/trips/5/tours/${placeId}`)
|
||||
.set('Cookie', sessionCookie(OWNER))
|
||||
.send(twoDays(-1));
|
||||
expect(joined.status).toBe(200);
|
||||
expect(joined.body.tour.stage_count).toBeNull();
|
||||
});
|
||||
|
||||
it('400 for a stage end at the start or the end of the tour, and nothing is written', async () => {
|
||||
const before = await countRows(orm, Tours, {});
|
||||
const refused = await Promise.all(
|
||||
[0, 2].map((at) =>
|
||||
request(server).post('/api/trips/5/tours').set('Cookie', sessionCookie(OWNER)).send(twoDays(at)),
|
||||
),
|
||||
);
|
||||
expect(refused.map((res) => res.status)).toEqual([400, 400]);
|
||||
expect(await countRows(orm, Tours, {})).toBe(before);
|
||||
});
|
||||
});
|
||||
|
||||
describe('planning over an import (PR1-05)', () => {
|
||||
/** An imported GPX tour on trip 5: no waypoints, the recorded line as it came. */
|
||||
const importedTour = async (tripId = 5) => {
|
||||
|
||||
@@ -21,7 +21,7 @@ export async function makeTour(orm: FactoryOrm, placeId: number, overrides: Enti
|
||||
|
||||
type WaypointRole = 'start' | 'via' | 'end';
|
||||
|
||||
/** A city walk station's columns (CW-01 to CW-04), a shaping point's flag and the place a point stands for. */
|
||||
/** A city walk station's columns (CW-01 to CW-04), a shaping point's and a stage end's flags and the place a point stands for. */
|
||||
interface StationColumns {
|
||||
station_key?: string | null;
|
||||
name?: string | null;
|
||||
@@ -32,6 +32,8 @@ interface StationColumns {
|
||||
opening_hours?: string | null;
|
||||
/** 1 for a shaping point. */
|
||||
shaping?: number;
|
||||
/** MDT-01: 1 for a stop that ends a stage of a multi-day tour. */
|
||||
stage_end?: number;
|
||||
/** The id of the place of the trip the point stands for. */
|
||||
linkedPlace?: number | null;
|
||||
/** The leg to the next point as a fixed line, as its JSON column stores it. */
|
||||
|
||||
@@ -74,6 +74,7 @@ const UNNUMBERED = [
|
||||
'Migration20200101045710_a_saved_tour_change_can_be_taken_back',
|
||||
'Migration20200101045900_a_tour_keeps_the_track_it_was_done_with',
|
||||
'Migration20200101045910_a_tour_can_be_left_out_of_ones_activities',
|
||||
'Migration20200101046000_a_tour_waypoint_can_end_a_stage',
|
||||
];
|
||||
// The last step the positional runner ever had; planLegacyBaseline refuses a
|
||||
// schema_version past it.
|
||||
|
||||
@@ -221,6 +221,21 @@ describe('TourWaypointsRepository', () => {
|
||||
expect((await repo.listForPlace(place.id))[1]).not.toHaveProperty('poi_category');
|
||||
});
|
||||
|
||||
it('TWREPO-012: a stop that ends a stage is stored as such and read back; the others carry no flag (MDT-01)', async () => {
|
||||
const { trip, place } = tourInTrip();
|
||||
await repo.insertForPlace(place.id, [
|
||||
{ lat: 47, lng: 11, role: 'start', sequence: 0 },
|
||||
{ lat: 47.05, lng: 11.05, role: 'via', sequence: 1, stage_end: true },
|
||||
{ lat: 47.1, lng: 11.1, role: 'end', sequence: 2 },
|
||||
]);
|
||||
const points = await repo.listForPlace(place.id);
|
||||
expect(points[1]).toEqual({ lat: 47.05, lng: 11.05, role: 'via', sequence: 1, stage_end: true });
|
||||
expect(points[0]).not.toHaveProperty('stage_end');
|
||||
expect(points[2]).not.toHaveProperty('stage_end');
|
||||
// A trip copy reads the mark with the rest of the point.
|
||||
expect((await repo.listForTrip(trip.id))[1]).toMatchObject({ place_id: place.id, stage_end: true });
|
||||
});
|
||||
|
||||
it('TWREPO-010: listForPlaces reads the waypoints of the tours asked for, by place and sequence, and none for none (CW-02)', async () => {
|
||||
const { trip, place } = tourInTrip();
|
||||
const second = await makePlace(t, trip.id);
|
||||
|
||||
@@ -11,7 +11,7 @@ import type { ToursRepository } from '../../../../src/db/repositories/Tours.repo
|
||||
import { createSnapshotTestDb } from '../../../helpers/db-mock';
|
||||
import { createDay, createDayAssignment, createPlace, createTrip, createUser } from '../../../helpers/factories';
|
||||
import { makePlace } from '../../../helpers/factories/places';
|
||||
import { insertRow, updateRows } from '../../../helpers/factories/rows';
|
||||
import { insertRow, insertRows, updateRows } from '../../../helpers/factories/rows';
|
||||
import { makeDay } from '../../../helpers/factories/trips';
|
||||
import { resetTestDb } from '../../../helpers/test-db';
|
||||
import { createTestOrm, type TestOrm } from '../../../helpers/test-orm';
|
||||
@@ -112,6 +112,7 @@ describe('ToursRepository reads', () => {
|
||||
ski_technique: null,
|
||||
track_started_at: null,
|
||||
track_ended_at: null,
|
||||
stage_ends: 0,
|
||||
},
|
||||
{
|
||||
place_id: older.id,
|
||||
@@ -151,6 +152,7 @@ describe('ToursRepository reads', () => {
|
||||
ski_technique: null,
|
||||
track_started_at: null,
|
||||
track_ended_at: null,
|
||||
stage_ends: 0,
|
||||
},
|
||||
]);
|
||||
expect((await repo.listForTrip(trip.id)).map((r) => r.place_id)).not.toContain(plain.id);
|
||||
@@ -195,6 +197,32 @@ describe('ToursRepository reads', () => {
|
||||
expect((await repo.findInTrip(trip.id, drawn.id))?.route_missing).toBe(0);
|
||||
});
|
||||
|
||||
it('TOURREPO-016: the list and the detail count the stops that end a stage, 0 for a day tour (MDT-01)', async () => {
|
||||
const { trip } = fixture();
|
||||
const hutToHut = await makePlace(t, trip.id, { name: 'Hut to hut' });
|
||||
const dayHike = await makePlace(t, trip.id, { name: 'Day hike' });
|
||||
createTour(testDb, hutToHut.id);
|
||||
createTour(testDb, dayHike.id);
|
||||
await insertRows(
|
||||
t,
|
||||
TourWaypoints,
|
||||
[0, 1, 1, 0].map((stage_end, sequence) => ({
|
||||
place: hutToHut.id,
|
||||
lat: 47,
|
||||
lng: 11,
|
||||
role: 'via',
|
||||
sequence,
|
||||
stage_end,
|
||||
})),
|
||||
);
|
||||
await addWaypoint(dayHike.id, 0);
|
||||
|
||||
const listed = new Map((await repo.listForTrip(trip.id)).map((row) => [row.place_id, row.stage_ends]));
|
||||
expect(listed.get(hutToHut.id)).toBe(2);
|
||||
expect(listed.get(dayHike.id)).toBe(0);
|
||||
expect((await repo.findInTrip(trip.id, hutToHut.id))?.stage_ends).toBe(2);
|
||||
});
|
||||
|
||||
it('TOURREPO-015: dayOfTrip reads a day of the trip with its date, and nothing for a day of another trip', async () => {
|
||||
const { trip, other } = fixture();
|
||||
const dated = await makeDay(t, trip.id, { date: '2026-07-12' });
|
||||
|
||||
@@ -337,6 +337,32 @@ describe('ToursMcp', () => {
|
||||
]);
|
||||
});
|
||||
|
||||
it('TOURS-MCP-015: list_tours counts the stages of a multi-day tour and get_tour_plan marks where each ends (MDT-01)', async () => {
|
||||
const { user, trip, ridge, gorge } = fixture();
|
||||
const orm = await createTestOrm(db);
|
||||
await addTourWaypoints(orm, ridge.id, [
|
||||
{ lat: 47, lng: 11 },
|
||||
{ lat: 47.05, lng: 11.05, stage_end: 1 },
|
||||
{ lat: 47.1, lng: 11.1, stage_end: 1 },
|
||||
{ lat: 47.15, lng: 11.15 },
|
||||
]);
|
||||
await orm.close();
|
||||
|
||||
const { tours } = read(await mcp.listTours({ tripId: trip.id }, ctx(user.id)));
|
||||
const stages = new Map(
|
||||
tours.map((tour: { place_id: number; stage_count: number | null }) => [tour.place_id, tour.stage_count]),
|
||||
);
|
||||
expect(stages.get(ridge.id)).toBe(3);
|
||||
expect(stages.get(gorge.id)).toBeNull();
|
||||
const plan = read(await mcp.getTourPlan({ tripId: trip.id, placeId: ridge.id }, ctx(user.id)));
|
||||
expect(plan.waypoints.map((point: { stage_end?: boolean }) => point.stage_end === true)).toEqual([
|
||||
false,
|
||||
true,
|
||||
true,
|
||||
false,
|
||||
]);
|
||||
});
|
||||
|
||||
it('TOURS-MCP-014: the tools name the station fields, and the deletes ask before a walk takes its protected stations along', () => {
|
||||
const description = (ctor: unknown, method: string) => {
|
||||
const entry = getEntry(ctor as ClassRef, method);
|
||||
@@ -344,6 +370,8 @@ describe('ToursMcp', () => {
|
||||
return entry.options.description ?? '';
|
||||
};
|
||||
expect(description(ToursMcp, 'listTours')).toMatch(/station_count.*route_pending/);
|
||||
expect(description(ToursMcp, 'listTours')).toContain('stage_count');
|
||||
expect(description(ToursMcp, 'getTourPlan')).toContain('stage_end');
|
||||
const planFields = ['key', 'stay_minutes', 'importance', 'protected', 'opening_hours', 'visit_status', 'derived'];
|
||||
for (const field of planFields) {
|
||||
expect(description(ToursMcp, 'getTourPlan')).toContain(field);
|
||||
|
||||
@@ -682,3 +682,69 @@ describe('ToursService runs (RUN-01)', () => {
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('ToursService multi-day tours (MDT-01)', () => {
|
||||
/** Hut to hut over three days: two stops between start and end each end a stage. */
|
||||
const hutToHut: TourCreateRequest = {
|
||||
...request,
|
||||
planned_duration_minutes: null,
|
||||
break_additional_minutes: 60,
|
||||
duration_seconds: 30 * 3600,
|
||||
route_geometry: [
|
||||
[48, 11, 600],
|
||||
[48.01, 11.02, 650],
|
||||
[48.02, 11.04, 630],
|
||||
[48.03, 11.06, 700],
|
||||
],
|
||||
waypoints: [
|
||||
{ lat: 48, lng: 11, role: 'start', sequence: 0 },
|
||||
{ lat: 48.01, lng: 11.02, role: 'via', sequence: 1, stage_end: true },
|
||||
{ lat: 48.02, lng: 11.04, role: 'via', sequence: 2, stage_end: true },
|
||||
{ lat: 48.03, lng: 11.06, role: 'end', sequence: 3 },
|
||||
],
|
||||
};
|
||||
|
||||
it('MDT-SVC-001: keeps the stage ends with their stops and lists the stages, a day tour none', async () => {
|
||||
const created = await service.createTour(tripId, hutToHut);
|
||||
expect(created.tour.stage_count).toBe(3);
|
||||
expect(created.waypoints.map((point) => point.stage_end ?? false)).toEqual([false, true, true, false]);
|
||||
const dayTour = await service.createTour(tripId, request);
|
||||
expect(dayTour.tour.stage_count).toBeNull();
|
||||
const listed = new Map((await service.listTours(tripId)).map((tour) => [tour.place_id, tour.stage_count]));
|
||||
expect(listed.get(created.tour.place_id)).toBe(3);
|
||||
expect(listed.get(dayTour.tour.place_id)).toBeNull();
|
||||
});
|
||||
|
||||
it('MDT-SVC-002: an edit moves, joins and drops stages with the points it saves', async () => {
|
||||
const created = await service.createTour(tripId, hutToHut);
|
||||
const placeId = String(created.tour.place_id);
|
||||
const joined = hutToHut.waypoints.map(({ stage_end: _end, ...point }, index) =>
|
||||
index === 2 ? { ...point, stage_end: true } : point,
|
||||
);
|
||||
const edited = await service.updateTour(tripId, placeId, { ...hutToHut, waypoints: joined });
|
||||
expect(edited.tour.stage_count).toBe(2);
|
||||
expect(edited.waypoints.map((point) => point.stage_end ?? false)).toEqual([false, false, true, false]);
|
||||
const oneDay = await service.updateTour(tripId, placeId, { ...request, waypoints: request.waypoints });
|
||||
expect(oneDay.tour.stage_count).toBeNull();
|
||||
expect(await countRows(await sharedTestOrm(testDb), TourWaypoints, { stage_end: 1 })).toBe(0);
|
||||
});
|
||||
|
||||
it('MDT-SVC-003: holds a multi-day tour to a day a stage, and refuses a stage end out of place', async () => {
|
||||
const tooLong = { ...hutToHut, duration_seconds: 72 * 3600 };
|
||||
const refusal = await catchDomainError(() => service.createTour(tripId, tooLong));
|
||||
expect(refusal instanceof DomainError && refusal.publicMessage).toBe(
|
||||
'walking time plus breaks must be 4320 minutes or less, a day a stage, unless a total is overridden',
|
||||
);
|
||||
// A write that did not come through the contract is held to the same rule.
|
||||
const atStart = {
|
||||
...hutToHut,
|
||||
waypoints: hutToHut.waypoints.map((point, index) => (index === 0 ? { ...point, stage_end: true } : point)),
|
||||
};
|
||||
const misplaced = await catchDomainError(() => service.createTour(tripId, atStart));
|
||||
expect(misplaced instanceof DomainError && [misplaced.getStatus(), misplaced.publicMessage]).toEqual([
|
||||
400,
|
||||
'a stage ends at a stop between start and end',
|
||||
]);
|
||||
expect(await count(Tours)).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user