11/**
2- * Unit tests for checkSocketWrapperSetup.
3- *
4- * Purpose:
5- * Tests checking Socket wrapper installation status. Validates detection of installed wrappers across package managers.
6- *
7- * Test Coverage:
8- * - Core functionality validation
9- * - Edge case handling
10- * - Error scenarios
11- * - Input validation
12- *
13- * Testing Approach:
14- * Comprehensive unit testing of module functionality with mocked dependencies
15- * where appropriate.
16- *
17- * Related Files:
18- * - src/checkSocketWrapperSetup.mts (implementation)
2+ * @fileoverview Unit tests for checkSocketWrapperSetup.
193 */
204
215import fs from 'node:fs'
226
23- import { beforeEach , describe , expect , it , vi } from 'vitest'
7+ import { afterEach , beforeEach , describe , expect , it , vi } from 'vitest'
248
25- import { checkSocketWrapperSetup } from '../../../../src/commands/../../../../src/commands/wrapper/check-socket-wrapper-setup.mts'
26-
27- // Mock the dependencies.
28- vi . mock ( 'node:fs' )
9+ import { checkSocketWrapperSetup } from '../../../../src/commands/wrapper/check-socket-wrapper-setup.mts'
2910
3011const mockLogger = vi . hoisted ( ( ) => ( {
3112 fail : vi . fn ( ) ,
@@ -42,13 +23,19 @@ vi.mock('@socketsecurity/lib/logger', () => ({
4223} ) )
4324
4425describe ( 'checkSocketWrapperSetup' , ( ) => {
26+ let readFileSyncSpy : ReturnType < typeof vi . spyOn >
27+
4528 beforeEach ( ( ) => {
4629 vi . clearAllMocks ( )
30+ readFileSyncSpy = vi . spyOn ( fs , 'readFileSync' )
31+ } )
32+
33+ afterEach ( ( ) => {
34+ readFileSyncSpy . mockRestore ( )
4735 } )
4836
4937 it ( 'detects npm alias in file' , ( ) => {
50- const mockReadFileSync = vi . mocked ( fs . readFileSync ) as any
51- mockReadFileSync . mockReturnValue ( 'alias npm="socket npm"\nother content' )
38+ readFileSyncSpy . mockReturnValue ( 'alias npm="socket npm"\nother content' )
5239
5340 const result = checkSocketWrapperSetup ( '/home/user/.bashrc' )
5441
@@ -57,17 +44,15 @@ describe('checkSocketWrapperSetup', () => {
5744 } )
5845
5946 it ( 'detects npx alias in file' , ( ) => {
60- const mockReadFileSync = vi . mocked ( fs . readFileSync ) as any
61- mockReadFileSync . mockReturnValue ( 'alias npx="socket npx"\nother content' )
47+ readFileSyncSpy . mockReturnValue ( 'alias npx="socket npx"\nother content' )
6248
6349 const result = checkSocketWrapperSetup ( '/home/user/.bashrc' )
6450
6551 expect ( result ) . toBe ( true )
6652 } )
6753
6854 it ( 'detects both aliases in file' , ( ) => {
69- const mockReadFileSync = vi . mocked ( fs . readFileSync ) as any
70- mockReadFileSync . mockReturnValue (
55+ readFileSyncSpy . mockReturnValue (
7156 'alias npm="socket npm"\nalias npx="socket npx"\nother content' ,
7257 )
7358
@@ -77,39 +62,36 @@ describe('checkSocketWrapperSetup', () => {
7762 } )
7863
7964 it ( 'returns false when no aliases found' , ( ) => {
80- const mockReadFileSync = vi . mocked ( fs . readFileSync ) as any
81- mockReadFileSync . mockReturnValue ( 'some other content\nno aliases here' )
65+ readFileSyncSpy . mockReturnValue ( 'some other content\nno aliases here' )
8266
8367 const result = checkSocketWrapperSetup ( '/home/user/.bashrc' )
8468
8569 expect ( result ) . toBe ( false )
8670 } )
8771
8872 it ( 'returns false for empty file' , ( ) => {
89- const mockReadFileSync = vi . mocked ( fs . readFileSync ) as any
90- mockReadFileSync . mockReturnValue ( '' )
73+ readFileSyncSpy . mockReturnValue ( '' )
9174
9275 const result = checkSocketWrapperSetup ( '/home/user/.bashrc' )
9376
9477 expect ( result ) . toBe ( false )
9578 } )
9679
97- it ( 'logs instructions when wrapper is set up' , async ( ) => {
98- await import ( '@socketsecurity/lib/logger' )
99- const mockReadFileSync = vi . mocked ( fs . readFileSync ) as any
100- mockReadFileSync . mockReturnValue ( 'alias npm="socket npm"' )
80+ it ( 'logs instructions when wrapper is set up' , ( ) => {
81+ readFileSyncSpy . mockReturnValue ( 'alias npm="socket npm"' )
10182
10283 checkSocketWrapperSetup ( '/home/user/.bashrc' )
10384
10485 expect ( mockLogger . log ) . toHaveBeenCalledWith (
10586 'The Socket npm/npx wrapper is set up in your bash profile (/home/user/.bashrc).' ,
10687 )
107- expect ( mockLogger . log ) . toHaveBeenCalledWith ( ' source /home/user/.bashrc' )
88+ expect ( mockLogger . log ) . toHaveBeenCalledWith (
89+ ' source /home/user/.bashrc' ,
90+ )
10891 } )
10992
11093 it ( 'ignores partial alias matches' , ( ) => {
111- const mockReadFileSync = vi . mocked ( fs . readFileSync ) as any
112- mockReadFileSync . mockReturnValue (
94+ readFileSyncSpy . mockReturnValue (
11395 'alias npm="other-tool npm"\nalias npx="other-tool npx"' ,
11496 )
11597
@@ -119,8 +101,7 @@ describe('checkSocketWrapperSetup', () => {
119101 } )
120102
121103 it ( 'handles multiline file with aliases mixed in' , ( ) => {
122- const mockReadFileSync = vi . mocked ( fs . readFileSync ) as any
123- mockReadFileSync . mockReturnValue (
104+ readFileSyncSpy . mockReturnValue (
124105 `#!/bin/bash
125106# User bashrc
126107export PATH=$PATH:/usr/local/bin
@@ -135,26 +116,20 @@ export NODE_ENV=development`,
135116 } )
136117
137118 it ( 'is case-sensitive for alias detection' , ( ) => {
138- const mockReadFileSync = vi . mocked ( fs . readFileSync ) as any
139- mockReadFileSync . mockReturnValue ( 'ALIAS NPM="SOCKET NPM"' )
119+ readFileSyncSpy . mockReturnValue ( 'ALIAS NPM="SOCKET NPM"' )
140120
141121 const result = checkSocketWrapperSetup ( '/home/user/.bashrc' )
142122
143123 expect ( result ) . toBe ( false )
144124 } )
145125
146126 it ( 'handles files with Windows line endings' , ( ) => {
147- const mockReadFileSync = vi . mocked ( fs . readFileSync ) as any
148- // When splitting on \n, Windows line endings leave \r at the end of lines,
149- // so 'alias npm="socket npm"\r' !== 'alias npm="socket npm"'.
150- // The function doesn't handle Windows line endings properly.
151- mockReadFileSync . mockReturnValue (
127+ readFileSyncSpy . mockReturnValue (
152128 'line1\r\nalias npm="socket npm"\r\nalias npx="socket npx"\r\n' ,
153129 )
154130
155131 const result = checkSocketWrapperSetup ( '/home/user/.bashrc' )
156132
157- // The function splits by \n, leaving \r at the end, so exact match fails.
158133 expect ( result ) . toBe ( false )
159134 } )
160135} )
0 commit comments