import { CommandClasses } from "@zwave-js/core" ; import type { Driver } from "../driver/Driver" ; import { ZWaveNode } from "../node/Node" ; import { createEmptyMockDriver } from "../test/mocks" ; import { BinarySensorCC , BinarySensorCCGet , BinarySensorCCReport , BinarySensorCCSupportedGet , BinarySensorCCSupportedReport , BinarySensorCommand , BinarySensorType , } from "./BinarySensorCC" ; function buildCCBuffer ( payload ) { return Buffer . concat ( [ Buffer . from ( [ CommandClasses [ "Binary Sensor" ] , ] ) , payload , ] ) ; } describe ( "lib/commandclass/BinarySensorCC => " , ( ) => { const fakeDriver = createEmptyMockDriver ( ) as unknown as Driver ; let node1 ; beforeAll ( ( ) => { node1 = new ZWaveNode ( 1 , fakeDriver as any as Driver ) ; ( fakeDriver . controller . nodes as Map < any , any > ) . set ( node1 . id , node1 ) ; } ) ; afterAll ( ( ) => { node1 . destroy ( ) ; } ) ; it ( "the Get command (v1) should serialize correctly" , ( ) => { const cc = new BinarySensorCCGet ( fakeDriver , { nodeId : 1 } ) ; const expected = buildCCBuffer ( Buffer . from ( [ BinarySensorCommand . Get , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the Get command (v2) should serialize correctly" , ( ) => { const cc = new BinarySensorCCGet ( fakeDriver , { nodeId : 1 , sensorType : BinarySensorType . CO } ) ; const expected = buildCCBuffer ( Buffer . from ( [ BinarySensorCommand . Get , BinarySensorType . CO ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the Report command (v1) should be deserialized correctly" , ( ) => { const ccData = buildCCBuffer ( Buffer . from ( [ BinarySensorCommand . Report , 0xff , ] ) ) ; const cc = new BinarySensorCCReport ( fakeDriver , { nodeId : 1 , data : ccData } ) ; expect ( cc . value ) . toBe ( true ) ; } ) ; it ( "the Report command (v2) should be deserialized correctly" , ( ) => { const ccData = buildCCBuffer ( Buffer . from ( [ BinarySensorCommand . Report , 0x00 , BinarySensorType . CO2 , ] ) ) ; const cc = new BinarySensorCCReport ( fakeDriver , { nodeId : 1 , data : ccData } ) ; expect ( cc . value ) . toBe ( false ) ; expect ( cc . type ) . toBe ( BinarySensorType . CO2 ) ; } ) ; it ( "the SupportedGet command should serialize correctly" , ( ) => { const cc = new BinarySensorCCSupportedGet ( fakeDriver , { nodeId : 1 } ) ; const expected = buildCCBuffer ( Buffer . from ( [ BinarySensorCommand . SupportedGet , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the SupportedReport command should be deserialized correctly" , ( ) => { const ccData = buildCCBuffer ( Buffer . from ( [ BinarySensorCommand . SupportedReport , 0b10101010 , 0b10 , ] ) ) ; const cc = new BinarySensorCCSupportedReport ( fakeDriver , { nodeId : 1 , data : ccData } ) ; expect ( cc . supportedSensorTypes ) . toEqual ( [ BinarySensorType [ "General Purpose" ] , BinarySensorType . CO , BinarySensorType . Heat , BinarySensorType . Freeze , BinarySensorType . Aux , ] ) ; } ) ; it ( "deserializing an unsupported command should return an unspecified version of BinarySensorCC" , ( ) => { const serializedCC = buildCCBuffer ( Buffer . from ( [ 255 ] ) ) ; const cc = new BinarySensorCC ( fakeDriver , { nodeId : 1 , data : serializedCC } ) ; expect ( cc . constructor ) . toBe ( BinarySensorCC ) ; } ) ; } ) ;