import * as assert from 'assert' ; import * as fse from 'fs-extra' ; import * as path from 'path' ; import { Uri } from 'vscode' ; import { createTestActionContext , runWithTestActionContext } from 'vscode-azureextensiondev' ; import { AzExtTreeItem } from 'vscode-azureextensionui' ; import { addBinding , createNewProjectInternal , ext , getRandomHexString , IFunctionBinding , IFunctionJson , ProjectLanguage } from '../extension.bundle' ; import { cleanTestWorkspace , getTestWorkspaceFolder } from './global.test' ; suite ( 'Add Binding' , ( ) => { let functionJsonPath ; const functionName = 'HttpTriggerTest' ; let initialBindingsCount ; suiteSetup ( async ( ) => { await cleanTestWorkspace ( ) ; const testWorkspacePath = getTestWorkspaceFolder ( ) ; await runWithTestActionContext ( 'createNewProject' , async ( context ) => { await context . ui . runWithInputs ( [ testWorkspacePath , ProjectLanguage . JavaScript , /http\s*trigger/i , functionName , 'Anonymous' ] , async ( ) => { await createNewProjectInternal ( context , { } ) ; } ) ; } ) ; functionJsonPath = path . join ( testWorkspacePath , functionName , 'function.json' ) ; assert . ok ( await fse . pathExists ( functionJsonPath ) , 'Failed to create project' ) ; initialBindingsCount = await getBindingsCount ( ) ; } ) ; suiteTeardown ( async ( ) => { const finalBindingsCount = await getBindingsCount ( ) ; assert . equal ( finalBindingsCount , initialBindingsCount + 3 , 'Not all expected bindings were added.' ) ; } ) ; test ( 'Command Palette' , async function ( this ) : Promise < void > { this . timeout ( 30 * 1000 ) ; const userInputs = [ functionName ] ; if ( ! await ext . azureAccountTreeItem . getIsLoggedIn ( ) ) { userInputs . unshift ( 'Local Project' ) ; } await validateAddBinding ( undefined , userInputs ) ; } ) ; test ( 'Uri' , async ( ) => { await validateAddBinding ( Uri . parse ( functionJsonPath ) , [ ] ) ; } ) ; test ( 'Tree' , async ( ) => { const treeItem = await ext . tree . findTreeItem ( `/localProject0/functions/${ functionName }` , await createTestActionContext ( ) ) ; assert . ok ( treeItem , 'Failed to find tree item' ) ; await validateAddBinding ( treeItem , [ ] ) ; } ) ; async function getBindingsCount ( ) { const data = < IFunctionJson > await fse . readJSON ( functionJsonPath ) ; return ( data . bindings || [ ] ) . length ; } async function validateAddBinding ( commandInput , userInputs ) { const bindingType = 'HTTP' ; const bindingDirection = 'out' ; const bindingName = 'binding' + getRandomHexString ( ) ; await runWithTestActionContext ( 'addBinding' , async ( context ) => { await context . ui . runWithInputs ( [ ... userInputs , bindingDirection , bindingType , bindingName ] , async ( ) => { await addBinding ( context , commandInput ) ; } ) ; } ) ; const data = < IFunctionJson > await fse . readJSON ( functionJsonPath ) ; const binding = data . bindings && data . bindings . find ( b => b . name === bindingName ) ; if ( ! binding ) { assert . fail ( `Failed to find binding "${ bindingName }".` ) ; } else { assert . equal ( binding . direction , bindingDirection ) ; assert . equal ( binding . type , bindingType . toLowerCase ( ) ) ; } } } ) ;