@forge/storage 1.5.15 → 1.6.0-next.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,8 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const errors_1 = require("../errors");
4
4
  const global_storage_1 = require("../global-storage");
5
5
  const gql_queries_1 = require("../gql-queries");
6
+ const mocks_1 = require("@atlassian/metrics-interface/dist/mocks");
6
7
  const contextAri = 'app-ari';
7
- const getStorage = (apiClientMock) => new global_storage_1.GlobalStorage(() => contextAri, apiClientMock);
8
+ const getStorage = (apiClientMock, metrics) => new global_storage_1.GlobalStorage(() => contextAri, apiClientMock, () => metrics);
8
9
  const getApiClientMock = (response, statusCode = 200) => {
9
10
  return jest.fn().mockReturnValue({
10
11
  ok: statusCode === 200,
@@ -12,6 +13,36 @@ const getApiClientMock = (response, statusCode = 200) => {
12
13
  text: jest.fn().mockResolvedValue(JSON.stringify(response))
13
14
  });
14
15
  };
16
+ const getMetricMock = () => {
17
+ const mockMetrics = new mocks_1.MockMetrics();
18
+ const mockCounter = new mocks_1.MockCounter('');
19
+ const mockTiming = new mocks_1.MockTiming('');
20
+ const mockStopTiming = jest.fn();
21
+ const mockMeasure = {
22
+ stop: mockStopTiming
23
+ };
24
+ mockMetrics.counter.mockReturnValue(mockCounter);
25
+ mockMetrics.timing.mockReturnValue(mockTiming);
26
+ mockTiming.measure.mockReturnValue(mockMeasure);
27
+ return {
28
+ mockMetrics,
29
+ mockCounter,
30
+ mockStopTiming
31
+ };
32
+ };
33
+ const checkMetricsFired = ({ mockMetrics, mockCounter, mockStopTiming }, { encrypted, ...restTags }, success) => {
34
+ expect(mockMetrics.counter).toHaveBeenCalledWith('forge.runtime.storage.operation', {
35
+ ...restTags,
36
+ encrypted: String(encrypted),
37
+ success: String(success)
38
+ });
39
+ expect(mockCounter.incr).toHaveBeenCalled();
40
+ expect(mockMetrics.timing).toHaveBeenCalledWith('forge.runtime.storage.operation.latency', {
41
+ ...restTags,
42
+ encrypted: String(encrypted)
43
+ });
44
+ expect(mockStopTiming).toHaveBeenCalledWith({ success: String(success) });
45
+ };
15
46
  const getApiClientMockInvalidJson = (response, statusCode = 200) => {
16
47
  return jest.fn().mockReturnValue({
17
48
  ok: statusCode === 200,
@@ -25,7 +56,10 @@ const INVALID_CURSOR_ERROR = {
25
56
  errorType: 'INVALID_CURSOR'
26
57
  }
27
58
  };
28
- describe('GlobalStorage', () => {
59
+ describe.each([
60
+ ['with metrics', true],
61
+ ['no metrics', false]
62
+ ])('GlobalStorage - %s', (_, passMetrics) => {
29
63
  function verifyApiClientCalledWith(apiClientMock, variables, query) {
30
64
  expect(apiClientMock).toHaveBeenCalledWith('/forge/entities/graphql', expect.objectContaining({
31
65
  method: 'POST',
@@ -52,7 +86,8 @@ describe('GlobalStorage', () => {
52
86
  }
53
87
  }
54
88
  });
55
- const globalStorage = getStorage(apiClientMock);
89
+ const metricMocks = getMetricMock();
90
+ const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
56
91
  const returnedValue = await globalStorage.get('testKey');
57
92
  verifyApiClientCalledWith(apiClientMock, {
58
93
  contextAri,
@@ -60,6 +95,9 @@ describe('GlobalStorage', () => {
60
95
  encrypted: false
61
96
  });
62
97
  expect(returnedValue).toEqual('testValue');
98
+ passMetrics
99
+ ? checkMetricsFired(metricMocks, { store: 'untyped', operation: 'get', encrypted: false }, true)
100
+ : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
63
101
  });
64
102
  it('should call the storage API, passing the provided key and returning undefined if the key doesnt exist', async () => {
65
103
  const apiClientMock = getApiClientMock({
@@ -69,7 +107,8 @@ describe('GlobalStorage', () => {
69
107
  }
70
108
  }
71
109
  });
72
- const globalStorage = getStorage(apiClientMock);
110
+ const metricMocks = getMetricMock();
111
+ const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
73
112
  const returnedValue = await globalStorage.get('testKey');
74
113
  verifyApiClientCalledWith(apiClientMock, {
75
114
  contextAri,
@@ -77,6 +116,9 @@ describe('GlobalStorage', () => {
77
116
  encrypted: false
78
117
  });
79
118
  expect(returnedValue).toEqual(undefined);
119
+ passMetrics
120
+ ? checkMetricsFired(metricMocks, { store: 'untyped', operation: 'get', encrypted: false }, true)
121
+ : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
80
122
  });
81
123
  it('should call the storage API, passing the provided key and returning the stored falsey value 0', async () => {
82
124
  const apiClientMock = getApiClientMock({
@@ -86,7 +128,8 @@ describe('GlobalStorage', () => {
86
128
  }
87
129
  }
88
130
  });
89
- const globalStorage = getStorage(apiClientMock);
131
+ const metricMocks = getMetricMock();
132
+ const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
90
133
  const returnedValue = await globalStorage.get('testKey');
91
134
  verifyApiClientCalledWith(apiClientMock, {
92
135
  contextAri,
@@ -94,6 +137,9 @@ describe('GlobalStorage', () => {
94
137
  encrypted: false
95
138
  });
96
139
  expect(returnedValue).toEqual(0);
140
+ passMetrics
141
+ ? checkMetricsFired(metricMocks, { store: 'untyped', operation: 'get', encrypted: false }, true)
142
+ : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
97
143
  });
98
144
  it('should call the storage API, passing the provided key and returning the stored empty string', async () => {
99
145
  const apiClientMock = getApiClientMock({
@@ -103,7 +149,8 @@ describe('GlobalStorage', () => {
103
149
  }
104
150
  }
105
151
  });
106
- const globalStorage = getStorage(apiClientMock);
152
+ const metricMocks = getMetricMock();
153
+ const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
107
154
  const returnedValue = await globalStorage.get('testKey');
108
155
  verifyApiClientCalledWith(apiClientMock, {
109
156
  contextAri,
@@ -111,29 +158,44 @@ describe('GlobalStorage', () => {
111
158
  encrypted: false
112
159
  });
113
160
  expect(returnedValue).toEqual('');
161
+ passMetrics
162
+ ? checkMetricsFired(metricMocks, { store: 'untyped', operation: 'get', encrypted: false }, true)
163
+ : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
114
164
  });
115
165
  it('should throw an error with the returned status for non-200 status codes', async () => {
116
166
  const apiClientMock = getApiClientMock(undefined, 400);
117
- const globalStorage = getStorage(apiClientMock);
167
+ const metricMocks = getMetricMock();
168
+ const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
118
169
  const response = globalStorage.get('testKey');
119
170
  expect(apiClientMock).toHaveBeenCalled();
120
171
  await expect(response).rejects.toThrow(errors_1.APIError.forStatus(400));
172
+ passMetrics
173
+ ? checkMetricsFired(metricMocks, { store: 'untyped', operation: 'get', encrypted: false }, false)
174
+ : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
121
175
  });
122
176
  it('should throw an error with the returned error message for failed responses', async () => {
123
177
  const apiClientMock = getApiClientMock({
124
178
  errors: [INVALID_CURSOR_ERROR]
125
179
  }, 200);
126
- const globalStorage = getStorage(apiClientMock);
180
+ const metricMocks = getMetricMock();
181
+ const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
127
182
  const response = globalStorage.get('testKey');
128
183
  expect(apiClientMock).toHaveBeenCalled();
129
184
  await expect(response).rejects.toThrow(errors_1.APIError.forErrorCode('CURSOR_INVALID', 'error message'));
185
+ passMetrics
186
+ ? checkMetricsFired(metricMocks, { store: 'untyped', operation: 'get', encrypted: false }, false)
187
+ : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
130
188
  });
131
189
  it('should throw an error if the response is not a valid JSON', async () => {
132
190
  const apiClientMock = getApiClientMockInvalidJson('test', 200);
133
- const globalStorage = getStorage(apiClientMock);
191
+ const metricMocks = getMetricMock();
192
+ const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
134
193
  const response = globalStorage.get('testKey');
135
194
  expect(apiClientMock).toHaveBeenCalled();
136
195
  await expect(response).rejects.toThrow(errors_1.APIError.forUnexpected('Response text was not a valid JSON: test'));
196
+ passMetrics
197
+ ? checkMetricsFired(metricMocks, { store: 'untyped', operation: 'get', encrypted: false }, false)
198
+ : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
137
199
  });
138
200
  });
139
201
  describe('get secret', () => {
@@ -145,7 +207,8 @@ describe('GlobalStorage', () => {
145
207
  }
146
208
  }
147
209
  });
148
- const globalStorage = getStorage(apiClientMock);
210
+ const metricMocks = getMetricMock();
211
+ const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
149
212
  const returnedValue = await globalStorage.getSecret('testKey');
150
213
  verifyApiClientCalledWith(apiClientMock, {
151
214
  contextAri,
@@ -153,6 +216,9 @@ describe('GlobalStorage', () => {
153
216
  encrypted: true
154
217
  });
155
218
  expect(returnedValue).toEqual('testValue');
219
+ passMetrics
220
+ ? checkMetricsFired(metricMocks, { store: 'untyped', operation: 'get', encrypted: true }, true)
221
+ : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
156
222
  });
157
223
  });
158
224
  describe('set', () => {
@@ -166,7 +232,8 @@ describe('GlobalStorage', () => {
166
232
  }
167
233
  }
168
234
  });
169
- const globalStorage = getStorage(apiClientMock);
235
+ const metricMocks = getMetricMock();
236
+ const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
170
237
  await globalStorage.set('testKey', 'testValue');
171
238
  verifyApiClientCalledWith(apiClientMock, {
172
239
  input: {
@@ -176,6 +243,9 @@ describe('GlobalStorage', () => {
176
243
  encrypted: false
177
244
  }
178
245
  });
246
+ passMetrics
247
+ ? checkMetricsFired(metricMocks, { store: 'untyped', operation: 'set', encrypted: false }, true)
248
+ : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
179
249
  });
180
250
  it('should throw an error if the storage API returns successful = false', async () => {
181
251
  const apiClientMock = getApiClientMock({
@@ -188,17 +258,25 @@ describe('GlobalStorage', () => {
188
258
  }
189
259
  }
190
260
  });
191
- const globalStorage = getStorage(apiClientMock);
261
+ const metricMocks = getMetricMock();
262
+ const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
192
263
  const response = globalStorage.set('testKey', 'testValue');
193
264
  expect(apiClientMock).toHaveBeenCalled();
194
265
  await expect(response).rejects.toThrow(errors_1.APIError.forErrorCode('INVALID_CURSOR', 'error message'));
266
+ passMetrics
267
+ ? checkMetricsFired(metricMocks, { store: 'untyped', operation: 'set', encrypted: false }, false)
268
+ : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
195
269
  });
196
270
  it('should throw an error if the storage API returns a non 200 status code', async () => {
197
271
  const apiClientMock = getApiClientMockInvalidJson('', 400);
198
- const globalStorage = getStorage(apiClientMock);
272
+ const metricMocks = getMetricMock();
273
+ const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
199
274
  const response = globalStorage.set('testKey', 'testValue');
200
275
  expect(apiClientMock).toHaveBeenCalled();
201
276
  await expect(response).rejects.toThrow(errors_1.APIError.forStatus(400));
277
+ passMetrics
278
+ ? checkMetricsFired(metricMocks, { store: 'untyped', operation: 'set', encrypted: false }, false)
279
+ : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
202
280
  });
203
281
  it('should throw a 500 error if success=false but no errors were returned', async () => {
204
282
  const apiClientMock = getApiClientMock({
@@ -210,7 +288,8 @@ describe('GlobalStorage', () => {
210
288
  }
211
289
  }
212
290
  });
213
- const globalStorage = getStorage(apiClientMock);
291
+ const metricMocks = getMetricMock();
292
+ const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
214
293
  await expect(globalStorage.set('testKey', 'testValue')).rejects.toThrow(errors_1.APIError.forStatus(500));
215
294
  verifyApiClientCalledWith(apiClientMock, {
216
295
  input: {
@@ -220,6 +299,9 @@ describe('GlobalStorage', () => {
220
299
  encrypted: false
221
300
  }
222
301
  });
302
+ passMetrics
303
+ ? checkMetricsFired(metricMocks, { store: 'untyped', operation: 'set', encrypted: false }, false)
304
+ : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
223
305
  });
224
306
  });
225
307
  describe('set secret', () => {
@@ -233,7 +315,8 @@ describe('GlobalStorage', () => {
233
315
  }
234
316
  }
235
317
  });
236
- const globalStorage = getStorage(apiClientMock);
318
+ const metricMocks = getMetricMock();
319
+ const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
237
320
  await globalStorage.setSecret('testKey', 'testValue');
238
321
  verifyApiClientCalledWith(apiClientMock, {
239
322
  input: {
@@ -243,6 +326,9 @@ describe('GlobalStorage', () => {
243
326
  encrypted: true
244
327
  }
245
328
  });
329
+ passMetrics
330
+ ? checkMetricsFired(metricMocks, { store: 'untyped', operation: 'set', encrypted: true }, true)
331
+ : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
246
332
  });
247
333
  });
248
334
  describe('delete', () => {
@@ -256,7 +342,8 @@ describe('GlobalStorage', () => {
256
342
  }
257
343
  }
258
344
  });
259
- const globalStorage = getStorage(apiClientMock);
345
+ const metricMocks = getMetricMock();
346
+ const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
260
347
  await globalStorage.delete('testKey');
261
348
  verifyApiClientCalledWith(apiClientMock, {
262
349
  input: {
@@ -265,6 +352,9 @@ describe('GlobalStorage', () => {
265
352
  encrypted: false
266
353
  }
267
354
  });
355
+ passMetrics
356
+ ? checkMetricsFired(metricMocks, { store: 'untyped', operation: 'delete', encrypted: false }, true)
357
+ : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
268
358
  });
269
359
  it('should throw an error if the storage API returns successful = false', async () => {
270
360
  const apiClientMock = getApiClientMock({
@@ -277,17 +367,25 @@ describe('GlobalStorage', () => {
277
367
  }
278
368
  }
279
369
  });
280
- const globalStorage = getStorage(apiClientMock);
370
+ const metricMocks = getMetricMock();
371
+ const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
281
372
  const response = globalStorage.delete('testKey');
282
373
  expect(apiClientMock).toHaveBeenCalled();
283
374
  await expect(response).rejects.toThrow(errors_1.APIError.forErrorCode('CURSOR_INVALID', 'error message'));
375
+ passMetrics
376
+ ? checkMetricsFired(metricMocks, { store: 'untyped', operation: 'delete', encrypted: false }, false)
377
+ : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
284
378
  });
285
379
  it('should throw an error if the storage API returns a non 200 status code and has no body', async () => {
286
380
  const apiClientMock = getApiClientMockInvalidJson('', 400);
287
- const globalStorage = getStorage(apiClientMock);
381
+ const metricMocks = getMetricMock();
382
+ const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
288
383
  const response = globalStorage.delete('testKey');
289
384
  expect(apiClientMock).toHaveBeenCalled();
290
385
  await expect(response).rejects.toThrow(errors_1.APIError.forStatus(400));
386
+ passMetrics
387
+ ? checkMetricsFired(metricMocks, { store: 'untyped', operation: 'delete', encrypted: false }, false)
388
+ : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
291
389
  });
292
390
  });
293
391
  describe('delete secret', () => {
@@ -301,7 +399,8 @@ describe('GlobalStorage', () => {
301
399
  }
302
400
  }
303
401
  });
304
- const globalStorage = getStorage(apiClientMock);
402
+ const metricMocks = getMetricMock();
403
+ const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
305
404
  await globalStorage.deleteSecret('testKey');
306
405
  verifyApiClientCalledWith(apiClientMock, {
307
406
  input: {
@@ -310,6 +409,9 @@ describe('GlobalStorage', () => {
310
409
  encrypted: true
311
410
  }
312
411
  });
412
+ passMetrics
413
+ ? checkMetricsFired(metricMocks, { store: 'untyped', operation: 'delete', encrypted: true }, true)
414
+ : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
313
415
  });
314
416
  });
315
417
  describe('getEntity', () => {
@@ -321,7 +423,8 @@ describe('GlobalStorage', () => {
321
423
  }
322
424
  }
323
425
  });
324
- const globalStorage = getStorage(apiClientMock);
426
+ const metricMocks = getMetricMock();
427
+ const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
325
428
  const returnedValue = await globalStorage.getEntity('testEntityName', 'testEntityKey');
326
429
  verifyApiClientCalledWith(apiClientMock, {
327
430
  contextAri,
@@ -329,6 +432,9 @@ describe('GlobalStorage', () => {
329
432
  key: 'testEntityKey'
330
433
  });
331
434
  expect(returnedValue).toEqual('testValue');
435
+ passMetrics
436
+ ? checkMetricsFired(metricMocks, { store: 'typed', operation: 'get', encrypted: false }, true)
437
+ : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
332
438
  });
333
439
  it('should call the storage API, passing the provided entity key and returning undefined if the key doesnt exist', async () => {
334
440
  const apiClientMock = getApiClientMock({
@@ -338,7 +444,8 @@ describe('GlobalStorage', () => {
338
444
  }
339
445
  }
340
446
  });
341
- const globalStorage = getStorage(apiClientMock);
447
+ const metricMocks = getMetricMock();
448
+ const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
342
449
  const returnedValue = await globalStorage.getEntity('testEntityName', 'testEntityKey');
343
450
  verifyApiClientCalledWith(apiClientMock, {
344
451
  contextAri,
@@ -346,6 +453,9 @@ describe('GlobalStorage', () => {
346
453
  key: 'testEntityKey'
347
454
  });
348
455
  expect(returnedValue).toEqual(undefined);
456
+ passMetrics
457
+ ? checkMetricsFired(metricMocks, { store: 'typed', operation: 'get', encrypted: false }, true)
458
+ : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
349
459
  });
350
460
  it('should call the storage API, passing the provided key and returning the stored falsey value 0', async () => {
351
461
  const apiClientMock = getApiClientMock({
@@ -355,7 +465,8 @@ describe('GlobalStorage', () => {
355
465
  }
356
466
  }
357
467
  });
358
- const globalStorage = getStorage(apiClientMock);
468
+ const metricMocks = getMetricMock();
469
+ const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
359
470
  const returnedValue = await globalStorage.getEntity('testEntityName', 'testEntityKey');
360
471
  verifyApiClientCalledWith(apiClientMock, {
361
472
  contextAri,
@@ -363,6 +474,9 @@ describe('GlobalStorage', () => {
363
474
  key: 'testEntityKey'
364
475
  });
365
476
  expect(returnedValue).toEqual(0);
477
+ passMetrics
478
+ ? checkMetricsFired(metricMocks, { store: 'typed', operation: 'get', encrypted: false }, true)
479
+ : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
366
480
  });
367
481
  it('should call the storage API, passing the provided key and returning the stored empty string', async () => {
368
482
  const apiClientMock = getApiClientMock({
@@ -372,7 +486,8 @@ describe('GlobalStorage', () => {
372
486
  }
373
487
  }
374
488
  });
375
- const globalStorage = getStorage(apiClientMock);
489
+ const metricMocks = getMetricMock();
490
+ const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
376
491
  const returnedValue = await globalStorage.getEntity('testEntityName', 'testEntityKey');
377
492
  verifyApiClientCalledWith(apiClientMock, {
378
493
  contextAri,
@@ -380,29 +495,44 @@ describe('GlobalStorage', () => {
380
495
  key: 'testEntityKey'
381
496
  });
382
497
  expect(returnedValue).toEqual('');
498
+ passMetrics
499
+ ? checkMetricsFired(metricMocks, { store: 'typed', operation: 'get', encrypted: false }, true)
500
+ : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
383
501
  });
384
502
  it('should throw an error with the returned status for non-200 status codes', async () => {
385
503
  const apiClientMock = getApiClientMock(undefined, 400);
386
- const globalStorage = getStorage(apiClientMock);
504
+ const metricMocks = getMetricMock();
505
+ const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
387
506
  const response = globalStorage.getEntity('testEntityName', 'testEntityKey');
388
507
  expect(apiClientMock).toHaveBeenCalled();
389
508
  await expect(response).rejects.toThrow(errors_1.APIError.forStatus(400));
509
+ passMetrics
510
+ ? checkMetricsFired(metricMocks, { store: 'typed', operation: 'get', encrypted: false }, false)
511
+ : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
390
512
  });
391
513
  it('should throw an error with the returned error message for failed responses', async () => {
392
514
  const apiClientMock = getApiClientMock({
393
515
  errors: [INVALID_CURSOR_ERROR]
394
516
  }, 200);
395
- const globalStorage = getStorage(apiClientMock);
517
+ const metricMocks = getMetricMock();
518
+ const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
396
519
  const response = globalStorage.getEntity('testEntityName', 'testEntityKey');
397
520
  expect(apiClientMock).toHaveBeenCalled();
398
521
  await expect(response).rejects.toThrow(errors_1.APIError.forErrorCode('CURSOR_INVALID', 'error message'));
522
+ passMetrics
523
+ ? checkMetricsFired(metricMocks, { store: 'typed', operation: 'get', encrypted: false }, false)
524
+ : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
399
525
  });
400
526
  it('should throw an error if the response is not a valid JSON', async () => {
401
527
  const apiClientMock = getApiClientMockInvalidJson('test', 200);
402
- const globalStorage = getStorage(apiClientMock);
528
+ const metricMocks = getMetricMock();
529
+ const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
403
530
  const response = globalStorage.getEntity('testEntityName', 'testEntityKey');
404
531
  expect(apiClientMock).toHaveBeenCalled();
405
532
  await expect(response).rejects.toThrow(errors_1.APIError.forUnexpected('Response text was not a valid JSON: test'));
533
+ passMetrics
534
+ ? checkMetricsFired(metricMocks, { store: 'typed', operation: 'get', encrypted: false }, false)
535
+ : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
406
536
  });
407
537
  });
408
538
  describe('setEntity', () => {
@@ -416,7 +546,8 @@ describe('GlobalStorage', () => {
416
546
  }
417
547
  }
418
548
  });
419
- const globalStorage = getStorage(apiClientMock);
549
+ const metricMocks = getMetricMock();
550
+ const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
420
551
  await globalStorage.setEntity('testEntityName', 'testEntityKey', 'testValue');
421
552
  verifyApiClientCalledWith(apiClientMock, {
422
553
  input: {
@@ -426,6 +557,9 @@ describe('GlobalStorage', () => {
426
557
  value: 'testValue'
427
558
  }
428
559
  });
560
+ passMetrics
561
+ ? checkMetricsFired(metricMocks, { store: 'typed', operation: 'set', encrypted: false }, true)
562
+ : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
429
563
  });
430
564
  it('should throw an error if the storage API returns successful = false', async () => {
431
565
  const apiClientMock = getApiClientMock({
@@ -438,17 +572,25 @@ describe('GlobalStorage', () => {
438
572
  }
439
573
  }
440
574
  });
441
- const globalStorage = getStorage(apiClientMock);
575
+ const metricMocks = getMetricMock();
576
+ const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
442
577
  const response = globalStorage.setEntity('testEntityName', 'testEntityKey', 'testValue');
443
578
  expect(apiClientMock).toHaveBeenCalled();
444
579
  await expect(response).rejects.toThrow(errors_1.APIError.forErrorCode('INVALID_CURSOR', 'error message'));
580
+ passMetrics
581
+ ? checkMetricsFired(metricMocks, { store: 'typed', operation: 'set', encrypted: false }, false)
582
+ : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
445
583
  });
446
584
  it('should throw an error if the storage API returns a non 200 status code', async () => {
447
585
  const apiClientMock = getApiClientMockInvalidJson('', 400);
448
- const globalStorage = getStorage(apiClientMock);
586
+ const metricMocks = getMetricMock();
587
+ const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
449
588
  const response = globalStorage.setEntity('testEntityName', 'testEntityKey', 'testValue');
450
589
  expect(apiClientMock).toHaveBeenCalled();
451
590
  await expect(response).rejects.toThrow(errors_1.APIError.forStatus(400));
591
+ passMetrics
592
+ ? checkMetricsFired(metricMocks, { store: 'typed', operation: 'set', encrypted: false }, false)
593
+ : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
452
594
  });
453
595
  it('should throw a 500 error if success=false but no errors were returned', async () => {
454
596
  const apiClientMock = getApiClientMock({
@@ -460,7 +602,8 @@ describe('GlobalStorage', () => {
460
602
  }
461
603
  }
462
604
  });
463
- const globalStorage = getStorage(apiClientMock);
605
+ const metricMocks = getMetricMock();
606
+ const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
464
607
  await expect(globalStorage.setEntity('testEntityName', 'testEntityKey', 'testValue')).rejects.toThrow(errors_1.APIError.forStatus(500));
465
608
  verifyApiClientCalledWith(apiClientMock, {
466
609
  input: {
@@ -470,6 +613,9 @@ describe('GlobalStorage', () => {
470
613
  value: 'testValue'
471
614
  }
472
615
  });
616
+ passMetrics
617
+ ? checkMetricsFired(metricMocks, { store: 'typed', operation: 'set', encrypted: false }, false)
618
+ : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
473
619
  });
474
620
  });
475
621
  describe('deleteEntity', () => {
@@ -483,7 +629,8 @@ describe('GlobalStorage', () => {
483
629
  }
484
630
  }
485
631
  });
486
- const globalStorage = getStorage(apiClientMock);
632
+ const metricMocks = getMetricMock();
633
+ const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
487
634
  await globalStorage.deleteEntity('testEntityName', 'testEntityKey');
488
635
  verifyApiClientCalledWith(apiClientMock, {
489
636
  input: {
@@ -492,6 +639,9 @@ describe('GlobalStorage', () => {
492
639
  key: 'testEntityKey'
493
640
  }
494
641
  });
642
+ passMetrics
643
+ ? checkMetricsFired(metricMocks, { store: 'typed', operation: 'delete', encrypted: false }, true)
644
+ : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
495
645
  });
496
646
  it('should throw an error if the storage API returns successful = false', async () => {
497
647
  const apiClientMock = getApiClientMock({
@@ -504,17 +654,25 @@ describe('GlobalStorage', () => {
504
654
  }
505
655
  }
506
656
  });
507
- const globalStorage = getStorage(apiClientMock);
657
+ const metricMocks = getMetricMock();
658
+ const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
508
659
  const response = globalStorage.deleteEntity('testEntityKey', 'testEntityKey');
509
660
  expect(apiClientMock).toHaveBeenCalled();
510
661
  await expect(response).rejects.toThrow(errors_1.APIError.forErrorCode('CURSOR_INVALID', 'error message'));
662
+ passMetrics
663
+ ? checkMetricsFired(metricMocks, { store: 'typed', operation: 'delete', encrypted: false }, false)
664
+ : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
511
665
  });
512
666
  it('should throw an error if the storage API returns a non 200 status code and has no body', async () => {
513
667
  const apiClientMock = getApiClientMockInvalidJson('', 400);
514
- const globalStorage = getStorage(apiClientMock);
668
+ const metricMocks = getMetricMock();
669
+ const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
515
670
  const response = globalStorage.deleteEntity('testEntityKey', 'testEntityKey');
516
671
  expect(apiClientMock).toHaveBeenCalled();
517
672
  await expect(response).rejects.toThrow(errors_1.APIError.forStatus(400));
673
+ passMetrics
674
+ ? checkMetricsFired(metricMocks, { store: 'typed', operation: 'delete', encrypted: false }, false)
675
+ : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
518
676
  });
519
677
  });
520
678
  describe('list', () => {
@@ -529,7 +687,8 @@ describe('GlobalStorage', () => {
529
687
  }
530
688
  }
531
689
  });
532
- const globalStorage = getStorage(apiClientMock);
690
+ const metricMocks = getMetricMock();
691
+ const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
533
692
  const where = [
534
693
  {
535
694
  field: 'key',
@@ -553,6 +712,9 @@ describe('GlobalStorage', () => {
553
712
  ],
554
713
  nextCursor: 'cursor2'
555
714
  }));
715
+ passMetrics
716
+ ? checkMetricsFired(metricMocks, { store: 'untyped', operation: 'query', encrypted: false }, true)
717
+ : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
556
718
  });
557
719
  it('should query the appStoredEntitiesForCleanup endpoint given process.env.IS_CLEANUP_FUNCTION is set to true', async () => {
558
720
  process.env.IS_CLEANUP_FUNCTION = 'true';
@@ -566,7 +728,8 @@ describe('GlobalStorage', () => {
566
728
  }
567
729
  }
568
730
  });
569
- const globalStorage = getStorage(apiClientMock);
731
+ const metricMocks = getMetricMock();
732
+ const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
570
733
  const where = [
571
734
  {
572
735
  field: 'key',
@@ -590,6 +753,9 @@ describe('GlobalStorage', () => {
590
753
  ],
591
754
  nextCursor: 'cursor2'
592
755
  }));
756
+ passMetrics
757
+ ? checkMetricsFired(metricMocks, { store: 'untyped', operation: 'query', encrypted: false }, true)
758
+ : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
593
759
  process.env.IS_CLEANUP_FUNCTION = '';
594
760
  });
595
761
  it('should use default values', async () => {
@@ -600,7 +766,8 @@ describe('GlobalStorage', () => {
600
766
  }
601
767
  }
602
768
  });
603
- const globalStorage = getStorage(apiClientMock);
769
+ const metricMocks = getMetricMock();
770
+ const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
604
771
  await globalStorage.list({});
605
772
  verifyApiClientCalledWith(apiClientMock, {
606
773
  contextAri,
@@ -608,6 +775,9 @@ describe('GlobalStorage', () => {
608
775
  cursor: null,
609
776
  limit: null
610
777
  }, gql_queries_1.UntypedQueries.listQuery(contextAri, {}).query);
778
+ passMetrics
779
+ ? checkMetricsFired(metricMocks, { store: 'untyped', operation: 'query', encrypted: false }, true)
780
+ : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
611
781
  });
612
782
  it('should handle an empty result set', async () => {
613
783
  const apiClientMock = getApiClientMock({
@@ -617,7 +787,8 @@ describe('GlobalStorage', () => {
617
787
  }
618
788
  }
619
789
  });
620
- const globalStorage = getStorage(apiClientMock);
790
+ const metricMocks = getMetricMock();
791
+ const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
621
792
  const where = [
622
793
  {
623
794
  field: 'key',
@@ -630,22 +801,33 @@ describe('GlobalStorage', () => {
630
801
  results: [],
631
802
  nextCursor: undefined
632
803
  }));
804
+ passMetrics
805
+ ? checkMetricsFired(metricMocks, { store: 'untyped', operation: 'query', encrypted: false }, true)
806
+ : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
633
807
  });
634
808
  it('should throw an error if the storage API returns an error', async () => {
635
809
  const apiClientMock = getApiClientMock({
636
810
  errors: [INVALID_CURSOR_ERROR]
637
811
  });
638
- const globalStorage = getStorage(apiClientMock);
812
+ const metricMocks = getMetricMock();
813
+ const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
639
814
  const response = globalStorage.list({});
640
815
  expect(apiClientMock).toHaveBeenCalled();
641
816
  await expect(response).rejects.toThrow(errors_1.APIError.forErrorCode('CURSOR_INVALID', 'error message'));
817
+ passMetrics
818
+ ? checkMetricsFired(metricMocks, { store: 'untyped', operation: 'query', encrypted: false }, false)
819
+ : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
642
820
  });
643
821
  it('should throw an error if the storage API returns a non 200 status code and has no body', async () => {
644
822
  const apiClientMock = getApiClientMockInvalidJson('', 400);
645
- const globalStorage = getStorage(apiClientMock);
823
+ const metricMocks = getMetricMock();
824
+ const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
646
825
  const response = globalStorage.list({});
647
826
  expect(apiClientMock).toHaveBeenCalled();
648
827
  await expect(response).rejects.toThrow(errors_1.APIError.forStatus(400));
828
+ passMetrics
829
+ ? checkMetricsFired(metricMocks, { store: 'untyped', operation: 'query', encrypted: false }, false)
830
+ : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
649
831
  });
650
832
  });
651
833
  describe('listCustomEntities', () => {
@@ -657,7 +839,8 @@ describe('GlobalStorage', () => {
657
839
  }
658
840
  }
659
841
  });
660
- const globalStorage = getStorage(apiClientMock);
842
+ const metricMocks = getMetricMock();
843
+ const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
661
844
  const response = await globalStorage.listCustomEntities({});
662
845
  expect(response).toMatchObject({
663
846
  results: [],
@@ -666,6 +849,9 @@ describe('GlobalStorage', () => {
666
849
  verifyApiClientCalledWith(apiClientMock, {
667
850
  contextAri
668
851
  }, gql_queries_1.CustomEntityQueries.listQuery(contextAri, {}).query);
852
+ passMetrics
853
+ ? checkMetricsFired(metricMocks, { store: 'typed', operation: 'query', encrypted: false }, true)
854
+ : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
669
855
  });
670
856
  it('should return cursor when results are not present', async () => {
671
857
  const apiClientMock = getApiClientMock({
@@ -676,7 +862,8 @@ describe('GlobalStorage', () => {
676
862
  }
677
863
  }
678
864
  });
679
- const globalStorage = getStorage(apiClientMock);
865
+ const metricMocks = getMetricMock();
866
+ const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
680
867
  const response = await globalStorage.listCustomEntities({});
681
868
  expect(response).toMatchObject({
682
869
  results: [],
@@ -685,6 +872,33 @@ describe('GlobalStorage', () => {
685
872
  verifyApiClientCalledWith(apiClientMock, {
686
873
  contextAri
687
874
  }, gql_queries_1.CustomEntityQueries.listQuery(contextAri, {}).query);
875
+ passMetrics
876
+ ? checkMetricsFired(metricMocks, { store: 'typed', operation: 'query', encrypted: false }, true)
877
+ : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
878
+ });
879
+ it('should throw an error if the storage API returns an error', async () => {
880
+ const apiClientMock = getApiClientMock({
881
+ errors: [INVALID_CURSOR_ERROR]
882
+ });
883
+ const metricMocks = getMetricMock();
884
+ const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
885
+ const response = globalStorage.listCustomEntities({});
886
+ expect(apiClientMock).toHaveBeenCalled();
887
+ await expect(response).rejects.toThrow(errors_1.APIError.forErrorCode('CURSOR_INVALID', 'error message'));
888
+ passMetrics
889
+ ? checkMetricsFired(metricMocks, { store: 'typed', operation: 'query', encrypted: false }, false)
890
+ : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
891
+ });
892
+ it('should throw an error if the storage API returns a non 200 status code and has no body', async () => {
893
+ const apiClientMock = getApiClientMockInvalidJson('', 400);
894
+ const metricMocks = getMetricMock();
895
+ const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
896
+ const response = globalStorage.listCustomEntities({});
897
+ expect(apiClientMock).toHaveBeenCalled();
898
+ await expect(response).rejects.toThrow(errors_1.APIError.forStatus(400));
899
+ passMetrics
900
+ ? checkMetricsFired(metricMocks, { store: 'typed', operation: 'query', encrypted: false }, false)
901
+ : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
688
902
  });
689
903
  });
690
904
  });