@app-connect/core 1.7.25 → 1.7.27

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 (138) hide show
  1. package/.env.test +5 -5
  2. package/README.md +441 -441
  3. package/connector/developerPortal.js +31 -31
  4. package/connector/mock.js +84 -77
  5. package/connector/proxy/engine.js +164 -164
  6. package/connector/proxy/index.js +500 -500
  7. package/connector/registry.js +252 -252
  8. package/docs/README.md +50 -50
  9. package/docs/architecture.md +93 -93
  10. package/docs/connectors.md +116 -116
  11. package/docs/handlers.md +125 -125
  12. package/docs/libraries.md +101 -101
  13. package/docs/models.md +144 -144
  14. package/docs/routes.md +115 -115
  15. package/docs/tests.md +73 -73
  16. package/handlers/admin.js +523 -523
  17. package/handlers/appointment.js +193 -0
  18. package/handlers/auth.js +320 -296
  19. package/handlers/calldown.js +99 -99
  20. package/handlers/contact.js +280 -280
  21. package/handlers/disposition.js +82 -80
  22. package/handlers/log.js +990 -973
  23. package/handlers/managedAuth.js +446 -446
  24. package/handlers/managedOAuth.js +247 -0
  25. package/handlers/plugin.js +208 -208
  26. package/handlers/user.js +142 -142
  27. package/index.js +3162 -2652
  28. package/jest.config.js +56 -56
  29. package/lib/analytics.js +54 -54
  30. package/lib/authSession.js +109 -109
  31. package/lib/cacheCleanup.js +21 -0
  32. package/lib/callLogComposer.js +898 -898
  33. package/lib/callLogLookup.js +34 -0
  34. package/lib/constants.js +8 -8
  35. package/lib/debugTracer.js +177 -177
  36. package/lib/encode.js +30 -30
  37. package/lib/errorHandler.js +218 -206
  38. package/lib/generalErrorMessage.js +41 -41
  39. package/lib/jwt.js +18 -18
  40. package/lib/logger.js +190 -190
  41. package/lib/migrateCallLogsSchema.js +116 -0
  42. package/lib/ringcentral.js +266 -266
  43. package/lib/s3ErrorLogReport.js +65 -65
  44. package/lib/sharedSMSComposer.js +471 -471
  45. package/lib/util.js +67 -67
  46. package/mcp/README.md +412 -395
  47. package/mcp/lib/validator.js +91 -91
  48. package/mcp/mcpHandler.js +425 -425
  49. package/mcp/tools/cancelAppointment.js +101 -0
  50. package/mcp/tools/checkAuthStatus.js +105 -105
  51. package/mcp/tools/confirmAppointment.js +101 -0
  52. package/mcp/tools/createAppointment.js +157 -0
  53. package/mcp/tools/createCallLog.js +327 -316
  54. package/mcp/tools/createContact.js +117 -117
  55. package/mcp/tools/createMessageLog.js +287 -287
  56. package/mcp/tools/doAuth.js +60 -60
  57. package/mcp/tools/findContactByName.js +93 -93
  58. package/mcp/tools/findContactByPhone.js +101 -101
  59. package/mcp/tools/getCallLog.js +111 -102
  60. package/mcp/tools/getGoogleFilePicker.js +99 -99
  61. package/mcp/tools/getHelp.js +43 -43
  62. package/mcp/tools/getPublicConnectors.js +94 -94
  63. package/mcp/tools/getSessionInfo.js +90 -90
  64. package/mcp/tools/index.js +51 -41
  65. package/mcp/tools/listAppointments.js +163 -0
  66. package/mcp/tools/logout.js +99 -96
  67. package/mcp/tools/rcGetCallLogs.js +65 -65
  68. package/mcp/tools/updateAppointment.js +154 -0
  69. package/mcp/tools/updateCallLog.js +130 -126
  70. package/mcp/ui/App/App.tsx +358 -358
  71. package/mcp/ui/App/components/AuthInfoForm.tsx +113 -113
  72. package/mcp/ui/App/components/AuthSuccess.tsx +22 -22
  73. package/mcp/ui/App/components/ConnectorList.tsx +82 -82
  74. package/mcp/ui/App/components/DebugPanel.tsx +43 -43
  75. package/mcp/ui/App/components/OAuthConnect.tsx +270 -270
  76. package/mcp/ui/App/lib/callTool.ts +130 -130
  77. package/mcp/ui/App/lib/debugLog.ts +41 -41
  78. package/mcp/ui/App/lib/developerPortal.ts +111 -111
  79. package/mcp/ui/App/main.css +5 -5
  80. package/mcp/ui/App/root.tsx +13 -13
  81. package/mcp/ui/index.html +13 -13
  82. package/mcp/ui/package-lock.json +6356 -6356
  83. package/mcp/ui/package.json +25 -25
  84. package/mcp/ui/tsconfig.json +26 -26
  85. package/mcp/ui/vite.config.ts +16 -16
  86. package/models/accountDataModel.js +33 -33
  87. package/models/adminConfigModel.js +35 -35
  88. package/models/cacheModel.js +30 -26
  89. package/models/callDownListModel.js +34 -34
  90. package/models/callLogModel.js +33 -27
  91. package/models/dynamo/connectorSchema.js +146 -146
  92. package/models/dynamo/lockSchema.js +24 -24
  93. package/models/dynamo/noteCacheSchema.js +29 -29
  94. package/models/llmSessionModel.js +17 -17
  95. package/models/messageLogModel.js +25 -25
  96. package/models/sequelize.js +16 -16
  97. package/models/userModel.js +45 -45
  98. package/package.json +1 -1
  99. package/releaseNotes.json +1101 -1081
  100. package/test/connector/proxy/engine.test.js +126 -126
  101. package/test/connector/proxy/index.test.js +279 -279
  102. package/test/connector/proxy/sample.json +161 -161
  103. package/test/connector/registry.test.js +415 -415
  104. package/test/handlers/admin.test.js +616 -616
  105. package/test/handlers/auth.test.js +1092 -1018
  106. package/test/handlers/contact.test.js +1014 -1014
  107. package/test/handlers/log.test.js +1298 -1160
  108. package/test/handlers/managedAuth.test.js +457 -457
  109. package/test/handlers/plugin.test.js +380 -380
  110. package/test/index.test.js +105 -105
  111. package/test/lib/cacheCleanup.test.js +42 -0
  112. package/test/lib/callLogComposer.test.js +1231 -1231
  113. package/test/lib/debugTracer.test.js +328 -328
  114. package/test/lib/jwt.test.js +176 -176
  115. package/test/lib/logger.test.js +206 -206
  116. package/test/lib/oauth.test.js +359 -359
  117. package/test/lib/ringcentral.test.js +467 -467
  118. package/test/lib/sharedSMSComposer.test.js +1084 -1084
  119. package/test/lib/util.test.js +329 -329
  120. package/test/mcp/tools/checkAuthStatus.test.js +83 -83
  121. package/test/mcp/tools/createCallLog.test.js +436 -436
  122. package/test/mcp/tools/createContact.test.js +58 -58
  123. package/test/mcp/tools/createMessageLog.test.js +595 -595
  124. package/test/mcp/tools/doAuth.test.js +113 -113
  125. package/test/mcp/tools/findContactByName.test.js +275 -275
  126. package/test/mcp/tools/findContactByPhone.test.js +296 -296
  127. package/test/mcp/tools/getCallLog.test.js +298 -298
  128. package/test/mcp/tools/getGoogleFilePicker.test.js +281 -281
  129. package/test/mcp/tools/getPublicConnectors.test.js +107 -107
  130. package/test/mcp/tools/getSessionInfo.test.js +127 -127
  131. package/test/mcp/tools/logout.test.js +233 -233
  132. package/test/mcp/tools/rcGetCallLogs.test.js +56 -56
  133. package/test/mcp/tools/updateCallLog.test.js +360 -360
  134. package/test/models/accountDataModel.test.js +98 -98
  135. package/test/models/dynamo/connectorSchema.test.js +189 -189
  136. package/test/models/models.test.js +568 -539
  137. package/test/routes/managedAuthRoutes.test.js +235 -104
  138. package/test/setup.js +178 -178
