import assert from 'assert' ; import type { UserOptions , UserInterceptor } from './types' ; function assertIsPage ( maybePage , prefix ) { if ( typeof maybePage . mainFrame !== 'function' ) { const message = prefix + 'not a valid page! Method page.mainFrame() not found.' ; throw new assert . AssertionError ( { message } ) ; } } function assertIsOptionalBoolean ( val , message ) { if ( typeof val !== 'undefined' && typeof val !== 'boolean' ) { throw new assert . AssertionError ( { message } ) ; } } function assertIsOptionalString ( val , message ) { if ( typeof val !== 'undefined' && typeof val !== 'string' ) { throw new assert . AssertionError ( { message } ) ; } } function assertIsStringOrStringArray ( val , prefix ) { const isString = typeof val === 'string' ; const isStringArray = Array . isArray ( val ) && val . every ( item => typeof item === 'string' ) ; if ( ! isString && ! isStringArray ) { const message = prefix + 'not a string or string[]' ; throw new assert . AssertionError ( { message } ) ; } } function assertIsUserInterceptor ( val , prefix ) { assertIsOptionalString ( val . name , prefix + 'interceptor.name must be a string' ) ; assertIsOptionalString ( val . url , prefix + 'interceptor.url must be a string' ) ; assertIsOptionalBoolean ( val . pass , prefix + 'interceptor.pass must be boolean' ) ; assertIsOptionalString ( val . name , prefix + 'interceptor.name must be a string' ) ; assertIsOptionalString ( val . name , prefix + 'interceptor.name must be a string' ) ; if ( typeof val . name !== 'undefined' && typeof val . name !== 'string' ) { const message = prefix + 'interceptor.name must be a string' ; throw new assert . AssertionError ( { message } ) ; } if ( typeof val . url !== 'undefined' && typeof val . url !== 'string' ) { const message = prefix + 'interceptor.url must be a string' ; throw new assert . AssertionError ( { message } ) ; } if ( typeof val . url !== 'undefined' && typeof val . url !== 'string' ) { const message = prefix + 'interceptor.url must be a string' ; throw new assert . AssertionError ( { message } ) ; } } export function assertUserOptions ( userOptions , msg = 'teremock.start() options validation: ' ) { if ( typeof userOptions . page !== 'undefined' ) { assertIsPage ( userOptions . page , msg ) ; } if ( typeof userOptions . wd !== 'undefined' ) { assertIsStringOrStringArray ( userOptions . wd , msg ) ; } if ( typeof userOptions . interceptors !== 'undefined' ) { Object . keys ( userOptions . interceptors ) . forEach ( key => { return assertIsUserInterceptor ( userOptions . interceptors [ key ] , msg + key + ' ' ) ; } ) ; } }