import { CommandClasses } from "@zwave-js/core" ; import type { Driver } from "../driver/Driver" ; import { createEmptyMockDriver } from "../test/mocks" ; import { Powerlevel , PowerlevelCC , PowerlevelCCGet , PowerlevelCCReport , PowerlevelCCSet , PowerlevelCommand , } from "./PowerlevelCC" ; const fakeDriver = createEmptyMockDriver ( ) as unknown as Driver ; function buildCCBuffer ( payload ) { return Buffer . concat ( [ Buffer . from ( [ CommandClasses . Powerlevel , ] ) , payload , ] ) ; } describe ( "lib/commandclass/PowerlevelCC => " , ( ) => { it ( "the Get command should serialize correctly" , ( ) => { const cc = new PowerlevelCCGet ( fakeDriver , { nodeId : 1 } ) ; const expected = buildCCBuffer ( Buffer . from ( [ PowerlevelCommand . Get , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the Set NormalPower command should serialize correctly" , ( ) => { const cc = new PowerlevelCCSet ( fakeDriver , { nodeId : 2 , powerlevel : Powerlevel [ "Normal Power" ] } ) ; const expected = buildCCBuffer ( Buffer . from ( [ PowerlevelCommand . Set , Powerlevel [ "Normal Power" ] , 0 , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the Set NormalPower command with timeout should serialize correctly" , ( ) => { const cc = new PowerlevelCCSet ( fakeDriver , { nodeId : 2 , powerlevel : Powerlevel [ "Normal Power" ] , timeout : 50 } ) ; const expected = buildCCBuffer ( Buffer . from ( [ PowerlevelCommand . Set , Powerlevel [ "Normal Power" ] , 0x00 , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the Set Custom power command should serialize correctly" , ( ) => { const cc = new PowerlevelCCSet ( fakeDriver , { nodeId : 2 , powerlevel : Powerlevel [ "-1 dBm" ] , timeout : 50 } ) ; const expected = buildCCBuffer ( Buffer . from ( [ PowerlevelCommand . Set , Powerlevel [ "-1 dBm" ] , 50 , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the Report command should be deserialized correctly (NormalPower)" , ( ) => { const ccData = buildCCBuffer ( Buffer . from ( [ PowerlevelCommand . Report , Powerlevel [ "Normal Power" ] , 50 , ] ) ) ; const cc = new PowerlevelCCReport ( fakeDriver , { nodeId : 5 , data : ccData } ) ; expect ( cc . powerlevel ) . toBe ( Powerlevel [ "Normal Power" ] ) ; expect ( cc . timeout ) . toBeUndefined ( ) ; } ) ; it ( "the Report command should be deserialized correctly (custom power)" , ( ) => { const ccData = buildCCBuffer ( Buffer . from ( [ PowerlevelCommand . Report , Powerlevel [ "-3 dBm" ] , 50 , ] ) ) ; const cc = new PowerlevelCCReport ( fakeDriver , { nodeId : 5 , data : ccData } ) ; expect ( cc . powerlevel ) . toBe ( Powerlevel [ "-3 dBm" ] ) ; expect ( cc . timeout ) . toBe ( 50 ) ; } ) ; it ( "deserializing an unsupported command should return an unspecified version of PowerlevelCC" , ( ) => { const serializedCC = buildCCBuffer ( Buffer . from ( [ 255 ] ) ) ; const cc = new PowerlevelCC ( fakeDriver , { nodeId : 1 , data : serializedCC } ) ; expect ( cc . constructor ) . toBe ( PowerlevelCC ) ; } ) ; } ) ;