avo-inspector 3.2.0 → 3.2.3

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.
Files changed (84) hide show
  1. package/README.md +10 -0
  2. package/dist/AvoBatcher.d.ts +9 -3
  3. package/dist/AvoInspector.d.ts +4 -0
  4. package/dist/AvoNetworkCallsHandler.d.ts +80 -5
  5. package/dist/__tests__/UnboundPublicMethods_test.d.ts +1 -0
  6. package/dist/__tests__/constants.d.ts +2 -1
  7. package/dist/__tests__/helpers/internalGateway.d.ts +23 -0
  8. package/dist/eventSpec/AvoEventSpecCache.d.ts +7 -4
  9. package/dist/eventSpec/AvoEventSpecFetchTypes.d.ts +12 -1
  10. package/dist/eventSpec/AvoEventSpecFetcher.d.ts +6 -1
  11. package/dist/index.js +1 -1
  12. package/dist/lite/AvoBatcherLite.d.ts +9 -3
  13. package/dist/lite/AvoBatcherLite.js +46 -7
  14. package/dist/lite/AvoInspectorLite.d.ts +2 -0
  15. package/dist/lite/AvoInspectorLite.js +95 -15
  16. package/dist/lite/AvoNetworkCallsHandlerLite.d.ts +79 -5
  17. package/dist/lite/AvoNetworkCallsHandlerLite.js +300 -20
  18. package/package.json +10 -1
  19. package/dist/AvoBatcher.js +0 -87
  20. package/dist/AvoDeduplicator.js +0 -142
  21. package/dist/AvoEncryption.js +0 -176
  22. package/dist/AvoInspector.js +0 -435
  23. package/dist/AvoNetworkCallsHandler.js +0 -229
  24. package/dist/AvoSchemaParser.js +0 -95
  25. package/dist/AvoStreamId.js +0 -82
  26. package/dist/__mocks__/xhr.js +0 -14
  27. package/dist/__mocks__/xhrError.js +0 -14
  28. package/dist/__tests__/AvoEncryption.js +0 -272
  29. package/dist/__tests__/AvoEncryptionIntegration.js +0 -191
  30. package/dist/__tests__/AvoEventSpecFetcher.js +0 -377
  31. package/dist/__tests__/AvoInspector.js +0 -335
  32. package/dist/__tests__/Batcher.js +0 -214
  33. package/dist/__tests__/BatcherPublic.js +0 -139
  34. package/dist/__tests__/Deduplicator.js +0 -190
  35. package/dist/__tests__/EventSpecCache.js +0 -143
  36. package/dist/__tests__/EventValidator.js +0 -543
  37. package/dist/__tests__/InstallationId.js +0 -60
  38. package/dist/__tests__/NetworkCallsHandler.js +0 -222
  39. package/dist/__tests__/NetworkCallsHandlerModelA.js +0 -206
  40. package/dist/__tests__/Parsing.js +0 -115
  41. package/dist/__tests__/Sessions.d.ts +0 -1
  42. package/dist/__tests__/Sessions.js +0 -10
  43. package/dist/__tests__/Storage.d.ts +0 -1
  44. package/dist/__tests__/Storage.js +0 -173
  45. package/dist/__tests__/StreamId.d.ts +0 -1
  46. package/dist/__tests__/StreamId.js +0 -153
  47. package/dist/__tests__/constants.js +0 -48
  48. package/dist/esm/AvoBatcher.js +0 -75
  49. package/dist/esm/AvoDeduplicator.js +0 -137
  50. package/dist/esm/AvoEncryption.js +0 -277
  51. package/dist/esm/AvoGuid.js +0 -9
  52. package/dist/esm/AvoInspector.js +0 -470
  53. package/dist/esm/AvoInspectorEnv.js +0 -5
  54. package/dist/esm/AvoNetworkCallsHandler.js +0 -181
  55. package/dist/esm/AvoSchemaParser.js +0 -128
  56. package/dist/esm/AvoStorage.js +0 -176
  57. package/dist/esm/AvoStreamId.js +0 -140
  58. package/dist/esm/eventSpec/AvoEventSpecCache.js +0 -165
  59. package/dist/esm/eventSpec/AvoEventSpecFetchTypes.js +0 -6
  60. package/dist/esm/eventSpec/AvoEventSpecFetcher.js +0 -203
  61. package/dist/esm/eventSpec/EventValidator.js +0 -687
  62. package/dist/esm/eventSpec/index.js +0 -4
  63. package/dist/esm/index.js +0 -3
  64. package/dist/esm/utils.js +0 -38
  65. package/dist/esm/version.js +0 -2
  66. package/dist/eventSpec/AvoEventSpecCache.js +0 -181
  67. package/dist/eventSpec/AvoEventSpecFetcher.js +0 -290
  68. package/dist/eventSpec/EventValidator.js +0 -846
  69. package/dist/eventSpec/index.js +0 -20
  70. package/dist/version.d.ts +0 -1
  71. package/dist/version.js +0 -5
  72. /package/dist/__tests__/{AvoEncryption.d.ts → ApiKeyHeaderInjection_test.d.ts} +0 -0
  73. /package/dist/__tests__/{AvoEncryptionIntegration.d.ts → BrowserBootstrap_test.d.ts} +0 -0
  74. /package/dist/__tests__/{AvoEventSpecFetcher.d.ts → EventSpecGatewayCoordinates_test.d.ts} +0 -0
  75. /package/dist/__tests__/{AvoInspector.d.ts → InFlightQueueGaps_test.d.ts} +0 -0
  76. /package/dist/__tests__/{Batcher.d.ts → InFlightQueue_test.d.ts} +0 -0
  77. /package/dist/__tests__/{BatcherPublic.d.ts → NetworkCallsHandlerLite_test.d.ts} +0 -0
  78. /package/dist/__tests__/{Deduplicator.d.ts → PublicSurface_test.d.ts} +0 -0
  79. /package/dist/__tests__/{EventSpecCache.d.ts → TrackOptionsIntegration_test.d.ts} +0 -0
  80. /package/dist/__tests__/{EventValidator.d.ts → TrackOptionsTransport_test.d.ts} +0 -0
  81. /package/dist/__tests__/{InstallationId.d.ts → TrackingHeaders_test.d.ts} +0 -0
  82. /package/dist/__tests__/{NetworkCallsHandler.d.ts → V1WireBaseline_test.d.ts} +0 -0
  83. /package/dist/__tests__/{NetworkCallsHandlerModelA.d.ts → V2QueuedAttribution_test.d.ts} +0 -0
  84. /package/dist/__tests__/{Parsing.d.ts → V2StopSending_test.d.ts} +0 -0
