feat(tours): read the walking time left of a city walk on a day over MCP, apart from its stays and the whole walk

This commit is contained in:
Maurice
2026-10-21 12:16:57 +02:00
parent 4eaf5e6a11
commit 43353b9394
12 changed files with 743 additions and 17 deletions
@@ -4,13 +4,14 @@
* 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 { Places } from '../../../src/db/entities/Places.entity';
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 { countRows, updateRows } 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';
@@ -174,6 +175,7 @@ describe('TourVisitsService', () => {
assignmentId: onDay1.id,
dayId: day1.id,
placeId: walk.id,
loop: false,
stations: expect.any(Array),
visits: [{ assignment_id: onDay1.id, station_key: 'stop-market', status: 'skipped' }],
});
@@ -330,4 +332,66 @@ describe('TourVisitsService', () => {
new NotFoundException('City walk not on this day'),
);
});
it('TOUR-VISITS-008: remaining counts from the last visit along the saved line, a point without a key passed behind it', async () => {
const { trip, day1, walk, onDay1 } = await fixture();
// Straight north through the four stations, one stretch of 0.01 degrees between each.
const line = [
[48.1, 11.5],
[48.11, 11.51],
[48.12, 11.52],
[48.13, 11.53],
];
await updateRows(orm(), Places, { id: walk.id }, { route_geometry: JSON.stringify(line) });
await markStationVisit(orm(), onDay1.id, 'stop-cathedral', 'visited');
await markStationVisit(orm(), onDay1.id, 'stop-market', 'visited');
const before = await countRows(orm(), TourStationVisits, {});
const remaining = await visits.remaining(trip.id, walk.id, day1.id);
expect(remaining).toMatchObject({
place_id: walk.id,
day_id: day1.id,
assignment_id: onDay1.id,
loop: false,
reference: { kind: 'last_visited', station: { position: 2, key: 'stop-market', name: null } },
walking: { total_minutes: 95, remaining_minutes: 63, upper_bound: false, after_last_station_minutes: 0 },
stays: { remaining_known_minutes: 45, unknown: [{ position: 3, key: null, name: null }] },
});
expect(remaining.open_stations.map((station) => [station.position, station.walk_minutes_from_reference])).toEqual([
[3, 31],
[4, 63],
]);
expect(await countRows(orm(), TourStationVisits, {})).toBe(before);
expect(t.realtime.broadcastMock).not.toHaveBeenCalled();
});
it('TOUR-VISITS-009: a loop keeps its way back once every station is done, and a walk not on that day is a 404', async () => {
const { trip, day1, day3, walk } = await fixture();
const loop = await makePlace(orm(), trip.id, {
name: 'Round',
route_geometry: JSON.stringify([
[48.1, 11.5],
[48.11, 11.5],
[48.11, 11.51],
[48.1, 11.5],
]),
});
await makeTour(orm(), loop.id, { tourTypeRef: 'walk', duration: 60 });
await addTourWaypoints(orm(), loop.id, [
{ lat: 48.1, lng: 11.5, station_key: 'stop-start', stay_minutes: 0 },
{ lat: 48.11, lng: 11.51, station_key: 'stop-tower', stay_minutes: 15 },
{ lat: 48.1, lng: 11.5, station_key: 'stop-back' },
]);
const onDay1 = await makeDayAssignment(orm(), day1.id, loop.id);
await markStationVisit(orm(), onDay1.id, 'stop-start', 'visited');
await markStationVisit(orm(), onDay1.id, 'stop-tower', 'visited');
const remaining = await visits.remaining(trip.id, loop.id, day1.id);
expect(remaining).toMatchObject({ loop: true, finished: true, open_stations: [] });
expect(remaining.walking.after_last_station_minutes).toBeGreaterThan(0);
expect(remaining.walking.remaining_minutes).toBe(remaining.walking.after_last_station_minutes);
await expect(visits.remaining(trip.id, walk.id, day3.id)).rejects.toEqual(
new NotFoundException('City walk not on this day'),
);
});
});
+154
View File
@@ -796,4 +796,158 @@ describe('ToursMcp', () => {
'routed where the piste router planned it and free',
);
});
/** CW-13: the walk of walkFixture with a saved line through its three stops, the market halfway along. */
async function remainingFixture() {
const base = await walkFixture();
const orm = await createTestOrm(db);
const line = [
[48.1, 11.5],
[48.11, 11.51],
[48.12, 11.52],
];
await updateRows(orm, Places, { id: base.walk.id }, { route_geometry: JSON.stringify(line) });
await orm.close();
const remaining = async (userId: number, dayId = base.day1.id) =>
read(await mcp.getCityWalkRemainingTime({ tripId: base.trip.id, placeId: base.walk.id, dayId }, ctx(userId)));
return { ...base, remaining };
}
it('TOURS-MCP-028: get_city_walk_remaining_time counts from the last visit, with walking time, stays and the whole walk apart', async () => {
const { member, trip, day1, walk, onDay1, remaining } = await remainingFixture();
const first = await remaining(member.id);
expect(first).toMatchObject({
place_id: walk.id,
day_id: day1.id,
assignment_id: onDay1.id,
loop: false,
live_position: false,
finished: false,
reference: { kind: 'last_visited', station: { position: 1, key: 'stop-cathedral', name: 'Cathedral' } },
});
// The market ahead was skipped, but the saved line still leads through it.
expect(first.walking).toEqual({
total_minutes: 80,
remaining_minutes: 80,
unknown_because: null,
upper_bound: true,
skipped_ahead: [{ position: 2, key: 'stop-market', name: null }],
after_last_station_minutes: 0,
});
expect(first.stays).toEqual({
remaining_known_minutes: 0,
unknown: [{ position: 3, key: 'stop-park', name: null }],
optional_known_minutes: 0,
});
expect(first.open_stations).toEqual([
{
position: 3,
key: 'stop-park',
name: null,
importance: 'must',
stay_minutes: null,
walk_minutes_from_reference: 80,
},
]);
expect(first.derived).toEqual(expect.arrayContaining(['walking.remaining_minutes', 'reference']));
// Checked off since: read again, the stretch to the market is behind.
await t.get(TourVisitsService).update(String(trip.id), onDay1.id, 'stop-market', { status: 'visited' });
const again = await remaining(member.id);
expect(again.reference).toEqual({
kind: 'last_visited',
station: { position: 2, key: 'stop-market', name: null },
});
expect(again.walking).toMatchObject({ remaining_minutes: 40, upper_bound: false, skipped_ahead: [] });
expect(again.open_stations[0].walk_minutes_from_reference).toBe(40);
});
it("TOURS-MCP-029: the same walk on another day counts from its start, none of the first day's visits in it", async () => {
const { member, day2, walk, remaining } = await remainingFixture();
const orm = await createTestOrm(db);
const onDay2 = await makeDayAssignment(orm, day2.id, walk.id);
await orm.close();
const result = await remaining(member.id, day2.id);
expect(result).toMatchObject({ assignment_id: onDay2.id, reference: { kind: 'start' }, finished: false });
expect(result.walking).toMatchObject({ remaining_minutes: 80, upper_bound: false });
expect(result.open_stations.map((station: { key: string }) => station.key)).toEqual([
'stop-cathedral',
'stop-market',
'stop-park',
]);
expect(result.stays).toMatchObject({ remaining_known_minutes: 50 });
});
it('TOURS-MCP-030: without a line or with a station open before the last visit, the time stays unknown with its reason', async () => {
const { user, trip, walk, onDay1, remaining } = await remainingFixture();
const visits = t.get(TourVisitsService);
await visits.update(String(trip.id), onDay1.id, 'stop-cathedral', { status: 'planned' });
await visits.update(String(trip.id), onDay1.id, 'stop-market', { status: 'visited' });
const unclear = await remaining(user.id);
expect(unclear.reference).toEqual({
kind: 'ambiguous',
last_visited: { position: 2, key: 'stop-market', name: null },
open_before: [{ position: 1, key: 'stop-cathedral', name: 'Cathedral' }],
});
expect(unclear.walking).toMatchObject({
total_minutes: 80,
remaining_minutes: null,
unknown_because: 'progress_ambiguous',
});
const orm = await createTestOrm(db);
await updateRows(orm, Places, { id: walk.id }, { route_geometry: null });
await orm.close();
await visits.update(String(trip.id), onDay1.id, 'stop-cathedral', { status: 'visited' });
const pending = await remaining(user.id);
expect(pending.walking).toMatchObject({
total_minutes: 80,
remaining_minutes: null,
unknown_because: 'route_pending',
after_last_station_minutes: null,
});
expect(pending.open_stations[0].walk_minutes_from_reference).toBeNull();
});
it('TOURS-MCP-031: reading changes nothing, tells an outsider nothing and names no walk for a day without it', async () => {
const { user, outsider, trip, day2, walk, remaining } = await remainingFixture();
const orm = await createTestOrm(db);
const before = await countRows(orm, TourStationVisits, {});
const revision = (await findRow(orm, Tours, { place: walk.id }))?.revision;
await remaining(user.id);
expect(await countRows(orm, TourStationVisits, {})).toBe(before);
expect((await findRow(orm, Tours, { place: walk.id }))?.revision).toBe(revision);
await orm.close();
expect(await remaining(outsider.id)).toEqual({ error: 'Trip not found or access denied.' });
expect(await remaining(user.id, day2.id)).toEqual({ error: 'No city walk of this trip is on that day.' });
const failure = new Error('database gone');
const spy = vi.spyOn(t.get(TourVisitsService), 'remaining').mockRejectedValueOnce(failure);
try {
await expect(
mcp.getCityWalkRemainingTime({ tripId: trip.id, placeId: walk.id, dayId: day2.id }, ctx(user.id)),
).rejects.toBe(failure);
} finally {
spy.mockRestore();
}
});
it('TOURS-MCP-032: the remaining time names its reference, its bounds and that no live position is known', () => {
const entry = getEntry(ToursMcp as unknown as ClassRef, 'getCityWalkRemainingTime');
const description = entry?.kind === 'tool' ? (entry.options.description ?? '') : '';
for (const field of [
'reference',
'live_position',
'upper_bound',
'after_last_station_minutes',
'unknown_because',
'stays.unknown',
'never by how many stations are open',
'Reading changes nothing',
]) {
expect(description).toContain(field);
}
const progress = getEntry(ToursMcp as unknown as ClassRef, 'getCityWalkProgress');
expect(progress?.kind === 'tool' ? progress.options.description : '').toContain('get_city_walk_remaining_time');
});
});