UNPKG

loqatevars

Version:

Locate JavaScript files with 'const' or 'process.env' usage in LLM-generated codebases

87 lines (76 loc) 3.88 kB
const { main } = require('../cli.js'); const { findMatchingFiles, findMatchingFilesDetailed } = require('../lib/utils.js'); const { AppError } = require('../lib/errors'); jest.mock('../lib/utils.js'); describe('CLI Execution', () => { let consoleSpy; let exitSpy; beforeEach(() => { consoleSpy = jest.spyOn(console, 'log').mockImplementation(); exitSpy = jest.spyOn(process, 'exit').mockImplementation(); }); afterEach(() => { consoleSpy.mockRestore(); exitSpy.mockRestore(); jest.clearAllMocks(); }); test('main should call findMatchingFiles with correct arguments for scan command', async () => { process.argv = ['node', 'cli.js', 'scan', './src', '--ignore', 'ignore.js', '--extensions', '.ts']; findMatchingFiles.mockResolvedValue(['file1.ts']); await main(); expect(findMatchingFiles).toHaveBeenCalledWith('./src', ['ignore.js'], ['.ts']); expect(consoleSpy).toHaveBeenNthCalledWith(1, 'file1.ts'); expect(consoleSpy).toHaveBeenNthCalledWith(2, '\nFound 1 file'); }); test('main handles trailing commas in options', async () => { process.argv = ['node', 'cli.js', 'scan', './src', '--ignore', 'ignore.js,', '--extensions', '.ts,']; findMatchingFiles.mockResolvedValue(['file1.ts']); await main(); expect(findMatchingFiles).toHaveBeenCalledWith('./src', ['ignore.js'], ['.ts']); }); test('main should call findMatchingFilesDetailed with correct arguments for detailed command', async () => { process.argv = ['node', 'cli.js', 'detailed', './src']; findMatchingFilesDetailed.mockResolvedValue({ matches: [{ relativePath: 'file1.js', reason: 'const' }], summary: { scannedFiles: 1, matchingFiles: 1, ignoredFiles: [] } }); await main(); expect(findMatchingFilesDetailed).toHaveBeenCalledWith('./src', ['**/localVars.js'], ['.js']); expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining('loqatevars Detailed Analysis')); }); test('main should handle no matches found for scan command', async () => { process.argv = ['node', 'cli.js', 'scan']; findMatchingFiles.mockResolvedValue([]); await main(); expect(consoleSpy).toHaveBeenCalledWith('No files found containing const or process.env'); }); test('main should handle no matches found for detailed command', async () => { process.argv = ['node', 'cli.js', 'detailed']; findMatchingFilesDetailed.mockResolvedValue({ matches: [], summary: { scannedFiles: 0, matchingFiles: 0, ignoredFiles: [] } }); await main(); expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining('No files found containing "const" variables or "process.env".')); }); test('main should handle errors gracefully', async () => { process.argv = ['node', 'cli.js', 'scan']; const error = new Error('Test Error'); findMatchingFiles.mockRejectedValue(error); const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(); await main(); expect(consoleErrorSpy).toHaveBeenCalledWith('Unexpected Error:', 'Test Error'); expect(exitSpy).not.toHaveBeenCalled(); consoleErrorSpy.mockRestore(); }); test('main should handle AppError as operational', async () => { process.argv = ['node', 'cli.js', 'scan']; const error = new AppError('App fail', 'FAIL'); findMatchingFiles.mockRejectedValue(error); const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(); await main(); expect(consoleErrorSpy).toHaveBeenCalledWith('Operational Error:', 'App fail'); expect(exitSpy).not.toHaveBeenCalled(); consoleErrorSpy.mockRestore(); }); });