|
| 1 | +import { SessionContext, SessionStatus } from '@sentry/types'; |
| 2 | +import { timestampInSeconds } from '@sentry/utils'; |
| 3 | + |
| 4 | +import { Session } from '../src/session'; |
| 5 | + |
| 6 | +describe('Session', () => { |
| 7 | + it('initializes with the proper defaults', () => { |
| 8 | + const session = new Session().toJSON(); |
| 9 | + |
| 10 | + // Grab current year to check if we are converting from sec -> ms correctly |
| 11 | + const currentYear = new Date(timestampInSeconds() * 1000).toISOString().slice(0, 4); |
| 12 | + expect(session).toEqual({ |
| 13 | + attrs: {}, |
| 14 | + duration: 0, |
| 15 | + errors: 0, |
| 16 | + init: true, |
| 17 | + sid: expect.any(String), |
| 18 | + started: expect.stringMatching(currentYear), |
| 19 | + status: SessionStatus.Ok, |
| 20 | + timestamp: expect.stringMatching(currentYear), |
| 21 | + }); |
| 22 | + |
| 23 | + expect(session.sid).toHaveLength(32); |
| 24 | + |
| 25 | + // started and timestamp should be the same on creation |
| 26 | + expect(session.started).toEqual(session.timestamp); |
| 27 | + }); |
| 28 | + |
| 29 | + describe('update', () => { |
| 30 | + const time = timestampInSeconds(); |
| 31 | + // [ name, in, out ] |
| 32 | + const table: Array<[string, SessionContext, Record<string, any>]> = [ |
| 33 | + ['sets an ip address', { user: { ip_address: '0.0.0.0' } }, { attrs: { ip_address: '0.0.0.0' } }], |
| 34 | + ['sets a did', { user: { id: 'specialID123' } }, { did: 'specialID123' }], |
| 35 | + ['sets a timestamp', { timestamp: time }, { timestamp: new Date(time * 1000).toISOString() }], |
| 36 | + ['sets a sid', { sid: '99705f22a3f1468e95ba7386e84691aa' }, { sid: '99705f22a3f1468e95ba7386e84691aa' }], |
| 37 | + ['requires custom sid to be of certain length', { sid: '123' }, { sid: expect.not.stringMatching('123') }], |
| 38 | + ['requires custom sid to be of certain length', { sid: '123' }, { sid: expect.not.stringMatching('123') }], |
| 39 | + ['sets an init', { init: false }, { init: false }], |
| 40 | + ['sets an did', { did: 'specialID123' }, { did: 'specialID123' }], |
| 41 | + ['overwrites user did with custom did', { did: 'custom-did', user: { id: 'user-id' } }, { did: 'custom-did' }], |
| 42 | + ['sets a started time', { started: time }, { started: new Date(time * 1000).toISOString() }], |
| 43 | + ['does not set a duration for browser env', { ignoreDuration: true }, { duration: undefined }], |
| 44 | + ['sets a duration', { duration: 12000 }, { duration: 12000 }], |
| 45 | + [ |
| 46 | + 'does not use custom duration for browser env', |
| 47 | + { duration: 12000, ignoreDuration: true }, |
| 48 | + { duration: undefined }, |
| 49 | + ], |
| 50 | + [ |
| 51 | + 'does not set a negative duration', |
| 52 | + { timestamp: 10, started: 100 }, |
| 53 | + { duration: 0, timestamp: expect.any(String), started: expect.any(String) }, |
| 54 | + ], |
| 55 | + [ |
| 56 | + 'sets duration based on timestamp and started', |
| 57 | + { timestamp: 100, started: 10 }, |
| 58 | + { duration: 90, timestamp: expect.any(String), started: expect.any(String) }, |
| 59 | + ], |
| 60 | + [ |
| 61 | + 'sets a release', |
| 62 | + { release: 'f1557994979ecd969963f53c27ca946379d721f3' }, |
| 63 | + { attrs: { release: 'f1557994979ecd969963f53c27ca946379d721f3' } }, |
| 64 | + ], |
| 65 | + ['sets an environment', { environment: 'staging' }, { attrs: { environment: 'staging' } }], |
| 66 | + ['sets an ipAddress', { ipAddress: '0.0.0.0' }, { attrs: { ip_address: '0.0.0.0' } }], |
| 67 | + [ |
| 68 | + 'overwrites user ip_address did with custom ipAddress', |
| 69 | + { ipAddress: '0.0.0.0', user: { ip_address: '1.1.1.1' } }, |
| 70 | + { attrs: { ip_address: '0.0.0.0' } }, |
| 71 | + ], |
| 72 | + ['sets an userAgent', { userAgent: 'Mozilla/5.0' }, { attrs: { user_agent: 'Mozilla/5.0' } }], |
| 73 | + ['sets errors', { errors: 3 }, { errors: 3 }], |
| 74 | + ['sets status', { status: SessionStatus.Crashed }, { status: SessionStatus.Crashed }], |
| 75 | + ]; |
| 76 | + |
| 77 | + test.each(table)('%s', (...test) => { |
| 78 | + // duration and timestamp can vary after session update, so let's expect anything unless |
| 79 | + // the out variable in a test explicitly refers to it. |
| 80 | + const DEFAULT_OUT = { duration: expect.any(Number), timestamp: expect.any(String) }; |
| 81 | + |
| 82 | + const session = new Session(); |
| 83 | + const initSessionProps = session.toJSON(); |
| 84 | + |
| 85 | + session.update(test[1]); |
| 86 | + expect(session.toJSON()).toEqual({ ...initSessionProps, ...DEFAULT_OUT, ...test[2] }); |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + describe('close', () => { |
| 91 | + it('exits a normal session', () => { |
| 92 | + const session = new Session(); |
| 93 | + expect(session.status).toEqual(SessionStatus.Ok); |
| 94 | + session.close(); |
| 95 | + expect(session.status).toEqual(SessionStatus.Exited); |
| 96 | + }); |
| 97 | + |
| 98 | + it('updates session status when give status', () => { |
| 99 | + const session = new Session(); |
| 100 | + expect(session.status).toEqual(SessionStatus.Ok); |
| 101 | + |
| 102 | + session.close(SessionStatus.Abnormal); |
| 103 | + expect(session.status).toEqual(SessionStatus.Abnormal); |
| 104 | + }); |
| 105 | + |
| 106 | + it('only changes status ok to exited', () => { |
| 107 | + const session = new Session(); |
| 108 | + session.update({ status: SessionStatus.Crashed }); |
| 109 | + expect(session.status).toEqual(SessionStatus.Crashed); |
| 110 | + |
| 111 | + session.close(); |
| 112 | + expect(session.status).toEqual(SessionStatus.Crashed); |
| 113 | + }); |
| 114 | + }); |
| 115 | +}); |
0 commit comments