import * as _ from "lodash" ; import * as Q from "q" ; import * as $ from "jquery" ; import { editActivityFromArray , removeActivityFromArray } from "../tools/specs-tools" ; import { ActivitiesChangesModel , AnalysisDataModel , AthleteModel , SyncedActivityModel , SyncResultModel , UserSettingsModel } from "@elevate/shared/models" ; import { AppResourcesModel } from "../../scripts/models/app-resources.model" ; import { ActivitiesSynchronize } from "../../scripts/processors/activities-synchronize" ; import { StravaActivityModel } from "../../scripts/models/sync/strava-activity.model" ; import { StreamActivityModel } from "../../scripts/models/sync/stream-activity.model" ; import { MultipleActivityProcessor } from "../../scripts/processors/multiple-activity-processor" ; import { SyncNotifyModel } from "../../scripts/models/sync/sync-notify.model" ; import { AthleteSnapshotResolver } from "@elevate/shared/resolvers" ; import { userSettingsData } from "@elevate/shared/data" ; describe ( "ActivitiesSynchronize" , ( ) => { let athleteModelResolver ; let userSettingsMock ; let appResourcesMock ; let activitiesSynchronize ; let rawPagesOfActivities ; let CHROME_STORAGE_STUB ; param id const addStravaActivity = ( activityId ) => { if ( _ . find ( CHROME_STORAGE_STUB . syncedActivities , { id : activityId } ) ) { CHROME_STORAGE_STUB . syncedActivities = removeActivityFromArray ( activityId , CHROME_STORAGE_STUB . syncedActivities ) ; return true ; } else { return false ; } } ; param activityId param rawPageOfActivities param newName param newType returns { boolean } const editStravaActivity = ( activityId , rawPageOfActivities , newName , newType ) => { const found = _ . find ( rawPageOfActivities . models , { id : activityId } ) ; if ( found ) { rawPageOfActivities . models = editActivityFromArray ( activityId , rawPageOfActivities . models , newName , newType ) ; return true ; } else { return false ; } } ; param id param atPage const removeStravaActivity = ( activityId , rawPageOfActivities ) => { const found = _ . find ( rawPageOfActivities . models , { id : activityId } ) ; if ( found ) { rawPageOfActivities . models = removeActivityFromArray ( activityId , rawPageOfActivities . models ) ; return true ; } else { return false ; } } ; beforeEach ( ( ) => { jasmine . DEFAULT_TIMEOUT_INTERVAL = 60000 ; CHROME_STORAGE_STUB = { } ; userSettingsMock = _ . cloneDeep ( userSettingsData ) ; appResourcesMock = _ . cloneDeep ( require ( "../fixtures/app-resources/app-resources.json" ) ) ; rawPagesOfActivities = [ _ . cloneDeep ( require ( "../fixtures/sync/rawPage0120161213.json" ) ) , _ . cloneDeep ( require ( "../fixtures/sync/rawPage0220161213.json" ) ) , _ . cloneDeep ( require ( "../fixtures/sync/rawPage0320161213.json" ) ) , _ . cloneDeep ( require ( "../fixtures/sync/rawPage0420161213.json" ) ) , _ . cloneDeep ( require ( "../fixtures/sync/rawPage0520161213.json" ) ) , _ . cloneDeep ( require ( "../fixtures/sync/rawPage0620161213.json" ) ) , _ . cloneDeep ( require ( "../fixtures/sync/rawPage0720161213.json" ) ) , ] ; athleteModelResolver = new AthleteSnapshotResolver ( AthleteModel . DEFAULT_MODEL ) ; activitiesSynchronize = new ActivitiesSynchronize ( appResourcesMock , userSettingsMock , athleteModelResolver ) ; /**
         * Stubing http calls to strava training pages
         */ spyOn ( activitiesSynchronize , "httpPageGet" ) . and . callFake ( ( perPage , page ) => { const defer = $ . Deferred ( ) ; const rawPagesOfActivity = rawPagesOfActivities [ page - 1 ] ; if ( rawPagesOfActivity ) { let total = 0 ; _ . forEach ( rawPagesOfActivities , rawPage => { total += rawPage . models . length ; } ) ; rawPagesOfActivity . total = total ; defer . resolve ( rawPagesOfActivity , "success" ) ; } else { defer . resolve ( { models : [ ] } , "success" ) ; } return defer . promise ( ) ; } ) ; /**
         * Stubing activity stream promised, reduce @ 50 samples
         */ const stream = _ . cloneDeep ( require ( "../fixtures/activities/723224273/stream.json" ) ) ; stream . watts = stream . watts_calc ; spyOn ( activitiesSynchronize , "fetchStreamByActivityId" ) . and . callFake ( ( activityId ) => { const defer = Q . defer ( ) ; const data = { } ; _ . forEach ( _ . keys ( stream ) , ( key ) => { data [ key ] = stream [ key ] . slice ( 0 , 50 ) ; } ) ; data . activityId = activityId ; defer . notify ( activityId ) ; defer . resolve ( data ) ; return defer . promise ; } ) ; spyOn ( activitiesSynchronize , "getSleepTime" ) . and . returnValue ( 10 ) ; /**
         * Stub MultipleActivityProcessor:compute. Create fake analysis results
         */ spyOn ( activitiesSynchronize . multipleActivityProcessor , "compute" ) . and . callFake ( ( activitiesWithStream ) => { const defer = Q . defer ( ) ; console . log ( "Spy activitiesSynchronize.multipleActivityProcessor:compute called" ) ; const activitiesComputed = [ ] ; const fakeAnalysisData = { moveRatio : null , runningPerformanceIndex : 25.0 , speedData : null , paceData : null , powerData : null , heartRateData : null , cadenceData : null , gradeData : null , elevationData : null } ; _ . forEach ( activitiesWithStream , ( awStream ) => { const activityComputed = < SyncedActivityModel > _ . pick ( awStream , MultipleActivityProcessor . outputFields ) ; activityComputed . extendedStats = fakeAnalysisData ; activitiesComputed . push ( activityComputed ) ; } ) ; defer . resolve ( activitiesComputed ) ; return defer . promise ; } ) ; /**
         * Stub:
         * - saveSyncedActivitiesToLocal
         * - getSyncedActivitiesFromLocal
         * - saveLastSyncDateToLocal
         * - getLastSyncDateFromLocal
         * - clearSyncCache
         */ spyOn ( activitiesSynchronize , "saveSyncedActivitiesToLocal" ) . and . callFake ( ( syncedActivities ) => { const defer = Q . defer ( ) ; CHROME_STORAGE_STUB . syncedActivities = syncedActivities ; defer . resolve ( ) ; return defer . promise ; } ) ; spyOn ( activitiesSynchronize , "getSyncedActivitiesFromLocal" ) . and . callFake ( ( ) => { const defer = Q . defer ( ) ; defer . resolve ( CHROME_STORAGE_STUB . syncedActivities ) ; return defer . promise ; } ) ; spyOn ( activitiesSynchronize , "saveLastSyncDateToLocal" ) . and . callFake ( ( timestamp ) => { const defer = Q . defer ( ) ; CHROME_STORAGE_STUB . lastSyncDateTime = timestamp ; defer . resolve ( ) ; return defer . promise ; } ) ; spyOn ( activitiesSynchronize , "getLastSyncDateFromLocal" ) . and . callFake ( ( ) => { const defer = Q . defer ( ) ; defer . resolve ( ( CHROME_STORAGE_STUB . lastSyncDateTime ) ? CHROME_STORAGE_STUB . lastSyncDateTime : null ) ; return defer . promise ; } ) ; spyOn ( activitiesSynchronize , "clearSyncCache" ) . and . callFake ( ( ) => { const defer = Q . defer ( ) ; CHROME_STORAGE_STUB = { } ; defer . resolve ( ) ; return defer . promise ; } ) ; spyOn ( activitiesSynchronize , "getAllSavedLocal" ) . and . callFake ( ( ) => { const defer = Q . defer ( ) ; defer . resolve ( CHROME_STORAGE_STUB ) ; return defer . promise ; } ) ; } ) ; it ( "should ensure ActivitiesSynchronize:getFirstPageRemoteActivities()" , ( done ) => { const expectedCount = 140 ; const expectedFirstPageModelCount = 20 ; const remoteActivitiesCount = activitiesSynchronize . getFirstPageRemoteActivities ( ) ; remoteActivitiesCount . then ( ( result ) => { expect ( result . firstPageModels . length ) . toEqual ( expectedFirstPageModelCount ) ; expect ( result . activitiesCountAllPages ) . toEqual ( expectedCount ) ; done ( ) ; } , ( err ) => { expect ( err ) . toBeNull ( ) ; done ( ) ; } , ( progress ) => { console . log ( progress ) ; } ) ; } ) ; it ( "should ensure ActivitiesSynchronize:fetchRawActivitiesRecursive()" , ( done ) => { activitiesSynchronize . fetchRawActivitiesRecursive ( null ) . then ( ( rawStravaActivities ) => { expect ( activitiesSynchronize . httpPageGet ) . toHaveBeenCalled ( ) ; expect ( rawStravaActivities ) . not . toBeNull ( ) ; expect ( rawStravaActivities . length ) . toEqual ( 20 * 7 ) ; const jeannieRide = _ . find ( rawStravaActivities , { id : 718908064 } ) ; expect ( jeannieRide . name ) . toEqual ( "Pédalage avec Madame Jeannie Longo" ) ; expect ( jeannieRide . start_time ) . toEqual ( "2016-09-20T13:44:54+0000" ) ; expect ( jeannieRide . moving_time_raw ) . toEqual ( 8557 ) ; const relaxRide = _ . find ( rawStravaActivities , { id : 642780978 } ) ; expect ( relaxRide . name ) . toEqual ( "Relax" ) ; expect ( relaxRide . moving_time_raw ) . toEqual ( 4888 ) ; const burnedRide = _ . find ( rawStravaActivities , { id : 377239233 } ) ; expect ( burnedRide . name ) . toEqual ( "Cramé !!" ) ; expect ( burnedRide . type ) . toEqual ( "Ride" ) ; expect ( burnedRide . moving_time_raw ) . toEqual ( 4315 ) ; const fakeRide = _ . find ( rawStravaActivities , { id : 9999999999 } ) ; expect ( fakeRide ) . toBeUndefined ( ) ; return activitiesSynchronize . fetchRawActivitiesRecursive ( null , 1 , 3 ) ; } ) . then ( ( rawStravaActivities ) => { expect ( rawStravaActivities . length ) . toEqual ( 20 * 3 ) ; return activitiesSynchronize . fetchRawActivitiesRecursive ( null , 6 , 3 ) ; } ) . then ( ( rawStravaActivities ) => { expect ( rawStravaActivities . length ) . toEqual ( 40 ) ; return activitiesSynchronize . fetchRawActivitiesRecursive ( null , 6 , 1 ) ; } ) . then ( ( rawStravaActivities ) => { expect ( rawStravaActivities . length ) . toEqual ( 20 ) ; done ( ) ; } , ( err ) => { expect ( err ) . toBeNull ( ) ; done ( ) ; } , ( progress ) => { console . log ( progress ) ; } ) ; } ) ; it ( "should ensure ActivitiesSynchronize:fetchWithStream()" , ( done ) => { activitiesSynchronize . fetchWithStream ( null , null , null ) . then ( ( activitiesWithStream ) => { expect ( activitiesSynchronize . fetchStreamByActivityId ) . toHaveBeenCalled ( ) ; expect ( activitiesWithStream ) . not . toBeNull ( ) ; expect ( activitiesWithStream . length ) . toEqual ( 140 ) ; const jeannieRide = _ . find ( activitiesWithStream , { id : 718908064 } ) ; expect ( jeannieRide . name ) . toEqual ( "Pédalage avec Madame Jeannie Longo" ) ; expect ( jeannieRide . start_time ) . toEqual ( "2016-09-20T13:44:54+0000" ) ; expect ( jeannieRide . moving_time_raw ) . toEqual ( 8557 ) ; expect ( jeannieRide . stream ) . not . toBeNull ( ) ; const fakeRide = _ . find ( activitiesWithStream , { id : 9999999999 } ) ; expect ( fakeRide ) . toBeUndefined ( ) ; return activitiesSynchronize . fetchWithStream ( null , 4 , 3 ) ; } ) . then ( ( activitiesWithStream ) => { expect ( activitiesWithStream ) . not . toBeNull ( ) ; expect ( activitiesWithStream . length ) . toEqual ( 60 ) ; const jeannieRide = _ . find ( activitiesWithStream , { id : 718908064 } ) ; expect ( jeannieRide ) . toBeUndefined ( ) ; done ( ) ; } , ( err ) => { expect ( err ) . toBeNull ( ) ; done ( ) ; } , ( progress ) => { console . log ( progress ) ; } ) ; } ) ; it ( "should ensure ActivitiesSynchronize:fetchAndComputeGroupOfPages()" , ( done ) => { activitiesSynchronize . fetchAndComputeGroupOfPages ( null , null , null ) . then ( ( activitiesComputed ) => { expect ( activitiesSynchronize . multipleActivityProcessor . compute ) . toHaveBeenCalled ( ) ; expect ( activitiesComputed ) . not . toBeNull ( ) ; expect ( activitiesComputed . length ) . toEqual ( 140 ) ; expect ( _ . first ( activitiesComputed ) . extendedStats ) . toBeDefined ( ) ; expect ( _ . first ( activitiesComputed ) . extendedStats . heartRateData ) . toBeNull ( ) ; expect ( _ . first ( activitiesComputed ) . extendedStats . speedData ) . toBeNull ( ) ; return activitiesSynchronize . fetchAndComputeGroupOfPages ( null , 7 , 3 ) ; } ) . then ( ( activitiesComputed ) => { expect ( activitiesComputed . length ) . toEqual ( 20 ) ; const ride = _ . find ( activitiesComputed , { id : 406217194 } ) ; expect ( ride . extendedStats ) . toBeDefined ( ) ; expect ( ride . extendedStats . heartRateData ) . toBeNull ( ) ; expect ( ride . extendedStats . speedData ) . toBeNull ( ) ; expect ( ride . moving_time_raw ) . toEqual ( 5901 ) ; const jeannieRide = _ . find ( activitiesComputed , { id : 718908064 } ) ; expect ( jeannieRide ) . toBeUndefined ( ) ; done ( ) ; } ) ; } ) ; it ( "should ensure ActivitiesSynchronize:computeActivitiesByGroupsOfPages() all pages" , ( done ) => { expect ( activitiesSynchronize ) . not . toBeNull ( ) ; expect ( activitiesSynchronize ) . not . toBeUndefined ( ) ; expect ( activitiesSynchronize . computeActivitiesByGroupsOfPages ) . not . toBeUndefined ( ) ; activitiesSynchronize . computeActivitiesByGroupsOfPages ( null ) . then ( ( mergedSyncedActivities ) => { expect ( activitiesSynchronize . getSyncedActivitiesFromLocal ) . toHaveBeenCalled ( ) ; expect ( activitiesSynchronize . saveSyncedActivitiesToLocal ) . toHaveBeenCalled ( ) ; expect ( mergedSyncedActivities ) . not . toBeNull ( ) ; expect ( mergedSyncedActivities . length ) . toEqual ( 140 ) ; const jeannieRide = _ . find ( mergedSyncedActivities , { id : 718908064 } ) ; expect ( jeannieRide . name ) . toEqual ( "Pédalage avec Madame Jeannie Longo" ) ; expect ( jeannieRide . start_time ) . toEqual ( "2016-09-20T13:44:54+0000" ) ; expect ( jeannieRide . moving_time_raw ) . toEqual ( 8557 ) ; expect ( jeannieRide . extendedStats ) . not . toBeNull ( ) ; expect ( jeannieRide . extendedStats . heartRateData ) . toBeNull ( ) ; expect ( jeannieRide . extendedStats . speedData ) . toBeNull ( ) ; const fakeRide = _ . find ( mergedSyncedActivities , { id : 9999999999 } ) ; expect ( fakeRide ) . toBeUndefined ( ) ; expect ( activitiesSynchronize . hasBeenSyncedActivities ) . not . toBeNull ( ) ; done ( ) ; } ) ; } ) ; it ( "should sync() when no existing stored synced activities" , ( done ) => { expect ( activitiesSynchronize . hasBeenSyncedActivities ) . toBeNull ( ) ; activitiesSynchronize . getLastSyncDateFromLocal ( ) . then ( ( savedLastSyncDateTime ) => { expect ( _ . isNull ( savedLastSyncDateTime ) || _ . isUndefined ( savedLastSyncDateTime ) ) . toBeTruthy ( ) ; return activitiesSynchronize . getSyncedActivitiesFromLocal ( ) ; } ) . then ( ( syncedActivitiesStored ) => { expect ( _ . isNull ( syncedActivitiesStored ) || _ . isUndefined ( syncedActivitiesStored ) ) . toBeTruthy ( ) ; return activitiesSynchronize . sync ( ) ; } ) . then ( ( syncResult ) => { expect ( activitiesSynchronize . getSyncedActivitiesFromLocal ) . toHaveBeenCalled ( ) ; expect ( activitiesSynchronize . saveSyncedActivitiesToLocal ) . toHaveBeenCalled ( ) ; expect ( activitiesSynchronize . getLastSyncDateFromLocal ) . toHaveBeenCalledTimes ( 2 ) ; expect ( activitiesSynchronize . saveLastSyncDateToLocal ) . toHaveBeenCalledTimes ( 1 ) ; expect ( syncResult . syncedActivities ) . not . toBeNull ( ) ; expect ( syncResult . syncedActivities . length ) . toEqual ( 140 ) ; const jeannieRide = _ . find ( syncResult . syncedActivities , { id : 718908064 } ) ; expect ( jeannieRide . name ) . toEqual ( "Pédalage avec Madame Jeannie Longo" ) ; expect ( jeannieRide . start_time ) . toEqual ( "2016-09-20T13:44:54+0000" ) ; expect ( jeannieRide . moving_time_raw ) . toEqual ( 8557 ) ; expect ( jeannieRide . extendedStats ) . not . toBeNull ( ) ; expect ( jeannieRide . extendedStats . heartRateData ) . toBeNull ( ) ; expect ( jeannieRide . extendedStats . speedData ) . toBeNull ( ) ; const fakeRide = _ . find ( syncResult . syncedActivities , { id : 9999999999 } ) ; expect ( fakeRide ) . toBeUndefined ( ) ; expect ( activitiesSynchronize . hasBeenSyncedActivities ) . not . toBeNull ( ) ; return activitiesSynchronize . getLastSyncDateFromLocal ( ) ; } ) . then ( ( savedLastSyncDateTime ) => { expect ( CHROME_STORAGE_STUB . lastSyncDateTime ) . not . toBeNull ( ) ; expect ( _ . isNumber ( CHROME_STORAGE_STUB . lastSyncDateTime ) ) . toBeTruthy ( ) ; expect ( savedLastSyncDateTime ) . not . toBeNull ( ) ; expect ( _ . isNumber ( savedLastSyncDateTime ) ) . toBeTruthy ( ) ; done ( ) ; } , ( err ) => { expect ( err ) . toBeNull ( ) ; done ( ) ; } , ( progress ) => { } ) ; } ) ; it ( "should sync() when a new today training came up + an old one" , ( done ) => { expect ( CHROME_STORAGE_STUB . syncedActivities ) . toBeUndefined ( ) ; expect ( CHROME_STORAGE_STUB . lastSyncDateTime ) . toBeUndefined ( ) ; activitiesSynchronize . sync ( ) . then ( ( syncResult ) => { expect ( CHROME_STORAGE_STUB . syncedActivities ) . not . toBeNull ( ) ; expect ( CHROME_STORAGE_STUB . syncedActivities . length ) . toEqual ( 140 ) ; expect ( syncResult . activitiesChangesModel . added . length ) . toEqual ( 140 ) ; expect ( syncResult . activitiesChangesModel . deleted . length ) . toEqual ( 0 ) ; expect ( syncResult . activitiesChangesModel . edited . length ) . toEqual ( 0 ) ; expect ( syncResult . syncedActivities . length ) . toEqual ( CHROME_STORAGE_STUB . syncedActivities . length ) ; expect ( addStravaActivity ( 799672885 ) ) . toBeTruthy ( ) ; expect ( addStravaActivity ( 644365059 ) ) . toBeTruthy ( ) ; expect ( addStravaActivity ( 371317512 ) ) . toBeTruthy ( ) ; expect ( CHROME_STORAGE_STUB . syncedActivities . length ) . toEqual ( syncResult . syncedActivities . length - 3 ) ; expect ( _ . find ( CHROME_STORAGE_STUB . syncedActivities , < any > { id : 799672885 } ) ) . toBeUndefined ( ) ; expect ( _ . find ( CHROME_STORAGE_STUB . syncedActivities , < any > { id : 644365059 } ) ) . toBeUndefined ( ) ; expect ( _ . find ( CHROME_STORAGE_STUB . syncedActivities , < any > { id : 371317512 } ) ) . toBeUndefined ( ) ; expect ( activitiesSynchronize . hasBeenSyncedActivities ) . not . toBeNull ( ) ; return activitiesSynchronize . sync ( ) ; } ) . then ( ( syncResult ) => { expect ( syncResult . activitiesChangesModel . added . length ) . toEqual ( 3 ) ; expect ( syncResult . activitiesChangesModel . deleted . length ) . toEqual ( 0 ) ; expect ( syncResult . activitiesChangesModel . edited . length ) . toEqual ( 0 ) ; expect ( CHROME_STORAGE_STUB . syncedActivities ) . not . toBeNull ( ) ; expect ( CHROME_STORAGE_STUB . syncedActivities . length ) . toEqual ( 140 ) ; expect ( CHROME_STORAGE_STUB . syncedActivities . length ) . toEqual ( syncResult . syncedActivities . length ) ; expect ( _ . find ( CHROME_STORAGE_STUB . syncedActivities , < any > { id : 799672885 } ) ) . toBeDefined ( ) ; expect ( _ . find ( CHROME_STORAGE_STUB . syncedActivities , < any > { id : 644365059 } ) ) . toBeDefined ( ) ; expect ( _ . find ( CHROME_STORAGE_STUB . syncedActivities , < any > { id : 371317512 } ) ) . toBeDefined ( ) ; done ( ) ; } ) ; } ) ; it ( "should sync() when a training has been upload today to but perform 2 weeks ago, then test added first and last" , ( done ) => { activitiesSynchronize . sync ( ) . then ( ( syncResult ) => { expect ( CHROME_STORAGE_STUB . syncedActivities ) . not . toBeNull ( ) ; expect ( CHROME_STORAGE_STUB . syncedActivities . length ) . toEqual ( 140 ) ; expect ( syncResult . syncedActivities . length ) . toEqual ( 140 ) ; expect ( syncResult . activitiesChangesModel . added . length ) . toEqual ( 140 ) ; expect ( syncResult . activitiesChangesModel . deleted . length ) . toEqual ( 0 ) ; expect ( syncResult . activitiesChangesModel . edited . length ) . toEqual ( 0 ) ; expect ( syncResult . syncedActivities . length ) . toEqual ( CHROME_STORAGE_STUB . syncedActivities . length ) ; expect ( addStravaActivity ( 657225503 ) ) . toBeTruthy ( ) ; expect ( CHROME_STORAGE_STUB . syncedActivities . length ) . toEqual ( syncResult . syncedActivities . length - 1 ) ; expect ( _ . find ( CHROME_STORAGE_STUB . syncedActivities , < any > { id : 657225503 } ) ) . toBeUndefined ( ) ; expect ( activitiesSynchronize . hasBeenSyncedActivities ) . not . toBeNull ( ) ; return activitiesSynchronize . sync ( ) ; } ) . then ( ( syncResult ) => { expect ( CHROME_STORAGE_STUB . syncedActivities . length ) . toEqual ( 140 ) ; expect ( syncResult . syncedActivities . length ) . toEqual ( 140 ) ; expect ( syncResult . activitiesChangesModel . added . length ) . toEqual ( 1 ) ; expect ( _ . find ( CHROME_STORAGE_STUB . syncedActivities , < any > { id : 657225503 } ) ) . toBeDefined ( ) ; expect ( _ . find ( CHROME_STORAGE_STUB . syncedActivities , < any > { id : 799672885 } ) ) . toBeDefined ( ) ; expect ( _ . find ( CHROME_STORAGE_STUB . syncedActivities , < any > { id : 367463594 } ) ) . toBeDefined ( ) ; expect ( addStravaActivity ( 799672885 ) ) . toBeTruthy ( ) ; expect ( addStravaActivity ( 367463594 ) ) . toBeTruthy ( ) ; expect ( CHROME_STORAGE_STUB . syncedActivities . length ) . toEqual ( 138 ) ; expect ( _ . find ( CHROME_STORAGE_STUB . syncedActivities , < any > { id : 799672885 } ) ) . toBeUndefined ( ) ; expect ( _ . find ( CHROME_STORAGE_STUB . syncedActivities , < any > { id : 367463594 } ) ) . toBeUndefined ( ) ; return activitiesSynchronize . sync ( ) ; } ) . then ( ( syncResult ) => { expect ( CHROME_STORAGE_STUB . syncedActivities . length ) . toEqual ( 140 ) ; expect ( syncResult . syncedActivities . length ) . toEqual ( 140 ) ; expect ( syncResult . activitiesChangesModel . added . length ) . toEqual ( 2 ) ; expect ( syncResult . activitiesChangesModel . deleted . length ) . toEqual ( 0 ) ; expect ( syncResult . activitiesChangesModel . edited . length ) . toEqual ( 0 ) ; expect ( _ . find ( CHROME_STORAGE_STUB . syncedActivities , < any > { id : 799672885 } ) ) . toBeDefined ( ) ; expect ( _ . find ( CHROME_STORAGE_STUB . syncedActivities , < any > { id : 367463594 } ) ) . toBeDefined ( ) ; done ( ) ; } , ( err ) => { console . log ( "!! ERROR !!" , err ) ; done ( ) ; } , ( progress ) => { } ) ; } ) ; it ( "should sync() when 2 activities been edited from strava.com" , ( done ) => { activitiesSynchronize . sync ( ) . then ( ( syncResult ) => { expect ( CHROME_STORAGE_STUB . syncedActivities ) . not . toBeNull ( ) ; expect ( CHROME_STORAGE_STUB . syncedActivities . length ) . toEqual ( 140 ) ; expect ( syncResult . syncedActivities . length ) . toEqual ( 140 ) ; expect ( syncResult . activitiesChangesModel . added . length ) . toEqual ( 140 ) ; expect ( syncResult . activitiesChangesModel . deleted . length ) . toEqual ( 0 ) ; expect ( syncResult . activitiesChangesModel . edited . length ) . toEqual ( 0 ) ; expect ( editStravaActivity ( 9999999 , rawPagesOfActivities [ 0 ] , "FakeName" , "FakeType" ) ) . toBeFalsy ( ) ; expect ( editStravaActivity ( 707356065 , rawPagesOfActivities [ 0 ] , "Prends donc un velo!" , "Ride" ) ) . toBeTruthy ( ) ; expect ( editStravaActivity ( 427606185 , rawPagesOfActivities [ 5 ] , "First Zwift" , "VirtualRide" ) ) . toBeTruthy ( ) ; return activitiesSynchronize . sync ( ) ; } ) . then ( ( syncResult ) => { expect ( CHROME_STORAGE_STUB . syncedActivities ) . not . toBeNull ( ) ; expect ( CHROME_STORAGE_STUB . syncedActivities . length ) . toEqual ( 140 ) ; expect ( syncResult . syncedActivities ) . not . toBeNull ( ) ; expect ( syncResult . syncedActivities . length ) . toEqual ( 140 ) ; expect ( syncResult . activitiesChangesModel . added . length ) . toEqual ( 0 ) ; expect ( syncResult . activitiesChangesModel . deleted . length ) . toEqual ( 0 ) ; expect ( syncResult . activitiesChangesModel . edited . length ) . toEqual ( 2 ) ; let ride = _ . find ( syncResult . syncedActivities , { id : 707356065 } ) ; expect ( ride . name ) . toEqual ( "Prends donc un velo!" ) ; expect ( ride . type ) . toEqual ( "Ride" ) ; expect ( ride . display_type ) . toEqual ( "Ride" ) ; let virtualRide = _ . find ( syncResult . syncedActivities , { id : 427606185 } ) ; expect ( virtualRide . name ) . toEqual ( "First Zwift" ) ; expect ( virtualRide . type ) . toEqual ( "VirtualRide" ) ; expect ( virtualRide . display_type ) . toEqual ( "VirtualRide" ) ; ride = _ . find ( CHROME_STORAGE_STUB . syncedActivities , { id : 707356065 } ) ; expect ( ride . name ) . toEqual ( "Prends donc un velo!" ) ; expect ( ride . type ) . toEqual ( "Ride" ) ; expect ( ride . display_type ) . toEqual ( "Ride" ) ; virtualRide = _ . find ( CHROME_STORAGE_STUB . syncedActivities , { id : 427606185 } ) ; expect ( virtualRide . name ) . toEqual ( "First Zwift" ) ; expect ( virtualRide . type ) . toEqual ( "VirtualRide" ) ; expect ( virtualRide . display_type ) . toEqual ( "VirtualRide" ) ; done ( ) ; } , ( err ) => { console . log ( "!! ERROR !!" , err ) ; done ( ) ; } , ( progress ) => { } ) ; } ) ; it ( "should sync() when 3 activities have been removed from strava.com" , ( done ) => { activitiesSynchronize . sync ( ) . then ( ( syncResult ) => { expect ( CHROME_STORAGE_STUB . syncedActivities ) . not . toBeNull ( ) ; expect ( CHROME_STORAGE_STUB . syncedActivities . length ) . toEqual ( 140 ) ; expect ( syncResult . syncedActivities . length ) . toEqual ( 140 ) ; expect ( syncResult . activitiesChangesModel . added . length ) . toEqual ( 140 ) ; expect ( syncResult . activitiesChangesModel . deleted . length ) . toEqual ( 0 ) ; expect ( syncResult . activitiesChangesModel . edited . length ) . toEqual ( 0 ) ; expect ( removeStravaActivity ( 9999999 , rawPagesOfActivities [ 0 ] ) ) . toBeFalsy ( ) ; expect ( removeStravaActivity ( 707356065 , rawPagesOfActivities [ 0 ] ) ) . toBeTruthy ( ) ; expect ( removeStravaActivity ( 427606185 , rawPagesOfActivities [ 5 ] ) ) . toBeTruthy ( ) ; expect ( _ . find ( rawPagesOfActivities [ 0 ] . models , { id : 707356065 } ) ) . toBeUndefined ( ) ; expect ( _ . find ( rawPagesOfActivities [ 5 ] . models , { id : 427606185 } ) ) . toBeUndefined ( ) ; expect ( _ . find ( rawPagesOfActivities [ 5 ] . models , { id : 424565561 } ) ) . toBeDefined ( ) ; return activitiesSynchronize . sync ( ) ; } ) . then ( ( syncResult ) => { expect ( CHROME_STORAGE_STUB . syncedActivities ) . not . toBeNull ( ) ; expect ( CHROME_STORAGE_STUB . syncedActivities . length ) . toEqual ( 138 ) ; expect ( syncResult . syncedActivities . length ) . toEqual ( 138 ) ; expect ( syncResult . activitiesChangesModel . added . length ) . toEqual ( 0 ) ; expect ( syncResult . activitiesChangesModel . deleted . length ) . toEqual ( 2 ) ; expect ( syncResult . activitiesChangesModel . edited . length ) . toEqual ( 0 ) ; const ride = _ . find ( CHROME_STORAGE_STUB . syncedActivities , { id : 707356065 } ) ; expect ( ride ) . toBeUndefined ( ) ; const virtualRide = _ . find ( CHROME_STORAGE_STUB . syncedActivities , { id : 427606185 } ) ; expect ( virtualRide ) . toBeUndefined ( ) ; const anotherRide = _ . find ( CHROME_STORAGE_STUB . syncedActivities , { id : 424565561 } ) ; expect ( anotherRide ) . toBeDefined ( ) ; done ( ) ; } , ( err ) => { console . log ( "!! ERROR !!" , err ) ; done ( ) ; } , ( progress ) => { } ) ; } ) ; it ( "should sync() when added/edited/deleted from strava.com in the same sync" , ( done ) => { activitiesSynchronize . sync ( ) . then ( ( syncResult ) => { expect ( CHROME_STORAGE_STUB . syncedActivities ) . not . toBeNull ( ) ; expect ( CHROME_STORAGE_STUB . syncedActivities . length ) . toEqual ( 140 ) ; expect ( syncResult . syncedActivities . length ) . toEqual ( 140 ) ; expect ( syncResult . activitiesChangesModel . added . length ) . toEqual ( 140 ) ; expect ( syncResult . activitiesChangesModel . deleted . length ) . toEqual ( 0 ) ; expect ( syncResult . activitiesChangesModel . edited . length ) . toEqual ( 0 ) ; /**
             * Add 3 on various pages
             */ expect ( addStravaActivity ( 723224273 ) ) . toBeTruthy ( ) ; expect ( addStravaActivity ( 556443499 ) ) . toBeTruthy ( ) ; expect ( addStravaActivity ( 368210547 ) ) . toBeTruthy ( ) ; expect ( CHROME_STORAGE_STUB . syncedActivities . length ) . toEqual ( 137 ) ; expect ( _ . find ( CHROME_STORAGE_STUB . syncedActivities , < any > { id : 723224273 } ) ) . toBeUndefined ( ) ; expect ( _ . find ( CHROME_STORAGE_STUB . syncedActivities , < any > { id : 556443499 } ) ) . toBeUndefined ( ) ; expect ( _ . find ( CHROME_STORAGE_STUB . syncedActivities , < any > { id : 368210547 } ) ) . toBeUndefined ( ) ; expect ( _ . find ( CHROME_STORAGE_STUB . syncedActivities , < any > { id : 367463594 } ) ) . toBeDefined ( ) ; /**
             * Edit 4 on various pages
             */ expect ( editStravaActivity ( 999999999 , rawPagesOfActivities [ 0 ] , "FakeName" , "FakeType" ) ) . toBeFalsy ( ) ; expect ( editStravaActivity ( 707356065 , rawPagesOfActivities [ 0 ] , "Prends donc un velo!" , "Ride" ) ) . toBeTruthy ( ) ; expect ( editStravaActivity ( 569640952 , rawPagesOfActivities [ 2 ] , "Petit nez!" , "Ride" ) ) . toBeTruthy ( ) ; expect ( editStravaActivity ( 427606185 , rawPagesOfActivities [ 5 ] , "First Zwift" , "VirtualRide" ) ) . toBeTruthy ( ) ; expect ( editStravaActivity ( 372761597 , rawPagesOfActivities [ 6 ] , "Rodage plaquettes new name" , "EBike" ) ) . toBeTruthy ( ) ; expect ( _ . find ( rawPagesOfActivities [ 2 ] . models , { id : 569640952 } ) . name ) . toEqual ( "Petit nez!" ) ; expect ( _ . find ( rawPagesOfActivities [ 6 ] . models , { id : 372761597 } ) . type ) . toEqual ( "EBike" ) ; expect ( _ . find ( rawPagesOfActivities [ 0 ] . models , { id : 707356065 } ) . type ) . not . toEqual ( "EBike" ) ; /**
             * Delete 5 on various pages
             */ expect ( removeStravaActivity ( 999999999 , rawPagesOfActivities [ 0 ] ) ) . toBeFalsy ( ) ; expect ( removeStravaActivity ( 661113141 , rawPagesOfActivities [ 0 ] ) ) . toBeTruthy ( ) ; expect ( removeStravaActivity ( 566288762 , rawPagesOfActivities [ 2 ] ) ) . toBeTruthy ( ) ; expect ( removeStravaActivity ( 552562511 , rawPagesOfActivities [ 3 ] ) ) . toBeTruthy ( ) ; expect ( removeStravaActivity ( 473894759 , rawPagesOfActivities [ 4 ] ) ) . toBeTruthy ( ) ; expect ( removeStravaActivity ( 406217194 , rawPagesOfActivities [ 6 ] ) ) . toBeTruthy ( ) ; expect ( _ . find ( rawPagesOfActivities [ 0 ] . models , { id : 661113141 } ) ) . toBeUndefined ( ) ; expect ( _ . find ( rawPagesOfActivities [ 2 ] . models , { id : 566288762 } ) ) . toBeUndefined ( ) ; expect ( _ . find ( rawPagesOfActivities [ 3 ] . models , { id : 552562511 } ) ) . toBeUndefined ( ) ; expect ( _ . find ( rawPagesOfActivities [ 4 ] . models , { id : 473894759 } ) ) . toBeUndefined ( ) ; expect ( _ . find ( rawPagesOfActivities [ 6 ] . models , { id : 406217194 } ) ) . toBeUndefined ( ) ; expect ( _ . find ( rawPagesOfActivities [ 5 ] . models , { id : 424565561 } ) ) . toBeDefined ( ) ; return activitiesSynchronize . sync ( ) ; } ) . then ( ( syncResult ) => { expect ( CHROME_STORAGE_STUB . syncedActivities ) . not . toBeNull ( ) ; expect ( syncResult . syncedActivities ) . not . toBeNull ( ) ; expect ( CHROME_STORAGE_STUB . syncedActivities . length ) . toEqual ( 135 ) ; expect ( syncResult . syncedActivities . length ) . toEqual ( 135 ) ; expect ( syncResult . activitiesChangesModel . added . length ) . toEqual ( 3 ) ; expect ( syncResult . activitiesChangesModel . deleted . length ) . toEqual ( 5 ) ; expect ( syncResult . activitiesChangesModel . edited . length ) . toEqual ( 4 ) ; let activity = _ . find ( CHROME_STORAGE_STUB . syncedActivities , { id : 707356065 } ) ; expect ( activity . name ) . toEqual ( "Prends donc un velo!" ) ; expect ( activity . type ) . toEqual ( "Ride" ) ; expect ( activity . display_type ) . toEqual ( "Ride" ) ; activity = _ . find ( CHROME_STORAGE_STUB . syncedActivities , { id : 372761597 } ) ; expect ( activity . name ) . toEqual ( "Rodage plaquettes new name" ) ; expect ( activity . type ) . toEqual ( "EBike" ) ; expect ( activity . display_type ) . toEqual ( "EBike" ) ; activity = _ . find ( CHROME_STORAGE_STUB . syncedActivities , { id : 723224273 } ) ; expect ( activity . name ) . toEqual ( "Bon rythme ! 33 KPH !!" ) ; activity = _ . find ( CHROME_STORAGE_STUB . syncedActivities , { id : 556443499 } ) ; expect ( activity . name ) . toEqual ( "75k @ 31.5 KPH // 181 BPM" ) ; activity = _ . find ( CHROME_STORAGE_STUB . syncedActivities , { id : 566288762 } ) ; expect ( activity ) . toBeUndefined ( ) ; activity = _ . find ( CHROME_STORAGE_STUB . syncedActivities , { id : 473894759 } ) ; expect ( activity ) . toBeUndefined ( ) ; activity = _ . find ( CHROME_STORAGE_STUB . syncedActivities , { id : 424565561 } ) ; expect ( activity ) . toBeDefined ( ) ; done ( ) ; } ) ; } ) ; it ( "should ensure hasRemoteFirstPageActivitiesMismatch() reject when no local activities" , ( done ) => { const hasMissMatchPromise = activitiesSynchronize . hasRemoteFirstPageActivitiesMismatch ( ) ; hasMissMatchPromise . then ( ( ) => { expect ( false ) . toBeTruthy ( "Should not be here !" ) ; done ( ) ; } , error => { expect ( error ) . not . toBeNull ( ) ; done ( ) ; } ) ; } ) ; it ( "should ensure hasRemoteFirstPageActivitiesMismatch() detect remote added activity" , ( done ) => { const newStravaActivityId = 799672885 ; const promiseLocalSyncedActivity = activitiesSynchronize . sync ( ) . then ( ( syncResult ) => { addStravaActivity ( newStravaActivityId ) ; return Q . resolve ( ) ; } ) ; promiseLocalSyncedActivity . then ( ( ) => { const hasMissMatchPromise = activitiesSynchronize . hasRemoteFirstPageActivitiesMismatch ( ) ; hasMissMatchPromise . then ( ( result ) => { expect ( result . hasMisMatch ) . toBeTruthy ( ) ; expect ( result . activitiesChangesModel . added . length ) . toEqual ( 1 ) ; expect ( result . activitiesChangesModel . edited . length ) . toEqual ( 0 ) ; expect ( result . activitiesChangesModel . deleted . length ) . toEqual ( 0 ) ; done ( ) ; } ) ; } ) ; } ) ; it ( "should ensure hasRemoteFirstPageActivitiesMismatch() detect no remote added activity" , ( done ) => { const promiseSynced = activitiesSynchronize . sync ( ) ; const hasMissMatchPromise = promiseSynced . then ( ( ) => { return activitiesSynchronize . hasRemoteFirstPageActivitiesMismatch ( ) ; } ) ; hasMissMatchPromise . then ( ( result ) => { expect ( result . hasMisMatch ) . toBeFalsy ( ) ; expect ( result . activitiesChangesModel . added . length ) . toEqual ( 0 ) ; expect ( result . activitiesChangesModel . edited . length ) . toEqual ( 0 ) ; expect ( result . activitiesChangesModel . deleted . length ) . toEqual ( 0 ) ; done ( ) ; } ) ; } ) ; it ( "should ensure hasRemoteFirstPageActivitiesMismatch() detect a remote edited activity" , ( done ) => { const editedActivityId = 727632286 ; const newName = "NewName" ; const newType = "NewType" ; const promiseLocalSyncedActivity = activitiesSynchronize . sync ( ) . then ( ( ) => { editStravaActivity ( editedActivityId , rawPagesOfActivities [ 0 ] , newName , newType ) ; return Q . resolve ( ) ; } ) ; promiseLocalSyncedActivity . then ( ( ) => { const hasMissMatchPromise = activitiesSynchronize . hasRemoteFirstPageActivitiesMismatch ( ) ; hasMissMatchPromise . then ( ( result ) => { expect ( result . hasMisMatch ) . toBeTruthy ( ) ; expect ( result . activitiesChangesModel . added . length ) . toEqual ( 0 ) ; expect ( result . activitiesChangesModel . edited . length ) . toEqual ( 1 ) ; expect ( result . activitiesChangesModel . deleted . length ) . toEqual ( 0 ) ; done ( ) ; } ) ; } ) ; } ) ; it ( "should ensure hasRemoteFirstPageActivitiesMismatch() detect a remote deleted activity" , ( done ) => { const deletedActivityId = 727632286 ; const promiseLocalSyncedActivity = activitiesSynchronize . sync ( ) . then ( ( ) => { removeStravaActivity ( deletedActivityId , rawPagesOfActivities [ 0 ] ) ; return Q . resolve ( ) ; } ) ; promiseLocalSyncedActivity . then ( ( ) => { const hasMissMatchPromise = activitiesSynchronize . hasRemoteFirstPageActivitiesMismatch ( ) ; hasMissMatchPromise . then ( ( result ) => { expect ( result . hasMisMatch ) . toBeTruthy ( ) ; expect ( result . activitiesChangesModel . added . length ) . toEqual ( 0 ) ; expect ( result . activitiesChangesModel . edited . length ) . toEqual ( 0 ) ; expect ( result . activitiesChangesModel . deleted . length ) . toEqual ( 1 ) ; done ( ) ; } ) ; } ) ; } ) ; it ( "should ensure hasRemoteFirstPageActivitiesMismatch() detect a remote edited and added activity" , ( done ) => { const editedActivityId = 727632286 ; const editedActivityId2 = 722210052 ; const addedActivityId = 723224273 ; const deletedActivityId = 707356065 ; const promiseLocalSyncedActivity = activitiesSynchronize . sync ( ) . then ( ( ) => { editStravaActivity ( editedActivityId , rawPagesOfActivities [ 0 ] , "Fake" , "Fake" ) ; editStravaActivity ( editedActivityId2 , rawPagesOfActivities [ 0 ] , "Fake" , "Fake" ) ; addStravaActivity ( addedActivityId ) ; removeStravaActivity ( deletedActivityId , rawPagesOfActivities [ 0 ] ) ; return Q . resolve ( ) ; } ) ; promiseLocalSyncedActivity . then ( ( ) => { const hasMissMatchPromise = activitiesSynchronize . hasRemoteFirstPageActivitiesMismatch ( ) ; hasMissMatchPromise . then ( ( result ) => { expect ( result . hasMisMatch ) . toBeTruthy ( ) ; expect ( result . activitiesChangesModel . added . length ) . toEqual ( 1 ) ; expect ( result . activitiesChangesModel . edited . length ) . toEqual ( 2 ) ; expect ( result . activitiesChangesModel . deleted . length ) . toEqual ( 1 ) ; done ( ) ; } ) ; } ) ; } ) ; it ( "should ensure hasRemoteFirstPageActivitiesMismatch() detect a remote edited, added activity and deleted activity" , ( done ) => { const editedActivityId = 727632286 ; const editedActivityId2 = 722210052 ; const addedActivityId = 723224273 ; const promiseLocalSyncedActivity = activitiesSynchronize . sync ( ) . then ( ( ) => { editStravaActivity ( editedActivityId , rawPagesOfActivities [ 0 ] , "Fake" , "Fake" ) ; editStravaActivity ( editedActivityId2 , rawPagesOfActivities [ 0 ] , "Fake" , "Fake" ) ; addStravaActivity ( addedActivityId ) ; return Q . resolve ( ) ; } ) ; promiseLocalSyncedActivity . then ( ( ) => { const hasMissMatchPromise = activitiesSynchronize . hasRemoteFirstPageActivitiesMismatch ( ) ; hasMissMatchPromise . then ( ( result ) => { expect ( result . hasMisMatch ) . toBeTruthy ( ) ; expect ( result . activitiesChangesModel . added . length ) . toEqual ( 1 ) ; expect ( result . activitiesChangesModel . edited . length ) . toEqual ( 2 ) ; expect ( result . activitiesChangesModel . deleted . length ) . toEqual ( 0 ) ; done ( ) ; } ) ; } ) ; } ) ; it ( "should ensure fast sync with added activity" , ( done ) => { const enableFastSync = true ; const addedStravaActivityId = 727632286 ; const expectedName = "Lunch Ride" ; const expectedType = "Ride" ; const promiseLocalSyncedActivity = activitiesSynchronize . sync ( ) . then ( ( ) => { addStravaActivity ( addedStravaActivityId ) ; return Q . resolve ( ) ; } ) ; const promiseFastSync = promiseLocalSyncedActivity . then ( ( ) => { return activitiesSynchronize . sync ( enableFastSync ) ; } ) ; promiseFastSync . then ( ( syncResultModel ) => { expect ( syncResultModel . activitiesChangesModel . added . length ) . toEqual ( 1 ) ; expect ( syncResultModel . activitiesChangesModel . edited . length ) . toEqual ( 0 ) ; expect ( syncResultModel . activitiesChangesModel . deleted . length ) . toEqual ( 0 ) ; const activity = _ . find ( CHROME_STORAGE_STUB . syncedActivities , { id : addedStravaActivityId } ) ; expect ( activity . name ) . toEqual ( expectedName ) ; expect ( activity . type ) . toEqual ( expectedType ) ; done ( ) ; } ) ; } ) ; it ( "should ensure fast sync with edited activity" , ( done ) => { const enableFastSync = true ; const editedActivityId = 727632286 ; const newName = "NewName" ; const newType = "NewType" ; const promiseLocalSyncedActivity = activitiesSynchronize . sync ( ) . then ( ( ) => { editStravaActivity ( editedActivityId , rawPagesOfActivities [ 0 ] , newName , newType ) ; return Q . resolve ( ) ; } ) ; const promiseFastSync = promiseLocalSyncedActivity . then ( ( ) => { return activitiesSynchronize . sync ( enableFastSync ) ; } ) ; promiseFastSync . then ( ( syncResultModel ) => { expect ( syncResultModel . activitiesChangesModel . added . length ) . toEqual ( 0 ) ; expect ( syncResultModel . activitiesChangesModel . edited . length ) . toEqual ( 1 ) ; expect ( syncResultModel . activitiesChangesModel . deleted . length ) . toEqual ( 0 ) ; const activity = _ . find ( CHROME_STORAGE_STUB . syncedActivities , { id : editedActivityId } ) ; expect ( activity . name ) . toEqual ( newName ) ; expect ( activity . type ) . toEqual ( newType ) ; expect ( activity . display_type ) . toEqual ( newType ) ; done ( ) ; } ) ; } ) ; it ( "should ensure fast sync with deleted activity" , ( done ) => { const enableFastSync = true ; const deletedActivityId = 727632286 ; const promiseLocalSyncedActivity = activitiesSynchronize . sync ( ) . then ( ( ) => { removeStravaActivity ( deletedActivityId , rawPagesOfActivities [ 0 ] ) ; return Q . resolve ( ) ; } ) ; const promiseFastSync = promiseLocalSyncedActivity . then ( ( ) => { return activitiesSynchronize . sync ( enableFastSync ) ; } ) ; promiseFastSync . then ( ( syncResultModel ) => { expect ( syncResultModel . activitiesChangesModel . added . length ) . toEqual ( 0 ) ; expect ( syncResultModel . activitiesChangesModel . edited . length ) . toEqual ( 0 ) ; expect ( syncResultModel . activitiesChangesModel . deleted . length ) . toEqual ( 1 ) ; const activity = < SyncedActivityModel > _ . find ( CHROME_STORAGE_STUB . syncedActivities , { id : deletedActivityId } ) ; expect ( _ . isEmpty ( activity ) ) . toBeTruthy ( ) ; expect ( CHROME_STORAGE_STUB . syncedActivities . length ) . toEqual ( 139 ) ; done ( ) ; } ) ; } ) ; it ( "should ensure fast sync with added, edited and deleted activities" , ( done ) => { const enableFastSync = true ; const editedActivityId = 727632286 ; const newName = "FakeName1" ; const newType = "FakeType1" ; const editedActivityId2 = 722210052 ; const newName2 = "FakeName2" ; const newType2 = "FakeType2" ; const addedActivityId = 723224273 ; const deletedActivityId = 707356065 ; const promiseLocalSyncedActivity = activitiesSynchronize . sync ( ) . then ( ( ) => { editStravaActivity ( editedActivityId , rawPagesOfActivities [ 0 ] , newName , newType ) ; editStravaActivity ( editedActivityId2 , rawPagesOfActivities [ 0 ] , newName2 , newType2 ) ; addStravaActivity ( addedActivityId ) ; removeStravaActivity ( deletedActivityId , rawPagesOfActivities [ 0 ] ) ; return Q . resolve ( ) ; } ) ; const promiseFastSync = promiseLocalSyncedActivity . then ( ( ) => { return activitiesSynchronize . sync ( enableFastSync ) ; } ) ; promiseFastSync . then ( ( syncResultModel ) => { expect ( syncResultModel . activitiesChangesModel . added . length ) . toEqual ( 1 ) ; expect ( syncResultModel . activitiesChangesModel . edited . length ) . toEqual ( 2 ) ; expect ( syncResultModel . activitiesChangesModel . deleted . length ) . toEqual ( 1 ) ; let activity = _ . find ( CHROME_STORAGE_STUB . syncedActivities , { id : editedActivityId } ) ; expect ( activity . name ) . toEqual ( newName ) ; expect ( activity . type ) . toEqual ( newType ) ; expect ( activity . display_type ) . toEqual ( newType ) ; activity = _ . find ( CHROME_STORAGE_STUB . syncedActivities , { id : editedActivityId2 } ) ; expect ( activity . name ) . toEqual ( newName2 ) ; expect ( activity . type ) . toEqual ( newType2 ) ; expect ( activity . display_type ) . toEqual ( newType2 ) ; activity = _ . find ( CHROME_STORAGE_STUB . syncedActivities , { id : addedActivityId } ) ; expect ( activity ) . not . toBeNull ( ) ; activity = _ . find ( CHROME_STORAGE_STUB . syncedActivities , { id : deletedActivityId } ) ; expect ( _ . isEmpty ( activity ) ) . toBeTruthy ( ) ; done ( ) ; } ) ; } ) ; it ( "should ensure fast sync with no changes" , ( done ) => { const enableFastSync = true ; const promiseLocalSyncedActivity = activitiesSynchronize . sync ( ) . then ( ( ) => { return Q . resolve ( ) ; } ) ; const promiseFastSync = promiseLocalSyncedActivity . then ( ( ) => { return activitiesSynchronize . sync ( enableFastSync ) ; } ) ; promiseFastSync . then ( ( syncResultModel ) => { expect ( syncResultModel . activitiesChangesModel . added . length ) . toEqual ( 0 ) ; expect ( syncResultModel . activitiesChangesModel . edited . length ) . toEqual ( 0 ) ; expect ( syncResultModel . activitiesChangesModel . deleted . length ) . toEqual ( 0 ) ; expect ( CHROME_STORAGE_STUB . syncedActivities . length ) . toEqual ( 140 ) ; done ( ) ; } , error => { expect ( error ) . toBeNull ( error ) ; done ( ) ; } ) ; } ) ; afterEach ( ( ) => { activitiesSynchronize = null ; } ) ; } ) ;