Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldambra committed Oct 19, 2023
1 parent c362083 commit 963a7a3
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 21 deletions.
82 changes: 62 additions & 20 deletions src/__tests__/sessionid.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('Session ID manager', () => {
sessionStartTimestamp: given.timestamp,
})
expect(given.persistence.register).toHaveBeenCalledWith({
[SESSION_ID]: [given.timestamp, 'newUUID', given.timestamp],
[SESSION_ID]: [given.timestamp, 'newUUID', given.timestamp, null],
})
expect(sessionStore.set).toHaveBeenCalledWith('ph_persistance-name_window_id', 'newUUID')
})
Expand All @@ -53,7 +53,7 @@ describe('Session ID manager', () => {
sessionStartTimestamp: given.timestamp,
})
expect(given.persistence.register).toHaveBeenCalledWith({
[SESSION_ID]: [given.timestamp, 'newUUID', given.timestamp],
[SESSION_ID]: [given.timestamp, 'newUUID', given.timestamp, null],
})
expect(sessionStore.set).toHaveBeenCalledWith('ph_persistance-name_window_id', 'newUUID')
})
Expand All @@ -62,7 +62,7 @@ describe('Session ID manager', () => {
describe('stored session data', () => {
given('timestampOfSessionStart', () => given.now - 3600)

given('storedSessionIdData', () => [given.now, 'oldSessionID', given.timestampOfSessionStart])
given('storedSessionIdData', () => [given.now, 'oldSessionID', given.timestampOfSessionStart, null])
beforeEach(() => {
sessionStore.parse.mockReturnValue('oldWindowID')
})
Expand All @@ -72,9 +72,10 @@ describe('Session ID manager', () => {
windowId: 'oldWindowID',
sessionId: 'oldSessionID',
sessionStartTimestamp: given.timestampOfSessionStart,
isSampled: null,
})
expect(given.persistence.register).toHaveBeenCalledWith({
[SESSION_ID]: [given.timestamp, 'oldSessionID', given.timestampOfSessionStart],
[SESSION_ID]: [given.timestamp, 'oldSessionID', given.timestampOfSessionStart, null],
})
})

Expand All @@ -83,16 +84,17 @@ describe('Session ID manager', () => {
const oldTimestamp = given.now - thirtyMinutesAndOneSecond
const sessionStart = oldTimestamp - 1000

given('storedSessionIdData', () => [oldTimestamp, 'oldSessionID', sessionStart])
given('storedSessionIdData', () => [oldTimestamp, 'oldSessionID', sessionStart, null])
given('readOnly', () => true)

expect(given.subject).toEqual({
windowId: 'oldWindowID',
sessionId: 'oldSessionID',
sessionStartTimestamp: sessionStart,
isSampled: null,
})
expect(given.persistence.register).toHaveBeenCalledWith({
[SESSION_ID]: [oldTimestamp, 'oldSessionID', sessionStart],
[SESSION_ID]: [oldTimestamp, 'oldSessionID', sessionStart, null],
})
})

Expand All @@ -102,77 +104,82 @@ describe('Session ID manager', () => {
windowId: 'newUUID',
sessionId: 'oldSessionID',
sessionStartTimestamp: given.timestampOfSessionStart,
isSampled: null,
})
expect(given.persistence.register).toHaveBeenCalledWith({
[SESSION_ID]: [given.timestamp, 'oldSessionID', given.timestampOfSessionStart],
[SESSION_ID]: [given.timestamp, 'oldSessionID', given.timestampOfSessionStart, null],
})
expect(sessionStore.set).toHaveBeenCalledWith('ph_persistance-name_window_id', 'newUUID')
})

it('generates a new session id and window id, and saves it when >30m since last event', () => {
const oldTimestamp = 1602107460000
given('storedSessionIdData', () => [oldTimestamp, 'oldSessionID', given.timestampOfSessionStart])
given('storedSessionIdData', () => [oldTimestamp, 'oldSessionID', given.timestampOfSessionStart, null])

expect(given.subject).toEqual({
windowId: 'newUUID',
sessionId: 'newUUID',
sessionStartTimestamp: given.timestamp,
isSampled: null,
})
expect(given.persistence.register).toHaveBeenCalledWith({
[SESSION_ID]: [given.timestamp, 'newUUID', given.timestamp],
[SESSION_ID]: [given.timestamp, 'newUUID', given.timestamp, null],
})
expect(sessionStore.set).toHaveBeenCalledWith('ph_persistance-name_window_id', 'newUUID')
})

it('generates a new session id and window id, and saves it when >24 hours since start timestamp', () => {
const oldTimestamp = 1602107460000
const twentyFourHours = 3600 * 24
given('storedSessionIdData', () => [oldTimestamp, 'oldSessionID', given.timestampOfSessionStart])
given('storedSessionIdData', () => [oldTimestamp, 'oldSessionID', given.timestampOfSessionStart, null])
given('timestamp', () => given.timestampOfSessionStart + twentyFourHours)

expect(given.subject).toEqual({
windowId: 'newUUID',
sessionId: 'newUUID',
sessionStartTimestamp: given.timestamp,
isSampled: null,
})

expect(given.persistence.register).toHaveBeenCalledWith({
[SESSION_ID]: [given.timestamp, 'newUUID', given.timestamp],
[SESSION_ID]: [given.timestamp, 'newUUID', given.timestamp, null],
})
expect(sessionStore.set).toHaveBeenCalledWith('ph_persistance-name_window_id', 'newUUID')
})

