|
| 1 | +import type { Contexts, Envelope, Event, SdkInfo } from '@sentry/core'; |
| 2 | +import { SDK_VERSION } from '@sentry/core'; |
| 3 | +import { expect } from 'vitest'; |
| 4 | + |
| 5 | +export const UUID_MATCHER = expect.stringMatching(/^[0-9a-f]{32}$/); |
| 6 | +export const UUID_V4_MATCHER = expect.stringMatching( |
| 7 | + /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/, |
| 8 | +); |
| 9 | +export const SHORT_UUID_MATCHER = expect.stringMatching(/^[0-9a-f]{16}$/); |
| 10 | +export const ISO_DATE_MATCHER = expect.stringMatching(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/); |
| 11 | + |
| 12 | +function dropUndefinedKeys<T extends Record<string, unknown>>(obj: T): T { |
| 13 | + for (const [key, value] of Object.entries(obj)) { |
| 14 | + if (value === undefined) { |
| 15 | + // eslint-disable-next-line @typescript-eslint/no-dynamic-delete |
| 16 | + delete obj[key]; |
| 17 | + } |
| 18 | + } |
| 19 | + return obj; |
| 20 | +} |
| 21 | + |
| 22 | +function getSdk(): SdkInfo { |
| 23 | + return { |
| 24 | + integrations: expect.any(Array), |
| 25 | + name: 'sentry.javascript.cloudflare', |
| 26 | + packages: [ |
| 27 | + { |
| 28 | + name: 'npm:@sentry/cloudflare', |
| 29 | + version: SDK_VERSION, |
| 30 | + }, |
| 31 | + ], |
| 32 | + version: SDK_VERSION, |
| 33 | + }; |
| 34 | +} |
| 35 | + |
| 36 | +function defaultContexts(eventContexts: Contexts = {}): Contexts { |
| 37 | + return dropUndefinedKeys({ |
| 38 | + trace: { |
| 39 | + trace_id: UUID_MATCHER, |
| 40 | + span_id: SHORT_UUID_MATCHER, |
| 41 | + }, |
| 42 | + cloud_resource: { 'cloud.provider': 'cloudflare' }, |
| 43 | + culture: { timezone: expect.any(String) }, |
| 44 | + runtime: { name: 'cloudflare' }, |
| 45 | + ...eventContexts, |
| 46 | + }); |
| 47 | +} |
| 48 | + |
| 49 | +export function expectedEvent(event: Event): Event { |
| 50 | + return dropUndefinedKeys({ |
| 51 | + event_id: UUID_MATCHER, |
| 52 | + timestamp: expect.any(Number), |
| 53 | + environment: 'production', |
| 54 | + platform: 'javascript', |
| 55 | + sdk: getSdk(), |
| 56 | + ...event, |
| 57 | + contexts: defaultContexts(event.contexts), |
| 58 | + }); |
| 59 | +} |
| 60 | + |
| 61 | +export function eventEnvelope(event: Event): Envelope { |
| 62 | + return [ |
| 63 | + { |
| 64 | + event_id: UUID_MATCHER, |
| 65 | + sent_at: ISO_DATE_MATCHER, |
| 66 | + sdk: { name: 'sentry.javascript.cloudflare', version: SDK_VERSION }, |
| 67 | + trace: { |
| 68 | + environment: event.environment || 'production', |
| 69 | + public_key: 'public', |
| 70 | + trace_id: UUID_MATCHER, |
| 71 | + sample_rate: expect.any(String), |
| 72 | + sampled: expect.any(String), |
| 73 | + transaction: expect.any(String), |
| 74 | + }, |
| 75 | + }, |
| 76 | + [[{ type: 'event' }, expectedEvent(event)]], |
| 77 | + ]; |
| 78 | +} |
0 commit comments