-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathenvelope.test.ts
217 lines (188 loc) · 7.92 KB
/
envelope.test.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import type { Event, EventEnvelope, SpanAttributes } from '@sentry/types';
import {
SEMANTIC_ATTRIBUTE_EXCLUSIVE_TIME,
SEMANTIC_ATTRIBUTE_SENTRY_MEASUREMENT_UNIT,
SEMANTIC_ATTRIBUTE_SENTRY_MEASUREMENT_VALUE,
spanToJSON,
} from '@sentry/core';
import { SentrySpan } from '@sentry/core';
import {
addItemToEnvelope,
createEnvelope,
createSpanEnvelopeItem,
forEachEnvelopeItem,
parseEnvelope,
serializeEnvelope,
} from '../src/envelope';
import type { InternalGlobal } from '../src/worldwide';
import { GLOBAL_OBJ } from '../src/worldwide';
describe('envelope', () => {
describe('createSpanEnvelope()', () => {
it('span-envelope-item of INP event has the correct object structure', () => {
const attributes: SpanAttributes = {
release: 'releaseString',
environment: 'dev',
transaction: '/test-route',
[SEMANTIC_ATTRIBUTE_EXCLUSIVE_TIME]: 80,
user: 10,
profile_id: 'test-profile-id',
replay_id: 'test-replay-id',
};
const startTime = 1713365480;
const span = new SentrySpan({
startTimestamp: startTime,
endTimestamp: startTime + 2,
op: 'ui.interaction.click',
name: '<unknown>',
attributes,
});
span.addEvent('inp', {
[SEMANTIC_ATTRIBUTE_SENTRY_MEASUREMENT_UNIT]: 'millisecond',
[SEMANTIC_ATTRIBUTE_SENTRY_MEASUREMENT_VALUE]: 100,
});
const spanEnvelopeItem = createSpanEnvelopeItem(spanToJSON(span));
const expectedObj = {
data: {
'sentry.origin': expect.any(String),
'sentry.op': expect.any(String),
release: expect.any(String),
environment: expect.any(String),
transaction: expect.any(String),
'sentry.exclusive_time': expect.any(Number),
user: expect.any(Number),
profile_id: expect.any(String),
replay_id: expect.any(String),
},
description: expect.any(String),
op: expect.any(String),
span_id: expect.any(String),
start_timestamp: expect.any(Number),
timestamp: expect.any(Number),
trace_id: expect.any(String),
origin: expect.any(String),
exclusive_time: expect.any(Number),
measurements: { inp: { value: expect.any(Number), unit: expect.any(String) } },
};
expect(spanEnvelopeItem[0]?.type).toBe('span');
expect(spanEnvelopeItem[1]).toMatchObject(expectedObj);
});
});
describe('createEnvelope()', () => {
const testTable: Array<[string, Parameters<typeof createEnvelope>[0], Parameters<typeof createEnvelope>[1]]> = [
['creates an empty envelope', {}, []],
['creates an envelope with a header but no items', { dsn: 'https://public@example.com/1', sdk: {} }, []],
];
it.each(testTable)('%s', (_: string, headers, items) => {
const env = createEnvelope(headers, items);
expect(env).toHaveLength(2);
expect(env[0]).toStrictEqual(headers);
expect(env[1]).toStrictEqual(items);
});
});
describe('serializeEnvelope and parseEnvelope', () => {
afterEach(() => {
delete (GLOBAL_OBJ as Partial<InternalGlobal>).__SENTRY__;
});
it('serializes an envelope', () => {
const env = createEnvelope<EventEnvelope>({ event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2', sent_at: '123' }, []);
const serializedEnvelope = serializeEnvelope(env);
expect(typeof serializedEnvelope).toBe('string');
const [headers] = parseEnvelope(serializedEnvelope);
expect(headers).toEqual({ event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2', sent_at: '123' });
});
test.each([
{
name: 'with TextEncoder/Decoder polyfill',
before: () => {
GLOBAL_OBJ.__SENTRY__ = {} as InternalGlobal['__SENTRY__'];
GLOBAL_OBJ.__SENTRY__.encodePolyfill = jest.fn<Uint8Array, [string]>((input: string) =>
new TextEncoder().encode(input),
);
GLOBAL_OBJ.__SENTRY__.decodePolyfill = jest.fn<string, [Uint8Array]>((input: Uint8Array) =>
new TextDecoder().decode(input),
);
},
after: () => {
expect(GLOBAL_OBJ.__SENTRY__.encodePolyfill).toHaveBeenCalled();
expect(GLOBAL_OBJ.__SENTRY__.decodePolyfill).toHaveBeenCalled();
},
},
{
name: 'with default TextEncoder/Decoder',
},
])('serializes an envelope with attachments $name', ({ before, after }) => {
before?.();
const items: EventEnvelope[1] = [
[{ type: 'event' }, { event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2' }],
[{ type: 'attachment', filename: 'bar.txt', length: 6 }, Uint8Array.from([1, 2, 3, 4, 5, 6])],
[{ type: 'attachment', filename: 'foo.txt', length: 6 }, Uint8Array.from([7, 8, 9, 10, 11, 12])],
];
const env = createEnvelope<EventEnvelope>(
{ event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2', sent_at: '123' },
items,
);
const serializedEnvelope = serializeEnvelope(env);
expect(serializedEnvelope).toBeInstanceOf(Uint8Array);
const [parsedHeaders, parsedItems] = parseEnvelope(serializedEnvelope);
expect(parsedHeaders).toEqual({ event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2', sent_at: '123' });
expect(parsedItems).toHaveLength(3);
expect(items[0]).toEqual([{ type: 'event' }, { event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2' }]);
expect(items[1]).toEqual([
{ type: 'attachment', filename: 'bar.txt', length: 6 },
Uint8Array.from([1, 2, 3, 4, 5, 6]),
]);
expect(items[2]).toEqual([
{ type: 'attachment', filename: 'foo.txt', length: 6 },
Uint8Array.from([7, 8, 9, 10, 11, 12]),
]);
after?.();
});
it("doesn't throw when being passed a an envelope that contains a circular item payload", () => {
const chicken: { egg?: any } = {};
const egg = { chicken } as unknown as Event;
chicken.egg = chicken;
const env = createEnvelope<EventEnvelope>({ event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2', sent_at: '123' }, [
[{ type: 'event' }, egg],
]);
const serializedEnvelope = serializeEnvelope(env);
const [, , serializedBody] = serializedEnvelope.toString().split('\n');
expect(serializedBody).toBe('{"chicken":{"egg":"[Circular ~]"}}');
});
});
describe('addItemToEnvelope()', () => {
it('adds an item to an envelope', () => {
const env = createEnvelope<EventEnvelope>({ event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2', sent_at: '123' }, []);
let [envHeaders, items] = parseEnvelope(serializeEnvelope(env));
expect(items).toHaveLength(0);
expect(envHeaders).toEqual({ event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2', sent_at: '123' });
const newEnv = addItemToEnvelope<EventEnvelope>(env, [
{ type: 'event' },
{ event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2' },
]);
[envHeaders, items] = parseEnvelope(serializeEnvelope(newEnv));
expect(envHeaders).toEqual({ event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2', sent_at: '123' });
expect(items).toHaveLength(1);
expect(items[0]).toEqual([{ type: 'event' }, { event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2' }]);
});
});
describe('forEachEnvelopeItem', () => {
it('loops through an envelope', () => {
const items: EventEnvelope[1] = [
[{ type: 'event' }, { event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2' }],
[{ type: 'attachment', filename: 'bar.txt', length: 6 }, Uint8Array.from([1, 2, 3, 4, 5, 6])],
[{ type: 'attachment', filename: 'foo.txt', length: 6 }, Uint8Array.from([7, 8, 9, 10, 11, 12])],
];
const env = createEnvelope<EventEnvelope>(
{ event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2', sent_at: '123' },
items,
);
expect.assertions(6);
let iteration = 0;
forEachEnvelopeItem(env, (item, type) => {
expect(item).toBe(items[iteration]);
expect(type).toBe(items[iteration]?.[0]?.type);
iteration = iteration + 1;
});
});
});
});