import type { CoreSetup , CoreStart } from 'kibana/server' ; import { coreMock } from 'src/core/server/mocks' ; import type { ReportingCore , ReportingInternalStart } from './core' ; import { LevelLogger } from './lib' ; import { ReportingPlugin } from './plugin' ; import { createMockConfigSchema , createMockLevelLogger , createMockPluginSetup , createMockPluginStart , } from './test_helpers' ; import type { ReportingSetupDeps } from './types' ; const sleep = ( time ) => new Promise ( ( r ) => setTimeout ( r , time ) ) ; describe ( 'Reporting Plugin' , ( ) => { let configSchema ; let initContext ; let coreSetup ; let coreStart ; let pluginSetup ; let pluginStart ; let logger ; let plugin ; beforeEach ( async ( ) => { configSchema = createMockConfigSchema ( ) ; initContext = coreMock . createPluginInitializerContext ( configSchema ) ; coreSetup = coreMock . createSetup ( configSchema ) ; coreStart = coreMock . createStart ( ) ; pluginSetup = createMockPluginSetup ( { } ) as unknown as ReportingSetupDeps ; pluginStart = await createMockPluginStart ( coreStart , configSchema ) ; logger = createMockLevelLogger ( ) ; plugin = new ReportingPlugin ( initContext ) ; ( plugin as unknown as { logger : LevelLogger ; } ) . logger = logger ; } ) ; it ( 'has a sync setup process' , ( ) => { expect ( plugin . setup ( coreSetup , pluginSetup ) ) . not . toHaveProperty ( 'then' ) ; } ) ; it ( 'has a sync startup process' , async ( ) => { plugin . setup ( coreSetup , pluginSetup ) ; await sleep ( 5 ) ; expect ( plugin . start ( coreStart , pluginStart ) ) . not . toHaveProperty ( 'then' ) ; } ) ; it ( 'registers an advanced setting for PDF logos' , async ( ) => { plugin . setup ( coreSetup , pluginSetup ) ; expect ( coreSetup . uiSettings . register ) . toHaveBeenCalled ( ) ; expect ( ( coreSetup . uiSettings . register as jest . Mock ) . mock . calls [ 0 ] [ 0 ] ) . toHaveProperty ( 'xpackReporting:customPdfLogo' ) ; } ) ; it ( 'logs start issues' , async ( ) => { plugin . setup ( coreSetup , pluginSetup ) ; await new Promise ( setImmediate ) ; const reportingCore = ( plugin as unknown as { reportingCore : ReportingCore ; } ) . reportingCore ; reportingCore . pluginStart = jest . fn ( ) . mockRejectedValueOnce ( 'silly' ) ; plugin . start ( coreStart , pluginStart ) ; await new Promise ( setImmediate ) ; expect ( logger . error . mock . calls . map ( ( [ message ] ) => message ) ) . toMatchInlineSnapshot ( `
      Array [
        "Error in Reporting start, reporting may not function properly",
        "silly",
      ]
    ` ) ; expect ( logger . error ) . toHaveBeenCalledTimes ( 2 ) ; } ) ; } ) ;