Files
TREK/server/tests/unit/nest/place-import/gpx.provider.test.ts
T
Maurice d0cc6fc2f4 feat(tours): link the track a tour was actually done with, and show its own values (ACT-01)
A planned tour can now be given the track it was actually done with. In the
tour's detail, "Add actual track" opens a dialog that asks for the day the
tour was done on (or none) and a recording of the trip: an import nobody
planned over, or a new one through the existing GPX import, its preview
preset to the tour's type. What the choice measures is shown before anything
is written, and only "Link" writes. The linked track keeps its own name and
source; the detail shows its distance, heights, profile and, only where the
file carried time stamps, the time it took, marked as actual values next to
the untouched plan. A track can be changed or unlinked again.

Server: a tour_activities table points a tour's execution (a day assignment,
or none) at a recording of the same trip, and the tours GPX import keeps the
first and last time of a track whose every point carries one. The values are
read from the recording's own line on every read, so a changed recording
updates them while the plan's line, measures and revision stay as they were.
Every check runs inside the write: the recording must be an import of this
trip not planned over, documenting no other tour; the day must be one of this
tour; a refusal names its reason and changes nothing. PUT and DELETE
/api/trips/:tripId/tours/:placeId/activities answer the detail, which now
carries the actual tracks; the list names them per tour and per recording.
MCP: assign_tour_track and unassign_tour_track over the same service and
checks, get_tour_plan reads the values without the line.

A recording linked as a tour's actual track is no longer counted as a tour of
its own in the trip's tour summary, and the GPX preview imports a city walk
as one instead of as a hike.
2026-10-20 15:22:55 +02:00

392 lines
13 KiB
TypeScript

