Skip to content

Commit

Permalink
chore: remove redundant test code (#2314)
Browse files Browse the repository at this point in the history
Removes trival and unused fixtures
  • Loading branch information
achingbrain authored Dec 15, 2023
1 parent 01e9a5f commit f81be14
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 70 deletions.
1 change: 0 additions & 1 deletion packages/libp2p/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@
"aegir": "^41.0.2",
"it-pushable": "^3.2.1",
"p-event": "^6.0.0",
"p-times": "^4.0.0",
"p-wait-for": "^5.0.2",
"sinon": "^17.0.0",
"sinon-ts": "^2.0.0"
Expand Down
11 changes: 5 additions & 6 deletions packages/libp2p/test/connection-manager/direct.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import { DefaultConnectionManager } from '../../src/connection-manager/index.js'
import { codes as ErrorCodes } from '../../src/errors.js'
import { createLibp2p } from '../../src/index.js'
import { DefaultTransportManager } from '../../src/transport-manager.js'
import { createPeerId } from '../fixtures/creators/peer.js'
import type { Libp2p, Connection, PeerId } from '@libp2p/interface'
import type { TransportManager } from '@libp2p/interface-internal'
import type { Multiaddr } from '@multiformats/multiaddr'
Expand Down Expand Up @@ -301,8 +300,8 @@ describe('dialing (direct, WebSockets)', () => {

// Perform dial
await expect(connectionManager.openConnection([
multiaddr(`/ip4/0.0.0.0/tcp/8000/ws/p2p/${(await createPeerId()).toString()}`),
multiaddr(`/ip4/0.0.0.0/tcp/8001/ws/p2p/${(await createPeerId()).toString()}`)
multiaddr(`/ip4/0.0.0.0/tcp/8000/ws/p2p/${(await createEd25519PeerId()).toString()}`),
multiaddr(`/ip4/0.0.0.0/tcp/8001/ws/p2p/${(await createEd25519PeerId()).toString()}`)
])).to.eventually.rejected
.with.property('code', 'ERR_INVALID_PARAMETERS')
})
Expand All @@ -313,15 +312,15 @@ describe('dialing (direct, WebSockets)', () => {

// Perform dial
await expect(connectionManager.openConnection([
multiaddr(`/ip4/0.0.0.0/tcp/8000/ws/p2p/${(await createPeerId()).toString()}`),
multiaddr(`/ip4/0.0.0.0/tcp/8000/ws/p2p/${(await createEd25519PeerId()).toString()}`),
multiaddr('/ip4/0.0.0.0/tcp/8001/ws')
])).to.eventually.rejected
.with.property('code', 'ERR_INVALID_PARAMETERS')

// Perform dial
await expect(connectionManager.openConnection([
multiaddr('/ip4/0.0.0.0/tcp/8001/ws'),
multiaddr(`/ip4/0.0.0.0/tcp/8000/ws/p2p/${(await createPeerId()).toString()}`)
multiaddr(`/ip4/0.0.0.0/tcp/8000/ws/p2p/${(await createEd25519PeerId()).toString()}`)
])).to.eventually.rejected
.with.property('code', 'ERR_INVALID_PARAMETERS')
})
Expand All @@ -332,7 +331,7 @@ describe('libp2p.dialer (direct, WebSockets)', () => {
let peerId: PeerId

beforeEach(async () => {
peerId = await createPeerId()
peerId = await createEd25519PeerId()
})

afterEach(async () => {
Expand Down
11 changes: 6 additions & 5 deletions packages/libp2p/test/connection-manager/index.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { TypedEventEmitter, start } from '@libp2p/interface'
import { mockConnection, mockDuplex, mockMultiaddrConnection } from '@libp2p/interface-compliance-tests/mocks'
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
import { expect } from 'aegir/chai'
import delay from 'delay'
import all from 'it-all'
Expand All @@ -13,7 +14,7 @@ import { defaultComponents } from '../../src/components.js'
import { DefaultConnectionManager } from '../../src/connection-manager/index.js'
import { codes } from '../../src/errors.js'
import { createBaseOptions } from '../fixtures/base-options.browser.js'
import { createNode, createPeerId } from '../fixtures/creators/peer.js'
import { createNode } from '../fixtures/creators/peer.js'
import { ECHO_PROTOCOL, echo } from '../fixtures/echo-service.js'
import type { Libp2p } from '../../src/index.js'
import type { Libp2pNode } from '../../src/libp2p.js'
Expand All @@ -26,8 +27,8 @@ describe('Connection Manager', () => {

before(async () => {
peerIds = await Promise.all([
createPeerId(),
createPeerId()
createEd25519PeerId(),
createEd25519PeerId()
])
})

Expand Down Expand Up @@ -122,8 +123,8 @@ describe('libp2p.connections', () => {

before(async () => {
peerIds = await Promise.all([
createPeerId(),
createPeerId()
createEd25519PeerId(),
createEd25519PeerId()
])
})

Expand Down
4 changes: 2 additions & 2 deletions packages/libp2p/test/core/consume-peer-record.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/* eslint-env mocha */

import { createEd25519PeerId } from '@libp2p/peer-id-factory'
import { plaintext } from '@libp2p/plaintext'
import { webSockets } from '@libp2p/websockets'
import { multiaddr } from '@multiformats/multiaddr'
import { createLibp2pNode, type Libp2pNode } from '../../src/libp2p.js'
import { createPeerId } from '../fixtures/creators/peer.js'

describe('Consume peer record', () => {
let libp2p: Libp2pNode

beforeEach(async () => {
const peerId = await createPeerId()
const peerId = await createEd25519PeerId()
libp2p = await createLibp2pNode({
peerId,
transports: [
Expand Down
4 changes: 2 additions & 2 deletions packages/libp2p/test/core/encryption.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/* eslint-env mocha */

import { createEd25519PeerId } from '@libp2p/peer-id-factory'
import { plaintext } from '@libp2p/plaintext'
import { webSockets } from '@libp2p/websockets'
import { createLibp2p, type Libp2pOptions } from '../../src/index.js'
import { createPeerId } from '../fixtures/creators/peer.js'
import type { PeerId } from '@libp2p/interface'

describe('Connection encryption configuration', () => {
let peerId: PeerId

before(async () => {
peerId = await createPeerId()
peerId = await createEd25519PeerId()
})

it('can be created', async () => {
Expand Down
8 changes: 4 additions & 4 deletions packages/libp2p/test/core/get-public-key.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* eslint-env mocha */

import { contentRoutingSymbol } from '@libp2p/interface'
import { createEd25519PeerId, createRSAPeerId } from '@libp2p/peer-id-factory'
import { expect } from 'aegir/chai'
import { stubInterface } from 'sinon-ts'
import { createLibp2p } from '../../src/index.js'
import { createPeerId } from '../fixtures/creators/peer.js'
import type { ContentRouting, ContentRoutingProvider, Libp2p } from '@libp2p/interface'
import type { StubbedInstance } from 'sinon-ts'

Expand All @@ -31,15 +31,15 @@ describe('getPublicKey', () => {
})

it('should extract embedded public key', async () => {
const otherPeer = await createPeerId()
const otherPeer = await createEd25519PeerId()

const key = await node.getPublicKey(otherPeer)

expect(otherPeer.publicKey).to.equalBytes(key)
})

it('should get key from the peerstore', async () => {
const otherPeer = await createPeerId({ opts: { type: 'rsa' } })
const otherPeer = await createRSAPeerId()

if (otherPeer.publicKey == null) {
throw new Error('Public key was missing')
Expand All @@ -55,7 +55,7 @@ describe('getPublicKey', () => {
})

it('should query content routing when the key is not in the keystore', async () => {
const otherPeer = await createPeerId({ opts: { type: 'rsa' } })
const otherPeer = await createRSAPeerId()

if (otherPeer.publicKey == null) {
throw new Error('Public key was missing')
Expand Down
4 changes: 2 additions & 2 deletions packages/libp2p/test/core/listening.node.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* eslint-env mocha */

import { createEd25519PeerId } from '@libp2p/peer-id-factory'
import { plaintext } from '@libp2p/plaintext'
import { tcp } from '@libp2p/tcp'
import { expect } from 'aegir/chai'
import { createLibp2pNode, type Libp2pNode } from '../../src/libp2p.js'
import { createPeerId } from '../fixtures/creators/peer.js'
import type { PeerId } from '@libp2p/interface'

const listenAddr = '/ip4/0.0.0.0/tcp/0'
Expand All @@ -14,7 +14,7 @@ describe('Listening', () => {
let libp2p: Libp2pNode

before(async () => {
peerId = await createPeerId()
peerId = await createEd25519PeerId()
})

after(async () => {
Expand Down
47 changes: 3 additions & 44 deletions packages/libp2p/test/fixtures/creators/peer.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { createEd25519PeerId, createRSAPeerId } from '@libp2p/peer-id-factory'
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
import { multiaddr } from '@multiformats/multiaddr'
import pTimes from 'p-times'
import { createLibp2pNode, type Libp2pNode } from '../../../src/libp2p.js'
import { createBaseOptions } from '../base-options.browser.js'
import type { AddressManagerInit } from '../../../src/address-manager/index.js'
import type { Libp2pOptions } from '../../../src/index.js'
import type { Libp2p, ServiceMap, PeerId } from '@libp2p/interface'
import type { ServiceMap } from '@libp2p/interface'

const listenAddr = multiaddr('/ip4/127.0.0.1/tcp/0')

Expand All @@ -29,7 +28,7 @@ export interface CreatePeerOptions <T extends ServiceMap> {
export async function createNode <T extends ServiceMap> (options: CreatePeerOptions<T> = {}): Promise<Libp2pNode<T>> {
const started = options.started ?? true
const config = options.config ?? {}
const peerId = await createPeerId()
const peerId = await createEd25519PeerId()
const addresses: AddressManagerInit = started
? {
listen: [listenAddr.toString()],
Expand All @@ -56,43 +55,3 @@ export async function createNode <T extends ServiceMap> (options: CreatePeerOpti

return peer
}

export async function populateAddressBooks (peers: Libp2p[]): Promise<void> {
for (let i = 0; i < peers.length; i++) {
for (let j = 0; j < peers.length; j++) {
if (i !== j) {
await peers[i].peerStore.patch(peers[j].peerId, {
multiaddrs: peers[j].getMultiaddrs()
})
}
}
}
}

export interface CreatePeerIdOptions {
/**
* Options to pass to the PeerId constructor
*/
opts?: {
type?: 'rsa' | 'ed25519'
bits?: number
}
}

/**
* Create Peer-id
*/
export async function createPeerId (options: CreatePeerIdOptions = {}): Promise<PeerId> {
const opts = options.opts ?? {}

return opts.type === 'rsa' ? createRSAPeerId({ bits: opts.bits ?? 512 }) : createEd25519PeerId()
}

/**
* Create Peer-ids
*/
export async function createPeerIds (count: number, options: Omit<CreatePeerIdOptions, 'fixture'> = {}): Promise<PeerId[]> {
const opts = options.opts ?? {}

return pTimes(count, async (i) => createPeerId({ opts }))
}
4 changes: 2 additions & 2 deletions packages/libp2p/test/peer-discovery/peer-discovery.spec.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
/* eslint-env mocha */

import { TypedEventEmitter } from '@libp2p/interface'
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
import { multiaddr } from '@multiformats/multiaddr'
import { expect } from 'aegir/chai'
import sinon from 'sinon'
import { stubInterface } from 'sinon-ts'
import { createLibp2p } from '../../src/index.js'
import { createPeerId } from '../fixtures/creators/peer.js'
import type { PeerDiscovery, PeerDiscoveryEvents, PeerId, Startable, Libp2p } from '@libp2p/interface'

describe('peer discovery', () => {
let peerId: PeerId
let libp2p: Libp2p

before(async () => {
peerId = await createPeerId()
peerId = await createEd25519PeerId()
})

afterEach(async () => {
Expand Down
3 changes: 1 addition & 2 deletions packages/libp2p/test/registrar/registrar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,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 { createPeerId } from '../fixtures/creators/peer.js'
import { matchPeerId } from '../fixtures/match-peer-id.js'
import type { ConnectionManager, Registrar, TransportManager } from '@libp2p/interface-internal'

Expand All @@ -30,7 +29,7 @@ describe('registrar', () => {
let libp2p: Libp2pNode

before(async () => {
peerId = await createPeerId()
peerId = await createEd25519PeerId()
})

describe('errors', () => {
Expand Down

0 comments on commit f81be14

Please sign in to comment.