import fs from 'fs'; import { CBFileSystem, FileSystemService } from '../../src/node/file-system'; // Mock the fs module jest.mock('fs'); describe('CBFileSystem', () => { let fileSystem: CBFileSystem; let mockFs: jest.Mocked; beforeEach(() => { // Clear all mocks before each test jest.clearAllMocks(); // Create a new instance for each test fileSystem = new FileSystemService(); // Get the mocked fs module mockFs = fs as jest.Mocked; }); describe('existsSync', () => { it('should call fs.existsSync with the correct path', () => { const testPath = '/test/path'; mockFs.existsSync.mockReturnValue(true); const result = fileSystem.existsSync(testPath); expect(mockFs.existsSync).toHaveBeenCalledWith(testPath); expect(result).toBe(true); }); it('should return false when file does not exist', () => { const testPath = '/nonexistent/path'; mockFs.existsSync.mockReturnValue(false); const result = fileSystem.existsSync(testPath); expect(mockFs.existsSync).toHaveBeenCalledWith(testPath); expect(result).toBe(false); }); }); describe('readdirSync', () => { it('should call fs.readdirSync with the correct path', () => { const testPath = '/test/directory'; const expectedFiles = ['file1.txt', 'file2.txt']; mockFs.readdirSync.mockReturnValue(expectedFiles as any); const result = fileSystem.readdirSync(testPath); expect(mockFs.readdirSync).toHaveBeenCalledWith(testPath, undefined); expect(result).toEqual(expectedFiles); }); it('should pass options to fs.readdirSync', () => { const testPath = '/test/directory'; const options = { encoding: 'utf8' }; const expectedFiles = ['file1.txt', 'file2.txt']; mockFs.readdirSync.mockReturnValue(expectedFiles as any); const result = fileSystem.readdirSync(testPath, options); expect(mockFs.readdirSync).toHaveBeenCalledWith(testPath, options); expect(result).toEqual(expectedFiles); }); }); describe('mkdirSync', () => { it('should call fs.mkdirSync with the correct path', () => { const testPath = '/test/new-directory'; fileSystem.mkdirSync(testPath); expect(mockFs.mkdirSync).toHaveBeenCalledWith(testPath, undefined); }); it('should call fs.mkdirSync with recursive option', () => { const testPath = '/test/nested/new-directory'; const options = { recursive: true }; fileSystem.mkdirSync(testPath, options); expect(mockFs.mkdirSync).toHaveBeenCalledWith(testPath, options); }); }); describe('unlinkSync', () => { it('should call fs.unlinkSync with the correct path', () => { const testPath = '/test/file-to-delete.txt'; fileSystem.unlinkSync(testPath); expect(mockFs.unlinkSync).toHaveBeenCalledWith(testPath); }); }); describe('rmdirSync', () => { it('should call fs.rmdirSync with the correct path', () => { const testPath = '/test/directory-to-remove'; fileSystem.rmdirSync(testPath); expect(mockFs.rmdirSync).toHaveBeenCalledWith(testPath); }); }); describe('statSync', () => { it('should call fs.statSync and return stat object', () => { const testPath = '/test/file.txt'; const mockStats = { isDirectory: jest.fn().mockReturnValue(false), size: 1024 }; mockFs.statSync.mockReturnValue(mockStats as any); const result = fileSystem.statSync(testPath); expect(mockFs.statSync).toHaveBeenCalledWith(testPath); expect(result).toEqual(mockStats); }); it('should return directory stats', () => { const testPath = '/test/directory'; const mockStats = { isDirectory: jest.fn().mockReturnValue(true), size: 4096 }; mockFs.statSync.mockReturnValue(mockStats as any); const result = fileSystem.statSync(testPath); expect(mockFs.statSync).toHaveBeenCalledWith(testPath); expect(result.isDirectory()).toBe(true); expect(result.size).toBe(4096); }); }); describe('readFileSync', () => { it('should call fs.readFileSync with path and encoding', () => { const testPath = '/test/file.txt'; const encoding = 'utf8'; const expectedContent = 'file content'; mockFs.readFileSync.mockReturnValue(expectedContent as any); const result = fileSystem.readFileSync(testPath, encoding); expect(mockFs.readFileSync).toHaveBeenCalledWith(testPath, encoding); expect(result).toBe(expectedContent); }); }); describe('writeFileSync', () => { it('should call fs.writeFileSync with path and data', () => { const testPath = '/test/file.txt'; const data = 'test content'; fileSystem.writeFileSync(testPath, data); expect(mockFs.writeFileSync).toHaveBeenCalledWith(testPath, data); }); }); describe('copyFileSync', () => { it('should call fs.copyFileSync with source and destination paths', () => { const srcPath = '/test/source.txt'; const destPath = '/test/destination.txt'; fileSystem.copyFileSync(srcPath, destPath); expect(mockFs.copyFileSync).toHaveBeenCalledWith(srcPath, destPath); }); }); describe('lstatSync', () => { it('should call fs.lstatSync with the correct path', () => { const testPath = '/test/symlink'; const mockStats = { isSymbolicLink: jest.fn().mockReturnValue(true), isFile: jest.fn().mockReturnValue(false), isDirectory: jest.fn().mockReturnValue(false) }; mockFs.lstatSync.mockReturnValue(mockStats as any); const result = fileSystem.lstatSync(testPath); expect(mockFs.lstatSync).toHaveBeenCalledWith(testPath); expect(result).toEqual(mockStats); }); it('should return file stats for regular files', () => { const testPath = '/test/file.txt'; const mockStats = { isSymbolicLink: jest.fn().mockReturnValue(false), isFile: jest.fn().mockReturnValue(true), isDirectory: jest.fn().mockReturnValue(false) }; mockFs.lstatSync.mockReturnValue(mockStats as any); const result = fileSystem.lstatSync(testPath); expect(mockFs.lstatSync).toHaveBeenCalledWith(testPath); expect(result.isFile()).toBe(true); expect(result.isSymbolicLink()).toBe(false); }); }); describe('renameSync', () => { it('should call fs.renameSync with old and new paths', () => { const oldPath = '/test/old-name.txt'; const newPath = '/test/new-name.txt'; fileSystem.renameSync(oldPath, newPath); expect(mockFs.renameSync).toHaveBeenCalledWith(oldPath, newPath); }); }); describe('cpSync', () => { it('should call fs.cpSync with source and destination paths', () => { const srcPath = '/test/source'; const destPath = '/test/destination'; fileSystem.cpSync(srcPath, destPath); expect(mockFs.cpSync).toHaveBeenCalledWith(srcPath, destPath, undefined); }); it('should call fs.cpSync with recursive option', () => { const srcPath = '/test/source'; const destPath = '/test/destination'; const options = { recursive: true }; fileSystem.cpSync(srcPath, destPath, options); expect(mockFs.cpSync).toHaveBeenCalledWith(srcPath, destPath, options); }); }); describe('createWriteStream', () => { it('should call fs.createWriteStream with the correct path', () => { const testPath = '/test/file.txt'; const mockStream = {} as NodeJS.WritableStream; mockFs.createWriteStream.mockReturnValue(mockStream as any); const result = fileSystem.createWriteStream(testPath); expect(mockFs.createWriteStream).toHaveBeenCalledWith(testPath); expect(result).toBe(mockStream); }); }); });