@@ -1,328 +1,328 @@
1
- const { DebugTracer } = require('../../lib/debugTracer');
2
-
3
- describe('DebugTracer', () => {
4
- let tracer;
5
-
6
- beforeEach(() => {
7
- tracer = new DebugTracer();
8
- });
9
-
10
- describe('constructor', () => {
11
- test('should initialize with empty traces array', () => {
12
- expect(tracer.traces).toEqual([]);
13
- });
14
-
15
- test('should set start time', () => {
16
- expect(tracer.startTime).toBeDefined();
17
- expect(typeof tracer.startTime).toBe('number');
18
- });
19
-
20
- test('should generate unique request ID', () => {
21
- const tracer2 = new DebugTracer();
22
- expect(tracer.requestId).toBeDefined();
23
- expect(tracer.requestId).toMatch(/^req_\d+_[a-z0-9]+$/);
24
- expect(tracer.requestId).not.toBe(tracer2.requestId);
25
- });
26
-
27
- test('should accept headers parameter', () => {
28
- const headers = { 'x-request-id': 'custom-id' };
29
- const tracerWithHeaders = new DebugTracer(headers);
30
- expect(tracerWithHeaders.requestId).toBeDefined();
31
- });
32
- });
33
-
34
- describe('trace', () => {
35
- test('should add trace entry with method name and data', () => {
36
- tracer.trace('testMethod', { name: 'value', count: 42 });
37
-
38
- expect(tracer.traces).toHaveLength(1);
39
- expect(tracer.traces[0].methodName).toBe('testMethod');
40
- expect(tracer.traces[0].data).toEqual({ name: 'value', count: 42 });
41
- });
42
-
43
- test('should include timestamp and elapsed time', () => {
44
- tracer.trace('testMethod', {});
45
-
46
- expect(tracer.traces[0].timestamp).toBeDefined();
47
- expect(tracer.traces[0].elapsed).toBeGreaterThanOrEqual(0);
48
- });
49
-
50
- test('should default to info level', () => {
51
- tracer.trace('testMethod', {});
52
-
53
- expect(tracer.traces[0].level).toBe('info');
54
- });
55
-
56
- test('should allow custom log level', () => {
57
- tracer.trace('testMethod', {}, { level: 'warn' });
58
-
59
- expect(tracer.traces[0].level).toBe('warn');
60
- });
61
-
62
- test('should include stack trace by default', () => {
63
- tracer.trace('testMethod', {});
64
-
65
- expect(tracer.traces[0].stackTrace).toBeDefined();
66
- expect(Array.isArray(tracer.traces[0].stackTrace)).toBe(true);
67
- });
68
-
69
- test('should exclude stack trace when disabled', () => {
70
- tracer.trace('testMethod', {}, { includeStack: false });
71
-
72
- expect(tracer.traces[0].stackTrace).toBeUndefined();
73
- });
74
-
75
- test('should return this for chaining', () => {
76
- const result = tracer.trace('method1', {});
77
- expect(result).toBe(tracer);
78
-
79
- tracer.trace('method2', {}).trace('method3', {});
80
- expect(tracer.traces).toHaveLength(3);
81
- });
82
-
83
- test('should handle empty data object', () => {
84
- tracer.trace('testMethod');
85
-
86
- expect(tracer.traces[0].data).toEqual({});
87
- });
88
- });
89
-
90
- describe('traceError', () => {
91
- test('should add error trace with Error object', () => {
92
- const error = new Error('Test error message');
93
- tracer.traceError('failingMethod', error);
94
-
95
- expect(tracer.traces).toHaveLength(1);
96
- expect(tracer.traces[0].methodName).toBe('failingMethod');
97
- expect(tracer.traces[0].level).toBe('error');
98
- expect(tracer.traces[0].data.message).toBe('Test error message');
99
- expect(tracer.traces[0].data.errorStack).toContain('Error: Test error message');
100
- });
101
-
102
- test('should handle string error', () => {
103
- tracer.traceError('failingMethod', 'Something went wrong');
104
-
105
- expect(tracer.traces[0].data.message).toBe('Something went wrong');
106
- expect(tracer.traces[0].data.errorStack).toBeNull();
107
- });
108
-
109
- test('should include additional data', () => {
110
- const error = new Error('Test error');
111
- tracer.traceError('failingMethod', error, { userId: 'user-123', action: 'create' });
112
-
113
- expect(tracer.traces[0].data.userId).toBe('user-123');
114
- expect(tracer.traces[0].data.action).toBe('create');
115
- });
116
-
117
- test('should return this for chaining', () => {
118
- const result = tracer.traceError('method', new Error('test'));
119
- expect(result).toBe(tracer);
120
- });
121
- });
122
-
123
- describe('_sanitizeData', () => {
124
- test('should redact sensitive fields', () => {
125
- tracer.trace('testMethod', {
126
- accessToken: 'secret-token',
127
- refreshToken: 'secret-refresh',
128
- apiKey: 'api-key-123',
129
- password: 'secret-password',
130
- username: 'normal-field'
131
- });
132
-
133
- const data = tracer.traces[0].data;
134
- expect(data.accessToken).toBe('[REDACTED]');
135
- expect(data.refreshToken).toBe('[REDACTED]');
136
- expect(data.apiKey).toBe('[REDACTED]');
137
- expect(data.password).toBe('[REDACTED]');
138
- expect(data.username).toBe('normal-field');
139
- });
140
-
141
- test('should sanitize nested objects', () => {
142
- tracer.trace('testMethod', {
143
- user: {
144
- name: 'John',
145
- userPassword: 'secret123',
146
- userAccessToken: 'token123'
147
- }
148
- });
149
-
150
- const data = tracer.traces[0].data;
151
- expect(data.user.name).toBe('John');
152
- expect(data.user.userPassword).toBe('[REDACTED]');
153
- expect(data.user.userAccessToken).toBe('[REDACTED]');
154
- });
155
-
156
- test('should handle arrays', () => {
157
- tracer.trace('testMethod', {
158
- users: [
159
- { name: 'User1', apiKey: 'key1' },
160
- { name: 'User2', apiKey: 'key2' }
161
- ]
162
- });
163
-
164
- const data = tracer.traces[0].data;
165
- expect(data.users[0].name).toBe('User1');
166
- expect(data.users[0].apiKey).toBe('[REDACTED]');
167
- expect(data.users[1].apiKey).toBe('[REDACTED]');
168
- });
169
-
170
- test('should handle null and undefined data', () => {
171
- tracer.trace('testMethod', null);
172
- tracer.trace('testMethod2');
173
-
174
- expect(tracer.traces[0].data).toBeNull();
175
- // When undefined is passed, it defaults to {} from the default parameter
176
- expect(tracer.traces[1].data).toEqual({});
177
- });
178
-
179
- test('should handle primitive data', () => {
180
- tracer.trace('testMethod', 'string-value');
181
- tracer.trace('testMethod2', 42);
182
-
183
- expect(tracer.traces[0].data).toBe('string-value');
184
- expect(tracer.traces[1].data).toBe(42);
185
- });
186
-
187
- test('should redact case-insensitive sensitive fields', () => {
188
- tracer.trace('testMethod', {
189
- AccessToken: 'secret1',
190
- APIKEY: 'secret2',
191
- clientSecret: 'secret3'
192
- });
193
-
194
- const data = tracer.traces[0].data;
195
- expect(data.AccessToken).toBe('[REDACTED]');
196
- expect(data.APIKEY).toBe('[REDACTED]');
197
- expect(data.clientSecret).toBe('[REDACTED]');
198
- });
199
-
200
- test('should redact partial matches', () => {
201
- tracer.trace('testMethod', {
202
- userAuthToken: 'secret',
203
- adminCredentials: 'secret',
204
- myPrivateKey: 'secret'
205
- });
206
-
207
- const data = tracer.traces[0].data;
208
- expect(data.userAuthToken).toBe('[REDACTED]');
209
- expect(data.adminCredentials).toBe('[REDACTED]');
210
- expect(data.myPrivateKey).toBe('[REDACTED]');
211
- });
212
- });
213
-
214
- describe('getTraceData', () => {
215
- test('should return complete trace data', () => {
216
- tracer.trace('method1', { step: 1 });
217
- tracer.trace('method2', { step: 2 });
218
-
219
- const traceData = tracer.getTraceData();
220
-
221
- expect(traceData.requestId).toBe(tracer.requestId);
222
- expect(traceData.totalDuration).toMatch(/\d+ms$/);
223
- expect(traceData.traceCount).toBe(2);
224
- expect(traceData.traces).toHaveLength(2);
225
- });
226
-
227
- test('should return empty traces for new tracer', () => {
228
- const traceData = tracer.getTraceData();
229
-
230
- expect(traceData.traceCount).toBe(0);
231
- expect(traceData.traces).toEqual([]);
232
- });
233
- });
234
-
235
- describe('wrapResponse', () => {
236
- test('should add debug trace to response', () => {
237
- tracer.trace('processRequest', { action: 'test' });
238
-
239
- const response = { success: true, data: { id: 123 } };
240
- const wrappedResponse = tracer.wrapResponse(response);
241
-
242
- expect(wrappedResponse.success).toBe(true);
243
- expect(wrappedResponse.data).toEqual({ id: 123 });
244
- expect(wrappedResponse._debug).toBeDefined();
245
- expect(wrappedResponse._debug.requestId).toBe(tracer.requestId);
246
- });
247
-
248
- test('should preserve original response properties', () => {
249
- const response = {
250
- status: 200,
251
- message: 'OK',
252
- nested: { key: 'value' }
253
- };
254
-
255
- const wrapped = tracer.wrapResponse(response);
256
-
257
- expect(wrapped.status).toBe(200);
258
- expect(wrapped.message).toBe('OK');
259
- expect(wrapped.nested.key).toBe('value');
260
- });
261
- });
262
-
263
- describe('static fromRequest', () => {
264
- test('should create tracer from Express request', () => {
265
- const req = {
266
- headers: {
267
- 'x-request-id': 'req-123',
268
- 'content-type': 'application/json'
269
- }
270
- };
271
-
272
- const tracer = DebugTracer.fromRequest(req);
273
-
274
- expect(tracer).toBeInstanceOf(DebugTracer);
275
- expect(tracer.requestId).toBeDefined();
276
- });
277
-
278
- test('should handle request without headers', () => {
279
- const req = {};
280
-
281
- const tracer = DebugTracer.fromRequest(req);
282
-
283
- expect(tracer).toBeInstanceOf(DebugTracer);
284
- });
285
- });
286
-
287
- describe('_captureStackTrace', () => {
288
- test('should return array of stack trace lines', () => {
289
- tracer.trace('testMethod', {});
290
-
291
- const stackTrace = tracer.traces[0].stackTrace;
292
- expect(Array.isArray(stackTrace)).toBe(true);
293
- stackTrace.forEach(line => {
294
- expect(line).toMatch(/^at /);
295
- });
296
- });
297
- });
298
-
299
- describe('integration', () => {
300
- test('should track multiple operations', () => {
301
- tracer
302
- .trace('startOperation', { userId: 'user-123' })
303
- .trace('fetchData', { query: 'SELECT *' })
304
- .trace('processData', { count: 100 })
305
- .trace('saveResult', { success: true });
306
-
307
- const traceData = tracer.getTraceData();
308
- expect(traceData.traceCount).toBe(4);
309
-
310
- // Verify elapsed times are increasing
311
- for (let i = 1; i < tracer.traces.length; i++) {
312
- expect(tracer.traces[i].elapsed).toBeGreaterThanOrEqual(tracer.traces[i-1].elapsed);
313
- }
314
- });
315
-
316
- test('should handle error in the middle of operations', () => {
317
- tracer
318
- .trace('startOperation', { step: 1 })
319
- .traceError('failedOperation', new Error('Database error'), { query: 'INSERT' })
320
- .trace('cleanupOperation', { step: 3 });
321
-
322
- expect(tracer.traces).toHaveLength(3);
323
- expect(tracer.traces[1].level).toBe('error');
324
- expect(tracer.traces[2].level).toBe('info');
325
- });
326
- });
327
- });
328
-
1
+ const { DebugTracer } = require('../../lib/debugTracer');
2
+
3
+ describe('DebugTracer', () => {
4
+ let tracer;
5
+
6
+ beforeEach(() => {
7
+ tracer = new DebugTracer();
8
+ });
9
+
10
+ describe('constructor', () => {
11
+ test('should initialize with empty traces array', () => {
12
+ expect(tracer.traces).toEqual([]);
13
+ });
14
+
15
+ test('should set start time', () => {
16
+ expect(tracer.startTime).toBeDefined();
17
+ expect(typeof tracer.startTime).toBe('number');
18
+ });
19
+
20
+ test('should generate unique request ID', () => {
21
+ const tracer2 = new DebugTracer();
22
+ expect(tracer.requestId).toBeDefined();
23
+ expect(tracer.requestId).toMatch(/^req_\d+_[a-z0-9]+$/);
24
+ expect(tracer.requestId).not.toBe(tracer2.requestId);
25
+ });
26
+
27
+ test('should accept headers parameter', () => {
28
+ const headers = { 'x-request-id': 'custom-id' };
29
+ const tracerWithHeaders = new DebugTracer(headers);
30
+ expect(tracerWithHeaders.requestId).toBeDefined();
31
+ });
32
+ });
33
+
34
+ describe('trace', () => {
35
+ test('should add trace entry with method name and data', () => {
36
+ tracer.trace('testMethod', { name: 'value', count: 42 });
37
+
38
+ expect(tracer.traces).toHaveLength(1);
39
+ expect(tracer.traces[0].methodName).toBe('testMethod');
40
+ expect(tracer.traces[0].data).toEqual({ name: 'value', count: 42 });
41
+ });
42
+
43
+ test('should include timestamp and elapsed time', () => {
44
+ tracer.trace('testMethod', {});
45
+
46
+ expect(tracer.traces[0].timestamp).toBeDefined();
47
+ expect(tracer.traces[0].elapsed).toBeGreaterThanOrEqual(0);
48
+ });
49
+
50
+ test('should default to info level', () => {
51
+ tracer.trace('testMethod', {});
52
+
53
+ expect(tracer.traces[0].level).toBe('info');
54
+ });
55
+
56
+ test('should allow custom log level', () => {
57
+ tracer.trace('testMethod', {}, { level: 'warn' });
58
+
59
+ expect(tracer.traces[0].level).toBe('warn');
60
+ });
61
+
62
+ test('should include stack trace by default', () => {
63
+ tracer.trace('testMethod', {});
64
+
65
+ expect(tracer.traces[0].stackTrace).toBeDefined();
66
+ expect(Array.isArray(tracer.traces[0].stackTrace)).toBe(true);
67
+ });
68
+
69
+ test('should exclude stack trace when disabled', () => {
70
+ tracer.trace('testMethod', {}, { includeStack: false });
71
+
72
+ expect(tracer.traces[0].stackTrace).toBeUndefined();
73
+ });
74
+
75
+ test('should return this for chaining', () => {
76
+ const result = tracer.trace('method1', {});
77
+ expect(result).toBe(tracer);
78
+
79
+ tracer.trace('method2', {}).trace('method3', {});
80
+ expect(tracer.traces).toHaveLength(3);
81
+ });
82
+
83
+ test('should handle empty data object', () => {
84
+ tracer.trace('testMethod');
85
+
86
+ expect(tracer.traces[0].data).toEqual({});
87
+ });
88
+ });
89
+
90
+ describe('traceError', () => {
91
+ test('should add error trace with Error object', () => {
92
+ const error = new Error('Test error message');
93
+ tracer.traceError('failingMethod', error);
94
+
95
+ expect(tracer.traces).toHaveLength(1);
96
+ expect(tracer.traces[0].methodName).toBe('failingMethod');
97
+ expect(tracer.traces[0].level).toBe('error');
98
+ expect(tracer.traces[0].data.message).toBe('Test error message');
99
+ expect(tracer.traces[0].data.errorStack).toContain('Error: Test error message');
100
+ });
101
+
102
+ test('should handle string error', () => {
103
+ tracer.traceError('failingMethod', 'Something went wrong');
104
+
105
+ expect(tracer.traces[0].data.message).toBe('Something went wrong');
106
+ expect(tracer.traces[0].data.errorStack).toBeNull();
107
+ });
108
+
109
+ test('should include additional data', () => {
110
+ const error = new Error('Test error');
111
+ tracer.traceError('failingMethod', error, { userId: 'user-123', action: 'create' });
112
+
113
+ expect(tracer.traces[0].data.userId).toBe('user-123');
114
+ expect(tracer.traces[0].data.action).toBe('create');
115
+ });
116
+
117
+ test('should return this for chaining', () => {
118
+ const result = tracer.traceError('method', new Error('test'));
119
+ expect(result).toBe(tracer);
120
+ });
121
+ });
122
+
123
+ describe('_sanitizeData', () => {
124
+ test('should redact sensitive fields', () => {
125
+ tracer.trace('testMethod', {
126
+ accessToken: 'secret-token',
127
+ refreshToken: 'secret-refresh',
128
+ apiKey: 'api-key-123',
129
+ password: 'secret-password',
130
+ username: 'normal-field'
131
+ });
132
+
133
+ const data = tracer.traces[0].data;
134
+ expect(data.accessToken).toBe('[REDACTED]');
135
+ expect(data.refreshToken).toBe('[REDACTED]');
136
+ expect(data.apiKey).toBe('[REDACTED]');
137
+ expect(data.password).toBe('[REDACTED]');
138
+ expect(data.username).toBe('normal-field');
139
+ });
140
+
141
+ test('should sanitize nested objects', () => {
142
+ tracer.trace('testMethod', {
143
+ user: {
144
+ name: 'John',
145
+ userPassword: 'secret123',
146
+ userAccessToken: 'token123'
147
+ }
148
+ });
149
+
150
+ const data = tracer.traces[0].data;
151
+ expect(data.user.name).toBe('John');
152
+ expect(data.user.userPassword).toBe('[REDACTED]');
153
+ expect(data.user.userAccessToken).toBe('[REDACTED]');
154
+ });
155
+
156
+ test('should handle arrays', () => {
157
+ tracer.trace('testMethod', {
158
+ users: [
159
+ { name: 'User1', apiKey: 'key1' },
160
+ { name: 'User2', apiKey: 'key2' }
161
+ ]
162
+ });
163
+
164
+ const data = tracer.traces[0].data;
165
+ expect(data.users[0].name).toBe('User1');
166
+ expect(data.users[0].apiKey).toBe('[REDACTED]');
167
+ expect(data.users[1].apiKey).toBe('[REDACTED]');
168
+ });
169
+
170
+ test('should handle null and undefined data', () => {
171
+ tracer.trace('testMethod', null);
172
+ tracer.trace('testMethod2');
173
+
174
+ expect(tracer.traces[0].data).toBeNull();
175
+ // When undefined is passed, it defaults to {} from the default parameter
176
+ expect(tracer.traces[1].data).toEqual({});
177
+ });
178
+
179
+ test('should handle primitive data', () => {
180
+ tracer.trace('testMethod', 'string-value');
181
+ tracer.trace('testMethod2', 42);
182
+
183
+ expect(tracer.traces[0].data).toBe('string-value');
184
+ expect(tracer.traces[1].data).toBe(42);
185
+ });
186
+
187
+ test('should redact case-insensitive sensitive fields', () => {
188
+ tracer.trace('testMethod', {
189
+ AccessToken: 'secret1',
190
+ APIKEY: 'secret2',
191
+ clientSecret: 'secret3'
192
+ });
193
+
194
+ const data = tracer.traces[0].data;
195
+ expect(data.AccessToken).toBe('[REDACTED]');
196
+ expect(data.APIKEY).toBe('[REDACTED]');
197
+ expect(data.clientSecret).toBe('[REDACTED]');
198
+ });
199
+
200
+ test('should redact partial matches', () => {
201
+ tracer.trace('testMethod', {
202
+ userAuthToken: 'secret',
203
+ adminCredentials: 'secret',
204
+ myPrivateKey: 'secret'
205
+ });
206
+
207
+ const data = tracer.traces[0].data;
208
+ expect(data.userAuthToken).toBe('[REDACTED]');
209
+ expect(data.adminCredentials).toBe('[REDACTED]');
210
+ expect(data.myPrivateKey).toBe('[REDACTED]');
211
+ });
212
+ });
213
+
214
+ describe('getTraceData', () => {
215
+ test('should return complete trace data', () => {
216
+ tracer.trace('method1', { step: 1 });
217
+ tracer.trace('method2', { step: 2 });
218
+
219
+ const traceData = tracer.getTraceData();
220
+
221
+ expect(traceData.requestId).toBe(tracer.requestId);
222
+ expect(traceData.totalDuration).toMatch(/\d+ms$/);
223
+ expect(traceData.traceCount).toBe(2);
224
+ expect(traceData.traces).toHaveLength(2);
225
+ });
226
+
227
+ test('should return empty traces for new tracer', () => {
228
+ const traceData = tracer.getTraceData();
229
+
230
+ expect(traceData.traceCount).toBe(0);
231
+ expect(traceData.traces).toEqual([]);
232
+ });
233
+ });
234
+
235
+ describe('wrapResponse', () => {
236
+ test('should add debug trace to response', () => {
237
+ tracer.trace('processRequest', { action: 'test' });
238
+
239
+ const response = { success: true, data: { id: 123 } };
240
+ const wrappedResponse = tracer.wrapResponse(response);
241
+
242
+ expect(wrappedResponse.success).toBe(true);
243
+ expect(wrappedResponse.data).toEqual({ id: 123 });
244
+ expect(wrappedResponse._debug).toBeDefined();
245
+ expect(wrappedResponse._debug.requestId).toBe(tracer.requestId);
246
+ });
247
+
248
+ test('should preserve original response properties', () => {
249
+ const response = {
250
+ status: 200,
251
+ message: 'OK',
252
+ nested: { key: 'value' }
253
+ };
254
+
255
+ const wrapped = tracer.wrapResponse(response);
256
+
257
+ expect(wrapped.status).toBe(200);
258
+ expect(wrapped.message).toBe('OK');
259
+ expect(wrapped.nested.key).toBe('value');
260
+ });
261
+ });
262
+
263
+ describe('static fromRequest', () => {
264
+ test('should create tracer from Express request', () => {
265
+ const req = {
266
+ headers: {
267
+ 'x-request-id': 'req-123',
268
+ 'content-type': 'application/json'
269
+ }
270
+ };
271
+
272
+ const tracer = DebugTracer.fromRequest(req);
273
+
274
+ expect(tracer).toBeInstanceOf(DebugTracer);
275
+ expect(tracer.requestId).toBeDefined();
276
+ });
277
+
278
+ test('should handle request without headers', () => {
279
+ const req = {};
280
+
281
+ const tracer = DebugTracer.fromRequest(req);
282
+
283
+ expect(tracer).toBeInstanceOf(DebugTracer);
284
+ });
285
+ });
286
+
287
+ describe('_captureStackTrace', () => {
288
+ test('should return array of stack trace lines', () => {
289
+ tracer.trace('testMethod', {});
290
+
291
+ const stackTrace = tracer.traces[0].stackTrace;
292
+ expect(Array.isArray(stackTrace)).toBe(true);
293
+ stackTrace.forEach(line => {
294
+ expect(line).toMatch(/^at /);
295
+ });
296
+ });
297
+ });
298
+
299
+ describe('integration', () => {
300
+ test('should track multiple operations', () => {
301
+ tracer
302
+ .trace('startOperation', { userId: 'user-123' })
303
+ .trace('fetchData', { query: 'SELECT *' })
304
+ .trace('processData', { count: 100 })
305
+ .trace('saveResult', { success: true });
306
+
307
+ const traceData = tracer.getTraceData();
308
+ expect(traceData.traceCount).toBe(4);
309
+
310
+ // Verify elapsed times are increasing
311
+ for (let i = 1; i < tracer.traces.length; i++) {
312
+ expect(tracer.traces[i].elapsed).toBeGreaterThanOrEqual(tracer.traces[i-1].elapsed);
313
+ }
314
+ });
315
+
316
+ test('should handle error in the middle of operations', () => {
317
+ tracer
318
+ .trace('startOperation', { step: 1 })
319
+ .traceError('failedOperation', new Error('Database error'), { query: 'INSERT' })
320
+ .trace('cleanupOperation', { step: 3 });
321
+
322
+ expect(tracer.traces).toHaveLength(3);
323
+ expect(tracer.traces[1].level).toBe('error');
324
+ expect(tracer.traces[2].level).toBe('info');
325
+ });
326
+ });
327
+ });
328
+