import { act } from 'react-dom/test-utils' ; import type { MlAction } from '../../../common/types' ; import { setupEnvironment } from '../helpers' ; import { ElasticsearchTestBed , setupElasticsearchPage } from './es_deprecations.helpers' ; import { esDeprecationsMockResponse , MOCK_SNAPSHOT_ID , MOCK_JOB_ID } from './mocked_responses' ; describe ( 'Machine learning deprecation flyout' , ( ) => { let testBed ; const { server , httpRequestsMockHelpers } = setupEnvironment ( ) ; const mlDeprecation = esDeprecationsMockResponse . deprecations [ 0 ] ; afterAll ( ( ) => { server . restore ( ) ; } ) ; beforeEach ( async ( ) => { httpRequestsMockHelpers . setLoadEsDeprecationsResponse ( esDeprecationsMockResponse ) ; httpRequestsMockHelpers . setLoadMlUpgradeModeResponse ( { mlUpgradeModeEnabled : false } ) ; httpRequestsMockHelpers . setUpgradeMlSnapshotStatusResponse ( { nodeId : 'my_node' , snapshotId : MOCK_SNAPSHOT_ID , jobId : MOCK_JOB_ID , status : 'idle' , } ) ; httpRequestsMockHelpers . setReindexStatusResponse ( { reindexOp : null , warnings : [ ] , hasRequiredPrivileges : true , meta : { indexName : 'foo' , reindexName : 'reindexed-foo' , aliases : [ ] , } , } ) ; await act ( async ( ) => { testBed = await setupElasticsearchPage ( { isReadOnlyMode : false } ) ; } ) ; const { actions , component } = testBed ; component . update ( ) ; await actions . table . clickDeprecationRowAt ( 'mlSnapshot' , 0 ) ; } ) ; test ( 'renders a flyout with deprecation details' , async ( ) => { const { find , exists } = testBed ; expect ( exists ( 'mlSnapshotDetails' ) ) . toBe ( true ) ; expect ( find ( 'mlSnapshotDetails.flyoutTitle' ) . text ( ) ) . toContain ( 'Upgrade or delete model snapshot' ) ; expect ( find ( 'mlSnapshotDetails.documentationLink' ) . props ( ) . href ) . toBe ( mlDeprecation . url ) ; } ) ; describe ( 'upgrade snapshots' , ( ) => { it ( 'successfully upgrades snapshots' , async ( ) => { const { find , actions , exists } = testBed ; httpRequestsMockHelpers . setUpgradeMlSnapshotResponse ( { nodeId : 'my_node' , snapshotId : MOCK_SNAPSHOT_ID , jobId : MOCK_JOB_ID , status : 'in_progress' , } ) ; httpRequestsMockHelpers . setUpgradeMlSnapshotStatusResponse ( { nodeId : 'my_node' , snapshotId : MOCK_SNAPSHOT_ID , jobId : MOCK_JOB_ID , status : 'complete' , } ) ; expect ( exists ( 'mlSnapshotDetails.criticalDeprecationBadge' ) ) . toBe ( true ) ; expect ( find ( 'mlSnapshotDetails.upgradeSnapshotButton' ) . text ( ) ) . toEqual ( 'Upgrade' ) ; await actions . mlDeprecationFlyout . clickUpgradeSnapshot ( ) ; const upgradeRequest = server . requests [ server . requests . length - 2 ] ; expect ( upgradeRequest . method ) . toBe ( 'POST' ) ; expect ( upgradeRequest . url ) . toBe ( '/api/upgrade_assistant/ml_snapshots' ) ; const statusRequest = server . requests [ server . requests . length - 1 ] ; expect ( statusRequest . method ) . toBe ( 'GET' ) ; expect ( statusRequest . url ) . toBe ( `/api/upgrade_assistant/ml_snapshots/${ MOCK_JOB_ID }/${ MOCK_SNAPSHOT_ID }` ) ; expect ( find ( 'mlActionResolutionCell' ) . text ( ) ) . toContain ( 'Upgrade complete' ) ; await actions . table . clickDeprecationRowAt ( 'mlSnapshot' , 0 ) ; expect ( exists ( 'mlSnapshotDetails.upgradeSnapshotButton' ) ) . toBe ( false ) ; expect ( exists ( 'mlSnapshotDetails.deleteSnapshotButton' ) ) . toBe ( false ) ; expect ( exists ( 'mlSnapshotDetails.resolvedDeprecationBadge' ) ) . toBe ( true ) ; } ) ; it ( 'handles upgrade failure' , async ( ) => { const { find , actions } = testBed ; const error = { statusCode : 500 , error : 'Upgrade snapshot error' , message : 'Upgrade snapshot error' , } ; httpRequestsMockHelpers . setUpgradeMlSnapshotResponse ( undefined , error ) ; httpRequestsMockHelpers . setUpgradeMlSnapshotStatusResponse ( { nodeId : 'my_node' , snapshotId : MOCK_SNAPSHOT_ID , jobId : MOCK_JOB_ID , status : 'error' , error , } ) ; await actions . mlDeprecationFlyout . clickUpgradeSnapshot ( ) ; const upgradeRequest = server . requests [ server . requests . length - 1 ] ; expect ( upgradeRequest . method ) . toBe ( 'POST' ) ; expect ( upgradeRequest . url ) . toBe ( '/api/upgrade_assistant/ml_snapshots' ) ; expect ( find ( 'mlActionResolutionCell' ) . text ( ) ) . toContain ( 'Upgrade failed' ) ; await actions . table . clickDeprecationRowAt ( 'mlSnapshot' , 0 ) ; expect ( find ( 'mlSnapshotDetails.resolveSnapshotError' ) . text ( ) ) . toContain ( 'Error upgrading snapshot' ) ; expect ( find ( 'mlSnapshotDetails.upgradeSnapshotButton' ) . text ( ) ) . toEqual ( 'Retry upgrade' ) ; } ) ; it ( 'Disables actions if ml_upgrade_mode is enabled' , async ( ) => { httpRequestsMockHelpers . setLoadMlUpgradeModeResponse ( { mlUpgradeModeEnabled : true } ) ; await act ( async ( ) => { testBed = await setupElasticsearchPage ( { isReadOnlyMode : false } ) ; } ) ; const { actions , exists , component } = testBed ; component . update ( ) ; await actions . table . clickDeprecationRowAt ( 'mlSnapshot' , 0 ) ; expect ( exists ( 'mlSnapshotDetails.mlUpgradeModeEnabledError' ) ) . toBe ( true ) ; expect ( exists ( 'mlSnapshotDetails.setUpgradeModeDocsLink' ) ) . toBe ( true ) ; expect ( exists ( 'mlSnapshotDetails.upgradeSnapshotButton' ) ) . toBe ( false ) ; expect ( exists ( 'mlSnapshotDetails.deleteSnapshotButton' ) ) . toBe ( false ) ; } ) ; } ) ; describe ( 'delete snapshots' , ( ) => { it ( 'successfully deletes snapshots' , async ( ) => { const { find , actions , exists } = testBed ; httpRequestsMockHelpers . setDeleteMlSnapshotResponse ( { acknowledged : true , } ) ; expect ( exists ( 'mlSnapshotDetails.criticalDeprecationBadge' ) ) . toBe ( true ) ; expect ( find ( 'mlSnapshotDetails.deleteSnapshotButton' ) . text ( ) ) . toEqual ( 'Delete' ) ; await actions . mlDeprecationFlyout . clickDeleteSnapshot ( ) ; const request = server . requests [ server . requests . length - 1 ] ; expect ( request . method ) . toBe ( 'DELETE' ) ; expect ( request . url ) . toBe ( `/api/upgrade_assistant/ml_snapshots/${ ( mlDeprecation . correctiveAction ! as MlAction ) . jobId }/${ ( mlDeprecation . correctiveAction ! as MlAction ) . snapshotId }` ) ; expect ( find ( 'mlActionResolutionCell' ) . at ( 0 ) . text ( ) ) . toEqual ( 'Deletion complete' ) ; await actions . table . clickDeprecationRowAt ( 'mlSnapshot' , 0 ) ; expect ( exists ( 'mlSnapshotDetails.upgradeSnapshotButton' ) ) . toBe ( false ) ; expect ( exists ( 'mlSnapshotDetails.deleteSnapshotButton' ) ) . toBe ( false ) ; expect ( exists ( 'mlSnapshotDetails.resolvedDeprecationBadge' ) ) . toBe ( true ) ; } ) ; it ( 'handles delete failure' , async ( ) => { const { find , actions } = testBed ; const error = { statusCode : 500 , error : 'Upgrade snapshot error' , message : 'Upgrade snapshot error' , } ; httpRequestsMockHelpers . setDeleteMlSnapshotResponse ( undefined , error ) ; await actions . mlDeprecationFlyout . clickDeleteSnapshot ( ) ; const request = server . requests [ server . requests . length - 1 ] ; expect ( request . method ) . toBe ( 'DELETE' ) ; expect ( request . url ) . toBe ( `/api/upgrade_assistant/ml_snapshots/${ ( mlDeprecation . correctiveAction ! as MlAction ) . jobId }/${ ( mlDeprecation . correctiveAction ! as MlAction ) . snapshotId }` ) ; expect ( find ( 'mlActionResolutionCell' ) . at ( 0 ) . text ( ) ) . toEqual ( 'Deletion failed' ) ; await actions . table . clickDeprecationRowAt ( 'mlSnapshot' , 0 ) ; expect ( find ( 'mlSnapshotDetails.resolveSnapshotError' ) . text ( ) ) . toContain ( 'Error deleting snapshot' ) ; expect ( find ( 'mlSnapshotDetails.deleteSnapshotButton' ) . text ( ) ) . toEqual ( 'Retry delete' ) ; } ) ; } ) ; } ) ;