import { CommandClasses } from "@zwave-js/core" ; import type { Driver } from "../driver/Driver" ; import { ZWaveNode } from "../node/Node" ; import { createEmptyMockDriver } from "../test/mocks" ; import { AssociationGroupInfoCC , AssociationGroupInfoCCCommandListGet , AssociationGroupInfoCCCommandListReport , AssociationGroupInfoCCInfoGet , AssociationGroupInfoCCInfoReport , AssociationGroupInfoCCNameGet , AssociationGroupInfoCCNameReport , AssociationGroupInfoCommand , AssociationGroupInfoProfile , } from "./AssociationGroupInfoCC" ; import { BasicCommand } from "./BasicCC" ; function buildCCBuffer ( payload ) { return Buffer . concat ( [ Buffer . from ( [ CommandClasses [ "Association Group Information" ] , ] ) , payload , ] ) ; } describe ( "lib/commandclass/AssociationGroupInfoCC => " , ( ) => { let fakeDriver ; let node1 ; beforeAll ( ( ) => { fakeDriver = createEmptyMockDriver ( ) as unknown as Driver ; node1 = new ZWaveNode ( 1 , fakeDriver as any ) ; ( fakeDriver . controller . nodes as any ) . set ( node1 . id , node1 ) ; } ) ; afterAll ( ( ) => { node1 . destroy ( ) ; } ) ; it ( "the NameGet command should serialize correctly" , ( ) => { const cc = new AssociationGroupInfoCCNameGet ( fakeDriver , { nodeId : 1 , groupId : 7 } ) ; const expected = buildCCBuffer ( Buffer . from ( [ AssociationGroupInfoCommand . NameGet , 7 , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the NameReport command should be deserialized correctly" , ( ) => { const ccData = buildCCBuffer ( Buffer . from ( [ AssociationGroupInfoCommand . NameReport , 7 , 6 , 0x66 , 0x6f , 0x6f , 0x62 , 0x61 , 0x72 , ] ) ) ; const cc = new AssociationGroupInfoCCNameReport ( fakeDriver , { nodeId : 1 , data : ccData } ) ; expect ( cc . groupId ) . toBe ( 7 ) ; expect ( cc . name ) . toBe ( "foobar" ) ; } ) ; it ( "the InfoGet command should serialize correctly (no flag set)" , ( ) => { const cc = new AssociationGroupInfoCCInfoGet ( fakeDriver , { nodeId : 1 , groupId : 7 , listMode : false , refreshCache : false } ) ; const expected = buildCCBuffer ( Buffer . from ( [ AssociationGroupInfoCommand . InfoGet , 0 , 7 , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the InfoGet command should serialize correctly (refresh cache flag set)" , ( ) => { const cc = new AssociationGroupInfoCCInfoGet ( fakeDriver , { nodeId : 1 , groupId : 7 , listMode : false , refreshCache : true } ) ; const expected = buildCCBuffer ( Buffer . from ( [ AssociationGroupInfoCommand . InfoGet , 128 , 7 , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the InfoGet command should serialize correctly (list mode flag set)" , ( ) => { const cc = new AssociationGroupInfoCCInfoGet ( fakeDriver , { nodeId : 1 , groupId : 7 , listMode : true , refreshCache : false } ) ; const expected = buildCCBuffer ( Buffer . from ( [ AssociationGroupInfoCommand . InfoGet , 64 , 0 , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the Info Report command should be deserialized correctly" , ( ) => { const ccData = buildCCBuffer ( Buffer . from ( [ AssociationGroupInfoCommand . InfoReport , 192 | 2 , 1 , 0 , 0 , 1 , 0 , 0 , 0 , 2 , 0 , 0x20 , 1 , 0 , 0 , 0 , ] ) ) ; const cc = new AssociationGroupInfoCCInfoReport ( fakeDriver , { nodeId : 1 , data : ccData } ) ; expect ( cc . groups ) . toHaveLength ( 2 ) ; expect ( cc . groups [ 0 ] . groupId ) . toBe ( 1 ) ; expect ( cc . groups [ 0 ] . profile ) . toBe ( AssociationGroupInfoProfile [ "General: Lifeline" ] ) ; expect ( cc . groups [ 1 ] . groupId ) . toBe ( 2 ) ; expect ( cc . groups [ 1 ] . profile ) . toBe ( AssociationGroupInfoProfile [ "Control: Key 01" ] ) ; } ) ; it ( "the CommandListGet command should serialize correctly" , ( ) => { const cc = new AssociationGroupInfoCCCommandListGet ( fakeDriver , { nodeId : 1 , groupId : 6 , allowCache : true } ) ; const expected = buildCCBuffer ( Buffer . from ( [ AssociationGroupInfoCommand . CommandListGet , 128 , 6 , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the CommandListReport command should be deserialized correctly" , ( ) => { const ccData = buildCCBuffer ( Buffer . from ( [ AssociationGroupInfoCommand . CommandListReport , 7 , 5 , CommandClasses . Basic , BasicCommand . Set , 0xf1 , 0x00 , 0x05 , ] ) ) ; const cc = new AssociationGroupInfoCCCommandListReport ( fakeDriver , { nodeId : 1 , data : ccData } ) ; expect ( cc . groupId ) . toBe ( 7 ) ; expect ( cc . commands . size ) . toBe ( 2 ) ; expect ( [ ... cc . commands . keys ( ) ] ) . toEqual ( [ CommandClasses . Basic , CommandClasses [ "Security Mark" ] , ] ) ; expect ( [ ... cc . commands . values ( ) ] ) . toEqual ( [ [ BasicCommand . Set ] , [ 0x05 ] ] ) ; } ) ; it ( "deserializing an unsupported command should return an unspecified version of AssociationGroupInfoCC" , ( ) => { const serializedCC = buildCCBuffer ( Buffer . from ( [ 255 ] ) ) ; const cc = new AssociationGroupInfoCC ( fakeDriver , { nodeId : 1 , data : serializedCC } ) ; expect ( cc . constructor ) . toBe ( AssociationGroupInfoCC ) ; } ) ; } ) ;