Skip to content

Commit aacd106

Browse files
committed
chore: restore missing tests
1 parent 116a887 commit aacd106

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { generateKeyPair } from '@libp2p/crypto/keys'
2+
import { peerIdFromPrivateKey } from '@libp2p/peer-id'
3+
import { pipe } from 'it-pipe'
4+
import tests from '../../src/connection/index.js'
5+
import { connectionPair } from '../../src/mocks/connection.js'
6+
import { mockRegistrar } from '../../src/mocks/registrar.js'
7+
import type { Connection } from '@libp2p/interface'
8+
9+
describe('mock connection compliance tests', () => {
10+
let connections: Connection[] = []
11+
12+
tests({
13+
async setup () {
14+
const privateKeyA = await generateKeyPair('Ed25519')
15+
const componentsA = {
16+
peerId: peerIdFromPrivateKey(privateKeyA),
17+
registrar: mockRegistrar()
18+
}
19+
const privateKeyB = await generateKeyPair('Ed25519')
20+
const componentsB = {
21+
peerId: peerIdFromPrivateKey(privateKeyB),
22+
registrar: mockRegistrar()
23+
}
24+
connections = connectionPair(componentsA, componentsB)
25+
26+
await componentsB.registrar.handle('/echo/0.0.1', (data) => {
27+
void pipe(
28+
data.stream,
29+
data.stream
30+
)
31+
})
32+
33+
return connections[0]
34+
},
35+
async teardown () {
36+
await Promise.all(connections.map(async conn => {
37+
await conn.close()
38+
}))
39+
}
40+
})
41+
})

0 commit comments

Comments
 (0)