node-avo-inspector 1.1.1 → 1.2.0

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 (93) hide show
  1. package/.claude/scheduled_tasks.lock +1 -0
  2. package/.claude/settings.local.json +11 -0
  3. package/.claude/worktrees/release-1.1.1/LICENSE +21 -0
  4. package/.claude/worktrees/release-1.1.1/README.md +69 -0
  5. package/.claude/worktrees/release-1.1.1/examples/node-inspector-js/index.js +30 -0
  6. package/.claude/worktrees/release-1.1.1/examples/node-inspector-js/package.json +15 -0
  7. package/.claude/worktrees/release-1.1.1/examples/node-inspector-js/yarn.lock +6 -0
  8. package/.claude/worktrees/release-1.1.1/examples/node-inspector-ts/dist/index.js +34 -0
  9. package/.claude/worktrees/release-1.1.1/examples/node-inspector-ts/index.ts +30 -0
  10. package/.claude/worktrees/release-1.1.1/examples/node-inspector-ts/package.json +18 -0
  11. package/.claude/worktrees/release-1.1.1/examples/node-inspector-ts/tsconfig.json +15 -0
  12. package/.claude/worktrees/release-1.1.1/examples/node-inspector-ts/yarn.lock +11 -0
  13. package/.claude/worktrees/release-1.1.1/package-lock.json +4039 -0
  14. package/.claude/worktrees/release-1.1.1/package.json +48 -0
  15. package/.claude/worktrees/release-1.1.1/reason/AvoInspector.rei +11 -0
  16. package/.claude/worktrees/release-1.1.1/reason/NodeAvoInspector.re +27 -0
  17. package/.claude/worktrees/release-1.1.1/src/AvoDeduplicator.ts +146 -0
  18. package/.claude/worktrees/release-1.1.1/src/AvoEncryption.ts +90 -0
  19. package/.claude/worktrees/release-1.1.1/src/AvoGuid.ts +9 -0
  20. package/.claude/worktrees/release-1.1.1/src/AvoInspector.ts +383 -0
  21. package/.claude/worktrees/release-1.1.1/src/AvoInspectorEnv.ts +8 -0
  22. package/.claude/worktrees/release-1.1.1/src/AvoNetworkCallsHandler.ts +267 -0
  23. package/.claude/worktrees/release-1.1.1/src/AvoSchemaParser.ts +115 -0
  24. package/.claude/worktrees/release-1.1.1/src/AvoStreamId.ts +16 -0
  25. package/.claude/worktrees/release-1.1.1/src/__tests__/TestAvoEncryption.ts +515 -0
  26. package/.claude/worktrees/release-1.1.1/src/__tests__/TestAvoEventSpecCache.ts +242 -0
  27. package/.claude/worktrees/release-1.1.1/src/__tests__/TestAvoEventSpecFetcher.ts +241 -0
  28. package/.claude/worktrees/release-1.1.1/src/__tests__/TestAvoInspector.ts +360 -0
  29. package/.claude/worktrees/release-1.1.1/src/__tests__/TestAvoStreamId.ts +72 -0
  30. package/.claude/worktrees/release-1.1.1/src/__tests__/TestDeduplicator.ts +191 -0
  31. package/.claude/worktrees/release-1.1.1/src/__tests__/TestEventValidator.ts +231 -0
  32. package/.claude/worktrees/release-1.1.1/src/__tests__/TestNetworkCallsHandler.ts +179 -0
  33. package/.claude/worktrees/release-1.1.1/src/__tests__/TestNetworkCallsHandlerPublic.ts +208 -0
  34. package/.claude/worktrees/release-1.1.1/src/__tests__/TestParsing.ts +153 -0
  35. package/.claude/worktrees/release-1.1.1/src/__tests__/constants.ts +63 -0
  36. package/.claude/worktrees/release-1.1.1/src/eventSpec/AvoEventSpecCache.ts +109 -0
  37. package/.claude/worktrees/release-1.1.1/src/eventSpec/AvoEventSpecFetchTypes.ts +47 -0
  38. package/.claude/worktrees/release-1.1.1/src/eventSpec/AvoEventSpecFetcher.ts +106 -0
  39. package/.claude/worktrees/release-1.1.1/src/eventSpec/EventValidator.ts +74 -0
  40. package/.claude/worktrees/release-1.1.1/src/index.ts +2 -0
  41. package/.claude/worktrees/release-1.1.1/src/utils.ts +49 -0
  42. package/.claude/worktrees/release-1.1.1/tsconfig.json +13 -0
  43. package/.claude/worktrees/release-1.1.1/yarn.lock +2310 -0
  44. package/.github/workflows/claude.yml +55 -0
  45. package/dist/AvoDeduplicator.d.ts +4 -3
  46. package/dist/AvoDeduplicator.js +25 -24
  47. package/dist/AvoEncryption.d.ts +26 -0
  48. package/dist/AvoEncryption.js +77 -0
  49. package/dist/AvoInspector.d.ts +34 -3
  50. package/dist/AvoInspector.js +182 -36
  51. package/dist/AvoNetworkCallsHandler.d.ts +39 -13
  52. package/dist/AvoNetworkCallsHandler.js +127 -33
  53. package/dist/AvoStreamId.d.ts +5 -0
  54. package/dist/AvoStreamId.js +15 -0
  55. package/dist/__tests__/TestAvoEncryption.d.ts +1 -0
  56. package/dist/__tests__/TestAvoEncryption.js +288 -0
  57. package/dist/__tests__/TestAvoEventSpecCache.d.ts +1 -0
  58. package/dist/__tests__/TestAvoEventSpecCache.js +194 -0
  59. package/dist/__tests__/TestAvoEventSpecFetcher.d.ts +1 -0
  60. package/dist/__tests__/TestAvoEventSpecFetcher.js +219 -0
  61. package/dist/__tests__/TestAvoInspector.js +76 -2
  62. package/dist/__tests__/TestAvoStreamId.d.ts +1 -0
  63. package/dist/__tests__/TestAvoStreamId.js +55 -0
  64. package/dist/__tests__/TestEventValidator.d.ts +1 -0
  65. package/dist/__tests__/TestEventValidator.js +196 -0
  66. package/dist/__tests__/TestNetworkCallsHandler.js +37 -10
  67. package/dist/__tests__/TestNetworkCallsHandlerPublic.js +74 -41
  68. package/dist/eventSpec/AvoEventSpecCache.d.ts +19 -0
  69. package/dist/eventSpec/AvoEventSpecCache.js +100 -0
  70. package/dist/eventSpec/AvoEventSpecFetchTypes.d.ts +41 -0
  71. package/dist/eventSpec/AvoEventSpecFetchTypes.js +5 -0
  72. package/dist/eventSpec/AvoEventSpecFetcher.d.ts +25 -0
  73. package/dist/eventSpec/AvoEventSpecFetcher.js +159 -0
  74. package/dist/eventSpec/EventValidator.d.ts +15 -0
  75. package/dist/eventSpec/EventValidator.js +53 -0
  76. package/package.json +4 -2
  77. package/src/AvoDeduplicator.ts +31 -27
  78. package/src/AvoEncryption.ts +90 -0
  79. package/src/AvoInspector.ts +275 -49
  80. package/src/AvoNetworkCallsHandler.ts +205 -45
  81. package/src/AvoStreamId.ts +16 -0
  82. package/src/__tests__/TestAvoEncryption.ts +515 -0
  83. package/src/__tests__/TestAvoEventSpecCache.ts +242 -0
  84. package/src/__tests__/TestAvoEventSpecFetcher.ts +279 -0
  85. package/src/__tests__/TestAvoInspector.ts +114 -2
  86. package/src/__tests__/TestAvoStreamId.ts +72 -0
  87. package/src/__tests__/TestEventValidator.ts +231 -0
  88. package/src/__tests__/TestNetworkCallsHandler.ts +82 -15
  89. package/src/__tests__/TestNetworkCallsHandlerPublic.ts +110 -44
  90. package/src/eventSpec/AvoEventSpecCache.ts +119 -0
  91. package/src/eventSpec/AvoEventSpecFetchTypes.ts +47 -0
  92. package/src/eventSpec/AvoEventSpecFetcher.ts +186 -0
  93. package/src/eventSpec/EventValidator.ts +74 -0
