js-waku/src/lib/node.spec.ts

146 lines
4.1 KiB
TypeScript
Raw Normal View History

2021-03-10 14:24:23 +11:00
import test from 'ava';
2021-03-10 14:55:16 +11:00
import Pubsub from 'libp2p-interfaces/src/pubsub';
2021-03-10 14:24:23 +11:00
2021-03-10 17:39:53 +11:00
import { delay } from '../test_utils/delay';
import { NimWaku } from '../test_utils/nim_waku';
2021-03-10 14:24:23 +11:00
import { createNode } from './node';
2021-03-10 16:22:49 +11:00
import { Message } from './waku_message';
import { CODEC, TOPIC, WakuRelay } from './waku_relay';
2021-03-10 14:24:23 +11:00
test('Publishes message', async (t) => {
2021-03-10 16:22:49 +11:00
const message = Message.fromString('Bird bird bird, bird is the word!');
2021-03-10 14:24:23 +11:00
const [node1, node2] = await Promise.all([createNode(), createNode()]);
2021-03-10 16:22:49 +11:00
const wakuRelayNode1 = new WakuRelay(node1.pubsub);
const wakuRelayNode2 = new WakuRelay(node2.pubsub);
2021-03-10 14:24:23 +11:00
// Add node's 2 data to the PeerStore
node1.peerStore.addressBook.set(node2.peerId, node2.multiaddrs);
await node1.dial(node2.peerId);
await wakuRelayNode1.subscribe();
await wakuRelayNode2.subscribe();
2021-03-10 14:24:23 +11:00
2021-03-10 14:55:16 +11:00
// Setup the promise before publishing to ensure the event is not missed
const promise = waitForNextData(node1.pubsub);
2021-03-10 14:24:23 +11:00
2021-03-10 14:55:16 +11:00
await delay(500);
2021-03-10 14:24:23 +11:00
await wakuRelayNode2.publish(message);
2021-03-10 14:24:23 +11:00
2021-03-10 14:55:16 +11:00
const node1Received = await promise;
2021-03-10 14:24:23 +11:00
2021-03-10 16:22:49 +11:00
t.true(node1Received.isEqualTo(message));
2021-03-10 14:24:23 +11:00
});
2021-03-10 14:55:16 +11:00
test('Registers waku relay protocol', async (t) => {
const node = await createNode();
const protocols = Array.from(node.upgrader.protocols.keys());
t.truthy(protocols.findIndex((value) => value == CODEC));
});
test('Does not register any sub protocol', async (t) => {
const node = await createNode();
const protocols = Array.from(node.upgrader.protocols.keys());
t.truthy(protocols.findIndex((value) => value.match(/sub/)));
});
test('Nim-interop: nim-waku node connects to js node', async (t) => {
2021-03-10 17:39:53 +11:00
const node = await createNode();
2021-03-11 10:54:35 +11:00
const peerId = node.peerId.toB58String();
const localMultiaddr = node.multiaddrs.find((addr) =>
addr.toString().match(/127\.0\.0\.1/)
);
const multiAddrWithId = localMultiaddr + '/p2p/' + peerId;
const nimWaku = new NimWaku();
await nimWaku.start({ staticnode: multiAddrWithId });
2021-03-10 17:39:53 +11:00
const nimPeers = await nimWaku.peers();
2021-03-10 17:39:53 +11:00
t.deepEqual(nimPeers, [
{
multiaddr: multiAddrWithId,
protocol: CODEC,
connected: true,
},
]);
const nimAddress = await nimWaku.info().then((info) => info.listenStr);
const nimPeerId = nimAddress.match(/[\d\w]+$/)[0];
const jsPeers = node.peerStore.peers;
t.true(jsPeers.has(nimPeerId));
2021-03-10 17:39:53 +11:00
});
test('Nim-interop: js node subscribes to default waku topic (only checking js side)', async (t) => {
const node = await createNode();
const peerId = node.peerId.toB58String();
const localMultiaddr = node.multiaddrs.find((addr) =>
addr.toString().match(/127\.0\.0\.1/)
);
const multiAddrWithId = localMultiaddr + '/p2p/' + peerId;
const nimWaku = new NimWaku();
await nimWaku.start({ staticnode: multiAddrWithId });
const wakuRelayNode = new WakuRelay(node.pubsub);
await wakuRelayNode.subscribe();
const nimAddress = await nimWaku.info().then((info) => info.listenStr);
const nimPeerId = nimAddress.match(/[\d\w]+$/)[0];
const subscribers = node.pubsub.getSubscribers(TOPIC);
t.true(subscribers.includes(nimPeerId));
});
test('Nim-interop: nim node sends message', async (t) => {
const node = await createNode();
const peerId = node.peerId.toB58String();
console.log(`js peer id: ${peerId}`);
const localMultiaddr = node.multiaddrs.find((addr) =>
addr.toString().match(/127\.0\.0\.1/)
);
const multiAddrWithId = localMultiaddr + '/p2p/' + peerId;
const nimWaku = new NimWaku();
await nimWaku.start({ staticnode: multiAddrWithId });
const wakuRelayNode = new WakuRelay(node.pubsub);
await wakuRelayNode.subscribe();
// Setup the promise before publishing to ensure the event is not missed
const promise = waitForNextData(node.pubsub);
const message = Message.fromString('This is a message.');
await delay(500);
await nimWaku.sendMessage(message);
await delay(1000);
const received = await promise;
t.true(received.isEqualTo(message));
});
2021-03-10 16:22:49 +11:00
function waitForNextData(pubsub: Pubsub): Promise<Message> {
2021-03-10 14:55:16 +11:00
return new Promise((resolve) => {
pubsub.once(TOPIC, resolve);
2021-03-10 14:56:12 +11:00
}).then((msg: any) => {
2021-03-10 16:22:49 +11:00
return Message.fromBinary(msg.data);
2021-03-10 14:55:16 +11:00
});
}