-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathNodeAFK.spec.js
426 lines (284 loc) · 12.3 KB
/
NodeAFK.spec.js
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/* global describe, afterEach, it */
const { expect } = require('chai');
const desktopIdle = require('desktop-idle');
const EventEmitter = require('events');
const sinon = require('sinon');
const NodeAFK = require('./NodeAFK');
const sandbox = sinon.createSandbox();
describe('NodeAFK', () => {
afterEach(sandbox.restore);
it('should be an event emitter', () => {
const afk = new NodeAFK(1000);
expect(afk).to.be.instanceOf(EventEmitter);
});
it('should create a new instance with the correct parameters', () => {
const inactivityDuration = 10000;
const pollInterval = 5000;
const afk = new NodeAFK(inactivityDuration, pollInterval);
expect(afk).to.have.property('inactivityDuration', inactivityDuration);
expect(afk).to.have.property('pollInterval', pollInterval);
});
it('should create a new instance with a default poll interval', () => {
const inactivityDuration = 10000;
const expectedDefaultPollInterval = 1000;
const afk = new NodeAFK(inactivityDuration);
expect(afk).to.have.property('pollInterval', expectedDefaultPollInterval);
});
it('should create a new instance with a default current status', () => {
const inactivityDuration = 10000;
const expectedDefaultStatus = NodeAFK.STATUS_ACTIVE;
const afk = new NodeAFK(inactivityDuration);
expect(afk).to.have.property('currentStatus', expectedDefaultStatus);
});
it('should set the current status', () => {
const inactivityDuration = 10000;
const afk = new NodeAFK(inactivityDuration);
const status = NodeAFK.STATUS_IDLE;
afk.setStatus(status);
expect(afk.currentStatus).to.equal(status);
});
it('should throw an error if an invalid value is used when setting the current status', () => {
const inactivityDuration = 10000;
const afk = new NodeAFK(inactivityDuration);
const status = 'foo';
expect(() => afk.setStatus(status)).to.throw('foo is not a valid status');
});
it('should record when the user became active when their status is set to active', () => {
const inactivityDuration = 10000;
const clock = sandbox.useFakeTimers();
const afk = new NodeAFK(inactivityDuration);
// the status of the user will be `active` by default so no need to
// explicitly call setStatus here
expect(afk.userLastActiveAt).to.not.equal(undefined);
// check that userLastActiveAt is updated each the status of the user changes to active
const { userLastActiveAt } = afk;
afk.setStatus(NodeAFK.STATUS_IDLE);
clock.tick(1000);
afk.setStatus(NodeAFK.STATUS_ACTIVE);
expect(afk.userLastActiveAt).to.be.a('Number');
expect(userLastActiveAt).to.be.lt(afk.userLastActiveAt);
});
it('should setup the poll interval on intialisation', () => {
const inactivityDuration = 10000;
const pollInterval = 1000;
const setIntervalStub = sandbox.stub(global, 'setInterval').returns(123);
const afk = new NodeAFK(inactivityDuration, pollInterval);
afk.init();
expect(setIntervalStub.callCount).to.equal(1);
expect(setIntervalStub.firstCall.args[0]).to.be.instanceOf(Function);
expect(setIntervalStub.firstCall.args[1]).to.equal(pollInterval);
});
it('should throw an error if initialising an already intialised instance', () => {
const inactivityDuration = 10000;
sandbox.stub(global, 'setInterval').returns(123);
const afk = new NodeAFK(inactivityDuration);
afk.init();
expect(() => afk.init()).to.throw('node-afk instance has already been initialised');
});
it('should stop the poll interval if the poll has been setup when destroyed', () => {
const inactivityDuration = 10000;
const expectedPollIntervalId = 123;
sandbox.stub(global, 'setInterval').returns(expectedPollIntervalId);
const clearIntervalStub = sandbox.stub(global, 'clearInterval');
const afk = new NodeAFK(inactivityDuration);
afk.init();
afk.destroy();
expect(clearIntervalStub.callCount).to.equal(1);
expect(clearIntervalStub.firstCall.args).to.deep.equal([expectedPollIntervalId]);
});
it('should not attempt to stop the poll interval if the poll has not been setup when destroyed', () => {
const inactivityDuration = 10000;
const clearIntervalStub = sandbox.stub(global, 'clearInterval');
const afk = new NodeAFK(inactivityDuration);
afk.destroy();
expect(clearIntervalStub.callCount).to.equal(0);
});
it('should clear any timed events that have been setup when destroyed', () => {
const inactivityDuration = 10000;
const afk = new NodeAFK(inactivityDuration);
afk.on('idle:2000', () => {});
afk.destroy();
expect(afk.timedEvents).to.deep.equal([]);
});
it('should remove any event listeners that have been setup when destroyed', () => {
const inactivityDuration = 10000;
const afk = new NodeAFK(inactivityDuration);
afk.on('status:idle', () => {});
afk.destroy();
expect(afk.eventNames().length).to.equal(0);
});
it('should emit a "status-change" event when the status of the user changes from active to idle', () => {
const oneSecond = 1000;
const inactivityDuration = oneSecond * 2;
const clock = sandbox.useFakeTimers();
const listener = sandbox.stub();
sandbox.stub(desktopIdle, 'getIdleTime')
.onFirstCall()
.returns(1)
.onSecondCall()
.returns(2);
const afk = new NodeAFK(inactivityDuration, oneSecond);
afk.init();
afk.on('status-change', listener);
clock.tick(inactivityDuration);
expect(listener.callCount).to.equal(1);
expect(listener.firstCall.args[0]).to.deep.equal({
previousStatus: NodeAFK.STATUS_ACTIVE,
currentStatus: NodeAFK.STATUS_IDLE,
});
});
it('should emit a "status-change" event when the status of the user changes from idle to active', () => {
const oneSecond = 1000;
const inactivityDuration = oneSecond * 2;
const clock = sandbox.useFakeTimers();
const listener = sandbox.stub();
sandbox.stub(desktopIdle, 'getIdleTime')
.onFirstCall()
.returns(1)
.onSecondCall()
.returns(2);
const afk = new NodeAFK(inactivityDuration, oneSecond);
afk.init();
afk.setStatus(NodeAFK.STATUS_IDLE);
afk.on('status-change', listener);
clock.tick(oneSecond);
expect(listener.callCount).to.equal(1);
expect(listener.firstCall.args[0]).to.deep.equal({
previousStatus: NodeAFK.STATUS_IDLE,
currentStatus: NodeAFK.STATUS_ACTIVE,
});
});
it('should emit a "status:idle" event when the status of the user changes from active to idle', () => {
const oneSecond = 1000;
const inactivityDuration = oneSecond * 2;
const clock = sandbox.useFakeTimers();
const listener = sandbox.stub();
sandbox.stub(desktopIdle, 'getIdleTime')
.onFirstCall()
.returns(oneSecond)
.onSecondCall()
.returns(oneSecond * 2);
const afk = new NodeAFK(inactivityDuration, oneSecond);
afk.init();
afk.on('status:idle', listener);
clock.tick(inactivityDuration);
expect(listener.callCount).to.equal(1);
});
it('should emit a "status:active" event when the status of the user changes from active to idle', () => {
const oneSecond = 1000;
const inactivityDuration = oneSecond * 2;
const clock = sandbox.useFakeTimers();
const listener = sandbox.stub();
sandbox.stub(desktopIdle, 'getIdleTime')
.onFirstCall()
.returns(1)
.onSecondCall()
.returns(2);
const afk = new NodeAFK(inactivityDuration, oneSecond);
afk.init();
afk.setStatus(NodeAFK.STATUS_IDLE);
afk.on('status:active', listener);
clock.tick(oneSecond);
expect(listener.callCount).to.equal(1);
});
it('should emit a "idle:<time>" event when user has been idle for the specified amount of time', () => {
const oneSecond = 1000;
const inactivityDuration = oneSecond * 2;
const clock = sandbox.useFakeTimers();
const listener = sandbox.stub();
const getIdleTimeStub = sandbox.stub(desktopIdle, 'getIdleTime');
// getIdleTime will be called 5 times in one second intervals
for (let i = 0; i <= 4; i += 1) {
getIdleTimeStub.onCall(i).returns(i + 1);
}
const afk = new NodeAFK(inactivityDuration, oneSecond);
afk.init();
afk.on('idle:3000', listener);
clock.tick(inactivityDuration); // user is now idle
clock.tick(oneSecond * 3); // "idle:3000" event is emitted
expect(listener.callCount).to.equal(1);
});
it('should emit a "active:<time>" event when user has been active for the specified amount of time', () => {
const oneSecond = 1000;
const inactivityDuration = oneSecond * 2;
const clock = sandbox.useFakeTimers();
const listener = sandbox.stub();
sandbox.stub(desktopIdle, 'getIdleTime').returns(0);
const afk = new NodeAFK(inactivityDuration, oneSecond);
afk.init();
afk.on('active:3000', listener);
clock.tick(oneSecond * 3); // "active:3000" event is emitted
expect(listener.callCount).to.equal(1);
});
it('should emit a "error" event when retrieval of the idle time from the system fails', () => {
const oneSecond = 1000;
const inactivityDuration = oneSecond * 2;
const clock = sandbox.useFakeTimers();
const activeForDurationListener = sandbox.stub();
const errorListener = sandbox.stub();
const getIdleTimeErrorMessage = 'foo';
const getIdleTimeError = new Error(getIdleTimeErrorMessage);
sandbox.stub(desktopIdle, 'getIdleTime')
.onFirstCall()
.throws(getIdleTimeError);
const afk = new NodeAFK(inactivityDuration, oneSecond);
afk.init();
afk.on('active:1000', activeForDurationListener);
afk.on('error', errorListener);
clock.tick(oneSecond * 1); // "active:1000" event would normally be emitted
expect(activeForDurationListener.callCount).to.equal(0);
expect(errorListener.callCount).to.equal(1);
expect(errorListener.firstCall.args[0]).to.be.a('Error');
expect(errorListener.firstCall.args[0]).to.have.property('message', `Failed to retrieve the idle time from the system: ${getIdleTimeErrorMessage}`);
});
it('should unregister a timed event', () => {
const oneSecond = 1000;
const inactivityDuration = oneSecond * 2;
const clock = sandbox.useFakeTimers();
const listenerToKeep = sandbox.stub();
const listenerToRemove = sandbox.stub();
sandbox.stub(desktopIdle, 'getIdleTime').returns(0);
const afk = new NodeAFK(inactivityDuration, oneSecond);
afk.init();
afk.on('active:3000', listenerToKeep);
afk.on('active:3000', listenerToRemove);
clock.tick(oneSecond);
afk.off('active:3000', listenerToRemove);
clock.tick(oneSecond * 2); // "active:3000" event is emitted
expect(listenerToKeep.callCount).to.equal(1);
expect(listenerToRemove.callCount).to.equal(0);
});
it('should only trigger a timed event once per status', () => {
const oneSecond = 1000;
const inactivityDuration = oneSecond * 2;
const clock = sandbox.useFakeTimers();
const listener = sandbox.stub();
sandbox.stub(desktopIdle, 'getIdleTime').returns(0);
const afk = new NodeAFK(inactivityDuration, oneSecond);
afk.init();
// check that the listener is only executed once
afk.on('active:3000', listener);
clock.tick(oneSecond * 3); // "active:3000" event is emitted
clock.tick(oneSecond * 3); // "active:3000" event should not be emitted again
expect(listener.callCount).to.equal(1);
// check that when the status is changed, the listener is only executed once again
afk.setStatus(NodeAFK.STATUS_IDLE);
clock.tick(oneSecond);
afk.setStatus(NodeAFK.STATUS_ACTIVE);
clock.tick(oneSecond * 3); // "active:3000" event is emitted
clock.tick(oneSecond * 3); // "active:3000" event should not be emitted again
expect(listener.callCount).to.equal(2);
});
it('should not update the status if the new status is the same as the current status', () => {
const oneSecond = 1000;
const inactivityDuration = oneSecond * 2;
const clock = sandbox.useFakeTimers();
const afk = new NodeAFK(inactivityDuration);
// we can check that setStatus did not do anything by checking that the userLastActiveAt
// value is not updated
const { userLastActiveAt } = afk;
clock.tick(oneSecond);
afk.setStatus(NodeAFK.STATUS_ACTIVE); // the current status is already `active`
expect(afk.userLastActiveAt).to.equal(userLastActiveAt);
});
});