@@ -7,10 +7,10 @@ import type { MongoshBus } from '@mongosh/types';
7
7
import type { Writable } from 'stream' ;
8
8
import type { MongoshLoggingAndTelemetry } from '.' ;
9
9
import { setupLoggingAndTelemetry } from '.' ;
10
+ import type { LoggingAndTelemetry } from './logging-and-telemetry' ;
10
11
import { getDeviceId } from './logging-and-telemetry' ;
11
12
import sinon from 'sinon' ;
12
13
import type { MongoshLoggingAndTelemetryArguments } from './types' ;
13
- import { eventually } from '../../../testing/eventually' ;
14
14
15
15
describe ( 'MongoshLoggingAndTelemetry' , function ( ) {
16
16
let logOutput : any [ ] ;
@@ -86,9 +86,11 @@ describe('MongoshLoggingAndTelemetry', function () {
86
86
expect ( ( ) => loggingAndTelemetry . attachLogger ( logger ) ) . does . not . throw ( ) ;
87
87
} ) ;
88
88
89
- it ( 'tracks new local connection events' , function ( ) {
89
+ it ( 'tracks new local connection events' , async function ( ) {
90
90
loggingAndTelemetry . attachLogger ( logger ) ;
91
91
92
+ await ( loggingAndTelemetry as LoggingAndTelemetry ) . setupTelemetryPromise ;
93
+
92
94
expect ( logOutput ) . to . have . lengthOf ( 0 ) ;
93
95
expect ( analyticsOutput ) . to . be . empty ;
94
96
@@ -142,8 +144,9 @@ describe('MongoshLoggingAndTelemetry', function () {
142
144
] ) ;
143
145
} ) ;
144
146
145
- it ( 'tracks new atlas connection events' , function ( ) {
147
+ it ( 'tracks new atlas connection events' , async function ( ) {
146
148
loggingAndTelemetry . attachLogger ( logger ) ;
149
+ await ( loggingAndTelemetry as LoggingAndTelemetry ) . setupTelemetryPromise ;
147
150
148
151
expect ( logOutput ) . to . have . lengthOf ( 0 ) ;
149
152
expect ( analyticsOutput ) . to . be . empty ;
@@ -203,95 +206,122 @@ describe('MongoshLoggingAndTelemetry', function () {
203
206
} ) ;
204
207
205
208
describe ( 'device ID' , function ( ) {
206
- let loggingAndTelemetry : MongoshLoggingAndTelemetry ;
207
209
let bus : EventEmitter ;
208
210
beforeEach ( function ( ) {
209
211
bus = new EventEmitter ( ) ;
210
- loggingAndTelemetry = setupLoggingAndTelemetry ( {
212
+ } ) ;
213
+
214
+ afterEach ( function ( ) {
215
+ sinon . restore ( ) ;
216
+ } ) ;
217
+
218
+ it ( 'uses device ID "unknown" and logs error if it fails to resolve it' , async function ( ) {
219
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
220
+ sinon . stub ( require ( 'child_process' ) , 'exec' ) . throws ( new Error ( 'Test' ) ) ;
221
+ const loggingAndTelemetry = setupLoggingAndTelemetry ( {
211
222
...testLoggingArguments ,
212
223
bus,
213
224
deviceId : undefined ,
214
225
} ) ;
215
- } ) ;
216
-
217
- it ( 'uses device ID "unknown" if it fails to resolve it' , async function ( ) {
218
226
loggingAndTelemetry . attachLogger ( logger ) ;
219
- // eslint-disable-next-line @typescript-eslint/no-var-requires
220
- sinon . stub ( require ( 'child_process' ) , 'exec' ) . throws ( ) ;
227
+ await ( loggingAndTelemetry as LoggingAndTelemetry ) . setupTelemetryPromise ;
221
228
222
229
bus . emit ( 'mongosh:new-user' , { userId, anonymousId : userId } ) ;
223
230
224
- await eventually ( ( ) => {
225
- expect ( analyticsOutput ) . deep . equal ( [
226
- [
227
- 'identify' ,
228
- {
229
- deviceId : undefined ,
230
- anonymousId : userId ,
231
- traits : {
232
- platform : process . platform ,
233
- arch : process . arch ,
234
- session_id : logId ,
235
- } ,
236
- } ,
237
- ] ,
238
- [
239
- 'identify' ,
240
- {
241
- deviceId : 'unknown' ,
242
- anonymousId : userId ,
243
- traits : {
244
- platform : process . platform ,
245
- arch : process . arch ,
246
- session_id : logId ,
247
- } ,
231
+ expect ( analyticsOutput ) . deep . equal ( [
232
+ [
233
+ 'identify' ,
234
+ {
235
+ deviceId : 'unknown' ,
236
+ anonymousId : userId ,
237
+ traits : {
238
+ platform : process . platform ,
239
+ arch : process . arch ,
240
+ session_id : logId ,
248
241
} ,
249
- ] ,
250
- ] ) ;
242
+ } ,
243
+ ] ,
244
+ ] ) ;
245
+ expect ( logOutput [ 0 ] ) . contains ( {
246
+ c : 'MONGOSH' ,
247
+ id : 1000000006 ,
248
+ ctx : 'telemetry' ,
249
+ msg : 'Error: Test' ,
251
250
} ) ;
252
- sinon . restore ( ) ;
253
251
} ) ;
254
252
255
253
it ( 'automatically sets up device ID for telemetry' , async function ( ) {
254
+ const loggingAndTelemetry = setupLoggingAndTelemetry ( {
255
+ ...testLoggingArguments ,
256
+ bus,
257
+ deviceId : undefined ,
258
+ } ) ;
259
+
256
260
loggingAndTelemetry . attachLogger ( logger ) ;
257
261
258
262
bus . emit ( 'mongosh:new-user' , { userId, anonymousId : userId } ) ;
259
263
260
264
const deviceId = await getDeviceId ( ) ;
261
265
262
- await eventually ( ( ) => {
263
- expect ( analyticsOutput ) . deep . equal ( [
264
- [
265
- 'identify' ,
266
- {
267
- deviceId : undefined ,
268
- anonymousId : userId ,
269
- traits : {
270
- platform : process . platform ,
271
- arch : process . arch ,
272
- session_id : logId ,
273
- } ,
266
+ await ( loggingAndTelemetry as LoggingAndTelemetry ) . setupTelemetryPromise ;
267
+
268
+ expect ( analyticsOutput ) . deep . equal ( [
269
+ [
270
+ 'identify' ,
271
+ {
272
+ deviceId ,
273
+ anonymousId : userId ,
274
+ traits : {
275
+ platform : process . platform ,
276
+ arch : process . arch ,
277
+ session_id : logId ,
274
278
} ,
275
- ] ,
276
- [
277
- 'identify' ,
278
- {
279
- deviceId,
280
- anonymousId : userId ,
281
- traits : {
282
- platform : process . platform ,
283
- arch : process . arch ,
284
- session_id : logId ,
285
- } ,
286
- } ,
287
- ] ,
288
- ] ) ;
279
+ } ,
280
+ ] ,
281
+ ] ) ;
282
+ } ) ;
283
+
284
+ it ( 'only delays analytic outputs, not logging' , async function ( ) {
285
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
286
+ let resolveTelemetry : ( value : unknown ) => void = ( ) => { } ;
287
+ const delayedTelemetry = new Promise ( ( resolve ) => {
288
+ resolveTelemetry = ( value ) => resolve ( value ) ;
289
289
} ) ;
290
+ sinon
291
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
292
+ . stub ( require ( 'node-machine-id' ) , 'machineId' )
293
+ . resolves ( delayedTelemetry ) ;
294
+
295
+ const loggingAndTelemetry = setupLoggingAndTelemetry ( {
296
+ ...testLoggingArguments ,
297
+ bus,
298
+ deviceId : undefined ,
299
+ } ) ;
300
+
301
+ loggingAndTelemetry . attachLogger ( logger ) ;
302
+
303
+ // This event has both analytics and logging
304
+ bus . emit ( 'mongosh:use' , { db : '' } ) ;
305
+
306
+ expect ( logOutput ) . to . have . lengthOf ( 1 ) ;
307
+ expect ( analyticsOutput ) . to . have . lengthOf ( 0 ) ;
308
+
309
+ resolveTelemetry ( '1234' ) ;
310
+ await ( loggingAndTelemetry as LoggingAndTelemetry ) . setupTelemetryPromise ;
311
+
312
+ expect ( logOutput ) . to . have . lengthOf ( 1 ) ;
313
+ expect ( analyticsOutput ) . to . have . lengthOf ( 1 ) ;
314
+
315
+ // Hash created from machine ID 1234
316
+ expect ( analyticsOutput [ 0 ] [ 1 ] . deviceId ) . equals (
317
+ '8c9f929608f0ef13bfd5a290e0233f283e2cc25ffefc2ad8d9ef0650eb224a52'
318
+ ) ;
290
319
} ) ;
291
320
} ) ;
292
321
293
- it ( 'detaching logger leads to no logging but persists analytics' , function ( ) {
322
+ it ( 'detaching logger leads to no logging but persists analytics' , async function ( ) {
294
323
loggingAndTelemetry . attachLogger ( logger ) ;
324
+ await ( loggingAndTelemetry as LoggingAndTelemetry ) . setupTelemetryPromise ;
295
325
296
326
expect ( logOutput ) . to . have . lengthOf ( 0 ) ;
297
327
expect ( analyticsOutput ) . to . have . lengthOf ( 0 ) ;
@@ -328,7 +358,7 @@ describe('MongoshLoggingAndTelemetry', function () {
328
358
expect ( logOutput ) . to . have . lengthOf ( 4 ) ;
329
359
} ) ;
330
360
331
- it ( 'detaching logger mid-way leads to no logging but persists analytics' , function ( ) {
361
+ it ( 'detaching logger mid-way leads to no logging but persists analytics' , async function ( ) {
332
362
loggingAndTelemetry . attachLogger ( logger ) ;
333
363
334
364
expect ( logOutput ) . to . have . lengthOf ( 0 ) ;
@@ -338,17 +368,21 @@ describe('MongoshLoggingAndTelemetry', function () {
338
368
bus . emit ( 'mongosh:use' , { db : '' } ) ;
339
369
340
370
expect ( logOutput ) . to . have . lengthOf ( 1 ) ;
371
+
372
+ await ( loggingAndTelemetry as LoggingAndTelemetry ) . setupTelemetryPromise ;
341
373
expect ( analyticsOutput ) . to . have . lengthOf ( 1 ) ;
342
374
343
375
loggingAndTelemetry . detachLogger ( ) ;
344
376
345
377
bus . emit ( 'mongosh:use' , { db : '' } ) ;
346
378
347
379
expect ( logOutput ) . to . have . lengthOf ( 1 ) ;
380
+
381
+ await ( loggingAndTelemetry as LoggingAndTelemetry ) . setupTelemetryPromise ;
348
382
expect ( analyticsOutput ) . to . have . lengthOf ( 2 ) ;
349
383
} ) ;
350
384
351
- it ( 'detaching logger is recoverable' , function ( ) {
385
+ it ( 'detaching logger is recoverable' , async function ( ) {
352
386
loggingAndTelemetry . attachLogger ( logger ) ;
353
387
354
388
expect ( logOutput ) . to . have . lengthOf ( 0 ) ;
@@ -358,25 +392,32 @@ describe('MongoshLoggingAndTelemetry', function () {
358
392
bus . emit ( 'mongosh:use' , { db : '' } ) ;
359
393
360
394
expect ( logOutput ) . to . have . lengthOf ( 1 ) ;
395
+ await ( loggingAndTelemetry as LoggingAndTelemetry ) . setupTelemetryPromise ;
396
+
361
397
expect ( analyticsOutput ) . to . have . lengthOf ( 1 ) ;
362
398
363
399
loggingAndTelemetry . detachLogger ( ) ;
364
400
365
401
bus . emit ( 'mongosh:use' , { db : '' } ) ;
366
402
367
403
expect ( logOutput ) . to . have . lengthOf ( 1 ) ;
404
+
405
+ await ( loggingAndTelemetry as LoggingAndTelemetry ) . setupTelemetryPromise ;
368
406
expect ( analyticsOutput ) . to . have . lengthOf ( 2 ) ;
369
407
370
408
loggingAndTelemetry . attachLogger ( logger ) ;
371
409
372
410
bus . emit ( 'mongosh:use' , { db : '' } ) ;
373
411
374
412
expect ( logOutput ) . to . have . lengthOf ( 2 ) ;
413
+
414
+ await ( loggingAndTelemetry as LoggingAndTelemetry ) . setupTelemetryPromise ;
375
415
expect ( analyticsOutput ) . to . have . lengthOf ( 3 ) ;
376
416
} ) ;
377
417
378
- it ( 'tracks a sequence of events' , function ( ) {
418
+ it ( 'tracks a sequence of events' , async function ( ) {
379
419
loggingAndTelemetry . attachLogger ( logger ) ;
420
+ await ( loggingAndTelemetry as LoggingAndTelemetry ) . setupTelemetryPromise ;
380
421
381
422
expect ( logOutput ) . to . have . lengthOf ( 0 ) ;
382
423
expect ( analyticsOutput ) . to . be . empty ;
@@ -677,7 +718,7 @@ describe('MongoshLoggingAndTelemetry', function () {
677
718
[
678
719
'identify' ,
679
720
{
680
- anonymousId : '53defe995fa47e6c13102d9d' ,
721
+ anonymousId : userId ,
681
722
deviceId : 'test-device' ,
682
723
traits : {
683
724
platform : process . platform ,
@@ -689,7 +730,7 @@ describe('MongoshLoggingAndTelemetry', function () {
689
730
[
690
731
'identify' ,
691
732
{
692
- anonymousId : '53defe995fa47e6c13102d9d' ,
733
+ anonymousId : userId ,
693
734
deviceId : 'test-device' ,
694
735
traits : {
695
736
platform : process . platform ,
@@ -701,7 +742,7 @@ describe('MongoshLoggingAndTelemetry', function () {
701
742
[
702
743
'track' ,
703
744
{
704
- anonymousId : '53defe995fa47e6c13102d9d' ,
745
+ anonymousId : userId ,
705
746
deviceId : 'test-device' ,
706
747
event : 'Startup Time' ,
707
748
properties : {
@@ -850,8 +891,9 @@ describe('MongoshLoggingAndTelemetry', function () {
850
891
] ) ;
851
892
} ) ;
852
893
853
- it ( 'buffers deprecated API calls' , function ( ) {
894
+ it ( 'buffers deprecated API calls' , async function ( ) {
854
895
loggingAndTelemetry . attachLogger ( logger ) ;
896
+ await ( loggingAndTelemetry as LoggingAndTelemetry ) . setupTelemetryPromise ;
855
897
856
898
expect ( logOutput ) . to . have . lengthOf ( 0 ) ;
857
899
expect ( analyticsOutput ) . to . be . empty ;
@@ -1005,6 +1047,7 @@ describe('MongoshLoggingAndTelemetry', function () {
1005
1047
] ) ;
1006
1048
1007
1049
bus . emit ( 'mongosh:new-user' , { userId, anonymousId : userId } ) ;
1050
+
1008
1051
logOutput = [ ] ;
1009
1052
analyticsOutput = [ ] ;
1010
1053
@@ -1027,6 +1070,7 @@ describe('MongoshLoggingAndTelemetry', function () {
1027
1070
class : 'Database' ,
1028
1071
method : 'cloneDatabase' ,
1029
1072
} ) ;
1073
+
1030
1074
expect ( analyticsOutput ) . to . have . lengthOf ( 2 ) ;
1031
1075
} ) ;
1032
1076
@@ -1054,11 +1098,12 @@ describe('MongoshLoggingAndTelemetry', function () {
1054
1098
expect ( analyticsOutput ) . to . be . empty ;
1055
1099
} ) ;
1056
1100
1057
- it ( 'tracks custom logging events' , function ( ) {
1101
+ it ( 'tracks custom logging events' , async function ( ) {
1058
1102
expect ( logOutput ) . to . have . lengthOf ( 0 ) ;
1059
1103
expect ( analyticsOutput ) . to . be . empty ;
1060
1104
1061
1105
loggingAndTelemetry . attachLogger ( logger ) ;
1106
+ await ( loggingAndTelemetry as LoggingAndTelemetry ) . setupTelemetryPromise ;
1062
1107
1063
1108
bus . emit ( 'mongosh:connect' , {
1064
1109
uri : 'mongodb://localhost/' ,
0 commit comments