nakarte

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

test_track_load.js (3543B)


      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     'gpslib_with_title',
     52     'gpslib_without_title',
     53     'gpslib_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     'openstreetmapRu',
     72     'openstreetmapRuGpx',
     73     'openstreetmapRu_not_exists',
     74     'wikiloc_not_exists',
     75     'wikiloc_with_waypoints',
     76     'wikiloc',
     77 ].forEach(function (testcase) {
     78     // eslint-disable-next-line import/no-dynamic-require
     79     const testData = require('./track_load_data/testcases/' + testcase + '.json');
     80     for (const track of testData.geodata) {
     81         if (track.tracks) {
     82             track.tracks = reduceSegmentsPointsPrecision(track.tracks);
     83         }
     84     }
     85     for (let i = 0; i < testData.query.length; i++) {
     86         let testcaseName = testcase;
     87         if (testData.query.length > 1) {
     88             testcaseName += `_#${i + 1}`;
     89         }
     90         test(testcaseName, async function () {
     91             this.timeout(5000);
     92             this.retries(5);
     93             const result = await loadFromUrl(testData.query[i]);
     94             if (result) {
     95                 for (const track of result) {
     96                     for (const [k, v] of Object.entries(track)) {
     97                         if (v === undefined) {
     98                             delete track[k];
     99                         }
    100                     }
    101                     if (track.tracks) {
    102                         track.tracks = reduceSegmentsPointsPrecision(track.tracks);
    103                     }
    104                     if (track.points) {
    105                         track.points = track.points.sort((point) => point.name);
    106                     }
    107                 }
    108             }
    109             assert.deepEqual(testData.geodata.map(trackSummary), result.map(trackSummary));
    110             assert.deepEqual(testData.geodata, result);
    111         });
    112     }
    113 });