Skip to content

Commit 0929feb

Browse files
committed
add unit tests
1 parent cb2102b commit 0929feb

File tree

3 files changed

+1593
-0
lines changed

3 files changed

+1593
-0
lines changed
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
2+
import { createMetricContainerEnvelopeItem, createMetricEnvelope } from '../../../src/metrics/envelope';
3+
import type { DsnComponents } from '../../../src/types-hoist/dsn';
4+
import type { SerializedMetric } from '../../../src/types-hoist/metric';
5+
import type { SdkMetadata } from '../../../src/types-hoist/sdkmetadata';
6+
import * as utilsDsn from '../../../src/utils/dsn';
7+
import * as utilsEnvelope from '../../../src/utils/envelope';
8+
9+
vi.mock('../../../src/utils/dsn', () => ({
10+
dsnToString: vi.fn(dsn => `https://${dsn.publicKey}@${dsn.host}/`),
11+
}));
12+
vi.mock('../../../src/utils/envelope', () => ({
13+
createEnvelope: vi.fn((_headers, items) => [_headers, items]),
14+
}));
15+
16+
describe('createMetricContainerEnvelopeItem', () => {
17+
it('creates an envelope item with correct structure', () => {
18+
const mockMetric: SerializedMetric = {
19+
timestamp: 1713859200,
20+
trace_id: '3d9355f71e9c444b81161599adac6e29',
21+
span_id: '8b5f5e5e5e5e5e5e',
22+
name: 'test.metric',
23+
type: 'counter',
24+
value: 1,
25+
unit: 'count',
26+
attributes: {},
27+
};
28+
29+
const result = createMetricContainerEnvelopeItem([mockMetric, mockMetric]);
30+
31+
expect(result).toHaveLength(2);
32+
expect(result[0]).toEqual({
33+
type: 'metric',
34+
item_count: 2,
35+
content_type: 'application/vnd.sentry.items.trace-metric+json',
36+
});
37+
expect(result[1]).toEqual({ items: [mockMetric, mockMetric] });
38+
});
39+
});
40+
41+
describe('createMetricEnvelope', () => {
42+
beforeEach(() => {
43+
vi.useFakeTimers();
44+
vi.setSystemTime(new Date('2023-01-01T12:00:00Z'));
45+
46+
// Reset mocks
47+
vi.mocked(utilsEnvelope.createEnvelope).mockClear();
48+
vi.mocked(utilsDsn.dsnToString).mockClear();
49+
});
50+
51+
afterEach(() => {
52+
vi.useRealTimers();
53+
});
54+
55+
it('creates an envelope with basic headers', () => {
56+
const mockMetrics: SerializedMetric[] = [
57+
{
58+
timestamp: 1713859200,
59+
trace_id: '3d9355f71e9c444b81161599adac6e29',
60+
span_id: '8b5f5e5e5e5e5e5e',
61+
name: 'test.metric',
62+
type: 'counter',
63+
value: 1,
64+
unit: 'count',
65+
attributes: {},
66+
},
67+
];
68+
69+
const result = createMetricEnvelope(mockMetrics);
70+
71+
expect(result[0]).toEqual({});
72+
73+
expect(utilsEnvelope.createEnvelope).toHaveBeenCalledWith({}, expect.any(Array));
74+
});
75+
76+
it('includes SDK info when metadata is provided', () => {
77+
const mockMetrics: SerializedMetric[] = [
78+
{
79+
timestamp: 1713859200,
80+
trace_id: '3d9355f71e9c444b81161599adac6e29',
81+
span_id: '8b5f5e5e5e5e5e5e',
82+
name: 'test.metric',
83+
type: 'counter',
84+
value: 1,
85+
unit: 'count',
86+
attributes: {},
87+
},
88+
];
89+
90+
const metadata: SdkMetadata = {
91+
sdk: {
92+
name: 'sentry.javascript.node',
93+
version: '10.0.0',
94+
},
95+
};
96+
97+
const result = createMetricEnvelope(mockMetrics, metadata);
98+
99+
expect(result[0]).toEqual({
100+
sdk: {
101+
name: 'sentry.javascript.node',
102+
version: '10.0.0',
103+
},
104+
});
105+
});
106+
107+
it('includes DSN when tunnel and DSN are provided', () => {
108+
const mockMetrics: SerializedMetric[] = [
109+
{
110+
timestamp: 1713859200,
111+
trace_id: '3d9355f71e9c444b81161599adac6e29',
112+
span_id: '8b5f5e5e5e5e5e5e',
113+
name: 'test.metric',
114+
type: 'counter',
115+
value: 1,
116+
unit: 'count',
117+
attributes: {},
118+
},
119+
];
120+
121+
const dsn: DsnComponents = {
122+
host: 'example.sentry.io',
123+
path: '/',
124+
projectId: '123',
125+
port: '',
126+
protocol: 'https',
127+
publicKey: 'abc123',
128+
};
129+
130+
const result = createMetricEnvelope(mockMetrics, undefined, 'https://tunnel.example.com', dsn);
131+
132+
expect(result[0]).toHaveProperty('dsn');
133+
expect(utilsDsn.dsnToString).toHaveBeenCalledWith(dsn);
134+
});
135+
136+
it('maps each metric to an envelope item', () => {
137+
const mockMetrics: SerializedMetric[] = [
138+
{
139+
timestamp: 1713859200,
140+
trace_id: '3d9355f71e9c444b81161599adac6e29',
141+
span_id: '8b5f5e5e5e5e5e5e',
142+
name: 'first.metric',
143+
type: 'counter',
144+
value: 1,
145+
unit: 'count',
146+
attributes: {},
147+
},
148+
{
149+
timestamp: 1713859201,
150+
trace_id: '3d9355f71e9c444b81161599adac6e29',
151+
span_id: '8b5f5e5e5e5e5e5e',
152+
name: 'second.metric',
153+
type: 'gauge',
154+
value: 42,
155+
unit: 'bytes',
156+
attributes: {},
157+
},
158+
];
159+
160+
createMetricEnvelope(mockMetrics);
161+
162+
// Check that createEnvelope was called with a single container item containing all metrics
163+
expect(utilsEnvelope.createEnvelope).toHaveBeenCalledWith(
164+
expect.anything(),
165+
expect.arrayContaining([
166+
expect.arrayContaining([
167+
{ type: 'metric', item_count: 2, content_type: 'application/vnd.sentry.items.trace-metric+json' },
168+
{ items: mockMetrics },
169+
]),
170+
]),
171+
);
172+
});
173+
});

0 commit comments

Comments
 (0)