1919import { diag } from '@opentelemetry/api'
2020import { afterEach , beforeEach , describe , expect , it , vi } from 'vitest'
2121
22+ import type { ExternalSessionMetadata } from '../../types/external-session-metadata'
23+
2224import { StorageManager } from '../storage'
2325import { SESSION_ID_LENGTH , SESSION_INACTIVITY_TIMEOUT_MS } from './constants'
2426import { SessionManager } from './session-manager'
@@ -27,10 +29,24 @@ vi.mock('../../utils/is-trusted-event', () => ({
2729 isTrustedEvent : vi . fn ( ( ) => true ) ,
2830} ) )
2931
32+ function createExternalSessionMetadata (
33+ overrides : Partial < NonNullable < ExternalSessionMetadata > > = { } ,
34+ ) : NonNullable < ExternalSessionMetadata > {
35+ const now = Date . now ( )
36+
37+ return {
38+ anonymousUserId : 'external-anonymous-id' ,
39+ sessionId : 'external-session-id' ,
40+ sessionLastActivity : now ,
41+ sessionStart : now - 1000 ,
42+ ...overrides ,
43+ }
44+ }
45+
3046declare global {
3147 interface Window {
32- SplunkRumNative ?: {
33- getNativeSessionId ( ) : string
48+ SplunkRumExternal ?: {
49+ getSessionMetadata ( ) : NonNullable < ExternalSessionMetadata >
3450 }
3551 }
3652}
@@ -40,7 +56,7 @@ describe('SessionManager', () => {
4056 let storageManager : StorageManager
4157
4258 beforeEach ( ( ) => {
43- window . SplunkRumNative = undefined
59+ window . SplunkRumExternal = undefined
4460 storageManager = new StorageManager ( {
4561 persistence : 'cookie' ,
4662 } )
@@ -103,9 +119,11 @@ describe('SessionManager', () => {
103119 expect ( sessionManager . getSessionId ( ) ) . toHaveLength ( SESSION_ID_LENGTH )
104120 } )
105121
106- it ( 'should use native session when available' , ( ) => {
107- window . SplunkRumNative = {
108- getNativeSessionId : vi . fn ( ) . mockReturnValue ( 'native-session-id' ) ,
122+ it ( 'should use external session when available' , ( ) => {
123+ window . SplunkRumExternal = {
124+ getSessionMetadata : vi
125+ . fn ( )
126+ . mockReturnValue ( createExternalSessionMetadata ( { sessionId : 'external-session-id' } ) ) ,
109127 }
110128
111129 const persistedSession = {
@@ -122,40 +140,42 @@ describe('SessionManager', () => {
122140
123141 sessionManager = new SessionManager ( storageManager )
124142
125- expect ( sessionManager . getSessionId ( ) ) . toBe ( 'native -session-id' )
143+ expect ( sessionManager . getSessionId ( ) ) . toBe ( 'external -session-id' )
126144 } )
127145 } )
128146
129147 describe ( 'Static Methods' , ( ) => {
130- describe ( 'getNativeSessionId ' , ( ) => {
131- it ( 'should return null when SplunkRumNative is not available' , ( ) => {
132- window . SplunkRumNative = undefined
148+ describe ( 'getExternalSession ' , ( ) => {
149+ it ( 'should return null when SplunkRumExternal is not available' , ( ) => {
150+ window . SplunkRumExternal = undefined
133151
134- expect ( SessionManager . getNativeSessionId ( ) ) . toBeNull ( )
152+ expect ( SessionManager . getExternalSession ( ) ) . toBeNull ( )
135153 } )
136154
137- it ( 'should return native session ID when available' , ( ) => {
138- window . SplunkRumNative = {
139- getNativeSessionId : vi . fn ( ) . mockReturnValue ( 'native-id' ) ,
155+ it ( 'should return external session when available' , ( ) => {
156+ window . SplunkRumExternal = {
157+ getSessionMetadata : vi
158+ . fn ( )
159+ . mockReturnValue ( createExternalSessionMetadata ( { sessionId : 'external-id' } ) ) ,
140160 }
141161
142- expect ( SessionManager . getNativeSessionId ( ) ) . toBe ( 'native -id' )
162+ expect ( SessionManager . getExternalSession ( ) ?. id ) . toBe ( 'external -id' )
143163 } )
144164 } )
145165
146- describe ( 'hasNativeSessionId ' , ( ) => {
147- it ( 'should return false when SplunkRumNative is not available' , ( ) => {
148- window . SplunkRumNative = undefined
166+ describe ( 'hasExternalSession ' , ( ) => {
167+ it ( 'should return false when SplunkRumExternal is not available' , ( ) => {
168+ window . SplunkRumExternal = undefined
149169
150- expect ( SessionManager . hasNativeSessionId ( ) ) . toBe ( false )
170+ expect ( SessionManager . hasExternalSession ( ) ) . toBe ( false )
151171 } )
152172
153- it ( 'should return true when SplunkRumNative is available' , ( ) => {
154- window . SplunkRumNative = {
155- getNativeSessionId : vi . fn ( ) ,
173+ it ( 'should return true when SplunkRumExternal is available' , ( ) => {
174+ window . SplunkRumExternal = {
175+ getSessionMetadata : vi . fn ( ) . mockReturnValue ( createExternalSessionMetadata ( ) ) ,
156176 }
157177
158- expect ( SessionManager . hasNativeSessionId ( ) ) . toBe ( true )
178+ expect ( SessionManager . hasExternalSession ( ) ) . toBe ( true )
159179 } )
160180 } )
161181 } )
@@ -427,13 +447,15 @@ describe('SessionManager', () => {
427447 expect ( sessionManager . getSessionId ( ) ) . not . toBe ( 'persisted-session-id' )
428448 } )
429449
430- it ( 'should not extend session when native session is available' , ( ) => {
450+ it ( 'should not extend session when external session is available' , ( ) => {
431451 const diagDebugSpy = vi . spyOn ( diag , 'debug' )
432452
433453 sessionManager . start ( )
434454
435- window . SplunkRumNative = {
436- getNativeSessionId : vi . fn ( ) . mockReturnValue ( 'native-id' ) ,
455+ window . SplunkRumExternal = {
456+ getSessionMetadata : vi
457+ . fn ( )
458+ . mockReturnValue ( createExternalSessionMetadata ( { sessionId : 'external-id' } ) ) ,
437459 }
438460
439461 const keydownEvent = new KeyboardEvent ( 'keydown' , {
@@ -446,7 +468,7 @@ describe('SessionManager', () => {
446468 window . dispatchEvent ( keydownEvent )
447469
448470 expect ( diagDebugSpy ) . toHaveBeenCalledWith (
449- 'SessionManager: Native session ID detected. Session extension or creation is managed natively .' ,
471+ 'SessionManager: External session ID detected. Session extension or creation is managed externally .' ,
450472 )
451473 } )
452474 } )
0 commit comments