1- import { resetExperimentalFeatures } from '../../../tools/experimentalFeatures'
21import { mockClock , getSessionState } from '../../../../test'
32import { setCookie , deleteCookie , getCookie , getCurrentSite } from '../../../browser/cookie'
43import type { SessionState } from '../sessionState'
54import type { Configuration } from '../../configuration'
6- import { SESSION_TIME_OUT_DELAY } from '../sessionConstants'
5+ import { SESSION_COOKIE_EXPIRATION_DELAY , SESSION_EXPIRATION_DELAY , SESSION_TIME_OUT_DELAY } from '../sessionConstants'
76import { buildCookieOptions , selectCookieStrategy , initCookieStrategy } from './sessionInCookie'
87import type { SessionStoreStrategy } from './sessionStoreStrategy'
98import { SESSION_STORE_KEY } from './sessionStoreStrategy'
@@ -103,6 +102,56 @@ describe('session in cookie strategy', () => {
103102 } )
104103 } )
105104} )
105+
106+ describe ( 'session in cookie strategy when opt-in anonymous user tracking' , ( ) => {
107+ const anonymousId = 'device-123'
108+ const sessionState : SessionState = { id : '123' , created : '0' }
109+ let cookieStorageStrategy : SessionStoreStrategy
110+ beforeEach ( ( ) => {
111+ cookieStorageStrategy = initCookieStrategy (
112+ { ...DEFAULT_INIT_CONFIGURATION , trackAnonymousUser : true } as Configuration ,
113+ { }
114+ )
115+ } )
116+
117+ afterEach ( ( ) => {
118+ deleteCookie ( SESSION_STORE_KEY )
119+ } )
120+ it ( 'should persist with anonymous id' , ( ) => {
121+ cookieStorageStrategy . persistSession ( { ...sessionState , anonymousId } )
122+ const session = cookieStorageStrategy . retrieveSession ( )
123+ expect ( session ) . toEqual ( { ...sessionState , anonymousId } )
124+ expect ( getCookie ( SESSION_STORE_KEY ) ) . toBe ( 'id=123&created=0&aid=device-123' )
125+ } )
126+
127+ it ( 'should expire with anonymous id' , ( ) => {
128+ cookieStorageStrategy . expireSession ( { ...sessionState , anonymousId } )
129+ const session = cookieStorageStrategy . retrieveSession ( )
130+ expect ( session ) . toEqual ( { isExpired : '1' , anonymousId } )
131+ expect ( getCookie ( SESSION_STORE_KEY ) ) . toBe ( 'isExpired=1&aid=device-123' )
132+ } )
133+
134+ it ( 'should persist for one year when opt-in' , ( ) => {
135+ const cookieSetSpy = spyOnProperty ( document , 'cookie' , 'set' )
136+ const clock = mockClock ( )
137+ cookieStorageStrategy . persistSession ( { ...sessionState , anonymousId } )
138+ expect ( cookieSetSpy . calls . argsFor ( 0 ) [ 0 ] ) . toContain (
139+ new Date ( clock . timeStamp ( SESSION_COOKIE_EXPIRATION_DELAY ) ) . toUTCString ( )
140+ )
141+ clock . cleanup ( )
142+ } )
143+
144+ it ( 'should expire in one year when opt-in' , ( ) => {
145+ const cookieSetSpy = spyOnProperty ( document , 'cookie' , 'set' )
146+ const clock = mockClock ( )
147+ cookieStorageStrategy . expireSession ( { ...sessionState , anonymousId } )
148+ expect ( cookieSetSpy . calls . argsFor ( 0 ) [ 0 ] ) . toContain (
149+ new Date ( clock . timeStamp ( SESSION_COOKIE_EXPIRATION_DELAY ) ) . toUTCString ( )
150+ )
151+ clock . cleanup ( )
152+ } )
153+ } )
154+
106155describe ( 'session in cookie strategy when opt-out anonymous user tracking' , ( ) => {
107156 const anonymousId = 'device-123'
108157 const sessionState : SessionState = { id : '123' , created : '0' }
@@ -113,7 +162,6 @@ describe('session in cookie strategy when opt-out anonymous user tracking', () =
113162 } )
114163
115164 afterEach ( ( ) => {
116- resetExperimentalFeatures ( )
117165 deleteCookie ( SESSION_STORE_KEY )
118166 } )
119167
@@ -125,6 +173,12 @@ describe('session in cookie strategy when opt-out anonymous user tracking', () =
125173 clock . cleanup ( )
126174 } )
127175
176+ it ( 'should not persist with one year when opt-out' , ( ) => {
177+ const cookieSetSpy = spyOnProperty ( document , 'cookie' , 'set' )
178+ cookieStorageStrategy . persistSession ( { ...sessionState , anonymousId } )
179+ expect ( cookieSetSpy . calls . argsFor ( 0 ) [ 0 ] ) . toContain ( new Date ( Date . now ( ) + SESSION_EXPIRATION_DELAY ) . toUTCString ( ) )
180+ } )
181+
128182 it ( 'should not persist or expire a session with anonymous id when opt-out' , ( ) => {
129183 cookieStorageStrategy . persistSession ( { ...sessionState , anonymousId } )
130184 cookieStorageStrategy . expireSession ( { ...sessionState , anonymousId } )
0 commit comments