mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-05 02:14:05 +00:00
cd60d4d859
P6: insert_tour_stop and remove_tour_stop take a place into a saved hike or city walk as one stop and out again, each the other's undo, with change cards in the chat. The planner's insertion rule (poiInsertion) moved to shared so the planner and the server place a stop the same way. "+ Tour" on a found place holds it as a stop of the saved tour open in the planner. P7: GPX, KML and KMZ files attached in the chat are read into their tracks and each is held as a change that takes it in as a hike through import_tour_gpx, undoable per track; the GPX text stays on the held call.
196 lines
8.1 KiB
TypeScript
196 lines
8.1 KiB
TypeScript
/**
|
|
* P6: TourStopsMcp over a snapshot test db: a found place taken into a saved
|
|
* hike as a stop with insert_tour_stop and taken out again with
|
|
* remove_tour_stop, through the write update_tour saves with. The hike is
|
|
* created through create_hike; its way is planned by a stub of
|
|
* HikeRoutingService, as in the hike suite. The gates are the tours': trip
|
|
* access, then place_edit, on the Tours addon.
|
|
*/
|
|
import { TourWaypoints } from '../../../src/db/entities/TourWaypoints.entity';
|
|
import { Tours } from '../../../src/db/entities/Tours.entity';
|
|
import type { McpContext } from '../../../src/nest-mcp';
|
|
import { getEntry, type ClassRef } from '../../../src/nest-mcp/metadata';
|
|
import { McpToolGuardsService } from '../../../src/nest/mcp-shared/mcp-tool-guards.service';
|
|
import { HikePlanMcp } from '../../../src/nest/tours/hike-plan.mcp';
|
|
import { HikeRoutingService, type PlannedHike } from '../../../src/nest/tours/hike-routing.service';
|
|
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 { WeatherService } from '../../../src/nest/weather/weather.service';
|
|
import { createSnapshotTestDb } from '../../helpers/db-mock';
|
|
import { countRows, findRow } from '../../helpers/factories/rows';
|
|
import { 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 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
|
|
|
|
const db = createSnapshotTestDb();
|
|
const plan = vi.fn<HikeRoutingService['plan']>();
|
|
let t: TestModule;
|
|
let stops: TourStopsMcp;
|
|
let hikes: HikePlanMcp;
|
|
const orm = () => t.get(MikroORM);
|
|
|
|
const LINE: GeometryPoint[] = [
|
|
[47.5, 11.1, 800],
|
|
[47.505, 11.1, 900],
|
|
[47.51, 11.1, 1000],
|
|
];
|
|
|
|
/** The stub's plan: the way above under the limit asked for, whatever the points. */
|
|
function routed(request: HikePlanRequest): PlannedHike {
|
|
return {
|
|
plan: {
|
|
kind: 'routed',
|
|
max_hiking_difficulty: request.max_hiking_difficulty ?? 2,
|
|
hiking_preferences: request.hiking_preferences ?? null,
|
|
distance_m: 1112,
|
|
ascent_m: 200,
|
|
descent_m: 0,
|
|
duration_minutes: 52,
|
|
duration_basis: 'din33466',
|
|
has_heights: true,
|
|
line: LINE,
|
|
profile: [
|
|
[0, 800],
|
|
[1112, 1000],
|
|
],
|
|
grades: null,
|
|
},
|
|
line: LINE,
|
|
snapped: request.waypoints.map(() => 5),
|
|
legs: request.waypoints.slice(1).map(() => 300),
|
|
};
|
|
}
|
|
|
|
beforeAll(async () => {
|
|
t = await createTestModule({
|
|
db,
|
|
imports: [ToursModule],
|
|
overrides: [
|
|
{ provide: HikeRoutingService, useValue: { plan } },
|
|
{ provide: TourPoiSearchService, useValue: { search: vi.fn() } },
|
|
{ provide: WeatherService, useValue: { getDetailed: vi.fn() } },
|
|
],
|
|
});
|
|
stops = t.get(TourStopsMcp);
|
|
hikes = t.get(HikePlanMcp);
|
|
});
|
|
beforeEach(() => {
|
|
resetTestDb(db);
|
|
plan.mockReset().mockImplementation(async (_userId, _tripId, request) => routed(request));
|
|
});
|
|
afterAll(async () => {
|
|
await t.close();
|
|
db.close();
|
|
});
|
|
|
|
const ctx = (userId: number) => ({ userId }) as McpContext;
|
|
|
|
function read(result: { content: Array<{ type: string; text: string }>; isError?: boolean }) {
|
|
const text = result.content[0]?.text ?? '';
|
|
return result.isError ? { error: text } : JSON.parse(text);
|
|
}
|
|
|
|
const WAYPOINTS = [
|
|
{ lat: 47.5, lng: 11.1, name: 'Car park' },
|
|
{ lat: 47.505, lng: 11.1, name: 'Alm' },
|
|
{ lat: 47.51, lng: 11.1, name: 'Summit' },
|
|
];
|
|
const SPRING = { lat: 47.507, lng: 11.1, name: 'Spring', poi_category: 'drinking_water' as const };
|
|
|
|
async function fixture() {
|
|
const { user } = await makeUser(orm());
|
|
const { user: outsider } = await makeUser(orm());
|
|
const trip = await makeTrip(orm(), user.id);
|
|
const hike = read(
|
|
await hikes.createHike({ tripId: trip.id, name: 'Herzogstand', waypoints: WAYPOINTS }, ctx(user.id)),
|
|
);
|
|
return { user, outsider, trip, placeId: hike.tour.place_id as number };
|
|
}
|
|
|
|
describe('TourStopsMcp', () => {
|
|
it('TOUR-STOPS-MCP-001: both tools write on the Tours addon, in the trips group', async () => {
|
|
const options = (method: string) => {
|
|
const entry = getEntry(TourStopsMcp as unknown as ClassRef, method);
|
|
if (!entry || entry.kind !== 'tool') throw new Error(`${method} is no MCP tool`);
|
|
return entry.options;
|
|
};
|
|
const tools = ['insertTourStop', 'removeTourStop'].map(options);
|
|
expect(tools.map((tool) => [tool.name, tool.annotations?.readOnlyHint, tool.access])).toEqual([
|
|
['insert_tour_stop', false, { group: 'trips', mode: 'write' }],
|
|
['remove_tour_stop', false, { group: 'trips', mode: 'write' }],
|
|
]);
|
|
expect(tools[0].description).toContain('after_key');
|
|
const isAddonEnabled = vi.fn(async (id: string) => id === 'tours');
|
|
const gated = await Promise.all(tools.map((tool) => tool.when?.(ctx(1), { addons: { isAddonEnabled } })));
|
|
expect(gated).toEqual([true, true]);
|
|
});
|
|
|
|
it('TOUR-STOPS-MCP-002: takes a place in after the last point before it, plans the hike anew and raises its revision', async () => {
|
|
const f = await fixture();
|
|
plan.mockClear();
|
|
const answer = read(
|
|
await stops.insertTourStop(
|
|
{ tripId: f.trip.id, placeId: f.placeId, expected_revision: 1, stop: SPRING },
|
|
ctx(f.user.id),
|
|
),
|
|
);
|
|
expect(plan).toHaveBeenCalledTimes(1);
|
|
expect(plan.mock.calls[0]?.[2].waypoints.map((point) => point.lat)).toEqual([47.5, 47.505, 47.507, 47.51]);
|
|
expect(answer.tour).toMatchObject({ place_id: f.placeId, tour_type: 'hike', revision: 2 });
|
|
expect(answer.stop).toMatchObject({ name: 'Spring', after_name: 'Alm' });
|
|
expect(typeof answer.stop.key).toBe('string');
|
|
expect(await countRows(orm(), TourWaypoints, { place: f.placeId })).toBe(4);
|
|
});
|
|
|
|
it('TOUR-STOPS-MCP-003: takes the stop out again by its key, back to the points it had', async () => {
|
|
const f = await fixture();
|
|
const own = ctx(f.user.id);
|
|
const inserted = read(
|
|
await stops.insertTourStop({ tripId: f.trip.id, placeId: f.placeId, expected_revision: 1, stop: SPRING }, own),
|
|
);
|
|
const removed = read(
|
|
await stops.removeTourStop(
|
|
{ tripId: f.trip.id, placeId: f.placeId, expected_revision: 2, key: inserted.stop.key },
|
|
own,
|
|
),
|
|
);
|
|
expect(removed.tour.revision).toBe(3);
|
|
expect(removed.removed).toMatchObject({ key: inserted.stop.key, name: 'Spring', lat: 47.507 });
|
|
expect(await countRows(orm(), TourWaypoints, { place: f.placeId })).toBe(3);
|
|
});
|
|
|
|
it('TOUR-STOPS-MCP-004: a stale revision is refused and the hike stays as it was', async () => {
|
|
const f = await fixture();
|
|
await expect(
|
|
stops.insertTourStop(
|
|
{ tripId: f.trip.id, placeId: f.placeId, expected_revision: 4, stop: SPRING },
|
|
ctx(f.user.id),
|
|
),
|
|
).rejects.toThrow('This tour was changed in the meantime');
|
|
expect((await findRow(orm(), Tours, { place: f.placeId }))?.revision).toBe(1);
|
|
});
|
|
|
|
it('TOUR-STOPS-MCP-005: an outsider is refused, and a member without place_edit, as REST refuses them', async () => {
|
|
const f = await fixture();
|
|
const request = { tripId: f.trip.id, placeId: f.placeId, expected_revision: 1, stop: SPRING };
|
|
expect(read(await stops.insertTourStop(request, ctx(f.outsider.id)))).toEqual({
|
|
error: 'Trip not found or access denied.',
|
|
});
|
|
const denied = vi.spyOn(t.get(McpToolGuardsService), 'hasTripPermission').mockResolvedValue(false);
|
|
expect((await stops.insertTourStop(request, ctx(f.user.id))).isError).toBe(true);
|
|
const removal = { tripId: f.trip.id, placeId: f.placeId, expected_revision: 1, key: 'abcdef' };
|
|
expect((await stops.removeTourStop(removal, ctx(f.user.id))).isError).toBe(true);
|
|
expect(denied).toHaveBeenCalledWith('place_edit', f.trip.id, f.user.id);
|
|
denied.mockRestore();
|
|
expect(await countRows(orm(), TourWaypoints, { place: f.placeId })).toBe(3);
|
|
});
|
|
});
|