node-avo-inspector 1.2.0 → 2.0.0-beta.2

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 (132) hide show
  1. package/CHANGELOG.md +46 -0
  2. package/README.md +270 -4
  3. package/dist/AvoBatchQueue.d.ts +73 -0
  4. package/dist/AvoBatchQueue.js +239 -0
  5. package/dist/AvoDeduplicator.d.ts +15 -6
  6. package/dist/AvoDeduplicator.js +46 -24
  7. package/dist/AvoInspector.d.ts +136 -23
  8. package/dist/AvoInspector.js +777 -109
  9. package/dist/AvoInspectorVersion.d.ts +3 -0
  10. package/dist/AvoInspectorVersion.js +10 -0
  11. package/dist/AvoLog.d.ts +52 -0
  12. package/dist/AvoLog.js +246 -0
  13. package/dist/AvoNetworkCallsHandler.d.ts +55 -8
  14. package/dist/AvoNetworkCallsHandler.js +266 -92
  15. package/dist/AvoSchemaParser.d.ts +6 -0
  16. package/dist/AvoSchemaParser.js +138 -33
  17. package/dist/AvoStreamId.d.ts +2 -1
  18. package/dist/AvoStreamId.js +21 -2
  19. package/dist/eventSpec/AvoEventSpecCache.d.ts +28 -5
  20. package/dist/eventSpec/AvoEventSpecCache.js +66 -11
  21. package/dist/eventSpec/AvoEventSpecFetchTypes.d.ts +50 -18
  22. package/dist/eventSpec/AvoEventSpecFetcher.d.ts +29 -6
  23. package/dist/eventSpec/AvoEventSpecFetcher.js +279 -69
  24. package/dist/eventSpec/EventValidator.d.ts +16 -10
  25. package/dist/eventSpec/EventValidator.js +351 -38
  26. package/dist/index.d.ts +2 -0
  27. package/dist/index.js +4 -1
  28. package/dist/utils.d.ts +29 -1
  29. package/dist/utils.js +86 -10
  30. package/package.json +18 -1
  31. package/.claude/scheduled_tasks.lock +0 -1
  32. package/.claude/settings.local.json +0 -11
  33. package/.claude/worktrees/release-1.1.1/LICENSE +0 -21
  34. package/.claude/worktrees/release-1.1.1/README.md +0 -69
  35. package/.claude/worktrees/release-1.1.1/examples/node-inspector-js/index.js +0 -30
  36. package/.claude/worktrees/release-1.1.1/examples/node-inspector-js/package.json +0 -15
  37. package/.claude/worktrees/release-1.1.1/examples/node-inspector-js/yarn.lock +0 -6
  38. package/.claude/worktrees/release-1.1.1/examples/node-inspector-ts/dist/index.js +0 -34
  39. package/.claude/worktrees/release-1.1.1/examples/node-inspector-ts/index.ts +0 -30
  40. package/.claude/worktrees/release-1.1.1/examples/node-inspector-ts/package.json +0 -18
  41. package/.claude/worktrees/release-1.1.1/examples/node-inspector-ts/tsconfig.json +0 -15
  42. package/.claude/worktrees/release-1.1.1/examples/node-inspector-ts/yarn.lock +0 -11
  43. package/.claude/worktrees/release-1.1.1/package-lock.json +0 -4039
  44. package/.claude/worktrees/release-1.1.1/package.json +0 -48
  45. package/.claude/worktrees/release-1.1.1/reason/AvoInspector.rei +0 -11
  46. package/.claude/worktrees/release-1.1.1/reason/NodeAvoInspector.re +0 -27
  47. package/.claude/worktrees/release-1.1.1/src/AvoDeduplicator.ts +0 -146
  48. package/.claude/worktrees/release-1.1.1/src/AvoEncryption.ts +0 -90
  49. package/.claude/worktrees/release-1.1.1/src/AvoGuid.ts +0 -9
  50. package/.claude/worktrees/release-1.1.1/src/AvoInspector.ts +0 -383
  51. package/.claude/worktrees/release-1.1.1/src/AvoInspectorEnv.ts +0 -8
  52. package/.claude/worktrees/release-1.1.1/src/AvoNetworkCallsHandler.ts +0 -267
  53. package/.claude/worktrees/release-1.1.1/src/AvoSchemaParser.ts +0 -115
  54. package/.claude/worktrees/release-1.1.1/src/AvoStreamId.ts +0 -16
  55. package/.claude/worktrees/release-1.1.1/src/__tests__/TestAvoEncryption.ts +0 -515
  56. package/.claude/worktrees/release-1.1.1/src/__tests__/TestAvoEventSpecCache.ts +0 -242
  57. package/.claude/worktrees/release-1.1.1/src/__tests__/TestAvoEventSpecFetcher.ts +0 -241
  58. package/.claude/worktrees/release-1.1.1/src/__tests__/TestAvoInspector.ts +0 -360
  59. package/.claude/worktrees/release-1.1.1/src/__tests__/TestAvoStreamId.ts +0 -72
  60. package/.claude/worktrees/release-1.1.1/src/__tests__/TestDeduplicator.ts +0 -191
  61. package/.claude/worktrees/release-1.1.1/src/__tests__/TestEventValidator.ts +0 -231
  62. package/.claude/worktrees/release-1.1.1/src/__tests__/TestNetworkCallsHandler.ts +0 -179
  63. package/.claude/worktrees/release-1.1.1/src/__tests__/TestNetworkCallsHandlerPublic.ts +0 -208
  64. package/.claude/worktrees/release-1.1.1/src/__tests__/TestParsing.ts +0 -153
  65. package/.claude/worktrees/release-1.1.1/src/__tests__/constants.ts +0 -63
  66. package/.claude/worktrees/release-1.1.1/src/eventSpec/AvoEventSpecCache.ts +0 -109
  67. package/.claude/worktrees/release-1.1.1/src/eventSpec/AvoEventSpecFetchTypes.ts +0 -47
  68. package/.claude/worktrees/release-1.1.1/src/eventSpec/AvoEventSpecFetcher.ts +0 -106
  69. package/.claude/worktrees/release-1.1.1/src/eventSpec/EventValidator.ts +0 -74
  70. package/.claude/worktrees/release-1.1.1/src/index.ts +0 -2
  71. package/.claude/worktrees/release-1.1.1/src/utils.ts +0 -49
  72. package/.claude/worktrees/release-1.1.1/tsconfig.json +0 -13
  73. package/.claude/worktrees/release-1.1.1/yarn.lock +0 -2310
  74. package/.github/workflows/claude.yml +0 -55
  75. package/dist/__tests__/TestAvoEncryption.d.ts +0 -1
  76. package/dist/__tests__/TestAvoEncryption.js +0 -288
  77. package/dist/__tests__/TestAvoEventSpecCache.d.ts +0 -1
  78. package/dist/__tests__/TestAvoEventSpecCache.js +0 -194
  79. package/dist/__tests__/TestAvoEventSpecFetcher.d.ts +0 -1
  80. package/dist/__tests__/TestAvoEventSpecFetcher.js +0 -219
  81. package/dist/__tests__/TestAvoInspector.d.ts +0 -1
  82. package/dist/__tests__/TestAvoInspector.js +0 -291
  83. package/dist/__tests__/TestAvoStreamId.d.ts +0 -1
  84. package/dist/__tests__/TestAvoStreamId.js +0 -55
  85. package/dist/__tests__/TestDeduplicator.d.ts +0 -1
  86. package/dist/__tests__/TestDeduplicator.js +0 -102
  87. package/dist/__tests__/TestEventValidator.d.ts +0 -1
  88. package/dist/__tests__/TestEventValidator.js +0 -196
  89. package/dist/__tests__/TestNetworkCallsHandler.d.ts +0 -1
  90. package/dist/__tests__/TestNetworkCallsHandler.js +0 -86
  91. package/dist/__tests__/TestNetworkCallsHandlerPublic.d.ts +0 -1
  92. package/dist/__tests__/TestNetworkCallsHandlerPublic.js +0 -160
  93. package/dist/__tests__/TestParsing.d.ts +0 -1
  94. package/dist/__tests__/TestParsing.js +0 -128
  95. package/dist/__tests__/constants.d.ts +0 -42
  96. package/dist/__tests__/constants.js +0 -53
  97. package/examples/node-inspector-js/index.js +0 -30
  98. package/examples/node-inspector-js/package.json +0 -15
  99. package/examples/node-inspector-js/yarn.lock +0 -6
  100. package/examples/node-inspector-ts/dist/index.js +0 -34
  101. package/examples/node-inspector-ts/index.ts +0 -30
  102. package/examples/node-inspector-ts/package.json +0 -18
  103. package/examples/node-inspector-ts/tsconfig.json +0 -15
  104. package/examples/node-inspector-ts/yarn.lock +0 -11
  105. package/reason/AvoInspector.rei +0 -11
  106. package/reason/NodeAvoInspector.re +0 -27
  107. package/src/AvoDeduplicator.ts +0 -150
  108. package/src/AvoEncryption.ts +0 -90
  109. package/src/AvoGuid.ts +0 -9
  110. package/src/AvoInspector.ts +0 -498
  111. package/src/AvoInspectorEnv.ts +0 -8
  112. package/src/AvoNetworkCallsHandler.ts +0 -340
  113. package/src/AvoSchemaParser.ts +0 -115
  114. package/src/AvoStreamId.ts +0 -16
  115. package/src/__tests__/TestAvoEncryption.ts +0 -515
  116. package/src/__tests__/TestAvoEventSpecCache.ts +0 -242
  117. package/src/__tests__/TestAvoEventSpecFetcher.ts +0 -279
  118. package/src/__tests__/TestAvoInspector.ts +0 -376
  119. package/src/__tests__/TestAvoStreamId.ts +0 -72
  120. package/src/__tests__/TestDeduplicator.ts +0 -191
  121. package/src/__tests__/TestEventValidator.ts +0 -231
  122. package/src/__tests__/TestNetworkCallsHandler.ts +0 -181
  123. package/src/__tests__/TestNetworkCallsHandlerPublic.ts +0 -208
  124. package/src/__tests__/TestParsing.ts +0 -153
  125. package/src/__tests__/constants.ts +0 -63
  126. package/src/eventSpec/AvoEventSpecCache.ts +0 -119
  127. package/src/eventSpec/AvoEventSpecFetchTypes.ts +0 -47
  128. package/src/eventSpec/AvoEventSpecFetcher.ts +0 -186
  129. package/src/eventSpec/EventValidator.ts +0 -74
  130. package/src/index.ts +0 -2
  131. package/src/utils.ts +0 -49
  132. package/tsconfig.json +0 -13
