UNPKG

@mbc-cqrs-serverless/cli

Version:

a CLI to get started with MBC CQRS serverless framework

129 lines (128 loc) 7.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const fs_1 = require("fs"); const path_1 = require("path"); const local_binaries_1 = require("./local-binaries"); jest.mock('fs'); jest.mock('path'); describe('Local Binaries Utilities', () => { const mockExistsSync = fs_1.existsSync; const mockJoin = path_1.join; const mockPosixJoin = path_1.posix.join; beforeEach(() => { jest.clearAllMocks(); mockJoin.mockImplementation((...args) => args.join('/')); mockPosixJoin.mockImplementation((...args) => args.join('/')); }); describe('Overview: Local binary detection and loading functionality', () => { describe('Purpose: Test localBinExists function', () => { it('should return true when local binary exists', () => { mockExistsSync.mockReturnValue(true); const result = (0, local_binaries_1.localBinExists)(); expect(result).toBe(true); expect(mockExistsSync).toHaveBeenCalledWith(expect.stringContaining('node_modules/@mbc-cqrs-serverless/cli')); }); it('should return false when local binary does not exist', () => { mockExistsSync.mockReturnValue(false); const result = (0, local_binaries_1.localBinExists)(); expect(result).toBe(false); expect(mockExistsSync).toHaveBeenCalledWith(expect.stringContaining('node_modules/@mbc-cqrs-serverless/cli')); }); it('should handle file system errors gracefully', () => { mockExistsSync.mockImplementation(() => { throw new Error('File system error'); }); expect(() => (0, local_binaries_1.localBinExists)()).toThrow('File system error'); }); }); describe('Purpose: Test loadLocalBinCommandLoader function', () => { it('should successfully load command loader when binary exists', () => { const mockCommandLoader = { loadCommands: jest.fn(), getCommand: jest.fn() }; jest.doMock('node_modules/@mbc-cqrs-serverless/cli/dist/commands', () => mockCommandLoader, { virtual: true }); const result = (0, local_binaries_1.loadLocalBinCommandLoader)(); expect(result).toBeDefined(); expect(mockPosixJoin).toHaveBeenCalledWith(expect.any(String), 'node_modules', '@mbc-cqrs-serverless', 'cli', 'dist', 'commands'); }); it('should handle missing command loader module', () => { jest.doMock('node_modules/@mbc-cqrs-serverless/cli/dist/commands', () => { throw new Error('Module not found'); }, { virtual: true }); expect(() => (0, local_binaries_1.loadLocalBinCommandLoader)()).not.toThrow(); }); it('should handle corrupted command loader module', () => { jest.doMock('node_modules/@mbc-cqrs-serverless/cli/dist/commands', () => null, { virtual: true }); const result = (0, local_binaries_1.loadLocalBinCommandLoader)(); expect(result).toBeDefined(); }); }); describe('Purpose: Test path construction and resolution', () => { it('should construct correct path segments for binary detection', () => { mockExistsSync.mockReturnValue(true); (0, local_binaries_1.localBinExists)(); expect(mockJoin).toHaveBeenCalledWith(process.cwd(), 'node_modules', '@mbc-cqrs-serverless', 'cli'); }); it('should construct correct posix path for command loader', () => { const mockCommandLoader = {}; jest.doMock('node_modules/@mbc-cqrs-serverless/cli/dist/commands', () => mockCommandLoader, { virtual: true }); (0, local_binaries_1.loadLocalBinCommandLoader)(); expect(mockPosixJoin).toHaveBeenCalledWith(expect.any(String), 'node_modules', '@mbc-cqrs-serverless', 'cli', 'dist', 'commands'); }); it('should handle different working directories', () => { const originalCwd = process.cwd; process.cwd = jest.fn().mockReturnValue('/custom/working/directory'); mockExistsSync.mockReturnValue(true); (0, local_binaries_1.localBinExists)(); expect(mockJoin).toHaveBeenCalledWith(expect.any(String), 'node_modules', '@mbc-cqrs-serverless', 'cli'); process.cwd = originalCwd; }); }); describe('Purpose: Test error scenarios and edge cases', () => { it('should handle permission denied errors', () => { mockExistsSync.mockImplementation(() => { throw new Error('EACCES: permission denied'); }); expect(() => (0, local_binaries_1.localBinExists)()).toThrow('EACCES: permission denied'); }); it('should handle network drive or symlink issues', () => { mockExistsSync.mockImplementation(() => { throw new Error('ENOTDIR: not a directory'); }); expect(() => (0, local_binaries_1.localBinExists)()).toThrow('ENOTDIR: not a directory'); }); it('should handle require cache issues in command loader', () => { const mockCommandLoader = { cached: true }; jest.doMock('node_modules/@mbc-cqrs-serverless/cli/dist/commands', () => mockCommandLoader, { virtual: true }); const result1 = (0, local_binaries_1.loadLocalBinCommandLoader)(); const result2 = (0, local_binaries_1.loadLocalBinCommandLoader)(); expect(result1).toBe(result2); }); }); describe('Purpose: Test integration scenarios', () => { it('should work correctly when binary exists and command loader is available', () => { mockExistsSync.mockReturnValue(true); const mockCommandLoader = { loadCommands: jest.fn(), getCommand: jest.fn() }; jest.doMock('node_modules/@mbc-cqrs-serverless/cli/dist/commands', () => mockCommandLoader, { virtual: true }); const binaryExists = (0, local_binaries_1.localBinExists)(); const commandLoader = (0, local_binaries_1.loadLocalBinCommandLoader)(); expect(binaryExists).toBe(true); expect(commandLoader).toBeDefined(); expect(typeof commandLoader).toBe('object'); }); it('should handle scenario where binary exists but command loader fails', () => { mockExistsSync.mockReturnValue(true); jest.doMock('node_modules/@mbc-cqrs-serverless/cli/dist/commands', () => { throw new Error('Command loader initialization failed'); }, { virtual: true }); const binaryExists = (0, local_binaries_1.localBinExists)(); expect(binaryExists).toBe(true); expect(() => (0, local_binaries_1.loadLocalBinCommandLoader)()).not.toThrow(); }); }); }); });