mirror of https://github.com/waku-org/js-waku.git
Add tests where js initialize the connection to nim
This commit is contained in:
parent
bfc1c45209
commit
4473ad4cc7
|
@ -3,8 +3,10 @@ import Mplex from 'libp2p-mplex';
|
||||||
import { bytes } from 'libp2p-noise/dist/src/@types/basic';
|
import { bytes } from 'libp2p-noise/dist/src/@types/basic';
|
||||||
import { Noise } from 'libp2p-noise/dist/src/noise';
|
import { Noise } from 'libp2p-noise/dist/src/noise';
|
||||||
import TCP from 'libp2p-tcp';
|
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 {
|
export default class Waku {
|
||||||
private constructor(public libp2p: Libp2p, public relay: WakuRelay) {}
|
private constructor(public libp2p: Libp2p, public relay: WakuRelay) {}
|
||||||
|
@ -35,6 +37,11 @@ export default class Waku {
|
||||||
return new Waku(libp2p, new WakuRelay(libp2p.pubsub));
|
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() {
|
async stop() {
|
||||||
await this.libp2p.stop();
|
await this.libp2p.stop();
|
||||||
}
|
}
|
||||||
|
|
|
@ -65,79 +65,148 @@ describe('Waku Relay', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Interop: Nim', function () {
|
describe('Interop: Nim', function () {
|
||||||
let waku: Waku;
|
describe('Nim connects to js', function () {
|
||||||
let nimWaku: NimWaku;
|
let waku: Waku;
|
||||||
|
let nimWaku: NimWaku;
|
||||||
|
|
||||||
beforeEach(async function () {
|
beforeEach(async function () {
|
||||||
this.timeout(10_000);
|
this.timeout(10_000);
|
||||||
waku = await Waku.create(NOISE_KEY_1);
|
waku = await Waku.create(NOISE_KEY_1);
|
||||||
|
|
||||||
const peerId = waku.libp2p.peerId.toB58String();
|
const peerId = waku.libp2p.peerId.toB58String();
|
||||||
const localMultiaddr = waku.libp2p.multiaddrs.find((addr) =>
|
const localMultiaddr = waku.libp2p.multiaddrs.find((addr) =>
|
||||||
addr.toString().match(/127\.0\.0\.1/)
|
addr.toString().match(/127\.0\.0\.1/)
|
||||||
);
|
);
|
||||||
const multiAddrWithId = localMultiaddr + '/p2p/' + peerId;
|
const multiAddrWithId = localMultiaddr + '/p2p/' + peerId;
|
||||||
|
|
||||||
nimWaku = new NimWaku(makeLogFileName(this));
|
nimWaku = new NimWaku(makeLogFileName(this));
|
||||||
await nimWaku.start({ staticnode: multiAddrWithId });
|
await nimWaku.start({ staticnode: multiAddrWithId });
|
||||||
|
|
||||||
await waku.relay.subscribe();
|
await waku.relay.subscribe();
|
||||||
await new Promise((resolve) =>
|
await new Promise((resolve) =>
|
||||||
waku.libp2p.pubsub.once('gossipsub:heartbeat', 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 nimPeerId = await nimWaku.getPeerId();
|
||||||
|
const subscribers = waku.libp2p.pubsub.getSubscribers(TOPIC);
|
||||||
|
|
||||||
|
expect(subscribers).to.contain(nimPeerId.toB58String());
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Js publishes to nim', async function () {
|
||||||
|
this.timeout(5000);
|
||||||
|
|
||||||
|
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 () {
|
||||||
|
this.timeout(5000);
|
||||||
|
const message = Message.fromUtf8String('Here is another message.');
|
||||||
|
|
||||||
|
await waku.relay.subscribe();
|
||||||
|
|
||||||
|
await new Promise((resolve) =>
|
||||||
|
waku.libp2p.pubsub.once('gossipsub:heartbeat', resolve)
|
||||||
|
);
|
||||||
|
|
||||||
|
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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(async function () {
|
describe('Js connects to nim', function () {
|
||||||
nimWaku ? nimWaku.stop() : null;
|
let waku: Waku;
|
||||||
waku ? await waku.stop() : null;
|
let nimWaku: NimWaku;
|
||||||
});
|
|
||||||
|
|
||||||
it('nim subscribes to js', async function () {
|
beforeEach(async function () {
|
||||||
const nimPeerId = await nimWaku.getPeerId();
|
this.timeout(10_000);
|
||||||
const subscribers = waku.libp2p.pubsub.getSubscribers(TOPIC);
|
waku = await Waku.create(NOISE_KEY_1);
|
||||||
|
|
||||||
expect(subscribers).to.contain(nimPeerId.toB58String());
|
nimWaku = new NimWaku(makeLogFileName(this));
|
||||||
});
|
await nimWaku.start();
|
||||||
|
|
||||||
it('Js publishes to nim', async function () {
|
const nimPeerId = await nimWaku.getPeerId();
|
||||||
this.timeout(5000);
|
|
||||||
|
|
||||||
const message = Message.fromUtf8String('This is a message');
|
await waku.dialWithMultiAddr(nimPeerId, [nimWaku.multiaddr]);
|
||||||
|
|
||||||
await waku.relay.publish(message);
|
await waku.relay.subscribe();
|
||||||
|
|
||||||
await nimWaku.waitForLog('WakuMessage received');
|
await new Promise((resolve) =>
|
||||||
|
waku.libp2p.pubsub.once('gossipsub:heartbeat', resolve)
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
const msgs = await nimWaku.messages();
|
afterEach(async function () {
|
||||||
|
nimWaku ? nimWaku.stop() : null;
|
||||||
|
waku ? await waku.stop() : null;
|
||||||
|
});
|
||||||
|
|
||||||
expect(msgs[0].contentTopic).to.equal(message.contentTopic);
|
it('nim subscribes to js', async function () {
|
||||||
expect(msgs[0].version).to.equal(message.version);
|
const subscribers = waku.libp2p.pubsub.getSubscribers(TOPIC);
|
||||||
|
|
||||||
const payload = Buffer.from(msgs[0].payload);
|
const nimPeerId = await nimWaku.getPeerId();
|
||||||
expect(Buffer.compare(payload, message.payload!)).to.equal(0);
|
expect(subscribers).to.contain(nimPeerId.toB58String());
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Nim publishes to js', async function () {
|
it('Js publishes to nim', async function () {
|
||||||
this.timeout(5000);
|
const message = Message.fromUtf8String('This is a message');
|
||||||
const message = Message.fromUtf8String('Here is another message.');
|
|
||||||
|
|
||||||
await waku.relay.subscribe();
|
await waku.relay.publish(message);
|
||||||
|
|
||||||
await new Promise((resolve) =>
|
await nimWaku.waitForLog('WakuMessage received');
|
||||||
waku.libp2p.pubsub.once('gossipsub:heartbeat', resolve)
|
|
||||||
);
|
|
||||||
|
|
||||||
const receivedPromise = waitForNextData(waku.libp2p.pubsub);
|
const msgs = await nimWaku.messages();
|
||||||
|
|
||||||
await nimWaku.sendMessage(message);
|
expect(msgs[0].contentTopic).to.equal(message.contentTopic);
|
||||||
|
expect(msgs[0].version).to.equal(message.version);
|
||||||
|
|
||||||
const receivedMsg = await receivedPromise;
|
const payload = Buffer.from(msgs[0].payload);
|
||||||
|
expect(Buffer.compare(payload, message.payload!)).to.equal(0);
|
||||||
|
});
|
||||||
|
|
||||||
expect(receivedMsg.contentTopic).to.eq(message.contentTopic);
|
it('Nim publishes to js', async function () {
|
||||||
expect(receivedMsg.version).to.eq(message.version);
|
const message = Message.fromUtf8String('Here is another message.');
|
||||||
|
|
||||||
const payload = Buffer.from(receivedMsg.payload!);
|
const receivedPromise = waitForNextData(waku.libp2p.pubsub);
|
||||||
expect(Buffer.compare(payload, message.payload!)).to.eq(0);
|
|
||||||
|
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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -42,7 +42,7 @@ export class NimWaku {
|
||||||
this.logPath = `${LOG_DIR}/nim-waku_${logName}.log`;
|
this.logPath = `${LOG_DIR}/nim-waku_${logName}.log`;
|
||||||
}
|
}
|
||||||
|
|
||||||
async start(args: Args) {
|
async start(args?: Args) {
|
||||||
try {
|
try {
|
||||||
await existsAsync(LOG_DIR);
|
await existsAsync(LOG_DIR);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
Loading…
Reference in New Issue