import { MessageHeaders , MockSerialPort } from "@zwave-js/serial" ; import { getEnumMemberName } from "@zwave-js/shared" ; import { wait } from "alcalzone-shared/async" ; import type { Driver } from "../../driver/Driver" ; import { FunctionType } from "../../message/Constants" ; import { ZWaveNode } from "../../node/Node" ; import { NodeStatus } from "../../node/Types" ; import { createAndStartDriver } from "../utils" ; function isFunctionSupported ( fn ) { switch ( fn ) { case FunctionType . SendDataBridge : case FunctionType . SendDataMulticastBridge : return false ; } return true ; } describe ( "When a ping succeeds, the node should be marked awake/alive" , ( ) => { let driver ; let serialport ; process . env . LOGLEVEL = "debug" ; beforeEach ( async ( ) => { ( { driver , serialport } = await createAndStartDriver ( ) ) ; driver [ "_controller" ] = { ownNodeId : 1 , isFunctionSupported , nodes : new Map ( ) , incrementStatistics : ( ) => { } , } as any ; } ) ; afterEach ( async ( ) => { await driver . destroy ( ) ; driver . removeAllListeners ( ) ; } ) ; for ( const initialStatus of [ NodeStatus . Unknown , NodeStatus . Asleep , NodeStatus . Dead , ] ) { for ( const canSleep of [ true , false ] ) { if ( ( initialStatus === NodeStatus . Asleep && ! canSleep ) || ( canSleep && initialStatus === NodeStatus . Dead ) ) { continue ; } const expectedStatus = canSleep ? NodeStatus . Awake : NodeStatus . Alive ; it ( `Can sleep: ${ canSleep }, initial status: ${ getEnumMemberName ( NodeStatus , initialStatus ) }` , async ( ) => { jest . setTimeout ( 5000 ) ; const node4 = new ZWaveNode ( 4 , driver ) ; ( driver . controller . nodes as Map < number , ZWaveNode > ) . set ( node4 . id , node4 ) ; for ( const node of driver . controller . nodes . values ( ) ) { driver [ "addNodeEventHandlers" ] ( node ) ; } if ( canSleep ) { node4 [ "_isListening" ] = false ; node4 [ "_isFrequentListening" ] = false ; } else { node4 [ "_isListening" ] = true ; node4 [ "_isFrequentListening" ] = false ; } if ( initialStatus === NodeStatus . Asleep ) { node4 . markAsAsleep ( ) ; } else if ( initialStatus === NodeStatus . Dead ) { node4 . markAsDead ( ) ; } expect ( node4 . status ) . toBe ( initialStatus ) ; const ACK = Buffer . from ( [ MessageHeaders . ACK ] ) ; const pingPromise = node4 . ping ( ) ; await wait ( 1 ) ; expect ( serialport . lastWrite ) . toEqual ( Buffer . from ( "010800130401002501c5" , "hex" ) ) ; await wait ( 10 ) ; serialport . receiveData ( ACK ) ; await wait ( 10 ) ; serialport . receiveData ( Buffer . from ( "0104011301e8" , "hex" ) ) ; expect ( serialport . lastWrite ) . toEqual ( ACK ) ; await wait ( 10 ) ; serialport . receiveData ( Buffer . from ( "011800130100000100bd7f7f7f7f010103000000000201000049" , "hex" ) ) ; expect ( serialport . lastWrite ) . toEqual ( ACK ) ; await wait ( 10 ) ; await pingPromise ; expect ( node4 . status ) . toBe ( expectedStatus ) ; } ) ; } } } ) ;