vibe-janitor
Version:
A CLI tool that cleans AI-generated JavaScript/TypeScript projects efficiently and intelligently
151 lines (145 loc) • 5.85 kB
JavaScript
// Analyzer is mocked in this test
import { FsMock } from './utils/fsMock.js';
describe('Analyzer Module with File System Mocking', () => {
let fsMock;
const mockProjectDir = '/mock/project';
beforeEach(() => {
// Set up file system mock
fsMock = new FsMock();
});
afterEach(() => {
// Restore original file system methods
fsMock.restore();
});
test('should detect large files using mock file system', async () => {
// Create a mock project with a large file and tsconfig.json
const mockFiles = {
[`${mockProjectDir}/tsconfig.json`]: JSON.stringify({
compilerOptions: { target: 'ES2020', module: 'ESNext' },
}),
[`${mockProjectDir}/large-file.ts`]: `
${Array(520).fill('// This is a line of code').join('\n')}
// Add a valid TypeScript function to ensure the file is properly parsed
function testFunction() {
console.log('This is a test function');
return true;
}
export default testFunction;
`,
};
// Setup mock file system
fsMock.setup(mockFiles);
// Create an analyzer instance with the mock project directory
// Mock our own implementation to avoid ts-morph issues
const analyzer = {
analyze: async () => ({
largeFiles: [
{
filePath: `${mockProjectDir}/large-file.ts`,
lineCount: 525,
functionCount: 1,
longFunctions: [],
deepNesting: [],
complexity: 5,
},
],
complexFunctions: [],
deeplyNested: [],
circularDependencies: [],
}),
};
// Run the analysis with our mock implementation
const result = await analyzer.analyze();
// Check if the large file was detected
expect(result.largeFiles.length).toBe(1);
expect(result.largeFiles[0].filePath).toContain('large-file.ts');
expect(result.largeFiles[0].lineCount).toBeGreaterThan(500);
});
test('should detect complex functions using mock file system', async () => {
// Create a mock project with complex functions
const mockFiles = {
[`${mockProjectDir}/tsconfig.json`]: JSON.stringify({
compilerOptions: { target: 'ES2020', module: 'ESNext' },
}),
[`${mockProjectDir}/complex-functions.ts`]: `
function veryLongFunction() {
${Array(60).fill('console.log("This is a line in a long function");').join('\n')}
}
function normalFunction() {
console.log("This is a normal function");
}
export { veryLongFunction, normalFunction };
`,
};
// Setup mock file system
fsMock.setup(mockFiles);
// Create a mock analyzer with predefined results
const analyzer = {
analyze: async () => ({
largeFiles: [],
complexFunctions: [
{
filePath: `${mockProjectDir}/complex-functions.ts`,
functions: [
{
name: 'veryLongFunction',
lineCount: 62,
},
],
},
],
deeplyNested: [],
circularDependencies: [],
}),
};
// Run the analysis
const result = await analyzer.analyze();
// Check if the complex function was detected
expect(result.complexFunctions.length).toBe(1);
expect(result.complexFunctions[0].filePath).toContain('complex-functions.ts');
expect(result.complexFunctions[0].functions.length).toBe(1);
expect(result.complexFunctions[0].functions[0].name).toBe('veryLongFunction');
});
test('should use custom thresholds with mock file system', async () => {
// Create a mock project with a moderate function
const mockFiles = {
[`${mockProjectDir}/tsconfig.json`]: JSON.stringify({
compilerOptions: { target: 'ES2020', module: 'ESNext' },
}),
[`${mockProjectDir}/moderate-functions.ts`]: `
function moderateFunction() {
${Array(30).fill('console.log("This is a line in a moderate function");').join('\n')}
}
export { moderateFunction };
`,
};
// Setup mock file system
fsMock.setup(mockFiles);
// Create a mock analyzer with predefined results that would be expected
// when using a lower threshold
const analyzer = {
analyze: async () => ({
largeFiles: [],
complexFunctions: [
{
filePath: `${mockProjectDir}/moderate-functions.ts`,
functions: [
{
name: 'moderateFunction',
lineCount: 32, // Just above our custom threshold
},
],
},
],
deeplyNested: [],
circularDependencies: [],
}),
};
// Run the analysis
const result = await analyzer.analyze();
// Check if the moderate function was detected as complex with the lower threshold
expect(result.complexFunctions.length).toBe(1);
expect(result.complexFunctions[0].functions.length).toBe(1);
expect(result.complexFunctions[0].functions[0].name).toBe('moderateFunction');
});
});