Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { TestClass } from '../TestClass'; import StorageManager from '../../src/properties/StorageManager'; import SessionManager from '../../src/properties/SessionManager'; import * as sinon from 'sinon'; import { IExtendedTelemetryItem, CoreUtils } from '@ms/1ds-core-js'; const extendTime = 1800000; //30 mins export class SessionManagerTest extends TestClass { private storageManagerStub: sinon.SinonStubbedInstance<StorageManager> | any; private sessionManager: SessionManager; private time: number; public testInitialize() { this.storageManagerStub = sinon.createStubInstance<StorageManager>(StorageManager); this.sessionManager = new SessionManager(this.storageManagerStub); this.time = new Date().getTime(); } public registerTests() { this.testCase({ name: 'Validate session active', test: () => { // When no session is there QUnit.assert.notOk(this.sessionManager._isSessionActive(this.time)); // Add session information (this.storageManagerStub as sinon.SinonStubbedInstance<StorageManager>)._getProperty.returns('id;' + this.time + ';' + (this.time + extendTime)); QUnit.assert.ok(this.sessionManager._isSessionActive(this.time, true)); // Expired if time is more than the extended time QUnit.assert.notOk(this.sessionManager._isSessionActive(this.time + extendTime + 1, true)); } }); this.testCase({ name: 'Custom signals do not get Session Id if no session is active', test: () => { let event: IExtendedTelemetryItem = { name: 'test', time: CoreUtils.toISOString(new Date()), data: { s: { u: {} } } }; QUnit.assert.notOk(this.sessionManager._isSessionActive(this.time)); this.sessionManager._processEvent(event, false, false); QUnit.assert.equal(event.data['s']['s']['i'], ''); } }); this.testCase({ name: 'Hit signals start a session', test: () => { QUnit.assert.notOk(this.sessionManager._isSessionActive(this.time)); let view: IExtendedTelemetryItem = { name: 'view', time: CoreUtils.toISOString(new Date()), data: { s: { u: {} } } }; this.sessionManager._processEvent(view, false, true); QUnit.assert.ok(this.sessionManager._isSessionActive(Date.parse(view.time))); QUnit.assert.ok(view.data['s']['s']['i']); QUnit.assert.equal(view.data['s']['s']['d'], 0); this.sessionManager = new SessionManager(this.storageManagerStub); let action: IExtendedTelemetryItem = { name: 'action', time: CoreUtils.toISOString(new Date()), data: { s: { u: {} } } }; this.sessionManager._processEvent(action, false, true); QUnit.assert.ok(action.data['s']['s']['i']); QUnit.assert.equal(action.data['s']['s']['d'], 0); } }); this.testCase({ name: 'Hit signals extend a session', test: () => { let sessionStored: string; let oldSessionExtendedDuration = this.time + extendTime; (this.storageManagerStub as sinon.SinonStubbedInstance<StorageManager>)._getProperty.returns('id;' + this.time + ';' + oldSessionExtendedDuration); (this.storageManagerStub as sinon.SinonStubbedInstance<StorageManager>)._setProperty.callsFake((arg1, arg2) => { sessionStored = arg2; }); this.sessionManager = new SessionManager(this.storageManagerStub); let view: IExtendedTelemetryItem = { name: 'view', time: CoreUtils.toISOString(new Date(this.time + 10)), data: { s: { u: {} } } }; this.sessionManager._processEvent(view, false, true); QUnit.assert.ok(this.sessionManager._isSessionActive(Date.parse(view.time))); QUnit.assert.equal(view.data['s']['s']['i'], 'id'); QUnit.assert.equal(view.data['s']['s']['d'], 10); QUnit.assert.equal(parseInt(sessionStored.split(';')[2], 10) - oldSessionExtendedDuration, 10); } }); this.testCase({ name: 'All signals contain session id if there is an active session', test: () => { (this.storageManagerStub as sinon.SinonStubbedInstance<StorageManager>)._getProperty.returns('id;' + this.time + ';' + (this.time + extendTime)); this.sessionManager = new SessionManager(this.storageManagerStub); let event: IExtendedTelemetryItem = { name: 'test', time: CoreUtils.toISOString(new Date()), data: { s: { u: {} } } }; this.sessionManager._processEvent(event, false, false); QUnit.assert.equal(event.data['s']['s']['i'], 'id'); QUnit.assert.notOk(event.data['s']['s']['d']); } }); this.testCase({ name: 'TimeSinceVisit is set correctly', test: () => { let view: IExtendedTelemetryItem = { name: 'view', time: CoreUtils.toISOString(new Date(this.time)), data: { s: { u: {} } } }; // New user does not have tims since visit set. this.sessionManager._processEvent(view, true, true); QUnit.assert.notOk(view.data['s']['u']['t']); // Second view in the session does not have the TimeSinceVisit this.sessionManager._processEvent(view, false, true); QUnit.assert.notOk(view.data['s']['u']['t']); // Set up a previous expired session that ended 5ms ago this.sessionManager = new SessionManager(this.storageManagerStub); (this.storageManagerStub as sinon.SinonStubbedInstance<StorageManager>)._getProperty.returns('id;' + (this.time - 10) + ';' + (this.time - 5)); this.sessionManager._processEvent(view, false, true); QUnit.assert.equal(view.data['s']['u']['t'], 5); } }); this.testCase({ name: 'New or Returning User is set correctly', test: () => { let view: IExtendedTelemetryItem = { name: 'view', time: CoreUtils.toISOString(new Date()), data: { s: { u: {} } } }; let sessionStored: string; (this.storageManagerStub as sinon.SinonStubbedInstance<StorageManager>)._setProperty.callsFake((arg1, arg2) => { sessionStored = arg2; }); // Process telemetry will add new user if no cookie is set. this.sessionManager._processEvent(view, true, true); QUnit.assert.equal(view.data['s']['u']['s'], 'new'); (this.storageManagerStub as sinon.SinonStubbedInstance<StorageManager>)._getProperty.returns(sessionStored); // Second view in the session still marks the user as new for entire session but user cookie is not created. this.sessionManager._processEvent(view, false, true); QUnit.assert.equal(view.data['s']['u']['s'], 'new'); // Mock new session but user cookie is not created. (this.storageManagerStub as sinon.SinonStubbedInstance<StorageManager>)._getProperty.returns(null); this.sessionManager._processEvent(view, false, true); QUnit.assert.equal(view.data['s']['u']['s'], 'returning'); } }); } } |