loqatevars
Version:
Locate JavaScript files with 'const' or 'process.env' usage in LLM-generated codebases
81 lines (67 loc) • 2.62 kB
JavaScript
const fs = require('fs-extra');
const path = require('path');
const os = require('os');
jest.mock('../lib/utils.js', () => ({
findMatchingFiles: jest.fn(),
findMatchingFilesDetailed: jest.fn(),
validateDirectory: jest.fn().mockResolvedValue()
}));
const { main } = require('../cli');
const utils = require('../lib/utils.js');
describe('Integration Tests', () => {
let testDir;
let mockLog;
let mockError;
let originalArgv;
beforeEach(() => {
testDir = fs.mkdtempSync(path.join(os.tmpdir(), 'integration-'));
mockLog = jest.spyOn(console, 'log').mockImplementation();
mockError = jest.spyOn(console, 'error').mockImplementation();
originalArgv = process.argv;
});
afterEach(() => {
fs.removeSync(testDir);
mockLog.mockRestore();
mockError.mockRestore();
process.argv = originalArgv;
});
test('should run scan command successfully', async () => {
utils.findMatchingFiles.mockResolvedValue(['file1.js', 'file2.js']);
process.argv = ['node', 'cli.js', 'scan', testDir, '--ignore', 'ignore.js'];
await main();
const output = mockLog.mock.calls.join('\\n');
expect(output).toContain('file1.js');
expect(output).toContain('file2.js');
expect(output).not.toContain('ignore.js');
});
test('should run detailed command successfully', async () => {
utils.findMatchingFilesDetailed.mockResolvedValue({
matches: [{ relativePath: 'file1.js', reason: 'variables' }],
summary: { scannedFiles: 1, matchingFiles: 1 }
});
process.argv = ['node', 'cli.js', 'detailed', testDir];
await main();
const output = mockLog.mock.calls.map(c => c.join(' ')).join('\\n');
expect(output).toContain('Scanned files');
expect(output).toContain('Matching files');
});
test('detailed command classifies files', async () => {
const caseDir = path.join(testDir, 'reason-data');
try {
utils.findMatchingFilesDetailed.mockResolvedValue({
matches: [
{ relativePath: 'envOnly.js', reason: 'process.env' },
{ relativePath: 'constOnly.js', reason: 'variables' },
{ relativePath: 'both.js', reason: 'both' }
],
summary: { scannedFiles: 3, matchingFiles: 3 }
});
process.argv = ['node', 'cli.js', 'detailed', caseDir];
await main();
const output = mockLog.mock.calls.map(c => c.join(' ')).join('\\n');
expect(output).toContain('Matching files');
} finally {
fs.removeSync(caseDir);
}
});
});