|
| 1 | +import 'package:sentry/sentry.dart'; |
| 2 | +import 'package:sentry/src/platform/mock_platform.dart'; |
| 3 | +import 'package:sentry/src/sentry_tracer.dart'; |
| 4 | +import 'package:test/test.dart'; |
| 5 | + |
| 6 | +import 'mocks/mock_client_report_recorder.dart'; |
| 7 | +import 'mocks/mock_log_batcher.dart'; |
| 8 | +import 'mocks/mock_transport.dart'; |
| 9 | +import 'sentry_client_test.dart'; |
| 10 | +import 'test_utils.dart'; |
| 11 | +import 'utils/url_details_test.dart'; |
| 12 | + |
| 13 | +void main() { |
| 14 | + group('SDK lifecycle callbacks', () { |
| 15 | + late Fixture fixture; |
| 16 | + |
| 17 | + setUp(() => fixture = Fixture()); |
| 18 | + |
| 19 | + group('Logs', () { |
| 20 | + SentryLog givenLog() { |
| 21 | + return SentryLog( |
| 22 | + timestamp: DateTime.now(), |
| 23 | + traceId: SentryId.newId(), |
| 24 | + level: SentryLogLevel.info, |
| 25 | + body: 'test', |
| 26 | + attributes: { |
| 27 | + 'attribute': SentryLogAttribute.string('value'), |
| 28 | + }, |
| 29 | + ); |
| 30 | + } |
| 31 | + |
| 32 | + test('captureLog triggers OnBeforeCaptureLog', () async { |
| 33 | + fixture.options.enableLogs = true; |
| 34 | + fixture.options.environment = 'test-environment'; |
| 35 | + fixture.options.release = 'test-release'; |
| 36 | + |
| 37 | + final log = givenLog(); |
| 38 | + |
| 39 | + final scope = Scope(fixture.options); |
| 40 | + final span = MockSpan(); |
| 41 | + scope.span = span; |
| 42 | + |
| 43 | + final client = fixture.getSut(); |
| 44 | + fixture.options.logBatcher = MockLogBatcher(); |
| 45 | + |
| 46 | + client.lifeCycleRegistry.registerCallback<OnBeforeCaptureLog>((event) { |
| 47 | + event.log.attributes['test'] = |
| 48 | + SentryLogAttribute.string('test-value'); |
| 49 | + }); |
| 50 | + |
| 51 | + await client.captureLog(log, scope: scope); |
| 52 | + |
| 53 | + final mockLogBatcher = fixture.options.logBatcher as MockLogBatcher; |
| 54 | + expect(mockLogBatcher.addLogCalls.length, 1); |
| 55 | + final capturedLog = mockLogBatcher.addLogCalls.first; |
| 56 | + |
| 57 | + expect(capturedLog.attributes['test']?.value, "test-value"); |
| 58 | + expect(capturedLog.attributes['test']?.type, 'string'); |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + group('SentryEvent', () { |
| 63 | + test('captureEvent triggers OnBeforeSendEvent', () async { |
| 64 | + fixture.options.enableLogs = true; |
| 65 | + fixture.options.environment = 'test-environment'; |
| 66 | + fixture.options.release = 'test-release'; |
| 67 | + |
| 68 | + final event = SentryEvent(); |
| 69 | + |
| 70 | + final scope = Scope(fixture.options); |
| 71 | + final span = MockSpan(); |
| 72 | + scope.span = span; |
| 73 | + |
| 74 | + final client = fixture.getSut(); |
| 75 | + fixture.options.logBatcher = MockLogBatcher(); |
| 76 | + |
| 77 | + client.lifeCycleRegistry.registerCallback<OnBeforeSendEvent>((event) { |
| 78 | + event.event.release = '999'; |
| 79 | + }); |
| 80 | + |
| 81 | + await client.captureEvent(event, scope: scope); |
| 82 | + |
| 83 | + final capturedEnvelope = (fixture.transport).envelopes.first; |
| 84 | + final capturedEvent = await eventFromEnvelope(capturedEnvelope); |
| 85 | + |
| 86 | + expect(capturedEvent.release, '999'); |
| 87 | + }); |
| 88 | + }); |
| 89 | + }); |
| 90 | +} |
| 91 | + |
| 92 | +class Fixture { |
| 93 | + final recorder = MockClientReportRecorder(); |
| 94 | + final transport = MockTransport(); |
| 95 | + |
| 96 | + final options = defaultTestOptions() |
| 97 | + ..platform = MockPlatform.iOS() |
| 98 | + ..groupExceptions = true; |
| 99 | + |
| 100 | + late SentryTransactionContext _context; |
| 101 | + late SentryTracer tracer; |
| 102 | + |
| 103 | + SentryLevel? loggedLevel; |
| 104 | + Object? loggedException; |
| 105 | + |
| 106 | + SentryClient getSut({ |
| 107 | + bool sendDefaultPii = false, |
| 108 | + bool attachStacktrace = true, |
| 109 | + bool attachThreads = false, |
| 110 | + double? sampleRate, |
| 111 | + BeforeSendCallback? beforeSend, |
| 112 | + BeforeSendTransactionCallback? beforeSendTransaction, |
| 113 | + BeforeSendCallback? beforeSendFeedback, |
| 114 | + EventProcessor? eventProcessor, |
| 115 | + bool provideMockRecorder = true, |
| 116 | + bool debug = false, |
| 117 | + Transport? transport, |
| 118 | + }) { |
| 119 | + options.tracesSampleRate = 1.0; |
| 120 | + options.sendDefaultPii = sendDefaultPii; |
| 121 | + options.attachStacktrace = attachStacktrace; |
| 122 | + options.attachThreads = attachThreads; |
| 123 | + options.sampleRate = sampleRate; |
| 124 | + options.beforeSend = beforeSend; |
| 125 | + options.beforeSendTransaction = beforeSendTransaction; |
| 126 | + options.beforeSendFeedback = beforeSendFeedback; |
| 127 | + options.debug = debug; |
| 128 | + options.log = mockLogger; |
| 129 | + |
| 130 | + if (eventProcessor != null) { |
| 131 | + options.addEventProcessor(eventProcessor); |
| 132 | + } |
| 133 | + |
| 134 | + // Internally also creates a SentryClient instance |
| 135 | + final hub = Hub(options); |
| 136 | + _context = SentryTransactionContext( |
| 137 | + 'name', |
| 138 | + 'op', |
| 139 | + ); |
| 140 | + tracer = SentryTracer(_context, hub); |
| 141 | + |
| 142 | + // Reset transport |
| 143 | + options.transport = transport ?? this.transport; |
| 144 | + |
| 145 | + // Again create SentryClient instance |
| 146 | + final client = SentryClient(options); |
| 147 | + |
| 148 | + if (provideMockRecorder) { |
| 149 | + options.recorder = recorder; |
| 150 | + } |
| 151 | + return client; |
| 152 | + } |
| 153 | + |
| 154 | + Future<SentryEvent?> droppingBeforeSend(SentryEvent event, Hint hint) async { |
| 155 | + return null; |
| 156 | + } |
| 157 | + |
| 158 | + SentryTransaction fakeTransaction() { |
| 159 | + return SentryTransaction( |
| 160 | + tracer, |
| 161 | + sdk: SdkVersion(name: 'sdk1', version: '1.0.0'), |
| 162 | + breadcrumbs: [], |
| 163 | + ); |
| 164 | + } |
| 165 | + |
| 166 | + SentryEvent fakeFeedbackEvent() { |
| 167 | + return SentryEvent( |
| 168 | + type: 'feedback', |
| 169 | + contexts: Contexts(feedback: fakeFeedback()), |
| 170 | + level: SentryLevel.info, |
| 171 | + ); |
| 172 | + } |
| 173 | + |
| 174 | + SentryFeedback fakeFeedback() { |
| 175 | + return SentryFeedback( |
| 176 | + message: 'fixture-message', |
| 177 | + contactEmail: 'fixture-contactEmail', |
| 178 | + name: 'fixture-name', |
| 179 | + replayId: 'fixture-replayId', |
| 180 | + url: "https://fixture-url.com", |
| 181 | + associatedEventId: SentryId.fromId('1d49af08b6e2c437f9052b1ecfd83dca'), |
| 182 | + ); |
| 183 | + } |
| 184 | + |
| 185 | + void mockLogger( |
| 186 | + SentryLevel level, |
| 187 | + String message, { |
| 188 | + String? logger, |
| 189 | + Object? exception, |
| 190 | + StackTrace? stackTrace, |
| 191 | + }) { |
| 192 | + loggedLevel = level; |
| 193 | + loggedException = exception; |
| 194 | + } |
| 195 | +} |
0 commit comments