|
| 1 | +import type { Event, EventHint } from '@sentry/types'; |
| 2 | + |
| 3 | +import { createTransport } from '../../src'; |
| 4 | +import type { ServerRuntimeClientOptions } from '../../src/server-runtime-client'; |
| 5 | +import { ServerRuntimeClient } from '../../src/server-runtime-client'; |
| 6 | + |
| 7 | +const PUBLIC_DSN = 'https://username@domain/123'; |
| 8 | + |
| 9 | +function getDefaultClientOptions(options: Partial<ServerRuntimeClientOptions> = {}): ServerRuntimeClientOptions { |
| 10 | + return { |
| 11 | + integrations: [], |
| 12 | + transport: () => createTransport({ recordDroppedEvent: () => undefined }, _ => Promise.resolve({})), |
| 13 | + stackParser: () => [], |
| 14 | + instrumenter: 'sentry', |
| 15 | + ...options, |
| 16 | + }; |
| 17 | +} |
| 18 | + |
| 19 | +describe('ServerRuntimeClient', () => { |
| 20 | + let client: ServerRuntimeClient; |
| 21 | + |
| 22 | + describe('_prepareEvent', () => { |
| 23 | + test('adds platform to event', () => { |
| 24 | + const options = getDefaultClientOptions({ dsn: PUBLIC_DSN }); |
| 25 | + const client = new ServerRuntimeClient({ ...options, platform: 'edge' }); |
| 26 | + |
| 27 | + const event: Event = {}; |
| 28 | + const hint: EventHint = {}; |
| 29 | + (client as any)._prepareEvent(event, hint); |
| 30 | + |
| 31 | + expect(event.platform).toEqual('edge'); |
| 32 | + }); |
| 33 | + |
| 34 | + test('adds server_name to event', () => { |
| 35 | + const options = getDefaultClientOptions({ dsn: PUBLIC_DSN }); |
| 36 | + const client = new ServerRuntimeClient({ ...options, serverName: 'server' }); |
| 37 | + |
| 38 | + const event: Event = {}; |
| 39 | + const hint: EventHint = {}; |
| 40 | + (client as any)._prepareEvent(event, hint); |
| 41 | + |
| 42 | + expect(event.server_name).toEqual('server'); |
| 43 | + }); |
| 44 | + |
| 45 | + test('adds runtime context to event', () => { |
| 46 | + const options = getDefaultClientOptions({ dsn: PUBLIC_DSN }); |
| 47 | + const client = new ServerRuntimeClient({ ...options, runtime: { name: 'edge' } }); |
| 48 | + |
| 49 | + const event: Event = {}; |
| 50 | + const hint: EventHint = {}; |
| 51 | + (client as any)._prepareEvent(event, hint); |
| 52 | + |
| 53 | + expect(event.contexts?.runtime).toEqual({ |
| 54 | + name: 'edge', |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + test("doesn't clobber existing runtime data", () => { |
| 59 | + const options = getDefaultClientOptions({ dsn: PUBLIC_DSN }); |
| 60 | + const client = new ServerRuntimeClient({ ...options, runtime: { name: 'edge' } }); |
| 61 | + |
| 62 | + const event: Event = { contexts: { runtime: { name: 'foo', version: '1.2.3' } } }; |
| 63 | + const hint: EventHint = {}; |
| 64 | + (client as any)._prepareEvent(event, hint); |
| 65 | + |
| 66 | + expect(event.contexts?.runtime).toEqual({ name: 'foo', version: '1.2.3' }); |
| 67 | + expect(event.contexts?.runtime).not.toEqual({ name: 'edge' }); |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + describe('captureCheckIn', () => { |
| 72 | + it('sends a checkIn envelope', () => { |
| 73 | + const options = getDefaultClientOptions({ |
| 74 | + dsn: PUBLIC_DSN, |
| 75 | + serverName: 'bar', |
| 76 | + release: '1.0.0', |
| 77 | + environment: 'dev', |
| 78 | + }); |
| 79 | + client = new ServerRuntimeClient(options); |
| 80 | + |
| 81 | + // @ts-ignore accessing private method |
| 82 | + const sendEnvelopeSpy = jest.spyOn(client, '_sendEnvelope'); |
| 83 | + |
| 84 | + const id = client.captureCheckIn( |
| 85 | + { monitorSlug: 'foo', status: 'in_progress' }, |
| 86 | + { |
| 87 | + schedule: { |
| 88 | + type: 'crontab', |
| 89 | + value: '0 * * * *', |
| 90 | + }, |
| 91 | + checkinMargin: 2, |
| 92 | + maxRuntime: 12333, |
| 93 | + timezone: 'Canada/Eastern', |
| 94 | + }, |
| 95 | + ); |
| 96 | + |
| 97 | + expect(sendEnvelopeSpy).toHaveBeenCalledTimes(1); |
| 98 | + expect(sendEnvelopeSpy).toHaveBeenCalledWith([ |
| 99 | + expect.any(Object), |
| 100 | + [ |
| 101 | + [ |
| 102 | + expect.any(Object), |
| 103 | + { |
| 104 | + check_in_id: id, |
| 105 | + monitor_slug: 'foo', |
| 106 | + status: 'in_progress', |
| 107 | + release: '1.0.0', |
| 108 | + environment: 'dev', |
| 109 | + monitor_config: { |
| 110 | + schedule: { |
| 111 | + type: 'crontab', |
| 112 | + value: '0 * * * *', |
| 113 | + }, |
| 114 | + checkin_margin: 2, |
| 115 | + max_runtime: 12333, |
| 116 | + timezone: 'Canada/Eastern', |
| 117 | + }, |
| 118 | + }, |
| 119 | + ], |
| 120 | + ], |
| 121 | + ]); |
| 122 | + |
| 123 | + client.captureCheckIn({ monitorSlug: 'foo', status: 'ok', duration: 1222, checkInId: id }); |
| 124 | + |
| 125 | + expect(sendEnvelopeSpy).toHaveBeenCalledTimes(2); |
| 126 | + expect(sendEnvelopeSpy).toHaveBeenCalledWith([ |
| 127 | + expect.any(Object), |
| 128 | + [ |
| 129 | + [ |
| 130 | + expect.any(Object), |
| 131 | + { |
| 132 | + check_in_id: id, |
| 133 | + monitor_slug: 'foo', |
| 134 | + duration: 1222, |
| 135 | + status: 'ok', |
| 136 | + release: '1.0.0', |
| 137 | + environment: 'dev', |
| 138 | + }, |
| 139 | + ], |
| 140 | + ], |
| 141 | + ]); |
| 142 | + }); |
| 143 | + |
| 144 | + it('does not send a checkIn envelope if disabled', () => { |
| 145 | + const options = getDefaultClientOptions({ dsn: PUBLIC_DSN, serverName: 'bar', enabled: false }); |
| 146 | + client = new ServerRuntimeClient(options); |
| 147 | + |
| 148 | + // @ts-ignore accessing private method |
| 149 | + const sendEnvelopeSpy = jest.spyOn(client, '_sendEnvelope'); |
| 150 | + |
| 151 | + client.captureCheckIn({ monitorSlug: 'foo', status: 'in_progress' }); |
| 152 | + |
| 153 | + expect(sendEnvelopeSpy).toHaveBeenCalledTimes(0); |
| 154 | + }); |
| 155 | + }); |
| 156 | +}); |
0 commit comments