mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-04 09:54:14 +00:00
554d6f1b9f
An assistant connected over MCP can now prepare a city walk from what the traveller tells it. propose_city_walk takes a start (and an end), a time frame on a day of the trip, the themes of CW-05, the stations the traveller wishes for, the places of the trip it may use and fixed appointments with their times, and saves nothing. Places for the themes come from the search along a tour, at most eight, each with the theme, category and source that brought it; nothing is made up. The stations are ordered on estimates over the straight distance, and a place of a theme or of the trip goes in only where it is open on arrival, keeps the frame and the appointments, and leaves every station already on the walk open when the walk gets there. The walk in that order is then planned on foot by the router the road trip uses (the traveller's own routing server where they set one), and its legs give the walking distance and time from one station to the next. Where the router cannot be reached, or a station lies more than 300 m off every way (the planner's limit, now shared with the server), the times are estimates over the straight distance with an allowance for streets at 4.5 km/h, and the answer says so and why. The answer gives the arrival and leave times, the stay where the traveller gave it and unknown otherwise, which makes every later arrival the earliest one, and the opening hours checked against the arrival on that day with the helpers the planner uses. Walking and stays are counted apart, the total is complete only when every stay is known, and the minutes the frame leaves for the unknown stays are named. Free time before an appointment or at the end, wishes and appointments that cannot be kept as asked, and places left out with their reason are listed; a tour of the trip offered as a station is named as a tour. A wish or an appointment is never dropped; an appointment is a must and protected. A place's default duration is not read as a stay: every place carries one, so it says nothing. Once the traveller confirms, create_city_walk saves the walk through the same creation the planner's save uses: one write, a key for every station, the links to the trip's places checked, the trip told, with place editing permission on the trip. Its way on foot is planned by the same router and saved with the walk, together with its distance and walking time. Where the router gives no way, the walk is saved with its stations only and the answer says why: no line, no distance and no walking time. The tours list says so (route_pending), the tour detail says in one line that the way is planned in the planner, and opening the walk in the planner plans its way at once and offers to save it, with a note once it is planned that it was just planned. Such a walk can be saved as a new tour and taken over into another trip, starting at its first stop, and is never offered as a place to stay. A tour without a line needs two points of its own. get_tour_plan now spells out every field of a city walk's stations: key, name, stay (null when not known), importance (null for normal), protection (false when not set) and opening hours (null when none are known); list_tours names the station count and whether a walk waits for its way, get_city_walk_progress gives each station's key and opening hours, and take_over_tour says a walk keeps its stations with their protection. No tool changes a walk's stations; deleting a walk's place takes its stations along, protected ones included, and both delete tools now ask the assistant to name those and ask the traveller first. REST answers the stations as before. A tour create refused for a type that is switched off, a description that is too long or planned times out of range is a domain refusal now, so an assistant gets the same text REST sends; REST still answers it as an error at 400.
334 lines
13 KiB
TypeScript
334 lines
13 KiB
TypeScript
/**
|
|
* TourVisitsService (CW-03, CW-09): the stations of a city walk checked off on
|
|
* one of its days, and how far the walk got on a day as an assistant reads it.
|
|
* The service comes out of ToursModule over a snapshot test db; rows are
|
|
* seeded through the ORM factories and broadcasts go to a FakeRealtimeService.
|
|
*/
|
|
import { TourStationVisits } from '../../../src/db/entities/TourStationVisits.entity';
|
|
import { TourVisitsService, type WalkOnDay, type WalkProgress } from '../../../src/nest/tours/tour-visits.service';
|
|
import { ToursModule } from '../../../src/nest/tours/tours.module';
|
|
import { createSnapshotTestDb } from '../../helpers/db-mock';
|
|
import { makeDayAssignment } from '../../helpers/factories/itinerary';
|
|
import { makePlace } from '../../helpers/factories/places';
|
|
import { countRows } from '../../helpers/factories/rows';
|
|
import { addTourWaypoints, makeTour, markStationVisit } 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';
|
|
import { MikroORM } from '@mikro-orm/core';
|
|
import { NotFoundException } from '@nestjs/common';
|
|
|
|
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';
|
|
|
|
const db = createSnapshotTestDb();
|
|
let t: TestModule;
|
|
let visits: TourVisitsService;
|
|
const orm = () => t.get(MikroORM);
|
|
|
|
beforeAll(async () => {
|
|
t = await createTestModule({ db, imports: [ToursModule] });
|
|
visits = t.get(TourVisitsService);
|
|
});
|
|
beforeEach(() => {
|
|
resetTestDb(db);
|
|
t.realtime.broadcastMock.mockClear();
|
|
});
|
|
afterAll(async () => {
|
|
await t.close();
|
|
db.close();
|
|
});
|
|
|
|
/**
|
|
* A trip with a city walk on days 1 and 2: a must-see cathedral that stands
|
|
* for the museum, a shaping point, a market passed through, a point saved
|
|
* before keys and a park. A hike and a plain cafe are on day 1 too; day 3
|
|
* holds nothing. Another trip has its own walk on its own day.
|
|
*/
|
|
async function fixture() {
|
|
const { user } = await makeUser(orm());
|
|
const trip = await makeTrip(orm(), user.id);
|
|
const day1 = await makeDay(orm(), trip.id, { day_number: 1 });
|
|
const day2 = await makeDay(orm(), trip.id, { day_number: 2 });
|
|
const day3 = await makeDay(orm(), trip.id, { day_number: 3 });
|
|
const museum = await makePlace(orm(), trip.id, { name: 'Museum' });
|
|
const walk = await makePlace(orm(), trip.id, { name: 'Old town' });
|
|
await makeTour(orm(), walk.id, { tourTypeRef: 'walk', duration: 95 });
|
|
await addTourWaypoints(orm(), walk.id, [
|
|
{
|
|
lat: 48.1,
|
|
lng: 11.5,
|
|
station_key: 'stop-cathedral',
|
|
name: 'Cathedral',
|
|
stay_minutes: 30,
|
|
importance: 'must',
|
|
protected: 1,
|
|
linkedPlace: museum.id,
|
|
},
|
|
{ lat: 48.105, lng: 11.505, station_key: 'shape-1', shaping: 1 },
|
|
{ lat: 48.11, lng: 11.51, station_key: 'stop-market', stay_minutes: 0, importance: 'optional' },
|
|
{ lat: 48.12, lng: 11.52 },
|
|
{ lat: 48.13, lng: 11.53, station_key: 'stop-park', name: 'Park', stay_minutes: 45 },
|
|
]);
|
|
const hike = await makePlace(orm(), trip.id, { name: 'Ridge' });
|
|
await makeTour(orm(), hike.id);
|
|
await addTourWaypoints(orm(), hike.id, [
|
|
{ lat: 47, lng: 11, station_key: 'ridge-a' },
|
|
{ lat: 47.1, lng: 11.1, station_key: 'ridge-b' },
|
|
]);
|
|
const cafe = await makePlace(orm(), trip.id, { name: 'Cafe' });
|
|
const onDay1 = await makeDayAssignment(orm(), day1.id, walk.id);
|
|
const onDay2 = await makeDayAssignment(orm(), day2.id, walk.id);
|
|
const hikeOnDay1 = await makeDayAssignment(orm(), day1.id, hike.id);
|
|
const cafeOnDay1 = await makeDayAssignment(orm(), day1.id, cafe.id);
|
|
|
|
const other = await makeTrip(orm(), user.id);
|
|
const otherDay = await makeDay(orm(), other.id, { day_number: 1 });
|
|
const otherWalk = await makePlace(orm(), other.id, { name: 'Harbour' });
|
|
await makeTour(orm(), otherWalk.id, { tourTypeRef: 'walk' });
|
|
const foreign = await makeDayAssignment(orm(), otherDay.id, otherWalk.id);
|
|
return {
|
|
trip,
|
|
day1,
|
|
day2,
|
|
day3,
|
|
museum,
|
|
walk,
|
|
hike,
|
|
cafe,
|
|
onDay1,
|
|
onDay2,
|
|
hikeOnDay1,
|
|
cafeOnDay1,
|
|
other,
|
|
otherDay,
|
|
otherWalk,
|
|
foreign,
|
|
};
|
|
}
|
|
|
|
describe('TourVisitsService', () => {
|
|
it('TOUR-VISITS-001: list reads every visit on the days of the trip, none of another trip', async () => {
|
|
const { trip, onDay1, onDay2, foreign } = await fixture();
|
|
await markStationVisit(orm(), onDay2.id, 'stop-park', 'skipped');
|
|
await markStationVisit(orm(), onDay1.id, 'stop-cathedral', 'visited');
|
|
await markStationVisit(orm(), foreign.id, 'stop-park', 'visited');
|
|
|
|
expect(await visits.list(String(trip.id))).toEqual({
|
|
visits: [
|
|
{ assignment_id: onDay1.id, station_key: 'stop-cathedral', status: 'visited' },
|
|
{ assignment_id: onDay2.id, station_key: 'stop-park', status: 'skipped' },
|
|
],
|
|
});
|
|
});
|
|
|
|
it('TOUR-VISITS-002: update marks a station visited, then skipped, then takes it back, and tells the trip each time', async () => {
|
|
const { trip, onDay1, onDay2 } = await fixture();
|
|
const tripId = String(trip.id);
|
|
await markStationVisit(orm(), onDay2.id, 'stop-cathedral', 'visited');
|
|
|
|
expect(await visits.update(tripId, onDay1.id, 'stop-cathedral', { status: 'visited' }, 'sock-1')).toEqual({
|
|
visits: [{ assignment_id: onDay1.id, station_key: 'stop-cathedral', status: 'visited' }],
|
|
});
|
|
expect(t.realtime.broadcastMock).toHaveBeenLastCalledWith(
|
|
tripId,
|
|
'tours:visits',
|
|
{
|
|
assignmentId: onDay1.id,
|
|
visits: [{ assignment_id: onDay1.id, station_key: 'stop-cathedral', status: 'visited' }],
|
|
},
|
|
'sock-1',
|
|
);
|
|
|
|
const skipped = await visits.update(tripId, onDay1.id, 'stop-cathedral', { status: 'skipped' });
|
|
expect(skipped.visits).toEqual([{ assignment_id: onDay1.id, station_key: 'stop-cathedral', status: 'skipped' }]);
|
|
expect(t.realtime.broadcastMock).toHaveBeenLastCalledWith(
|
|
tripId,
|
|
'tours:visits',
|
|
{ assignmentId: onDay1.id, visits: skipped.visits },
|
|
undefined,
|
|
);
|
|
|
|
expect(await visits.update(tripId, onDay1.id, 'stop-cathedral', { status: 'planned' }, 'sock-2')).toEqual({
|
|
visits: [],
|
|
});
|
|
expect(t.realtime.broadcastMock).toHaveBeenLastCalledWith(
|
|
tripId,
|
|
'tours:visits',
|
|
{ assignmentId: onDay1.id, visits: [] },
|
|
'sock-2',
|
|
);
|
|
expect(t.realtime.broadcastMock).toHaveBeenCalledTimes(3);
|
|
// The same walk on day 2 keeps what was checked off there.
|
|
expect((await visits.walkOnDay(trip.id, onDay2.id)).visits).toEqual([
|
|
{ assignment_id: onDay2.id, station_key: 'stop-cathedral', status: 'visited' },
|
|
]);
|
|
});
|
|
|
|
it('TOUR-VISITS-003: walkOnDay reads the walk on its day without shaping points, and 404s anything else', async () => {
|
|
const { trip, day1, walk, onDay1, hikeOnDay1, cafeOnDay1, foreign } = await fixture();
|
|
await markStationVisit(orm(), onDay1.id, 'stop-market', 'skipped');
|
|
|
|
const walkOnDay = await visits.walkOnDay(String(trip.id), onDay1.id);
|
|
expect(walkOnDay).toEqual<WalkOnDay>({
|
|
assignmentId: onDay1.id,
|
|
dayId: day1.id,
|
|
placeId: walk.id,
|
|
stations: expect.any(Array),
|
|
visits: [{ assignment_id: onDay1.id, station_key: 'stop-market', status: 'skipped' }],
|
|
});
|
|
expect(walkOnDay.stations.map((station) => station.sequence)).toEqual([0, 2, 3, 4]);
|
|
|
|
await expect(visits.walkOnDay(trip.id, foreign.id)).rejects.toEqual(new NotFoundException('Assignment not found'));
|
|
await expect(visits.walkOnDay(trip.id, 999_999)).rejects.toEqual(new NotFoundException('Assignment not found'));
|
|
await expect(visits.walkOnDay(trip.id, hikeOnDay1.id)).rejects.toEqual(
|
|
new NotFoundException('City walk not found'),
|
|
);
|
|
await expect(visits.walkOnDay(trip.id, cafeOnDay1.id)).rejects.toEqual(
|
|
new NotFoundException('City walk not found'),
|
|
);
|
|
});
|
|
|
|
it('TOUR-VISITS-004: a station the walk does not have is a 404 that writes and tells nothing', async () => {
|
|
const { trip, onDay1, hikeOnDay1 } = await fixture();
|
|
const tripId = String(trip.id);
|
|
const refusals = [
|
|
visits.update(tripId, onDay1.id, 'stop-unknown', { status: 'visited' }),
|
|
// A shaping point steers the line only; it is no station.
|
|
visits.update(tripId, onDay1.id, 'shape-1', { status: 'visited' }),
|
|
visits.update(tripId, hikeOnDay1.id, 'ridge-a', { status: 'visited' }),
|
|
];
|
|
const errors = await Promise.all(refusals.map((refusal) => refusal.catch((err: unknown) => err)));
|
|
expect(errors).toEqual([
|
|
new NotFoundException('Station not found'),
|
|
new NotFoundException('Station not found'),
|
|
new NotFoundException('City walk not found'),
|
|
]);
|
|
expect(await countRows(orm(), TourStationVisits, {})).toBe(0);
|
|
expect(t.realtime.broadcastMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('TOUR-VISITS-005: progress lists every station with its status that day and sums what is still open', async () => {
|
|
const { trip, day1, museum, walk, onDay1, onDay2 } = await fixture();
|
|
await markStationVisit(orm(), onDay1.id, 'stop-cathedral', 'visited');
|
|
await markStationVisit(orm(), onDay1.id, 'stop-market', 'skipped');
|
|
await markStationVisit(orm(), onDay2.id, 'stop-park', 'visited');
|
|
const before = await countRows(orm(), TourStationVisits, {});
|
|
|
|
expect(await visits.progress(trip.id, walk.id, day1.id)).toEqual<WalkProgress>({
|
|
place_id: walk.id,
|
|
day_id: day1.id,
|
|
assignment_id: onDay1.id,
|
|
stations: [
|
|
{
|
|
position: 1,
|
|
key: 'stop-cathedral',
|
|
name: 'Cathedral',
|
|
lat: 48.1,
|
|
lng: 11.5,
|
|
status: 'visited',
|
|
importance: 'must',
|
|
protected: true,
|
|
stay_minutes: 30,
|
|
opening_hours: null,
|
|
linked_place_id: museum.id,
|
|
},
|
|
{
|
|
position: 2,
|
|
key: 'stop-market',
|
|
name: null,
|
|
lat: 48.11,
|
|
lng: 11.51,
|
|
status: 'skipped',
|
|
importance: 'optional',
|
|
protected: false,
|
|
stay_minutes: 0,
|
|
opening_hours: null,
|
|
linked_place_id: null,
|
|
},
|
|
{
|
|
position: 3,
|
|
key: null,
|
|
name: null,
|
|
lat: 48.12,
|
|
lng: 11.52,
|
|
status: 'planned',
|
|
importance: 'normal',
|
|
protected: false,
|
|
stay_minutes: null,
|
|
opening_hours: null,
|
|
linked_place_id: null,
|
|
},
|
|
{
|
|
position: 4,
|
|
key: 'stop-park',
|
|
name: 'Park',
|
|
lat: 48.13,
|
|
lng: 11.53,
|
|
status: 'planned',
|
|
importance: 'normal',
|
|
protected: false,
|
|
stay_minutes: 45,
|
|
opening_hours: null,
|
|
linked_place_id: null,
|
|
},
|
|
],
|
|
open: { stations: 2, stay_minutes_known: 45, stays_unknown: 1 },
|
|
walking_minutes: 95,
|
|
});
|
|
expect(await countRows(orm(), TourStationVisits, {})).toBe(before);
|
|
});
|
|
|
|
it('TOUR-VISITS-007: the way back of a loop is no station: its start is read once and its key is refused', async () => {
|
|
const { trip, day1 } = await fixture();
|
|
const loop = await makePlace(orm(), trip.id, { name: 'Round' });
|
|
await makeTour(orm(), loop.id, { tourTypeRef: 'walk', duration: 60 });
|
|
const cathedral = {
|
|
lat: 48.1,
|
|
lng: 11.5,
|
|
name: 'Cathedral',
|
|
stay_minutes: 30,
|
|
importance: 'must',
|
|
protected: 1,
|
|
} as const;
|
|
await addTourWaypoints(orm(), loop.id, [
|
|
{ ...cathedral, station_key: 'stop-cathedral' },
|
|
{ lat: 48.11, lng: 11.51, station_key: 'stop-market' },
|
|
{ lat: 48.13, lng: 11.53, station_key: 'stop-park', stay_minutes: 45 },
|
|
// A way back saved as a copy of the start, the way loops were closed at first.
|
|
{ ...cathedral, station_key: 'stop-back' },
|
|
]);
|
|
const onDay1 = await makeDayAssignment(orm(), day1.id, loop.id);
|
|
await markStationVisit(orm(), onDay1.id, 'stop-cathedral', 'visited');
|
|
|
|
const progress = await visits.progress(trip.id, loop.id, day1.id);
|
|
expect(progress.stations.map((station) => [station.name, station.status])).toEqual([
|
|
['Cathedral', 'visited'],
|
|
[null, 'planned'],
|
|
[null, 'planned'],
|
|
]);
|
|
expect(progress.open).toEqual({ stations: 2, stay_minutes_known: 45, stays_unknown: 1 });
|
|
await expect(visits.update(String(trip.id), onDay1.id, 'stop-back', { status: 'visited' })).rejects.toEqual(
|
|
new NotFoundException('Station not found'),
|
|
);
|
|
});
|
|
|
|
it('TOUR-VISITS-006: progress of a walk without stations or walking time, and of a walk not on that day', async () => {
|
|
const { trip, day3, walk, other, otherDay, otherWalk, foreign } = await fixture();
|
|
expect(await visits.progress(other.id, otherWalk.id, otherDay.id)).toEqual<WalkProgress>({
|
|
place_id: otherWalk.id,
|
|
day_id: otherDay.id,
|
|
assignment_id: foreign.id,
|
|
stations: [],
|
|
open: { stations: 0, stay_minutes_known: 0, stays_unknown: 0 },
|
|
walking_minutes: null,
|
|
});
|
|
await expect(visits.progress(trip.id, walk.id, day3.id)).rejects.toEqual(
|
|
new NotFoundException('City walk not on this day'),
|
|
);
|
|
await expect(visits.progress(trip.id, otherWalk.id, otherDay.id)).rejects.toEqual(
|
|
new NotFoundException('City walk not on this day'),
|
|
);
|
|
});
|
|
});
|