Something went wrong. Try again.
cee sky
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329const NOW = new Date('2026-09-22T12:00:00.000Z')const mockDeviceValues = new Map<string, unknown>()const mockDeviceGetRaw = jest.fn((key: string[]) => { if (!mockDeviceValues.has(key[0])) return undefined return JSON.stringify({data: mockDeviceValues.get(key[0])})})const mockDeviceListeners = new Set<() => void>()const mockDeviceSet = jest.fn((key: string[], value: unknown) => { mockDeviceValues.set(key[0], value) emitStorageChange()})const mockDeviceSubscribe = jest.fn((_key: string[], listener: () => void) => { mockDeviceListeners.add(listener) return {remove: () => mockDeviceListeners.delete(listener)}})const mockAppStateListeners = new Set<(state: string) => void>()const mockOnAppStateChange = jest.fn((listener: (state: string) => void) => { mockAppStateListeners.add(listener) return {remove: () => mockAppStateListeners.delete(listener)}})const mockUuidV4 = jest.fn<unknown, []>()let mockIsNative = truelet sessionValidation: jest.SpyInstance
jest.mock('react-native-uuid', () => ({ __esModule: true, default: {v4: mockUuidV4},}))jest.mock('#/env', () => ({IS_NATIVE: mockIsNative}))jest.mock('#/analytics/identifiers/sessionStorage', () => jest.requireActual<typeof import('./sessionStorage')>( mockIsNative ? '#/analytics/identifiers/sessionStorage/index.ts' : '#/analytics/identifiers/sessionStorage/index.web.ts', ),)jest.mock('#/storage', () => ({ device: { getRaw: mockDeviceGetRaw, set: mockDeviceSet, addOnValueChangedListener: mockDeviceSubscribe, },}))jest.mock('#/lib/appState', () => ({ onAppStateChange: mockOnAppStateChange,}))
beforeEach(() => { jest.useFakeTimers() jest.setSystemTime(NOW) jest.clearAllMocks() mockDeviceValues.clear() mockDeviceListeners.clear() mockAppStateListeners.clear() mockUuidV4.mockReset().mockReturnValue('session-a')})afterEach(() => { jest.restoreAllMocks() jest.useRealTimers()})
function emitStorageChange() { mockDeviceListeners.forEach(listener => listener())}
function setStoredSession(id: string, lastEventAt?: number) { mockDeviceValues.set('analyticsSession', {id, lastEventAt}) emitStorageChange()}
function emitAppState(state: string) { mockAppStateListeners.forEach(listener => listener(state))}
function loadSession(): typeof import('./session') { let session: typeof import('./session') | undefined jest.isolateModules(() => { const {z} = require('zod') as typeof import('zod') sessionValidation = jest.spyOn(z.ZodType.prototype, 'safeParse') session = require('./session') }) return session!}
describe('native analytics sessions', () => { const isNative = true const ttl = 5 * 60 * 1e3 beforeEach(() => { mockIsNative = isNative })
it('creates and persists the ID and timestamp in one write', () => { const {getSessionId} = loadSession()
expect(getSessionId()).toBe('session-a') expect(mockUuidV4).toHaveBeenCalledTimes(1) expect(mockDeviceSet).toHaveBeenCalledTimes(1) expect(mockDeviceSet).toHaveBeenCalledWith(['analyticsSession'], { id: 'session-a', lastEventAt: NOW.getTime(), }) })
it('reuses validated data with native cached raw reads', () => { const {getSessionId} = loadSession() getSessionId() mockDeviceGetRaw.mockClear() sessionValidation.mockClear() const parse = jest.spyOn(JSON, 'parse')
for (let i = 0; i < 100; i++) getSessionId()
expect(sessionValidation).not.toHaveBeenCalled() expect(parse).not.toHaveBeenCalled() expect(mockDeviceGetRaw).toHaveBeenCalledTimes(isNative ? 0 : 100) })
it('parses and validates a changed raw value once', () => { const {getSessionId} = loadSession() getSessionId() mockDeviceGetRaw.mockClear() sessionValidation.mockClear() const parse = jest.spyOn(JSON, 'parse')
setStoredSession('other-runtime-session', Date.now()) expect(getSessionId()).toBe('other-runtime-session') expect(getSessionId()).toBe('other-runtime-session')
expect(parse).toHaveBeenCalledTimes(1) expect(sessionValidation).toHaveBeenCalledTimes(1) expect(mockDeviceGetRaw).toHaveBeenCalledTimes(isNative ? 1 : 2) expect(mockDeviceSubscribe).toHaveBeenCalledTimes(isNative ? 1 : 0) })
it('observes timestamp-only changes before deciding whether to rotate', () => { const {getSessionId} = loadSession() expect(getSessionId()).toBe('session-a') setStoredSession('session-a', Date.now() - ttl) mockUuidV4.mockReturnValueOnce('session-b')
emitAppState('active')
expect(getSessionId()).toBe('session-b') })
it('reuses an unexpired stored session', () => { mockDeviceValues.set('analyticsSession', { id: 'existing-session', lastEventAt: NOW.getTime() - ttl + 1, extra: true, }) const {getSessionId} = loadSession()
expect(getSessionId()).toBe('existing-session') expect(mockUuidV4).not.toHaveBeenCalled() expect(mockDeviceValues.get('analyticsSession')).toEqual({ id: 'existing-session', lastEventAt: NOW.getTime(), }) })
it('rotates an expired stored session on startup at the exact boundary', () => { setStoredSession('existing-session', NOW.getTime() - ttl) const {getSessionId} = loadSession()
expect(getSessionId()).toBe('session-a') expect(mockUuidV4).toHaveBeenCalledTimes(1) })
it.each([undefined, null, '123', Number.NaN, Infinity, -Infinity])( 'preserves a session with an invalid or missing timestamp: %s', lastEventAt => { mockDeviceValues.set('analyticsSession', { id: 'existing-session', lastEventAt, }) const {getSessionId} = loadSession()
expect(getSessionId()).toBe('existing-session') expect(mockUuidV4).not.toHaveBeenCalled() }, )
it.each([undefined, null, 'session-a', [], {}, {id: ''}, {id: 123}])( 'replaces an invalid record: %j', record => { mockDeviceValues.set('analyticsSession', record) const {getSessionId} = loadSession()
expect(getSessionId()).toBe('session-a') expect(mockDeviceSet).toHaveBeenCalledWith(['analyticsSession'], { id: 'session-a', lastEventAt: NOW.getTime(), }) }, )
it.each(['{', 'null', '[]', '42', '"string"', '{}'])( 'replaces malformed or invalid serialized storage: %s', raw => { mockDeviceGetRaw.mockReturnValueOnce(raw) expect(loadSession().getSessionId()).toBe('session-a') }, )
describe.each(['read', 'active', 'inactive', 'background'])( 'recovering through %s', trigger => { it.each(['missing', 'cleared', 'invalid', 'malformed JSON'])( 'persists a fresh session after rotation when storage is %s', failure => { const {getSessionId} = loadSession() expect(getSessionId()).toBe('session-a') emitAppState('background') jest.advanceTimersByTime(ttl) mockUuidV4.mockReturnValueOnce('session-b') emitAppState('active') expect(getSessionId()).toBe('session-b')
if (failure === 'missing') { mockDeviceValues.delete('analyticsSession') } else if (failure === 'cleared') { mockDeviceValues.clear() } else if (failure === 'invalid') { mockDeviceValues.set('analyticsSession', {id: ''}) } else { mockDeviceGetRaw.mockReturnValueOnce('{') } emitStorageChange() jest.advanceTimersByTime(1_000) mockUuidV4.mockReturnValueOnce('session-c') mockDeviceSet.mockClear()
if (trigger !== 'read') emitAppState(trigger) expect(getSessionId()).toBe('session-c') expect(getSessionId()).toBe('session-c') expect(mockUuidV4).toHaveBeenCalledTimes(3) expect(mockDeviceSet).toHaveBeenCalledTimes(1) expect(mockDeviceValues.get('analyticsSession')).toEqual({ id: 'session-c', lastEventAt: Date.now(), }) }, ) }, )
it('does not rotate before the inactivity threshold', () => { setStoredSession('existing-session', NOW.getTime()) const {getSessionId} = loadSession()
emitAppState('background') jest.advanceTimersByTime(ttl - 1) emitAppState('active')
expect(getSessionId()).toBe('existing-session') expect(mockUuidV4).not.toHaveBeenCalled() })
it('rotates once at the exact boundary without React consumers', () => { setStoredSession('existing-session', NOW.getTime()) const {getSessionId} = loadSession() expect(mockOnAppStateChange).toHaveBeenCalledTimes(1)
emitAppState('background') jest.advanceTimersByTime(ttl) mockDeviceSet.mockClear() emitAppState('active')
expect(mockUuidV4).toHaveBeenCalledTimes(1) expect([getSessionId(), getSessionId(), getSessionId()]).toEqual([ 'session-a', 'session-a', 'session-a', ]) expect(mockDeviceSubscribe).toHaveBeenCalledTimes(isNative ? 1 : 0) expect(mockDeviceSet).toHaveBeenCalledTimes(1) expect(mockDeviceSet).toHaveBeenCalledWith(['analyticsSession'], { id: 'session-a', lastEventAt: NOW.getTime() + ttl, }) expect([...mockDeviceValues.keys()]).toEqual(['analyticsSession']) })
it('keeps the original last-event semantics across intermediate states', () => { setStoredSession('existing-session', NOW.getTime()) const {getSessionId} = loadSession()
emitAppState('inactive') jest.advanceTimersByTime(ttl - 1) emitAppState('background') jest.advanceTimersByTime(1) emitAppState('active')
expect(getSessionId()).toBe('existing-session') expect(mockUuidV4).not.toHaveBeenCalled() })
it('observes local storage writes without React consumers', () => { const {getSessionId} = loadSession()
mockDeviceSet(['analyticsSession'], { id: 'local-session', lastEventAt: Date.now(), })
expect(getSessionId()).toBe('local-session') expect(mockDeviceSubscribe).toHaveBeenCalledTimes(isNative ? 1 : 0) })
it('keeps tracking repeated lifecycle events when no one reads the ID', () => { const {getSessionId} = loadSession()
emitAppState('background') jest.advanceTimersByTime(ttl) mockUuidV4.mockReturnValueOnce('session-b') emitAppState('active') emitAppState('background') jest.advanceTimersByTime(ttl) mockUuidV4.mockReturnValueOnce('session-c') emitAppState('active')
expect(getSessionId()).toBe('session-c') expect(mockOnAppStateChange).toHaveBeenCalledTimes(1) expect(mockAppStateListeners.size).toBe(1) expect(mockDeviceSubscribe).toHaveBeenCalledTimes(isNative ? 1 : 0) })})