11import { beforeEach , describe , expect , it , vi } from 'vitest'
22
3+ // Mock the registry debug functions.
4+ vi . mock ( '@socketsecurity/lib/debug' , ( ) => ( {
5+ debug : vi . fn ( ) ,
6+ debugCache : vi . fn ( ) ,
7+ debugDir : vi . fn ( ) ,
8+ debugDirNs : vi . fn ( ) ,
9+ debugNs : vi . fn ( ) ,
10+ isDebug : vi . fn ( ( ) => false ) ,
11+ isDebugNs : vi . fn ( ( ) => false ) ,
12+ } ) )
13+
14+ import {
15+ debug ,
16+ debugDir ,
17+ debugNs ,
18+ isDebug ,
19+ isDebugNs ,
20+ } from '@socketsecurity/lib/debug'
321import {
422 debugApiResponse ,
523 debugConfig ,
@@ -8,104 +26,84 @@ import {
826 debugScan ,
927} from './debug.mts'
1028
11- // Mock the registry debug functions.
12- // Mock variable to track debug function calls.
13- let debugFn : ReturnType < typeof vi . fn >
14-
1529describe ( 'debug utilities' , ( ) => {
1630 beforeEach ( ( ) => {
1731 vi . clearAllMocks ( )
32+ vi . mocked ( isDebug ) . mockReturnValue ( false )
33+ vi . mocked ( isDebugNs ) . mockReturnValue ( false )
1834 } )
1935
2036 describe ( 'debugApiResponse' , ( ) => {
21- it ( 'logs error when error is provided' , async ( ) => {
22- const { debugDir } = await import ( '@socketsecurity/lib/debug' )
37+ it ( 'logs error when error is provided' , ( ) => {
2338 const error = new Error ( 'API failed' )
2439
2540 debugApiResponse ( '/api/test' , undefined , error )
2641
27- expect ( debugDir ) . toHaveBeenCalledWith ( 'error' , {
42+ expect ( debugDir ) . toHaveBeenCalledWith ( {
2843 endpoint : '/api/test' ,
2944 error : 'API failed' ,
3045 } )
3146 } )
3247
33- it ( 'logs warning for HTTP error status codes' , async ( ) => {
34- const { debugFn } = await import ( '@socketsecurity/lib/debug' )
35-
48+ it ( 'logs warning for HTTP error status codes' , ( ) => {
3649 debugApiResponse ( '/api/test' , 404 )
3750
38- expect ( debugFn ) . toHaveBeenCalledWith ( 'warn' , 'API /api/test: HTTP 404' )
51+ expect ( debug ) . toHaveBeenCalledWith ( 'API /api/test: HTTP 404' )
3952 } )
4053
41- it ( 'logs notice for successful responses when debug is enabled' , async ( ) => {
42- const { debug : _debug , isDebug } = await import (
43- '@socketsecurity/lib/debug'
44- )
45- vi . mocked ( isDebug ) . mockReturnValue ( true )
54+ it ( 'logs notice for successful responses when debug is enabled' , ( ) => {
55+ vi . mocked ( isDebugNs ) . mockReturnValue ( true )
4656
4757 debugApiResponse ( '/api/test' , 200 )
4858
49- expect ( debugFn ) . toHaveBeenCalledWith ( 'notice' , 'API /api/test: 200' )
59+ expect ( debugNs ) . toHaveBeenCalledWith ( 'notice' , 'API /api/test: 200' )
5060 } )
5161
52- it ( 'does not log for successful responses when debug is disabled' , async ( ) => {
53- const { debug : _debug , isDebug } = await import (
54- '@socketsecurity/lib/debug'
55- )
56- vi . mocked ( isDebug ) . mockReturnValue ( false )
62+ it ( 'does not log for successful responses when debug is disabled' , ( ) => {
63+ vi . mocked ( isDebugNs ) . mockReturnValue ( false )
5764
5865 debugApiResponse ( '/api/test' , 200 )
5966
60- expect ( debugFn ) . not . toHaveBeenCalled ( )
67+ expect ( debugNs ) . not . toHaveBeenCalled ( )
6168 } )
6269
63- it ( 'handles non-Error objects in error parameter' , async ( ) => {
64- const { debugDir } = await import ( '@socketsecurity/lib/debug' )
65-
70+ it ( 'handles non-Error objects in error parameter' , ( ) => {
6671 debugApiResponse ( '/api/test' , undefined , 'String error' )
6772
68- expect ( debugDir ) . toHaveBeenCalledWith ( 'error' , {
73+ expect ( debugDir ) . toHaveBeenCalledWith ( {
6974 endpoint : '/api/test' ,
7075 error : 'Unknown error' ,
7176 } )
7277 } )
7378 } )
7479
7580 describe ( 'debugFileOp' , ( ) => {
76- it ( 'logs warning when error occurs' , async ( ) => {
77- const { debugDir } = await import ( '@socketsecurity/lib/debug' )
81+ it ( 'logs warning when error occurs' , ( ) => {
7882 const error = new Error ( 'File not found' )
7983
8084 debugFileOp ( 'read' , '/path/to/file' , error )
8185
82- expect ( debugDir ) . toHaveBeenCalledWith ( 'warn' , {
86+ expect ( debugDir ) . toHaveBeenCalledWith ( {
8387 operation : 'read' ,
8488 filepath : '/path/to/file' ,
8589 error : 'File not found' ,
8690 } )
8791 } )
8892
89- it ( 'logs silly level for successful operations when enabled' , async ( ) => {
90- const { debug : _debug , isDebug } = await import (
91- '@socketsecurity/lib/debug'
92- )
93- vi . mocked ( isDebug ) . mockReturnValue ( true )
93+ it ( 'logs silly level for successful operations when enabled' , ( ) => {
94+ vi . mocked ( isDebugNs ) . mockReturnValue ( true )
9495
9596 debugFileOp ( 'write' , '/path/to/file' )
9697
97- expect ( debugFn ) . toHaveBeenCalledWith ( 'silly' , 'File write: /path/to/file' )
98+ expect ( debugNs ) . toHaveBeenCalledWith ( 'silly' , 'File write: /path/to/file' )
9899 } )
99100
100- it ( 'does not log for successful operations when silly is disabled' , async ( ) => {
101- const { debug : _debug , isDebug } = await import (
102- '@socketsecurity/lib/debug'
103- )
104- vi . mocked ( isDebug ) . mockReturnValue ( false )
101+ it ( 'does not log for successful operations when silly is disabled' , ( ) => {
102+ vi . mocked ( isDebugNs ) . mockReturnValue ( false )
105103
106104 debugFileOp ( 'create' , '/path/to/file' )
107105
108- expect ( debugFn ) . not . toHaveBeenCalled ( )
106+ expect ( debugNs ) . not . toHaveBeenCalled ( )
109107 } )
110108
111109 it ( 'handles all operation types' , ( ) => {
@@ -124,166 +122,134 @@ describe('debug utilities', () => {
124122 } )
125123
126124 describe ( 'debugScan' , ( ) => {
127- it ( 'logs start phase with package count' , async ( ) => {
128- const { debugFn } = await import ( '@socketsecurity/lib/debug' )
129-
125+ it ( 'logs start phase with package count' , ( ) => {
130126 debugScan ( 'start' , 42 )
131127
132- expect ( debugFn ) . toHaveBeenCalledWith ( 'notice' , 'Scanning 42 packages' )
128+ expect ( debug ) . toHaveBeenCalledWith ( 'Scanning 42 packages' )
133129 } )
134130
135- it ( 'does not log start phase without package count' , async ( ) => {
136- const { debugFn } = await import ( '@socketsecurity/lib/debug' )
137-
131+ it ( 'does not log start phase without package count' , ( ) => {
138132 debugScan ( 'start' )
139133
140- expect ( debugFn ) . not . toHaveBeenCalled ( )
134+ expect ( debug ) . not . toHaveBeenCalled ( )
141135 } )
142136
143- it ( 'logs progress when silly debug is enabled' , async ( ) => {
144- const { debug : _debug , isDebug } = await import (
145- '@socketsecurity/lib/debug'
146- )
147- vi . mocked ( isDebug ) . mockReturnValue ( true )
137+ it ( 'logs progress when silly debug is enabled' , ( ) => {
138+ vi . mocked ( isDebugNs ) . mockReturnValue ( true )
148139
149140 debugScan ( 'progress' , 10 )
150141
151- expect ( debugFn ) . toHaveBeenCalledWith (
142+ expect ( debugNs ) . toHaveBeenCalledWith (
152143 'silly' ,
153144 'Scan progress: 10 packages processed' ,
154145 )
155146 } )
156147
157- it ( 'logs complete phase' , async ( ) => {
158- const { debugFn } = await import ( '@socketsecurity/lib/debug' )
159-
148+ it ( 'logs complete phase' , ( ) => {
160149 debugScan ( 'complete' , 50 )
161150
162- expect ( debugFn ) . toHaveBeenCalledWith (
151+ expect ( debugNs ) . toHaveBeenCalledWith (
163152 'notice' ,
164153 'Scan complete: 50 packages' ,
165154 )
166155 } )
167156
168- it ( 'logs complete phase without package count' , async ( ) => {
169- const { debugFn } = await import ( '@socketsecurity/lib/debug' )
170-
157+ it ( 'logs complete phase without package count' , ( ) => {
171158 debugScan ( 'complete' )
172159
173- expect ( debugFn ) . toHaveBeenCalledWith ( 'notice' , 'Scan complete' )
160+ expect ( debugNs ) . toHaveBeenCalledWith ( 'notice' , 'Scan complete' )
174161 } )
175162
176- it ( 'logs error phase with details' , async ( ) => {
177- const { debugDir } = await import ( '@socketsecurity/lib/debug' )
163+ it ( 'logs error phase with details' , ( ) => {
178164 const errorDetails = { message : 'Scan failed' }
179165
180166 debugScan ( 'error' , undefined , errorDetails )
181167
182- expect ( debugDir ) . toHaveBeenCalledWith ( 'error' , {
168+ expect ( debugDir ) . toHaveBeenCalledWith ( {
183169 phase : 'scan_error' ,
184170 details : errorDetails ,
185171 } )
186172 } )
187173 } )
188174
189175 describe ( 'debugConfig' , ( ) => {
190- it ( 'logs error when provided' , async ( ) => {
191- const { debugDir } = await import ( '@socketsecurity/lib/debug' )
176+ it ( 'logs error when provided' , ( ) => {
192177 const error = new Error ( 'Config invalid' )
193178
194179 debugConfig ( '.socketrc' , false , error )
195180
196- expect ( debugDir ) . toHaveBeenCalledWith ( 'warn' , {
181+ expect ( debugDir ) . toHaveBeenCalledWith ( {
197182 source : '.socketrc' ,
198183 error : 'Config invalid' ,
199184 } )
200185 } )
201186
202- it ( 'logs notice when config is found' , async ( ) => {
203- const { debugFn } = await import ( '@socketsecurity/lib/debug' )
204-
187+ it ( 'logs notice when config is found' , ( ) => {
205188 debugConfig ( '.socketrc' , true )
206189
207- expect ( debugFn ) . toHaveBeenCalledWith ( 'notice' , 'Config loaded: .socketrc' )
190+ expect ( debug ) . toHaveBeenCalledWith ( 'Config loaded: .socketrc' )
208191 } )
209192
210- it ( 'logs silly when config not found and debug enabled' , async ( ) => {
211- const { debug : _debug , isDebug } = await import (
212- '@socketsecurity/lib/debug'
213- )
214- vi . mocked ( isDebug ) . mockReturnValue ( true )
193+ it ( 'logs silly when config not found and debug enabled' , ( ) => {
194+ vi . mocked ( isDebugNs ) . mockReturnValue ( true )
215195
216196 debugConfig ( '.socketrc' , false )
217197
218- expect ( debugFn ) . toHaveBeenCalledWith (
198+ expect ( debugNs ) . toHaveBeenCalledWith (
219199 'silly' ,
220200 'Config not found: .socketrc' ,
221201 )
222202 } )
223203
224- it ( 'does not log when config not found and debug disabled' , async ( ) => {
225- const { debug : _debug , isDebug } = await import (
226- '@socketsecurity/lib/debug'
227- )
228- vi . mocked ( isDebug ) . mockReturnValue ( false )
204+ it ( 'does not log when config not found and debug disabled' , ( ) => {
205+ vi . mocked ( isDebugNs ) . mockReturnValue ( false )
229206
230207 debugConfig ( '.socketrc' , false )
231208
232- expect ( debugFn ) . not . toHaveBeenCalled ( )
209+ expect ( debugNs ) . not . toHaveBeenCalled ( )
233210 } )
234211 } )
235212
236213 describe ( 'debugGit' , ( ) => {
237- it ( 'logs warning for failed operations' , async ( ) => {
238- const { debugDir } = await import ( '@socketsecurity/lib/debug' )
239-
214+ it ( 'logs warning for failed operations' , ( ) => {
240215 debugGit ( 'push' , false , { branch : 'main' } )
241216
242- expect ( debugDir ) . toHaveBeenCalledWith ( 'warn' , {
217+ expect ( debugDir ) . toHaveBeenCalledWith ( {
243218 git_op : 'push' ,
244219 branch : 'main' ,
245220 } )
246221 } )
247222
248- it ( 'logs notice for important successful operations' , async ( ) => {
249- const { debug : _debug , isDebug } = await import (
250- '@socketsecurity/lib/debug'
251- )
252- vi . mocked ( isDebug ) . mockReturnValue ( true )
223+ it ( 'logs notice for important successful operations' , ( ) => {
224+ vi . mocked ( isDebugNs ) . mockImplementation ( level => level === 'notice' )
253225
254226 debugGit ( 'push' , true )
255227
256- expect ( debugFn ) . toHaveBeenCalledWith ( 'notice' , 'Git push succeeded' )
228+ expect ( debugNs ) . toHaveBeenCalledWith ( 'notice' , 'Git push succeeded' )
257229 } )
258230
259- it ( 'logs commit operations' , async ( ) => {
260- const { debugFn } = await import ( '@socketsecurity/lib/debug' )
231+ it ( 'logs commit operations' , ( ) => {
232+ vi . mocked ( isDebugNs ) . mockReturnValue ( true )
261233
262234 debugGit ( 'commit' , true )
263235
264- expect ( debugFn ) . toHaveBeenCalledWith ( 'notice' , 'Git commit succeeded' )
236+ expect ( debugNs ) . toHaveBeenCalledWith ( 'notice' , 'Git commit succeeded' )
265237 } )
266238
267- it ( 'logs other operations only with silly debug' , async ( ) => {
268- const { debug : _debug , isDebug } = await import (
269- '@socketsecurity/lib/debug'
270- )
271- vi . mocked ( isDebug ) . mockImplementation ( level => level === 'silly' )
239+ it ( 'logs other operations only with silly debug' , ( ) => {
240+ vi . mocked ( isDebugNs ) . mockImplementation ( level => level === 'silly' )
272241
273242 debugGit ( 'status' , true )
274243
275- expect ( debugFn ) . toHaveBeenCalledWith ( 'silly' , 'Git status' )
244+ expect ( debugNs ) . toHaveBeenCalledWith ( 'silly' , 'Git status' )
276245 } )
277246
278- it ( 'does not log non-important operations without silly debug' , async ( ) => {
279- const { debug : _debug , isDebug } = await import (
280- '@socketsecurity/lib/debug'
281- )
282- vi . mocked ( isDebug ) . mockReturnValue ( false )
247+ it ( 'does not log non-important operations without silly debug' , ( ) => {
248+ vi . mocked ( isDebugNs ) . mockReturnValue ( false )
283249
284250 debugGit ( 'status' , true )
285251
286- expect ( debugFn ) . not . toHaveBeenCalled ( )
252+ expect ( debugNs ) . not . toHaveBeenCalled ( )
287253 } )
288254 } )
289255} )
0 commit comments