import { DiscoveredPlugin } from '../../server' ; import { InjectedMetadataService } from './injected_metadata_service' ; describe ( 'setup.getOpenSearchDashboardsBuildNumber()' , ( ) => { it ( 'returns buildNumber from injectedMetadata' , ( ) => { const setup = new InjectedMetadataService ( { injectedMetadata : { buildNumber : 'foo' , } , } as any ) . setup ( ) ; expect ( setup . getOpenSearchDashboardsBuildNumber ( ) ) . toBe ( 'foo' ) ; } ) ; } ) ; describe ( 'setup.getCspConfig()' , ( ) => { it ( 'returns injectedMetadata.csp' , ( ) => { const setup = new InjectedMetadataService ( { injectedMetadata : { csp : { warnLegacyBrowsers : true , } , } , } as any ) . setup ( ) ; expect ( setup . getCspConfig ( ) ) . toEqual ( { warnLegacyBrowsers : true , } ) ; } ) ; it ( 'csp config is frozen' , ( ) => { const injectedMetadata = new InjectedMetadataService ( { injectedMetadata : { csp : { warnLegacyBrowsers : true , } , } , } as any ) ; const csp = injectedMetadata . setup ( ) . getCspConfig ( ) ; expect ( ( ) => { csp . warnLegacyBrowsers = false ; } ) . toThrowError ( ) ; } ) ; } ) ; describe ( 'setup.getPlugins()' , ( ) => { it ( 'returns injectedMetadata.uiPlugins' , ( ) => { const injectedMetadata = new InjectedMetadataService ( { injectedMetadata : { uiPlugins : [ { id : 'plugin-1' , plugin : { } , config : { clientProp : 'clientValue' } } , { id : 'plugin-2' , plugin : { } } , ] , } , } as any ) ; const plugins = injectedMetadata . setup ( ) . getPlugins ( ) ; expect ( plugins ) . toEqual ( [ { id : 'plugin-1' , plugin : { } , config : { clientProp : 'clientValue' } } , { id : 'plugin-2' , plugin : { } } , ] ) ; } ) ; it ( 'returns frozen version of uiPlugins' , ( ) => { const injectedMetadata = new InjectedMetadataService ( { injectedMetadata : { uiPlugins : [ { id : 'plugin-1' , plugin : { } } , { id : 'plugin-2' , plugin : { } } , ] , } , } as any ) ; const plugins = injectedMetadata . setup ( ) . getPlugins ( ) ; expect ( ( ) => { plugins . pop ( ) ; } ) . toThrowError ( ) ; expect ( ( ) => { plugins . push ( { id : 'new-plugin' , plugin : { } as DiscoveredPlugin } ) ; } ) . toThrowError ( ) ; expect ( ( ) => { plugins [ 0 ] . name = 'changed' ; } ) . toThrowError ( ) ; expect ( ( ) => { plugins [ 0 ] . newProp = 'changed' ; } ) . toThrowError ( ) ; } ) ; } ) ; describe ( 'setup.getLegacyMetadata()' , ( ) => { it ( 'returns injectedMetadata.legacyMetadata' , ( ) => { const injectedMetadata = new InjectedMetadataService ( { injectedMetadata : { legacyMetadata : 'foo' , } , } as any ) ; const contract = injectedMetadata . setup ( ) ; expect ( contract . getLegacyMetadata ( ) ) . toBe ( 'foo' ) ; } ) ; it ( 'exposes frozen version of legacyMetadata' , ( ) => { const injectedMetadata = new InjectedMetadataService ( { injectedMetadata : { legacyMetadata : { foo : true , } , } , } as any ) ; const legacyMetadata = injectedMetadata . setup ( ) . getLegacyMetadata ( ) ; expect ( legacyMetadata ) . toEqual ( { foo : true , } ) ; expect ( ( ) => { legacyMetadata . foo = false ; } ) . toThrowError ( ) ; } ) ; } ) ; describe ( 'setup.getInjectedVar()' , ( ) => { it ( 'returns values from injectedMetadata.vars' , ( ) => { const setup = new InjectedMetadataService ( { injectedMetadata : { vars : { foo : { bar : '1' , } , 'baz:box' : { foo : 2 , } , } , } , } as any ) . setup ( ) ; expect ( setup . getInjectedVar ( 'foo' ) ) . toEqual ( { bar : '1' , } ) ; expect ( setup . getInjectedVar ( 'foo.bar' ) ) . toBe ( '1' ) ; expect ( setup . getInjectedVar ( 'baz:box' ) ) . toEqual ( { foo : 2 , } ) ; expect ( setup . getInjectedVar ( '' ) ) . toBe ( undefined ) ; } ) ; it ( 'returns read-only values' , ( ) => { const setup = new InjectedMetadataService ( { injectedMetadata : { vars : { foo : { bar : 1 , } , } , } , } as any ) . setup ( ) ; const foo = setup . getInjectedVar ( 'foo' ) ; expect ( ( ) => { foo . bar = 2 ; } ) . toThrowErrorMatchingInlineSnapshot ( `"Cannot assign to read only property 'bar' of object '#<Object>'"` ) ; expect ( ( ) => { foo . newProp = 2 ; } ) . toThrowErrorMatchingInlineSnapshot ( `"Cannot add property newProp, object is not extensible"` ) ; } ) ; } ) ; describe ( 'setup.getInjectedVars()' , ( ) => { it ( 'returns all injected vars, readonly' , ( ) => { const setup = new InjectedMetadataService ( { injectedMetadata : { vars : { foo : { bar : 1 , } , } , } , } as any ) . setup ( ) ; const vars = setup . getInjectedVars ( ) ; expect ( ( ) => { vars . foo = 2 ; } ) . toThrowErrorMatchingInlineSnapshot ( `"Cannot assign to read only property 'foo' of object '#<Object>'"` ) ; expect ( ( ) => { vars . newProp = 2 ; } ) . toThrowErrorMatchingInlineSnapshot ( `"Cannot add property newProp, object is not extensible"` ) ; } ) ; } ) ; describe ( 'setup.getBranding()' , ( ) => { it ( 'returns injectedMetadata.branding' , ( ) => { const injectedMetadata = new InjectedMetadataService ( { injectedMetadata : { branding : { fullLogoUrl : '/' , logoUrl : '/' , title : 'title' } , } , } as any ) ; const branding = injectedMetadata . setup ( ) . getBranding ( ) ; expect ( branding ) . toEqual ( { fullLogoUrl : '/' , logoUrl : '/' , title : 'title' } ) ; } ) ; } ) ;