11/**
2- * Unit tests for json command output.
3- *
4- * Purpose:
5- * Tests the output-cmd-json utility for displaying socket.json contents.
6- *
7- * Test Coverage:
8- * - File not found handling
9- * - Non-file (directory) handling
10- * - Successful file reading
11- *
12- * Related Files:
13- * - commands/json/output-cmd-json.mts (implementation)
2+ * @fileoverview Unit tests for json command output.
143 */
154
16- import path from 'node:path '
5+ import fs from 'node:fs '
176
187import { afterEach , beforeEach , describe , expect , it , vi } from 'vitest'
198
20- // Mock dependencies.
219const mockLogger = vi . hoisted ( ( ) => ( {
2210 error : vi . fn ( ) ,
2311 fail : vi . fn ( ) ,
@@ -30,48 +18,42 @@ vi.mock('@socketsecurity/lib/logger', () => ({
3018 getDefaultLogger : ( ) => mockLogger ,
3119} ) )
3220
33- vi . mock ( 'node:fs' , async ( ) => {
34- const actual = await vi . importActual ( 'node:fs' )
35- return {
36- ...actual ,
37- existsSync : vi . fn ( ) ,
38- }
39- } )
21+ const mockSafeReadFileSync = vi . fn ( )
22+ const mockSafeStatsSync = vi . fn ( )
4023
4124vi . mock ( '@socketsecurity/lib/fs' , ( ) => ( {
42- safeReadFileSync : vi . fn ( ) ,
43- safeStatsSync : vi . fn ( ) ,
25+ safeReadFileSync : ( ... args : unknown [ ] ) => mockSafeReadFileSync ( ... args ) ,
26+ safeStatsSync : ( ... args : unknown [ ] ) => mockSafeStatsSync ( ... args ) ,
4427} ) )
4528
46- import { existsSync } from 'node:fs'
47-
48- import { safeReadFileSync , safeStatsSync } from '@socketsecurity/lib/fs'
49-
5029import { outputCmdJson } from '../../../../src/commands/json/output-cmd-json.mts'
5130
5231describe ( 'output-cmd-json' , ( ) => {
5332 const originalExitCode = process . exitCode
33+ let existsSyncSpy : ReturnType < typeof vi . spyOn >
5434
5535 beforeEach ( ( ) => {
5636 vi . clearAllMocks ( )
5737 process . exitCode = undefined
38+ existsSyncSpy = vi . spyOn ( fs , 'existsSync' )
5839 } )
5940
6041 afterEach ( ( ) => {
42+ existsSyncSpy . mockRestore ( )
6143 process . exitCode = originalExitCode
6244 } )
6345
6446 describe ( 'outputCmdJson' , ( ) => {
6547 it ( 'logs info about target cwd' , async ( ) => {
66- vi . mocked ( existsSync ) . mockReturnValue ( false )
48+ existsSyncSpy . mockReturnValue ( false )
6749
6850 await outputCmdJson ( '/test/path' )
6951
7052 expect ( mockLogger . info ) . toHaveBeenCalledWith ( 'Target cwd:' , expect . any ( String ) )
7153 } )
7254
7355 it ( 'handles socket.json not found' , async ( ) => {
74- vi . mocked ( existsSync ) . mockReturnValue ( false )
56+ existsSyncSpy . mockReturnValue ( false )
7557
7658 await outputCmdJson ( '/test/path' )
7759
@@ -82,10 +64,10 @@ describe('output-cmd-json', () => {
8264 } )
8365
8466 it ( 'handles non-file (directory) path' , async ( ) => {
85- vi . mocked ( existsSync ) . mockReturnValue ( true )
86- vi . mocked ( safeStatsSync ) . mockReturnValue ( {
67+ existsSyncSpy . mockReturnValue ( true )
68+ mockSafeStatsSync . mockReturnValue ( {
8769 isFile : ( ) => false ,
88- } as any )
70+ } )
8971
9072 await outputCmdJson ( '/test/path' )
9173
@@ -97,11 +79,11 @@ describe('output-cmd-json', () => {
9779
9880 it ( 'successfully reads and outputs socket.json contents' , async ( ) => {
9981 const mockContent = JSON . stringify ( { version : '1.0.0' } , null , 2 )
100- vi . mocked ( existsSync ) . mockReturnValue ( true )
101- vi . mocked ( safeStatsSync ) . mockReturnValue ( {
82+ existsSyncSpy . mockReturnValue ( true )
83+ mockSafeStatsSync . mockReturnValue ( {
10284 isFile : ( ) => true ,
103- } as any )
104- vi . mocked ( safeReadFileSync ) . mockReturnValue ( mockContent )
85+ } )
86+ mockSafeReadFileSync . mockReturnValue ( mockContent )
10587
10688 await outputCmdJson ( '/test/path' )
10789
@@ -113,8 +95,8 @@ describe('output-cmd-json', () => {
11395 } )
11496
11597 it ( 'handles null safeStatsSync result' , async ( ) => {
116- vi . mocked ( existsSync ) . mockReturnValue ( true )
117- vi . mocked ( safeStatsSync ) . mockReturnValue ( null )
98+ existsSyncSpy . mockReturnValue ( true )
99+ mockSafeStatsSync . mockReturnValue ( null )
118100
119101 await outputCmdJson ( '/test/path' )
120102
0 commit comments