@@ -1,335 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
- Object.defineProperty(exports, "__esModule", { value: true });
39
- var AvoInspector_1 = require("../AvoInspector");
40
- var AvoInspectorEnv_1 = require("../AvoInspectorEnv");
41
- var AvoStreamId_1 = require("../AvoStreamId");
42
- var constants_1 = require("../__tests__/constants");
43
- // Mock global fetch to avoid real network calls
44
- var mockFetch = jest.fn();
45
- global.fetch = mockFetch;
46
- describe("Initialization", function () {
47
- test("Api Key is set", function () {
48
- // Given
49
- var apiKey = "api-key-xxx";
50
- // When
51
- var inspector = new AvoInspector_1.AvoInspector({
52
- env: AvoInspectorEnv_1.AvoInspectorEnv.Prod,
53
- version: "0",
54
- apiKey: apiKey,
55
- });
56
- // Then
57
- expect(inspector.apiKey).toBe(apiKey);
58
- });
59
- test("Error is thrown when Api Key is not set", function () {
60
- // Given
61
- // @ts-ignore
62
- var apiKey;
63
- // Then
64
- expect(function () {
65
- new AvoInspector_1.AvoInspector({
66
- env: AvoInspectorEnv_1.AvoInspectorEnv.Prod,
67
- version: "0",
68
- // @ts-ignore
69
- apiKey: apiKey,
70
- });
71
- }).toThrow(constants_1.error.API_KEY);
72
- });
73
- test("Error is thrown when empty Api Key is used", function () {
74
- // Given
75
- var apiKey = " ";
76
- // Then
77
- expect(function () {
78
- new AvoInspector_1.AvoInspector({
79
- env: AvoInspectorEnv_1.AvoInspectorEnv.Prod,
80
- version: "0",
81
- apiKey: apiKey,
82
- });
83
- }).toThrow(constants_1.error.API_KEY);
84
- });
85
- test("Error is thrown when Api Key is set to null", function () {
86
- // Given
87
- var apiKey = null;
88
- // Then
89
- expect(function () {
90
- new AvoInspector_1.AvoInspector({
91
- env: AvoInspectorEnv_1.AvoInspectorEnv.Prod,
92
- version: "0",
93
- // @ts-ignore
94
- apiKey: apiKey,
95
- });
96
- }).toThrow(constants_1.error.API_KEY);
97
- });
98
- test("Dev environment is used when env is not provided", function () {
99
- // Given
100
- // @ts-ignore
101
- var env;
102
- // When
103
- var inspector = new AvoInspector_1.AvoInspector({
104
- apiKey: "api-key-xxx",
105
- version: "0",
106
- // @ts-ignore
107
- env: env,
108
- });
109
- // Then
110
- expect(inspector.environment).toBe(AvoInspectorEnv_1.AvoInspectorEnv.Dev);
111
- });
112
- test("Dev environment is used when empty string is used", function () {
113
- // Given
114
- var env = "";
115
- // When
116
- var inspector = new AvoInspector_1.AvoInspector({
117
- apiKey: "api-key-xxx",
118
- version: "0",
119
- // @ts-ignore
120
- env: env,
121
- });
122
- // Then
123
- expect(inspector.environment).toBe(AvoInspectorEnv_1.AvoInspectorEnv.Dev);
124
- });
125
- test("Dev env is set using AvoInspectorEnv", function () {
126
- // When
127
- var inspector = new AvoInspector_1.AvoInspector({
128
- apiKey: "api-key-xxx",
129
- env: AvoInspectorEnv_1.AvoInspectorEnv.Dev,
130
- version: "0",
131
- });
132
- // Then
133
- expect(inspector.environment).toBe(AvoInspectorEnv_1.AvoInspectorEnv.Dev);
134
- });
135
- test("Dev environment is set using string", function () {
136
- // When
137
- var inspector = new AvoInspector_1.AvoInspector({
138
- apiKey: "api-key-xxx",
139
- env: "dev",
140
- version: "0",
141
- });
142
- // Then
143
- expect(inspector.environment).toBe(AvoInspectorEnv_1.AvoInspectorEnv.Dev);
144
- });
145
- test("Staging env is set using AvoInspectorEnv", function () {
146
- // When
147
- var inspector = new AvoInspector_1.AvoInspector({
148
- apiKey: "api-key-xxx",
149
- env: AvoInspectorEnv_1.AvoInspectorEnv.Staging,
150
- version: "0",
151
- });
152
- // Then
153
- expect(inspector.environment).toBe(AvoInspectorEnv_1.AvoInspectorEnv.Staging);
154
- });
155
- test("Staging environment is set using string", function () {
156
- // When
157
- var inspector = new AvoInspector_1.AvoInspector({
158
- apiKey: "api-key-xxx",
159
- env: "staging",
160
- version: "0",
161
- });
162
- // Then
163
- expect(inspector.environment).toBe(AvoInspectorEnv_1.AvoInspectorEnv.Staging);
164
- });
165
- test("Prod env is set using AvoInspectorEnv", function () {
166
- // When
167
- var inspector = new AvoInspector_1.AvoInspector({
168
- apiKey: "api-key-xxx",
169
- env: AvoInspectorEnv_1.AvoInspectorEnv.Prod,
170
- version: "0",
171
- });
172
- // Then
173
- expect(inspector.environment).toBe(AvoInspectorEnv_1.AvoInspectorEnv.Prod);
174
- });
175
- test("Prod environment is set using string", function () {
176
- // When
177
- var inspector = new AvoInspector_1.AvoInspector({
178
- apiKey: "api-key-xxx",
179
- env: "prod",
180
- version: "0",
181
- });
182
- // Then
183
- expect(inspector.environment).toBe(AvoInspectorEnv_1.AvoInspectorEnv.Prod);
184
- });
185
- test("Environment other than Dev, Staging, Prod falls back to Dev", function () {
186
- // When
187
- var env = "test";
188
- var inspector = new AvoInspector_1.AvoInspector({
189
- apiKey: "api-key-xxx",
190
- version: "0",
191
- // @ts-ignore
192
- env: env,
193
- });
194
- // Then
195
- expect(inspector.environment).toBe(AvoInspectorEnv_1.AvoInspectorEnv.Dev);
196
- });
197
- test("Version is set", function () {
198
- var version = "1";
199
- // When
200
- var inspector = new AvoInspector_1.AvoInspector({
201
- apiKey: "api-key-xxx",
202
- env: AvoInspectorEnv_1.AvoInspectorEnv.Prod,
203
- version: version,
204
- });
205
- // Then
206
- expect(inspector.version).toBe(version);
207
- });
208
- test("Error is thrown when version is not set", function () {
209
- // Given
210
- // @ts-ignore
211
- var version;
212
- // Then
213
- expect(function () {
214
- new AvoInspector_1.AvoInspector({
215
- apiKey: "api-key-xxx",
216
- env: AvoInspectorEnv_1.AvoInspectorEnv.Prod,
217
- // @ts-ignore
218
- version: version,
219
- });
220
- }).toThrow(constants_1.error.VERSION);
221
- });
222
- test("Error is thrown when version is set to empty string", function () {
223
- // Given
224
- var version = " ";
225
- // Then
226
- expect(function () {
227
- new AvoInspector_1.AvoInspector({
228
- apiKey: "api-key-xxx",
229
- env: AvoInspectorEnv_1.AvoInspectorEnv.Prod,
230
- version: version,
231
- });
232
- }).toThrow(constants_1.error.VERSION);
233
- });
234
- test("Error is thrown when version is set to null", function () {
235
- // Given
236
- var version = null;
237
- // Then
238
- expect(function () {
239
- new AvoInspector_1.AvoInspector({
240
- apiKey: "api-key-xxx",
241
- env: AvoInspectorEnv_1.AvoInspectorEnv.Prod,
242
- // @ts-ignore
243
- version: version,
244
- });
245
- }).toThrow(constants_1.error.VERSION);
246
- });
247
- });
248
- describe("fetchAndValidateEvent integration", function () {
249
- beforeEach(function () {
250
- mockFetch.mockReset();
251
- // Reset AvoStreamId static state between tests
252
- AvoStreamId_1.AvoStreamId._anonymousId = null;
253
- AvoStreamId_1.AvoStreamId._initializationPromise = null;
254
- });
255
- test("dev env creates validator, fetchAndValidateEvent merges validation results into tracked events", function () { return __awaiter(void 0, void 0, void 0, function () {
256
- var wireResponse, inspector, schema, colorProp, calledUrl;
257
- return __generator(this, function (_a) {
258
- switch (_a.label) {
259
- case 0:
260
- wireResponse = {
261
- events: [
262
- {
263
- b: "branch1",
264
- id: "evt_1",
265
- vids: [],
266
- p: {
267
- color: {
268
- t: "string",
269
- r: true,
270
- // pinned value: "red" is required for evt_1
271
- p: { red: ["evt_1"] },
272
- },
273
- },
274
- },
275
- ],
276
- metadata: {
277
- schemaId: "schema1",
278
- branchId: "branch1",
279
- latestActionId: "action1",
280
- },
281
- };
282
- mockFetch.mockResolvedValue({
283
- status: 200,
284
- json: function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
285
- return [2 /*return*/, wireResponse];
286
- }); }); },
287
- });
288
- // Inject a known streamId so fetchAndValidateEvent proceeds past the streamId guard
289
- AvoStreamId_1.AvoStreamId._anonymousId = "test-stream-id";
290
- inspector = new AvoInspector_1.AvoInspector({
291
- apiKey: "api-key-xxx",
292
- env: AvoInspectorEnv_1.AvoInspectorEnv.Dev,
293
- version: "1",
294
- });
295
- // Force streamId to be set synchronously (it's fire-and-forget in constructor)
296
- inspector.streamId = "test-stream-id";
297
- return [4 /*yield*/, inspector.trackSchemaFromEvent("Sign Up", {
298
- color: "blue", // does NOT match pinned "red"
299
- })];
300
- case 1:
301
- schema = _a.sent();
302
- colorProp = schema.find(function (p) { return p.propertyName === "color"; });
303
- expect(colorProp).toBeDefined();
304
- // The avoBatcher should have been called with validation data merged in
305
- // We verify this indirectly by checking that fetch was called (validation ran)
306
- expect(mockFetch).toHaveBeenCalledTimes(1);
307
- calledUrl = mockFetch.mock.calls[0][0];
308
- expect(calledUrl).toContain("eventName=Sign+Up");
309
- expect(calledUrl).toContain("apiKey=api-key-xxx");
310
- return [2 /*return*/];
311
- }
312
- });
313
- }); });
314
- test("prod env skips fetchAndValidateEvent (no fetch calls)", function () { return __awaiter(void 0, void 0, void 0, function () {
315
- var inspector;
316
- return __generator(this, function (_a) {
317
- switch (_a.label) {
318
- case 0:
319
- inspector = new AvoInspector_1.AvoInspector({
320
- apiKey: "api-key-xxx",
321
- env: AvoInspectorEnv_1.AvoInspectorEnv.Prod,
322
- version: "1",
323
- });
324
- // When
325
- return [4 /*yield*/, inspector.trackSchemaFromEvent("Purchase", { amount: 42 })];
326
- case 1:
327
- // When
328
- _a.sent();
329
- // Then: no fetch should have been called in prod
330
- expect(mockFetch).not.toHaveBeenCalled();
331
- return [2 /*return*/];
332
- }
333
- });
334
- }); });
335
- });
@@ -1,214 +0,0 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
- return new (P || (P = Promise))(function (resolve, reject) {
16
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
- step((generator = generator.apply(thisArg, _arguments || [])).next());
20
- });
21
- };
22
- var __generator = (this && this.__generator) || function (thisArg, body) {
23
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
- function verb(n) { return function (v) { return step([n, v]); }; }
26
- function step(op) {
27
- if (f) throw new TypeError("Generator is already executing.");
28
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
- if (y = 0, t) op = [op[0] & 2, t.value];
31
- switch (op[0]) {
32
- case 0: case 1: t = op; break;
33
- case 4: _.label++; return { value: op[1], done: false };
34
- case 5: _.label++; y = op[1]; op = [0]; continue;
35
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
- default:
37
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
- if (t[2]) _.ops.pop();
42
- _.trys.pop(); continue;
43
- }
44
- op = body.call(thisArg, _);
45
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
- }
48
- };
49
- Object.defineProperty(exports, "__esModule", { value: true });
50
- var AvoBatcher_1 = require("../AvoBatcher");
51
- var AvoInspector_1 = require("../AvoInspector");
52
- var AvoNetworkCallsHandler_1 = require("../AvoNetworkCallsHandler");
53
- var AvoStorage_1 = require("../AvoStorage");
54
- var AvoStreamId_1 = require("../AvoStreamId");
55
- var constants_1 = require("./constants");
56
- var inspectorVersion = process.env.npm_package_version || "";
57
- jest.spyOn(AvoStreamId_1.AvoStreamId, "initialize").mockResolvedValue(constants_1.mockedReturns.ANONYMOUS_ID);
58
- describe("Batcher", function () {
59
- var checkBatchSpy;
60
- var inspectorCallSpy;
61
- var apiKey = constants_1.defaultOptions.apiKey, env = constants_1.defaultOptions.env, version = constants_1.defaultOptions.version, shouldLog = constants_1.defaultOptions.shouldLog;
62
- var storage = new AvoStorage_1.AvoStorage(shouldLog);
63
- var networkHandler = new AvoNetworkCallsHandler_1.AvoNetworkCallsHandler(apiKey, env, "", version, inspectorVersion);
64
- beforeAll(function () {
65
- checkBatchSpy = jest.spyOn(AvoBatcher_1.AvoBatcher.prototype, "checkIfBatchNeedsToBeSent");
66
- inspectorCallSpy = jest.spyOn(AvoNetworkCallsHandler_1.AvoNetworkCallsHandler.prototype, "callInspectorWithBatchBody");
67
- });
68
- afterEach(function () {
69
- jest.clearAllMocks();
70
- AvoInspector_1.AvoInspector.avoStorage.removeItem(AvoBatcher_1.AvoBatcher.cacheKey);
71
- });
72
- test("handleTrackSchema adds event to storage", function () { return __awaiter(void 0, void 0, void 0, function () {
73
- var inspector, events;
74
- return __generator(this, function (_a) {
75
- switch (_a.label) {
76
- case 0:
77
- inspector = new AvoInspector_1.AvoInspector(constants_1.defaultOptions);
78
- inspector.enableLogging(false);
79
- AvoInspector_1.AvoInspector.avoStorage.removeItem(AvoBatcher_1.AvoBatcher.cacheKey);
80
- inspector.avoBatcher.handleTrackSchema("event name", [], null, null);
81
- // Wait for async bodyForEventSchemaCall to resolve
82
- return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 50); })];
83
- case 1:
84
- // Wait for async bodyForEventSchemaCall to resolve
85
- _a.sent();
86
- events = AvoInspector_1.AvoInspector.avoStorage.getItem(AvoBatcher_1.AvoBatcher.cacheKey);
87
- expect(events).not.toBeNull();
88
- if (events !== null) {
89
- expect(events.length).toEqual(1);
90
- expect(events[0].type === constants_1.networkCallType.EVENT);
91
- }
92
- return [2 /*return*/];
93
- }
94
- });
95
- }); });
96
- test("checkIfBatchNeedsToBeSent is called on Batcher initialization", function () { return __awaiter(void 0, void 0, void 0, function () {
97
- var event;
98
- return __generator(this, function (_a) {
99
- switch (_a.label) {
100
- case 0: return [4 /*yield*/, networkHandler.bodyForEventSchemaCall("name", [
101
- { propertyName: "prop0", propertyType: "string" },
102
- ], "testEventId", "testEventHash")];
103
- case 1:
104
- event = _a.sent();
105
- storage.setItem(AvoBatcher_1.AvoBatcher.cacheKey, [event]);
106
- return [4 /*yield*/, new AvoBatcher_1.AvoBatcher(networkHandler)];
107
- case 2:
108
- _a.sent();
109
- expect(checkBatchSpy).toHaveBeenCalledTimes(1);
110
- return [2 /*return*/];
111
- }
112
- });
113
- }); });
114
- test("checkIfBatchNeedsToBeSent is called on handleTrackSchema", function () { return __awaiter(void 0, void 0, void 0, function () {
115
- var inspector;
116
- return __generator(this, function (_a) {
117
- switch (_a.label) {
118
- case 0:
119
- inspector = new AvoInspector_1.AvoInspector(constants_1.defaultOptions);
120
- inspector.enableLogging(false);
121
- inspector.avoBatcher.handleTrackSchema("event name", [], null, null);
122
- // Wait for async bodyForEventSchemaCall to resolve
123
- return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 50); })];
124
- case 1:
125
- // Wait for async bodyForEventSchemaCall to resolve
126
- _a.sent();
127
- expect(checkBatchSpy).toHaveBeenCalledTimes(1);
128
- return [2 /*return*/];
129
- }
130
- });
131
- }); });
132
- test("Batcher saves events produced by AvoNetworkCallsHandler", function () { return __awaiter(void 0, void 0, void 0, function () {
133
- var schemaEvent, inspector, events;
134
- return __generator(this, function (_a) {
135
- switch (_a.label) {
136
- case 0:
137
- AvoInspector_1.AvoInspector.avoStorage.removeItem(AvoBatcher_1.AvoBatcher.cacheKey);
138
- return [4 /*yield*/, networkHandler.bodyForEventSchemaCall("name", [
139
- { propertyName: "prop0", propertyType: "string" },
140
- ], "testEventId", "testEventHash")];
141
- case 1:
142
- schemaEvent = _a.sent();
143
- inspector = new AvoInspector_1.AvoInspector(constants_1.defaultOptions);
144
- inspector.enableLogging(false);
145
- inspector.avoBatcher.handleTrackSchema("name", [
146
- { propertyName: "prop0", propertyType: "string" },
147
- ], "testEventId", "testEventHash");
148
- // Wait for async bodyForEventSchemaCall to resolve
149
- return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 50); })];
150
- case 2:
151
- // Wait for async bodyForEventSchemaCall to resolve
152
- _a.sent();
153
- events = storage.getItem(AvoBatcher_1.AvoBatcher.cacheKey);
154
- expect(events).not.toBeNull();
155
- if (events != null) {
156
- expect(events[0]).not.toBe(schemaEvent);
157
- expect(__assign(__assign({}, events[0]), { messageId: undefined, createdAt: undefined })).toStrictEqual(__assign(__assign({}, schemaEvent), { messageId: undefined, createdAt: undefined }));
158
- }
159
- return [2 /*return*/];
160
- }
161
- });
162
- }); });
163
- test("Events are retrieved from Storage on init", function () {
164
- var getItemAsyncSpy = jest.spyOn(AvoStorage_1.AvoStorage.prototype, "getItemAsync");
165
- new AvoInspector_1.AvoInspector(constants_1.defaultOptions);
166
- expect(getItemAsyncSpy).toHaveBeenCalledTimes(1);
167
- expect(getItemAsyncSpy).toHaveBeenCalledWith(AvoBatcher_1.AvoBatcher.cacheKey);
168
- });
169
- test("Only latest 1000 events are stored in the storage", function () { return __awaiter(void 0, void 0, void 0, function () {
170
- var eventLimit, events, i, _a, _b, inspector, setItemsSpy, savedEvents;
171
- return __generator(this, function (_c) {
172
- switch (_c.label) {
173
- case 0:
174
- AvoInspector_1.AvoInspector.avoStorage.removeItem(AvoBatcher_1.AvoBatcher.cacheKey);
175
- eventLimit = 1000;
176
- events = [];
177
- i = 0;
178
- _c.label = 1;
179
- case 1:
180
- if (!(i < eventLimit + 1)) return [3 /*break*/, 4];
181
- _b = (_a = events).push;
182
- return [4 /*yield*/, networkHandler.bodyForEventSchemaCall("event-name-".concat(i), [
183
- { propertyName: "prop0", propertyType: "string" },
184
- ], null, null)];
185
- case 2:
186
- _b.apply(_a, [_c.sent()]);
187
- _c.label = 3;
188
- case 3:
189
- i++;
190
- return [3 /*break*/, 1];
191
- case 4:
192
- AvoInspector_1.AvoInspector.avoStorage.setItem(AvoBatcher_1.AvoBatcher.cacheKey, events);
193
- inspector = new AvoInspector_1.AvoInspector(constants_1.defaultOptions);
194
- inspector.enableLogging(false);
195
- return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 100); })];
196
- case 5:
197
- _c.sent();
198
- setItemsSpy = jest.spyOn(AvoStorage_1.AvoStorage.prototype, "setItem");
199
- inspector.avoBatcher.handleTrackSchema("extra event", [], null, null);
200
- // Wait for async bodyForEventSchemaCall to resolve
201
- return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 50); })];
202
- case 6:
203
- // Wait for async bodyForEventSchemaCall to resolve
204
- _c.sent();
205
- savedEvents = AvoInspector_1.AvoInspector.avoStorage.getItem(AvoBatcher_1.AvoBatcher.cacheKey);
206
- expect(savedEvents).not.toBeNull();
207
- if (savedEvents !== null) {
208
- expect(savedEvents.length).toBeLessThanOrEqual(1000);
209
- }
210
- return [2 /*return*/];
211
- }
212
- });
213
- }); });
214
- });