|
1 | 1 | import { expect } from 'aegir/chai'
|
2 | 2 | import { trackedMap } from '../src/index.js'
|
3 |
| -import sinon from 'sinon' |
4 |
| -import type { ComponentMetricsTracker, ComponentMetricsUpdate } from '@libp2p/interface-metrics' |
5 |
| -import type { SinonStub } from 'sinon' |
| 3 | +import type { SinonStubbedInstance } from 'sinon' |
| 4 | +import type { Metric, Metrics } from '@libp2p/interface-metrics' |
| 5 | +import { stubInterface } from 'sinon-ts' |
6 | 6 |
|
7 | 7 | describe('tracked-map', () => {
|
8 |
| - let metrics: ComponentMetricsTracker |
9 |
| - let updateComponentMetricStub: SinonStub<[ComponentMetricsUpdate], void> |
| 8 | + let metrics: SinonStubbedInstance<Metrics> |
10 | 9 |
|
11 | 10 | beforeEach(() => {
|
12 |
| - updateComponentMetricStub = sinon.stub() |
13 |
| - |
14 |
| - metrics = { |
15 |
| - updateComponentMetric: updateComponentMetricStub, |
16 |
| - getComponentMetrics: sinon.stub() |
17 |
| - } |
| 11 | + metrics = stubInterface<Metrics>() |
18 | 12 | })
|
19 | 13 |
|
20 | 14 | it('should return a map with metrics', () => {
|
21 |
| - const system = 'system' |
22 |
| - const component = 'component' |
23 |
| - const metric = 'metric' |
| 15 | + const name = 'system_component_metric' |
| 16 | + const metric = stubInterface<Metric>() |
| 17 | + // @ts-expect-error the wrong overload is selected |
| 18 | + metrics.registerMetric.withArgs(name).returns(metric) |
24 | 19 |
|
25 | 20 | const map = trackedMap({
|
26 |
| - metrics, |
27 |
| - system, |
28 |
| - component, |
29 |
| - metric |
| 21 | + name, |
| 22 | + metrics |
30 | 23 | })
|
31 | 24 |
|
32 | 25 | expect(map).to.be.an.instanceOf(Map)
|
33 |
| - expect(updateComponentMetricStub.calledWith({ |
34 |
| - system, |
35 |
| - component, |
36 |
| - metric, |
37 |
| - value: 0 |
38 |
| - })).to.be.true() |
| 26 | + expect(metrics.registerMetric.calledWith(name)).to.be.true() |
39 | 27 | })
|
40 | 28 |
|
41 | 29 | it('should return a map without metrics', () => {
|
42 |
| - const system = 'system' |
43 |
| - const component = 'component' |
44 |
| - const metric = 'metric' |
| 30 | + const name = 'system_component_metric' |
| 31 | + const metric = stubInterface<Metric>() |
| 32 | + // @ts-expect-error the wrong overload is selected |
| 33 | + metrics.registerMetric.withArgs(name).returns(metric) |
45 | 34 |
|
46 | 35 | const map = trackedMap({
|
47 |
| - system, |
48 |
| - component, |
49 |
| - metric |
| 36 | + name |
50 | 37 | })
|
51 | 38 |
|
52 | 39 | expect(map).to.be.an.instanceOf(Map)
|
53 |
| - expect(updateComponentMetricStub.called).to.be.false() |
54 |
| - }) |
55 |
| - |
56 |
| - it('should default system to libp2p', () => { |
57 |
| - const component = 'component' |
58 |
| - const metric = 'metric' |
59 |
| - |
60 |
| - const map = trackedMap({ |
61 |
| - metrics, |
62 |
| - component, |
63 |
| - metric |
64 |
| - }) |
65 |
| - |
66 |
| - expect(map).to.be.an.instanceOf(Map) |
67 |
| - expect(updateComponentMetricStub.calledWith({ |
68 |
| - system: 'libp2p', |
69 |
| - component, |
70 |
| - metric, |
71 |
| - value: 0 |
72 |
| - })).to.be.true() |
| 40 | + expect(metrics.registerMetric.called).to.be.false() |
73 | 41 | })
|
74 | 42 |
|
75 | 43 | it('should track metrics', () => {
|
76 |
| - const system = 'system' |
77 |
| - const component = 'component' |
78 |
| - const metric = 'metric' |
| 44 | + const name = 'system_component_metric' |
79 | 45 | let value = 0
|
80 | 46 | let callCount = 0
|
81 | 47 |
|
82 |
| - metrics.updateComponentMetric = (data) => { |
83 |
| - expect(data.system).to.equal(system) |
84 |
| - expect(data.component).to.equal(component) |
85 |
| - expect(data.metric).to.equal(metric) |
| 48 | + const metric = stubInterface<Metric>() |
| 49 | + // @ts-expect-error the wrong overload is selected |
| 50 | + metrics.registerMetric.withArgs(name).returns(metric) |
86 | 51 |
|
87 |
| - if (typeof data.value === 'number') { |
88 |
| - value = data.value |
| 52 | + metric.update.callsFake((v) => { |
| 53 | + if (typeof v === 'number') { |
| 54 | + value = v |
89 | 55 | }
|
90 | 56 |
|
91 | 57 | callCount++
|
92 |
| - } |
| 58 | + }) |
93 | 59 |
|
94 | 60 | const map = trackedMap({
|
95 |
| - metrics, |
96 |
| - system, |
97 |
| - component, |
98 |
| - metric |
| 61 | + name, |
| 62 | + metrics |
99 | 63 | })
|
100 | 64 |
|
101 | 65 | expect(map).to.be.an.instanceOf(Map)
|
|
0 commit comments