main
ts 100 lines 3.18 KB
Raw
1 import { generateKeyPair } from '@libp2p/crypto/keys'
2 import { TypedEventEmitter, start, stop } from '@libp2p/interface'
3 import { defaultLogger } from '@libp2p/logger'
4 import { peerIdFromPrivateKey } from '@libp2p/peer-id'
5 import { expect } from 'aegir/chai'
6 import { stubInterface } from 'sinon-ts'
7 import { LIBP2P_INSPECTOR_METRICS_KEY, inspectorMetrics } from '../src/index.js'
8 import type { ComponentLogger, ContentRouting, Libp2pEvents, Metrics, NodeInfo, PeerId, PeerRouting, PeerStore } from '@libp2p/interface'
9 import type { AddressManager, ConnectionManager, Registrar, TransportManager } from '@libp2p/interface-internal'
10 import type { StubbedInstance } from 'sinon-ts'
11
12 interface StubbedComponents {
13 logger: ComponentLogger
14 events: TypedEventEmitter<Libp2pEvents>
15 peerId: PeerId
16 transportManager: StubbedInstance<TransportManager>
17 registrar: StubbedInstance<Registrar>
18 connectionManager: StubbedInstance<ConnectionManager>
19 peerStore: StubbedInstance<PeerStore>
20 contentRouting: StubbedInstance<ContentRouting>
21 peerRouting: StubbedInstance<PeerRouting>
22 addressManager: StubbedInstance<AddressManager>
23 nodeInfo: NodeInfo
24 }
25
26 describe('inspector-metrics', () => {
27 let components: StubbedComponents
28 let metrics: Metrics
29
30 beforeEach(async () => {
31 components = {
32 logger: defaultLogger(),
33 events: new TypedEventEmitter(),
34 peerId: peerIdFromPrivateKey(await generateKeyPair('Ed25519')),
35 transportManager: stubInterface(),
36 registrar: stubInterface(),
37 connectionManager: stubInterface(),
38 peerStore: stubInterface(),
39 contentRouting: stubInterface(),
40 peerRouting: stubInterface(),
41 addressManager: stubInterface(),
42 nodeInfo: {
43 name: 'test',
44 userAgent: 'test',
45 version: 'test'
46 }
47 }
48
49 metrics = inspectorMetrics({
50 intervalMs: 10
51 })(components)
52
53 await start(metrics)
54 })
55
56 afterEach(async () => {
57 await stop(metrics)
58 })
59 /*
60 it('should broadcast metrics', async () => {
61 const event = await raceEvent<MessageEvent<ApplicationMessage>>(window, 'message', AbortSignal.timeout(1000), {
62 filter: (evt) => {
63 return evt.data.source === SOURCE_METRICS && evt.data.type === 'metrics'
64 }
65 })
66
67 expect(event).to.have.nested.property('data.metrics')
68 })
69
70 it('should identify node', async () => {
71 components.transportManager.getListeners.returns([])
72 components.registrar.getProtocols.returns([])
73 components.peerStore.get.withArgs(components.peerId).resolves({
74 id: components.peerId,
75 addresses: [],
76 metadata: new Map(),
77 protocols: [],
78 tags: new Map()
79 })
80
81 // devtools asks the node to reveal itself
82 window.postMessage({
83 source: SOURCE_DEVTOOLS,
84 type: 'identify'
85 }, '*')
86
87 const event = await raceEvent<MessageEvent<ApplicationMessage>>(window, 'message', AbortSignal.timeout(1000), {
88 filter: (evt) => {
89 return evt.data.source === SOURCE_METRICS && evt.data.type === 'self'
90 }
91 })
92
93 expect(event).to.have.nested.property('data.peer.id')
94 })
95 */
96
97 it('should signal presence of metrics', () => {
98 expect(globalThis).to.have.property(LIBP2P_INSPECTOR_METRICS_KEY).that.is.true()
99 })
100 })