mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-06 10:44:23 +00:00
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:
@@ -1,8 +1,8 @@
|
||||
/**
|
||||
* Tours e2e for city walks (CW-01 to CW-03, CW-10, CW-11): a walk saved and read
|
||||
* Tours e2e for city walks (CW-01 to CW-03, CW-10, CW-11, CW-13): a walk saved and read
|
||||
* with its stations, one saved with its stations only waiting for its way, the
|
||||
* keys of its stations kept through an edit, and the stations checked off on
|
||||
* the days the walk is on, through the real
|
||||
* the days the walk is on, and what is left of it read over MCP, through the real
|
||||
* JwtAuthGuard, AddonGuard and TripAccessGuard against a migrated and seeded
|
||||
* temp SQLite db. The permission check is a container spy and the broadcast
|
||||
* goes to a FakeRealtimeService, as in tours.e2e.test.ts.
|
||||
@@ -17,13 +17,17 @@ import { Places } from '../../src/db/entities/Places.entity';
|
||||
import { TourStationVisits } from '../../src/db/entities/TourStationVisits.entity';
|
||||
import { Tours } from '../../src/db/entities/Tours.entity';
|
||||
import { Trips } from '../../src/db/entities/Trips.entity';
|
||||
import { trekMcpAccessPolicy, trekMcpValidateAccess } from '../../src/mcp/nest-mcp-policy';
|
||||
import { createTestRegistry, type McpContext } from '../../src/nest-mcp';
|
||||
import { AssignmentsModule } from '../../src/nest/assignments/assignments.module';
|
||||
import { TrekExceptionFilter } from '../../src/nest/common/trek-exception.filter';
|
||||
import { ZodValidationPipe } from '../../src/nest/common/zod-validation.pipe';
|
||||
import { withRequestContext } from '../../src/nest/database/request-context';
|
||||
import { JourneyDomainService } from '../../src/nest/journey/journey-domain.service';
|
||||
import { PermissionsService } from '../../src/nest/permissions/permissions.service';
|
||||
import { RealtimeModule } from '../../src/nest/realtime/realtime.module';
|
||||
import { RealtimeService } from '../../src/nest/realtime/realtime.service';
|
||||
import { ToursMcp } from '../../src/nest/tours/tours.mcp';
|
||||
import { ToursModule } from '../../src/nest/tours/tours.module';
|
||||
import { createSnapshotTestDb } from '../helpers/db-mock';
|
||||
import { makePlace } from '../helpers/factories/places';
|
||||
@@ -36,6 +40,7 @@ import { FakeRealtimeService } from '../helpers/fake-realtime';
|
||||
import { createTestMikroOrmModule, createTestOrm, type TestOrm } from '../helpers/test-orm';
|
||||
import { TestUnitOfWorkModule } from '../helpers/test-uow';
|
||||
import { sessionCookie } from './harness';
|
||||
import { MikroORM } from '@mikro-orm/core';
|
||||
import { Test } from '@nestjs/testing';
|
||||
import { stationKeySchema } from '@trek/shared';
|
||||
|
||||
@@ -472,6 +477,50 @@ describe('City walks e2e (real guards + temp SQLite)', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('remaining time over MCP (CW-13)', () => {
|
||||
const remainingOf = (placeId: number, dayId: number) =>
|
||||
withRequestContext(app.get(MikroORM), async () => {
|
||||
const mcp = app.get(ToursMcp);
|
||||
const result = await mcp.getCityWalkRemainingTime({ tripId: 5, placeId, dayId }, {
|
||||
userId: VIEWER,
|
||||
} as McpContext);
|
||||
return JSON.parse(result.content[0]?.text ?? '{}');
|
||||
});
|
||||
|
||||
it('is registered as a read of the trip', () => {
|
||||
expect(() =>
|
||||
createTestRegistry([app.get(ToursMcp)], {
|
||||
accessPolicy: trekMcpAccessPolicy,
|
||||
validateAccess: trekMcpValidateAccess,
|
||||
}),
|
||||
).not.toThrow();
|
||||
});
|
||||
|
||||
it('counts from the stations checked off over REST, and the same walk on another day from its start', async () => {
|
||||
const { placeId, assignmentId, cathedral, market } = await walkOnDay3();
|
||||
await assign(placeId, 4);
|
||||
expect((await mark(assignmentId, cathedral, { status: 'visited' })).status).toBe(200);
|
||||
expect((await remainingOf(placeId, 3)).walking).toMatchObject({ total_minutes: 60, remaining_minutes: 60 });
|
||||
|
||||
expect((await mark(assignmentId, market, { status: 'visited' })).status).toBe(200);
|
||||
const day3 = await remainingOf(placeId, 3);
|
||||
expect(day3.reference).toMatchObject({ kind: 'last_visited', station: { key: market, name: 'Market' } });
|
||||
expect(day3.walking).toMatchObject({ remaining_minutes: 40, upper_bound: false });
|
||||
expect(day3.open_stations).toEqual([
|
||||
expect.objectContaining({ name: 'Park', importance: 'optional', walk_minutes_from_reference: 40 }),
|
||||
]);
|
||||
expect(day3.stays).toMatchObject({
|
||||
remaining_known_minutes: 0,
|
||||
unknown: [expect.objectContaining({ name: 'Park' })],
|
||||
});
|
||||
|
||||
const day4 = await remainingOf(placeId, 4);
|
||||
expect(day4.reference).toEqual({ kind: 'start' });
|
||||
expect(day4.walking.remaining_minutes).toBe(60);
|
||||
expect(await countRows(orm, TourStationVisits)).toBe(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('refusals', () => {
|
||||
it('404 for an assignment of another trip, a walk that is none, and a station the walk does not have', async () => {
|
||||
const { placeId: hikeId } = await saveWalk(
|
||||
|
||||
@@ -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'),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user