Files
TREK/server/tests/unit/nest/journey-map.mcp.test.ts
T
Maurice 9b71948497 feat(journey): read what a journey map shows beside its entries, and share its plan and recorded days on purpose
GET /api/journeys/:id/map-layers answers the planned places of the linked
trips (one per place, with every day the plan puts it on and the entry
written about it), the trips' GPX routes while the track switch is on, and
the located stays entries were taken over from. It goes through
journey_trips, so a trip nobody wrote about yet shows its plan too. Every
source is read on its own: one that fails says unavailable and the others
still draw. A position that is not one is left out, never moved.

GET /api/public/journey/:token/map-layers cuts the same on the server by the
link: nothing while the map is not shared, the plan and its routes only with
share_plan, the recorded days only with share_recordings and only beside a
published entry, stays only of published entries. Migration 046190 adds both
flags to journey_share_tokens, off by default, so a journey that is public
already shows what it showed before. The share link REST route and the MCP
share tool take them alike; get_journey_map_layers reads the layers over MCP
on the same access, a route by its size only.
2026-10-21 18:15:24 +02:00

107 lines
4.5 KiB
TypeScript

/**
* JourneyMapMcp (JRN-04): the journey map's layers over MCP, on the same
* service and journey access as REST. Places and stays come with their
* positions; a route answers with its name and size, never its geometry.
*/
import { Places } from '../../../src/db/entities/Places.entity';
import type { McpContext } from '../../../src/nest-mcp';
import { getEntry, type ClassRef } from '../../../src/nest-mcp/metadata';
import { AddonsModule } from '../../../src/nest/addons/addons.module';
import { AddonsService } from '../../../src/nest/addons/addons.service';
import { JourneyDomainModule } from '../../../src/nest/journey/journey-domain.module';
import { JourneyMapMcp } from '../../../src/nest/journey/journey-map.mcp';
import { createSnapshotTestDb } from '../../helpers/db-mock';
import { makeDayAssignment } from '../../helpers/factories/itinerary';
import { linkTripToJourney, makeJourney } from '../../helpers/factories/journeys';
import { makePlace } from '../../helpers/factories/places';
import { updateRows } from '../../helpers/factories/rows';
import { makeTrip, readTripDays } 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 { createTestOrm, type TestOrm } from '../../helpers/test-orm';
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
const db = createSnapshotTestDb();
let t: TestModule;
let orm: TestOrm;
let mcp: JourneyMapMcp;
beforeAll(async () => {
t = await createTestModule({ db, imports: [JourneyDomainModule, AddonsModule], providers: [JourneyMapMcp] });
mcp = t.get(JourneyMapMcp);
orm = await createTestOrm(db);
});
beforeEach(() => {
resetTestDb(db);
orm.clear();
vi.restoreAllMocks();
});
afterAll(async () => {
await orm.close();
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);
}
describe('JourneyMapMcp', () => {
it('JRNMAP-MCP-001: answers the plan with its positions and a route by its size, without geometry', async () => {
const { user } = await makeUser(orm);
const journey = await makeJourney(orm, user.id, { show_trip_tracks: 1 });
const trip = await makeTrip(orm, user.id, { start_date: '2026-05-14', end_date: '2026-05-14' });
await linkTripToJourney(orm, journey.id, trip.id);
const [day] = await readTripDays(orm, trip.id);
if (!day) throw new Error('the trip has no day');
const square = await makePlace(orm, trip.id, { name: 'San Marco', lat: 45.434, lng: 12.3388 });
await makeDayAssignment(orm, day.id, square.id);
await updateRows(
orm,
Places,
{ id: square.id },
{
route_geometry: JSON.stringify([
[45.43, 12.33],
[45.44, 12.34],
[45.45, 12.35],
]),
},
);
const answer = read(await mcp.getJourneyMapLayers({ journeyId: journey.id }, ctx(user.id)));
expect(answer.places.items).toEqual([
expect.objectContaining({ name: 'San Marco', lat: 45.434, lng: 12.3388, days: ['2026-05-14'], entry_id: null }),
]);
expect(answer.routes).toEqual({
status: 'ok',
items: [{ place_id: square.id, trip_id: trip.id, name: 'San Marco', points: 3 }],
});
expect(answer.stays).toEqual({ status: 'ok', items: [] });
});
it('JRNMAP-MCP-002: a stranger is refused like REST, and the tool reads under the Journey addon', async () => {
const { user: owner } = await makeUser(orm);
const { user: stranger } = await makeUser(orm);
const journey = await makeJourney(orm, owner.id);
await expect(mcp.getJourneyMapLayers({ journeyId: journey.id }, ctx(stranger.id))).rejects.toThrow(
'Journey not found',
);
const entry = getEntry(JourneyMapMcp as unknown as ClassRef, 'getJourneyMapLayers');
if (entry?.kind !== 'tool') throw new Error('getJourneyMapLayers is no tool');
expect(entry.options.access).toEqual({ group: 'journey', mode: 'read' });
const enabled = vi.spyOn(t.get(AddonsService), 'isAddonEnabled').mockResolvedValue(false);
expect(await entry.options.when?.(ctx(owner.id), mcp)).toBe(false);
enabled.mockResolvedValue(true);
expect(await entry.options.when?.(ctx(owner.id), mcp)).toBe(true);
});
});