import { CommandClasses , Duration } from "@zwave-js/core" ; import type { Driver } from "../driver/Driver" ; import { createEmptyMockDriver } from "../test/mocks" ; import { MultilevelSwitchCC , MultilevelSwitchCCGet , MultilevelSwitchCCReport , MultilevelSwitchCCSet , MultilevelSwitchCCStartLevelChange , MultilevelSwitchCCStopLevelChange , MultilevelSwitchCCSupportedGet , MultilevelSwitchCommand , } from "./MultilevelSwitchCC" ; const fakeDriver = createEmptyMockDriver ( ) as unknown as Driver ; function buildCCBuffer ( payload ) { return Buffer . concat ( [ Buffer . from ( [ CommandClasses [ "Multilevel Switch" ] , ] ) , payload , ] ) ; } describe ( "lib/commandclass/MultilevelSwitchCC => " , ( ) => { it ( "the Get command should serialize correctly" , ( ) => { const cc = new MultilevelSwitchCCGet ( fakeDriver , { nodeId : 1 } ) ; const expected = buildCCBuffer ( Buffer . from ( [ MultilevelSwitchCommand . Get , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the Set command (V1) should serialize correctly" , ( ) => { const cc = new MultilevelSwitchCCSet ( fakeDriver , { nodeId : 2 , targetValue : 55 } ) ; const expected = buildCCBuffer ( Buffer . from ( [ MultilevelSwitchCommand . Set , 55 , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the Set command (V2) should serialize correctly" , ( ) => { const cc = new MultilevelSwitchCCSet ( fakeDriver , { nodeId : 2 , targetValue : 55 , duration : new Duration ( 2 , "minutes" ) } ) ; const expected = buildCCBuffer ( Buffer . from ( [ MultilevelSwitchCommand . Set , 55 , 0x81 , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the Report command (V1) should be deserialized correctly" , ( ) => { const ccData = buildCCBuffer ( Buffer . from ( [ MultilevelSwitchCommand . Report , 55 , ] ) ) ; const cc = new MultilevelSwitchCCReport ( fakeDriver , { nodeId : 2 , data : ccData } ) ; expect ( cc . currentValue ) . toBe ( 55 ) ; expect ( cc . targetValue ) . toBeUndefined ( ) ; expect ( cc . duration ) . toBeUndefined ( ) ; } ) ; it ( "the Report command (v4) should be deserialized correctly" , ( ) => { const ccData = buildCCBuffer ( Buffer . from ( [ MultilevelSwitchCommand . Report , 55 , 66 , 1 , ] ) ) ; const cc = new MultilevelSwitchCCReport ( fakeDriver , { nodeId : 2 , data : ccData } ) ; expect ( cc . currentValue ) . toBe ( 55 ) ; expect ( cc . targetValue ) . toBe ( 66 ) ; expect ( cc . duration ! . unit ) . toBe ( "seconds" ) ; expect ( cc . duration ! . value ) . toBe ( 1 ) ; } ) ; it ( "the StartLevelChange command (V1) should serialize correctly (up, ignore start level)" , ( ) => { const cc = new MultilevelSwitchCCStartLevelChange ( fakeDriver , { nodeId : 2 , direction : "up" , ignoreStartLevel : true } ) ; const expected = buildCCBuffer ( Buffer . from ( [ MultilevelSwitchCommand . StartLevelChange , 32 , 0 , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the StartLevelChange command (V1) should serialize correctly (down)" , ( ) => { const cc = new MultilevelSwitchCCStartLevelChange ( fakeDriver , { nodeId : 2 , direction : "down" , ignoreStartLevel : false , startLevel : 50 } ) ; const expected = buildCCBuffer ( Buffer . from ( [ MultilevelSwitchCommand . StartLevelChange , 64 , 50 , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the StopLevelChange command should serialize correctly" , ( ) => { const cc = new MultilevelSwitchCCStopLevelChange ( fakeDriver , { nodeId : 1 } ) ; const expected = buildCCBuffer ( Buffer . from ( [ MultilevelSwitchCommand . StopLevelChange , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the StartLevelChange command (V2) should serialize correctly (down, with duration)" , ( ) => { const cc = new MultilevelSwitchCCStartLevelChange ( fakeDriver , { nodeId : 2 , direction : "down" , ignoreStartLevel : false , startLevel : 50 , duration : new Duration ( 3 , "seconds" ) } ) ; const expected = buildCCBuffer ( Buffer . from ( [ MultilevelSwitchCommand . StartLevelChange , 64 , 50 , 3 , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the SupportedGet command should serialize correctly" , ( ) => { const cc = new MultilevelSwitchCCSupportedGet ( fakeDriver , { nodeId : 1 } ) ; const expected = buildCCBuffer ( Buffer . from ( [ MultilevelSwitchCommand . SupportedGet , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "deserializing an unsupported command should return an unspecified version of MultilevelSwitchCC" , ( ) => { const serializedCC = buildCCBuffer ( Buffer . from ( [ 255 ] ) ) ; const cc = new MultilevelSwitchCC ( fakeDriver , { nodeId : 2 , data : serializedCC } ) ; expect ( cc . constructor ) . toBe ( MultilevelSwitchCC ) ; } ) ; } ) ;