import { CommandClasses } from "@zwave-js/core" ; import type { Driver } from "../driver/Driver" ; import { ZWaveNode } from "../node/Node" ; import { createEmptyMockDriver } from "../test/mocks" ; import { BasicCC , BasicCCGet , BasicCCReport , BasicCCSet , BasicCommand , } from "./BasicCC" ; import { getCCValueMetadata } from "./CommandClass" ; const fakeDriver = createEmptyMockDriver ( ) as unknown as Driver ; function buildCCBuffer ( payload ) { return Buffer . concat ( [ Buffer . from ( [ CommandClasses . Basic , ] ) , payload , ] ) ; } describe ( "lib/commandclass/BasicCC => " , ( ) => { it ( "the Get command should serialize correctly" , ( ) => { const basicCC = new BasicCCGet ( fakeDriver , { nodeId : 1 } ) ; const expected = buildCCBuffer ( Buffer . from ( [ BasicCommand . Get , ] ) ) ; expect ( basicCC . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the Set command should serialize correctly" , ( ) => { const basicCC = new BasicCCSet ( fakeDriver , { nodeId : 2 , targetValue : 55 } ) ; const expected = buildCCBuffer ( Buffer . from ( [ BasicCommand . Set , 55 , ] ) ) ; expect ( basicCC . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the Report command (v1) should be deserialized correctly" , ( ) => { const ccData = buildCCBuffer ( Buffer . from ( [ BasicCommand . Report , 55 , ] ) ) ; const basicCC = new BasicCCReport ( fakeDriver , { nodeId : 2 , data : ccData } ) ; expect ( basicCC . currentValue ) . toBe ( 55 ) ; expect ( basicCC . targetValue ) . toBeUndefined ( ) ; expect ( basicCC . duration ) . toBeUndefined ( ) ; } ) ; it ( "the Report command (v2) should be deserialized correctly" , ( ) => { const ccData = buildCCBuffer ( Buffer . from ( [ BasicCommand . Report , 55 , 66 , 1 , ] ) ) ; const basicCC = new BasicCCReport ( fakeDriver , { nodeId : 2 , data : ccData } ) ; expect ( basicCC . currentValue ) . toBe ( 55 ) ; expect ( basicCC . targetValue ) . toBe ( 66 ) ; expect ( basicCC . duration ! . unit ) . toBe ( "seconds" ) ; expect ( basicCC . duration ! . value ) . toBe ( 1 ) ; } ) ; it ( "deserializing an unsupported command should return an unspecified version of BasicCC" , ( ) => { const serializedCC = buildCCBuffer ( Buffer . from ( [ 255 ] ) ) ; const basicCC = new BasicCC ( fakeDriver , { nodeId : 2 , data : serializedCC } ) ; expect ( basicCC . constructor ) . toBe ( BasicCC ) ; } ) ; it ( "the CC values should have the correct metadata" , ( ) => { const currentValueMeta = getCCValueMetadata ( CommandClasses . Basic , "currentValue" ) ; expect ( currentValueMeta ) . toMatchObject ( { readable : true , writeable : false , min : 0 , max : 99 } ) ; const targetValueMeta = getCCValueMetadata ( CommandClasses . Basic , "targetValue" ) ; expect ( targetValueMeta ) . toMatchObject ( { readable : true , writeable : true , min : 0 , max : 99 } ) ; } ) ; describe ( "getDefinedValueIDs()" , ( ) => { it ( "should include the target value for all endpoints except the node itself" , ( ) => { const node = new ZWaveNode ( 2 , fakeDriver as unknown as Driver ) ; ( fakeDriver as any ) . controller . nodes . set ( node . id , node ) ; node . valueDB . setValue ( { commandClass : CommandClasses [ "Multi Channel" ] , property : "interviewComplete" } , true ) ; node . valueDB . setValue ( { commandClass : CommandClasses [ "Multi Channel" ] , property : "individualCount" } , 2 ) ; for ( let ep = 0 ; ep <= 2 ; ep ++ ) { node . getEndpoint ( ep ) ! . addCC ( CommandClasses . Basic , { isSupported : true , version : 1 } ) ; } const valueIDs = node . getDefinedValueIDs ( ) . filter ( ( { commandClass , property } ) => commandClass === CommandClasses . Basic && property === "targetValue" ) ; const endpoints = valueIDs . map ( ( { endpoint } ) => endpoint ) ; node . destroy ( ) ; expect ( endpoints ) . toEqual ( [ 1 , 2 ] ) ; } ) ; } ) ; describe ( "responses should be detected correctly" , ( ) => { it ( "BasicCCSet should expect no response" , ( ) => { const cc = new BasicCCSet ( fakeDriver , { nodeId : 2 , endpoint : 2 , targetValue : 7 } ) ; expect ( cc . expectsCCResponse ( ) ) . toBeFalse ( ) ; } ) ; it ( "BasicCCSet => BasicCCReport = unexpected" , ( ) => { const ccRequest = new BasicCCSet ( fakeDriver , { nodeId : 2 , endpoint : 2 , targetValue : 7 } ) ; const ccResponse = new BasicCCReport ( fakeDriver , { nodeId : ccRequest . nodeId , currentValue : 7 } ) ; expect ( ccRequest . isExpectedCCResponse ( ccResponse ) ) . toBeFalse ( ) ; } ) ; it ( "BasicCCGet should expect a response" , ( ) => { const cc = new BasicCCGet ( fakeDriver , { nodeId : 2 } ) ; expect ( cc . expectsCCResponse ( ) ) . toBeTrue ( ) ; } ) ; it ( "BasicCCGet => BasicCCReport = expected" , ( ) => { const ccRequest = new BasicCCGet ( fakeDriver , { nodeId : 2 } ) ; const ccResponse = new BasicCCReport ( fakeDriver , { nodeId : ccRequest . nodeId , currentValue : 7 } ) ; expect ( ccRequest . isExpectedCCResponse ( ccResponse ) ) . toBeTrue ( ) ; } ) ; it ( "BasicCCGet => BasicCCReport (wrong node) = unexpected" , ( ) => { const ccRequest = new BasicCCGet ( fakeDriver , { nodeId : 2 } ) ; const ccResponse = new BasicCCReport ( fakeDriver , { nodeId : ( ccRequest . nodeId as number ) + 1 , currentValue : 7 } ) ; expect ( ccRequest . isExpectedCCResponse ( ccResponse ) ) . toBeFalse ( ) ; } ) ; it ( "BasicCCGet => BasicCCSet = unexpected" , ( ) => { const ccRequest = new BasicCCGet ( fakeDriver , { nodeId : 2 } ) ; const ccResponse = new BasicCCSet ( fakeDriver , { nodeId : ccRequest . nodeId , targetValue : 7 } ) ; expect ( ccRequest . isExpectedCCResponse ( ccResponse ) ) . toBeFalse ( ) ; } ) ; } ) ; } ) ;