import test from 'unit.js' ; import assert from 'assert' ; import { VEHICLE_EVENTS , VehicleEvent } from '@mds-core/mds-types' ; import { routeDistance , isStateTransitionValid , normalizeToArray , filterDefined } from '../utils' ; import { expectedTransitions } from './state-transition-expected' ; const Boston = { lat : 42.360081 , lng : - 71.058884 } ; const LosAngeles = { lat : 34.052235 , lng : - 118.243683 } ; const BostonToLA = 4169605.469765776 ; describe ( 'Tests Utilities' , ( ) => { it ( 'routeDistance: Verifies single point' , done => { test . value ( routeDistance ( [ Boston ] ) ) . is ( 0 ) ; done ( ) ; } ) ; it ( 'routeDistance: Verifies 2 points' , done => { test . value ( routeDistance ( [ Boston , LosAngeles ] ) ) . is ( BostonToLA ) ; done ( ) ; } ) ; it ( 'routeDistance: Verifies 2+ points' , done => { test . value ( routeDistance ( [ Boston , LosAngeles , Boston ] ) ) . is ( BostonToLA * 2 ) ; done ( ) ; } ) ; describe ( 'Filter empty' , ( ) => { it ( 'Filters out null/undefined elements' , ( ) => { const arr = [ 1 , 2 , null , 3 , undefined , 4 ] ; const actual = arr . filter ( filterDefined ( ) ) ; const expected = [ 1 , 2 , 3 , 4 ] ; assert . deepStrictEqual ( actual , expected ) ; } ) ; it ( 'Does not filter 0 or "" (empty string) or [] (empty array)' , ( ) => { const arr = [ 1 , 2 , '' , 3 , [ ] , 0 ] ; const actual = arr . filter ( filterDefined ( ) ) ; const expected = arr ; assert . deepStrictEqual ( actual , expected ) ; } ) ; } ) ; describe ( 'Normalize to array' , ( ) => { it ( 'Normalizes undefined to empty array' , ( ) => { assert . deepStrictEqual ( normalizeToArray ( undefined ) , [ ] ) ; } ) ; it ( 'Normalizes single element into singleton array' , ( ) => { assert . deepStrictEqual ( normalizeToArray ( 'test' ) , [ 'test' ] ) ; } ) ; it ( 'Leaves array untouched' , ( ) => { assert . deepStrictEqual ( normalizeToArray ( [ 'test1' , 'test2' ] ) , [ 'test1' , 'test2' ] ) ; } ) ; } ) ; describe ( 'State machine' , ( ) => { it ( 'Tests state transitions' , ( ) => { const events = Object . keys ( VEHICLE_EVENTS ) ; for ( const event_type_A of events ) { for ( const event_type_B of events ) { const eventA = { event_type : event_type_A } as VehicleEvent ; const eventB = { event_type : event_type_B } as VehicleEvent ; const actual = isStateTransitionValid ( eventA , eventB ) ; const transitionKey = `${ eventA . event_type }, ${ eventB . event_type }` ; assert . strictEqual ( actual , expectedTransitions [ eventA . event_type ] [ eventB . event_type ] , transitionKey ) ; } } } ) ; } ) ; } ) ;