/**
* GPX writer unit tests. The builder is pure, so everything here runs without a
* DB or a Nest container. The last block parses its own output back with the very
* parser the importer uses, which is the closest thing to "a real reader can open
* this" that does not need a device.
*/
import {
buildGpx,
buildTourGpx,
GpxProvider,
gpxFilename,
trackTimeSpan,
type GpxExportPlace,
type GpxExportDay,
} from '../../../../src/nest/place-import/providers/gpx.provider';
import { XMLParser, XMLValidator } from 'fast-xml-parser';
import { describe, it, expect } from 'vitest';
const place = (over: Partial<GpxExportPlace> = {}): GpxExportPlace => ({
name: 'Somewhere',
description: null,
address: null,
lat: 48.8566,
lng: 2.3522,
route_geometry: null,
category: null,
...over,
});
const day = (over: Partial<GpxExportDay> = {}): GpxExportDay => ({
dayNumber: 1,
date: '2026-05-01',
title: null,
points: [],
...over,
});
const parser = new XMLParser({
ignoreAttributes: false,
attributeNamePrefix: '@_',
isArray: (name) => ['wpt', 'trkpt', 'rtept', 'trk', 'trkseg', 'rte'].includes(name),
});
describe('buildGpx', () => {
it('writes a place without geometry as a waypoint, with its category as <sym>', () => {
const gpx = buildGpx({
tripTitle: 'Paris',
places: [place({ name: 'Louvre', description: 'Go early', address: 'Rue de Rivoli', category: 'Museum' })],
days: [],
})!;
expect(gpx).toContain('<wpt lat="48.8566" lon="2.3522">');
expect(gpx).toContain('<name>Louvre</name>');
expect(gpx).toContain('<sym>Museum</sym>');
// Description first, address after: a device usually shows only the first line.
expect(gpx).toContain('<desc>Go early, Rue de Rivoli</desc>');
});
it('writes a place carrying geometry as a track, elevation included', () => {
const gpx = buildGpx({
tripTitle: 'Hike',
places: [
place({
name: 'Ridge',
route_geometry: JSON.stringify([
[47.1, 11.2, 1830],
[47.11, 11.21, 1902],
]),
}),
],
days: [],
})!;
expect(gpx).toContain('<trkpt lat="47.1" lon="11.2">');
expect(gpx).toContain('<ele>1830</ele>');
expect(gpx).toContain('<ele>1902</ele>');
});
it('does not also drop a waypoint on the start of a track', () => {
const gpx = buildGpx({
tripTitle: 'Hike',
places: [
place({
name: 'Ridge',
route_geometry: JSON.stringify([
[47.1, 11.2],
[47.11, 11.21],
]),
}),
],
days: [],
})!;
expect(gpx).not.toContain('<wpt');
});
it('writes each planned day as a route of its stops in order', () => {
const gpx = buildGpx({
tripTitle: 'Rome',
places: [],
days: [
day({
dayNumber: 2,
date: '2026-05-02',
title: 'Ancient city',
points: [
{ name: 'Colosseum', lat: 41.89, lng: 12.49 },
{ name: 'Forum', lat: 41.892, lng: 12.485 },
],
}),
],
})!;
expect(gpx).toContain('<name>2. Ancient city</name>');
const order = [gpx.indexOf('Colosseum'), gpx.indexOf('Forum')];
expect(order[0]).toBeLessThan(order[1]);
});
it('falls back to the date, then the bare number, when a day has no title', () => {
const dated = buildGpx({
tripTitle: 'T',
places: [],
days: [
day({
dayNumber: 3,
points: [
{ name: 'A', lat: 1, lng: 1 },
{ name: 'B', lat: 2, lng: 2 },
],
}),
],
})!;
expect(dated).toContain('<name>3. 2026-05-01</name>');
const bare = buildGpx({
tripTitle: 'T',
places: [],
days: [
day({
dayNumber: 4,
date: null,
points: [
{ name: 'A', lat: 1, lng: 1 },
{ name: 'B', lat: 2, lng: 2 },
],
}),
],
})!;
expect(bare).toContain('<name>4</name>');
});
it('skips a day with a single stop — one point is not a route', () => {
const gpx = buildGpx({
tripTitle: 'T',
places: [place()],
days: [day({ points: [{ name: 'Only stop', lat: 1, lng: 1 }] })],
})!;
expect(gpx).not.toContain('<rte>');
});
it('honours each flag on its own', () => {
const input = {
tripTitle: 'T',
places: [
place({ name: 'Point' }),
place({
name: 'Line',
route_geometry: JSON.stringify([
[1, 1],
[2, 2],
]),
}),
],
days: [
day({
points: [
{ name: 'A', lat: 1, lng: 1 },
{ name: 'B', lat: 2, lng: 2 },
],
}),
],
};
const noWpt = buildGpx(input, { waypoints: false })!;
expect(noWpt).not.toContain('<wpt');
expect(noWpt).toContain('<trk>');
const noTrk = buildGpx(input, { tracks: false })!;
expect(noTrk).not.toContain('<trk>');
// With tracks off, the geometry place still has coordinates, so it lands as a waypoint.
expect(noTrk).toContain('<name>Line</name>');
const noRoutes = buildGpx(input, { dayRoutes: false })!;
expect(noRoutes).not.toContain('<rte>');
});
it('returns null when the selection produced nothing', () => {
expect(buildGpx({ tripTitle: 'Empty', places: [], days: [] })).toBeNull();
expect(buildGpx({ tripTitle: 'T', places: [place({ lat: null, lng: null })], days: [] })).toBeNull();
expect(buildGpx({ tripTitle: 'T', places: [place()], days: [] }, { waypoints: false })).toBeNull();
});
it('ignores geometry that is not a usable coordinate list', () => {
const garbage = ['not json', '{}', '[]', JSON.stringify([['a', 'b']]), JSON.stringify([[1]])];
for (const route_geometry of garbage) {
// Falls through to the waypoint branch rather than emitting an empty <trkseg>.
const gpx = buildGpx({ tripTitle: 'T', places: [place({ route_geometry })], days: [] })!;
expect(gpx).not.toContain('<trk>');
expect(gpx).toContain('<wpt');
}
});
it('produces a document the GPX parser reads back', () => {
const gpx = buildGpx({
tripTitle: 'Round trip',
places: [
place({ name: 'Waypoint', lat: 10.5, lng: -3.25 }),
place({
name: 'Track',
route_geometry: JSON.stringify([
[1.5, 2.5],
[1.6, 2.6],
]),
}),
],
days: [
day({
points: [
{ name: 'A', lat: 3, lng: 4 },
{ name: 'B', lat: 5, lng: 6 },
],
}),
],
})!;
expect(XMLValidator.validate(gpx)).toBe(true);
const parsed = parser.parse(gpx).gpx;
expect(parsed['@_version']).toBe('1.1');
expect(parsed.metadata.name).toBe('Round trip');
expect(parsed.wpt).toHaveLength(1);
// Attributes come back as strings — the importer runs them through parseFloat too.
expect(parsed.wpt[0]['@_lat']).toBe('10.5');
expect(parsed.trk[0].trkseg[0].trkpt).toHaveLength(2);
expect(parsed.rte[0].rtept).toHaveLength(2);
});
it('escapes markup in names instead of breaking the document', () => {
const gpx = buildGpx({ tripTitle: 'T', places: [place({ name: 'Bar & Grill <best>' })], days: [] })!;
expect(XMLValidator.validate(gpx)).toBe(true);
expect(parser.parse(gpx).gpx.wpt[0].name).toBe('Bar & Grill <best>');
});
});
describe('gpxFilename', () => {
it('keeps a plain title readable', () => {
expect(gpxFilename('Berlin 2026')).toBe('Berlin-2026.gpx');
});
it('drops what a filesystem or a header would choke on', () => {
expect(gpxFilename('Trip: "Rome"/2026 <draft>')).toBe('Trip-Rome-2026-draft.gpx');
});
it('falls back when a title leaves nothing behind', () => {
expect(gpxFilename('///')).toBe('trip.gpx');
expect(gpxFilename('')).toBe('trip.gpx');
});
// Header safety moved into contentDisposition() (#2165) — the filename may
// keep the title's own script instead of tripping setHeader's Latin-1 check.
it('keeps a non-ASCII title as typed', () => {
expect(gpxFilename('沖縄 4泊5日')).toBe('沖縄-4泊5日.gpx');
// No NFKD any more: the composed é stays composed instead of decaying into
// 'e' + a combining mark that only looked stripped.
expect(gpxFilename('Café')).toBe('Café.gpx');
});
it('trims on codepoint boundaries, never through the middle of an emoji', () => {
expect(gpxFilename('\u{1F5FE}'.repeat(70))).toBe('\u{1F5FE}'.repeat(60) + '.gpx');
});
});
describe('buildTourGpx (PR1-06A)', () => {
const tour = (route_geometry: string | null) => ({
name: 'Grat <Ost>',
description: 'Früh los',
website: 'https://example.test/route?a=1&b=2',
activity: 'hiking',
route_geometry,
stops: [{ name: 'Hütte', lat: 47.5, lng: 11.1 }],
});
const parser = new XMLParser({ ignoreAttributes: false, attributeNamePrefix: '@_' });
it('writes the line as one track with the heights it has and none it lacks', () => {
const gpx =
buildTourGpx(
tour(
JSON.stringify([
[47, 11, 600.4],
[47.1, 11.1],
[47.2, 11.2, 'x'],
[47.3, 11.3, 650],
]),
),
) ?? '';
expect(XMLValidator.validate(gpx)).toBe(true);
const doc = parser.parse(gpx).gpx;
expect(doc.metadata).toMatchObject({
name: 'Grat <Ost>',
desc: 'Früh los',
link: { '@_href': 'https://example.test/route?a=1&b=2' },
});
expect(doc.trk).toMatchObject({ name: 'Grat <Ost>', type: 'hiking' });
const points = doc.trk.trkseg.trkpt;
expect(points).toHaveLength(4);
expect(points.map((p: { ele?: number }) => p.ele ?? null)).toEqual([600.4, null, null, 650]);
expect(doc.wpt).toMatchObject({ '@_lat': '47.5', '@_lon': '11.1', name: 'Hütte' });
});
it('writes no description or link a tour has not got', () => {
const gpx =
buildTourGpx({
...tour(
JSON.stringify([
[47, 11],
[47.1, 11.1],
]),
),
description: ' ',
website: null,
stops: [],
}) ?? '';
const doc = parser.parse(gpx).gpx;
expect(doc.metadata).toEqual({ name: 'Grat <Ost>' });
expect(doc.trk.desc).toBeUndefined();
expect(doc.trk.link).toBeUndefined();
expect(doc.wpt).toBeUndefined();
});
it('refuses a line of fewer than two points, or none at all', () => {
expect(buildTourGpx(tour(JSON.stringify([[47, 11, 600]])))).toBeNull();
expect(buildTourGpx(tour('not json'))).toBeNull();
expect(buildTourGpx(tour(null))).toBeNull();
});
it('names a tour whose name folds away to nothing "tour"', () => {
expect(gpxFilename('///', 'tour')).toBe('tour.gpx');
expect(gpxFilename('Grat <Ost>', 'tour')).toBe('Grat-Ost.gpx');
});
});
describe('track times (ACT-01)', () => {
it('GPX-TIME-001: the first and last time of a track whose every point carries one, in order', () => {
expect(trackTimeSpan(['2026-07-02T06:10:00Z', '2026-07-02T06:10:00Z', ' 2026-07-02T09:00:00+02:00 '])).toEqual({
startedAt: '2026-07-02T06:10:00.000Z',
endedAt: '2026-07-02T07:00:00.000Z',
});
});
it('GPX-TIME-002: no times at all when one is missing, has no zone, runs backwards or none passes', () => {
expect(trackTimeSpan(['2026-07-02T06:10:00Z', undefined])).toBeUndefined();
expect(trackTimeSpan(['2026-07-02T06:10:00', '2026-07-02T07:10:00'])).toBeUndefined();
expect(trackTimeSpan(['2026-07-02T07:10:00Z', '2026-07-02T06:10:00Z'])).toBeUndefined();
expect(trackTimeSpan(['2026-07-02T06:10:00Z', '2026-07-02T06:10:00Z'])).toBeUndefined();
expect(trackTimeSpan(['not a time Z'])).toBeUndefined();
expect(trackTimeSpan([])).toBeUndefined();
});
it('GPX-TIME-003: the reader hands a track its times and never a route', () => {
const xml = `<?xml version="1.0"?>
<gpx version="1.1" creator="unit">
<rte><name>Plan</name><rtept lat="1" lon="2"><time>2026-07-02T06:00:00Z</time></rtept><rtept lat="1.1" lon="2"><time>2026-07-02T07:00:00Z</time></rtept></rte>
<trk><name>Walked</name><trkseg>
<trkpt lat="1" lon="2"><time>2026-07-02T06:00:00Z</time></trkpt>
</trkseg><trkseg>
<trkpt lat="1.1" lon="2"><time>2026-07-02T08:30:00Z</time></trkpt>
</trkseg></trk>
<trk><name>Drawn</name><trkseg><trkpt lat="3" lon="4"/><trkpt lat="3.1" lon="4"/></trkseg></trk>
</gpx>`;
const rows = new GpxProvider().read(Buffer.from(xml), { importWaypoints: false });
const byName = new Map(rows.map((row) => [row.name, row]));
expect(byName.get('Plan')?.trackTimes).toBeUndefined();
expect(byName.get('Walked')?.trackTimes).toEqual({
startedAt: '2026-07-02T06:00:00.000Z',
endedAt: '2026-07-02T08:30:00.000Z',
});
expect(byName.get('Drawn')).not.toHaveProperty('trackTimes');
});
});