mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 11:36:05 +00:00
7470a2c6d4
When the name of maps.app.goo.gl or goo.gl could not be looked up for a moment, the walk import said the short link leads somewhere other than Google Maps. It now says the link could not be followed just now and answers 502, as for any other failure on the way; a name that leads into this network is still refused. A short link Google answers with 404 or 410 is one it does not know, and asking again changes nothing. The dialog now says so in the reader's language and asks to share the route from Google Maps again, with a 400 and the reason short_link_unknown, instead of suggesting to try again later. A busy or blocking answer from Google stays a failure that may pass. The MCP tool preview_google_maps_walk is marked open world, since it follows a short link to Google on every call, and the English text REST and MCP send for each reason is held to the English text the planner shows.
108 lines
4.6 KiB
TypeScript
108 lines
4.6 KiB
TypeScript
/**
|
|
* TourWalkImportMcp (CW-07, WALK-IMPORT-MCP-*): the Google Maps walk preview
|
|
* over MCP, with the same service and checks as REST. The subject comes from
|
|
* the container over a snapshot test db; the refusal text comes back through
|
|
* the registry's error mapper, as a client sees it.
|
|
*/
|
|
import { trekMcpErrorMapper } from '../../../src/mcp/nest-mcp-policy';
|
|
import type { McpContext } from '../../../src/nest-mcp';
|
|
import { getEntry, type ClassRef } from '../../../src/nest-mcp/metadata';
|
|
import { DomainError, catchDomainError } from '../../../src/nest/common/domain-error';
|
|
import { McpToolGuardsService } from '../../../src/nest/mcp-shared/mcp-tool-guards.service';
|
|
import { GoogleMapsShortLinkProvider } from '../../../src/nest/place-import/providers/google-maps-short-link.provider';
|
|
import { TourWalkImportMcp } from '../../../src/nest/tours/tour-walk-import.mcp';
|
|
import { ToursModule } from '../../../src/nest/tours/tours.module';
|
|
import { PORTO_RIBEIRA } from '../../fixtures/google-maps-walks';
|
|
import { createSnapshotTestDb } from '../../helpers/db-mock';
|
|
import { makeTrip } from '../../helpers/factories/trips';
|
|
import { makeUser } from '../../helpers/factories/users';
|
|
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();
|
|
const resolve = vi.fn();
|
|
let t: TestModule;
|
|
let orm: TestOrm;
|
|
let mcp: TourWalkImportMcp;
|
|
|
|
beforeAll(async () => {
|
|
t = await createTestModule({
|
|
db,
|
|
imports: [ToursModule],
|
|
overrides: [{ provide: GoogleMapsShortLinkProvider, useValue: { resolve } }],
|
|
});
|
|
orm = await createTestOrm(db);
|
|
mcp = t.get(TourWalkImportMcp);
|
|
});
|
|
beforeEach(() => resolve.mockReset());
|
|
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);
|
|
}
|
|
|
|
async function fixture() {
|
|
const { user } = await makeUser(orm);
|
|
const { user: outsider } = await makeUser(orm);
|
|
const trip = await makeTrip(orm, user.id, { title: 'Porto' });
|
|
return { user, outsider, trip };
|
|
}
|
|
|
|
describe('TourWalkImportMcp', () => {
|
|
it('WALK-IMPORT-MCP-001: preview_google_maps_walk reads the stops of a walking route and stores nothing', async () => {
|
|
const { user, trip } = await fixture();
|
|
const answer = read(await mcp.previewGoogleMapsWalk({ tripId: trip.id, url: PORTO_RIBEIRA }, ctx(user.id)));
|
|
expect(answer.resolved_url).toBe(PORTO_RIBEIRA);
|
|
expect(answer.stations).toHaveLength(5);
|
|
expect(answer.stations[3]).toEqual({ name: 'Mirador de Porto y Duero', lat: 41.1391452, lng: -8.609616 });
|
|
expect(resolve).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('WALK-IMPORT-MCP-002: nothing for someone outside the trip, nor for a member who may not edit places', async () => {
|
|
const { user, outsider, trip } = await fixture();
|
|
expect((await mcp.previewGoogleMapsWalk({ tripId: trip.id, url: PORTO_RIBEIRA }, ctx(outsider.id))).isError).toBe(
|
|
true,
|
|
);
|
|
const denied = vi.spyOn(t.get(McpToolGuardsService), 'hasTripPermission').mockResolvedValue(false);
|
|
const result = await mcp.previewGoogleMapsWalk({ tripId: trip.id, url: PORTO_RIBEIRA }, ctx(user.id));
|
|
expect(result.isError).toBe(true);
|
|
expect(denied).toHaveBeenCalledWith('place_edit', trip.id, user.id);
|
|
denied.mockRestore();
|
|
});
|
|
|
|
it('WALK-IMPORT-MCP-003: a route not on foot is refused with the English text REST sends', async () => {
|
|
const { user, trip } = await fixture();
|
|
const car = PORTO_RIBEIRA.replace(/!3e2$/, '!3e0');
|
|
const err = await catchDomainError(() => mcp.previewGoogleMapsWalk({ tripId: trip.id, url: car }, ctx(user.id)));
|
|
expect(err).toBeInstanceOf(DomainError);
|
|
expect(trekMcpErrorMapper(err)).toEqual({
|
|
content: [
|
|
{
|
|
type: 'text',
|
|
text: 'That route is not planned on foot. Switch Google Maps to walking and share the route again.',
|
|
},
|
|
],
|
|
isError: true,
|
|
});
|
|
});
|
|
|
|
it('WALK-IMPORT-MCP-004: preview_google_maps_walk is read-only and open world, since a short link is followed to Google', () => {
|
|
const entry = getEntry(TourWalkImportMcp as unknown as ClassRef, 'previewGoogleMapsWalk');
|
|
expect(entry?.kind).toBe('tool');
|
|
expect(entry?.kind === 'tool' && entry.options.annotations).toMatchObject({
|
|
readOnlyHint: true,
|
|
destructiveHint: false,
|
|
openWorldHint: true,
|
|
});
|
|
});
|
|
});
|