Skip to content

Commit

Permalink
type safety for decide response in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldambra committed Oct 20, 2023
1 parent 5cd18b4 commit e3432ba
Showing 1 changed file with 53 additions and 33 deletions.
86 changes: 53 additions & 33 deletions src/__tests__/extensions/sessionrecording.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ const createIncrementalSnapshot = (event = {}) => ({
...event,
})

function makeDecideResponse(partialResponse: Partial<DecideResponse>) {
return partialResponse as unknown as DecideResponse
}

describe('SessionRecording', () => {
let _emit: any
let posthog: PostHog
Expand Down Expand Up @@ -236,7 +240,7 @@ describe('SessionRecording', () => {
_emit(createIncrementalSnapshot({ data: { source: 1 } }))
expect(sessionRecording.snapshots.length).toEqual(1)

sessionRecording.afterDecideResponse({ sessionRecording: false } as unknown as DecideResponse)
sessionRecording.afterDecideResponse(makeDecideResponse({ sessionRecording: undefined }))
expect(sessionRecording.emit).toBe('disabled')
expect(sessionRecording.snapshots.length).toEqual(0)
expect(posthog.capture).not.toHaveBeenCalled()
Expand All @@ -247,7 +251,7 @@ describe('SessionRecording', () => {
expect(loadScript).toHaveBeenCalled()
expect(sessionRecording.emit).toBe('buffering')

sessionRecording.afterDecideResponse({ sessionRecording: { endpoint: '/s/' } } as unknown as DecideResponse)
sessionRecording.afterDecideResponse(makeDecideResponse({ sessionRecording: { endpoint: '/s/' } }))
expect(sessionRecording.emit).toBe('active')
})

Expand All @@ -256,41 +260,45 @@ describe('SessionRecording', () => {
expect(loadScript).toHaveBeenCalled()
expect(sessionRecording.getIsSampled()).toBe(undefined)

sessionRecording.afterDecideResponse({ sessionRecording: { endpoint: '/s/' } } as unknown as DecideResponse)
sessionRecording.afterDecideResponse(makeDecideResponse({ sessionRecording: { endpoint: '/s/' } }))
expect(sessionRecording.getIsSampled()).toBe(undefined)
})

it('stores true in persistence if recording is enabled from the server', () => {
posthog.persistence?.register({ [SESSION_RECORDING_ENABLED_SERVER_SIDE]: undefined })

sessionRecording.afterDecideResponse({ sessionRecording: { endpoint: '/s/' } } as unknown as DecideResponse)
sessionRecording.afterDecideResponse(makeDecideResponse({ sessionRecording: { endpoint: '/s/' } }))

expect(posthog.get_property(SESSION_RECORDING_ENABLED_SERVER_SIDE)).toBe(true)
})

it('stores false in persistence if recording is not enabled from the server', () => {
posthog.persistence?.register({ [SESSION_RECORDING_ENABLED_SERVER_SIDE]: undefined })

sessionRecording.afterDecideResponse({} as unknown as DecideResponse)
sessionRecording.afterDecideResponse(makeDecideResponse({}))

expect(posthog.get_property(SESSION_RECORDING_ENABLED_SERVER_SIDE)).toBe(false)
})

it('stores sample rate in persistence', () => {
posthog.persistence?.register({ SESSION_RECORDING_SAMPLE_RATE: undefined })

sessionRecording.afterDecideResponse({
sessionRecording: { endpoint: '/s/', sampleRate: '0.70' },
} as unknown as DecideResponse)
sessionRecording.afterDecideResponse(
makeDecideResponse({
sessionRecording: { endpoint: '/s/', sampleRate: '0.70' },
})
)

expect(posthog.get_property(SESSION_RECORDING_SAMPLE_RATE)).toBe(0.7)
})

it('starts session recording, saves setting and endpoint when enabled', () => {
posthog.persistence?.register({ [SESSION_RECORDING_ENABLED_SERVER_SIDE]: undefined })
sessionRecording.afterDecideResponse({
sessionRecording: { endpoint: '/ses/' },
} as unknown as DecideResponse)
sessionRecording.afterDecideResponse(
makeDecideResponse({
sessionRecording: { endpoint: '/ses/' },
})
)

expect(sessionRecording.startRecordingIfEnabled).toHaveBeenCalled()
expect(loadScript).toHaveBeenCalled()
Expand All @@ -314,9 +322,11 @@ describe('SessionRecording', () => {
it('does not emit to capture if the sample rate is 0', () => {
sessionRecording.startRecordingIfEnabled()

sessionRecording.afterDecideResponse({
sessionRecording: { endpoint: '/s/', sampleRate: '0.00' },
} as unknown as DecideResponse)
sessionRecording.afterDecideResponse(
makeDecideResponse({
sessionRecording: { endpoint: '/s/', sampleRate: '0.00' },
})
)
expect(sessionRecording.emit).toBe('disabled')

_emit(createIncrementalSnapshot({ data: { source: 1 } }))
Expand All @@ -327,9 +337,11 @@ describe('SessionRecording', () => {
it('stores excluded session when excluded', () => {
sessionRecording.startRecordingIfEnabled()

sessionRecording.afterDecideResponse({
sessionRecording: { endpoint: '/s/', sampleRate: '0.00' },
} as unknown as DecideResponse)
sessionRecording.afterDecideResponse(
makeDecideResponse({
sessionRecording: { endpoint: '/s/', sampleRate: '0.00' },
})
)

expect(sessionRecording.getIsSampled()).toStrictEqual(false)
})
Expand All @@ -340,9 +352,11 @@ describe('SessionRecording', () => {
_emit(createIncrementalSnapshot({ data: { source: 1 } }))
expect(posthog.capture).not.toHaveBeenCalled()

sessionRecording.afterDecideResponse({
sessionRecording: { endpoint: '/s/', sampleRate: '1.00' },
} as unknown as DecideResponse)
sessionRecording.afterDecideResponse(
makeDecideResponse({
sessionRecording: { endpoint: '/s/', sampleRate: '1.00' },
})
)
_emit(createIncrementalSnapshot({ data: { source: 1 } }))

expect(sessionRecording.emit).toBe('sampled')
Expand All @@ -358,9 +372,11 @@ describe('SessionRecording', () => {
it('sets emit as expected when sample rate is 0.5', () => {
sessionRecording.startRecordingIfEnabled()

sessionRecording.afterDecideResponse({
sessionRecording: { endpoint: '/s/', sampleRate: '0.50' },
} as unknown as DecideResponse)
sessionRecording.afterDecideResponse(
makeDecideResponse({
sessionRecording: { endpoint: '/s/', sampleRate: '0.50' },
})
)
const emitValues = []
let lastSessionId = sessionRecording['sessionId']

Expand Down Expand Up @@ -414,7 +430,7 @@ describe('SessionRecording', () => {
_emit(createIncrementalSnapshot({ data: { source: 1 } }))
expect(posthog.capture).not.toHaveBeenCalled()

sessionRecording.afterDecideResponse({ sessionRecording: { endpoint: '/s/' } } as unknown as DecideResponse)
sessionRecording.afterDecideResponse(makeDecideResponse({ sessionRecording: { endpoint: '/s/' } }))
_emit(createIncrementalSnapshot({ data: { source: 2 } }))

// access private method 🤯
Expand Down Expand Up @@ -446,7 +462,7 @@ describe('SessionRecording', () => {
})

it('buffers emitted events', () => {
sessionRecording.afterDecideResponse({ sessionRecording: { endpoint: '/s/' } } as unknown as DecideResponse)
sessionRecording.afterDecideResponse(makeDecideResponse({ sessionRecording: { endpoint: '/s/' } }))
sessionRecording.startRecordingIfEnabled()
expect(loadScript).toHaveBeenCalled()

Expand Down Expand Up @@ -485,7 +501,7 @@ describe('SessionRecording', () => {
})

it('flushes buffer if the size of the buffer hits the limit', () => {
sessionRecording.afterDecideResponse({ sessionRecording: { endpoint: '/s/' } } as unknown as DecideResponse)
sessionRecording.afterDecideResponse(makeDecideResponse({ sessionRecording: { endpoint: '/s/' } }))
sessionRecording.startRecordingIfEnabled()
expect(loadScript).toHaveBeenCalled()
const bigData = 'a'.repeat(RECORDING_MAX_EVENT_SIZE * 0.8)
Expand All @@ -505,7 +521,7 @@ describe('SessionRecording', () => {
})

it('flushes buffer if the session_id changes', () => {
sessionRecording.afterDecideResponse({ sessionRecording: { endpoint: '/s/' } } as unknown as DecideResponse)
sessionRecording.afterDecideResponse(makeDecideResponse({ sessionRecording: { endpoint: '/s/' } }))
sessionRecording.startRecordingIfEnabled()

_emit(createIncrementalSnapshot())
Expand Down Expand Up @@ -643,9 +659,11 @@ describe('SessionRecording', () => {
sessionRecording['windowId'] = 'old-window-id'

sessionRecording.startRecordingIfEnabled()
sessionRecording.afterDecideResponse({
sessionRecording: { endpoint: '/s/' },
} as unknown as DecideResponse)
sessionRecording.afterDecideResponse(
makeDecideResponse({
sessionRecording: { endpoint: '/s/' },
})
)
sessionRecording['startCaptureAndTrySendingQueuedSnapshots']()
})

Expand Down Expand Up @@ -991,9 +1009,11 @@ describe('SessionRecording', () => {
})

it('can set minimum duration from decide response', () => {
sessionRecording.afterDecideResponse({
sessionRecording: { minimumDurationMilliseconds: 1500 },
} as unknown as DecideResponse)
sessionRecording.afterDecideResponse(
makeDecideResponse({
sessionRecording: { minimumDurationMilliseconds: 1500 },
})
)
expect(sessionRecording.getMinimumDuration()).toBe(1500)
})
})
Expand Down

0 comments on commit e3432ba

Please sign in to comment.