import { CommandClasses } from "@zwave-js/core" ; import type { Driver } from "../driver/Driver" ; import { ZWaveNode } from "../node/Node" ; import { createEmptyMockDriver } from "../test/mocks" ; import { CentralSceneCC , CentralSceneCCConfigurationGet , CentralSceneCCConfigurationReport , CentralSceneCCConfigurationSet , CentralSceneCCNotification , CentralSceneCCSupportedGet , CentralSceneCCSupportedReport , CentralSceneCommand , CentralSceneKeys , } from "./CentralSceneCC" ; function buildCCBuffer ( payload ) { return Buffer . concat ( [ Buffer . from ( [ CommandClasses [ "Central Scene" ] , ] ) , payload , ] ) ; } describe ( "lib/commandclass/CentralSceneCC => " , ( ) => { let fakeDriver ; let node1 ; beforeAll ( ( ) => { fakeDriver = createEmptyMockDriver ( ) as unknown as Driver ; node1 = new ZWaveNode ( 1 , fakeDriver as any ) ; ( fakeDriver . controller . nodes as any ) . set ( node1 . id , node1 ) ; } ) ; afterAll ( ( ) => { node1 . destroy ( ) ; } ) ; it ( "the ConfigurationGet command should serialize correctly" , ( ) => { const cc = new CentralSceneCCConfigurationGet ( fakeDriver , { nodeId : 1 } ) ; const expected = buildCCBuffer ( Buffer . from ( [ CentralSceneCommand . ConfigurationGet , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the ConfigurationSet command should serialize correctly (flags set)" , ( ) => { const cc = new CentralSceneCCConfigurationSet ( fakeDriver , { nodeId : 2 , slowRefresh : true } ) ; const expected = buildCCBuffer ( Buffer . from ( [ CentralSceneCommand . ConfigurationSet , 128 , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the ConfigurationSet command should serialize correctly (flags not set)" , ( ) => { const cc = new CentralSceneCCConfigurationSet ( fakeDriver , { nodeId : 2 , slowRefresh : false } ) ; const expected = buildCCBuffer ( Buffer . from ( [ CentralSceneCommand . ConfigurationSet , 0 , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the ConfigurationReport command should be deserialized correctly" , ( ) => { const ccData = buildCCBuffer ( Buffer . from ( [ CentralSceneCommand . ConfigurationReport , 128 , ] ) ) ; const cc = new CentralSceneCCConfigurationReport ( fakeDriver , { nodeId : 1 , data : ccData } ) ; expect ( cc . slowRefresh ) . toBe ( true ) ; } ) ; it ( "the SupportedGet command should serialize correctly" , ( ) => { const cc = new CentralSceneCCSupportedGet ( fakeDriver , { nodeId : 1 } ) ; const expected = buildCCBuffer ( Buffer . from ( [ CentralSceneCommand . SupportedGet , ] ) ) ; expect ( cc . serialize ( ) ) . toEqual ( expected ) ; } ) ; it ( "the SupportedReport command should be deserialized correctly" , ( ) => { const ccData = buildCCBuffer ( Buffer . from ( [ CentralSceneCommand . SupportedReport , 2 , 132 , 0b1 , 0b11 , 0b10101 , 0 , ] ) ) ; const cc = new CentralSceneCCSupportedReport ( fakeDriver , { nodeId : 1 , data : ccData } ) ; expect ( cc . sceneCount ) . toBe ( 2 ) ; expect ( cc . supportsSlowRefresh ) . toBeTrue ( ) ; expect ( cc . supportedKeyAttributes . size ) . toBe ( 2 ) ; expect ( cc . supportedKeyAttributes . get ( 1 ) ) . toEqual ( [ 0 , 8 , 9 ] ) ; expect ( cc . supportedKeyAttributes . get ( 2 ) ) . toEqual ( [ 0 , 2 , 4 ] ) ; } ) ; it ( "the Notification command should be deserialized correctly" , ( ) => { const ccData = buildCCBuffer ( Buffer . from ( [ CentralSceneCommand . Notification , 7 , 128 | CentralSceneKeys . KeyPressed4x , 8 , ] ) ) ; const cc = new CentralSceneCCNotification ( fakeDriver , { nodeId : 1 , data : ccData } ) ; expect ( cc . sequenceNumber ) . toBe ( 7 ) ; expect ( cc . slowRefresh ) . toBeFalsy ( ) ; expect ( cc . keyAttribute ) . toBe ( CentralSceneKeys . KeyPressed4x ) ; expect ( cc . sceneNumber ) . toBe ( 8 ) ; } ) ; it ( "the Notification command should be deserialized correctly (KeyHeldDown)" , ( ) => { const ccData = buildCCBuffer ( Buffer . from ( [ CentralSceneCommand . Notification , 7 , 128 | CentralSceneKeys . KeyHeldDown , 8 , ] ) ) ; const cc = new CentralSceneCCNotification ( fakeDriver , { nodeId : 1 , data : ccData } ) ; expect ( cc . sequenceNumber ) . toBe ( 7 ) ; expect ( cc . slowRefresh ) . toBeTrue ( ) ; expect ( cc . keyAttribute ) . toBe ( CentralSceneKeys . KeyHeldDown ) ; expect ( cc . sceneNumber ) . toBe ( 8 ) ; } ) ; it ( "deserializing an unsupported command should return an unspecified version of CentralSceneCC" , ( ) => { const serializedCC = buildCCBuffer ( Buffer . from ( [ 255 ] ) ) ; const cc = new CentralSceneCC ( fakeDriver , { nodeId : 1 , data : serializedCC } ) ; expect ( cc . constructor ) . toBe ( CentralSceneCC ) ; } ) ; } ) ;