import * as vscode from 'vscode' ; import * as chai from 'chai' ; import * as sinon from 'sinon' ; import * as sinonChai from 'sinon-chai' ; import { ExtensionUtil } from '../../extension/util/ExtensionUtil' ; import { TestUtil } from '../TestUtil' ; import { TemporaryCommandRegistry } from '../../extension/dependencies/TemporaryCommandRegistry' ; import { ExtensionCommands } from '../../ExtensionCommands' ; chai . should ( ) ; chai . use ( sinonChai ) ; const should = chai . should ( ) ; describe ( 'TemporaryCommandRegistry Tests' , ( ) => { const mySandBox = sinon . createSandbox ( ) ; before ( async ( ) => { await TestUtil . setupTests ( mySandBox ) ; } ) ; afterEach ( ( ) => { mySandBox . restore ( ) ; } ) ; it ( 'should delay execution of commands' , async ( ) => { const numberOfCommands = ExtensionUtil . getPackageJSON ( ) . actualActivationEvents . onCommand . length ; const registerCommandStub = mySandBox . stub ( vscode . commands , 'registerCommand' ) ; registerCommandStub . returns ( new vscode . Disposable ( ( ) : void => { return ; } ) ) ; registerCommandStub . yields ( undefined ) ; const tempRegistry = TemporaryCommandRegistry . instance ( ) ; tempRegistry . createTempCommands ( true ) ; tempRegistry [ 'delayExecution' ] . should . equal ( true ) ; should . not . exist ( tempRegistry [ 'alternativeCommand' ] ) ; registerCommandStub . callCount . should . equal ( numberOfCommands - 4 ) ; tempRegistry [ 'tempCommands' ] . length . should . deep . equal ( numberOfCommands - 4 ) ; tempRegistry . restoreCommands ( ) ; registerCommandStub . should . not . have . been . calledWith ( ExtensionCommands . OPEN_PRE_REQ_PAGE ) ; tempRegistry [ 'delayedCommandsToExecute' ] . size . should . equal ( numberOfCommands - 4 ) ; tempRegistry [ 'tempCommands' ] . should . deep . equal ( [ ] ) ; const executeStub = mySandBox . stub ( vscode . commands , 'executeCommand' ) . resolves ( ) ; await tempRegistry . executeStoredCommands ( ) ; executeStub . callCount . should . equal ( numberOfCommands - 4 ) ; tempRegistry [ 'delayedCommandsToExecute' ] . size . should . equal ( 0 ) ; } ) ; it ( 'should not delay execution of commands' , async ( ) => { const numberOfCommands = ExtensionUtil . getPackageJSON ( ) . actualActivationEvents . onCommand . length ; const registerCommandStub = mySandBox . stub ( vscode . commands , 'registerCommand' ) ; registerCommandStub . returns ( new vscode . Disposable ( ( ) : void => { return ; } ) ) ; registerCommandStub . yields ( undefined ) ; const tempRegistry = TemporaryCommandRegistry . instance ( ) ; const executeStub = mySandBox . stub ( vscode . commands , 'executeCommand' ) . resolves ( ) ; tempRegistry . createTempCommands ( false , 'some.otherCommand' ) ; tempRegistry [ 'delayExecution' ] . should . equal ( false ) ; tempRegistry [ 'alternativeCommand' ] . should . equal ( 'some.otherCommand' ) ; registerCommandStub . callCount . should . equal ( numberOfCommands - 4 ) ; tempRegistry [ 'tempCommands' ] . length . should . deep . equal ( numberOfCommands - 4 ) ; executeStub . callCount . should . equal ( numberOfCommands - 4 ) ; executeStub . should . have . been . calledWith ( 'some.otherCommand' ) ; executeStub . reset ( ) ; tempRegistry . restoreCommands ( ) ; registerCommandStub . should . not . have . been . calledWith ( ExtensionCommands . OPEN_PRE_REQ_PAGE ) ; tempRegistry [ 'delayedCommandsToExecute' ] . size . should . equal ( 0 ) ; tempRegistry [ 'tempCommands' ] . should . deep . equal ( [ ] ) ; executeStub . callCount . should . equal ( 0 ) ; executeStub . reset ( ) ; await tempRegistry . executeStoredCommands ( ) ; executeStub . callCount . should . equal ( 0 ) ; tempRegistry [ 'delayedCommandsToExecute' ] . size . should . equal ( 0 ) ; } ) ; } ) ;