import * as _ from "lodash" ; import { CourseMaker , ExportTypes , ICourseBounds } from "../../scripts/processors/course-marker" ; import { ActivityStreamsModel } from "@elevate/shared/models" ; const activityStreamObject = require ( "../fixtures/activities/829770999/stream.json" ) ; describe ( "CourseMaker" , ( ) => { const courseMaker = new CourseMaker ( ) ; const xmlParser = new DOMParser ( ) ; let activityStream ; beforeEach ( ( ) => { activityStream = _ . cloneDeep ( activityStreamObject ) ; } ) ; it ( "should export GPX stream with consistency data" , ( done ) => { const courseName = "MyCourse" ; const gpxStream = courseMaker . create ( ExportTypes . GPX , courseName , activityStream ) ; const xmlStream = xmlParser . parseFromString ( gpxStream , "text/xml" ) ; expect ( xmlStream ) . not . toBeNull ( ) ; expect ( _ . isEmpty ( xmlStream ) ) . toBeFalsy ( ) ; expect ( xmlStream . getElementsByTagName ( "metadata" ) [ 0 ] . getElementsByTagName ( "name" ) [ 0 ] . childNodes [ 0 ] . nodeValue ) . toBe ( "Elevate" ) ; const trkNode = xmlStream . getElementsByTagName ( "trk" ) [ 0 ] ; expect ( trkNode . getElementsByTagName ( "name" ) [ 0 ] . childNodes [ 0 ] . nodeValue ) . toBe ( courseName ) ; const trackPointsLength = xmlStream . getElementsByTagName ( "trkpt" ) . length ; expect ( trackPointsLength ) . toBe ( activityStream . time . length ) ; const firstTrackPointsLength = xmlStream . getElementsByTagName ( "trkpt" ) [ 0 ] ; expect ( firstTrackPointsLength . getAttribute ( "lat" ) ) . toMatch ( /^51.509485/ ) ; expect ( firstTrackPointsLength . getAttribute ( "lon" ) ) . toMatch ( /^-0.080033/ ) ; expect ( firstTrackPointsLength . getElementsByTagName ( "ele" ) [ 0 ] . childNodes [ 0 ] . nodeValue ) . toMatch ( /^20.2/ ) ; expect ( firstTrackPointsLength . getElementsByTagName ( "time" ) [ 0 ] . childNodes [ 0 ] . nodeValue ) . toBe ( ( new Date ( 0 ) ) . toISOString ( ) ) ; const extensions = firstTrackPointsLength . getElementsByTagName ( "extensions" ) [ 0 ] ; expect ( extensions . getElementsByTagName ( "power" ) [ 0 ] . childNodes [ 0 ] . nodeValue ) . toMatch ( /^62/ ) ; const trackPointNamespaceURI = "http://www.garmin.com/xmlschemas/TrackPointExtension/v1" ; const trackPointExtension = extensions . getElementsByTagNameNS ( trackPointNamespaceURI , "TrackPointExtension" ) [ 0 ] ; expect ( trackPointExtension . getElementsByTagNameNS ( trackPointNamespaceURI , "hr" ) [ 0 ] . childNodes [ 0 ] . nodeValue ) . toMatch ( /^104/ ) ; expect ( trackPointExtension . getElementsByTagNameNS ( trackPointNamespaceURI , "cad" ) [ 0 ] . childNodes [ 0 ] . nodeValue ) . toMatch ( /^69/ ) ; done ( ) ; } ) ; it ( "should export GPX with no HRM, Cadence, altimeter & Power sensor" , ( done ) => { const courseName = "MyCourse" ; activityStream = < ActivityStreamsModel > _ . omit ( activityStream , [ "heartrate" , "cadence" , "watts" , "watts_calc" , "altitude" ] ) ; let errorCatched = null ; try { courseMaker . create ( ExportTypes . GPX , courseName , activityStream ) ; } catch ( err ) { errorCatched = err ; } expect ( errorCatched ) . toBeNull ( ) ; done ( ) ; } ) ; it ( "should export TCX stream with consistency data" , ( done ) => { const courseName = "MyCourse" ; const tcxStream = courseMaker . create ( ExportTypes . TCX , courseName , activityStream ) ; const xmlStream = xmlParser . parseFromString ( tcxStream , "text/xml" ) ; expect ( xmlStream ) . not . toBeNull ( ) ; expect ( _ . isEmpty ( xmlStream ) ) . toBeFalsy ( ) ; const CourseNode = xmlStream . getElementsByTagName ( "TrainingCenterDatabase" ) [ 0 ] . getElementsByTagName ( "Courses" ) [ 0 ] . getElementsByTagName ( "Course" ) [ 0 ] ; expect ( CourseNode . getElementsByTagName ( "Name" ) [ 0 ] . childNodes [ 0 ] . nodeValue ) . toBe ( courseName ) ; const LapNode = CourseNode . getElementsByTagName ( "Lap" ) [ 0 ] ; expect ( LapNode . getElementsByTagName ( "TotalTimeSeconds" ) [ 0 ] . childNodes [ 0 ] . nodeValue ) . toMatch ( /^2283$/ ) ; expect ( LapNode . getElementsByTagName ( "DistanceMeters" ) [ 0 ] . childNodes [ 0 ] . nodeValue ) . toMatch ( /^15723/ ) ; const trackPointsLength = CourseNode . getElementsByTagName ( "Track" ) [ 0 ] . getElementsByTagName ( "Trackpoint" ) . length ; expect ( trackPointsLength ) . toBe ( activityStream . time . length ) ; const firstTrackPoint = CourseNode . getElementsByTagName ( "Track" ) [ 0 ] . getElementsByTagName ( "Trackpoint" ) [ 0 ] ; expect ( firstTrackPoint . getElementsByTagName ( "Time" ) [ 0 ] . childNodes [ 0 ] . nodeValue ) . toBe ( ( new Date ( 0 ) ) . toISOString ( ) ) ; expect ( firstTrackPoint . getElementsByTagName ( "Position" ) [ 0 ] . getElementsByTagName ( "LatitudeDegrees" ) [ 0 ] . childNodes [ 0 ] . nodeValue ) . toMatch ( /^51.509485/ ) ; expect ( firstTrackPoint . getElementsByTagName ( "Position" ) [ 0 ] . getElementsByTagName ( "LongitudeDegrees" ) [ 0 ] . childNodes [ 0 ] . nodeValue ) . toMatch ( /^-0.080033/ ) ; expect ( firstTrackPoint . getElementsByTagName ( "AltitudeMeters" ) [ 0 ] . childNodes [ 0 ] . nodeValue ) . toMatch ( /^20.2$/ ) ; expect ( firstTrackPoint . getElementsByTagName ( "Cadence" ) [ 0 ] . childNodes [ 0 ] . nodeValue ) . toMatch ( /^69$/ ) ; expect ( firstTrackPoint . getElementsByTagName ( "HeartRateBpm" ) [ 0 ] . getElementsByTagName ( "Value" ) [ 0 ] . childNodes [ 0 ] . nodeValue ) . toMatch ( /^104$/ ) ; done ( ) ; } ) ; it ( "should export TCX with no HRM, Cadence, altimeter & Power sensor" , ( done ) => { const courseName = "MyCourse" ; activityStream = < ActivityStreamsModel > _ . omit ( activityStream , [ "heartrate" , "cadence" , "watts" , "watts_calc" , "altitude" ] ) ; let errorCatched = null ; try { courseMaker . create ( ExportTypes . TCX , courseName , activityStream ) ; } catch ( err ) { errorCatched = err ; } expect ( errorCatched ) . toBeNull ( ) ; done ( ) ; } ) ; it ( "should export GPX with bounds" , ( done ) => { const courseName = "MyCourse" ; const bounds = { start : 200 , end : 300 } ; const gpxStream = courseMaker . create ( ExportTypes . GPX , courseName , activityStream , bounds ) ; const xmlStream = xmlParser . parseFromString ( gpxStream , "text/xml" ) ; const trackPointsLength = xmlStream . getElementsByTagName ( "trkpt" ) . length ; expect ( trackPointsLength ) . toBe ( 100 ) ; done ( ) ; } ) ; it ( "should export TCX with bounds" , ( done ) => { const courseName = "MyCourse" ; const bounds = { start : 300 , end : 400 } ; const tcxStream = courseMaker . create ( ExportTypes . TCX , courseName , activityStream , bounds ) ; const xmlStream = xmlParser . parseFromString ( tcxStream , "text/xml" ) ; const trackPointsLength = xmlStream . getElementsByTagName ( "TrainingCenterDatabase" ) [ 0 ] . getElementsByTagName ( "Courses" ) [ 0 ] . getElementsByTagName ( "Course" ) [ 0 ] . getElementsByTagName ( "Track" ) [ 0 ] . getElementsByTagName ( "Trackpoint" ) . length ; expect ( trackPointsLength ) . toBe ( 100 ) ; done ( ) ; } ) ; it ( "should failed" , ( done ) => { const courseName = "MyCourse" ; expect ( ( ) => { courseMaker . create ( - 1 , courseName , activityStream ) ; } ) . toThrowError ( "Export type do not exist" ) ; done ( ) ; } ) ; } ) ;