|
| 1 | +import { generateKeyPair } from '@libp2p/crypto/keys' |
| 2 | +import { peerIdFromString, peerIdFromPrivateKey } from '@libp2p/peer-id' |
| 3 | +import { multiaddr } from '@multiformats/multiaddr' |
| 4 | +import { expect } from 'aegir/chai' |
| 5 | +import Sinon from 'sinon' |
| 6 | +import { matchMultiaddr, matchPeerId } from '../src/matchers.js' |
| 7 | + |
| 8 | +describe('peer id matcher', () => { |
| 9 | + it('should match the same object', async () => { |
| 10 | + const privateKey = await generateKeyPair('Ed25519') |
| 11 | + const peerId = peerIdFromPrivateKey(privateKey) |
| 12 | + |
| 13 | + const stub = Sinon.stub() |
| 14 | + stub(peerId) |
| 15 | + |
| 16 | + expect(stub.calledWith(matchPeerId(peerId))).to.be.true() |
| 17 | + }) |
| 18 | + |
| 19 | + it('should match the same value', async () => { |
| 20 | + const privateKey = await generateKeyPair('Ed25519') |
| 21 | + const peerId = peerIdFromPrivateKey(privateKey) |
| 22 | + const peerId2 = peerIdFromString(peerId.toString()) |
| 23 | + |
| 24 | + const stub = Sinon.stub() |
| 25 | + stub(peerId) |
| 26 | + |
| 27 | + // this does not match because peerId2 does not contain the private key so |
| 28 | + // the values are not deeply equal |
| 29 | + expect(stub.calledWith(peerId2)).to.be.false() |
| 30 | + expect(stub.calledWith(matchPeerId(peerId2))).to.be.true() |
| 31 | + }) |
| 32 | +}) |
| 33 | + |
| 34 | +describe('multiaddr matcher', () => { |
| 35 | + it('should match the same object', async () => { |
| 36 | + const ma = multiaddr('/ip4/127.0.0.1/tcp/4001') |
| 37 | + |
| 38 | + const stub = Sinon.stub() |
| 39 | + stub(ma) |
| 40 | + |
| 41 | + expect(stub.calledWith(matchMultiaddr(ma))).to.be.true() |
| 42 | + }) |
| 43 | + |
| 44 | + it('should match the same value', async () => { |
| 45 | + const ma = multiaddr('/ip4/127.0.0.1/tcp/4001') |
| 46 | + const ma2 = multiaddr('/ip4/127.0.0.1/tcp/4001') |
| 47 | + |
| 48 | + const stub = Sinon.stub() |
| 49 | + stub(ma) |
| 50 | + |
| 51 | + // this would match because no properties are changed after creation since |
| 52 | + // https://github.com/multiformats/js-multiaddr/pull/330 |
| 53 | + // expect(stub.calledWith(ma2)).to.be.false() |
| 54 | + expect(stub.calledWith(matchMultiaddr(ma2))).to.be.true() |
| 55 | + }) |
| 56 | +}) |
0 commit comments