This repository was archived by the owner on Aug 7, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlog.test.js
More file actions
246 lines (205 loc) · 7.26 KB
/
Copy pathlog.test.js
File metadata and controls
246 lines (205 loc) · 7.26 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
/**
* Unit tests for input validation functions.
*/
import {jest} from '@jest/globals'
/**
* Unit tests for the log utility class.
*/
import log from '../../src/util/log.js'
jest.mock('../../src/util/log.js')
import mockLog from '@mocks/util/log.js'
const baseOptions = {
entity: 'test-entity',
token: 'myS3cr3tT0k3n',
isDebug: false,
}
const originalEnv = process.env.DEBUG
let logger
let mockLogger
/**
* Test suite for the log function and Log class.
*/
describe('log', () => {
beforeEach(() => {
// Reset environment variables that might affect tests
process.env.DEBUG = undefined
// Create a new logger instance before each test
logger = log(baseOptions.entity, baseOptions.token, baseOptions.isDebug)
mockLogger = mockLog(baseOptions.entity, baseOptions.token, baseOptions.isDebug)
})
afterEach(() => {
logger = null
// Reset the mock log instance to clear any logged messages
mockLogger = null
mockLog._reset()
// Reset the mock log instance
jest.restoreAllMocks()
// Reset environment variables to original state
process.env.DEBUG = originalEnv
})
/**
* Test basic functionality
*/
describe('basic functionality', () => {
/**
* Test singleton
* This test checks that the log function returns the same instance of the logger
* regardless of how many times it is called with the same parameters.
* It ensures that the logger is a singleton.
*/
test('should return singleton instance', () => {
const logger2 = log('different', 'values', true)
expect(logger).toBe(logger2)
})
/**
* Test constructor
* This test checks that the logger instance is created with the correct properties.
*/
test('constructor should set properties correctly', () => {
expect(logger.entity).toBe('test-entity')
expect(logger.isDebug).toBe(false)
})
/**
* Test logger methods
* This test checks that the logger has the expected methods and they are functions.
*/
test('logger should have expected methods', () => {
expect(logger).toHaveProperty('error')
expect(logger).toHaveProperty('warn')
expect(logger).toHaveProperty('info')
expect(logger).toHaveProperty('debug')
expect(logger).toHaveProperty('start')
expect(logger).toHaveProperty('stopAndPersist')
expect(logger).toHaveProperty('fail')
expect(logger).toHaveProperty('text')
expect(typeof logger.error).toBe('function')
expect(typeof logger.warn).toBe('function')
expect(typeof logger.info).toBe('function')
expect(typeof logger.debug).toBe('function')
expect(typeof logger.start).toBe('function')
expect(typeof logger.stopAndPersist).toBe('function')
expect(typeof logger.fail).toBe('function')
expect(typeof logger.text).toBe('string')
})
})
/**
* Test logging methods
*/
describe('logging methods', () => {
/**
* Test info method
*/
test('info method should log messages correctly', () => {
mockLogger.info('This is an info message')
expect(mockLogger.infos.length).toBe(1)
expect(mockLogger.infos[0].message).toBe('This is an info message')
})
/**
* Test warn method
*/
test('warn method should log messages correctly', () => {
mockLogger.warn('This is a warning message')
expect(mockLogger.warns.length).toBe(1)
expect(mockLogger.warns[0].message).toBe('This is a warning message')
})
/**
* Test debug method when debug mode is disabled
*/
test('debug method should not log messages when debug mode is disabled', () => {
// Test without debug mode enabled
mockLogger.debug('This is a debug message')
expect(mockLogger.debugs.length).toBe(0)
})
/**
* Test debug method with debug mode enabled
*/
test('debug method should log messages correctly', () => {
const debugLogger = mockLog._createNew(baseOptions.entity, baseOptions.token, true)
debugLogger.debug('This is a debug message')
expect(debugLogger.debugs.length).toBe(1)
expect(debugLogger.debugs[0].message).toBe('This is a debug message')
})
/**
* Test error method
*/
test('error method should log messages correctly', () => {
mockLogger.error('This is an error message')
expect(mockLogger.errors.length).toBe(1)
expect(mockLogger.errors[0].message).toBe('This is an error message')
})
})
/**
* Test spinner functionality
*/
describe('spinner functionality', () => {
/**
* Test spinner functionality
*/
test('spinner start and success methods should work correctly', () => {
mockLogger.start('Starting process...')
expect(mockLogger.spinnerActive).toBe(true)
expect(mockLogger.currentText).toBe('Starting process...')
mockLogger.stopAndPersist({text: 'Process completed'})
expect(mockLogger.spinnerActive).toBe(false)
expect(mockLogger.spinnerLogs[1].text).toContain('Process completed')
})
/**
* Test spinner functionality
*/
test('spinner start and fail methods should work correctly', () => {
mockLogger.start('Starting process...')
mockLogger.fail('Process failed')
expect(mockLogger.spinnerActive).toBe(false)
expect(mockLogger.spinnerLogs[1].text).toContain('Process failed')
})
})
})
/**
* Tests for security-related behavior using the real Log class.
*/
import {Log} from '../../src/util/log.js'
describe('Log security', () => {
describe('isDebug boolean coercion', () => {
test('should coerce truthy non-boolean values to true', () => {
const l = new Log('entity', 'token', 'yes')
expect(l.isDebug).toBe(true)
})
test('should coerce falsy non-boolean values to false', () => {
const l = new Log('entity', 'token', 0)
expect(l.isDebug).toBe(false)
})
test('should keep explicit boolean true', () => {
const l = new Log('entity', 'token', true)
expect(l.isDebug).toBe(true)
})
test('should keep explicit boolean false', () => {
const l = new Log('entity', 'token', false)
expect(l.isDebug).toBe(false)
})
})
describe('maskSensitive prototype pollution guard', () => {
test('should not process __proto__ keys', () => {
const l = new Log('entity', 'secret123', false)
const input = JSON.parse('{"__proto__": {"polluted": true}, "safe": "secret123"}')
const result = l.maskSensitive(input)
expect(result.safe).toBe('***')
const emptyObj = {}
expect(emptyObj.polluted).toBeUndefined()
})
test('should not process constructor keys', () => {
const l = new Log('entity', 'secret123', false)
const input = {constructor: 'secret123', normal: 'secret123'}
const result = l.maskSensitive(input)
expect(result.normal).toBe('***')
// constructor key string value should still be masked for security
expect(result.constructor).toBe('***')
})
test('should still mask tokens in regular properties', () => {
const l = new Log('entity', 'secret123', false)
const input = {name: 'hello secret123 world', nested: {value: 'secret123'}}
const result = l.maskSensitive(input)
expect(result.name).toBe('hello *** world')
expect(result.nested.value).toBe('***')
})
})
})