import assert from 'node:assert/strict'; import { describe, it } from 'node:test'; import { legacyFileToGPX, LEGACY_IMPORTED_KEY } from '$lib/storage/legacy-import'; // A file as the former `gpx` library kept it. const point = (lat: number, lon: number, extra: Record = {}) => ({ attributes: { lat, lon }, ...extra, }); describe('legacyFileToGPX', () => { it('writes a GPX file with its metadata, waypoints and tracks', () => { const gpx = legacyFileToGPX({ metadata: { name: 'Morning ride', desc: 'Around the lake', author: { name: 'someone', link: { attributes: { href: 'https://gpx.studio' }, text: 'site' }, }, link: { attributes: { href: 'https://example.com' } }, time: '2024-05-01T08:00:00.000Z', }, wpt: [ point(50.1, 4.2, { ele: 100, name: 'Café', sym: 'Restaurant', link: { attributes: { href: 'https://cafe.example' }, text: 'menu' }, }), ], trk: [ { name: 'Track 1', type: 'Cycling', trkseg: [ { trkpt: [ point(50, 4, { ele: 10, time: '2024-05-01T08:00:00.000Z' }), point(50.001, 4.001, { ele: 12 }), ], }, { trkpt: [point(51, 5)] }, ], }, ], }); assert.ok(gpx.startsWith('')); assert.match( gpx, /Morning ride<\/name>Around the lake<\/desc>/); assert.match( gpx, /someone<\/name>site<\/text><\/link><\/author>/ ); assert.match( gpx, /<\/link>')); assert.ok(!gpx.includes('') && !gpx.includes(' { const gpx = legacyFileToGPX({ metadata: { name: 'Tom & Jerry <3', desc: 'a > b' }, wpt: [ point(1, 2, { name: '"quoted" & ', link: { attributes: { href: 'https://example.com/?a="1"&b=2' } }, }), ], }); assert.ok(gpx.includes('Tom & Jerry <3')); assert.ok(gpx.includes('a > b')); assert.ok(gpx.includes('"quoted" & <tagged>')); assert.ok(gpx.includes('href="https://example.com/?a="1"&b=2"')); }); it('writes all the links of an element', () => { const links = [ { attributes: { href: 'https://a.example/' }, text: 'first' }, { attributes: { href: 'https://b.example/' } }, ]; const gpx = legacyFileToGPX({ metadata: { name: 'x', link: links }, trk: [{ link: links, trkseg: [] }], }); const written = 'first'; assert.ok(gpx.includes(`x${written}`)); assert.ok(gpx.includes(`${written}`)); }); it('leaves out what is missing or empty', () => { const gpx = legacyFileToGPX({ metadata: { name: '', desc: null, time: 'not a date' }, wpt: [point(1, 2, { name: '', ele: undefined, link: { attributes: {} } })], trk: [{ trkseg: [{ trkpt: [point(1, 2, { time: 'garbage' })] }] }], }); assert.ok(!gpx.includes('') && !gpx.includes('')); assert.ok(!gpx.includes('