import * as sinon from "sinon" ; import * as moq from "typemoq" ; import { IModelDb , IModelJsNative } from "@itwin/core-backend" ; import { Id64sRulesetVariableJSON , NodeKeyJSON , RulesetVariableJSON , SetRulesetVariableParams , StringRulesetVariable , UnsetRulesetVariableParams , UpdateHierarchyStateParams , VariableValueTypes , } from "@itwin/presentation-common" ; import { createRandomBaseNodeKey , createRandomId } from "@itwin/presentation-common/lib/cjs/test" ; import { NativePlatformDefinition } from "../presentation-backend/NativePlatform" ; import { Presentation } from "../presentation-backend/Presentation" ; import { PresentationIpcHandler } from "../presentation-backend/PresentationIpcHandler" ; import { PresentationManager } from "../presentation-backend/PresentationManager" ; import { RulesetVariablesManager } from "../presentation-backend/RulesetVariablesManager" ; import { CompressedId64Set , OrderedId64Iterable } from "@itwin/core-bentley" ; describe ( "PresentationIpcHandler" , ( ) => { const presentationManagerMock = moq . Mock . ofType < PresentationManager > ( ) ; beforeEach ( ( ) => { presentationManagerMock . reset ( ) ; sinon . stub ( Presentation , "getManager" ) . returns ( presentationManagerMock . object ) ; } ) ; describe ( "setRulesetVariable" , ( ) => { const testRulesetId = "test-ruleset-id" ; const variablesManagerMock = moq . Mock . ofType < RulesetVariablesManager > ( ) ; beforeEach ( ( ) => { presentationManagerMock . setup ( ( x ) => x . vars ( testRulesetId ) ) . returns ( ( ) => variablesManagerMock . object ) ; } ) ; it ( "sets ruleset variable" , async ( ) => { const testVariable = { id : "var-id" , type : VariableValueTypes . String , value : "test-val" } ; variablesManagerMock . setup ( ( x ) => x . setValue ( testVariable . id , testVariable . type , testVariable . value ) ) . verifiable ( moq . Times . once ( ) ) ; const ipcHandler = new PresentationIpcHandler ( ) ; const params = { clientId : "test-client-id" , rulesetId : testRulesetId , variable : testVariable } ; await ipcHandler . setRulesetVariable ( params ) ; variablesManagerMock . verifyAll ( ) ; } ) ; it ( "decompresses ids set before setting value variables" , async ( ) => { const ids = OrderedId64Iterable . sortArray ( [ createRandomId ( ) , createRandomId ( ) ] ) ; const testVariable = { type : VariableValueTypes . Id64Array , id : "test" , value : CompressedId64Set . compressIds ( ids ) } ; variablesManagerMock . setup ( ( x ) => x . setValue ( testVariable . id , testVariable . type , ids ) ) . verifiable ( moq . Times . once ( ) ) ; const ipcHandler = new PresentationIpcHandler ( ) ; const params = { clientId : "test-client-id" , rulesetId : testRulesetId , variable : testVariable } ; await ipcHandler . setRulesetVariable ( params ) ; variablesManagerMock . verifyAll ( ) ; } ) ; } ) ; describe ( "unsetRulesetVariable" , ( ) => { const testRulesetId = "test-ruleset-id" ; const variablesManagerMock = moq . Mock . ofType < RulesetVariablesManager > ( ) ; beforeEach ( ( ) => { presentationManagerMock . setup ( ( x ) => x . vars ( testRulesetId ) ) . returns ( ( ) => variablesManagerMock . object ) ; } ) ; it ( "unsets ruleset variable" , async ( ) => { variablesManagerMock . setup ( ( x ) => x . unset ( "test-id" ) ) . verifiable ( moq . Times . once ( ) ) ; const ipcHandler = new PresentationIpcHandler ( ) ; const params = { clientId : "test-client-id" , rulesetId : testRulesetId , variableId : "test-id" } ; await ipcHandler . unsetRulesetVariable ( params ) ; variablesManagerMock . verifyAll ( ) ; } ) ; } ) ; describe ( "updateHierarchyState" , ( ) => { const testRulesetId = "test-ruleset-id" ; const nativeAddonMock = moq . Mock . ofType < NativePlatformDefinition > ( ) ; beforeEach ( ( ) => { nativeAddonMock . reset ( ) ; presentationManagerMock . setup ( ( x ) => x . getNativePlatform ( ) ) . returns ( ( ) => nativeAddonMock . object ) ; } ) ; it ( "does not call native platform's updateHierarchyState if imodelDb is not found" , async ( ) => { nativeAddonMock . setup ( ( x ) => x . updateHierarchyState ( moq . It . isAny ( ) , moq . It . isAny ( ) , moq . It . isAny ( ) , moq . It . isAny ( ) ) ) . verifiable ( moq . Times . never ( ) ) ; const ipcHandler = new PresentationIpcHandler ( ) ; const params = { clientId : "client-id" , rulesetId : testRulesetId , imodelKey : "imodel-key" , changeType : "nodesExpanded" , nodeKeys : [ ] } ; await ipcHandler . updateHierarchyState ( params ) ; nativeAddonMock . verifyAll ( ) ; } ) ; it ( "calls native platform's updateHierarchyState" , async ( ) => { const imodelDbMock = moq . Mock . ofType < IModelDb > ( ) ; const nativeDgnDbMock = moq . Mock . ofType < IModelJsNative . DgnDb > ( ) ; imodelDbMock . setup ( ( x ) => x . nativeDb ) . returns ( ( ) => nativeDgnDbMock . object ) ; sinon . stub ( IModelDb , "tryFindByKey" ) . returns ( imodelDbMock . object ) ; const nodeKey = createRandomBaseNodeKey ( ) ; nativeAddonMock . setup ( ( x ) => x . updateHierarchyState ( nativeDgnDbMock . object , testRulesetId , "nodesExpanded" , JSON . stringify ( [ nodeKey ] ) ) ) . verifiable ( moq . Times . once ( ) ) ; const ipcHandler = new PresentationIpcHandler ( ) ; const params = { clientId : "client-id" , rulesetId : testRulesetId , imodelKey : "imodel-key" , changeType : "nodesExpanded" , nodeKeys : [ nodeKey ] } ; await ipcHandler . updateHierarchyState ( params ) ; nativeAddonMock . verifyAll ( ) ; } ) ; } ) ; } ) ;