import type { ZWaveLogContainer } from "@zwave-js/core" ; import { Mixin } from "@zwave-js/shared" ; import { EventEmitter } from "events" ; import { PassThrough } from "stream" ; import { ZWaveSerialPort } from "./ZWaveSerialPort" ; import type { ZWaveSerialPortEventCallbacks } from "./ZWaveSerialPortBase" ; const instances = new Map < string , MockSerialPort > ( ) ; @ Mixin ( [ EventEmitter ] ) class MockBinding extends PassThrough { } interface MockSerialPortEventCallbacks extends ZWaveSerialPortEventCallbacks { write : ( data : Buffer ) => void ; } type MockSerialPortEvents = Extract < keyof MockSerialPortEventCallbacks , string > ; export interface MockSerialPort { on < TEvent extends MockSerialPortEvents > ( event , callback ) : this ; addListener < TEvent extends MockSerialPortEvents > ( event , callback ) : this ; once < TEvent extends MockSerialPortEvents > ( event , callback ) : this ; off < TEvent extends MockSerialPortEvents > ( event , callback ) : this ; removeListener < TEvent extends MockSerialPortEvents > ( event , callback ) : this ; removeAllListeners ( event ? ) : this ; emit < TEvent extends MockSerialPortEvents > ( event , ... args ) : boolean ; } export class MockSerialPort extends ZWaveSerialPort { constructor ( port , loggers ) { super ( port , loggers , MockBinding as any ) ; instances . set ( port , this ) ; } public static getInstance ( port ) { return instances . get ( port ) ; } private __isOpen : boolean = false ; public get isOpen ( ) : boolean { return this . __isOpen ; } public open ( ) { return this . openStub ( ) . then ( ( ) => { this . __isOpen = true ; } ) ; } public readonly openStub : jest . Mock = jest . fn ( ( ) => Promise . resolve ( ) ) ; public close ( ) { return this . closeStub ( ) . then ( ( ) => { this . __isOpen = false ; } ) ; } public readonly closeStub : jest . Mock = jest . fn ( ( ) => Promise . resolve ( ) ) ; public receiveData ( data ) { this . serial . push ( data ) ; } public raiseError ( err ) { this . emit ( "error" , err ) ; } public writeAsync ( data ) { this . _lastWrite = data ; this . emit ( "write" , data ) ; return this . writeStub ( data ) ; } public readonly writeStub : jest . Mock = jest . fn ( ) ; private _lastWrite : string | number [ ] | Buffer | undefined ; public get lastWrite ( ) : string | number [ ] | Buffer | undefined { return this . _lastWrite ; } }