Skip to content

Commit

Permalink
Improve cookie helper
Browse files Browse the repository at this point in the history
  • Loading branch information
cy-moi committed Jan 22, 2025
1 parent ff0e8a8 commit 23f3eb3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 24 deletions.
16 changes: 13 additions & 3 deletions test/e2e/lib/helpers/session.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { SESSION_STORE_KEY, SESSION_TIME_OUT_DELAY } from '@datadog/browser-core'
import { setCookie } from './browser'

type SessionCookie = {
id?: string
isExpired?: string
aid?: string
}

export async function renewSession() {
await expireSession()
const documentElement = await $('html')
await documentElement.click()

expect(await findSessionCookie()).not.toContain('isExpired=1')
expect((await findSessionCookie())?.isExpired).not.toEqual('1')
}

export async function expireSession() {
Expand All @@ -17,7 +23,7 @@ export async function expireSession() {

await setCookie(SESSION_STORE_KEY, expireCookie, SESSION_TIME_OUT_DELAY)

expect(await findSessionCookie()).toContain('isExpired=1')
expect((await findSessionCookie())?.isExpired).toEqual('1')

// Cookies are cached for 1s, wait until the cache expires
await browser.pause(1100)
Expand All @@ -27,5 +33,9 @@ export async function findSessionCookie() {
const cookies = await browser.getCookies(SESSION_STORE_KEY)
// In some case, the session cookie is returned but with an empty value. Let's consider it expired
// in this case.
return cookies[0]?.value || undefined
const rawValue = cookies[0]?.value
if (!rawValue) {
return
}
return Object.fromEntries(rawValue.split('&').map((part: string) => part.split('='))) as SessionCookie
}
29 changes: 8 additions & 21 deletions test/e2e/scenario/rum/sessions.scenario.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,18 @@ describe('rum sessions', () => {
createTest('persists when session is expired')
.withRum()
.run(async () => {
const prevSessionCookie = await findSessionCookie()
const anonymousId = prevSessionCookie?.match(/aid=[a-z0-9-]+/)
expect(anonymousId).not.toBeNull()
const anonymousId = (await findSessionCookie())?.aid

await expireSession()
await flushEvents()

if (anonymousId) {
expect(await findSessionCookie()).toContain(anonymousId[0])
}
expect((await findSessionCookie())?.aid).toEqual(anonymousId)
})

createTest('persists when session renewed')
.withRum()
.run(async () => {
const prevSessionCookie = await findSessionCookie()
const anonymousId = prevSessionCookie?.match(/aid=[a-z0-9-]+/)
const anonymousId = (await findSessionCookie())?.aid
expect(anonymousId).not.toBeNull()

await browser.execute(() => {
Expand All @@ -81,15 +76,7 @@ describe('rum sessions', () => {
// The session is not created right away, let's wait until we see a cookie
await browser.waitUntil(async () => Boolean(await findSessionCookie()))

await browser.execute(() => {
window.DD_RUM!.addAction('foo')
})

await flushEvents()

if (anonymousId) {
expect(await findSessionCookie()).toContain(anonymousId[0])
}
expect((await findSessionCookie())?.aid).toEqual(anonymousId)
})

createTest('generated when cookie is cleared')
Expand All @@ -99,7 +86,7 @@ describe('rum sessions', () => {
await renewSession()
await flushEvents()

expect(await findSessionCookie()).toMatch(/aid=[a-z0-9-]+/)
expect((await findSessionCookie())?.aid).toBeDefined()
})
})

Expand All @@ -121,7 +108,7 @@ describe('rum sessions', () => {
})
await flushEvents()

expect(await findSessionCookie()).toContain('isExpired=1')
expect((await findSessionCookie())?.isExpired).toEqual('1')
expect(intakeRegistry.rumActionEvents.length).toBe(0)
})

Expand All @@ -142,8 +129,8 @@ describe('rum sessions', () => {

await flushEvents()

expect(await findSessionCookie()).not.toContain('isExpired=1')
expect(await findSessionCookie()).toMatch(/\bid=[a-f0-9-]+/)
expect((await findSessionCookie())?.isExpired).not.toEqual('1')
expect((await findSessionCookie())?.id).toBeDefined()
expect(intakeRegistry.rumActionEvents.length).toBe(1)
})

Expand Down

0 comments on commit 23f3eb3

Please sign in to comment.