js-waku/src/lib/node.spec.ts
2021-03-10 15:08:27 +11:00

53 lines
1.5 KiB
TypeScript

import { TextDecoder, TextEncoder } from 'util';
import test from 'ava';
import Pubsub from 'libp2p-interfaces/src/pubsub';
import { createNode } from './node';
import { CODEC } from './waku_relay';
function delay(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
test('Can publish message', async (t) => {
const topic = 'news';
const message = 'Bird bird bird, bird is the word!';
const [node1, node2] = await Promise.all([createNode(), createNode()]);
// Add node's 2 data to the PeerStore
node1.peerStore.addressBook.set(node2.peerId, node2.multiaddrs);
await node1.dial(node2.peerId);
await node1.pubsub.subscribe(topic);
await node2.pubsub.subscribe(topic);
// Setup the promise before publishing to ensure the event is not missed
// TODO: Is it possible to import `Message` type?
const promise = waitForNextData(node1.pubsub, topic);
await delay(500);
await node2.pubsub.publish(topic, new TextEncoder().encode(message));
const node1Received = await promise;
t.deepEqual(node1Received, message);
});
test('Register waku relay protocol', async (t) => {
const node = await createNode();
const protocols = Array.from(node.upgrader.protocols.keys());
t.truthy(protocols.findIndex((value) => value == CODEC));
});
function waitForNextData(pubsub: Pubsub, topic: string) {
return new Promise((resolve) => {
pubsub.once(topic, resolve);
}).then((msg: any) => {
return new TextDecoder().decode(msg.data);
});
}