-
-
Notifications
You must be signed in to change notification settings - Fork 360
Expand file tree
/
Copy pathutils.test.ts
More file actions
150 lines (124 loc) · 5.39 KB
/
utils.test.ts
File metadata and controls
150 lines (124 loc) · 5.39 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
jest.mock('../../src/js/utils/environment');
jest.mock('../../src/js/profiling/debugid');
import type { Event } from '@sentry/core';
import type { AndroidCombinedProfileEvent, CombinedProfileEvent } from '../../src/js/profiling/types';
import { getDebugMetadata } from '../../src/js/profiling/debugid';
import {
enrichAndroidProfileWithEventContext,
enrichCombinedProfileWithEventContext,
} from '../../src/js/profiling/utils';
import { getDefaultEnvironment } from '../../src/js/utils/environment';
import { createMockMinimalValidAndroidProfile, createMockMinimalValidHermesProfileEvent } from './fixtures';
describe('enrichCombinedProfileWithEventContext', () => {
beforeEach(() => {
(getDefaultEnvironment as jest.Mock).mockReturnValue('production');
(getDebugMetadata as jest.Mock).mockReturnValue([]);
});
function createMockEvent(overrides?: Partial<Event>): Event {
return {
event_id: 'test-event-id',
transaction: 'test-transaction',
release: 'test-release',
environment: 'test-env',
start_timestamp: 1000,
contexts: {
trace: {
trace_id: '12345678901234567890123456789012',
},
os: { name: 'iOS', version: '17.0' },
device: {},
},
...overrides,
};
}
test('should use profilingStartTimestampNs for timestamp when available', () => {
const profilingStartTimestampNs = 1500 * 1e9; // 1500 seconds in ns
const profile: CombinedProfileEvent = {
...createMockMinimalValidHermesProfileEvent(),
profilingStartTimestampNs,
};
const event = createMockEvent({ start_timestamp: 1000 }); // earlier than profiling start
const result = enrichCombinedProfileWithEventContext('profile-id', profile, event);
expect(result).not.toBeNull();
expect(result!.timestamp).toBe(new Date(profilingStartTimestampNs / 1e6).toISOString());
// Should NOT use event.start_timestamp
expect(result!.timestamp).not.toBe(new Date(1000 * 1000).toISOString());
});
test('should fall back to event.start_timestamp when profilingStartTimestampNs is not set', () => {
const profile: CombinedProfileEvent = createMockMinimalValidHermesProfileEvent();
const event = createMockEvent({ start_timestamp: 1000 });
const result = enrichCombinedProfileWithEventContext('profile-id', profile, event);
expect(result).not.toBeNull();
expect(result!.timestamp).toBe(new Date(1000 * 1000).toISOString());
});
test('should not include profilingStartTimestampNs in the output', () => {
const profile: CombinedProfileEvent = {
...createMockMinimalValidHermesProfileEvent(),
profilingStartTimestampNs: 1500 * 1e9,
};
const event = createMockEvent();
const result = enrichCombinedProfileWithEventContext('profile-id', profile, event);
expect(result).not.toBeNull();
expect(result).not.toHaveProperty('profilingStartTimestampNs');
});
});
describe('enrichAndroidProfileWithEventContext', () => {
beforeEach(() => {
(getDefaultEnvironment as jest.Mock).mockReturnValue('production');
(getDebugMetadata as jest.Mock).mockReturnValue([]);
});
function createMockEvent(overrides?: Partial<Event>): Event {
return {
event_id: 'test-event-id',
transaction: 'test-transaction',
release: 'test-release',
environment: 'test-env',
start_timestamp: 1000,
contexts: {
trace: {
trace_id: '12345678901234567890123456789012',
},
os: { name: 'Android', version: '14' },
device: {},
},
...overrides,
};
}
function createMockAndroidCombinedProfile(
overrides?: Partial<AndroidCombinedProfileEvent>,
): AndroidCombinedProfileEvent {
const hermesProfileEvent = createMockMinimalValidHermesProfileEvent();
return {
platform: 'android',
sampled_profile: createMockMinimalValidAndroidProfile().sampled_profile,
js_profile: hermesProfileEvent.profile,
android_api_level: 34,
duration_ns: '1000000',
active_thread_id: '123',
...overrides,
};
}
test('should use profilingStartTimestampNs for timestamp when available', () => {
const profilingStartTimestampNs = 1500 * 1e9;
const profile = createMockAndroidCombinedProfile({ profilingStartTimestampNs });
const event = createMockEvent({ start_timestamp: 1000 });
const result = enrichAndroidProfileWithEventContext('profile-id', profile, event);
expect(result).not.toBeNull();
expect(result!.timestamp).toBe(new Date(profilingStartTimestampNs / 1e6).toISOString());
expect(result!.timestamp).not.toBe(new Date(1000 * 1000).toISOString());
});
test('should fall back to event.start_timestamp when profilingStartTimestampNs is not set', () => {
const profile = createMockAndroidCombinedProfile();
const event = createMockEvent({ start_timestamp: 1000 });
const result = enrichAndroidProfileWithEventContext('profile-id', profile, event);
expect(result).not.toBeNull();
expect(result!.timestamp).toBe(new Date(1000 * 1000).toISOString());
});
test('should not include profilingStartTimestampNs in the output', () => {
const profile = createMockAndroidCombinedProfile({ profilingStartTimestampNs: 1500 * 1e9 });
const event = createMockEvent();
const result = enrichAndroidProfileWithEventContext('profile-id', profile, event);
expect(result).not.toBeNull();
expect(result).not.toHaveProperty('profilingStartTimestampNs');
});
});