test_track_load.js (3481B)
1 import L from 'leaflet'; 2 3 import loadFromUrl from '~/lib/leaflet.control.track-list/lib/loadFromUrl'; 4 5 function calcLineLength(points) { 6 let lineLength = 0; 7 for (let i = 1; i < points.length; i++) { 8 const p1 = points[i]; 9 const p2 = points[i - 1]; 10 lineLength += L.latLng(p1.lat, p1.lng).distanceTo(L.latLng(p2.lat, p2.lng)); 11 } 12 return lineLength; 13 } 14 15 function getSegmentSummary(points) { 16 return { 17 first: points[0], 18 last: points[points.length - 1], 19 count: points.length, 20 length: Math.round(calcLineLength(points)), 21 }; 22 } 23 24 function trackSummary(track) { 25 const result = {...track}; 26 if (result.tracks) { 27 result.tracksSummary = result.tracks.map(getSegmentSummary); 28 delete result.tracks; 29 } 30 31 return result; 32 } 33 34 function reduceSegmentsPointsPrecision(segments) { 35 return segments.map((segment) => segment.map(({lat, lng}) => ({lat: lat.toFixed(7), lng: lng.toFixed(7)}))); 36 } 37 38 suite('Load tracks from services'); 39 [ 40 'strava_with_title', 41 'strava_without_title', 42 'strava_private', 43 'strava_not_exists', 44 'strava_short_url_private', 45 'strava_short_url_not_exists', 46 'strava_short_url_deleted', 47 'garmin_connect_activity_with_title', 48 'garmin_connect_activity_without_title', 49 'garmin_connect_activity_private', 50 'garmin_connect_activity_not_exists', 51 'garmin_connect_route_with_title', 52 'garmin_connect_route_private', 53 'garmin_connect_route_not_exists', 54 'osm_with_title', 55 'osm_without_title', 56 'osm_private', 57 'osm_trackable', 58 'osm_public', 59 'osm_not_exists', 60 'etomesto_with_title', 61 'etomesto_without_title', 62 'etomesto_private', 63 'etomesto_not_exists', 64 'tracedetrail_with_title', 65 'tracedetrail_private', 66 'tracedetrail_not_exists', 67 'sportstracker_with_title', 68 'sportstracker_without_title', 69 'sportstracker_private', 70 'sportstracker_not_exists', 71 'wikiloc_not_exists', 72 'wikiloc_with_waypoints', 73 'wikiloc', 74 ].forEach(function (testcase) { 75 // eslint-disable-next-line import/no-dynamic-require 76 const testData = require('./track_load_data/testcases/' + testcase + '.json'); 77 for (const track of testData.geodata) { 78 if (track.tracks) { 79 track.tracks = reduceSegmentsPointsPrecision(track.tracks); 80 } 81 } 82 for (let i = 0; i < testData.query.length; i++) { 83 let testcaseName = testcase; 84 if (testData.query.length > 1) { 85 testcaseName += `_#${i + 1}`; 86 } 87 test(testcaseName, async function () { 88 this.timeout(5000); 89 this.retries(5); 90 const result = await loadFromUrl(testData.query[i]); 91 if (result) { 92 for (const track of result) { 93 for (const [k, v] of Object.entries(track)) { 94 if (v === undefined) { 95 delete track[k]; 96 } 97 } 98 if (track.tracks) { 99 track.tracks = reduceSegmentsPointsPrecision(track.tracks); 100 } 101 if (track.points) { 102 track.points = track.points.sort((point) => point.name); 103 } 104 } 105 } 106 assert.deepEqual(testData.geodata.map(trackSummary), result.map(trackSummary)); 107 assert.deepEqual(testData.geodata, result); 108 }); 109 } 110 });