mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-04 01:44:13 +00:00
feat(tours): take a tour over from another trip, server and contract (LIB-02)
GET /api/tours/library lists the tours of every trip the user owns or is a member of, with the trip each is in and how many tours each trip holds, to choose one to take over into the trip being planned. It is narrowed by a part of the name (matched ignoring case, a "%" or "_" just a character), a type or one trip, and the asking trip can be left out; up to 100 tours, and the answer says when there were more. GET /api/tours/library/:placeId previews one: its facts and its line thinned to 400 points, its ends kept. Neither is trip-scoped, so every answer is bounded by the user's own trips, read anew each call: a tour of a trip out of reach is "not found", nothing hints that it exists. Measures stay null where the tour has none. POST /api/trips/:tripId/tours/take-over copies one into this trip, which the guard checks with 'place_edit'; the source trip is checked against the user's trips as they are at that moment, and the source is read again inside the write. The copy is a tour of its own: its line, measures, settings and control points, without days, without its stops' links to places of the other trip and without the obstacle decisions taken there; it keeps the import record of its line. A copy that fails leaves nothing behind. Where it came from is kept as text (tours.taken_over_from_trip_title and taken_over_from_tour_name, migration 045100), not as a pointer, and read back in the provenance as takenOverFrom; a copy of such a copy names it too. The in-trip copy (P2C-07) and the take-over share one copy routine. MCP: list_tour_library and take_over_tour, over the same service and checks. The repository tests follow the hiking preferences column of MF-08.
This commit is contained in:
@@ -62,6 +62,7 @@ const UNNUMBERED = [
|
||||
'Migration20200101044800_a_tour_counts_its_revisions',
|
||||
'Migration20200101044900_a_tour_waypoint_can_be_a_place_of_the_trip',
|
||||
'Migration20200101045000_a_hike_keeps_its_routing_preferences',
|
||||
'Migration20200101045100_a_tour_remembers_the_trip_it_was_taken_from',
|
||||
];
|
||||
// The last step the positional runner ever had; planLegacyBaseline refuses a
|
||||
// schema_version past it.
|
||||
|
||||
@@ -100,6 +100,7 @@ describe('ToursRepository reads', () => {
|
||||
track_marks: null,
|
||||
has_recorded_track: 0,
|
||||
revision: 1,
|
||||
hiking_preferences: null,
|
||||
},
|
||||
{
|
||||
place_id: older.id,
|
||||
@@ -129,6 +130,7 @@ describe('ToursRepository reads', () => {
|
||||
track_marks: null,
|
||||
has_recorded_track: 0,
|
||||
revision: 1,
|
||||
hiking_preferences: null,
|
||||
},
|
||||
]);
|
||||
expect((await repo.listForTrip(trip.id)).map((r) => r.place_id)).not.toContain(plain.id);
|
||||
@@ -214,6 +216,7 @@ describe('ToursRepository writes', () => {
|
||||
track_marks: null,
|
||||
has_recorded_track: 0,
|
||||
revision: 1,
|
||||
hiking_preferences: null,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -293,6 +296,9 @@ describe('ToursRepository writes', () => {
|
||||
paddle_lake_style: null,
|
||||
track_marks: null,
|
||||
revision: 1,
|
||||
hiking_preferences: null,
|
||||
taken_over_from_trip_title: null,
|
||||
taken_over_from_tour_name: null,
|
||||
},
|
||||
]);
|
||||
expect(await repo.listRowsForTrip(other.id)).toEqual([]);
|
||||
|
||||
@@ -0,0 +1,130 @@
|
||||
/**
|
||||
* TourLibraryMcp (LIB-02): the tours of the user's other trips and taking one
|
||||
* over, over the same service as REST. The suite takes its subject from the
|
||||
* container over a snapshot test db; the addon gate is the registry's.
|
||||
*/
|
||||
import { Places } from '../../../src/db/entities/Places.entity';
|
||||
import type { McpContext } from '../../../src/nest-mcp';
|
||||
import { McpToolGuardsService } from '../../../src/nest/mcp-shared/mcp-tool-guards.service';
|
||||
import { TourLibraryMcp } from '../../../src/nest/tours/tour-library.mcp';
|
||||
import { ToursModule } from '../../../src/nest/tours/tours.module';
|
||||
import { createSnapshotTestDb } from '../../helpers/db-mock';
|
||||
import { createPlace, createTrip, createUser } from '../../helpers/factories';
|
||||
import { findRow, updateRows } from '../../helpers/factories/rows';
|
||||
import { resetTestDb } from '../../helpers/test-db';
|
||||
import { createTestModule, type TestModule } from '../../helpers/test-module';
|
||||
import { createTestOrm } from '../../helpers/test-orm';
|
||||
import { createTour } from '../../helpers/tours-repos';
|
||||
|
||||
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
const db = createSnapshotTestDb();
|
||||
let t: TestModule;
|
||||
let mcp: TourLibraryMcp;
|
||||
|
||||
beforeAll(async () => {
|
||||
t = await createTestModule({ db, imports: [ToursModule] });
|
||||
mcp = t.get(TourLibraryMcp);
|
||||
});
|
||||
beforeEach(() => resetTestDb(db));
|
||||
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);
|
||||
}
|
||||
|
||||
async function fixture() {
|
||||
const { user } = createUser(db);
|
||||
const { user: outsider } = createUser(db);
|
||||
const alps = createTrip(db, user.id, { title: 'Alps' });
|
||||
const next = createTrip(db, user.id, { title: 'Next' });
|
||||
const elsewhere = createTrip(db, outsider.id, { title: 'Elsewhere' });
|
||||
const ridge = createPlace(db, alps.id, { name: 'Ridge' });
|
||||
createTour(db, ridge.id, { distance: 8.2, max_hiking_difficulty: 3 });
|
||||
const gorge = createPlace(db, alps.id, { name: 'Gorge' });
|
||||
createTour(db, gorge.id);
|
||||
const orm = await createTestOrm(db);
|
||||
await updateRows(
|
||||
orm,
|
||||
Places,
|
||||
{ id: ridge.id },
|
||||
{
|
||||
route_geometry: JSON.stringify([
|
||||
[47, 11, 600],
|
||||
[47.01, 11.01, 650],
|
||||
]),
|
||||
},
|
||||
);
|
||||
await orm.close();
|
||||
return { user, outsider, alps, next, elsewhere, ridge, gorge };
|
||||
}
|
||||
|
||||
describe('TourLibraryMcp', () => {
|
||||
it('TOUR-LIB-MCP-001: list_tour_library lists the tours of the user’s trips with their trip, nothing of others', async () => {
|
||||
const { user, outsider, alps, next, ridge } = await fixture();
|
||||
const own = read(await mcp.listTourLibrary({ exclude_trip_id: next.id }, ctx(user.id)));
|
||||
expect(own.tours.map((tour: { name: string }) => tour.name).sort()).toEqual(['Gorge', 'Ridge']);
|
||||
expect(own.tours.find((tour: { place_id: number }) => tour.place_id === ridge.id)).toMatchObject({
|
||||
distance: 8.2,
|
||||
trip: { id: alps.id, title: 'Alps' },
|
||||
});
|
||||
expect(read(await mcp.listTourLibrary({ q: 'gor' }, ctx(user.id))).tours).toHaveLength(1);
|
||||
expect(read(await mcp.listTourLibrary({}, ctx(outsider.id))).tours).toEqual([]);
|
||||
});
|
||||
|
||||
it('TOUR-LIB-MCP-002: take_over_tour copies a tour into a trip, remembering where it came from', async () => {
|
||||
const { user, alps, next, ridge } = await fixture();
|
||||
const result = read(
|
||||
await mcp.takeOverTour({ tripId: next.id, source_trip_id: alps.id, source_place_id: ridge.id }, ctx(user.id)),
|
||||
);
|
||||
expect(result.tour).toMatchObject({ name: 'Ridge', revision: 1 });
|
||||
expect(result.provenance.takenOverFrom).toEqual({ tripTitle: 'Alps', tourName: 'Ridge' });
|
||||
const orm = await createTestOrm(db);
|
||||
expect(await findRow(orm, Places, { id: result.tour.place_id })).toMatchObject({ trip_id: next.id });
|
||||
await orm.close();
|
||||
});
|
||||
|
||||
it('TOUR-LIB-MCP-003: refuses a trip the user cannot open, and a source out of reach or without a line', async () => {
|
||||
const { user, outsider, alps, next, elsewhere, ridge, gorge } = await fixture();
|
||||
expect(
|
||||
read(
|
||||
await mcp.takeOverTour(
|
||||
{ tripId: alps.id, source_trip_id: alps.id, source_place_id: ridge.id },
|
||||
ctx(outsider.id),
|
||||
),
|
||||
),
|
||||
).toEqual({ error: 'Trip not found or access denied.' });
|
||||
expect(
|
||||
read(
|
||||
await mcp.takeOverTour(
|
||||
{ tripId: elsewhere.id, source_trip_id: alps.id, source_place_id: ridge.id },
|
||||
ctx(outsider.id),
|
||||
),
|
||||
),
|
||||
).toEqual({ error: 'Tour not found.' });
|
||||
expect(
|
||||
read(
|
||||
await mcp.takeOverTour({ tripId: next.id, source_trip_id: alps.id, source_place_id: gorge.id }, ctx(user.id)),
|
||||
),
|
||||
).toEqual({ error: 'Tour not found.' });
|
||||
});
|
||||
|
||||
it('TOUR-LIB-MCP-004: without place_edit on the trip it writes into, nothing is copied', async () => {
|
||||
const { user, alps, next, ridge } = await fixture();
|
||||
const denied = vi.spyOn(t.get(McpToolGuardsService), 'hasTripPermission').mockResolvedValue(false);
|
||||
const result = await mcp.takeOverTour(
|
||||
{ tripId: next.id, source_trip_id: alps.id, source_place_id: ridge.id },
|
||||
ctx(user.id),
|
||||
);
|
||||
expect(result.isError).toBe(true);
|
||||
expect(denied).toHaveBeenCalledWith('place_edit', next.id, user.id);
|
||||
expect(read(await mcp.listTourLibrary({ trip_id: next.id }, ctx(user.id))).tours).toEqual([]);
|
||||
denied.mockRestore();
|
||||
});
|
||||
});
|
||||
@@ -265,6 +265,8 @@ function tourRepositoryFixture(ownerTripId: number) {
|
||||
has_recorded_track: 0,
|
||||
derived_from_place_id: null,
|
||||
derived_from_name: null,
|
||||
taken_over_from_trip_title: null,
|
||||
taken_over_from_tour_name: null,
|
||||
source_instance: null,
|
||||
source_external_id: null,
|
||||
source_external_url: null,
|
||||
|
||||
@@ -76,6 +76,8 @@ function makeService() {
|
||||
has_recorded_track: 0,
|
||||
derived_from_place_id: null,
|
||||
derived_from_name: null,
|
||||
taken_over_from_trip_title: null,
|
||||
taken_over_from_tour_name: null,
|
||||
source_instance: null,
|
||||
source_external_id: null,
|
||||
source_external_url: null,
|
||||
@@ -185,6 +187,7 @@ describe('ToursService planner contracts (mock-only; SQLite rollback semantics a
|
||||
paddle_speed_kmh: null,
|
||||
portage_speed_kmh: null,
|
||||
paddle_lake_style: null,
|
||||
hiking_preferences: null,
|
||||
});
|
||||
expect((setup.toursRepo.insertTour.mock.calls[0][0] as { distance: number }).distance).toBeGreaterThan(0);
|
||||
expect(setup.waypointsRepo.insertForPlace).toHaveBeenCalledExactlyOnceWith(42, request.waypoints);
|
||||
@@ -199,6 +202,7 @@ describe('ToursService planner contracts (mock-only; SQLite rollback semantics a
|
||||
derivedFrom: null,
|
||||
plannedOver: false,
|
||||
editedLocally: null,
|
||||
takenOverFrom: null,
|
||||
});
|
||||
expect(setup.places.broadcast).toHaveBeenNthCalledWith(1, '7', 'tours:changed', { placeIds: [42] }, 'socket-1');
|
||||
expect(setup.places.broadcast).toHaveBeenNthCalledWith(2, '7', 'place:created', { place: setup.place }, 'socket-1');
|
||||
@@ -300,6 +304,7 @@ describe('ToursService planner contracts (mock-only; SQLite rollback semantics a
|
||||
paddle_speed_kmh: null,
|
||||
portage_speed_kmh: null,
|
||||
paddle_lake_style: null,
|
||||
hiking_preferences: null,
|
||||
});
|
||||
expect(setup.waypointsRepo.deleteForPlace).toHaveBeenCalledExactlyOnceWith(42);
|
||||
expect(setup.waypointsRepo.insertForPlace).toHaveBeenCalledExactlyOnceWith(42, update.waypoints);
|
||||
|
||||
Reference in New Issue
Block a user