From 82043f530809785bcdc50554e0b9337078c6d7c8 Mon Sep 17 00:00:00 2001 From: "Cimon Lucas (LCM)" Date: Wed, 16 Nov 2022 15:16:55 +0100 Subject: [PATCH] Adding MockPortBinding.writeToPort & MockBinding.getOpenMockPort() --- lib/index-test.ts | 48 ++++++++++++++++++++++++++++++++++++++++++++++- lib/index.ts | 20 +++++++++++++++++++- 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/lib/index-test.ts b/lib/index-test.ts index 43ad031..49346b5 100644 --- a/lib/index-test.ts +++ b/lib/index-test.ts @@ -27,7 +27,7 @@ const openOptions: OpenOptions = { } describe('MockBinding', () => { - afterEach(() => { + beforeEach(() => { MockBinding.reset() }) @@ -88,5 +88,51 @@ describe('MockBinding', () => { assert.strictEqual(port.port.info.serialNumber, '1') }) }) + describe('getOpenMockPort', () => { + beforeEach(async () => { + MockBinding.reset() + }) + + it('should return a value for an existing open port', async () => { + MockBinding.createPort('/dev/exists') + await MockBinding.open(openOptions) + const openPort = MockBinding.getOpenMockPort('/dev/exists') + assert.strictEqual(openPort.port.info.serialNumber, '1') + }) + + it('should return undefined for an unknown port path', async () => { + const openPort = MockBinding.getOpenMockPort('/dev/unknown') + assert.strictEqual(openPort, undefined) + }) + + it('should return undefined for an existing port that is not open', async () => { + MockBinding.createPort('/dev/exists') + const openPort = MockBinding.getOpenMockPort('/dev/exists') + assert.strictEqual(openPort, undefined) + }) + }) + }) +}) + +describe('MockPortBinding', () => { + beforeEach(() => { + MockBinding.reset() + }) + + describe('instance property', () => { + describe('writeToPort', () => { + it('can send data from one port to another', async () => { + MockBinding.createPort('/dev/exists') + const port1 = await MockBinding.open(openOptions) + const port2 = await MockBinding.open(openOptions) + port1.writeToPort = port2 + + const message = Buffer.from('MSG') + await port1.write(message) + const receivingBuffer = Buffer.alloc(message.length) + await port2.read(receivingBuffer, 0, message.length) + assert.isTrue(receivingBuffer.equals(message)) + }) + }) }) }) diff --git a/lib/index.ts b/lib/index.ts index cedb871..db74ae2 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -25,6 +25,9 @@ export interface CreatePortOptions { let ports: { [key: string]: MockPortInternal } = {} +let openMockPortBindings: { + [key: string]: MockPortBinding +} = {}; let serialNumber = 0 function resolveNextTick() { @@ -42,11 +45,13 @@ export class CanceledError extends Error { export interface MockBindingInterface extends BindingInterface { reset(): void createPort(path: string, opt?: CreatePortOptions): void + getOpenMockPort(path: string): MockPortBinding } export const MockBinding: MockBindingInterface = { reset() { ports = {} + openMockPortBindings = {} serialNumber = 0 }, @@ -82,6 +87,10 @@ export const MockBinding: MockBindingInterface = { debug(serialNumber, 'created port', JSON.stringify({ path, opt: options })) }, + getOpenMockPort(path) { + return openMockPortBindings[path]; + }, + async list() { debug(null, 'list') return Object.values(ports).map(port => port.info) @@ -133,7 +142,9 @@ export const MockBinding: MockBindingInterface = { port.openOpt = { ...openOptions } - return new MockPortBinding(port, openOptions) + const portBinding = new MockPortBinding(port, openOptions) + openMockPortBindings[path] = portBinding + return portBinding }, } @@ -149,6 +160,7 @@ export class MockPortBinding implements BindingPortInterface { writeOperation: null | Promise isOpen: boolean serialNumber?: string + writeToPort?: MockPortBinding constructor(port: MockPortInternal, openOptions: Required) { this.port = port @@ -289,6 +301,12 @@ export class MockPortBinding implements BindingPortInterface { this.emitData(data) } }) + } else if (this.writeToPort) { + process.nextTick(() => { + if (this.writeToPort.isOpen) { + this.writeToPort.emitData(data); + } + }); } this.writeOperation = null debug(this.serialNumber, 'writing finished')