UNPKG

agentsqripts

Version:

Comprehensive static code analysis toolkit for identifying technical debt, security vulnerabilities, performance issues, and code quality problems

124 lines (108 loc) 3.87 kB
// 🔗 Tests: analyzeCodeComplexity → analyzeTechnicalDebt → calculateMaintainabilityIndex const { analyzeCodeComplexity } = require('./analyzeCodeComplexity'); // Import directly from atomic modules following SRP const { analyzeTechnicalDebt } = require('./technicalDebtAnalyzer'); const { calculateMaintainabilityIndex } = require('./maintainabilityCalculator'); const { identifyComplexityIssues } = require('./complexityIssueDetector'); const fs = require('fs'); const path = require('path'); /** * Test runner for codeAnalysis utilities */ async function runTests() { const results = { total: 0, passed: 0 }; console.log('=== Testing Code Analysis Utilities ==='); // Test calculateMaintainabilityIndex results.total++; try { const metrics = { linesOfCode: 100, cyclomaticComplexity: 5, functionCount: 8 }; const index = calculateMaintainabilityIndex(metrics.linesOfCode, metrics.cyclomaticComplexity); if (typeof index === 'number' && index >= 0 && index <= 100) { console.log('✓ calculateMaintainabilityIndex returns valid index'); results.passed++; } else { console.log('✗ calculateMaintainabilityIndex failed - invalid index range'); } } catch (error) { console.log(`✗ calculateMaintainabilityIndex failed: ${error.message}`); } // Test analyzeCodeComplexity with this test file results.total++; try { const analysis = await analyzeCodeComplexity(__filename); if (analysis && analysis.metrics && typeof analysis.metrics.linesOfCode === 'number' && typeof analysis.metrics.cyclomaticComplexity === 'number' && typeof analysis.maintainabilityIndex === 'number') { console.log('✓ analyzeCodeComplexity returns valid analysis'); results.passed++; } else { console.log('✗ analyzeCodeComplexity failed - invalid analysis structure'); } } catch (error) { console.log(`✗ analyzeCodeComplexity failed: ${error.message}`); } // Test analyzeTechnicalDebt with sample content results.total++; try { const sampleContent = ` // TODO: fix this later function test() { var x = 12345; // magic number return x; } `; const debt = analyzeTechnicalDebt(sampleContent); if (debt && typeof debt.debtScore === 'number' && Array.isArray(debt.issues) && debt.debtLevel) { console.log('✓ analyzeTechnicalDebt returns valid analysis'); results.passed++; } else { console.log('✗ analyzeTechnicalDebt failed - invalid analysis structure'); } } catch (error) { console.log(`✗ analyzeTechnicalDebt failed: ${error.message}`); } // Test identifyComplexityIssues with sample content results.total++; try { const sampleContent = ` function veryLongFunction() { if (condition) { if (another) { if (nested) { if (deep) { return true; } } } } } `; const mockMetrics = { cyclomaticComplexity: 15, linesOfCode: 200, functionCount: 10 }; const issues = identifyComplexityIssues(sampleContent, mockMetrics); if (Array.isArray(issues)) { console.log('✓ identifyComplexityIssues returns valid issues array'); results.passed++; } else { console.log('✗ identifyComplexityIssues failed - invalid return type'); } } catch (error) { console.log(`✗ identifyComplexityIssues failed: ${error.message}`); } console.log(`=== Code Analysis Test Results ===`); console.log(`Tests passed: ${results.passed}/${results.total}`); console.log(`Success rate: ${((results.passed / results.total) * 100).toFixed(1)}%`); return results; } module.exports = { runTests };