import { CommandClasses } from "@zwave-js/core" ; import type { Driver } from "../driver/Driver" ; import { createEmptyMockDriver } from "../test/mocks" ; import { BasicCCGet , BasicCCReport , BasicCCSet , BasicCommand } from "./BasicCC" ; import type { CommandClass } from "./CommandClass" ; import { isEncapsulatingCommandClass } from "./EncapsulatingCommandClass" ; import { MultiChannelCC , MultiChannelCCAggregatedMembersGet , MultiChannelCCCapabilityGet , MultiChannelCCCommandEncapsulation , MultiChannelCCEndPointFind , MultiChannelCCEndPointGet , MultiChannelCCV1CommandEncapsulation , MultiChannelCommand , } from "./MultiChannelCC" ; import { MultiCommandCC } from "./MultiCommandCC" ; const fakeDriver = createEmptyMockDriver ( ) as unknown as Driver ; function buildCCBuffer ( payload ) { return Buffer . concat ( [ Buffer . from ( [ CommandClasses [ "Multi Channel" ] , ] ) , payload , ] ) ; } describe ( "lib/commandclass/MultiChannelCC" , ( ) => { describe ( "class MultiChannelCC" , ( ) => { it ( "is an encapsulating CommandClass" , ( ) => { let cc = new BasicCCSet ( fakeDriver , { nodeId : 1 , targetValue : 50 } ) ; cc = MultiChannelCC . encapsulate ( fakeDriver , cc ) ; expect ( isEncapsulatingCommandClass ( cc ) ) . toBeTrue ( ) ; } ) ; } ) ; it ( "the EndPointGet command should serialize correctly" , ( ) => { const cc = new MultiChannelCCEndPointGet ( fakeDriver , { nodeId : 1 } ) ; const expected = buildCCBuffer ( Buffer . from ( [ MultiChannelCommand . EndPointGet , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the CapabilityGet command should serialize correctly" , ( ) => { const cc = new MultiChannelCCCapabilityGet ( fakeDriver , { nodeId : 2 , requestedEndpoint : 7 } ) ; const expected = buildCCBuffer ( Buffer . from ( [ MultiChannelCommand . CapabilityGet , 7 , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the EndPointFind command should serialize correctly" , ( ) => { const cc = new MultiChannelCCEndPointFind ( fakeDriver , { nodeId : 2 , genericClass : 0x01 , specificClass : 0x02 } ) ; const expected = buildCCBuffer ( Buffer . from ( [ MultiChannelCommand . EndPointFind , 0x01 , 0x02 , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the CommandEncapsulation command should serialize correctly" , ( ) => { let cc = new BasicCCSet ( fakeDriver , { nodeId : 2 , targetValue : 5 , endpoint : 7 } ) ; cc = MultiChannelCC . encapsulate ( fakeDriver , cc ) ; const expected = buildCCBuffer ( Buffer . from ( [ MultiChannelCommand . CommandEncapsulation , 0 , 7 , CommandClasses . Basic , BasicCommand . Set , 5 , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the AggregatedMembersGet command should serialize correctly" , ( ) => { const cc = new MultiChannelCCAggregatedMembersGet ( fakeDriver , { nodeId : 2 , requestedEndpoint : 6 } ) ; const expected = buildCCBuffer ( Buffer . from ( [ MultiChannelCommand . AggregatedMembersGet , 6 , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the CommandEncapsulation command should also accept V1CommandEncapsulation as a response" , ( ) => { const sent = new MultiChannelCCCommandEncapsulation ( fakeDriver , { nodeId : 2 , destination : 2 , encapsulated : new BasicCCGet ( fakeDriver , { nodeId : 2 } ) } ) ; const received = new MultiChannelCCV1CommandEncapsulation ( fakeDriver , { nodeId : 2 , encapsulated : new BasicCCReport ( fakeDriver , { nodeId : 2 , currentValue : 50 } ) } ) ; received . endpointIndex = sent . destination as any ; expect ( sent . isExpectedCCResponse ( received ) ) . toBeTrue ( ) ; } ) ; it ( "deserializing an unsupported command should return an unspecified version of MultiChannelCC" , ( ) => { const serializedCC = buildCCBuffer ( Buffer . from ( [ 255 ] ) ) ; const cc = new MultiChannelCC ( fakeDriver , { nodeId : 1 , data : serializedCC } ) ; expect ( cc . constructor ) . toBe ( MultiChannelCC ) ; } ) ; describe ( "responses should be detected correctly" , ( ) => { it ( "MultiChannelCC/BasicCCGet should expect a response" , ( ) => { const ccRequest = MultiChannelCC . encapsulate ( fakeDriver , new BasicCCGet ( fakeDriver , { nodeId : 2 , endpoint : 2 } ) ) ; expect ( ccRequest . expectsCCResponse ( ) ) . toBeTrue ( ) ; } ) ; it ( "MultiChannelCC/BasicCCGet (multicast) should expect NO response" , ( ) => { const ccRequest = MultiChannelCC . encapsulate ( fakeDriver , new BasicCCGet ( fakeDriver , { nodeId : 2 , endpoint : 2 } ) ) ; ccRequest . destination = [ 1 , 2 , 3 ] ; expect ( ccRequest . expectsCCResponse ( ) ) . toBeFalse ( ) ; } ) ; it ( "MultiChannelCC/BasicCCSet should expect NO response" , ( ) => { const ccRequest = MultiChannelCC . encapsulate ( fakeDriver , new BasicCCSet ( fakeDriver , { nodeId : 2 , endpoint : 2 , targetValue : 7 } ) ) ; expect ( ccRequest . expectsCCResponse ( ) ) . toBeFalse ( ) ; } ) ; it ( "MultiChannelCC/BasicCCGet => MultiChannelCC/BasicCCReport = expected" , ( ) => { const ccRequest = MultiChannelCC . encapsulate ( fakeDriver , new BasicCCGet ( fakeDriver , { nodeId : 2 , endpoint : 2 } ) ) ; const ccResponse = MultiChannelCC . encapsulate ( fakeDriver , new BasicCCReport ( fakeDriver , { nodeId : ccRequest . nodeId , currentValue : 7 } ) ) ; ccResponse . endpointIndex = 2 ; expect ( ccRequest . isExpectedCCResponse ( ccResponse ) ) . toBeTrue ( ) ; } ) ; it ( "MultiChannelCC/BasicCCGet => MultiChannelCC/BasicCCGet = unexpected" , ( ) => { const ccRequest = MultiChannelCC . encapsulate ( fakeDriver , new BasicCCGet ( fakeDriver , { nodeId : 2 , endpoint : 2 } ) ) ; const ccResponse = MultiChannelCC . encapsulate ( fakeDriver , new BasicCCGet ( fakeDriver , { nodeId : ccRequest . nodeId , endpoint : 2 } ) ) ; ccResponse . endpointIndex = 2 ; expect ( ccRequest . isExpectedCCResponse ( ccResponse ) ) . toBeFalse ( ) ; } ) ; it ( "MultiChannelCC/BasicCCGet => MultiCommandCC/BasicCCReport = unexpected" , ( ) => { const ccRequest = MultiChannelCC . encapsulate ( fakeDriver , new BasicCCGet ( fakeDriver , { nodeId : 2 , endpoint : 2 } ) ) ; const ccResponse = MultiCommandCC . encapsulate ( fakeDriver , [ new BasicCCReport ( fakeDriver , { nodeId : ccRequest . nodeId , currentValue : 7 } ) , ] ) ; ccResponse . endpointIndex = 2 ; expect ( ccRequest . isExpectedCCResponse ( ccResponse ) ) . toBeFalse ( ) ; } ) ; } ) ; } ) ;