import { CommandClasses } from "@zwave-js/core" ; import type { Driver } from "../driver/Driver" ; import { createEmptyMockDriver } from "../test/mocks" ; import { MultiChannelAssociationCCGet , MultiChannelAssociationCCRemove , MultiChannelAssociationCCReport , MultiChannelAssociationCCSet , MultiChannelAssociationCCSupportedGroupingsGet , MultiChannelAssociationCCSupportedGroupingsReport , MultiChannelAssociationCommand , } from "./MultiChannelAssociationCC" ; const fakeDriver = createEmptyMockDriver ( ) as unknown as Driver ; function buildCCBuffer ( payload ) { return Buffer . concat ( [ Buffer . from ( [ CommandClasses [ "Multi Channel Association" ] , ] ) , payload , ] ) ; } describe ( "lib/commandclass/MultiChannelAssociationCC => " , ( ) => { it ( "the SupportedGroupingsGet command should serialize correctly" , ( ) => { const cc = new MultiChannelAssociationCCSupportedGroupingsGet ( fakeDriver , { nodeId : 1 } ) ; const expected = buildCCBuffer ( Buffer . from ( [ MultiChannelAssociationCommand . SupportedGroupingsGet , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the SupportedGroupingsReport command should be deserialized correctly" , ( ) => { const ccData = buildCCBuffer ( Buffer . from ( [ MultiChannelAssociationCommand . SupportedGroupingsReport , 7 , ] ) ) ; const cc = new MultiChannelAssociationCCSupportedGroupingsReport ( fakeDriver , { nodeId : 4 , data : ccData , } ) ; expect ( cc . groupCount ) . toBe ( 7 ) ; } ) ; it ( "the Set command should serialize correctly (node IDs only)" , ( ) => { const cc = new MultiChannelAssociationCCSet ( fakeDriver , { nodeId : 2 , groupId : 5 , nodeIds : [ 1 , 2 , 5 ] , } ) ; const expected = buildCCBuffer ( Buffer . from ( [ MultiChannelAssociationCommand . Set , 5 , 1 , 2 , 5 , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the Set command should serialize correctly (endpoint addresses only)" , ( ) => { const cc = new MultiChannelAssociationCCSet ( fakeDriver , { nodeId : 2 , groupId : 5 , endpoints : [ { nodeId : 5 , endpoint : 1 , } , { nodeId : 7 , endpoint : [ 1 , 2 , 3 , 5 , 7 ] , } , ] , } ) ; const expected = buildCCBuffer ( Buffer . from ( [ MultiChannelAssociationCommand . Set , 5 , 0 , 5 , 1 , 7 , 0b11010111 , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the Set command should serialize correctly (both options)" , ( ) => { const cc = new MultiChannelAssociationCCSet ( fakeDriver , { nodeId : 2 , groupId : 5 , nodeIds : [ 1 , 2 , 3 ] , endpoints : [ { nodeId : 5 , endpoint : 1 , } , { nodeId : 7 , endpoint : [ 1 , 2 , 3 , 5 , 7 ] , } , ] , } ) ; const expected = buildCCBuffer ( Buffer . from ( [ MultiChannelAssociationCommand . Set , 5 , 1 , 2 , 3 , 0 , 5 , 1 , 7 , 0b11010111 , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the Get command should serialize correctly" , ( ) => { const cc = new MultiChannelAssociationCCGet ( fakeDriver , { nodeId : 1 , groupId : 9 , } ) ; const expected = buildCCBuffer ( Buffer . from ( [ MultiChannelAssociationCommand . Get , 9 , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the Report command should be deserialized correctly (node IDs only)" , ( ) => { const ccData = buildCCBuffer ( Buffer . from ( [ MultiChannelAssociationCommand . Report , 5 , 9 , 0 , 1 , 2 , 5 , ] ) ) ; const cc = new MultiChannelAssociationCCReport ( fakeDriver , { nodeId : 4 , data : ccData , } ) ; expect ( cc . groupId ) . toBe ( 5 ) ; expect ( cc . maxNodes ) . toBe ( 9 ) ; expect ( cc . reportsToFollow ) . toBe ( 0 ) ; expect ( cc . nodeIds ) . toEqual ( [ 1 , 2 , 5 ] ) ; expect ( cc . endpoints ) . toEqual ( [ ] ) ; } ) ; it ( "the Report command should be deserialized correctly (endpoint addresses only)" , ( ) => { const ccData = buildCCBuffer ( Buffer . from ( [ MultiChannelAssociationCommand . Report , 5 , 9 , 0 , 0 , 5 , 1 , 7 , 0b11010111 , ] ) ) ; const cc = new MultiChannelAssociationCCReport ( fakeDriver , { nodeId : 4 , data : ccData , } ) ; expect ( cc . nodeIds ) . toEqual ( [ ] ) ; expect ( cc . endpoints ) . toEqual ( [ { nodeId : 5 , endpoint : 1 , } , { nodeId : 7 , endpoint : [ 1 , 2 , 3 , 5 , 7 ] , } , ] ) ; } ) ; it ( "the Report command should be deserialized correctly (both options)" , ( ) => { const ccData = buildCCBuffer ( Buffer . from ( [ MultiChannelAssociationCommand . Report , 5 , 9 , 0 , 1 , 5 , 9 , 0 , 5 , 1 , 7 , 0b11010111 , ] ) ) ; const cc = new MultiChannelAssociationCCReport ( fakeDriver , { nodeId : 4 , data : ccData , } ) ; expect ( cc . nodeIds ) . toEqual ( [ 1 , 5 , 9 ] ) ; expect ( cc . endpoints ) . toEqual ( [ { nodeId : 5 , endpoint : 1 , } , { nodeId : 7 , endpoint : [ 1 , 2 , 3 , 5 , 7 ] , } , ] ) ; } ) ; it ( "the Remove command should serialize correctly (node IDs only)" , ( ) => { const cc = new MultiChannelAssociationCCRemove ( fakeDriver , { nodeId : 2 , groupId : 5 , nodeIds : [ 1 , 2 , 5 ] , } ) ; const expected = buildCCBuffer ( Buffer . from ( [ MultiChannelAssociationCommand . Remove , 5 , 1 , 2 , 5 , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the Remove command should serialize correctly (endpoint addresses only)" , ( ) => { const cc = new MultiChannelAssociationCCRemove ( fakeDriver , { nodeId : 2 , groupId : 5 , endpoints : [ { nodeId : 5 , endpoint : 1 , } , { nodeId : 7 , endpoint : [ 1 , 2 , 3 , 5 , 7 ] , } , ] , } ) ; const expected = buildCCBuffer ( Buffer . from ( [ MultiChannelAssociationCommand . Remove , 5 , 0 , 5 , 1 , 7 , 0b11010111 , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the Remove command should serialize correctly (both options)" , ( ) => { const cc = new MultiChannelAssociationCCRemove ( fakeDriver , { nodeId : 2 , groupId : 5 , nodeIds : [ 1 , 2 , 3 ] , endpoints : [ { nodeId : 5 , endpoint : 1 , } , { nodeId : 7 , endpoint : [ 1 , 2 , 3 , 5 , 7 ] , } , ] , } ) ; const expected = buildCCBuffer ( Buffer . from ( [ MultiChannelAssociationCommand . Remove , 5 , 1 , 2 , 3 , 0 , 5 , 1 , 7 , 0b11010111 , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the Remove command should serialize correctly (both empty)" , ( ) => { const cc = new MultiChannelAssociationCCRemove ( fakeDriver , { nodeId : 2 , groupId : 5 , } ) ; const expected = buildCCBuffer ( Buffer . from ( [ MultiChannelAssociationCommand . Remove , 5 , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; } ) ;