|
| 1 | +jest.mock('../../src/js/utils/environment'); |
| 2 | +jest.mock('../../src/js/profiling/debugid'); |
| 3 | + |
| 4 | +import type { Event } from '@sentry/core'; |
| 5 | + |
| 6 | +import type { AndroidCombinedProfileEvent, CombinedProfileEvent } from '../../src/js/profiling/types'; |
| 7 | + |
| 8 | +import { getDebugMetadata } from '../../src/js/profiling/debugid'; |
| 9 | +import { |
| 10 | + enrichAndroidProfileWithEventContext, |
| 11 | + enrichCombinedProfileWithEventContext, |
| 12 | +} from '../../src/js/profiling/utils'; |
| 13 | +import { getDefaultEnvironment } from '../../src/js/utils/environment'; |
| 14 | +import { createMockMinimalValidAndroidProfile, createMockMinimalValidHermesProfileEvent } from './fixtures'; |
| 15 | + |
| 16 | +describe('enrichCombinedProfileWithEventContext', () => { |
| 17 | + beforeEach(() => { |
| 18 | + (getDefaultEnvironment as jest.Mock).mockReturnValue('production'); |
| 19 | + (getDebugMetadata as jest.Mock).mockReturnValue([]); |
| 20 | + }); |
| 21 | + |
| 22 | + function createMockEvent(overrides?: Partial<Event>): Event { |
| 23 | + return { |
| 24 | + event_id: 'test-event-id', |
| 25 | + transaction: 'test-transaction', |
| 26 | + release: 'test-release', |
| 27 | + environment: 'test-env', |
| 28 | + start_timestamp: 1000, |
| 29 | + contexts: { |
| 30 | + trace: { |
| 31 | + trace_id: '12345678901234567890123456789012', |
| 32 | + }, |
| 33 | + os: { name: 'iOS', version: '17.0' }, |
| 34 | + device: {}, |
| 35 | + }, |
| 36 | + ...overrides, |
| 37 | + }; |
| 38 | + } |
| 39 | + |
| 40 | + test('should use profilingStartTimestampNs for timestamp when available', () => { |
| 41 | + const profilingStartTimestampNs = 1500 * 1e9; // 1500 seconds in ns |
| 42 | + const profile: CombinedProfileEvent = { |
| 43 | + ...createMockMinimalValidHermesProfileEvent(), |
| 44 | + profilingStartTimestampNs, |
| 45 | + }; |
| 46 | + const event = createMockEvent({ start_timestamp: 1000 }); // earlier than profiling start |
| 47 | + |
| 48 | + const result = enrichCombinedProfileWithEventContext('profile-id', profile, event); |
| 49 | + |
| 50 | + expect(result).not.toBeNull(); |
| 51 | + expect(result!.timestamp).toBe(new Date(profilingStartTimestampNs / 1e6).toISOString()); |
| 52 | + // Should NOT use event.start_timestamp |
| 53 | + expect(result!.timestamp).not.toBe(new Date(1000 * 1000).toISOString()); |
| 54 | + }); |
| 55 | + |
| 56 | + test('should fall back to event.start_timestamp when profilingStartTimestampNs is not set', () => { |
| 57 | + const profile: CombinedProfileEvent = createMockMinimalValidHermesProfileEvent(); |
| 58 | + const event = createMockEvent({ start_timestamp: 1000 }); |
| 59 | + |
| 60 | + const result = enrichCombinedProfileWithEventContext('profile-id', profile, event); |
| 61 | + |
| 62 | + expect(result).not.toBeNull(); |
| 63 | + expect(result!.timestamp).toBe(new Date(1000 * 1000).toISOString()); |
| 64 | + }); |
| 65 | + |
| 66 | + test('should not include profilingStartTimestampNs in the output', () => { |
| 67 | + const profile: CombinedProfileEvent = { |
| 68 | + ...createMockMinimalValidHermesProfileEvent(), |
| 69 | + profilingStartTimestampNs: 1500 * 1e9, |
| 70 | + }; |
| 71 | + const event = createMockEvent(); |
| 72 | + |
| 73 | + const result = enrichCombinedProfileWithEventContext('profile-id', profile, event); |
| 74 | + |
| 75 | + expect(result).not.toBeNull(); |
| 76 | + expect(result).not.toHaveProperty('profilingStartTimestampNs'); |
| 77 | + }); |
| 78 | +}); |
| 79 | + |
| 80 | +describe('enrichAndroidProfileWithEventContext', () => { |
| 81 | + beforeEach(() => { |
| 82 | + (getDefaultEnvironment as jest.Mock).mockReturnValue('production'); |
| 83 | + (getDebugMetadata as jest.Mock).mockReturnValue([]); |
| 84 | + }); |
| 85 | + |
| 86 | + function createMockEvent(overrides?: Partial<Event>): Event { |
| 87 | + return { |
| 88 | + event_id: 'test-event-id', |
| 89 | + transaction: 'test-transaction', |
| 90 | + release: 'test-release', |
| 91 | + environment: 'test-env', |
| 92 | + start_timestamp: 1000, |
| 93 | + contexts: { |
| 94 | + trace: { |
| 95 | + trace_id: '12345678901234567890123456789012', |
| 96 | + }, |
| 97 | + os: { name: 'Android', version: '14' }, |
| 98 | + device: {}, |
| 99 | + }, |
| 100 | + ...overrides, |
| 101 | + }; |
| 102 | + } |
| 103 | + |
| 104 | + function createMockAndroidCombinedProfile( |
| 105 | + overrides?: Partial<AndroidCombinedProfileEvent>, |
| 106 | + ): AndroidCombinedProfileEvent { |
| 107 | + const hermesProfileEvent = createMockMinimalValidHermesProfileEvent(); |
| 108 | + return { |
| 109 | + platform: 'android', |
| 110 | + sampled_profile: createMockMinimalValidAndroidProfile().sampled_profile, |
| 111 | + js_profile: hermesProfileEvent.profile, |
| 112 | + android_api_level: 34, |
| 113 | + duration_ns: '1000000', |
| 114 | + active_thread_id: '123', |
| 115 | + ...overrides, |
| 116 | + }; |
| 117 | + } |
| 118 | + |
| 119 | + test('should use profilingStartTimestampNs for timestamp when available', () => { |
| 120 | + const profilingStartTimestampNs = 1500 * 1e9; |
| 121 | + const profile = createMockAndroidCombinedProfile({ profilingStartTimestampNs }); |
| 122 | + const event = createMockEvent({ start_timestamp: 1000 }); |
| 123 | + |
| 124 | + const result = enrichAndroidProfileWithEventContext('profile-id', profile, event); |
| 125 | + |
| 126 | + expect(result).not.toBeNull(); |
| 127 | + expect(result!.timestamp).toBe(new Date(profilingStartTimestampNs / 1e6).toISOString()); |
| 128 | + expect(result!.timestamp).not.toBe(new Date(1000 * 1000).toISOString()); |
| 129 | + }); |
| 130 | + |
| 131 | + test('should fall back to event.start_timestamp when profilingStartTimestampNs is not set', () => { |
| 132 | + const profile = createMockAndroidCombinedProfile(); |
| 133 | + const event = createMockEvent({ start_timestamp: 1000 }); |
| 134 | + |
| 135 | + const result = enrichAndroidProfileWithEventContext('profile-id', profile, event); |
| 136 | + |
| 137 | + expect(result).not.toBeNull(); |
| 138 | + expect(result!.timestamp).toBe(new Date(1000 * 1000).toISOString()); |
| 139 | + }); |
| 140 | + |
| 141 | + test('should not include profilingStartTimestampNs in the output', () => { |
| 142 | + const profile = createMockAndroidCombinedProfile({ profilingStartTimestampNs: 1500 * 1e9 }); |
| 143 | + const event = createMockEvent(); |
| 144 | + |
| 145 | + const result = enrichAndroidProfileWithEventContext('profile-id', profile, event); |
| 146 | + |
| 147 | + expect(result).not.toBeNull(); |
| 148 | + expect(result).not.toHaveProperty('profilingStartTimestampNs'); |
| 149 | + }); |
| 150 | +}); |
0 commit comments