Skip to content

Commit

Permalink
feat: add sinon matchers for PeerIds and Multiaddrs to test module (#…
Browse files Browse the repository at this point in the history
…2373)

* feat: add sinon matchers for PeerIds and Multiaddrs to test module

To allow matching PeerId/Multiaddrs by value (not just by reference)
add reusible matchers to the compliance test module.

* chore: add test
  • Loading branch information
achingbrain authored Jan 24, 2024
1 parent 74fb567 commit 2e464c0
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/interface-compliance-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@
"types": "./dist/src/is-valid-tick.d.ts",
"import": "./dist/src/is-valid-tick.js"
},
"./matchers": {
"types": "./dist/src/matchers.d.ts",
"import": "./dist/src/matchers.js"
},
"./mocks": {
"types": "./dist/src/mocks/index.d.ts",
"import": "./dist/src/mocks/index.js"
Expand Down
11 changes: 11 additions & 0 deletions packages/interface-compliance-tests/src/matchers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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())
}
54 changes: 54 additions & 0 deletions packages/interface-compliance-tests/test/matchers.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { peerIdFromString } from '@libp2p/peer-id'
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
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 peerId = await createEd25519PeerId()

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

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

it('should match the same value', async () => {
const peerId = await createEd25519PeerId()
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()
})
})

0 comments on commit 2e464c0

Please sign in to comment.