import { packageMock , mockedRootDir , gitRevExecMock , devConfigMock , readUuidFileMock , resetAllMocks , } from './config.test.mocks' ; import { ApmConfiguration } from './config' ; describe ( 'ApmConfiguration' , ( ) => { beforeEach ( ( ) => { packageMock . raw = { version : '8.0.0' , build : { sha : 'sha' , } , } ; } ) ; afterEach ( ( ) => { resetAllMocks ( ) ; } ) ; it ( 'sets the correct service name and version' , ( ) => { packageMock . raw = { version : '9.2.1' , } ; const config = new ApmConfiguration ( mockedRootDir , { } , false ) ; expect ( config . getConfig ( 'myservice' ) . serviceName ) . toBe ( 'myservice' ) ; expect ( config . getConfig ( 'myservice' ) . serviceVersion ) . toBe ( '9.2.1' ) ; } ) ; it ( 'sets the git revision from `git rev-parse` command in non distribution mode' , ( ) => { gitRevExecMock . mockReturnValue ( 'some-git-rev' ) ; const config = new ApmConfiguration ( mockedRootDir , { } , false ) ; expect ( config . getConfig ( 'serviceName' ) . globalLabels . git_rev ) . toBe ( 'some-git-rev' ) ; } ) ; it ( 'sets the git revision from `pkg.build.sha` in distribution mode' , ( ) => { gitRevExecMock . mockReturnValue ( 'dev-sha' ) ; packageMock . raw = { version : '9.2.1' , build : { sha : 'distribution-sha' , } , } ; const config = new ApmConfiguration ( mockedRootDir , { } , true ) ; expect ( config . getConfig ( 'serviceName' ) . globalLabels . git_rev ) . toBe ( 'distribution-sha' ) ; } ) ; it ( 'reads the opensearchDashboards uuid from the uuid file' , ( ) => { readUuidFileMock . mockReturnValue ( 'instance-uuid' ) ; const config = new ApmConfiguration ( mockedRootDir , { } , false ) ; expect ( config . getConfig ( 'serviceName' ) . globalLabels . opensearch_dashboards_uuid ) . toBe ( 'instance-uuid' ) ; } ) ; it ( 'uses the uuid from the opensearchDashboards config if present' , ( ) => { readUuidFileMock . mockReturnValue ( 'uuid-from-file' ) ; const opensearchDashboardsConfig = { server : { uuid : 'uuid-from-config' , } , } ; const config = new ApmConfiguration ( mockedRootDir , opensearchDashboardsConfig , false ) ; expect ( config . getConfig ( 'serviceName' ) . globalLabels . opensearch_dashboards_uuid ) . toBe ( 'uuid-from-config' ) ; } ) ; it ( 'uses the correct default config depending on the `isDistributable` parameter' , ( ) => { let config = new ApmConfiguration ( mockedRootDir , { } , false ) ; expect ( config . getConfig ( 'serviceName' ) ) . toEqual ( expect . objectContaining ( { serverUrl : expect . any ( String ) , secretToken : expect . any ( String ) , } ) ) ; config = new ApmConfiguration ( mockedRootDir , { } , true ) ; expect ( Object . keys ( config . getConfig ( 'serviceName' ) ) ) . not . toContain ( 'serverUrl' ) ; } ) ; it ( 'loads the configuration from the opensearchDashboards config file' , ( ) => { const opensearchDashboardsConfig = { opensearch : { apm : { active : true , serverUrl : 'https://url' , secretToken : 'secret' , } , } , } ; const config = new ApmConfiguration ( mockedRootDir , opensearchDashboardsConfig , true ) ; expect ( config . getConfig ( 'serviceName' ) ) . toEqual ( expect . objectContaining ( { active : true , serverUrl : 'https://url' , secretToken : 'secret' , } ) ) ; } ) ; it ( 'loads the configuration from the dev config is present' , ( ) => { devConfigMock . raw = { active : true , serverUrl : 'https://dev-url.co' , } ; const config = new ApmConfiguration ( mockedRootDir , { } , true ) ; expect ( config . getConfig ( 'serviceName' ) ) . toEqual ( expect . objectContaining ( { active : true , serverUrl : 'https://dev-url.co' , } ) ) ; } ) ; it ( 'respect the precedence of the dev config' , ( ) => { const opensearchDashboardsConfig = { opensearch : { apm : { active : true , serverUrl : 'https://url' , secretToken : 'secret' , } , } , } ; devConfigMock . raw = { active : true , serverUrl : 'https://dev-url.co' , } ; const config = new ApmConfiguration ( mockedRootDir , opensearchDashboardsConfig , true ) ; expect ( config . getConfig ( 'serviceName' ) ) . toEqual ( expect . objectContaining ( { active : true , serverUrl : 'https://dev-url.co' , secretToken : 'secret' , } ) ) ; } ) ; } ) ;