import "reflect-metadata" ; const rmock = require ( "mock-require" ) ; rmock ( "vscode" , { } ) ; import * as tsMockito from "ts-mockito" ; import { anything , verify , when } from "ts-mockito" ; import { CvsProviderProxy } from "../../../src/dal/cvsproviderproxy" ; import { RemoteRun } from "../../../src/bll/commands/remoterun" ; import { CustomPatchSender } from "../../../src/bll/remoterun/patchsender" ; import * as TypeMoq from "typemoq" ; import { IResourceProvider } from "../../../src/view/dataproviders/interfaces/iresourceprovider" ; import { IBuildProvider } from "../../../src/view/dataproviders/interfaces/ibuildprovider" ; import { IProviderManager } from "../../../src/view/iprovidermanager" ; import { PatchManager } from "../../../src/bll/utils/patchmanager" ; import { WindowProxy } from "../../../src/bll/moduleproxies/window-proxy" ; import { ChangeListStatus } from "../../../src/bll/utils/constants" ; import { ShowMyChanges } from "../../../src/bll/commands/showmychanges" ; suite ( "Run Remote Run" , ( ) => { const showMyChangesMock = tsMockito . mock ( ShowMyChanges ) ; when ( showMyChangesMock . exec ( ) ) . thenReturn ( Promise . resolve ( undefined ) ) ; when ( showMyChangesMock . exec ( anything ( ) ) ) . thenReturn ( Promise . resolve ( undefined ) ) ; const showMyChangesSpy = tsMockito . instance ( showMyChangesMock ) ; test ( "should verify we request getting source data and trigger output methods" , function ( done ) { const cvsProviderProxyMock = tsMockito . mock ( CvsProviderProxy ) ; const cvsProviderProxySpy = tsMockito . instance ( cvsProviderProxyMock ) ; const patchSenderMock = tsMockito . mock ( CustomPatchSender ) ; when ( patchSenderMock . sendPatch ( anything ( ) , anything ( ) , anything ( ) ) ) . thenReturn ( Promise . resolve ( [ ] ) ) ; when ( patchSenderMock . waitForChangeFinish ( anything ( ) ) ) . thenReturn ( Promise . resolve ( ChangeListStatus . CHECKED ) ) ; const patchSenderSpy = tsMockito . instance ( patchSenderMock ) ; const resourceProviderMock = TypeMoq . Mock . ofType < IResourceProvider > ( ) ; resourceProviderMock . setup ( ( foo ) => foo . getSelectedContent ( ) ) . returns ( ( ) => [ anything ( ) , anything ( ) ] ) ; const resourceProviderSpy = resourceProviderMock . object ; const buildProviderMock = TypeMoq . Mock . ofType < IBuildProvider > ( ) ; buildProviderMock . setup ( ( bar ) => bar . getSelectedContent ( ) ) . returns ( ( ) => [ anything ( ) , anything ( ) ] ) ; const buildProviderSpy = buildProviderMock . object ; const providerManagerMock = TypeMoq . Mock . ofType < IProviderManager > ( ) ; const providerManagerSpy = providerManagerMock . object ; const patchManagerMock = tsMockito . mock ( PatchManager ) ; const patchManagerSpy = tsMockito . instance ( patchManagerMock ) ; const windowMock = tsMockito . mock ( WindowProxy ) ; const windowSpy = tsMockito . instance ( windowMock ) ; const testableCommand = new RemoteRun ( cvsProviderProxySpy , buildProviderSpy , resourceProviderSpy , providerManagerSpy , patchSenderSpy , patchManagerSpy , windowSpy , showMyChangesSpy ) ; testableCommand . exec ( [ false ] ) . then ( ( ) => { verify ( patchManagerMock . preparePatch ( anything ( ) ) ) . called ( ) ; buildProviderMock . verify ( ( foo ) => foo . getSelectedContent ( ) , TypeMoq . Times . atLeastOnce ( ) ) ; tsMockito . verify ( patchSenderMock . sendPatch ( anything ( ) , anything ( ) , anything ( ) ) ) . called ( ) ; done ( ) ; } ) . catch ( ( err ) => { done ( "There is no reason for error: " + err ) ; } ) ; } ) ; test ( "should verify we reject calling sendPatch without suitable build configs" , function ( done ) { const cvsProviderProxyMock = tsMockito . mock ( CvsProviderProxy ) ; const cvsProviderProxySpy = tsMockito . instance ( cvsProviderProxyMock ) ; const resourceProviderMock = TypeMoq . Mock . ofType < IResourceProvider > ( ) ; const resourceProviderSpy = resourceProviderMock . object ; const buildProviderMock = TypeMoq . Mock . ofType < IBuildProvider > ( ) ; buildProviderMock . setup ( ( bar ) => bar . getSelectedContent ( ) ) . returns ( ( ) => [ ] ) ; const buildProviderSpy = buildProviderMock . object ; const patchSenderMock = tsMockito . mock ( CustomPatchSender ) ; when ( patchSenderMock . sendPatch ( anything ( ) , anything ( ) , anything ( ) ) ) . thenReturn ( Promise . resolve ( [ ] ) ) ; const patchSenderSpy = tsMockito . instance ( patchSenderMock ) ; const providerManagerMock = TypeMoq . Mock . ofType < IProviderManager > ( ) ; const providerManagerSpy = providerManagerMock . object ; const windowMock = tsMockito . mock ( WindowProxy ) ; const windowSpy = tsMockito . instance ( windowMock ) ; const testableCommand = new RemoteRun ( cvsProviderProxySpy , buildProviderSpy , resourceProviderSpy , providerManagerSpy , patchSenderSpy , undefined , windowSpy , showMyChangesSpy ) ; testableCommand . exec ( [ false ] ) . then ( ( ) => { done ( "There should be an error!" ) ; } ) . catch ( ( ) => { tsMockito . verify ( patchSenderMock . sendPatch ( anything ( ) , anything ( ) , anything ( ) ) ) . never ( ) ; done ( ) ; } ) ; } ) ; test ( "should verify we clean providers when data has been read" , function ( done ) { const cvsProviderProxyMock = tsMockito . mock ( CvsProviderProxy ) ; const cvsProviderProxySpy = tsMockito . instance ( cvsProviderProxyMock ) ; const resourceProviderMock = TypeMoq . Mock . ofType < IResourceProvider > ( ) ; resourceProviderMock . setup ( ( foo ) => foo . getSelectedContent ( ) ) . returns ( ( ) => [ anything ( ) , anything ( ) ] ) ; const resourceProviderSpy = resourceProviderMock . object ; const buildProviderMock = TypeMoq . Mock . ofType < IBuildProvider > ( ) ; buildProviderMock . setup ( ( bar ) => bar . getSelectedContent ( ) ) . returns ( ( ) => [ anything ( ) , anything ( ) ] ) ; const buildProviderSpy = buildProviderMock . object ; const patchSenderMock = tsMockito . mock ( CustomPatchSender ) ; when ( patchSenderMock . sendPatch ( anything ( ) , anything ( ) , anything ( ) ) ) . thenReturn ( Promise . resolve ( [ ] ) ) ; when ( patchSenderMock . waitForChangeFinish ( anything ( ) ) ) . thenReturn ( Promise . resolve ( ChangeListStatus . CHECKED ) ) ; const patchSenderSpy = tsMockito . instance ( patchSenderMock ) ; const providerManagerMock = TypeMoq . Mock . ofType < IProviderManager > ( ) ; const providerManagerSpy = providerManagerMock . object ; const patchManagerMock = tsMockito . mock ( PatchManager ) ; const patchManagerSpy = tsMockito . instance ( patchManagerMock ) ; const windowMock = tsMockito . mock ( WindowProxy ) ; const windowSpy = tsMockito . instance ( windowMock ) ; const testableCommand = new RemoteRun ( cvsProviderProxySpy , buildProviderSpy , resourceProviderSpy , providerManagerSpy , patchSenderSpy , patchManagerSpy , windowSpy , showMyChangesSpy ) ; testableCommand . exec ( [ false ] ) . then ( ( ) => { buildProviderMock . verify ( ( foo ) => foo . resetTreeContent ( ) , TypeMoq . Times . atLeastOnce ( ) ) ; resourceProviderMock . verify ( ( foo ) => foo . resetTreeContent ( ) , TypeMoq . Times . atLeastOnce ( ) ) ; providerManagerMock . verify ( ( foo ) => foo . showChangesProvider ( ) , TypeMoq . Times . atLeastOnce ( ) ) ; done ( ) ; } ) . catch ( ( err ) => { done ( "There is no reason for error: " + err ) ; } ) ; } ) ; } ) ;