import { CommandClasses } from "@zwave-js/core" ; import type { Driver } from "../driver/Driver" ; import { ZWaveNode } from "../node/Node" ; import { createEmptyMockDriver } from "../test/mocks" ; import { getIndicatorValueValueID , getSupportedIndicatorIDsValueID , IndicatorCC , IndicatorCCGet , IndicatorCCReport , IndicatorCCSet , IndicatorCommand , } from "./IndicatorCC" ; function buildCCBuffer ( payload ) { return Buffer . concat ( [ Buffer . from ( [ CommandClasses . Indicator , ] ) , payload , ] ) ; } describe ( "lib/commandclass/IndicatorCC => " , ( ) => { let fakeDriver ; let node1 ; beforeAll ( async ( ) => { fakeDriver = createEmptyMockDriver ( ) as unknown as Driver ; node1 = new ZWaveNode ( 1 , fakeDriver as any ) ; ( fakeDriver . controller . nodes as any ) . set ( 1 , node1 ) ; node1 . addCC ( CommandClasses . Indicator , { isSupported : true , version : 3 , } ) ; if ( process . env . CI ) jest . setTimeout ( 30000 ) ; await fakeDriver . configManager . loadIndicators ( ) ; } ) ; afterAll ( ( ) => { node1 . destroy ( ) ; } ) ; it ( "the Get command (V1) should serialize correctly" , ( ) => { const cc = new IndicatorCCGet ( fakeDriver , { nodeId : 1 } ) ; const expected = buildCCBuffer ( Buffer . from ( [ IndicatorCommand . Get , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the Get command (V2) should serialize correctly" , ( ) => { const cc = new IndicatorCCGet ( fakeDriver , { nodeId : 1 , indicatorId : 5 , } ) ; const expected = buildCCBuffer ( Buffer . from ( [ IndicatorCommand . Get , 5 , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the Set command (v1) should serialize correctly" , ( ) => { const cc = new IndicatorCCSet ( fakeDriver , { nodeId : 2 , value : 23 , } ) ; const expected = buildCCBuffer ( Buffer . from ( [ IndicatorCommand . Set , 23 , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the Set command (v2) should serialize correctly" , ( ) => { const cc = new IndicatorCCSet ( fakeDriver , { nodeId : 2 , values : [ { indicatorId : 1 , propertyId : 2 , value : 3 , } , { indicatorId : 5 , propertyId : 3 , value : 1 , } , ] , } ) ; const expected = buildCCBuffer ( Buffer . from ( [ IndicatorCommand . Set , 0 , 2 , 1 , 2 , 3 , 5 , 3 , 1 , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the Report command (v1) should be deserialized correctly" , ( ) => { const ccData = buildCCBuffer ( Buffer . from ( [ IndicatorCommand . Report , 55 , ] ) ) ; const cc = new IndicatorCCReport ( fakeDriver , { nodeId : 1 , data : ccData , } ) ; expect ( cc . value ) . toBe ( 55 ) ; expect ( cc . values ) . toBeUndefined ( ) ; } ) ; it ( "the Report command (v2) should be deserialized correctly" , ( ) => { const ccData = buildCCBuffer ( Buffer . from ( [ IndicatorCommand . Report , 0 , 2 , 1 , 2 , 3 , 5 , 3 , 1 , ] ) ) ; const cc = new IndicatorCCReport ( fakeDriver , { nodeId : 1 , data : ccData , } ) ; expect ( cc . value ) . toBe ( undefined ) ; expect ( cc . values ) . toEqual ( [ { indicatorId : 1 , propertyId : 2 , value : true , } , { indicatorId : 5 , propertyId : 3 , value : 1 , } , ] ) ; } ) ; it ( "deserializing an unsupported command should return an unspecified version of IndicatorCC" , ( ) => { const serializedCC = buildCCBuffer ( Buffer . from ( [ 255 ] ) ) ; const cc = new IndicatorCC ( fakeDriver , { nodeId : 1 , data : serializedCC , } ) ; expect ( cc . constructor ) . toBe ( IndicatorCC ) ; } ) ; it ( "the value IDs should be translated properly" , ( ) => { const valueId = getIndicatorValueValueID ( 2 , 0x43 , 2 ) ; const ccInstance = node1 . createCCInstance ( CommandClasses . Indicator ) ! ; const translatedProperty = ccInstance . translateProperty ( valueId . property , valueId . propertyKey ) ; const translatedPropertyKey = ccInstance . translatePropertyKey ( valueId . property , valueId . propertyKey ! ) ; expect ( translatedProperty ) . toBe ( "Button 1 indication" ) ; expect ( translatedPropertyKey ) . toBe ( "Binary" ) ; } ) ; describe ( "interviewing the node" , ( ) => { beforeAll ( ( ) => { ( fakeDriver . sendCommand as jest . Mock ) . mockResolvedValueOnce ( { supportedProperties : [ ] , indicatorId : 0x30 , nextIndicatorId : 0x46 , } ) . mockResolvedValueOnce ( { supportedProperties : [ 2 , 3 , 4 ] , nextIndicatorId : 0x47 , } ) . mockResolvedValueOnce ( { supportedProperties : [ 2 , 3 , 4 ] , nextIndicatorId : 0x00 , } ) . mockResolvedValueOnce ( [ { indicatorId : 0x30 , propertyId : 0x02 , value : 0xff , } , ] ) . mockResolvedValueOnce ( [ { indicatorId : 0x46 , propertyId : 0x02 , value : 0xff , } , ] ) . mockResolvedValueOnce ( [ { indicatorId : 0x47 , propertyId : 0x02 , value : 0xff , } , ] ) ; } ) ; it ( "should return all supported indicator IDs" , async ( ) => { const ccInstance = node1 . createCCInstance ( CommandClasses . Indicator ) ! ; await ccInstance . interview ( true ) ; const indicatorIds = [ 0x30 , 0x46 , 0x47 ] ; expect ( node1 . getValue ( getSupportedIndicatorIDsValueID ( 0 ) ) ) . toEqual ( indicatorIds ) ; } ) ; } ) ; } ) ;