From f4dda4a3cdb46556eb68e49089bf90b029a445e7 Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Wed, 24 Jan 2024 13:59:24 +0100 Subject: [PATCH] chore: use PeerId/Multiaddr matchers from interface tests (#2375) Removes duplicated code. --- packages/integration-tests/package.json | 2 +- packages/kad-dht/test/fixtures/match-peer-id.ts | 6 ------ packages/kad-dht/test/libp2p-routing.spec.ts | 2 +- .../libp2p/test/connection-manager/auto-dial.spec.ts | 2 +- .../libp2p/test/connection-manager/dial-queue.spec.ts | 2 +- packages/libp2p/test/fixtures/matchers.ts | 11 ----------- packages/libp2p/test/registrar/registrar.spec.ts | 2 +- packages/protocol-identify/package.json | 2 +- packages/protocol-identify/test/fixtures/index.ts | 5 ----- packages/protocol-identify/test/push.spec.ts | 3 ++- packages/transport-circuit-relay-v2/test/hop.spec.ts | 5 +---- 11 files changed, 9 insertions(+), 33 deletions(-) delete mode 100644 packages/kad-dht/test/fixtures/match-peer-id.ts delete mode 100644 packages/libp2p/test/fixtures/matchers.ts diff --git a/packages/integration-tests/package.json b/packages/integration-tests/package.json index 4a9cdde1cc..d046ee67bc 100644 --- a/packages/integration-tests/package.json +++ b/packages/integration-tests/package.json @@ -46,7 +46,7 @@ "@libp2p/floodsub": "^9.0.0", "@libp2p/identify": "^1.0.9", "@libp2p/interface": "^1.0.0", - "@libp2p/interface-compliance-tests": "^5.1.1", + "@libp2p/interface-compliance-tests": "^5.1.3", "@libp2p/interface-internal": "^1.0.0", "@libp2p/interop": "^10.0.0", "@libp2p/kad-dht": "^12.0.0", diff --git a/packages/kad-dht/test/fixtures/match-peer-id.ts b/packages/kad-dht/test/fixtures/match-peer-id.ts deleted file mode 100644 index ad9dae226b..0000000000 --- a/packages/kad-dht/test/fixtures/match-peer-id.ts +++ /dev/null @@ -1,6 +0,0 @@ -import Sinon from 'sinon' -import type { PeerId } from '@libp2p/interface' - -export function matchPeerId (peerId: PeerId): Sinon.SinonMatcher { - return Sinon.match(p => p.toString() === peerId.toString()) -} diff --git a/packages/kad-dht/test/libp2p-routing.spec.ts b/packages/kad-dht/test/libp2p-routing.spec.ts index c4ce230e92..848c3a506c 100644 --- a/packages/kad-dht/test/libp2p-routing.spec.ts +++ b/packages/kad-dht/test/libp2p-routing.spec.ts @@ -1,4 +1,5 @@ import { contentRoutingSymbol, TypedEventEmitter, start, stop, peerRoutingSymbol } from '@libp2p/interface' +import { matchPeerId } from '@libp2p/interface-compliance-tests/matchers' import { defaultLogger } from '@libp2p/logger' import { multiaddr } from '@multiformats/multiaddr' import { expect } from 'aegir/chai' @@ -13,7 +14,6 @@ import { stubInterface, type StubbedInstance } from 'sinon-ts' import { kadDHT, passthroughMapper, type KadDHT } from '../src/index.js' import { Message, MessageType } from '../src/message/dht.js' import { convertBuffer } from '../src/utils.js' -import { matchPeerId } from './fixtures/match-peer-id.js' import { createPeerIds } from './utils/create-peer-id.js' import { sortClosestPeers } from './utils/sort-closest-peers.js' import type { ContentRouting, PeerStore, PeerId, TypedEventTarget, ComponentLogger, Connection, Peer, Stream, PeerRouting } from '@libp2p/interface' diff --git a/packages/libp2p/test/connection-manager/auto-dial.spec.ts b/packages/libp2p/test/connection-manager/auto-dial.spec.ts index cc3cd75d5a..2d7bdcec03 100644 --- a/packages/libp2p/test/connection-manager/auto-dial.spec.ts +++ b/packages/libp2p/test/connection-manager/auto-dial.spec.ts @@ -1,6 +1,7 @@ /* eslint-env mocha */ import { TypedEventEmitter, type TypedEventTarget, type Libp2pEvents, type Connection, type PeerId, type PeerStore, type Peer } from '@libp2p/interface' +import { matchPeerId } from '@libp2p/interface-compliance-tests/matchers' import { defaultLogger } from '@libp2p/logger' import { PeerMap } from '@libp2p/peer-collections' import { createEd25519PeerId } from '@libp2p/peer-id-factory' @@ -16,7 +17,6 @@ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' import { defaultComponents } from '../../src/components.js' import { AutoDial } from '../../src/connection-manager/auto-dial.js' import { LAST_DIAL_FAILURE_KEY } from '../../src/connection-manager/constants.js' -import { matchPeerId } from '../fixtures/matchers.js' import type { ConnectionManager } from '@libp2p/interface-internal' describe('auto-dial', () => { diff --git a/packages/libp2p/test/connection-manager/dial-queue.spec.ts b/packages/libp2p/test/connection-manager/dial-queue.spec.ts index 4b986796dc..82aab3b4de 100644 --- a/packages/libp2p/test/connection-manager/dial-queue.spec.ts +++ b/packages/libp2p/test/connection-manager/dial-queue.spec.ts @@ -1,6 +1,7 @@ /* eslint-env mocha */ import { CodeError } from '@libp2p/interface' +import { matchMultiaddr } from '@libp2p/interface-compliance-tests/matchers' import { mockConnection, mockDuplex, mockMultiaddrConnection } from '@libp2p/interface-compliance-tests/mocks' import { peerLogger } from '@libp2p/logger' import { createEd25519PeerId } from '@libp2p/peer-id-factory' @@ -12,7 +13,6 @@ import pDefer from 'p-defer' import sinon from 'sinon' import { type StubbedInstance, stubInterface } from 'sinon-ts' import { DialQueue } from '../../src/connection-manager/dial-queue.js' -import { matchMultiaddr } from '../fixtures/matchers.js' import type { ComponentLogger, Connection, ConnectionGater, PeerId, PeerRouting, PeerStore, Transport } from '@libp2p/interface' import type { TransportManager } from '@libp2p/interface-internal' diff --git a/packages/libp2p/test/fixtures/matchers.ts b/packages/libp2p/test/fixtures/matchers.ts deleted file mode 100644 index d4f56c4a4a..0000000000 --- a/packages/libp2p/test/fixtures/matchers.ts +++ /dev/null @@ -1,11 +0,0 @@ -import Sinon from 'sinon' -import type { PeerId } from '@libp2p/interface' -import type { Multiaddr } from '@multiformats/multiaddr' - -export function matchPeerId (peerId: PeerId): Sinon.SinonMatcher { - return Sinon.match(p => p.toString() === peerId.toString()) -} - -export function matchMultiaddr (ma: Multiaddr): Sinon.SinonMatcher { - return Sinon.match(m => m.toString() === ma.toString()) -} diff --git a/packages/libp2p/test/registrar/registrar.spec.ts b/packages/libp2p/test/registrar/registrar.spec.ts index f74fb8166f..a0ca3995f4 100644 --- a/packages/libp2p/test/registrar/registrar.spec.ts +++ b/packages/libp2p/test/registrar/registrar.spec.ts @@ -2,6 +2,7 @@ import { yamux } from '@chainsafe/libp2p-yamux' import { TypedEventEmitter, type TypedEventTarget, type Libp2pEvents, type ConnectionGater, type PeerId, type PeerStore, type Topology } from '@libp2p/interface' +import { matchPeerId } from '@libp2p/interface-compliance-tests/matchers' import { mockDuplex, mockMultiaddrConnection, mockUpgrader, mockConnection } from '@libp2p/interface-compliance-tests/mocks' import { defaultLogger } from '@libp2p/logger' import { mplex } from '@libp2p/mplex' @@ -17,7 +18,6 @@ import { type Components, defaultComponents } from '../../src/components.js' import { DefaultConnectionManager } from '../../src/connection-manager/index.js' import { createLibp2pNode, type Libp2pNode } from '../../src/libp2p.js' import { DefaultRegistrar } from '../../src/registrar.js' -import { matchPeerId } from '../fixtures/matchers.js' import type { ConnectionManager, Registrar, TransportManager } from '@libp2p/interface-internal' const protocol = '/test/1.0.0' diff --git a/packages/protocol-identify/package.json b/packages/protocol-identify/package.json index 0fe7ffa8fa..753d65e6cb 100644 --- a/packages/protocol-identify/package.json +++ b/packages/protocol-identify/package.json @@ -64,6 +64,7 @@ "wherearewe": "^2.0.1" }, "devDependencies": { + "@libp2p/interface-compliance-tests": "^5.1.3", "@libp2p/logger": "^4.0.5", "@libp2p/peer-id-factory": "^4.0.5", "aegir": "^42.0.0", @@ -73,7 +74,6 @@ "it-pair": "^2.0.6", "it-pushable": "^3.2.1", "protons": "^7.3.0", - "sinon": "^17.0.1", "sinon-ts": "^2.0.0" }, "sideEffects": false diff --git a/packages/protocol-identify/test/fixtures/index.ts b/packages/protocol-identify/test/fixtures/index.ts index 61f45fde25..bb0ba1ec14 100644 --- a/packages/protocol-identify/test/fixtures/index.ts +++ b/packages/protocol-identify/test/fixtures/index.ts @@ -1,17 +1,12 @@ import drain from 'it-drain' import * as lp from 'it-length-prefixed' import { pushable } from 'it-pushable' -import Sinon from 'sinon' import { stubInterface, type StubbedInstance } from 'sinon-ts' import { Uint8ArrayList } from 'uint8arraylist' import { Identify as IdentifyMessage } from '../../src/pb/message.js' import type { ComponentLogger, Libp2pEvents, NodeInfo, TypedEventTarget, PeerId, PeerStore, Connection, Stream } from '@libp2p/interface' import type { AddressManager, ConnectionManager, Registrar } from '@libp2p/interface-internal' -export function matchPeerId (peerId: PeerId): Sinon.SinonMatcher { - return Sinon.match(p => p.toString() === peerId.toString()) -} - export interface StubbedIdentifyComponents { peerId: PeerId peerStore: StubbedInstance diff --git a/packages/protocol-identify/test/push.spec.ts b/packages/protocol-identify/test/push.spec.ts index 7e4ed7c489..94960baafc 100644 --- a/packages/protocol-identify/test/push.spec.ts +++ b/packages/protocol-identify/test/push.spec.ts @@ -1,4 +1,5 @@ import { TypedEventEmitter, start, stop } from '@libp2p/interface' +import { matchPeerId } from '@libp2p/interface-compliance-tests/matchers' import { defaultLogger } from '@libp2p/logger' import { createEd25519PeerId } from '@libp2p/peer-id-factory' import { multiaddr } from '@multiformats/multiaddr' @@ -8,7 +9,7 @@ import { pbStream } from 'it-protobuf-stream' import { stubInterface } from 'sinon-ts' import { Identify } from '../src/identify.js' import { Identify as IdentifyMessage } from '../src/pb/message.js' -import { identifyPushStream, matchPeerId, type StubbedIdentifyComponents } from './fixtures/index.js' +import { identifyPushStream, type StubbedIdentifyComponents } from './fixtures/index.js' import type { Libp2pEvents, PeerStore } from '@libp2p/interface' import type { AddressManager, ConnectionManager, Registrar } from '@libp2p/interface-internal' diff --git a/packages/transport-circuit-relay-v2/test/hop.spec.ts b/packages/transport-circuit-relay-v2/test/hop.spec.ts index dd2aef249d..39a004780c 100644 --- a/packages/transport-circuit-relay-v2/test/hop.spec.ts +++ b/packages/transport-circuit-relay-v2/test/hop.spec.ts @@ -2,6 +2,7 @@ import { TypedEventEmitter, type TypedEventTarget, type ComponentLogger, type Libp2pEvents, type Connection, type Stream, type ConnectionGater, type ContentRouting, type PeerId, type PeerStore, type Transport, type Upgrader } from '@libp2p/interface' import { isStartable } from '@libp2p/interface' +import { matchPeerId } from '@libp2p/interface-compliance-tests/matchers' import { mockRegistrar, mockUpgrader, mockNetwork, mockConnectionManager, mockConnectionGater } from '@libp2p/interface-compliance-tests/mocks' import { defaultLogger } from '@libp2p/logger' import { PeerMap } from '@libp2p/peer-collections' @@ -17,10 +18,6 @@ import { HopMessage, Status } from '../src/pb/index.js' import type { CircuitRelayServerInit } from '../src/server/index.js' import type { AddressManager, ConnectionManager, Registrar, TransportManager } from '@libp2p/interface-internal' -export function matchPeerId (peerId: PeerId): Sinon.SinonMatcher { - return Sinon.match(p => p.toString() === peerId.toString()) -} - interface Node { peerId: PeerId multiaddr: Multiaddr