js-waku/packages/tests/tests/multiaddr.node.spec.ts
Danish Arora 1892f5093d
fix: don't dial discovered peers if have already been attempted dial (#1657)
* don't dial peers if they exist in PeerStore already

* fix(tests): bugs & add types to dispatch event

* fix: more tests

* fix: dial validation

* update doc & reduce delay

* refactor test

* use -1 instead of Infinity

* fix comment

* fix rebase
2023-10-20 18:16:48 +05:30

103 lines
2.7 KiB
TypeScript

import { CustomEvent } from "@libp2p/interface/events";
import type { PeerId } from "@libp2p/interface/peer-id";
import type { PeerInfo } from "@libp2p/interface/peer-info";
import { multiaddr } from "@multiformats/multiaddr";
import type { Multiaddr } from "@multiformats/multiaddr";
import type { Waku } from "@waku/interfaces";
import { createLightNode } from "@waku/sdk";
import { expect } from "chai";
import Sinon, { SinonSpy, SinonStub } from "sinon";
import {
delay,
makeLogFileName,
NimGoNode,
tearDownNodes
} from "../src/index.js";
describe("multiaddr: dialing", function () {
let waku: Waku;
let nwaku: NimGoNode;
let dialPeerSpy: SinonSpy;
let isPeerTopicConfigured: SinonStub;
afterEach(async function () {
this.timeout(15000);
await tearDownNodes(nwaku, waku);
});
it("can dial TLS multiaddrs", async function () {
this.timeout(20_000);
let tlsWorks = true;
waku = await createLightNode();
await waku.start();
try {
// dummy multiaddr, doesn't have to be valid
await waku.dial(multiaddr(`/ip4/127.0.0.1/tcp/30303/tls/ws`));
} catch (error) {
if (error instanceof Error) {
// if the error is of tls unsupported, the test should fail
// for any other dial errors, the test should pass
if (error.message === "Unsupported protocol tls") {
tlsWorks = false;
}
}
}
expect(tlsWorks).to.eq(true);
});
describe("does not attempt the same peer discovered multiple times more than once", function () {
const PEER_DISCOVERY_COUNT = 3;
let peerId: PeerId;
let multiaddr: Multiaddr;
beforeEach(async function () {
this.timeout(10_000);
nwaku = new NimGoNode(makeLogFileName(this));
await nwaku.start();
waku = await createLightNode();
peerId = await nwaku.getPeerId();
multiaddr = await nwaku.getMultiaddrWithId();
isPeerTopicConfigured = Sinon.stub(
waku.connectionManager as any,
"isPeerTopicConfigured"
);
isPeerTopicConfigured.resolves(true);
dialPeerSpy = Sinon.spy(waku.connectionManager as any, "dialPeer");
});
afterEach(function () {
dialPeerSpy.restore();
});
it("through manual discovery", async function () {
this.timeout(20_000);
const discoverPeer = (): void => {
waku.libp2p.dispatchEvent(
new CustomEvent<PeerInfo>("peer:discovery", {
detail: {
id: peerId,
protocols: [],
multiaddrs: [multiaddr]
}
})
);
};
for (let i = 0; i < PEER_DISCOVERY_COUNT; i++) {
discoverPeer();
await delay(100);
}
expect(dialPeerSpy.callCount).to.eq(1);
});
});
});