import { CommandClasses , Duration } from "@zwave-js/core" ; import type { Driver } from "../driver/Driver" ; import { createEmptyMockDriver } from "../test/mocks" ; import { DoorLockCCCapabilitiesGet , DoorLockCCCapabilitiesReport , DoorLockCCConfigurationGet , DoorLockCCConfigurationReport , DoorLockCCConfigurationSet , DoorLockCCOperationGet , DoorLockCCOperationReport , DoorLockCCOperationSet , DoorLockCommand , DoorLockMode , DoorLockOperationType , } from "./DoorLockCC" ; const fakeDriver = createEmptyMockDriver ( ) as unknown as Driver ; function buildCCBuffer ( payload ) { return Buffer . concat ( [ Buffer . from ( [ CommandClasses [ "Door Lock" ] , ] ) , payload , ] ) ; } describe ( "lib/commandclass/DoorLockCC => " , ( ) => { it ( "the OperationGet command should serialize correctly" , ( ) => { const cc = new DoorLockCCOperationGet ( fakeDriver , { nodeId : 1 } ) ; const expected = buildCCBuffer ( Buffer . from ( [ DoorLockCommand . OperationGet , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the OperationSet command should serialize correctly" , ( ) => { const cc = new DoorLockCCOperationSet ( fakeDriver , { nodeId : 2 , mode : DoorLockMode . OutsideUnsecured } ) ; const expected = buildCCBuffer ( Buffer . from ( [ DoorLockCommand . OperationSet , 0x20 , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the OperationReport command (v1-v3) should be deserialized correctly" , ( ) => { const ccData = buildCCBuffer ( Buffer . from ( [ DoorLockCommand . OperationReport , DoorLockMode . InsideUnsecuredWithTimeout , 130 , 0b010 , 50 , 20 , ] ) ) ; const cc = new DoorLockCCOperationReport ( fakeDriver , { nodeId : 1 , data : ccData } ) ; expect ( cc . currentMode ) . toBe ( DoorLockMode . InsideUnsecuredWithTimeout ) ; expect ( cc . outsideHandlesCanOpenDoor ) . toEqual ( [ false , false , false , true , ] ) ; expect ( cc . insideHandlesCanOpenDoor ) . toEqual ( [ false , true , false , false , ] ) ; expect ( cc . lockTimeout ) . toBe ( 50 * 60 + 20 ) ; expect ( cc . latchStatus ) . toBe ( "open" ) ; expect ( cc . boltStatus ) . toBe ( "unlocked" ) ; expect ( cc . doorStatus ) . toBe ( "open" ) ; expect ( cc . targetMode ) . toBeUndefined ( ) ; expect ( cc . duration ) . toBeUndefined ( ) ; } ) ; it ( "the OperationReport command (v4) should be deserialized correctly" , ( ) => { const ccData = buildCCBuffer ( Buffer . from ( [ DoorLockCommand . OperationReport , DoorLockMode . OutsideUnsecured , 79 , 0b101 , 0x00 , 0xfe , DoorLockMode . Secured , 0x01 , ] ) ) ; const cc = new DoorLockCCOperationReport ( fakeDriver , { nodeId : 1 , data : ccData } ) ; expect ( cc . currentMode ) . toBe ( DoorLockMode . OutsideUnsecured ) ; expect ( cc . outsideHandlesCanOpenDoor ) . toEqual ( [ false , false , true , false , ] ) ; expect ( cc . insideHandlesCanOpenDoor ) . toEqual ( [ true , true , true , true ] ) ; expect ( cc . lockTimeout ) . toBeUndefined ( ) ; expect ( cc . latchStatus ) . toBe ( "closed" ) ; expect ( cc . boltStatus ) . toBe ( "locked" ) ; expect ( cc . doorStatus ) . toBe ( "closed" ) ; expect ( cc . targetMode ) . toBe ( DoorLockMode . Secured ) ; expect ( cc . duration ) . toEqual ( new Duration ( 1 , "seconds" ) ) ; } ) ; it ( "the ConfigurationGet command should serialize correctly" , ( ) => { const cc = new DoorLockCCConfigurationGet ( fakeDriver , { nodeId : 1 } ) ; const expected = buildCCBuffer ( Buffer . from ( [ DoorLockCommand . ConfigurationGet , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the ConfigurationReport command (v1-v3) should be deserialized correctly" , ( ) => { const ccData = buildCCBuffer ( Buffer . from ( [ DoorLockCommand . ConfigurationReport , DoorLockOperationType . Timed , 130 , 50 , 20 , ] ) ) ; const cc = new DoorLockCCConfigurationReport ( fakeDriver , { nodeId : 1 , data : ccData } ) ; expect ( cc . operationType ) . toBe ( DoorLockOperationType . Timed ) ; expect ( cc . outsideHandlesCanOpenDoorConfiguration ) . toEqual ( [ false , false , false , true , ] ) ; expect ( cc . insideHandlesCanOpenDoorConfiguration ) . toEqual ( [ false , true , false , false , ] ) ; expect ( cc . lockTimeoutConfiguration ) . toBe ( 50 * 60 + 20 ) ; expect ( cc . autoRelockTime ) . toBeUndefined ( ) ; expect ( cc . holdAndReleaseTime ) . toBeUndefined ( ) ; expect ( cc . twistAssist ) . toBeUndefined ( ) ; expect ( cc . blockToBlock ) . toBeUndefined ( ) ; } ) ; it ( "the ConfigurationReport command must ignore invalid timeouts (constant)" , ( ) => { const ccData = buildCCBuffer ( Buffer . from ( [ DoorLockCommand . ConfigurationReport , DoorLockOperationType . Constant , 130 , 50 , 20 , ] ) ) ; const cc = new DoorLockCCConfigurationReport ( fakeDriver , { nodeId : 1 , data : ccData } ) ; expect ( cc . lockTimeoutConfiguration ) . toBeUndefined ( ) ; } ) ; it ( "the ConfigurationReport command must ignore invalid timeouts (invalid minutes)" , ( ) => { const ccData = buildCCBuffer ( Buffer . from ( [ DoorLockCommand . ConfigurationReport , DoorLockOperationType . Constant , 130 , 0xff , 20 , ] ) ) ; const cc = new DoorLockCCConfigurationReport ( fakeDriver , { nodeId : 1 , data : ccData } ) ; expect ( cc . lockTimeoutConfiguration ) . toBeUndefined ( ) ; } ) ; it ( "the ConfigurationReport command must ignore invalid timeouts (invalid seconds)" , ( ) => { const ccData = buildCCBuffer ( Buffer . from ( [ DoorLockCommand . ConfigurationReport , DoorLockOperationType . Constant , 130 , 20 , 0xff , ] ) ) ; const cc = new DoorLockCCConfigurationReport ( fakeDriver , { nodeId : 1 , data : ccData } ) ; expect ( cc . lockTimeoutConfiguration ) . toBeUndefined ( ) ; } ) ; it ( "the ConfigurationReport command (v4) should be deserialized correctly" , ( ) => { const ccData = buildCCBuffer ( Buffer . from ( [ DoorLockCommand . ConfigurationReport , DoorLockOperationType . Timed , 130 , 50 , 20 , 0xff , 0x01 , 0x02 , 0x03 , 0b01 , ] ) ) ; const cc = new DoorLockCCConfigurationReport ( fakeDriver , { nodeId : 1 , data : ccData } ) ; expect ( cc . autoRelockTime ) . toBe ( 0xff01 ) ; expect ( cc . holdAndReleaseTime ) . toBe ( 0x0203 ) ; expect ( cc . twistAssist ) . toBeTrue ( ) ; expect ( cc . blockToBlock ) . toBeFalse ( ) ; } ) ; it ( "the ConfigurationSet command (v1-3) should serialize correctly (timed)" , ( ) => { const cc = new DoorLockCCConfigurationSet ( fakeDriver , { nodeId : 2 , operationType : DoorLockOperationType . Timed , outsideHandlesCanOpenDoorConfiguration : [ false , true , true , true ] , insideHandlesCanOpenDoorConfiguration : [ true , false , true , false ] , lockTimeoutConfiguration : 60 * 15 + 18 } ) ; const expected = buildCCBuffer ( Buffer . from ( [ DoorLockCommand . ConfigurationSet , DoorLockOperationType . Timed , 229 , 15 , 18 , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the ConfigurationSet command (v1-3) should serialize correctly (constant)" , ( ) => { const cc = new DoorLockCCConfigurationSet ( fakeDriver , { nodeId : 2 , operationType : DoorLockOperationType . Constant , outsideHandlesCanOpenDoorConfiguration : [ false , true , true , true ] , insideHandlesCanOpenDoorConfiguration : [ true , false , true , false ] } ) ; const expected = buildCCBuffer ( Buffer . from ( [ DoorLockCommand . ConfigurationSet , DoorLockOperationType . Constant , 229 , 0xfe , 0xfe , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the ConfigurationSet command (v4) should serialize correctly" , ( ) => { const cc = new DoorLockCCConfigurationSet ( fakeDriver , { nodeId : 2 , operationType : DoorLockOperationType . Timed , outsideHandlesCanOpenDoorConfiguration : [ false , true , true , true ] , insideHandlesCanOpenDoorConfiguration : [ true , false , true , false ] , lockTimeoutConfiguration : 60 * 15 + 18 , autoRelockTime : 0xfefd , holdAndReleaseTime : undefined , blockToBlock : undefined , twistAssist : true } ) ; const expected = buildCCBuffer ( Buffer . from ( [ DoorLockCommand . ConfigurationSet , DoorLockOperationType . Timed , 229 , 15 , 18 , 0xfe , 0xfd , 0 , 0 , 0b1 , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the CapabilitiesGet command should serialize correctly" , ( ) => { const cc = new DoorLockCCCapabilitiesGet ( fakeDriver , { nodeId : 1 } ) ; const expected = buildCCBuffer ( Buffer . from ( [ DoorLockCommand . CapabilitiesGet , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the CapabilitiesReport command should be deserialized correctly" , ( ) => { const ccData = buildCCBuffer ( Buffer . from ( [ DoorLockCommand . CapabilitiesReport , 1 , 0b11 , 3 , DoorLockMode . Unsecured , DoorLockMode . InsideUnsecured , DoorLockMode . Secured , 0 , 0b111 , 0b1010 , ] ) ) ; const cc = new DoorLockCCCapabilitiesReport ( fakeDriver , { nodeId : 1 , data : ccData } ) ; expect ( cc . supportedOperationTypes ) . toEqual ( [ DoorLockOperationType . Constant , DoorLockOperationType . Timed , ] ) ; expect ( cc . supportedDoorLockModes ) . toEqual ( [ DoorLockMode . Unsecured , DoorLockMode . InsideUnsecured , DoorLockMode . Secured , ] ) ; expect ( cc . latchSupported ) . toBeTrue ( ) ; expect ( cc . boltSupported ) . toBeTrue ( ) ; expect ( cc . doorSupported ) . toBeTrue ( ) ; expect ( cc . autoRelockSupported ) . toBeTrue ( ) ; expect ( cc . holdAndReleaseSupported ) . toBeFalse ( ) ; expect ( cc . twistAssistSupported ) . toBeTrue ( ) ; expect ( cc . blockToBlockSupported ) . toBeFalse ( ) ; } ) ; } ) ;