@@ -0,0 +1,72 @@
1
+ import { AvoStreamId } from "../AvoStreamId";
2
+
3
+ describe("AvoStreamId", () => {
4
+ let consoleWarnSpy: jest.SpyInstance;
5
+
6
+ beforeEach(() => {
7
+ consoleWarnSpy = jest.spyOn(console, "warn").mockImplementation(() => {});
8
+ });
9
+
10
+ afterEach(() => {
11
+ jest.restoreAllMocks();
12
+ });
13
+
14
+ test("streamId getter returns empty string by default", () => {
15
+ const avoStreamId = new AvoStreamId();
16
+ expect(avoStreamId.streamId).toBe("");
17
+ });
18
+
19
+ test("streamId getter returns the value passed to constructor", () => {
20
+ const avoStreamId = new AvoStreamId("my-stream-id");
21
+ expect(avoStreamId.streamId).toBe("my-stream-id");
22
+ });
23
+
24
+ test("streamId containing ':' logs a warning", () => {
25
+ const avoStreamId = new AvoStreamId("invalid:stream:id");
26
+ const id = avoStreamId.streamId;
27
+
28
+ expect(consoleWarnSpy).toHaveBeenCalledWith(
29
+ "[Avo Inspector] Warning: streamId contains ':' which is not supported"
30
+ );
31
+ expect(id).toBe("invalid:stream:id");
32
+ });
33
+
34
+ test("streamId without ':' does not log a warning", () => {
35
+ const avoStreamId = new AvoStreamId("valid-stream-id");
36
+ const id = avoStreamId.streamId;
37
+
38
+ expect(consoleWarnSpy).not.toHaveBeenCalled();
39
+ expect(id).toBe("valid-stream-id");
40
+ });
41
+
42
+ test("empty streamId does not log a warning", () => {
43
+ const avoStreamId = new AvoStreamId("");
44
+ const id = avoStreamId.streamId;
45
+
46
+ expect(consoleWarnSpy).not.toHaveBeenCalled();
47
+ expect(id).toBe("");
48
+ });
49
+
50
+ test("explicit undefined arg returns empty string and does not warn", () => {
51
+ const avoStreamId = new AvoStreamId(undefined);
52
+ const id = avoStreamId.streamId;
53
+
54
+ expect(consoleWarnSpy).not.toHaveBeenCalled();
55
+ expect(id).toBe("");
56
+ });
57
+
58
+ test("warning is only logged once per instance, not on every .streamId access", () => {
59
+ const avoStreamId = new AvoStreamId("bad:id");
60
+
61
+ // The warning is logged in the constructor, so it's already been called once
62
+ expect(consoleWarnSpy).toHaveBeenCalledTimes(1);
63
+
64
+ // Access streamId multiple times
65
+ avoStreamId.streamId;
66
+ avoStreamId.streamId;
67
+ avoStreamId.streamId;
68
+
69
+ // Should still be only one warning total
70
+ expect(consoleWarnSpy).toHaveBeenCalledTimes(1);
71
+ });
72
+ });
@@ -0,0 +1,231 @@
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
+ });
@@ -15,7 +15,6 @@ describe("NetworkCallsHandler", () => {
15
15
  let networkHandler: AvoNetworkCallsHandler;
16
16
  let baseBody: BaseBody;
17
17
 
18
- const customCallback = jest.fn();
19
18
  const now = new Date();
20
19
 
21
20
  beforeAll(() => {
@@ -43,8 +42,9 @@ describe("NetworkCallsHandler", () => {
43
42
  libPlatform: "node",
44
43
  messageId: mockedReturns.GUID,
45
44
  trackingId: "",
45
+ sessionId: "",
46
+ anonymousId: "",
46
47
  createdAt: new Date().toISOString(),
47
- sessionId: mockedReturns.SESSION_ID,
48
48
  samplingRate: 1.0,
49
49
  };
50
50
  });
@@ -53,23 +53,12 @@ describe("NetworkCallsHandler", () => {
53
53
  jest.clearAllMocks();
54
54
  });
55
55
 
56
- test("bodyForSessionStartedCall returns base body + session started body used for session started", () => {
57
- const body = networkHandler.bodyForSessionStartedCall(
58
- mockedReturns.SESSION_ID
59
- );
60
-
61
- expect(body).toEqual({
62
- ...baseBody,
63
- type: "sessionStarted",
64
- });
65
- });
66
-
67
56
  test("bodyForEventSchemaCall returns base body + event schema used for event sending from non Codegen", () => {
68
57
  const eventName = "event name";
69
58
  const eventProperties = [{ propertyName: "prop0", propertyType: "string" }];
70
59
 
71
60
  const body = networkHandler.bodyForEventSchemaCall(
72
- mockedReturns.SESSION_ID,
61
+ "",
73
62
  eventName,
74
63
  eventProperties,
75
64
  null,
@@ -78,6 +67,7 @@ describe("NetworkCallsHandler", () => {
78
67
 
79
68
  expect(body).toEqual({
80
69
  ...baseBody,
70
+ anonymousId: "",
81
71
  type: "event",
82
72
  eventName,
83
73
  eventProperties,
@@ -94,7 +84,7 @@ describe("NetworkCallsHandler", () => {
94
84
  const eventProperties = [{ propertyName: "prop0", propertyType: "string" }];
95
85
 
96
86
  const body = networkHandler.bodyForEventSchemaCall(
97
- mockedReturns.SESSION_ID,
87
+ "",
98
88
  eventName,
99
89
  eventProperties,
100
90
  eventId,
@@ -103,6 +93,7 @@ describe("NetworkCallsHandler", () => {
103
93
 
104
94
  expect(body).toEqual({
105
95
  ...baseBody,
96
+ anonymousId: "",
106
97
  type: "event",
107
98
  eventName,
108
99
  eventProperties,
@@ -111,4 +102,80 @@ describe("NetworkCallsHandler", () => {
111
102
  eventHash,
112
103
  });
113
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
+ });
114
181
  });
@@ -20,7 +20,6 @@ describe("AvoNetworkCallsHandler", () => {
20
20
  };
21
21
 
22
22
  const happyPathInspectorCall = () => {
23
- inspector.avoNetworkCallsHandler["samplingRate"] = 0.1;
24
23
  return Promise.resolve();
25
24
  };
26
25
 
@@ -44,13 +43,14 @@ describe("AvoNetworkCallsHandler", () => {
44
43
  inspector.avoDeduplicator._clearEvents();
45
44
  });
46
45
 
47
- test("handleTrackSchema is called on trackSchemaFromEvent", async () => {
46
+ test("trackSchemaFromEvent sends only event call (no sessionStarted)", async () => {
48
47
  await inspector.trackSchemaFromEvent(eventName, properties);
49
48
 
50
- expect(inspector.avoNetworkCallsHandler.callInspectorWithBatchBody).toHaveBeenCalledTimes(2);
49
+ // Should be called once (event only, no sessionStarted)
50
+ expect(inspector.avoNetworkCallsHandler.callInspectorWithBatchBody).toHaveBeenCalledTimes(1);
51
51
  expect(inspector.avoNetworkCallsHandler.callInspectorWithBatchBody).toHaveBeenCalledWith([
52
52
  expect.objectContaining({
53
- type: "sessionStarted",
53
+ type: "event",
54
54
  apiKey: apiKey,
55
55
  appName: appName,
56
56
  appVersion: version,
@@ -58,32 +58,49 @@ describe("AvoNetworkCallsHandler", () => {
58
58
  env: env,
59
59
  libPlatform: "node",
60
60
  messageId: expect.anything(),
61
- trackingId: "",
61
+ anonymousId: "",
62
62
  createdAt: expect.any(String),
63
- sessionId: expect.anything(),
64
63
  samplingRate: 1,
65
64
  }),
66
- ]
67
- );
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);
68
72
  expect(inspector.avoNetworkCallsHandler.callInspectorWithBatchBody).toHaveBeenCalledWith([
69
73
  expect.objectContaining({
70
74
  type: "event",
71
- apiKey: apiKey,
72
- appName: appName,
73
- appVersion: version,
74
- libVersion: expect.anything(),
75
- env: env,
76
- libPlatform: "node",
77
- messageId: expect.anything(),
78
- trackingId: "",
79
- createdAt: expect.anything(),
80
- sessionId: expect.anything(),
81
- samplingRate: 0.1,
75
+ anonymousId: "user-stream-123",
82
76
  }),
83
77
  ]);
84
78
  });
85
79
 
86
- test("handleTrackSchema is called on _avoFunctionTrackSchemaFromEvent", async () => {
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 () => {
87
104
  const eventId = "testId";
88
105
  const eventHash = "testHash";
89
106
 
@@ -95,24 +112,8 @@ describe("AvoNetworkCallsHandler", () => {
95
112
  eventHash
96
113
  );
97
114
 
98
- expect(inspector.avoNetworkCallsHandler.callInspectorWithBatchBody).toHaveBeenCalledTimes(2);
99
- expect(inspector.avoNetworkCallsHandler.callInspectorWithBatchBody).toBeCalledWith([
100
- expect.objectContaining({
101
- type: "sessionStarted",
102
- apiKey: apiKey,
103
- appName: appName,
104
- appVersion: version,
105
- libVersion: expect.anything(),
106
- env: env,
107
- libPlatform: "node",
108
- messageId: expect.anything(),
109
- trackingId: "",
110
- createdAt: expect.anything(),
111
- sessionId: expect.anything(),
112
- samplingRate: 0.1,
113
- })
114
- ]
115
- );
115
+ // Should be called once (event only, no sessionStarted)
116
+ expect(inspector.avoNetworkCallsHandler.callInspectorWithBatchBody).toHaveBeenCalledTimes(1);
116
117
  expect(inspector.avoNetworkCallsHandler.callInspectorWithBatchBody).toHaveBeenCalledWith([
117
118
  expect.objectContaining({
118
119
  type: "event",
@@ -123,20 +124,85 @@ describe("AvoNetworkCallsHandler", () => {
123
124
  env: env,
124
125
  libPlatform: "node",
125
126
  messageId: expect.anything(),
126
- trackingId: "",
127
- createdAt: expect.anything(),
128
- sessionId: expect.anything(),
129
- samplingRate: 0.1,
127
+ anonymousId: "",
128
+ createdAt: expect.any(String),
130
129
  }),
131
130
  ]);
132
131
  });
133
132
 
134
133
  test("an error is logged if event schema call fails and AvoInspector.shouldLog is true", async () => {
135
- inspectorCallSpy.mockImplementationOnce(happyPathInspectorCall);
134
+ const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
136
135
  inspectorCallSpy.mockRejectedValueOnce('Network error');
137
136
 
138
137
  await inspector.trackSchemaFromEvent(eventName, properties);
139
138
 
140
- expect(consoleLogSpy).toHaveBeenCalledWith('Avo Inspector: schema sending failed: Network error.');
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();
141
207
  });
142
208
  });