// Test for path utilities functionality import fs from 'fs'; import path from 'path'; // Mock fs module jest.mock('fs'); const mockFs = fs as jest.Mocked; describe('Path Utilities Logic', () => { beforeEach(() => { jest.clearAllMocks(); delete process.env.MCPHUB_SETTING_PATH; }); // Test the core logic of path resolution const findConfigFile = (filename: string): string => { const envPath = process.env.MCPHUB_SETTING_PATH; const potentialPaths = [ ...(envPath ? [envPath] : []), path.resolve(process.cwd(), filename), path.join(process.cwd(), filename), ]; for (const filePath of potentialPaths) { if (fs.existsSync(filePath)) { return filePath; } } return path.resolve(process.cwd(), filename); }; describe('Configuration File Resolution', () => { it('should find existing file in current directory', () => { const filename = 'test-config.json'; const expectedPath = path.resolve(process.cwd(), filename); mockFs.existsSync.mockImplementation((filePath) => { return filePath === expectedPath; }); const result = findConfigFile(filename); expect(result).toBe(expectedPath); expect(mockFs.existsSync).toHaveBeenCalled(); }); it('should prioritize environment variable path', () => { const filename = 'test-config.json'; const envPath = '/custom/path/test-config.json'; process.env.MCPHUB_SETTING_PATH = envPath; mockFs.existsSync.mockImplementation((filePath) => { return filePath === envPath; }); const result = findConfigFile(filename); expect(result).toBe(envPath); expect(mockFs.existsSync).toHaveBeenCalledWith(envPath); }); it('should return default path when file does not exist', () => { const filename = 'nonexistent-config.json'; const expectedDefaultPath = path.resolve(process.cwd(), filename); mockFs.existsSync.mockReturnValue(false); const result = findConfigFile(filename); expect(result).toBe(expectedDefaultPath); }); it('should handle different file types', () => { const testFiles = [ 'config.json', 'settings.yaml', 'data.xml', 'servers.json' ]; testFiles.forEach(filename => { const expectedPath = path.resolve(process.cwd(), filename); mockFs.existsSync.mockImplementation((filePath) => { return filePath === expectedPath; }); const result = findConfigFile(filename); expect(result).toBe(expectedPath); expect(path.isAbsolute(result)).toBe(true); }); }); }); describe('Path Operations', () => { it('should generate absolute paths', () => { const filename = 'test.json'; mockFs.existsSync.mockReturnValue(false); const result = findConfigFile(filename); expect(path.isAbsolute(result)).toBe(true); expect(result).toContain(filename); }); it('should handle path normalization', () => { const filename = './config/../settings.json'; mockFs.existsSync.mockReturnValue(false); const result = findConfigFile(filename); expect(typeof result).toBe('string'); expect(result.length).toBeGreaterThan(0); }); it('should work consistently across multiple calls', () => { const filename = 'consistent-test.json'; const expectedPath = path.resolve(process.cwd(), filename); mockFs.existsSync.mockImplementation((filePath) => { return filePath === expectedPath; }); const result1 = findConfigFile(filename); const result2 = findConfigFile(filename); expect(result1).toBe(result2); expect(result1).toBe(expectedPath); }); }); describe('Environment Variable Handling', () => { it('should handle missing environment variable gracefully', () => { const filename = 'test.json'; delete process.env.MCPHUB_SETTING_PATH; mockFs.existsSync.mockReturnValue(false); const result = findConfigFile(filename); expect(typeof result).toBe('string'); expect(result).toContain(filename); }); it('should handle empty environment variable', () => { const filename = 'test.json'; process.env.MCPHUB_SETTING_PATH = ''; mockFs.existsSync.mockReturnValue(false); const result = findConfigFile(filename); expect(typeof result).toBe('string'); expect(result).toContain(filename); }); }); describe('Error Handling', () => { it('should handle fs.existsSync errors gracefully', () => { const filename = 'test.json'; mockFs.existsSync.mockImplementation(() => { throw new Error('File system error'); }); expect(() => findConfigFile(filename)).toThrow('File system error'); }); it('should validate input parameters', () => { const emptyFilename = ''; mockFs.existsSync.mockReturnValue(false); const result = findConfigFile(emptyFilename); expect(typeof result).toBe('string'); // Should still return a path, even for empty filename }); }); });