import { powerTimeData } from "./power_data_1480020375" ; import * as _ from "lodash" ; import { SplitCalculator } from "../../scripts/processors/split-calculator" ; describe ( "SplitCalculator" , ( ) => { let _POWER_TIME_DATA_ ; beforeEach ( ( done ) => { _POWER_TIME_DATA_ = _ . cloneDeep ( powerTimeData ) ; done ( ) ; } ) ; it ( "should normalize data over scale" , ( done ) => { const scale = [ 0 , 1 , 3 , 6 ] ; const data = [ 0 , 10 , 40 , 50 ] ; const expectedNormalizedScale = [ 0 , 1 , 2 , 3 , 4 , 5 , 6 ] ; const expectedInterpolatedData = [ 0 , 10 , 25 , 40 , 43.3333 , 46.6666 , 50 ] ; const splitCalculator = new SplitCalculator ( scale , data ) ; expect ( splitCalculator . scale ) . toEqual ( expectedNormalizedScale ) ; expect ( splitCalculator . data ) . toEqual ( expectedInterpolatedData ) ; expect ( splitCalculator . scale . length ) . toEqual ( splitCalculator . data . length ) ; done ( ) ; } ) ; it ( "should NOT normalize scale having a gaps over 5000" , ( done ) => { const maxScaleGapThreshold = 60 * 60 * 8 ; const scale = [ 0 , 1 , 3 , 6 , 50 + maxScaleGapThreshold ] ; const data = [ 0 , 10 , 40 , 60 , 90 ] ; const scaleRange = 3 ; const call = ( ) => { const splitCalculator = new SplitCalculator ( scale , data , maxScaleGapThreshold ) ; splitCalculator . getBestSplit ( scaleRange , true ) ; } ; expect ( call ) . toThrow ( new Error ( "Scale has a too importants gap. Cannot normalize scale" ) ) ; done ( ) ; } ) ; it ( "should NOT normalize scale having gaps < 0" , ( done ) => { const scale = [ 0 , 1 , 3 , 6 , 5 ] ; const data = [ 0 , 10 , 40 , 60 , 90 ] ; const scaleRange = 3 ; const call = ( ) => { const splitCalculator = new SplitCalculator ( scale , data ) ; splitCalculator . getBestSplit ( scaleRange , true ) ; } ; expect ( call ) . toThrow ( new Error ( "Scale should have gaps >= 0" ) ) ; done ( ) ; } ) ; it ( "should get best split with scale of 3" , ( done ) => { const scale = [ 0 , 1 , 6 , 8 ] ; const data = [ 0 , 10 , 40 , 60 ] ; const scaleRange = 3 ; const splitCalculator = new SplitCalculator ( scale , data ) ; const bestSplit = splitCalculator . getBestSplit ( scaleRange , true ) ; expect ( bestSplit ) . toEqual ( 50 ) ; done ( ) ; } ) ; it ( "should NOT get best split with scale range higher than scale" , ( done ) => { const scale = [ 0 , 1 , 3 , 6 ] ; const data = [ 0 , 10 , 40 , 60 ] ; const scaleRange = 100 ; const splitCalculator = new SplitCalculator ( scale , data ) ; const call = ( ) => { splitCalculator . getBestSplit ( scaleRange , true ) ; } ; expect ( call ) . toThrow ( new Error ( "Requested scaleRange of " + scaleRange + " is greater than scale range length of 7." ) ) ; done ( ) ; } ) ; it ( "should get rider FTP of activity 1480020375" , ( done ) => { const expectedFTP = 226 ; const timeScale = _POWER_TIME_DATA_ . time ; const wattsData = _POWER_TIME_DATA_ . watts ; const scaleRange = 20 * 60 ; const splitCalculator = new SplitCalculator ( timeScale , wattsData ) ; const riderFTP = splitCalculator . getBestSplit ( scaleRange ) ; expect ( Math . floor ( riderFTP ) ) . toEqual ( expectedFTP ) ; done ( ) ; } ) ; it ( "should get rider power curve of activity 1480020375" , ( done ) => { const expectedPowerOneMinute = 370 ; const expectedFTP = 226 ; const timeScale = _POWER_TIME_DATA_ . time ; const wattsData = _POWER_TIME_DATA_ . watts ; const scaleRanges = [ 60 , 20 * 60 ] ; const splitCalculator = new SplitCalculator ( timeScale , wattsData ) ; const results = splitCalculator . getBestSplitRanges ( scaleRanges ) ; expect ( Math . floor ( results [ 0 ] . range ) ) . toEqual ( 60 ) ; expect ( Math . floor ( results [ 0 ] . result ) ) . toEqual ( expectedPowerOneMinute ) ; expect ( Math . floor ( results [ 1 ] . range ) ) . toEqual ( 20 * 60 ) ; expect ( Math . floor ( results [ 1 ] . result ) ) . toEqual ( expectedFTP ) ; done ( ) ; } ) ; } ) ;