@@ -1,231 +0,0 @@
1
- import { EventValidator } from "../eventSpec/EventValidator";
2
- import { EventSpec, PropertyConstraint, PropertyValidationResult } from "../eventSpec/AvoEventSpecFetchTypes";
3
-
4
- // We need to mock safe-regex2 for some tests
5
- jest.mock("safe-regex2", () => {
6
- const actual = jest.requireActual("safe-regex2");
7
- return {
8
- __esModule: true,
9
- default: actual.default || actual,
10
- _mockable: true,
11
- };
12
- });
13
-
14
- describe("EventValidator", () => {
15
- let validator: EventValidator;
16
-
17
- beforeEach(() => {
18
- validator = new EventValidator();
19
- jest.spyOn(console, "warn").mockImplementation(() => {});
20
- });
21
-
22
- afterEach(() => {
23
- jest.restoreAllMocks();
24
- });
25
-
26
- describe("type validation", () => {
27
- test("passes when property types match spec", () => {
28
- const spec: EventSpec = {
29
- eventName: "click",
30
- properties: [
31
- { propertyName: "target", propertyType: "string" },
32
- { propertyName: "count", propertyType: "int" },
33
- ],
34
- };
35
- const eventProperties = [
36
- { propertyName: "target", propertyType: "string" },
37
- { propertyName: "count", propertyType: "int" },
38
- ];
39
-
40
- const results = validator.validate(spec, eventProperties, "evt-1");
41
- // All properties passed type check, so failedEventIds should be empty
42
- expect(results.every((r: PropertyValidationResult) => r.failedEventIds.length === 0)).toBe(true);
43
- // Bandwidth optimization: passedEventIds cleared because passed >= failed for each property
44
- expect(results.every((r: PropertyValidationResult) => r.passedEventIds.length === 0)).toBe(true);
45
- });
46
-
47
- test("fails when property type does not match spec", () => {
48
- const spec: EventSpec = {
49
- eventName: "click",
50
- properties: [{ propertyName: "count", propertyType: "int" }],
51
- };
52
- const eventProperties = [
53
- { propertyName: "count", propertyType: "string" },
54
- ];
55
-
56
- const results = validator.validate(spec, eventProperties, "evt-2");
57
- const countResult = results.find((r: PropertyValidationResult) => r.propertyName === "count");
58
- expect(countResult?.failedEventIds).toContain("evt-2");
59
- expect(countResult?.passedEventIds).not.toContain("evt-2");
60
- });
61
-
62
- test("fails for missing required properties", () => {
63
- const spec: EventSpec = {
64
- eventName: "click",
65
- properties: [
66
- { propertyName: "target", propertyType: "string" },
67
- { propertyName: "count", propertyType: "int" },
68
- ],
69
- };
70
- const eventProperties = [
71
- { propertyName: "target", propertyType: "string" },
72
- // count is missing
73
- ];
74
-
75
- const results = validator.validate(spec, eventProperties, "evt-3");
76
- const countResult = results.find((r: PropertyValidationResult) => r.propertyName === "count");
77
- expect(countResult?.failedEventIds).toContain("evt-3");
78
- });
79
- });
80
-
81
- describe("regex validation", () => {
82
- test("passes when value matches safe regex pattern (type match)", () => {
83
- const spec: EventSpec = {
84
- eventName: "navigate",
85
- properties: [
86
- {
87
- propertyName: "url",
88
- propertyType: "string",
89
- regex: "^https://",
90
- },
91
- ],
92
- };
93
- const eventProperties = [
94
- { propertyName: "url", propertyType: "string", propertyValue: "https://example.com" },
95
- ];
96
-
97
- const results = validator.validate(spec, eventProperties, "evt-4");
98
- const urlResult = results.find((r: PropertyValidationResult) => r.propertyName === "url");
99
- // Type matches and regex matches, property passed. Bandwidth optimization: passedEventIds cleared
100
- // because passed count (1) >= failed count (0)
101
- expect(urlResult?.failedEventIds).toEqual([]);
102
- });
103
-
104
- test("fails when value does not match safe regex pattern", () => {
105
- const spec: EventSpec = {
106
- eventName: "navigate",
107
- properties: [
108
- {
109
- propertyName: "url",
110
- propertyType: "string",
111
- regex: "^https://",
112
- },
113
- ],
114
- };
115
- const eventProperties = [
116
- { propertyName: "url", propertyType: "string", propertyValue: "http://example.com" },
117
- ];
118
-
119
- const results = validator.validate(spec, eventProperties, "evt-regex-fail");
120
- const urlResult = results.find((r: PropertyValidationResult) => r.propertyName === "url");
121
- expect(urlResult?.failedEventIds).toContain("evt-regex-fail");
122
- expect(urlResult?.passedEventIds).not.toContain("evt-regex-fail");
123
- });
124
-
125
- test("logs warning and passes type check when regex syntax is invalid", () => {
126
- const spec: EventSpec = {
127
- eventName: "filter",
128
- properties: [
129
- {
130
- propertyName: "pattern",
131
- propertyType: "string",
132
- regex: "[invalid",
133
- },
134
- ],
135
- };
136
- const eventProperties = [
137
- { propertyName: "pattern", propertyType: "string", propertyValue: "anything" },
138
- ];
139
-
140
- const results = validator.validate(spec, eventProperties, "evt-bad-regex");
141
- const patternResult = results.find((r: PropertyValidationResult) => r.propertyName === "pattern");
142
- // Invalid regex is caught, property still passes type check
143
- expect(patternResult?.failedEventIds).toEqual([]);
144
- expect(console.warn).toHaveBeenCalledWith(
145
- expect.stringContaining("[Avo Inspector] Warning")
146
- );
147
- });
148
-
149
- test("skips unsafe regex patterns with warning", () => {
150
- const spec: EventSpec = {
151
- eventName: "search",
152
- properties: [
153
- {
154
- propertyName: "query",
155
- propertyType: "string",
156
- // Catastrophic backtracking pattern
157
- regex: "(a+)+$",
158
- },
159
- ],
160
- };
161
- const eventProperties = [
162
- { propertyName: "query", propertyType: "string", propertyValue: "aaa" },
163
- ];
164
-
165
- const results = validator.validate(spec, eventProperties, "evt-5");
166
- // Unsafe regex should be skipped, property still passes type check
167
- const queryResult = results.find((r: PropertyValidationResult) => r.propertyName === "query");
168
- // Bandwidth optimization: passedEventIds cleared because passed count >= failed count
169
- expect(queryResult?.failedEventIds).toEqual([]);
170
- expect(console.warn).toHaveBeenCalledWith(
171
- expect.stringContaining("[Avo Inspector] Warning")
172
- );
173
- });
174
- });
175
-
176
- describe("extra/unexpected properties", () => {
177
- test("ignores properties present in event but not in the spec", () => {
178
- const spec: EventSpec = {
179
- eventName: "click",
180
- properties: [
181
- { propertyName: "target", propertyType: "string" },
182
- ],
183
- };
184
- const eventProperties = [
185
- { propertyName: "target", propertyType: "string" },
186
- { propertyName: "extraProp", propertyType: "int" },
187
- { propertyName: "anotherExtra", propertyType: "boolean" },
188
- ];
189
-
190
- const results = validator.validate(spec, eventProperties, "evt-extra");
191
- // Only spec properties get results — extra properties are not reported
192
- expect(results).toHaveLength(1);
193
- expect(results[0].propertyName).toBe("target");
194
- expect(results.find((r: PropertyValidationResult) => r.propertyName === "extraProp")).toBeUndefined();
195
- expect(results.find((r: PropertyValidationResult) => r.propertyName === "anotherExtra")).toBeUndefined();
196
- });
197
- });
198
-
199
- describe("bandwidth optimization", () => {
200
- test("returns passedEventIds only when strictly smaller than failedEventIds", () => {
201
- const spec: EventSpec = {
202
- eventName: "click",
203
- properties: [{ propertyName: "target", propertyType: "string" }],
204
- };
205
-
206
- // Single pass
207
- const passResults = validator.validate(
208
- spec,
209
- [{ propertyName: "target", propertyType: "string" }],
210
- "evt-pass"
211
- );
212
- const passResult = passResults.find((r: PropertyValidationResult) => r.propertyName === "target");
213
-
214
- // When there's only passed and no failed, passedEventIds count (1) > failedEventIds count (0)
215
- // So passedEventIds is NOT strictly smaller - it should be returned
216
- // But the optimization says: return passedEventIds ONLY when strictly smaller than failedEventIds
217
- // So when passedEventIds.length >= failedEventIds.length, we clear passedEventIds
218
- expect(passResult?.passedEventIds).toEqual([]);
219
-
220
- // Single fail
221
- const failResults = validator.validate(
222
- spec,
223
- [{ propertyName: "target", propertyType: "int" }],
224
- "evt-fail"
225
- );
226
- const failResult = failResults.find((r: PropertyValidationResult) => r.propertyName === "target");
227
- // failedEventIds has 1, passedEventIds has 0 -> passedEventIds is strictly smaller, return it
228
- expect(failResult?.failedEventIds).toContain("evt-fail");
229
- });
230
- });
231
- });
@@ -1,181 +0,0 @@
1
- import { AvoGuid } from "../AvoGuid";
2
- import { AvoNetworkCallsHandler, BaseBody } from "../AvoNetworkCallsHandler";
3
-
4
- import {
5
- defaultOptions,
6
- mockedReturns,
7
- } from "./constants";
8
-
9
- const inspectorVersion = process.env.npm_package_version || "";
10
-
11
- describe("NetworkCallsHandler", () => {
12
- const { apiKey, env, version } = defaultOptions;
13
- const appName = "";
14
-
15
- let networkHandler: AvoNetworkCallsHandler;
16
- let baseBody: BaseBody;
17
-
18
- const now = new Date();
19
-
20
- beforeAll(() => {
21
- // @ts-ignore
22
- jest.spyOn(global, "Date").mockImplementation(() => now);
23
-
24
- jest
25
- .spyOn(AvoGuid as any, "newGuid")
26
- .mockImplementation(() => mockedReturns.GUID);
27
-
28
- networkHandler = new AvoNetworkCallsHandler(
29
- apiKey,
30
- env,
31
- "",
32
- version,
33
- inspectorVersion
34
- );
35
-
36
- baseBody = {
37
- apiKey,
38
- appName,
39
- appVersion: version,
40
- libVersion: inspectorVersion,
41
- env,
42
- libPlatform: "node",
43
- messageId: mockedReturns.GUID,
44
- trackingId: "",
45
- sessionId: "",
46
- anonymousId: "",
47
- createdAt: new Date().toISOString(),
48
- samplingRate: 1.0,
49
- };
50
- });
51
-
52
- afterEach(() => {
53
- jest.clearAllMocks();
54
- });
55
-
56
- test("bodyForEventSchemaCall returns base body + event schema used for event sending from non Codegen", () => {
57
- const eventName = "event name";
58
- const eventProperties = [{ propertyName: "prop0", propertyType: "string" }];
59
-
60
- const body = networkHandler.bodyForEventSchemaCall(
61
- "",
62
- eventName,
63
- eventProperties,
64
- null,
65
- null
66
- );
67
-
68
- expect(body).toEqual({
69
- ...baseBody,
70
- anonymousId: "",
71
- type: "event",
72
- eventName,
73
- eventProperties,
74
- avoFunction: false,
75
- eventId: null,
76
- eventHash: null,
77
- });
78
- });
79
-
80
- test("bodyForEventSchemaCall returns base body + event schema used for event sending from Codegen", () => {
81
- const eventName = "event name";
82
- const eventId = "event id";
83
- const eventHash = "event hash";
84
- const eventProperties = [{ propertyName: "prop0", propertyType: "string" }];
85
-
86
- const body = networkHandler.bodyForEventSchemaCall(
87
- "",
88
- eventName,
89
- eventProperties,
90
- eventId,
91
- eventHash
92
- );
93
-
94
- expect(body).toEqual({
95
- ...baseBody,
96
- anonymousId: "",
97
- type: "event",
98
- eventName,
99
- eventProperties,
100
- avoFunction: true,
101
- eventId,
102
- eventHash,
103
- });
104
- });
105
-
106
- test("bodyForEventSchemaCall uses streamId as anonymousId", () => {
107
- const eventName = "event name";
108
- const streamId = "user-123";
109
- const eventProperties = [{ propertyName: "prop0", propertyType: "string" }];
110
-
111
- const body = networkHandler.bodyForEventSchemaCall(
112
- streamId,
113
- eventName,
114
- eventProperties,
115
- null,
116
- null
117
- );
118
-
119
- expect(body.anonymousId).toBe("user-123");
120
- });
121
-
122
- test("bodyForEventSchemaCall with rawEventProperties passes them through (no encryption in dev without key)", () => {
123
- const eventName = "event name";
124
- const eventProperties = [{ propertyName: "prop0", propertyType: "string" }];
125
- const rawEventProperties = { prop0: "hello" };
126
-
127
- const body = networkHandler.bodyForEventSchemaCall(
128
- "",
129
- eventName,
130
- eventProperties,
131
- null,
132
- null,
133
- rawEventProperties
134
- );
135
-
136
- // Without a publicEncryptionKey, encryption is not triggered,
137
- // so eventProperties should be the plain schema (not encrypted)
138
- expect(body.eventProperties).toEqual(eventProperties);
139
- expect(body.type).toBe("event");
140
- expect(body.eventName).toBe(eventName);
141
- });
142
-
143
- test("base body includes publicEncryptionKey when handler is constructed with one", () => {
144
- const handlerWithKey = new AvoNetworkCallsHandler(
145
- apiKey,
146
- env,
147
- "",
148
- version,
149
- inspectorVersion,
150
- "test-public-key-abc"
151
- );
152
-
153
- const eventName = "event name";
154
- const eventProperties = [{ propertyName: "prop0", propertyType: "string" }];
155
-
156
- const body = handlerWithKey.bodyForEventSchemaCall(
157
- "",
158
- eventName,
159
- eventProperties,
160
- null,
161
- null
162
- );
163
-
164
- expect(body.publicEncryptionKey).toBe("test-public-key-abc");
165
- });
166
-
167
- test("base body does not include publicEncryptionKey when handler is constructed without one", () => {
168
- const eventName = "event name";
169
- const eventProperties = [{ propertyName: "prop0", propertyType: "string" }];
170
-
171
- const body = networkHandler.bodyForEventSchemaCall(
172
- "",
173
- eventName,
174
- eventProperties,
175
- null,
176
- null
177
- );
178
-
179
- expect(body.publicEncryptionKey).toBeUndefined();
180
- });
181
- });
@@ -1,208 +0,0 @@
1
- import { AvoInspector } from "../AvoInspector";
2
- import { AvoNetworkCallsHandler } from "../AvoNetworkCallsHandler";
3
-
4
- import { defaultOptions } from "./constants";
5
-
6
- describe("AvoNetworkCallsHandler", () => {
7
- let inspectorCallSpy: jest.SpyInstance<any, unknown[]>;
8
- let consoleLogSpy: jest.SpyInstance<any, unknown[]>;
9
-
10
- let inspector: AvoInspector;
11
-
12
- const { apiKey, env, version, appName } = defaultOptions;
13
-
14
- const eventName = "event name";
15
- const properties = {
16
- prop0: "",
17
- prop2: false,
18
- prop3: 0,
19
- prop4: 0.0,
20
- };
21
-
22
- const happyPathInspectorCall = () => {
23
- return Promise.resolve();
24
- };
25
-
26
- beforeAll(() => {
27
- inspector = new AvoInspector(defaultOptions);
28
- inspector.enableLogging(true);
29
-
30
- inspectorCallSpy = jest
31
- .spyOn(AvoNetworkCallsHandler.prototype as any, "callInspectorWithBatchBody");
32
-
33
- consoleLogSpy = jest.spyOn(console, "log").mockImplementation(() => {});
34
- });
35
-
36
- beforeEach(() => {
37
- inspectorCallSpy.mockImplementation(happyPathInspectorCall);
38
- })
39
-
40
- afterEach(() => {
41
- jest.clearAllMocks();
42
- // @ts-ignore
43
- inspector.avoDeduplicator._clearEvents();
44
- });
45
-
46
- test("trackSchemaFromEvent sends only event call (no sessionStarted)", async () => {
47
- await inspector.trackSchemaFromEvent(eventName, properties);
48
-
49
- // Should be called once (event only, no sessionStarted)
50
- expect(inspector.avoNetworkCallsHandler.callInspectorWithBatchBody).toHaveBeenCalledTimes(1);
51
- expect(inspector.avoNetworkCallsHandler.callInspectorWithBatchBody).toHaveBeenCalledWith([
52
- expect.objectContaining({
53
- type: "event",
54
- apiKey: apiKey,
55
- appName: appName,
56
- appVersion: version,
57
- libVersion: expect.anything(),
58
- env: env,
59
- libPlatform: "node",
60
- messageId: expect.anything(),
61
- anonymousId: "",
62
- createdAt: expect.any(String),
63
- samplingRate: 1,
64
- }),
65
- ]);
66
- });
67
-
68
- test("trackSchemaFromEvent with streamId sets anonymousId to streamId", async () => {
69
- await inspector.trackSchemaFromEvent(eventName, properties, "user-stream-123");
70
-
71
- expect(inspector.avoNetworkCallsHandler.callInspectorWithBatchBody).toHaveBeenCalledTimes(1);
72
- expect(inspector.avoNetworkCallsHandler.callInspectorWithBatchBody).toHaveBeenCalledWith([
73
- expect.objectContaining({
74
- type: "event",
75
- anonymousId: "user-stream-123",
76
- }),
77
- ]);
78
- });
79
-
80
- test("trackSchemaFromEvent without streamId sets anonymousId to empty string", async () => {
81
- await inspector.trackSchemaFromEvent(eventName, properties);
82
-
83
- expect(inspector.avoNetworkCallsHandler.callInspectorWithBatchBody).toHaveBeenCalledTimes(1);
84
- expect(inspector.avoNetworkCallsHandler.callInspectorWithBatchBody).toHaveBeenCalledWith([
85
- expect.objectContaining({
86
- anonymousId: "",
87
- }),
88
- ]);
89
- });
90
-
91
- test("trackSchemaFromEvent with streamId containing ':' logs warning", async () => {
92
- const consoleWarnSpy = jest.spyOn(console, "warn").mockImplementation(() => {});
93
-
94
- await inspector.trackSchemaFromEvent(eventName, properties, "invalid:stream");
95
-
96
- expect(consoleWarnSpy).toHaveBeenCalledWith(
97
- "[Avo Inspector] Warning: streamId contains ':' which is not supported"
98
- );
99
-
100
- consoleWarnSpy.mockRestore();
101
- });
102
-
103
- test("_avoFunctionTrackSchemaFromEvent sends only event call (no sessionStarted)", async () => {
104
- const eventId = "testId";
105
- const eventHash = "testHash";
106
-
107
- // @ts-ignore
108
- await inspector._avoFunctionTrackSchemaFromEvent(
109
- eventName,
110
- properties,
111
- eventId,
112
- eventHash
113
- );
114
-
115
- // Should be called once (event only, no sessionStarted)
116
- expect(inspector.avoNetworkCallsHandler.callInspectorWithBatchBody).toHaveBeenCalledTimes(1);
117
- expect(inspector.avoNetworkCallsHandler.callInspectorWithBatchBody).toHaveBeenCalledWith([
118
- expect.objectContaining({
119
- type: "event",
120
- apiKey: apiKey,
121
- appName: appName,
122
- appVersion: version,
123
- libVersion: expect.anything(),
124
- env: env,
125
- libPlatform: "node",
126
- messageId: expect.anything(),
127
- anonymousId: "",
128
- createdAt: expect.any(String),
129
- }),
130
- ]);
131
- });
132
-
133
- test("an error is logged if event schema call fails and AvoInspector.shouldLog is true", async () => {
134
- const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
135
- inspectorCallSpy.mockRejectedValueOnce('Network error');
136
-
137
- await inspector.trackSchemaFromEvent(eventName, properties);
138
-
139
- expect(consoleErrorSpy).toHaveBeenCalledWith('Avo Inspector: schema sending failed: Network error.');
140
- consoleErrorSpy.mockRestore();
141
- });
142
-
143
- test("_avoFunctionTrackSchemaFromEvent passes empty string as anonymousId", async () => {
144
- const eventId = "testId";
145
- const eventHash = "testHash";
146
-
147
- // @ts-ignore
148
- await inspector._avoFunctionTrackSchemaFromEvent(
149
- eventName,
150
- properties,
151
- eventId,
152
- eventHash
153
- );
154
-
155
- expect(inspector.avoNetworkCallsHandler.callInspectorWithBatchBody).toHaveBeenCalledWith([
156
- expect.objectContaining({
157
- anonymousId: "",
158
- }),
159
- ]);
160
- });
161
-
162
- test("trackSchemaFromEvent with streamId containing ':' still sends the event", async () => {
163
- const consoleWarnSpy = jest.spyOn(console, "warn").mockImplementation(() => {});
164
-
165
- await inspector.trackSchemaFromEvent(eventName, properties, "bad:stream");
166
-
167
- // Warning is logged
168
- expect(consoleWarnSpy).toHaveBeenCalledWith(
169
- "[Avo Inspector] Warning: streamId contains ':' which is not supported"
170
- );
171
-
172
- // Event is still sent despite the warning
173
- expect(inspector.avoNetworkCallsHandler.callInspectorWithBatchBody).toHaveBeenCalledTimes(1);
174
- expect(inspector.avoNetworkCallsHandler.callInspectorWithBatchBody).toHaveBeenCalledWith([
175
- expect.objectContaining({
176
- type: "event",
177
- anonymousId: "bad:stream",
178
- }),
179
- ]);
180
-
181
- consoleWarnSpy.mockRestore();
182
- });
183
-
184
- test("same event with different streamIds is not deduplicated", async () => {
185
- await inspector.trackSchemaFromEvent(eventName, properties, "stream-a");
186
- await inspector.trackSchemaFromEvent(eventName, properties, "stream-b");
187
-
188
- expect(inspector.avoNetworkCallsHandler.callInspectorWithBatchBody).toHaveBeenCalledTimes(2);
189
- });
190
-
191
- test("trackSchemaFromEvent rejection contains the expected error string", async () => {
192
- const consoleErrorSpy = jest.spyOn(console, "error").mockImplementation(() => {});
193
-
194
- // Force the catch block in trackSchemaFromEvent by making extractSchema throw
195
- jest.spyOn(inspector, "extractSchema").mockImplementation(() => {
196
- throw new Error("test explosion");
197
- });
198
-
199
- await expect(
200
- inspector.trackSchemaFromEvent(eventName, properties)
201
- ).rejects.toBe(
202
- "Avo Inspector: something went wrong. Please report to support@avo.app."
203
- );
204
-
205
- consoleErrorSpy.mockRestore();
206
- (inspector.extractSchema as jest.Mock).mockRestore();
207
- });
208
- });