import * as assert from "assert" ; import { getResourcesInfo , IGotoResourcesArgs , IJsonResourceInfo , ParentOrChildCodeLens , sortArray } from "../extension.bundle" ; import { IPartialDeploymentTemplate } from "./support/diagnostics" ; import { parseTemplate } from "./support/parseTemplate" ; suite ( "ParentAndChildCodeLenses" , ( ) => { type Expected = [ string , string , number [ ] | undefined ] ; function createTest ( name , template , expected ) { test ( name , async ( ) => { const dt = parseTemplate ( template , [ ] , { ignoreInfos : true } ) ; const lenses = dt . getCodeLenses ( undefined ) . filter ( cl => cl instanceof ParentOrChildCodeLens ) as ParentOrChildCodeLens [ ] ; const allInfos = [ ] ; for ( const scope of dt . allScopes ) { const infos = getResourcesInfo ( { scope , recognizeDecoupledChildren : false } ) ; allInfos . push ( ... infos ) ; } const actual = [ ] ; for ( const lens of lenses ) { await lens . resolve ( ) ; const enclosingResources = allInfos . filter ( info => info . resourceObject . span . startIndex <= lens . span . startIndex && info . resourceObject . span . endIndex >= lens . span . endIndex ) ; const res = sortArray ( enclosingResources , info => info . resourceObject . startIndex , { descending : true } ) [ 0 ] ; assert ( res , "Could not find a resource at the location of the code lens" ) ; const targetStartLines = lens . command ?. arguments ? ( < IGotoResourcesArgs > lens . command . arguments [ 0 ] ) . targets . map ( loc => loc . range . start . line ) : undefined ; actual . push ( [ res . getFriendlyNameExpression ( { } ) ?? '' , lens . command ?. title ?? '' , targetStartLines ] ) ; } assert . deepStrictEqual ( actual , expected ) ; } ) ; } createTest ( "single resource" , { resources : [ { name : "self" , type : "microsoft.abc/def" } ] } , [ ] ) ; createTest ( "single resource on single line" , { resources : [ { name : "self" , type : "microsoft.abc/def" } ] } , [ ] ) ; suite ( "nested children" , ( ) => { createTest ( "single nested child" , { resources : [ { name : "parent" , type : "microsoft.abc/def" , resources : [ { name : "parent/child" , type : "microsoft.abc/def/ghi" } ] } ] } , [ [ "parent" , "1 child: child (ghi)" , [ 6 ] ] , [ "child" , "Parent: parent (def)" , [ 2 ] ] , ] ) ; createTest ( "nested and decoupled child" , { resources : [ { name : "parent/child2" , type : "microsoft.abc/def/ghi" } , { name : "parent" , type : "microsoft.abc/def" , resources : [ { name : "parent/child1" , type : "microsoft.abc/def/jkl" } ] } ] } , [ [ "child2" , "Parent: parent (def)" , [ 6 ] ] , [ "parent" , "2 children: child1 (jkl), child2 (ghi)" , [ 10 , 2 ] ] , [ "child1" , "Parent: parent (def)" , [ 6 ] ] , ] ) ; } ) ; createTest ( "nested template" , { resources : [ { type : "Microsoft.Resources/deployments" , apiVersion : "2019-10-01" , name : "inner1" , properties : { expressionEvaluationOptions : { scope : "inner" } , mode : "Incremental" , template : { resources : [ { name : "virtualNetwork1" , type : "Microsoft.Network/virtualNetworks" , tags : { displayName : "virtualNetwork1" } , properties : { subnets : [ { name : "Subnet-1" } , { name : "Subnet-2" } ] } } ] } } } , { type : "Microsoft.Resources/deployments" , apiVersion : "2019-10-01" , name : "outer1" , properties : { expressionEvaluationOptions : { scope : "outer" } , mode : "Incremental" , template : { resources : [ { name : "virtualNetwork1" , type : "Microsoft.Network/virtualNetworks" , tags : { displayName : "virtualNetwork1" } , properties : { subnets : [ { name : "Subnet-1" } , { name : "Subnet-2" } ] } } ] } } } , { condition : "[equals(parameters('vnet-new-or-existing'), 'existing')]" , apiVersion : "2019-11-01" , type : "Microsoft.Network/virtualNetworks/subnets" , name : "[concat(parameters('vnet-name'), '/', variables('bastion-subnet-name'))]" , } , { condition : "[equals(parameters('vnet-new-or-existing'), 'new')]" , apiVersion : "2019-11-01" , name : "[parameters('vnet-name')]" , type : "Microsoft.Network/virtualNetworks" , properties : { subnets : [ { name : "[variables('bastion-subnet-name')]" } ] } } ] , parameters : { "vnet-new-or-existing" : { } , "vnet-name" : { } } , variables : { "bastion-subnet-name" : "" } } , [ [ "${bastion-subnet-name}" , "Parent: ${vnet-name} (virtualNetworks)" , [ 72 ] ] , [ "${vnet-name}" , "2 children: ${bastion-subnet-name} (subnets), ${bastion-subnet-name} (subnets)" , [ 79 , 66 ] ] , [ "${bastion-subnet-name}" , "Parent: ${vnet-name} (virtualNetworks)" , [ 72 ] ] , [ "virtualNetwork1" , "2 children: Subnet-1 (subnets), Subnet-2 (subnets)" , [ 21 , 24 ] ] , [ "Subnet-1" , "Parent: virtualNetwork1 (virtualNetworks)" , [ 13 ] ] , [ "Subnet-2" , "Parent: virtualNetwork1 (virtualNetworks)" , [ 13 ] ] , [ "virtualNetwork1" , "2 children: Subnet-1 (subnets), Subnet-2 (subnets)" , [ 53 , 56 ] ] , [ "Subnet-1" , "Parent: virtualNetwork1 (virtualNetworks)" , [ 45 ] ] , [ "Subnet-2" , "Parent: virtualNetwork1 (virtualNetworks)" , [ 45 ] ] ] ) ; } ) ;