Files
TREK/server/tests/unit/nest/journey-recordings.mcp.test.ts
Maurice 9f3722f459 feat(journey): keep recorded days from a GPX file beside Dawarich
A GPX file is a second source of a journey's recorded days (#3085). The
new route POST /api/journeys/:id/recordings/import-gpx (multipart, 15 MB,
edit access, no Dawarich needed) and the MCP tool import_journey_gpx_days
read the file's track segments through the place import's GPX reader,
place every timed point on its local day in the zone of where it was
recorded (or in one zone the caller fixes), cut the pieces at gaps, thin
them like a Dawarich day and keep one day per date with source gpx in one
transaction. Reading the file again replaces only its GPX days and leaves
an unchanged day and every Dawarich day alone; a day that changed pings
journey:recording:updated.

A GPX day is stored with source_kind tracks, so the existing contract
still reads it. DELETE :date and delete_journey_recorded_day take an
optional source, Dawarich's when left out. The shared map shows a GPX day
also without a published entry and prefers it over a Dawarich day of the
same date; the list of kept days carries both, ordered by date and source.
No migration: source and source_kind are text columns already.
2026-11-30 16:55:54 +01:00

227 lines
9.7 KiB
TypeScript

/**
* JourneyRecordingsMcp (JRN-01): the recorded days a journey keeps over MCP,
* on the same service and journey access as REST. The suite takes its subject
* from the container over a snapshot test db; only the read from Dawarich is
* replaced. A tool answers what a day covers and never its geometry.
*/
import { JourneyRecordedDays } from '../../../src/db/entities/JourneyRecordedDays.entity';
import type { McpContext } from '../../../src/nest-mcp';
import { getEntry, type ClassRef } from '../../../src/nest-mcp/metadata';
import { AddonsService } from '../../../src/nest/addons/addons.service';
import { DawarichTracksService, type RecordedDayFetch } from '../../../src/nest/integrations/dawarich-tracks.service';
import { JourneyRecordingsMcp } from '../../../src/nest/journey-recordings/journey-recordings.mcp';
import { JourneyRecordingsModule } from '../../../src/nest/journey-recordings/journey-recordings.module';
import { createSnapshotTestDb } from '../../helpers/db-mock';
import { addJourneyContributor, makeJourney } from '../../helpers/factories/journeys';
import { countRows } from '../../helpers/factories/rows';
import { setAddonEnabled } from '../../helpers/factories/settings';
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();
const fetchDay = vi.fn<(...args: unknown[]) => Promise<RecordedDayFetch>>();
let t: TestModule;
let orm: TestOrm;
let mcp: JourneyRecordingsMcp;
beforeAll(async () => {
t = await createTestModule({
db,
imports: [JourneyRecordingsModule],
overrides: [{ provide: DawarichTracksService, useValue: { fetchDay } }],
});
mcp = t.get(JourneyRecordingsMcp);
orm = await createTestOrm(db);
});
beforeEach(async () => {
resetTestDb(db);
orm.clear();
vi.restoreAllMocks();
fetchDay.mockReset();
await setAddonEnabled(orm, 'journey', true);
await setAddonEnabled(orm, 'dawarich', true);
});
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);
}
const DAY: RecordedDayFetch = {
segments: [
{
points: [
[45.434, 12.3388],
[45.4364, 12.3218],
],
mode: 'walking',
startedAt: '2026-05-14T09:00:00+02:00',
endedAt: '2026-05-14T09:40:00+02:00',
distanceMeters: 1500,
},
],
sourceKind: 'tracks',
utcOffsetMinutes: 120,
offsetSource: 'source',
sourceOffsetMinutes: 120,
window: { from: '2026-05-13T22:00:00.000Z', to: '2026-05-14T22:00:00.000Z' },
fetchedAt: '2026-05-15T07:00:00.000Z',
truncated: false,
};
async function fixture() {
const { user: owner } = await makeUser(orm);
const { user: viewer } = await makeUser(orm);
const journey = await makeJourney(orm, owner.id, { title: 'Venice' });
await addJourneyContributor(orm, journey.id, viewer.id, 'viewer');
return { owner, viewer, journey };
}
describe('JourneyRecordingsMcp', () => {
it('JRNREC-MCP-001: previews, keeps, lists and drops a day, answering without geometry', async () => {
const f = await fixture();
fetchDay.mockResolvedValue(DAY);
const preview = read(
await mcp.previewRecordedDay(
{ journeyId: f.journey.id, date: '2026-05-14', utcOffsetMinutes: 0, useSourceOffset: true },
ctx(f.owner.id),
),
);
expect(preview).toMatchObject({
journey: { id: f.journey.id, title: 'Venice' },
utcOffsetMinutes: 120,
stats: { distanceMeters: 1500, segmentCount: 1 },
stored: null,
audience: 1,
});
expect(preview.segments).toBeUndefined();
const saved = read(
await mcp.saveRecordedDay(
{ journeyId: f.journey.id, date: '2026-05-14', utcOffsetMinutes: 120, expectedHash: preview.contentHash },
ctx(f.owner.id),
),
);
expect(saved.outcome).toBe('created');
expect(saved.day).toMatchObject({ date: '2026-05-14', sourceKind: 'tracks' });
expect(saved.day.segments).toBeUndefined();
const listed = read(await mcp.listRecordedDays({ journeyId: f.journey.id }, ctx(f.viewer.id)));
expect(listed.days).toHaveLength(1);
expect(listed.days[0].segments).toBeUndefined();
const again = read(
await mcp.previewRecordedDay(
{ journeyId: f.journey.id, date: '2026-05-14', utcOffsetMinutes: 0, useSourceOffset: true },
ctx(f.owner.id),
),
);
expect(again).toMatchObject({ sameAsStored: true, stored: { date: '2026-05-14' } });
expect(again.stored.segments).toBeUndefined();
expect(read(await mcp.deleteRecordedDay({ journeyId: f.journey.id, date: '2026-05-14' }, ctx(f.owner.id)))).toEqual(
{ success: true },
);
expect(await countRows(orm, JourneyRecordedDays, {})).toBe(0);
});
it('JRNREC-MCP-002: refuses what REST refuses: a viewer keeps nothing, a stranger reads nothing', async () => {
const f = await fixture();
const { user: stranger } = await makeUser(orm);
fetchDay.mockResolvedValue(DAY);
await expect(
mcp.saveRecordedDay(
{ journeyId: f.journey.id, date: '2026-05-14', utcOffsetMinutes: 120, expectedHash: 'a'.repeat(64) },
ctx(f.viewer.id),
),
).rejects.toThrow('Not allowed');
await expect(mcp.listRecordedDays({ journeyId: f.journey.id }, ctx(stranger.id))).rejects.toThrow(
'Journey not found',
);
expect(await countRows(orm, JourneyRecordedDays, {})).toBe(0);
});
it('JRNREC-MCP-003: reading and dropping need the Journey addon, previewing and keeping need Dawarich too', async () => {
const options = (name: string) => {
const entry = getEntry(JourneyRecordingsMcp as unknown as ClassRef, name);
if (entry?.kind !== 'tool') throw new Error(`${name} is no tool`);
return entry.options;
};
const enabled = vi.spyOn(t.get(AddonsService), 'isAddonEnabled').mockResolvedValue(true);
const ALL = ['listRecordedDays', 'deleteRecordedDay', 'previewRecordedDay', 'saveRecordedDay', 'getRecordingPrep'];
const gates = () => Promise.all(ALL.map((name) => options(name).when?.(ctx(1), mcp)));
expect(await gates()).toEqual([true, true, true, true, true]);
enabled.mockImplementation(async (id: string) => id !== 'dawarich');
expect(await gates()).toEqual([true, true, false, false, false]);
enabled.mockImplementation(async (id: string) => id !== 'journey');
expect(await gates()).toEqual([false, false, false, false, false]);
expect(options('listRecordedDays').access).toEqual({ group: 'journey', mode: 'read' });
expect(options('previewRecordedDay').access).toEqual({ group: 'journey', mode: 'read' });
expect(options('saveRecordedDay').access).toEqual({ group: 'journey', mode: 'write' });
expect(options('deleteRecordedDay').access).toEqual({ group: 'journey', mode: 'write' });
expect(options('getRecordingPrep').access).toEqual({ group: 'journey', mode: 'read' });
});
it('JRNREC-MCP-004: reads how the journey prepares its days for the caller, and refuses a reader as REST does', async () => {
const f = await fixture();
expect(read(await mcp.getRecordingPrep({ journeyId: f.journey.id }, ctx(f.owner.id)))).toEqual({
enabled: false,
connection: 'not_connected',
lastAttemptAt: null,
lastSuccessAt: null,
lastError: null,
});
await expect(mcp.getRecordingPrep({ journeyId: f.journey.id }, ctx(f.viewer.id))).rejects.toThrow('Not allowed');
});
it('JRNREC-MCP-005: reads days from GPX text and drops a day of one source, as REST does (#3085)', async () => {
const f = await fixture();
const gpx =
'<gpx><trk><trkseg>' +
'<trkpt lat="45.43" lon="12.33"><time>2026-05-14T08:00:00Z</time></trkpt>' +
'<trkpt lat="45.44" lon="12.34"><time>2026-05-14T08:05:00Z</time></trkpt>' +
'<trkpt lat="45.45" lon="12.36"/>' +
'</trkseg></trk></gpx>';
const imported = read(await mcp.importGpxDays({ journeyId: f.journey.id, gpx }, ctx(f.owner.id)));
expect(imported.untimedPoints).toBe(1);
expect(imported.days).toHaveLength(1);
expect(imported.days[0]).toMatchObject({ outcome: 'created', day: { date: '2026-05-14', source: 'gpx' } });
expect(imported.days[0].day.segments).toBeUndefined();
await expect(mcp.importGpxDays({ journeyId: f.journey.id, gpx }, ctx(f.viewer.id))).rejects.toThrow('Not allowed');
// Without a source the Dawarich day is meant, and there is none.
const day = { journeyId: f.journey.id, date: '2026-05-14' };
await expect(mcp.deleteRecordedDay(day, ctx(f.owner.id))).rejects.toThrow('No recording kept for that day');
const dropped = await mcp.deleteRecordedDay({ ...day, source: 'gpx' }, ctx(f.owner.id));
expect(read(dropped)).toEqual({ success: true });
expect(await countRows(orm, JourneyRecordedDays, {})).toBe(0);
const entry = getEntry(JourneyRecordingsMcp as unknown as ClassRef, 'importGpxDays');
if (entry?.kind !== 'tool') throw new Error('importGpxDays is no tool');
expect(entry.options.access).toEqual({ group: 'journey', mode: 'write' });
const enabled = vi.spyOn(t.get(AddonsService), 'isAddonEnabled');
enabled.mockImplementation(async (id: string) => id !== 'dawarich');
expect(await entry.options.when?.(ctx(1), mcp)).toBe(true);
enabled.mockImplementation(async (id: string) => id !== 'journey');
expect(await entry.options.when?.(ctx(1), mcp)).toBe(false);
});
});