@app-connect/core 1.7.8 → 1.7.11
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.
- package/connector/developerPortal.js +43 -0
- package/connector/proxy/index.js +10 -3
- package/connector/registry.js +8 -6
- package/handlers/admin.js +44 -21
- package/handlers/auth.js +97 -69
- package/handlers/calldown.js +10 -4
- package/handlers/contact.js +45 -112
- package/handlers/disposition.js +4 -142
- package/handlers/log.js +174 -259
- package/handlers/user.js +19 -6
- package/index.js +310 -122
- package/lib/analytics.js +3 -1
- package/lib/authSession.js +68 -0
- package/lib/callLogComposer.js +498 -420
- package/lib/errorHandler.js +206 -0
- package/lib/jwt.js +2 -0
- package/lib/logger.js +190 -0
- package/lib/oauth.js +21 -12
- package/lib/ringcentral.js +2 -10
- package/lib/sharedSMSComposer.js +471 -0
- package/mcp/SupportedPlatforms.md +12 -0
- package/mcp/lib/validator.js +91 -0
- package/mcp/mcpHandler.js +166 -0
- package/mcp/tools/checkAuthStatus.js +90 -0
- package/mcp/tools/collectAuthInfo.js +86 -0
- package/mcp/tools/createCallLog.js +299 -0
- package/mcp/tools/createMessageLog.js +283 -0
- package/mcp/tools/doAuth.js +185 -0
- package/mcp/tools/findContactByName.js +87 -0
- package/mcp/tools/findContactByPhone.js +96 -0
- package/mcp/tools/getCallLog.js +98 -0
- package/mcp/tools/getHelp.js +39 -0
- package/mcp/tools/getPublicConnectors.js +46 -0
- package/mcp/tools/index.js +58 -0
- package/mcp/tools/logout.js +63 -0
- package/mcp/tools/rcGetCallLogs.js +73 -0
- package/mcp/tools/setConnector.js +64 -0
- package/mcp/tools/updateCallLog.js +122 -0
- package/models/accountDataModel.js +34 -0
- package/models/cacheModel.js +3 -0
- package/package.json +6 -4
- package/releaseNotes.json +36 -0
- package/test/connector/registry.test.js +145 -0
- package/test/handlers/admin.test.js +583 -0
- package/test/handlers/auth.test.js +355 -0
- package/test/handlers/contact.test.js +852 -0
- package/test/handlers/log.test.js +872 -0
- package/test/lib/callLogComposer.test.js +1231 -0
- package/test/lib/debugTracer.test.js +328 -0
- package/test/lib/logger.test.js +206 -0
- package/test/lib/oauth.test.js +359 -0
- package/test/lib/ringcentral.test.js +473 -0
- package/test/lib/sharedSMSComposer.test.js +1084 -0
- package/test/lib/util.test.js +282 -0
- package/test/mcp/tools/collectAuthInfo.test.js +192 -0
- package/test/mcp/tools/createCallLog.test.js +412 -0
- package/test/mcp/tools/createMessageLog.test.js +580 -0
- package/test/mcp/tools/doAuth.test.js +363 -0
- package/test/mcp/tools/findContactByName.test.js +263 -0
- package/test/mcp/tools/findContactByPhone.test.js +284 -0
- package/test/mcp/tools/getCallLog.test.js +286 -0
- package/test/mcp/tools/getPublicConnectors.test.js +128 -0
- package/test/mcp/tools/logout.test.js +169 -0
- package/test/mcp/tools/setConnector.test.js +177 -0
- package/test/mcp/tools/updateCallLog.test.js +346 -0
- package/test/models/accountDataModel.test.js +98 -0
- package/test/models/dynamo/connectorSchema.test.js +189 -0
- package/test/models/models.test.js +539 -0
- package/test/setup.js +176 -176
|
@@ -0,0 +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
|
+
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
const { Logger, LOG_LEVELS } = require('../../lib/logger');
|
|
2
|
+
|
|
3
|
+
describe('Logger', () => {
|
|
4
|
+
let originalEnv;
|
|
5
|
+
let consoleSpy;
|
|
6
|
+
let consoleErrorSpy;
|
|
7
|
+
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
originalEnv = process.env.NODE_ENV;
|
|
10
|
+
consoleSpy = jest.spyOn(console, 'log').mockImplementation();
|
|
11
|
+
consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation();
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
afterEach(() => {
|
|
15
|
+
process.env.NODE_ENV = originalEnv;
|
|
16
|
+
consoleSpy.mockRestore();
|
|
17
|
+
consoleErrorSpy.mockRestore();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
describe('Log Levels', () => {
|
|
21
|
+
test('should only log messages at or above the configured level', () => {
|
|
22
|
+
const logger = new Logger({ level: 'WARN' });
|
|
23
|
+
|
|
24
|
+
logger.debug('debug message');
|
|
25
|
+
logger.info('info message');
|
|
26
|
+
logger.warn('warn message');
|
|
27
|
+
logger.error('error message');
|
|
28
|
+
|
|
29
|
+
// Only warn and error should be logged
|
|
30
|
+
expect(consoleSpy).not.toHaveBeenCalled();
|
|
31
|
+
expect(consoleErrorSpy).toHaveBeenCalledTimes(2);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test('should log all messages when level is DEBUG', () => {
|
|
35
|
+
const logger = new Logger({ level: 'DEBUG' });
|
|
36
|
+
|
|
37
|
+
logger.debug('debug message');
|
|
38
|
+
logger.info('info message');
|
|
39
|
+
logger.warn('warn message');
|
|
40
|
+
logger.error('error message');
|
|
41
|
+
|
|
42
|
+
expect(consoleSpy).toHaveBeenCalledTimes(2); // debug and info
|
|
43
|
+
expect(consoleErrorSpy).toHaveBeenCalledTimes(2); // warn and error
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
describe('Production vs Development', () => {
|
|
48
|
+
test('should output JSON in production', () => {
|
|
49
|
+
process.env.NODE_ENV = 'production';
|
|
50
|
+
const logger = new Logger({ level: 'INFO' });
|
|
51
|
+
|
|
52
|
+
logger.info('test message', { userId: '123' });
|
|
53
|
+
|
|
54
|
+
const logOutput = consoleSpy.mock.calls[0][0];
|
|
55
|
+
const parsed = JSON.parse(logOutput);
|
|
56
|
+
|
|
57
|
+
expect(parsed).toHaveProperty('timestamp');
|
|
58
|
+
expect(parsed).toHaveProperty('level', 'INFO');
|
|
59
|
+
expect(parsed).toHaveProperty('message', 'test message');
|
|
60
|
+
expect(parsed).toHaveProperty('userId', '123');
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test('should output human-readable format in development', () => {
|
|
64
|
+
process.env.NODE_ENV = 'development';
|
|
65
|
+
const logger = new Logger({ level: 'INFO' });
|
|
66
|
+
|
|
67
|
+
logger.info('test message', { userId: '123' });
|
|
68
|
+
|
|
69
|
+
const logOutput = consoleSpy.mock.calls[0][0];
|
|
70
|
+
expect(logOutput).toContain('[INFO]');
|
|
71
|
+
expect(logOutput).toContain('test message');
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
describe('Error Handling', () => {
|
|
76
|
+
test('should extract error information from Error objects', () => {
|
|
77
|
+
process.env.NODE_ENV = 'production';
|
|
78
|
+
const logger = new Logger({ level: 'ERROR' });
|
|
79
|
+
const error = new Error('Test error');
|
|
80
|
+
error.response = {
|
|
81
|
+
status: 500,
|
|
82
|
+
data: { message: 'Server error' },
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
logger.error('Operation failed', { error });
|
|
86
|
+
|
|
87
|
+
const logOutput = consoleErrorSpy.mock.calls[0][0];
|
|
88
|
+
const parsed = JSON.parse(logOutput);
|
|
89
|
+
|
|
90
|
+
expect(parsed).toHaveProperty('errorMessage', 'Test error');
|
|
91
|
+
expect(parsed).toHaveProperty('errorStack');
|
|
92
|
+
expect(parsed).toHaveProperty('errorStatus', 500);
|
|
93
|
+
expect(parsed).toHaveProperty('errorResponse');
|
|
94
|
+
expect(parsed).not.toHaveProperty('error'); // Should be removed
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
describe('Child Logger', () => {
|
|
99
|
+
test('should create child logger with default context', () => {
|
|
100
|
+
process.env.NODE_ENV = 'production';
|
|
101
|
+
const logger = new Logger({ level: 'INFO' });
|
|
102
|
+
const childLogger = logger.child({ platform: 'clio', userId: '123' });
|
|
103
|
+
|
|
104
|
+
childLogger.info('test message', { operation: 'createLog' });
|
|
105
|
+
|
|
106
|
+
const logOutput = consoleSpy.mock.calls[0][0];
|
|
107
|
+
const parsed = JSON.parse(logOutput);
|
|
108
|
+
|
|
109
|
+
expect(parsed).toHaveProperty('platform', 'clio');
|
|
110
|
+
expect(parsed).toHaveProperty('userId', '123');
|
|
111
|
+
expect(parsed).toHaveProperty('operation', 'createLog');
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
test('should support nested child loggers', () => {
|
|
115
|
+
process.env.NODE_ENV = 'production';
|
|
116
|
+
const logger = new Logger({ level: 'INFO' });
|
|
117
|
+
const child1 = logger.child({ platform: 'clio' });
|
|
118
|
+
const child2 = child1.child({ userId: '123' });
|
|
119
|
+
|
|
120
|
+
child2.info('test message');
|
|
121
|
+
|
|
122
|
+
const logOutput = consoleSpy.mock.calls[0][0];
|
|
123
|
+
const parsed = JSON.parse(logOutput);
|
|
124
|
+
|
|
125
|
+
expect(parsed).toHaveProperty('platform', 'clio');
|
|
126
|
+
expect(parsed).toHaveProperty('userId', '123');
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
describe('API Request Logging', () => {
|
|
131
|
+
test('should log successful API requests at DEBUG level', () => {
|
|
132
|
+
const logger = new Logger({ level: 'DEBUG' });
|
|
133
|
+
|
|
134
|
+
logger.logApiRequest({
|
|
135
|
+
method: 'GET',
|
|
136
|
+
url: 'https://api.example.com/users',
|
|
137
|
+
status: 200,
|
|
138
|
+
duration: 150,
|
|
139
|
+
platform: 'clio',
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
expect(consoleSpy).toHaveBeenCalledTimes(1);
|
|
143
|
+
expect(consoleErrorSpy).not.toHaveBeenCalled();
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
test('should log failed API requests at ERROR level', () => {
|
|
147
|
+
const logger = new Logger({ level: 'ERROR' });
|
|
148
|
+
const error = new Error('Request failed');
|
|
149
|
+
|
|
150
|
+
logger.logApiRequest({
|
|
151
|
+
method: 'POST',
|
|
152
|
+
url: 'https://api.example.com/logs',
|
|
153
|
+
status: 500,
|
|
154
|
+
duration: 300,
|
|
155
|
+
platform: 'clio',
|
|
156
|
+
error,
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
expect(consoleErrorSpy).toHaveBeenCalledTimes(1);
|
|
160
|
+
expect(consoleSpy).not.toHaveBeenCalled();
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
test('should log 4xx errors at WARN level', () => {
|
|
164
|
+
const logger = new Logger({ level: 'WARN' });
|
|
165
|
+
|
|
166
|
+
logger.logApiRequest({
|
|
167
|
+
method: 'GET',
|
|
168
|
+
url: 'https://api.example.com/contact/999',
|
|
169
|
+
status: 404,
|
|
170
|
+
duration: 100,
|
|
171
|
+
platform: 'clio',
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
expect(consoleErrorSpy).toHaveBeenCalledTimes(1);
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
describe('Database Query Logging', () => {
|
|
179
|
+
test('should log successful queries at DEBUG level', () => {
|
|
180
|
+
const logger = new Logger({ level: 'DEBUG' });
|
|
181
|
+
|
|
182
|
+
logger.logDatabaseQuery({
|
|
183
|
+
operation: 'SELECT',
|
|
184
|
+
table: 'users',
|
|
185
|
+
duration: 50,
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
expect(consoleSpy).toHaveBeenCalledTimes(1);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
test('should log failed queries at ERROR level', () => {
|
|
192
|
+
const logger = new Logger({ level: 'ERROR' });
|
|
193
|
+
const error = new Error('Constraint violation');
|
|
194
|
+
|
|
195
|
+
logger.logDatabaseQuery({
|
|
196
|
+
operation: 'INSERT',
|
|
197
|
+
table: 'call_logs',
|
|
198
|
+
duration: 20,
|
|
199
|
+
error,
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
expect(consoleErrorSpy).toHaveBeenCalledTimes(1);
|
|
203
|
+
});
|
|
204
|
+
});
|
|
205
|
+
});
|
|
206
|
+
|