mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-02 17:26:24 +00:00
9f3722f459
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.
437 lines
15 KiB
TypeScript
437 lines
15 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,
|
|
readGpxTrackPoints,
|
|
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');
|
|
});
|
|
});
|
|
|
|
describe('recorded track points (#3085)', () => {
|
|
const doc = (body: string) => Buffer.from(`<?xml version="1.0"?><gpx version="1.1">${body}</gpx>`);
|
|
|
|
it('GPX-REC-001: every track segment apart, its points in file order with the times that carry a zone', () => {
|
|
const read = readGpxTrackPoints(
|
|
doc(
|
|
'<wpt lat="1" lon="1"><name>Pin</name></wpt>' +
|
|
'<rte><rtept lat="2" lon="2"/></rte>' +
|
|
'<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-14T10:00:00+02:00</time></trkpt>' +
|
|
'<trkpt lat="45.45" lon="12.35"><time>2026-05-14T08:10:00</time></trkpt>' +
|
|
'<trkpt lat="45.46" lon="12.36"/>' +
|
|
'</trkseg><trkseg>' +
|
|
'<trkpt lat="45.47" lon="12.37"><time>2026-05-14T09:00:00Z</time></trkpt>' +
|
|
'<trkpt lat="95" lon="12.38"><time>2026-05-14T09:01:00Z</time></trkpt>' +
|
|
'<trkpt lat="x" lon="12.38"><time>2026-05-14T09:01:00Z</time></trkpt>' +
|
|
'</trkseg><trkseg/></trk>',
|
|
),
|
|
);
|
|
expect(read).toEqual({
|
|
segments: [
|
|
[
|
|
{ lat: 45.43, lng: 12.33, at: Date.parse('2026-05-14T08:00:00Z') },
|
|
{ lat: 45.44, lng: 12.34, at: Date.parse('2026-05-14T08:00:00Z') },
|
|
{ lat: 45.45, lng: 12.35, at: null },
|
|
{ lat: 45.46, lng: 12.36, at: null },
|
|
],
|
|
[{ lat: 45.47, lng: 12.37, at: Date.parse('2026-05-14T09:00:00Z') }],
|
|
],
|
|
});
|
|
});
|
|
|
|
it('GPX-REC-002: a file that is not GPX, or declares a DOCTYPE, reads as nothing; one without tracks as no segments', () => {
|
|
expect(readGpxTrackPoints(Buffer.from('<kml><Placemark/></kml>'))).toBeNull();
|
|
expect(readGpxTrackPoints(Buffer.from('not xml at all'))).toBeNull();
|
|
expect(
|
|
readGpxTrackPoints(Buffer.from('<?xml version="1.0"?><!DOCTYPE gpx [<!ENTITY a "b">]><gpx><trk/></gpx>')),
|
|
).toBeNull();
|
|
expect(readGpxTrackPoints(doc('<wpt lat="1" lon="1"/>'))).toEqual({ segments: [] });
|
|
expect(new GpxProvider().readTrackPoints(doc('<trk/>'))).toEqual({ segments: [] });
|
|
});
|
|
});
|