@@ -27,6 +27,7 @@ describe('LogEnricher Integration', () => {
27
27
let mockClient : jest . Mocked < Client > ;
28
28
let mockOn : jest . Mock ;
29
29
let mockFetchNativeLogAttributes : jest . Mock ;
30
+ let mockGetIntegrationByName : jest . Mock ;
30
31
31
32
const triggerAfterInit = ( ) => {
32
33
const afterInitCallback = mockOn . mock . calls . find ( call => call [ 0 ] === 'afterInit' ) ?. [ 1 ] as ( ( ) => void ) | undefined ;
@@ -40,9 +41,11 @@ describe('LogEnricher Integration', () => {
40
41
41
42
mockOn = jest . fn ( ) ;
42
43
mockFetchNativeLogAttributes = jest . fn ( ) ;
44
+ mockGetIntegrationByName = jest . fn ( ) ;
43
45
44
46
mockClient = {
45
47
on : mockOn ,
48
+ getIntegrationByName : mockGetIntegrationByName ,
46
49
} as unknown as jest . Mocked < Client > ;
47
50
48
51
( NATIVE as jest . Mocked < typeof NATIVE > ) . fetchNativeLogAttributes = mockFetchNativeLogAttributes ;
@@ -404,4 +407,113 @@ describe('LogEnricher Integration', () => {
404
407
expect ( mockOn ) . toHaveBeenCalledWith ( 'beforeCaptureLog' , expect . any ( Function ) ) ;
405
408
} ) ;
406
409
} ) ;
410
+
411
+ describe ( 'replay log functionality' , ( ) => {
412
+ let logHandler : ( log : Log ) => void ;
413
+ let mockLog : Log ;
414
+ let mockGetIntegrationByName : jest . Mock ;
415
+
416
+ beforeEach ( async ( ) => {
417
+ const integration = logEnricherIntegration ( ) ;
418
+
419
+ const mockNativeResponse : NativeDeviceContextsResponse = {
420
+ contexts : {
421
+ device : {
422
+ brand : 'Apple' ,
423
+ model : 'iPhone 14' ,
424
+ family : 'iPhone' ,
425
+ } as Record < string , unknown > ,
426
+ os : {
427
+ name : 'iOS' ,
428
+ version : '16.0' ,
429
+ } as Record < string , unknown > ,
430
+ release : '1.0.0' as unknown as Record < string , unknown > ,
431
+ } ,
432
+ } ;
433
+
434
+ mockFetchNativeLogAttributes . mockResolvedValue ( mockNativeResponse ) ;
435
+ mockGetIntegrationByName = jest . fn ( ) ;
436
+
437
+ mockClient = {
438
+ on : mockOn ,
439
+ getIntegrationByName : mockGetIntegrationByName ,
440
+ } as unknown as jest . Mocked < Client > ;
441
+
442
+ integration . setup ( mockClient ) ;
443
+
444
+ triggerAfterInit ( ) ;
445
+
446
+ await jest . runAllTimersAsync ( ) ;
447
+
448
+ const beforeCaptureLogCall = mockOn . mock . calls . find ( call => call [ 0 ] === 'beforeCaptureLog' ) ;
449
+ expect ( beforeCaptureLogCall ) . toBeDefined ( ) ;
450
+ logHandler = beforeCaptureLogCall ! [ 1 ] as ( log : Log ) => void ;
451
+
452
+ mockLog = {
453
+ message : 'Test log message' ,
454
+ level : 'info' ,
455
+ attributes : { } ,
456
+ } ;
457
+ } ) ;
458
+
459
+ it ( 'should add replay_id when MobileReplay integration is available and returns a replay ID' , ( ) => {
460
+ const mockReplayId = 'replay-123-abc' ;
461
+ const mockReplayIntegration = {
462
+ getReplayId : jest . fn ( ) . mockReturnValue ( mockReplayId ) ,
463
+ } ;
464
+
465
+ mockGetIntegrationByName . mockReturnValue ( mockReplayIntegration ) ;
466
+
467
+ logHandler ( mockLog ) ;
468
+
469
+ expect ( mockLog . attributes ) . toEqual ( {
470
+ 'device.brand' : 'Apple' ,
471
+ 'device.model' : 'iPhone 14' ,
472
+ 'device.family' : 'iPhone' ,
473
+ 'os.name' : 'iOS' ,
474
+ 'os.version' : '16.0' ,
475
+ 'sentry.release' : '1.0.0' ,
476
+ 'sentry.replay_id' : mockReplayId ,
477
+ } ) ;
478
+ expect ( mockGetIntegrationByName ) . toHaveBeenCalledWith ( 'MobileReplay' ) ;
479
+ expect ( mockReplayIntegration . getReplayId ) . toHaveBeenCalled ( ) ;
480
+ } ) ;
481
+
482
+ it ( 'should not add replay_id when MobileReplay integration returns null' , ( ) => {
483
+ const mockReplayIntegration = {
484
+ getReplayId : jest . fn ( ) . mockReturnValue ( null ) ,
485
+ } ;
486
+
487
+ mockGetIntegrationByName . mockReturnValue ( mockReplayIntegration ) ;
488
+
489
+ logHandler ( mockLog ) ;
490
+
491
+ expect ( mockLog . attributes ) . toEqual ( {
492
+ 'device.brand' : 'Apple' ,
493
+ 'device.model' : 'iPhone 14' ,
494
+ 'device.family' : 'iPhone' ,
495
+ 'os.name' : 'iOS' ,
496
+ 'os.version' : '16.0' ,
497
+ 'sentry.release' : '1.0.0' ,
498
+ } ) ;
499
+ expect ( mockGetIntegrationByName ) . toHaveBeenCalledWith ( 'MobileReplay' ) ;
500
+ expect ( mockReplayIntegration . getReplayId ) . toHaveBeenCalled ( ) ;
501
+ } ) ;
502
+
503
+ it ( 'should not add replay_id when MobileReplay integration is not available' , ( ) => {
504
+ mockGetIntegrationByName . mockReturnValue ( undefined ) ;
505
+
506
+ logHandler ( mockLog ) ;
507
+
508
+ expect ( mockLog . attributes ) . toEqual ( {
509
+ 'device.brand' : 'Apple' ,
510
+ 'device.model' : 'iPhone 14' ,
511
+ 'device.family' : 'iPhone' ,
512
+ 'os.name' : 'iOS' ,
513
+ 'os.version' : '16.0' ,
514
+ 'sentry.release' : '1.0.0' ,
515
+ } ) ;
516
+ expect ( mockGetIntegrationByName ) . toHaveBeenCalledWith ( 'MobileReplay' ) ;
517
+ } ) ;
518
+ } ) ;
407
519
} ) ;
0 commit comments