From 2e464c09993c919f66a465d8c0ed511f6a9e9c90 Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Wed, 24 Jan 2024 13:05:00 +0100 Subject: [PATCH] feat: add sinon matchers for PeerIds and Multiaddrs to test module (#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 --- .../interface-compliance-tests/package.json | 4 ++ .../src/matchers.ts | 11 ++++ .../test/matchers.spec.ts | 54 +++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 packages/interface-compliance-tests/src/matchers.ts create mode 100644 packages/interface-compliance-tests/test/matchers.spec.ts diff --git a/packages/interface-compliance-tests/package.json b/packages/interface-compliance-tests/package.json index 75b3cdfb08..8ca8129daf 100644 --- a/packages/interface-compliance-tests/package.json +++ b/packages/interface-compliance-tests/package.json @@ -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" diff --git a/packages/interface-compliance-tests/src/matchers.ts b/packages/interface-compliance-tests/src/matchers.ts new file mode 100644 index 0000000000..d4f56c4a4a --- /dev/null +++ b/packages/interface-compliance-tests/src/matchers.ts @@ -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()) +} diff --git a/packages/interface-compliance-tests/test/matchers.spec.ts b/packages/interface-compliance-tests/test/matchers.spec.ts new file mode 100644 index 0000000000..568aca0f1e --- /dev/null +++ b/packages/interface-compliance-tests/test/matchers.spec.ts @@ -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() + }) +})