mirror of https://github.com/waku-org/js-waku.git
tests
This commit is contained in:
parent
f8213e425f
commit
3194a4cd84
|
@ -26,72 +26,51 @@ describe("Events", function () {
|
||||||
await tearDownNodes([], waku);
|
await tearDownNodes([], waku);
|
||||||
});
|
});
|
||||||
|
|
||||||
//TODO: add tests for local discovery
|
describe.only("peer:discovery", () => {
|
||||||
describe("peer:discovery", () => {
|
[Tags.BOOTSTRAP].map((discoveryType) => {
|
||||||
it("should emit `peer:discovery:bootstrap` event when a peer is discovered", async function () {
|
it(`should emit 'peer:discovery:${discoveryType}' event when a peer is discovered`, async function () {
|
||||||
const peerIdBootstrap = await createSecp256k1PeerId();
|
const peerId = await createSecp256k1PeerId();
|
||||||
|
|
||||||
await waku.libp2p.peerStore.save(peerIdBootstrap, {
|
await waku.libp2p.peerStore.save(peerId, {
|
||||||
tags: {
|
tags: {
|
||||||
[Tags.BOOTSTRAP]: {
|
[discoveryType]: {
|
||||||
value: 50,
|
value: 50,
|
||||||
ttl: 1200000
|
ttl: 1200000
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const peerDiscoveryBootstrap = new Promise<boolean>((resolve) => {
|
let eventName: EPeersByDiscoveryEvents;
|
||||||
waku.connectionManager.addEventListener(
|
switch (discoveryType) {
|
||||||
EPeersByDiscoveryEvents.PEER_DISCOVERY_BOOTSTRAP,
|
case Tags.BOOTSTRAP:
|
||||||
({ detail: receivedPeerId }) => {
|
eventName = EPeersByDiscoveryEvents.PEER_DISCOVERY_BOOTSTRAP;
|
||||||
resolve(receivedPeerId.toString() === peerIdBootstrap.toString());
|
break;
|
||||||
|
case Tags.PEER_EXCHANGE:
|
||||||
|
eventName = EPeersByDiscoveryEvents.PEER_DISCOVERY_PEER_EXCHANGE;
|
||||||
|
break;
|
||||||
|
case Tags.LOCAL:
|
||||||
|
eventName = EPeersByDiscoveryEvents.PEER_DISCOVERY_LOCAL;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
);
|
|
||||||
|
const peerDiscovery = new Promise<boolean>((resolve) => {
|
||||||
|
waku.connectionManager.addEventListener(eventName, () => {
|
||||||
|
console.log("here");
|
||||||
|
resolve(true);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
waku.libp2p.dispatchEvent(
|
waku.libp2p.dispatchEvent(
|
||||||
new CustomEvent<PeerInfo>("peer:discovery", {
|
new CustomEvent<PeerInfo>("peer:discovery", {
|
||||||
detail: {
|
detail: {
|
||||||
id: peerIdBootstrap,
|
id: peerId,
|
||||||
multiaddrs: []
|
multiaddrs: []
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(await peerDiscoveryBootstrap).to.eq(true);
|
expect(await peerDiscovery).to.eq(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should emit `peer:discovery:peer-exchange` event when a peer is discovered", async function () {
|
|
||||||
const peerIdPx = await createSecp256k1PeerId();
|
|
||||||
|
|
||||||
await waku.libp2p.peerStore.save(peerIdPx, {
|
|
||||||
tags: {
|
|
||||||
[Tags.PEER_EXCHANGE]: {
|
|
||||||
value: 50,
|
|
||||||
ttl: 1200000
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
const peerDiscoveryPeerExchange = new Promise<boolean>((resolve) => {
|
|
||||||
waku.connectionManager.addEventListener(
|
|
||||||
EPeersByDiscoveryEvents.PEER_DISCOVERY_PEER_EXCHANGE,
|
|
||||||
({ detail: receivedPeerId }) => {
|
|
||||||
resolve(receivedPeerId.toString() === peerIdPx.toString());
|
|
||||||
}
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
waku.libp2p.dispatchEvent(
|
|
||||||
new CustomEvent<PeerInfo>("peer:discovery", {
|
|
||||||
detail: {
|
|
||||||
id: peerIdPx,
|
|
||||||
multiaddrs: []
|
|
||||||
}
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(await peerDiscoveryPeerExchange).to.eq(true);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue