Skip to content

Commit

Permalink
chore: restore missing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Oct 2, 2024
1 parent 116a887 commit aacd106
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
56 changes: 56 additions & 0 deletions packages/interface-compliance-tests/test/matchers.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { generateKeyPair } from '@libp2p/crypto/keys'
import { peerIdFromString, peerIdFromPrivateKey } from '@libp2p/peer-id'
import { multiaddr } from '@multiformats/multiaddr'
import { expect } from 'aegir/chai'
import Sinon from 'sinon'
import { matchMultiaddr, matchPeerId } from '../src/matchers.js'

describe('peer id matcher', () => {
it('should match the same object', async () => {
const privateKey = await generateKeyPair('Ed25519')
const peerId = peerIdFromPrivateKey(privateKey)

const stub = Sinon.stub()
stub(peerId)

expect(stub.calledWith(matchPeerId(peerId))).to.be.true()
})

it('should match the same value', async () => {
const privateKey = await generateKeyPair('Ed25519')
const peerId = peerIdFromPrivateKey(privateKey)
const peerId2 = peerIdFromString(peerId.toString())

const stub = Sinon.stub()
stub(peerId)

// this does not match because peerId2 does not contain the private key so
// the values are not deeply equal
expect(stub.calledWith(peerId2)).to.be.false()
expect(stub.calledWith(matchPeerId(peerId2))).to.be.true()
})
})

describe('multiaddr matcher', () => {
it('should match the same object', async () => {
const ma = multiaddr('/ip4/127.0.0.1/tcp/4001')

const stub = Sinon.stub()
stub(ma)

expect(stub.calledWith(matchMultiaddr(ma))).to.be.true()
})

it('should match the same value', async () => {
const ma = multiaddr('/ip4/127.0.0.1/tcp/4001')
const ma2 = multiaddr('/ip4/127.0.0.1/tcp/4001')

const stub = Sinon.stub()
stub(ma)

// this would match because no properties are changed after creation since
// https://github.com/multiformats/js-multiaddr/pull/330
// expect(stub.calledWith(ma2)).to.be.false()
expect(stub.calledWith(matchMultiaddr(ma2))).to.be.true()
})
})
41 changes: 41 additions & 0 deletions packages/interface-compliance-tests/test/mocks/connection.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { generateKeyPair } from '@libp2p/crypto/keys'
import { peerIdFromPrivateKey } from '@libp2p/peer-id'
import { pipe } from 'it-pipe'
import tests from '../../src/connection/index.js'
import { connectionPair } from '../../src/mocks/connection.js'
import { mockRegistrar } from '../../src/mocks/registrar.js'
import type { Connection } from '@libp2p/interface'

describe('mock connection compliance tests', () => {
let connections: Connection[] = []

tests({
async setup () {
const privateKeyA = await generateKeyPair('Ed25519')
const componentsA = {
peerId: peerIdFromPrivateKey(privateKeyA),
registrar: mockRegistrar()
}
const privateKeyB = await generateKeyPair('Ed25519')
const componentsB = {
peerId: peerIdFromPrivateKey(privateKeyB),
registrar: mockRegistrar()
}
connections = connectionPair(componentsA, componentsB)

await componentsB.registrar.handle('/echo/0.0.1', (data) => {
void pipe(
data.stream,
data.stream
)
})

return connections[0]
},
async teardown () {
await Promise.all(connections.map(async conn => {
await conn.close()
}))
}
})
})

0 comments on commit aacd106

Please sign in to comment.