Add tests where js initialize the connection to nim

This commit is contained in:
Franck Royer 2021-03-23 11:14:51 +11:00
parent bfc1c45209
commit 4473ad4cc7
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
3 changed files with 128 additions and 52 deletions

View File

@ -3,8 +3,10 @@ import Mplex from 'libp2p-mplex';
import { bytes } from 'libp2p-noise/dist/src/@types/basic';
import { Noise } from 'libp2p-noise/dist/src/noise';
import TCP from 'libp2p-tcp';
import Multiaddr from 'multiaddr';
import PeerId from 'peer-id';
import { WakuRelay, WakuRelayPubsub } from './waku_relay';
import { CODEC, WakuRelay, WakuRelayPubsub } from './waku_relay';
export default class Waku {
private constructor(public libp2p: Libp2p, public relay: WakuRelay) {}
@ -35,6 +37,11 @@ export default class Waku {
return new Waku(libp2p, new WakuRelay(libp2p.pubsub));
}
async dialWithMultiAddr(peerId: PeerId, multiaddr: Multiaddr[]) {
this.libp2p.peerStore.addressBook.set(peerId, multiaddr);
return this.libp2p.dialProtocol(peerId, CODEC);
}
async stop() {
await this.libp2p.stop();
}

View File

@ -65,6 +65,7 @@ describe('Waku Relay', () => {
});
describe('Interop: Nim', function () {
describe('Nim connects to js', function () {
let waku: Waku;
let nimWaku: NimWaku;
@ -140,6 +141,74 @@ describe('Waku Relay', () => {
expect(Buffer.compare(payload, message.payload!)).to.eq(0);
});
});
describe('Js connects to nim', function () {
let waku: Waku;
let nimWaku: NimWaku;
beforeEach(async function () {
this.timeout(10_000);
waku = await Waku.create(NOISE_KEY_1);
nimWaku = new NimWaku(makeLogFileName(this));
await nimWaku.start();
const nimPeerId = await nimWaku.getPeerId();
await waku.dialWithMultiAddr(nimPeerId, [nimWaku.multiaddr]);
await waku.relay.subscribe();
await new Promise((resolve) =>
waku.libp2p.pubsub.once('gossipsub:heartbeat', resolve)
);
});
afterEach(async function () {
nimWaku ? nimWaku.stop() : null;
waku ? await waku.stop() : null;
});
it('nim subscribes to js', async function () {
const subscribers = waku.libp2p.pubsub.getSubscribers(TOPIC);
const nimPeerId = await nimWaku.getPeerId();
expect(subscribers).to.contain(nimPeerId.toB58String());
});
it('Js publishes to nim', async function () {
const message = Message.fromUtf8String('This is a message');
await waku.relay.publish(message);
await nimWaku.waitForLog('WakuMessage received');
const msgs = await nimWaku.messages();
expect(msgs[0].contentTopic).to.equal(message.contentTopic);
expect(msgs[0].version).to.equal(message.version);
const payload = Buffer.from(msgs[0].payload);
expect(Buffer.compare(payload, message.payload!)).to.equal(0);
});
it('Nim publishes to js', async function () {
const message = Message.fromUtf8String('Here is another message.');
const receivedPromise = waitForNextData(waku.libp2p.pubsub);
await nimWaku.sendMessage(message);
const receivedMsg = await receivedPromise;
expect(receivedMsg.contentTopic).to.eq(message.contentTopic);
expect(receivedMsg.version).to.eq(message.version);
const payload = Buffer.from(receivedMsg.payload!);
expect(Buffer.compare(payload, message.payload!)).to.eq(0);
});
});
});
});
function waitForNextData(pubsub: Pubsub): Promise<Message> {

View File

@ -42,7 +42,7 @@ export class NimWaku {
this.logPath = `${LOG_DIR}/nim-waku_${logName}.log`;
}
async start(args: Args) {
async start(args?: Args) {
try {
await existsAsync(LOG_DIR);
} catch (e) {