From c335c214c2a94e62382987e6154eca1fba450261 Mon Sep 17 00:00:00 2001 From: zetxx Date: Mon, 26 Jul 2021 23:48:11 +0300 Subject: [PATCH] fix: tests --- lib/bridge/index.js | 44 ++++++++++++++++++++------------ lib/methods/index.js | 7 +++++ tests/unit/bridge/test.bridge.js | 30 +++++++++++++++++++--- 3 files changed, 61 insertions(+), 20 deletions(-) diff --git a/lib/bridge/index.js b/lib/bridge/index.js index f4b98f9..f61e9c3 100644 --- a/lib/bridge/index.js +++ b/lib/bridge/index.js @@ -8,19 +8,36 @@ const directions = { }; let id = 0; +const callMethod = (methods) => async({ + direction, + packet +}) => { + try { + return { + payload: await methods.call({ + direction, + packet + }) + }; + } catch (error) { + return {error, payload: undefined}; + } +}; + class Bridge { constructor({ config: { request = { waitTime: 30000 - } + }, + id: bridgeId } = {} } = {}) { this.config = { request }; this.methods = Methods(); - this.nodeId = Symbol(++id); + this.nodeId = Symbol(bridgeId || ++id); this.requests = Request({ nodeId: this.nodeId, config: this.config.request @@ -61,21 +78,13 @@ class Bridge { async pass({direction, packet}) { const {match} = packet; const requestFound = this.requests.find(packet); - const methodCallResult = (async() => { - try { - return { - payload: await this.methods.call({ - direction, - packet - }) - }; - } catch (error) { - return {error, payload: undefined}; - } - })(); + const methodCallResult = await callMethod(this.methods)({ + direction, + packet + }); const packetNew = { ...packet, - ...(await methodCallResult), + ...methodCallResult, match: undefined }; if (!requestFound) { @@ -112,7 +121,10 @@ class Bridge { request: requestFound, match }); - + } + + async destroy() { + this.requests.destroy(); } } diff --git a/lib/methods/index.js b/lib/methods/index.js index 5571807..c994e90 100644 --- a/lib/methods/index.js +++ b/lib/methods/index.js @@ -1,5 +1,12 @@ const {NotFound} = require('./errors'); +/** + * method name should be in following pattern + * method.direction + * if real name of the method is 'a' and direction is 'in' + * method name should be 'a.in' +*/ + module.exports = () => { const methods = {}; const o = { diff --git a/tests/unit/bridge/test.bridge.js b/tests/unit/bridge/test.bridge.js index 711bc5e..ccaddfc 100644 --- a/tests/unit/bridge/test.bridge.js +++ b/tests/unit/bridge/test.bridge.js @@ -1,10 +1,32 @@ const tap = require('tap'); -const bridge = require('../../../lib/bridge'); +const Bridge = require('../../../lib/bridge'); -tap.test('Bridge', (l1) => { - l1.test('1', (t) => { +tap.test('Bridge', (l0) => { + // l0.test('Simple checks and coverage', (t) => { + // const bridge = new Bridge(); + // bridge.start({}); + // bridge.destroy(); + // t.end(); + // }); + + l0.test('Method call positive cases', (t) => { + // set up + const bridgeA = new Bridge({config: {id: 'bridgeA'}}); + const bridgeB = new Bridge({config: {id: 'bridgeB'}}); + bridgeA.start({other: bridgeB}); + bridgeB.start({other: bridgeA}); + // register methods + bridgeA.methods.add({method: 'a.in', fn: ({payload}) => { + return payload; + }}); + bridgeB.methods.add({method: 'a.out', fn: ({payload}) => { + return payload; + }}); + + bridgeA.pass({direction: 'in', packet: {meta: {method: 'a'}, payload: {a: 1}}}); t.end(); }); - l1.end(); + + l0.end(); });