-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcompliance.spec.ts
68 lines (59 loc) · 2.04 KB
/
compliance.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/* eslint-env mocha */
import { generateKeyPair, publicKeyToProtobuf } from '@libp2p/crypto/keys'
import tests from '@libp2p/interface-compliance-tests/peer-discovery'
import { defaultLogger } from '@libp2p/logger'
import { peerIdFromPrivateKey } from '@libp2p/peer-id'
import { multiaddr } from '@multiformats/multiaddr'
import { stubInterface } from 'sinon-ts'
import { pubsubPeerDiscovery, TOPIC } from '../src/index.js'
import { Peer as PBPeer } from '../src/peer.js'
import type { PubSub } from '@libp2p/interface'
import type { AddressManager } from '@libp2p/interface-internal'
describe('compliance tests', () => {
let intervalId: ReturnType<typeof setInterval>
tests({
async setup () {
const privateKey = await generateKeyPair('Ed25519')
const peerId = peerIdFromPrivateKey(privateKey)
const subscriberPrivateKey = await generateKeyPair('Ed25519')
const subscriber = peerIdFromPrivateKey(subscriberPrivateKey)
const addressManager = stubInterface<AddressManager>()
addressManager.getAddresses.returns([
multiaddr(`/ip4/43.10.1.2/tcp/39832/p2p/${peerId.toString()}`)
])
const pubsubDiscovery = pubsubPeerDiscovery()({
pubsub: stubInterface<PubSub>({
getSubscribers: () => {
return [
subscriber
]
}
}),
peerId,
addressManager,
logger: defaultLogger()
})
intervalId = setInterval(() => {
const peer = PBPeer.encode({
publicKey: publicKeyToProtobuf(subscriber.publicKey),
addrs: [
multiaddr('/ip4/166.10.1.2/tcp/80').bytes
]
}).subarray()
// @ts-expect-error private field
pubsubDiscovery._onMessage(new CustomEvent('message', {
detail: {
type: 'unsigned',
topic: TOPIC,
data: peer
}
}))
}, 1000)
return pubsubDiscovery
},
async teardown () {
await new Promise(resolve => setTimeout(resolve, 10))
clearInterval(intervalId)
}
})
})