import { Destination , validateNameAvailable , setDestinationsInEnv } from '@sap-cloud-sdk/connectivity/internal' ; import { getTestDestinationByAlias , GetTestDestinationOptions , getTestDestinations } from './test-destination-provider' ; param name param options export function mockTestDestination ( name , options ? ) { const mockedDestination = getTestDestinationByAlias ( name , options ) ; setTestDestination ( mockedDestination ) ; } param destination param options export function setTestDestination ( destination ) { const currentDestinations = getDestinationsFromEnv ( ) ; const existingNames = new Set < string > ( currentDestinations . map ( dest => { if ( ! dest . name ) { throw Error ( 'The destination name is undefined.' ) ; } return dest . name ; } ) ) ; if ( ! destination . name ) { throw Error ( 'The destination name is undefined.' ) ; } validateNameAvailable ( destination . name , existingNames ) ; destination . isTestDestination = true ; setDestinationsInEnv ( [ ... currentDestinations , destination ] ) ; } param name export function unmockTestDestination ( name ) { const currentDestinations = getDestinationsFromEnv ( ) ; const cleanedDestinations = currentDestinations . filter ( destination => ! ( destination . isTestDestination && destination . name === name ) ) ; setDestinationsInEnv ( cleanedDestinations ) ; } param options export function mockAllTestDestinations ( options ? ) { const testDestinations = getTestDestinations ( options ) ; const currentDestinations = getDestinationsFromEnv ( ) ; const existingNames = new Set < string > ( currentDestinations . map ( dest => { if ( ! dest . name ) { throw Error ( 'The destination name is undefined.' ) ; } return dest . name ; } ) ) ; testDestinations . forEach ( dest => { if ( ! dest . name ) { throw Error ( "At least one of the provided destinations is missing a the 'name' property!" ) ; } validateNameAvailable ( dest . name , existingNames ) ; currentDestinations . push ( dest ) ; } ) ; setDestinationsInEnv ( currentDestinations ) ; } /**
 * Removes all test destinations stored in `systems.json` and `credentials.json` files or added through [[setTestDestination]]() from the `destinations` environment variable.
 *
 * This function should be called to invert the behavior of [[mockAllTestDestinations]]() and other add single mocked destination functions.
 */ export function unmockAllTestDestinations ( ) { const currentDestinations = getDestinationsFromEnv ( ) ; const cleanedDestinations = currentDestinations . filter ( destination => ! destination . isTestDestination ) ; setDestinationsInEnv ( cleanedDestinations ) ; } function getDestinationsFromEnv ( ) { try { const envDestinations = process . env [ 'destinations' ] || '[]' ; return JSON . parse ( envDestinations ) ; } catch ( error ) { throw new Error ( `Destinations environment variable cannot be read: ${ error . message }` ) ; } }