it('generates a new session id and window id, and saves it when >24 hours since start timestamp even when readonly is true', () => {
const oldTimestamp = 1602107460000
const twentyFourHoursAndOneSecond = (3600 * 24 + 1) * 1000
given('storedSessionIdData', () => [oldTimestamp, 'oldSessionID', given.timestampOfSessionStart])
given('storedSessionIdData', () => [oldTimestamp, 'oldSessionID', given.timestampOfSessionStart, null])
given('timestamp', () => given.timestampOfSessionStart + twentyFourHoursAndOneSecond)
given('readOnly', () => true)

expect(given.subject).toEqual({
windowId: 'newUUID',
sessionId: 'newUUID',
sessionStartTimestamp: given.timestamp,
isSampled: null,
})

expect(given.persistence.register).toHaveBeenCalledWith({
[SESSION_ID]: [given.timestamp, 'newUUID', given.timestamp],
[SESSION_ID]: [given.timestamp, 'newUUID', given.timestamp, null],
})
expect(sessionStore.set).toHaveBeenCalledWith('ph_persistance-name_window_id', 'newUUID')
})

it('uses the current time if no timestamp is provided', () => {
const now = new Date().getTime()
const oldTimestamp = 1601107460000
given('storedSessionIdData', () => [oldTimestamp, 'oldSessionID', given.timestampOfSessionStart])
given('storedSessionIdData', () => [oldTimestamp, 'oldSessionID', given.timestampOfSessionStart, null])
given('timestamp', () => undefined)
expect(given.subject).toEqual({
windowId: 'newUUID',
sessionId: 'newUUID',
sessionStartTimestamp: now,
isSampled: null,
})
expect(given.persistence.register).toHaveBeenCalledWith({
[SESSION_ID]: [given.now, 'newUUID', given.now],
[SESSION_ID]: [given.now, 'newUUID', given.now, null],
})
})

Expand All @@ -182,9 +189,10 @@ describe('Session ID manager', () => {
windowId: 'oldWindowID',
sessionId: 'oldSessionID',
sessionStartTimestamp: given.timestamp,
isSampled: null,
})
expect(given.persistence.register).toHaveBeenCalledWith({
[SESSION_ID]: [given.timestamp, 'oldSessionID', given.timestamp],
[SESSION_ID]: [given.timestamp, 'oldSessionID', given.timestamp, null],
})
})
})
Expand All @@ -211,18 +219,18 @@ describe('Session ID manager', () => {

describe('session id storage', () => {
it('stores and retrieves a session id and timestamp', () => {
given.sessionIdManager._setSessionId('newSessionId', 1603107460000, 1603107460000)
given.sessionIdManager._setSessionId('newSessionId', 1603107460000, 1603107460000, null)
expect(given.persistence.register).toHaveBeenCalledWith({
[SESSION_ID]: [1603107460000, 'newSessionId', 1603107460000],
[SESSION_ID]: [1603107460000, 'newSessionId', 1603107460000, null],
})
expect(given.sessionIdManager._getSessionId()).toEqual([1603107460000, 'newSessionId', 1603107460000])
expect(given.sessionIdManager._getSessionId()).toEqual([1603107460000, 'newSessionId', 1603107460000, null])
})
})

describe('reset session id', () => {
it('clears the existing session id', () => {
given.sessionIdManager.resetSessionId()
expect(given.persistence.register).toHaveBeenCalledWith({ [SESSION_ID]: [null, null, null] })
expect(given.persistence.register).toHaveBeenCalledWith({ [SESSION_ID]: [null, null, null, null] })
})
it('a new session id is generated when called', () => {
given('storedSessionIdData', () => [null, null, null])
Expand Down Expand Up @@ -285,4 +293,38 @@ describe('Session ID manager', () => {
expect(console.warn).toBeCalledTimes(3)
})
})

describe('sampling', () => {
it('respects sampled session', () => {
given('storedSessionIdData', () => [given.now, 'oldSessionID', given.timestampOfSessionStart, true])
expect(given.subject).toEqual({
windowId: 'oldWindowId',
sessionId: 'oldSessionID',
sessionStartTimestamp: given.timestampOfSessionStart,
isSampled: true,
})
})

it('respects excluded sampled session', () => {
given('storedSessionIdData', () => [given.now, 'oldSessionID', given.timestampOfSessionStart, false])
expect(given.subject).toEqual({
windowId: 'oldWindowId',
sessionId: 'oldSessionID',
sessionStartTimestamp: given.timestampOfSessionStart,
isSampled: false,
})
})

it('uses provided function to check for sampling', () => {
given.sessionIdManager.checkSampling = () => true
given('storedSessionIdData', () => [null, null, null])

expect(given.subject).toEqual({
windowId: 'newUUID',
sessionId: 'newUUID',
sessionStartTimestamp: given.timestamp,
isSampled: true,
})
})
})
})
10 changes: 9 additions & 1 deletion src/sessionid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,12 @@ export class SessionIdManager {
}

private _getSessionId(): [number, string, number, boolean | null] {
if (this._sessionId && this._sessionActivityTimestamp && this._sessionStartTimestamp && this._isSampled) {
if (
this._sessionId &&
this._sessionActivityTimestamp &&
this._sessionStartTimestamp &&
this._isSampled !== undefined
) {
return [this._sessionActivityTimestamp, this._sessionId, this._sessionStartTimestamp, this._isSampled]
}
const sessionId = this.persistence.props[SESSION_ID]
Expand Down Expand Up @@ -220,6 +225,9 @@ export class SessionIdManager {
const sessionPastMaximumLength =
startTimestamp && startTimestamp > 0 && Math.abs(timestamp - startTimestamp) > SESSION_LENGTH_LIMIT

// isSampled isn't necessarily in storage and if not will be undefined, which we don't want
isSampled = isSampled === undefined ? null : isSampled

let valuesChanged = false
if (
!sessionId ||
Expand Down

0 comments on commit 963a7a3

Please sign in to comment.