Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
})
})