From 13e941513d11b4ea47de27e6d95d6f9cb5d1da7e Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Fri, 19 Mar 2021 15:03:31 +1100 Subject: [PATCH] Reorg tests, add describe sections --- src/lib/waku.spec.ts | 44 +++++---- src/lib/waku_relay.spec.ts | 184 +++++++++++++++++++------------------ 2 files changed, 118 insertions(+), 110 deletions(-) diff --git a/src/lib/waku.spec.ts b/src/lib/waku.spec.ts index f0f83b6440..ceaf19f563 100644 --- a/src/lib/waku.spec.ts +++ b/src/lib/waku.spec.ts @@ -3,31 +3,35 @@ import { NimWaku } from '../test_utils/nim_waku'; import Waku from './waku'; import { CODEC } from './waku_relay'; -test('Nim-interop: nim-waku node connects to js node', async () => { - const waku = await Waku.create(); +describe('Waku', () => { + describe('Interop: Nim', () => { + test('nim connects to js', async () => { + const waku = await Waku.create(); - const peerId = waku.libp2p.peerId.toB58String(); + const peerId = waku.libp2p.peerId.toB58String(); - const localMultiaddr = waku.libp2p.multiaddrs.find((addr) => - addr.toString().match(/127\.0\.0\.1/) - ); - const multiAddrWithId = localMultiaddr + '/p2p/' + peerId; + const localMultiaddr = waku.libp2p.multiaddrs.find((addr) => + addr.toString().match(/127\.0\.0\.1/) + ); + const multiAddrWithId = localMultiaddr + '/p2p/' + peerId; - const nimWaku = new NimWaku(expect.getState().currentTestName); - await nimWaku.start({ staticnode: multiAddrWithId }); + const nimWaku = new NimWaku(expect.getState().currentTestName); + await nimWaku.start({ staticnode: multiAddrWithId }); - const nimPeers = await nimWaku.peers(); + const nimPeers = await nimWaku.peers(); - expect(nimPeers).toEqual([ - { - multiaddr: multiAddrWithId, - protocol: CODEC, - connected: true, - }, - ]); + expect(nimPeers).toEqual([ + { + multiaddr: multiAddrWithId, + protocol: CODEC, + connected: true, + }, + ]); - const nimPeerId = await nimWaku.getPeerId(); - const jsPeers = waku.libp2p.peerStore.peers; + const nimPeerId = await nimWaku.getPeerId(); + const jsPeers = waku.libp2p.peerStore.peers; - expect(jsPeers.has(nimPeerId.toB58String())).toBeTruthy(); + expect(jsPeers.has(nimPeerId.toB58String())).toBeTruthy(); + }); + }); }); diff --git a/src/lib/waku_relay.spec.ts b/src/lib/waku_relay.spec.ts index 374c165e74..a56d1966af 100644 --- a/src/lib/waku_relay.spec.ts +++ b/src/lib/waku_relay.spec.ts @@ -8,135 +8,139 @@ import Waku from './waku'; import { Message } from './waku_message'; import { CODEC, TOPIC } from './waku_relay'; -// TODO: Fix this, see https://github.com/ChainSafe/js-libp2p-gossipsub/issues/151 -test.skip('Publishes message', async () => { - const message = Message.fromUtf8String('Bird bird bird, bird is the word!'); +describe('Waku Relay', () => { + // TODO: Fix this, see https://github.com/ChainSafe/js-libp2p-gossipsub/issues/151 + test.skip('Publish', async () => { + const message = Message.fromUtf8String('Bird bird bird, bird is the word!'); - const [waku1, waku2] = await Promise.all([Waku.create(), Waku.create()]); + const [waku1, waku2] = await Promise.all([Waku.create(), Waku.create()]); - // Add node's 2 data to the PeerStore - waku1.libp2p.peerStore.addressBook.set( - waku2.libp2p.peerId, - waku2.libp2p.multiaddrs - ); - await waku1.libp2p.dial(waku2.libp2p.peerId); + // Add node's 2 data to the PeerStore + waku1.libp2p.peerStore.addressBook.set( + waku2.libp2p.peerId, + waku2.libp2p.multiaddrs + ); + await waku1.libp2p.dial(waku2.libp2p.peerId); - await waku2.relay.subscribe(); - await new Promise((resolve) => - waku2.libp2p.pubsub.once('pubsub:subscription-change', (...args) => - resolve(args) - ) - ); + await waku2.relay.subscribe(); + await new Promise((resolve) => + waku2.libp2p.pubsub.once('pubsub:subscription-change', (...args) => + resolve(args) + ) + ); - // Setup the promise before publishing to ensure the event is not missed - const promise = waitForNextData(waku1.libp2p.pubsub); + // Setup the promise before publishing to ensure the event is not missed + const promise = waitForNextData(waku1.libp2p.pubsub); - await waku2.relay.publish(message); + await waku2.relay.publish(message); - const node1Received = await promise; + const node1Received = await promise; - expect(node1Received.isEqualTo(message)).toBeTruthy(); -}); + expect(node1Received.isEqualTo(message)).toBeTruthy(); + }); -test('Registers waku relay protocol', async () => { - const waku = await Waku.create(); + test('Registers waku relay protocol', async () => { + const waku = await Waku.create(); - const protocols = Array.from(waku.libp2p.upgrader.protocols.keys()); + const protocols = Array.from(waku.libp2p.upgrader.protocols.keys()); - expect(protocols.findIndex((value) => value == CODEC)).toBeTruthy(); -}); + expect(protocols.findIndex((value) => value == CODEC)).toBeTruthy(); + }); -test('Does not register any sub protocol', async () => { - const waku = await Waku.create(); + test('Does not register any sub protocol', async () => { + const waku = await Waku.create(); - const protocols = Array.from(waku.libp2p.upgrader.protocols.keys()); - expect(protocols.findIndex((value) => value.match(/sub/))).toBeTruthy(); -}); + const protocols = Array.from(waku.libp2p.upgrader.protocols.keys()); + expect(protocols.findIndex((value) => value.match(/sub/))).toBeTruthy(); + }); -test('Nim-interop: js node receives default subscription from nim node', async () => { - const waku = await Waku.create(); + describe('Interop: Nim', () => { + test('nim subscribes to js', async () => { + const waku = await Waku.create(); - const peerId = waku.libp2p.peerId.toB58String(); + const peerId = waku.libp2p.peerId.toB58String(); - const localMultiaddr = waku.libp2p.multiaddrs.find((addr) => - addr.toString().match(/127\.0\.0\.1/) - ); - const multiAddrWithId = localMultiaddr + '/p2p/' + peerId; + const localMultiaddr = waku.libp2p.multiaddrs.find((addr) => + addr.toString().match(/127\.0\.0\.1/) + ); + const multiAddrWithId = localMultiaddr + '/p2p/' + peerId; - const nimWaku = new NimWaku(expect.getState().currentTestName); - await nimWaku.start({ staticnode: multiAddrWithId }); + const nimWaku = new NimWaku(expect.getState().currentTestName); + await nimWaku.start({ staticnode: multiAddrWithId }); - const nimPeerId = await nimWaku.getPeerId(); - const subscribers = waku.libp2p.pubsub.getSubscribers(TOPIC); + const nimPeerId = await nimWaku.getPeerId(); + const subscribers = waku.libp2p.pubsub.getSubscribers(TOPIC); - expect(subscribers).toContain(nimPeerId.toB58String()); -}); + expect(subscribers).toContain(nimPeerId.toB58String()); + }); -test('Nim-interop: js node sends message to nim node', async () => { - const message = Message.fromUtf8String('This is a message'); - const waku = await Waku.create(); + test('Js publishes to nim', async () => { + const message = Message.fromUtf8String('This is a message'); + const waku = await Waku.create(); - // TODO: nim-waku does follow the `StrictNoSign` policy hence we need to change - // it for nim-waku to process our messages. Can be removed once - // https://github.com/status-im/nim-waku/issues/422 is fixed - waku.libp2p.pubsub.globalSignaturePolicy = 'StrictSign'; + // TODO: nim-waku does follow the `StrictNoSign` policy hence we need to change + // it for nim-waku to process our messages. Can be removed once + // https://github.com/status-im/nim-waku/issues/422 is fixed + waku.libp2p.pubsub.globalSignaturePolicy = 'StrictSign'; - const peerId = waku.libp2p.peerId.toB58String(); - const localMultiaddr = waku.libp2p.multiaddrs.find((addr) => - addr.toString().match(/127\.0\.0\.1/) - ); - const multiAddrWithId = localMultiaddr + '/p2p/' + peerId; + const peerId = waku.libp2p.peerId.toB58String(); + const localMultiaddr = waku.libp2p.multiaddrs.find((addr) => + addr.toString().match(/127\.0\.0\.1/) + ); + const multiAddrWithId = localMultiaddr + '/p2p/' + peerId; - const nimWaku = new NimWaku(expect.getState().currentTestName); - await nimWaku.start({ staticnode: multiAddrWithId }); + const nimWaku = new NimWaku(expect.getState().currentTestName); + await nimWaku.start({ staticnode: multiAddrWithId }); - await patchPeerStore(nimWaku, waku.libp2p); + await patchPeerStore(nimWaku, waku.libp2p); - await waku.relay.publish(message); + await waku.relay.publish(message); - await nimWaku.waitForLog('WakuMessage received'); + await nimWaku.waitForLog('WakuMessage received'); - const msgs = await nimWaku.messages(); + const msgs = await nimWaku.messages(); - expect(msgs[0].contentTopic).toEqual(message.contentTopic); - expect(msgs[0].version).toEqual(message.version); + expect(msgs[0].contentTopic).toEqual(message.contentTopic); + expect(msgs[0].version).toEqual(message.version); - const payload = Buffer.from(msgs[0].payload); - expect(Buffer.compare(payload, message.payload)).toBe(0); -}); + const payload = Buffer.from(msgs[0].payload); + expect(Buffer.compare(payload, message.payload)).toBe(0); + }); -test('Nim-interop: nim node sends message to js node', async () => { - const message = Message.fromUtf8String('Here is another message.'); - const waku = await Waku.create(); + test('Nim publishes to js', async () => { + const message = Message.fromUtf8String('Here is another message.'); + const waku = await Waku.create(); - const peerId = waku.libp2p.peerId.toB58String(); - const localMultiaddr = waku.libp2p.multiaddrs.find((addr) => - addr.toString().match(/127\.0\.0\.1/) - ); - const multiAddrWithId = localMultiaddr + '/p2p/' + peerId; + const peerId = waku.libp2p.peerId.toB58String(); + const localMultiaddr = waku.libp2p.multiaddrs.find((addr) => + addr.toString().match(/127\.0\.0\.1/) + ); + const multiAddrWithId = localMultiaddr + '/p2p/' + peerId; - const nimWaku = new NimWaku(expect.getState().currentTestName); - await nimWaku.start({ staticnode: multiAddrWithId }); + const nimWaku = new NimWaku(expect.getState().currentTestName); + await nimWaku.start({ staticnode: multiAddrWithId }); - await patchPeerStore(nimWaku, waku.libp2p); + await patchPeerStore(nimWaku, waku.libp2p); - await waku.relay.subscribe(); + await waku.relay.subscribe(); - await new Promise((resolve) => - waku.libp2p.pubsub.once('gossipsub:heartbeat', resolve) - ); + await new Promise((resolve) => + waku.libp2p.pubsub.once('gossipsub:heartbeat', resolve) + ); - const receivedPromise = waitForNextData(waku.libp2p.pubsub); + const receivedPromise = waitForNextData(waku.libp2p.pubsub); - await nimWaku.sendMessage(message); + await nimWaku.sendMessage(message); - const receivedMsg = await receivedPromise; + const receivedMsg = await receivedPromise; - expect(receivedMsg.contentTopic).toBe(message.contentTopic); - expect(receivedMsg.version).toBe(message.version); + expect(receivedMsg.contentTopic).toBe(message.contentTopic); + expect(receivedMsg.version).toBe(message.version); - const payload = Buffer.from(receivedMsg.payload); - expect(Buffer.compare(payload, message.payload)).toBe(0); + const payload = Buffer.from(receivedMsg.payload); + expect(Buffer.compare(payload, message.payload)).toBe(0); + }); + }); }); function waitForNextData(pubsub: Pubsub): Promise {