nakarte

Source code of https://map.sikmir.ru (fork)
git clone git://git.sikmir.ru/nakarte
Log | Files | Refs | LICENSE

test_track_load.js (3465B)


      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     'garmin_connect_activity_with_title',
     45     'garmin_connect_activity_without_title',
     46     'garmin_connect_activity_private',
     47     'garmin_connect_activity_not_exists',
     48     'garmin_connect_route_with_title',
     49     'garmin_connect_route_private',
     50     'garmin_connect_route_not_exists',
     51     'osm_with_title',
     52     'osm_without_title',
     53     'osm_private',
     54     'osm_trackable',
     55     'osm_public',
     56     'osm_not_exists',
     57     'etomesto_with_title',
     58     'etomesto_without_title',
     59     'etomesto_private',
     60     'etomesto_not_exists',
     61     'tracedetrail_with_title',
     62     'tracedetrail_private',
     63     'tracedetrail_not_exists',
     64     'sportstracker_with_title',
     65     'sportstracker_without_title',
     66     'sportstracker_private',
     67     'sportstracker_not_exists',
     68     'openstreetmapRu',
     69     'openstreetmapRuGpx',
     70     'openstreetmapRu_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 });