import { CommandClasses , Duration } from "@zwave-js/core" ; import type { Driver } from "../driver/Driver" ; import { createEmptyMockDriver } from "../test/mocks" ; import { BinarySwitchCC , BinarySwitchCCGet , BinarySwitchCCReport , BinarySwitchCCSet , BinarySwitchCommand , } from "./BinarySwitchCC" ; const fakeDriver = createEmptyMockDriver ( ) as unknown as Driver ; function buildCCBuffer ( payload ) { return Buffer . concat ( [ Buffer . from ( [ CommandClasses [ "Binary Switch" ] , ] ) , payload , ] ) ; } describe ( "lib/commandclass/BinarySwitchCC => " , ( ) => { it ( "the Get command should serialize correctly" , ( ) => { const cc = new BinarySwitchCCGet ( fakeDriver , { nodeId : 1 } ) ; const expected = buildCCBuffer ( Buffer . from ( [ BinarySwitchCommand . Get , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the Set command (v1) should serialize correctly" , ( ) => { const cc = new BinarySwitchCCSet ( fakeDriver , { nodeId : 2 , targetValue : false } ) ; const expected = buildCCBuffer ( Buffer . from ( [ BinarySwitchCommand . Set , 0x00 , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the Set command (v2) should serialize correctly" , ( ) => { const duration = new Duration ( 2 , "minutes" ) ; const cc = new BinarySwitchCCSet ( fakeDriver , { nodeId : 2 , targetValue : true , duration } ) ; const expected = buildCCBuffer ( Buffer . from ( [ BinarySwitchCommand . Set , 0xff , duration . serializeSet ( ) , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the Report command (v1) should be deserialized correctly" , ( ) => { const ccData = buildCCBuffer ( Buffer . from ( [ BinarySwitchCommand . Report , 0xff , ] ) ) ; const cc = new BinarySwitchCCReport ( fakeDriver , { nodeId : 2 , data : ccData } ) ; expect ( cc . currentValue ) . toBe ( true ) ; expect ( cc . targetValue ) . toBeUndefined ( ) ; expect ( cc . duration ) . toBeUndefined ( ) ; } ) ; it ( "the Report command (v2) should be deserialized correctly" , ( ) => { const ccData = buildCCBuffer ( Buffer . from ( [ BinarySwitchCommand . Report , 0xff , 0x00 , 1 , ] ) ) ; const cc = new BinarySwitchCCReport ( fakeDriver , { nodeId : 2 , data : ccData } ) ; expect ( cc . currentValue ) . toBe ( true ) ; expect ( cc . targetValue ) . toBe ( false ) ; expect ( cc . duration ! . unit ) . toBe ( "seconds" ) ; expect ( cc . duration ! . value ) . toBe ( 1 ) ; } ) ; it ( "deserializing an unsupported command should return an unspecified version of BinarySwitchCC" , ( ) => { const serializedCC = buildCCBuffer ( Buffer . from ( [ 255 ] ) ) ; const cc = new BinarySwitchCC ( fakeDriver , { nodeId : 2 , data : serializedCC } ) ; expect ( cc . constructor ) . toBe ( BinarySwitchCC ) ; } ) ; } ) ;