From d4210c7d178e73840e8bf1bbd4efcb70265c350e Mon Sep 17 00:00:00 2001 From: Sasha Date: Thu, 24 Oct 2024 17:53:08 +0200 Subject: [PATCH] remove old test for peerManager, fix check and spell --- .cspell.json | 1 + .../protocols/light_push/light_push.spec.ts | 5 +- .../sdk/src/protocols/peer_manager.spec.ts | 148 ------------------ 3 files changed, 5 insertions(+), 149 deletions(-) delete mode 100644 packages/sdk/src/protocols/peer_manager.spec.ts diff --git a/.cspell.json b/.cspell.json index 99da5df60e..1b262f3a0d 100644 --- a/.cspell.json +++ b/.cspell.json @@ -116,6 +116,7 @@ "upgrader", "vacp", "varint", + "weboko", "waku", "wakuconnect", "wakunode", diff --git a/packages/sdk/src/protocols/light_push/light_push.spec.ts b/packages/sdk/src/protocols/light_push/light_push.spec.ts index 1e0a24293c..7c695212b5 100644 --- a/packages/sdk/src/protocols/light_push/light_push.spec.ts +++ b/packages/sdk/src/protocols/light_push/light_push.spec.ts @@ -10,6 +10,8 @@ import { utf8ToBytes } from "@waku/utils/bytes"; import { expect } from "chai"; import sinon from "sinon"; +import { PeerManager } from "../peer_manager.js"; + import { LightPush } from "./light_push.js"; const PUBSUB_TOPIC = "/waku/2/rs/1/4"; @@ -156,7 +158,8 @@ function mockLightPush(options: MockLightPushOptions): LightPush { return new LightPush( { configuredPubsubTopics: options.pubsubTopics || [PUBSUB_TOPIC] - } as ConnectionManager, + } as unknown as ConnectionManager, + {} as unknown as PeerManager, options.libp2p ); } diff --git a/packages/sdk/src/protocols/peer_manager.spec.ts b/packages/sdk/src/protocols/peer_manager.spec.ts deleted file mode 100644 index bea3bb0c6d..0000000000 --- a/packages/sdk/src/protocols/peer_manager.spec.ts +++ /dev/null @@ -1,148 +0,0 @@ -import { Peer, PeerId } from "@libp2p/interface"; -import { ConnectionManager, LightPushCodec } from "@waku/core"; -import { BaseProtocol } from "@waku/core/lib/base_protocol"; -import { Logger } from "@waku/utils"; -import { expect } from "chai"; -import sinon from "sinon"; - -import { PeerManager } from "./peer_manager.js"; - -describe("PeerManager", () => { - let peerManager: PeerManager; - let mockConnectionManager: sinon.SinonStubbedInstance; - let mockCore: sinon.SinonStubbedInstance; - let mockLogger: any; - - beforeEach(() => { - mockConnectionManager = sinon.createStubInstance(ConnectionManager); - mockCore = sinon.createStubInstance(BaseProtocol); - mockLogger = { - info: sinon.stub(), - warn: sinon.stub(), - error: sinon.stub(), - debug: sinon.stub(), - extend: sinon.stub().returns({ - info: sinon.stub(), - warn: sinon.stub(), - error: sinon.stub(), - debug: sinon.stub() - }) - }; - - mockCore.multicodec = LightPushCodec; - - peerManager = new PeerManager( - mockConnectionManager as any, - mockCore as any, - mockLogger as Logger - ); - }); - - afterEach(() => { - sinon.restore(); - }); - - const createMockPeer = (id: string): Peer => - ({ - id: { - toString: () => id - } as PeerId - }) as Peer; - - describe("addPeer", () => { - it("should add a peer", async () => { - const peer = createMockPeer("peer1"); - await peerManager.addPeer(peer); - - expect(mockConnectionManager.attemptDial.calledWith(peer.id)).to.be.true; - expect( - mockLogger.info.calledWith(sinon.match(/Added and dialed peer: peer1/)) - ).to.be.true; - expect(await peerManager.getPeerCount()).to.equal(1); - }); - }); - - describe("removePeer", () => { - it("should remove a peer", async () => { - const peer = createMockPeer("peer1"); - await peerManager.addPeer(peer); - await peerManager.removePeer(peer.id); - - expect(mockLogger.info.calledWith(sinon.match(/Removed peer: peer1/))).to - .be.true; - expect(await peerManager.getPeerCount()).to.equal(0); - }); - }); - - describe("getPeerCount", () => { - it("should return the correct number of peers", async () => { - await peerManager.addPeer(createMockPeer("peer1")); - await peerManager.addPeer(createMockPeer("peer2")); - - const count = await peerManager.getPeerCount(); - expect(count).to.equal(2); - }); - }); - - describe("hasPeers", () => { - it("should return true when peers exist", async () => { - await peerManager.addPeer(createMockPeer("peer1")); - const result = await peerManager.hasPeers(); - expect(result).to.be.true; - }); - - it("should return false when no peers exist", async () => { - const result = await peerManager.hasPeers(); - expect(result).to.be.false; - }); - }); - - describe("removeExcessPeers", () => { - it("should remove the specified number of excess peers", async () => { - await peerManager.addPeer(createMockPeer("peer1")); - await peerManager.addPeer(createMockPeer("peer2")); - await peerManager.addPeer(createMockPeer("peer3")); - - await peerManager.removeExcessPeers(2); - - const count = await peerManager.getPeerCount(); - expect(count).to.equal(1); - expect(mockLogger.info.calledWith(`Removing 2 excess peer(s)`)).to.be - .true; - }); - }); - - describe("findAndAddPeers", () => { - it("should find and add new peers", async () => { - const newPeers = [createMockPeer("peer1"), createMockPeer("peer2")]; - mockCore.getPeers.resolves(newPeers); - - const addedPeers = await peerManager.findAndAddPeers(2); - - expect(addedPeers).to.have.lengthOf(2); - expect(mockConnectionManager.attemptDial.callCount).to.equal(2); - }); - - it("should not add existing peers", async () => { - const existingPeer = createMockPeer("existing"); - await peerManager.addPeer(existingPeer); - - const newPeers = [existingPeer, createMockPeer("new")]; - mockCore.getPeers.resolves(newPeers); - - const addedPeers = await peerManager.findAndAddPeers(2); - - expect(addedPeers).to.have.lengthOf(1); - expect(mockConnectionManager.attemptDial.callCount).to.equal(2); // Once for existing, once for new - }); - - it("should log when no additional peers are found", async () => { - mockCore.getPeers.resolves([]); - - await peerManager.findAndAddPeers(2); - - expect(mockLogger.warn.calledWith("No additional peers found")).to.be - .true; - }); - }); -});