-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathdebug.test.mts
More file actions
443 lines (341 loc) · 12 KB
/
debug.test.mts
File metadata and controls
443 lines (341 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/**
* Unit tests for debug utilities.
*
* Purpose:
* Tests debug logging and diagnostic utilities. Validates conditional logging and debug mode detection.
*
* Test Coverage:
* - Debug mode detection
* - Conditional logging
* - Log level filtering
* - Debug namespace support
* - Performance timing
*
* Testing Approach:
* Tests debug utility functions with environment variable mocking.
*
* Related Files:
* - utils/debug.mts (implementation)
*/
import { beforeEach, describe, expect, it, vi } from 'vitest'
// Mock the registry debug functions.
const mockDebug = vi.hoisted(() => vi.fn())
const mockDebugCache = vi.hoisted(() => vi.fn())
const mockDebugDir = vi.hoisted(() => vi.fn())
const mockDebugDirNs = vi.hoisted(() => vi.fn())
const mockDebugNs = vi.hoisted(() => vi.fn())
const mockIsDebug = vi.hoisted(() => vi.fn(() => false))
const mockIsDebugNs = vi.hoisted(() => vi.fn(() => false))
vi.mock('@socketsecurity/lib/debug', () => ({
debug: mockDebug,
debugCache: mockDebugCache,
debugDir: mockDebugDir,
debugDirNs: mockDebugDirNs,
debugNs: mockDebugNs,
isDebug: mockIsDebug,
isDebugNs: mockIsDebugNs,
}))
import { debug, debugDir, debugNs } from '@socketsecurity/lib/debug'
import {
debugApiRequest,
debugApiResponse,
debugConfig,
debugFileOp,
debugGit,
debugScan,
} from '../../../src/utils/debug.mts'
describe('debug utilities', () => {
beforeEach(() => {
vi.clearAllMocks()
mockIsDebug.mockReturnValue(false)
mockIsDebugNs.mockReturnValue(false)
})
describe('debugApiRequest', () => {
it('logs request when silly debug is enabled', () => {
mockIsDebugNs.mockReturnValue(true)
debugApiRequest('GET', '/api/test', 5000)
expect(debugNs).toHaveBeenCalled()
const call = mockDebugNs.mock.calls[0]
expect(call?.[0]).toBe('silly')
expect(call?.[1]).toContain('GET')
expect(call?.[1]).toContain('/api/test')
expect(call?.[1]).toContain('5000ms')
})
it('does not log when silly debug is disabled', () => {
mockIsDebugNs.mockReturnValue(false)
debugApiRequest('POST', '/api/scan', 10000)
expect(debugNs).not.toHaveBeenCalled()
})
it('handles request without timeout', () => {
mockIsDebugNs.mockReturnValue(true)
debugApiRequest('DELETE', '/api/resource')
expect(debugNs).toHaveBeenCalled()
const call = mockDebugNs.mock.calls[0]
expect(call?.[1]).toContain('DELETE')
expect(call?.[1]).not.toContain('timeout')
})
})
describe('debugApiResponse', () => {
it('logs error when error is provided', () => {
const error = new Error('API failed')
debugApiResponse('/api/test', undefined, error)
expect(debugDir).toHaveBeenCalledWith({
endpoint: '/api/test',
error: 'API failed',
})
})
it('logs warning for HTTP error status codes', () => {
debugApiResponse('/api/test', 404)
expect(debug).toHaveBeenCalledWith('API /api/test: HTTP 404')
})
it('logs notice for successful responses when debug is enabled', () => {
mockIsDebugNs.mockReturnValue(true)
debugApiResponse('/api/test', 200)
expect(debugNs).toHaveBeenCalledWith('notice', 'API /api/test: 200')
})
it('does not log for successful responses when debug is disabled', () => {
mockIsDebugNs.mockReturnValue(false)
debugApiResponse('/api/test', 200)
expect(debugNs).not.toHaveBeenCalled()
})
it('handles non-Error objects in error parameter', () => {
debugApiResponse('/api/test', undefined, 'String error')
expect(debugDir).toHaveBeenCalledWith({
endpoint: '/api/test',
error: 'Unknown error',
})
})
it('includes request info in error logging', () => {
const error = new Error('Request failed')
const requestInfo = {
method: 'POST',
url: 'https://api.socket.dev/test',
durationMs: 1500,
headers: {
'Content-Type': 'application/json',
Authorization: 'Bearer secret-token',
},
}
debugApiResponse('/api/test', undefined, error, requestInfo)
const calledWith = mockDebugDir.mock.calls[0]?.[0]
expect(calledWith.method).toBe('POST')
expect(calledWith.url).toBe('https://api.socket.dev/test')
expect(calledWith.durationMs).toBe(1500)
// Authorization should be redacted.
expect(calledWith.headers?.Authorization).toBe('[REDACTED]')
expect(calledWith.headers?.['Content-Type']).toBe('application/json')
})
it('includes request info in HTTP error logging', () => {
const requestInfo = {
method: 'GET',
url: 'https://api.socket.dev/resource',
durationMs: 500,
headers: {
'x-api-key': 'my-api-key',
},
}
debugApiResponse('/api/resource', 500, undefined, requestInfo)
const calledWith = mockDebugDir.mock.calls[0]?.[0]
expect(calledWith.status).toBe(500)
expect(calledWith.method).toBe('GET')
// API key should be redacted.
expect(calledWith.headers?.['x-api-key']).toBe('[REDACTED]')
})
it('handles partial request info', () => {
const requestInfo = {
method: 'PUT',
}
debugApiResponse('/api/update', 400, undefined, requestInfo)
const calledWith = mockDebugDir.mock.calls[0]?.[0]
expect(calledWith.method).toBe('PUT')
expect(calledWith.url).toBeUndefined()
expect(calledWith.headers).toBeUndefined()
})
it('includes requestedAt timestamp when provided', () => {
const requestInfo = {
method: 'POST',
url: 'https://api.socket.dev/x',
requestedAt: '2026-04-18T00:00:00.000Z',
}
debugApiResponse('/api/x', 500, undefined, requestInfo)
const calledWith = mockDebugDir.mock.calls[0]?.[0]
expect(calledWith.requestedAt).toBe('2026-04-18T00:00:00.000Z')
})
it('extracts cf-ray as a top-level field and keeps responseHeaders', () => {
const requestInfo = {
method: 'GET',
url: 'https://api.socket.dev/y',
responseHeaders: {
'cf-ray': 'abc123-IAD',
'content-type': 'application/json',
},
}
debugApiResponse('/api/y', 500, undefined, requestInfo)
const calledWith = mockDebugDir.mock.calls[0]?.[0]
expect(calledWith.cfRay).toBe('abc123-IAD')
expect(calledWith.responseHeaders?.['cf-ray']).toBe('abc123-IAD')
})
it('tolerates CF-Ray header casing', () => {
const requestInfo = {
method: 'GET',
url: 'https://api.socket.dev/z',
responseHeaders: {
'CF-Ray': 'xyz789-SJC',
},
}
debugApiResponse('/api/z', 500, undefined, requestInfo)
const calledWith = mockDebugDir.mock.calls[0]?.[0]
expect(calledWith.cfRay).toBe('xyz789-SJC')
})
it('includes response body on error', () => {
const requestInfo = {
method: 'GET',
url: 'https://api.socket.dev/body',
responseBody: '{"error":"bad"}',
}
debugApiResponse('/api/body', 400, undefined, requestInfo)
const calledWith = mockDebugDir.mock.calls[0]?.[0]
expect(calledWith.responseBody).toBe('{"error":"bad"}')
})
it('truncates oversized response bodies', () => {
const bigBody = 'x'.repeat(5000)
const requestInfo = {
method: 'GET',
url: 'https://api.socket.dev/big',
responseBody: bigBody,
}
debugApiResponse('/api/big', 500, undefined, requestInfo)
const calledWith = mockDebugDir.mock.calls[0]?.[0]
expect(calledWith.responseBody).toMatch(/… \(truncated, 5000 bytes\)$/)
expect((calledWith.responseBody as string).length).toBeLessThan(
bigBody.length,
)
})
})
describe('debugFileOp', () => {
it('logs warning when error occurs', () => {
const error = new Error('File not found')
debugFileOp('read', '/path/to/file', error)
expect(debugDir).toHaveBeenCalledWith({
operation: 'read',
filepath: '/path/to/file',
error: 'File not found',
})
})
it('logs silly level for successful operations when enabled', () => {
mockIsDebugNs.mockReturnValue(true)
debugFileOp('write', '/path/to/file')
expect(debugNs).toHaveBeenCalledWith('silly', 'File write: /path/to/file')
})
it('does not log for successful operations when silly is disabled', () => {
mockIsDebugNs.mockReturnValue(false)
debugFileOp('create', '/path/to/file')
expect(debugNs).not.toHaveBeenCalled()
})
it('handles all operation types', () => {
const operations: Array<'read' | 'write' | 'delete' | 'create'> = [
'read',
'write',
'delete',
'create',
]
operations.forEach(op => {
debugFileOp(op, `/path/${op}`)
// No errors expected.
})
})
})
describe('debugScan', () => {
it('logs start phase with package count', () => {
debugScan('start', 42)
expect(debug).toHaveBeenCalledWith('Scanning 42 packages')
})
it('does not log start phase without package count', () => {
debugScan('start')
expect(debug).not.toHaveBeenCalled()
})
it('logs progress when silly debug is enabled', () => {
mockIsDebugNs.mockReturnValue(true)
debugScan('progress', 10)
expect(debugNs).toHaveBeenCalledWith(
'silly',
'Scan progress: 10 packages processed',
)
})
it('logs complete phase', () => {
debugScan('complete', 50)
expect(debugNs).toHaveBeenCalledWith(
'notice',
'Scan complete: 50 packages',
)
})
it('logs complete phase without package count', () => {
debugScan('complete')
expect(debugNs).toHaveBeenCalledWith('notice', 'Scan complete')
})
it('logs error phase with details', () => {
const errorDetails = { message: 'Scan failed' }
debugScan('error', undefined, errorDetails)
expect(debugDir).toHaveBeenCalledWith({
phase: 'scan_error',
details: errorDetails,
})
})
})
describe('debugConfig', () => {
it('logs error when provided', () => {
const error = new Error('Config invalid')
debugConfig('.socketrc', false, error)
expect(debugDir).toHaveBeenCalledWith({
source: '.socketrc',
error: 'Config invalid',
})
})
it('logs notice when config is found', () => {
debugConfig('.socketrc', true)
expect(debug).toHaveBeenCalledWith('Config loaded: .socketrc')
})
it('logs silly when config not found and debug enabled', () => {
mockIsDebugNs.mockReturnValue(true)
debugConfig('.socketrc', false)
expect(debugNs).toHaveBeenCalledWith(
'silly',
'Config not found: .socketrc',
)
})
it('does not log when config not found and debug disabled', () => {
mockIsDebugNs.mockReturnValue(false)
debugConfig('.socketrc', false)
expect(debugNs).not.toHaveBeenCalled()
})
})
describe('debugGit', () => {
it('logs warning for failed operations', () => {
debugGit('push', false, { branch: 'main' })
expect(debugDir).toHaveBeenCalledWith({
git_op: 'push',
branch: 'main',
})
})
it('logs notice for important successful operations', () => {
mockIsDebugNs.mockImplementation(level => level === 'notice')
debugGit('push', true)
expect(debugNs).toHaveBeenCalledWith('notice', 'Git push succeeded')
})
it('logs commit operations', () => {
mockIsDebugNs.mockReturnValue(true)
debugGit('commit', true)
expect(debugNs).toHaveBeenCalledWith('notice', 'Git commit succeeded')
})
it('logs other operations only with silly debug', () => {
mockIsDebugNs.mockImplementation(level => level === 'silly')
debugGit('status', true)
expect(debugNs).toHaveBeenCalledWith('silly', 'Git status')
})
it('does not log non-important operations without silly debug', () => {
mockIsDebugNs.mockReturnValue(false)
debugGit('status', true)
expect(debugNs).not.toHaveBeenCalled()
})
})
})