Skip to content

Commit

Permalink
👌 add RUM/Logs session missing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bcaudan committed Dec 1, 2021
1 parent 7677079 commit 73a04fc
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
20 changes: 19 additions & 1 deletion packages/logs/src/domain/loggerSession.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
SESSION_COOKIE_NAME,
setCookie,
stopSessionManagement,
ONE_SECOND,
RelativeTime,
} from '@datadog/browser-core'
import { Clock, mockClock } from '../../../core/test/specHelper'

Expand All @@ -13,10 +15,11 @@ import { LOGGER_SESSION_KEY, LoggerTrackingType, startLoggerSession } from './lo
describe('logger session', () => {
const DURATION = 123456
const configuration: Partial<Configuration> = { sampleRate: 0.5 }
let tracked = true
let clock: Clock
let tracked: boolean

beforeEach(() => {
tracked = true
spyOn(Math, 'random').and.callFake(() => (tracked ? 0 : 1))
clock = mockClock()
})
Expand Down Expand Up @@ -84,4 +87,19 @@ describe('logger session', () => {
expect(session.getId()).toBeUndefined()
expect(session.isTracked()).toMatch(/true|false/)
})

it('should get session from history', () => {
const session = startLoggerSession(configuration as Configuration, true)

clock.tick(10 * ONE_SECOND)

setCookie(SESSION_COOKIE_NAME, '', DURATION)
clock.tick(COOKIE_ACCESS_DELAY)

expect(session.getId()).toBeUndefined()
expect(session.isTracked()).toBe(false)

expect(session.getId(ONE_SECOND as RelativeTime)).toBeDefined()
expect(session.isTracked(ONE_SECOND as RelativeTime)).toBe(true)
})
})
2 changes: 1 addition & 1 deletion packages/logs/src/domain/loggerSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function startLoggerSession(configuration: Configuration, areCookieAuthor
)
return {
getId: session.getId,
isTracked: () => session.getTrackingType() === LoggerTrackingType.TRACKED,
isTracked: (startTime) => session.getTrackingType(startTime) === LoggerTrackingType.TRACKED,
}
}

Expand Down
44 changes: 44 additions & 0 deletions packages/rum-core/src/domain/rumSession.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
SESSION_COOKIE_NAME,
setCookie,
stopSessionManagement,
ONE_SECOND,
RelativeTime,
} from '@datadog/browser-core'
import { Clock, mockClock } from '../../../core/test/specHelper'

Expand Down Expand Up @@ -137,6 +139,28 @@ describe('rum session', () => {
clock.tick(COOKIE_ACCESS_DELAY)
expect(rumSession.getId()).toBe(undefined)
})

it('should return session id corresponding to start time', () => {
setCookie(SESSION_COOKIE_NAME, 'id=abcdef&rum=1', DURATION)
const rumSession = startRumSession(configuration as Configuration, lifeCycle)
clock.tick(10 * ONE_SECOND)
setCookie(SESSION_COOKIE_NAME, '', DURATION)
clock.tick(COOKIE_ACCESS_DELAY)
expect(rumSession.getId()).toBeUndefined()
expect(rumSession.getId(0 as RelativeTime)).toBe('abcdef')
})
})

describe('isTracked', () => {
it('should return value corresponding to start time', () => {
setCookie(SESSION_COOKIE_NAME, 'id=abcdef&rum=1', DURATION)
const rumSession = startRumSession(configuration as Configuration, lifeCycle)
clock.tick(10 * ONE_SECOND)
setCookie(SESSION_COOKIE_NAME, '', DURATION)
clock.tick(COOKIE_ACCESS_DELAY)
expect(rumSession.isTracked()).toBeFalse()
expect(rumSession.isTracked(ONE_SECOND as RelativeTime)).toBeTrue()
})
})

describe('hasReplayPlan', () => {
Expand All @@ -158,6 +182,16 @@ describe('rum session', () => {
const rumSession = startRumSession(configuration as Configuration, lifeCycle)
expect(rumSession.hasReplayPlan()).toBeFalse()
})

it('should return plan corresponding to start time', () => {
setCookie(SESSION_COOKIE_NAME, 'id=abcdef&rum=1', DURATION)
const rumSession = startRumSession(configuration as Configuration, lifeCycle)
clock.tick(10 * ONE_SECOND)
setCookie(SESSION_COOKIE_NAME, '', DURATION)
clock.tick(COOKIE_ACCESS_DELAY)
expect(rumSession.hasReplayPlan()).toBeFalse()
expect(rumSession.hasReplayPlan(ONE_SECOND as RelativeTime)).toBeTrue()
})
})

describe('hasLitePlan', () => {
Expand All @@ -179,5 +213,15 @@ describe('rum session', () => {
const rumSession = startRumSession(configuration as Configuration, lifeCycle)
expect(rumSession.hasLitePlan()).toBeFalse()
})

it('should return plan corresponding to start time', () => {
setCookie(SESSION_COOKIE_NAME, 'id=abcdef&rum=2', DURATION)
const rumSession = startRumSession(configuration as Configuration, lifeCycle)
clock.tick(10 * ONE_SECOND)
setCookie(SESSION_COOKIE_NAME, '', DURATION)
clock.tick(COOKIE_ACCESS_DELAY)
expect(rumSession.hasLitePlan()).toBeFalse()
expect(rumSession.hasLitePlan(ONE_SECOND as RelativeTime)).toBeTrue()
})
})
})

0 comments on commit 73a04fc

Please sign in to comment.