Repository navigation
Expand file tree
/
Copy pathutils.test.ts
More file actions
130 lines (114 loc) · 3.61 KB
/
Copy pathutils.test.ts
File metadata and controls
130 lines (114 loc) · 3.61 KB
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
import type { Event } from '@sentry/core';
import { addItemToEnvelope, createEnvelope, uuid4 } from '@sentry/core';
import type { RawThreadCpuProfile } from '@sentry/node-cpu-profiler';
import { describe, expect, it } from 'vitest';
import {
addProfilesToEnvelope,
findProfiledTransactionsFromEnvelope,
isValidProfile,
isValidSampleRate,
} from '../src/utils';
interface ProfiledEvent extends Event {
sdkProcessingMetadata: {
profile?: RawThreadCpuProfile;
};
}
function makeProfile(
props: Partial<ProfiledEvent['sdkProcessingMetadata']['profile']>,
): NonNullable<ProfiledEvent['sdkProcessingMetadata']['profile']> {
return {
profile_id: '1',
profiler_logging_mode: 'lazy',
stacks: [],
samples: [
{ elapsed_since_start_ns: '0', thread_id: '0', stack_id: 0 },
{ elapsed_since_start_ns: '10', thread_id: '0', stack_id: 0 },
],
measurements: {},
resources: [],
frames: [],
...props,
};
}
describe('isValidSampleRate', () => {
it.each([
[0, true],
[0.1, true],
[1, true],
[true, true],
[false, true],
// invalid values
[1.1, false],
[-0.1, false],
[NaN, false],
[Infinity, false],
[null, false],
[undefined, false],
['', false],
[' ', false],
[{}, false],
[[], false],
[() => null, false],
])('value %s is %s', (input, expected) => {
expect(isValidSampleRate(input)).toBe(expected);
});
});
describe('isValidProfile', () => {
it('is not valid if samples <= 1', () => {
expect(isValidProfile(makeProfile({ samples: [] }))).toBe(false);
});
it('is not valid if it does not have a profile_id', () => {
expect(isValidProfile(makeProfile({ samples: [], profile_id: undefined } as any))).toBe(false);
});
});
describe('addProfilesToEnvelope', () => {
it('adds profile', () => {
const profile = makeProfile({});
const envelope = createEnvelope({});
// @ts-expect-error profile is untyped
addProfilesToEnvelope(envelope, [profile]);
// @ts-expect-error profile is untyped
const addedBySdk = addItemToEnvelope(createEnvelope({}), [{ type: 'profile' }, profile]);
expect(envelope?.[1][0]?.[0]).toEqual({ type: 'profile' });
expect(envelope?.[1][0]?.[1]).toEqual(profile);
expect(JSON.stringify(addedBySdk)).toEqual(JSON.stringify(envelope));
});
});
describe('findProfiledTransactionsFromEnvelope', () => {
it('returns transactions with profile context', () => {
const txnWithProfile: Event = {
event_id: uuid4(),
type: 'transaction',
contexts: {
profile: {
profile_id: uuid4(),
},
},
};
const envelope = addItemToEnvelope(createEnvelope({}), [{ type: 'transaction' }, txnWithProfile]);
expect(findProfiledTransactionsFromEnvelope(envelope)[0]).toBe(txnWithProfile);
});
it('skips if transaction event is not profiled', () => {
const txnWithProfile: Event = {
event_id: uuid4(),
type: 'transaction',
contexts: {},
};
const envelope = addItemToEnvelope(createEnvelope({}), [{ type: 'transaction' }, txnWithProfile]);
expect(findProfiledTransactionsFromEnvelope(envelope)[0]).toBe(undefined);
});
it('skips if event is not a transaction', () => {
const nonTransactionEvent: Event = {
event_id: uuid4(),
type: 'replay_event',
contexts: {
profile: {
profile_id: uuid4(),
},
},
};
// @ts-expect-error replay event is partial
const envelope = addItemToEnvelope(createEnvelope({}), [{ type: 'replay_event' }, nonTransactionEvent]);
expect(findProfiledTransactionsFromEnvelope(envelope)[0]).toBe(undefined);
});
});