Files
TREK/server/tests/unit/nest/journey-recordings.controller.test.ts
T
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

129 lines
6.1 KiB
TypeScript

/**
* JourneyRecordingsController (JRN-01): the routes of the recorded days a
* journey keeps, their guards, the date check it makes before the service is
* asked, and what it hands the service and answers.
*/
import 'reflect-metadata';
import { AddonGuard } from '../../../src/nest/addons/addon.guard';
import { REQUIRE_ADDON } from '../../../src/nest/addons/require-addon.decorator';
import { JwtAuthGuard } from '../../../src/nest/auth-core/jwt-auth.guard';
import type { JourneyGpxImportService } from '../../../src/nest/journey-recordings/journey-gpx-import.service';
import { JourneyRecordingsController } from '../../../src/nest/journey-recordings/journey-recordings.controller';
import type { JourneyRecordingsService } from '../../../src/nest/journey-recordings/journey-recordings.service';
import type { User } from '../../../src/types';
import { HttpException, RequestMethod } from '@nestjs/common';
import { GUARDS_METADATA, HTTP_CODE_METADATA, METHOD_METADATA, PATH_METADATA } from '@nestjs/common/constants';
import { describe, expect, it, vi } from 'vitest';
const user = { id: 4 } as User;
const HASH = 'f'.repeat(64);
function controllerWith() {
const service = {
list: vi.fn(async () => []),
preview: vi.fn(async () => ({ date: '2026-05-14' })),
save: vi.fn(async () => ({ outcome: 'created' })),
remove: vi.fn(async () => undefined),
prepStatus: vi.fn(async () => ({ enabled: true })),
};
const gpx = { importGpx: vi.fn(async () => ({ days: [], untimedPoints: 0 })) };
return {
service,
gpx,
controller: new JourneyRecordingsController(
service as unknown as JourneyRecordingsService,
gpx as unknown as JourneyGpxImportService,
),
};
}
describe('JourneyRecordingsController', () => {
it('JRNREC-CTL-001: sits behind the Journey addon before auth, with the preview and the removal answering 200', () => {
expect(Reflect.getMetadata(PATH_METADATA, JourneyRecordingsController)).toBe('api/journeys/:id/recordings');
expect(Reflect.getMetadata(GUARDS_METADATA, JourneyRecordingsController)).toEqual([AddonGuard, JwtAuthGuard]);
expect(Reflect.getMetadata(REQUIRE_ADDON, JourneyRecordingsController)).toEqual({
addonId: 'journey',
label: 'Journey',
});
const routes = [
['list', RequestMethod.GET, '/', undefined],
['prep', RequestMethod.GET, 'prep', undefined],
['preview', RequestMethod.POST, 'preview', 200],
['importGpx', RequestMethod.POST, 'import-gpx', 200],
['save', RequestMethod.PUT, ':date', undefined],
['remove', RequestMethod.DELETE, ':date', 200],
] as const;
for (const [handler, method, path, code] of routes) {
const fn = JourneyRecordingsController.prototype[handler];
expect(Reflect.getMetadata(METHOD_METADATA, fn)).toBe(method);
expect(Reflect.getMetadata(PATH_METADATA, fn)).toBe(path);
expect(Reflect.getMetadata(HTTP_CODE_METADATA, fn)).toBe(code);
}
});
it('JRNREC-CTL-002: hands the caller, the journey, the day, the body and the socket id to the service', async () => {
const { service, controller } = controllerWith();
expect(await controller.list(user, 9)).toEqual({ days: [] });
expect(service.list).toHaveBeenCalledExactlyOnceWith(4, 9);
const preview = { date: '2026-05-14', utcOffsetMinutes: 120, useSourceOffset: true };
expect(await controller.preview(user, 9, preview)).toEqual({ date: '2026-05-14' });
expect(service.preview).toHaveBeenCalledExactlyOnceWith(4, 9, preview);
const save = { utcOffsetMinutes: 120, expectedHash: HASH, replace: true };
expect(await controller.save(user, 9, '2026-05-14', save, 'sock-1')).toEqual({ outcome: 'created' });
expect(service.save).toHaveBeenCalledExactlyOnceWith(4, 9, '2026-05-14', save, 'sock-1');
expect(await controller.remove(user, 9, '2026-05-14', {}, 'sock-1')).toEqual({ success: true });
expect(service.remove).toHaveBeenCalledExactlyOnceWith(4, 9, '2026-05-14', 'sock-1', undefined);
await controller.remove(user, 9, '2026-05-14', { source: 'gpx' });
expect(service.remove).toHaveBeenLastCalledWith(4, 9, '2026-05-14', undefined, 'gpx');
});
it('JRNREC-CTL-003: a day that is no calendar day is a 400 before the service is asked', async () => {
const { service, controller } = controllerWith();
const refusals = await Promise.all(
['2026-02-30', '14.05.2026', 'today'].map((date) =>
controller.remove(user, 9, date, {}).catch((err: unknown) => err),
),
);
for (const err of refusals) {
expect(err).toBeInstanceOf(HttpException);
expect((err as HttpException).getStatus()).toBe(400);
expect((err as HttpException).getResponse()).toEqual({ error: 'Invalid date' });
}
await expect(controller.save(user, 9, 'nope', { utcOffsetMinutes: 0, expectedHash: HASH })).rejects.toBeInstanceOf(
HttpException,
);
expect(service.remove).not.toHaveBeenCalled();
expect(service.save).not.toHaveBeenCalled();
});
it('JRNREC-CTL-004: asks the service how the journey is prepared for the caller (JRN-03)', async () => {
const { service, controller } = controllerWith();
expect(await controller.prep(user, 9)).toEqual({ enabled: true });
expect(service.prepStatus).toHaveBeenCalledExactlyOnceWith(4, 9);
});
it('JRNREC-CTL-005: hands a GPX upload with its fields to the import, and refuses a request without a file (#3085)', async () => {
const { gpx, controller } = controllerWith();
const file = { buffer: Buffer.from('<gpx/>') } as Express.Multer.File;
expect(await controller.importGpx(user, 9, file, { date: '2026-05-14' }, 'sock-1')).toEqual({
days: [],
untimedPoints: 0,
});
expect(gpx.importGpx).toHaveBeenCalledExactlyOnceWith(4, 9, file.buffer, { date: '2026-05-14' }, 'sock-1');
const missing = await controller.importGpx(user, 9, undefined, {}).catch((err: unknown) => err);
expect(missing).toBeInstanceOf(HttpException);
expect((missing as HttpException).getStatus()).toBe(400);
expect((missing as HttpException).getResponse()).toEqual({ error: 'No file uploaded', code: 'no-file' });
expect(gpx.importGpx).toHaveBeenCalledTimes(1);
});
});