|
| 1 | +import { expect } from '@playwright/test'; |
| 2 | +import type { Event, Profile, ProfileChunkEnvelope } from '@sentry/core'; |
| 3 | +import { sentryTest } from '../../../utils/fixtures'; |
| 4 | +import { |
| 5 | + getMultipleSentryEnvelopeRequests, |
| 6 | + properEnvelopeRequestParser, |
| 7 | + properFullEnvelopeRequestParser, |
| 8 | + shouldSkipTracingTest, |
| 9 | + waitForTransactionRequestOnUrl, |
| 10 | +} from '../../../utils/helpers'; |
| 11 | + |
| 12 | +sentryTest('does not send profile envelope when document-policy is not set', async ({ page, getLocalTestUrl }) => { |
| 13 | + if (shouldSkipTracingTest()) { |
| 14 | + // Profiling only works when tracing is enabled |
| 15 | + sentryTest.skip(); |
| 16 | + } |
| 17 | + |
| 18 | + const url = await getLocalTestUrl({ testDir: __dirname }); |
| 19 | + |
| 20 | + const req = await waitForTransactionRequestOnUrl(page, url); |
| 21 | + const transactionEvent = properEnvelopeRequestParser<Event>(req, 0); |
| 22 | + const profileEvent = properEnvelopeRequestParser<Profile>(req, 1); |
| 23 | + |
| 24 | + expect(transactionEvent).toBeDefined(); |
| 25 | + |
| 26 | + expect(profileEvent).toBeUndefined(); |
| 27 | +}); |
| 28 | + |
| 29 | +sentryTest( |
| 30 | + 'sends profile envelope in trace mode (single chunk for overlapping spans)', |
| 31 | + async ({ page, getLocalTestUrl, browserName }) => { |
| 32 | + if (shouldSkipTracingTest() || browserName !== 'chromium') { |
| 33 | + // Profiling only works when tracing is enabled |
| 34 | + sentryTest.skip(); |
| 35 | + } |
| 36 | + |
| 37 | + const url = await getLocalTestUrl({ testDir: __dirname, responseHeaders: { 'Document-Policy': 'js-profiling' } }); |
| 38 | + await page.goto(url); |
| 39 | + |
| 40 | + const profileChunkEnvelopePromise = getMultipleSentryEnvelopeRequests<ProfileChunkEnvelope>( |
| 41 | + page, |
| 42 | + 1, |
| 43 | + { envelopeType: 'profile_chunk' }, |
| 44 | + properFullEnvelopeRequestParser, |
| 45 | + ); |
| 46 | + |
| 47 | + const profileChunkEnvelopeItem = (await profileChunkEnvelopePromise)[0][1][0]; |
| 48 | + const envelopeItemHeader = profileChunkEnvelopeItem[0]; |
| 49 | + const envelopeItemPayload = profileChunkEnvelopeItem[1]; |
| 50 | + |
| 51 | + expect(envelopeItemHeader).toHaveProperty('type', 'profile_chunk'); |
| 52 | + |
| 53 | + expect(envelopeItemPayload.profile).toBeDefined(); |
| 54 | + expect(envelopeItemPayload.version).toBe('2'); |
| 55 | + expect(envelopeItemPayload.platform).toBe('javascript'); |
| 56 | + |
| 57 | + const profile = envelopeItemPayload.profile; |
| 58 | + |
| 59 | + expect(profile.samples).toBeDefined(); |
| 60 | + expect(profile.stacks).toBeDefined(); |
| 61 | + expect(profile.frames).toBeDefined(); |
| 62 | + expect(profile.thread_metadata).toBeDefined(); |
| 63 | + |
| 64 | + // Samples |
| 65 | + expect(profile.samples.length).toBeGreaterThanOrEqual(2); |
| 66 | + let previousTimestamp = Number.NEGATIVE_INFINITY; |
| 67 | + for (const sample of profile.samples) { |
| 68 | + expect(typeof sample.stack_id).toBe('number'); |
| 69 | + expect(sample.stack_id).toBeGreaterThanOrEqual(0); |
| 70 | + expect(sample.stack_id).toBeLessThan(profile.stacks.length); |
| 71 | + |
| 72 | + // In trace lifecycle mode, samples carry a numeric timestamp (ms since epoch or similar clock) |
| 73 | + expect(typeof (sample as any).timestamp).toBe('number'); |
| 74 | + const ts = (sample as any).timestamp as number; |
| 75 | + expect(Number.isFinite(ts)).toBe(true); |
| 76 | + expect(ts).toBeGreaterThan(0); |
| 77 | + // Monotonic non-decreasing timestamps |
| 78 | + expect(ts).toBeGreaterThanOrEqual(previousTimestamp); |
| 79 | + previousTimestamp = ts; |
| 80 | + |
| 81 | + expect(sample.thread_id).toBe('0'); // Should be main thread |
| 82 | + } |
| 83 | + |
| 84 | + // Stacks |
| 85 | + expect(profile.stacks.length).toBeGreaterThan(0); |
| 86 | + for (const stack of profile.stacks) { |
| 87 | + expect(Array.isArray(stack)).toBe(true); |
| 88 | + for (const frameIndex of stack) { |
| 89 | + expect(typeof frameIndex).toBe('number'); |
| 90 | + expect(frameIndex).toBeGreaterThanOrEqual(0); |
| 91 | + expect(frameIndex).toBeLessThan(profile.frames.length); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // Frames |
| 96 | + expect(profile.frames.length).toBeGreaterThan(0); |
| 97 | + for (const frame of profile.frames) { |
| 98 | + expect(frame).toHaveProperty('function'); |
| 99 | + expect(frame).toHaveProperty('abs_path'); |
| 100 | + expect(frame).toHaveProperty('lineno'); |
| 101 | + expect(frame).toHaveProperty('colno'); |
| 102 | + |
| 103 | + expect(typeof frame.function).toBe('string'); |
| 104 | + expect(typeof frame.abs_path).toBe('string'); |
| 105 | + expect(typeof frame.lineno).toBe('number'); |
| 106 | + expect(typeof frame.colno).toBe('number'); |
| 107 | + } |
| 108 | + |
| 109 | + const functionNames = profile.frames.map(frame => frame.function).filter(name => name !== ''); |
| 110 | + |
| 111 | + if ((process.env.PW_BUNDLE || '').endsWith('min')) { |
| 112 | + // In bundled mode, function names are minified |
| 113 | + expect(functionNames.length).toBeGreaterThan(0); |
| 114 | + expect((functionNames as string[]).every(name => name?.length > 0)).toBe(true); // Just make sure they're not empty strings |
| 115 | + } else { |
| 116 | + expect(functionNames).toEqual( |
| 117 | + expect.arrayContaining([ |
| 118 | + '_startRootSpan', |
| 119 | + 'withScope', |
| 120 | + 'createChildOrRootSpan', |
| 121 | + 'startSpanManual', |
| 122 | + 'startJSSelfProfile', |
| 123 | + |
| 124 | + // both functions are captured |
| 125 | + 'fibonacci', |
| 126 | + 'largeSum', |
| 127 | + ]), |
| 128 | + ); |
| 129 | + } |
| 130 | + |
| 131 | + expect(profile.thread_metadata).toHaveProperty('0'); |
| 132 | + expect(profile.thread_metadata['0']).toHaveProperty('name'); |
| 133 | + expect(profile.thread_metadata['0'].name).toBe('main'); |
| 134 | + |
| 135 | + // Test that profile duration makes sense (should be > 20ms based on test setup) |
| 136 | + const startTimeMs = (profile.samples[0] as any).timestamp as number; |
| 137 | + const endTimeMs = (profile.samples[profile.samples.length - 1] as any).timestamp as number; |
| 138 | + const durationMs = endTimeMs - startTimeMs; |
| 139 | + |
| 140 | + // Should be at least 20ms based on our setTimeout(21) in the test |
| 141 | + expect(durationMs).toBeGreaterThan(20); |
| 142 | + }, |
| 143 | +); |
0 commit comments