|
| 1 | +/** |
| 2 | + * Detailed Coverage Attribution Analysis for mcp-debugger |
| 3 | + * Run manually with: npm run test:coverage:analyze |
| 4 | + */ |
| 5 | + |
| 6 | +import fs from 'fs'; |
| 7 | +import path from 'path'; |
| 8 | +import { fileURLToPath } from 'url'; |
| 9 | + |
| 10 | +const __filename = fileURLToPath(import.meta.url); |
| 11 | +const __dirname = path.dirname(__filename); |
| 12 | + |
| 13 | +try { |
| 14 | + const summaryPath = path.join(__dirname, 'coverage', 'coverage-summary.json'); |
| 15 | + |
| 16 | + if (!fs.existsSync(summaryPath)) { |
| 17 | + console.log('No coverage data found. Run: npm run test:coverage'); |
| 18 | + process.exit(1); |
| 19 | + } |
| 20 | + |
| 21 | + const coverage = JSON.parse(fs.readFileSync(summaryPath, 'utf8')); |
| 22 | + const overall = coverage.total ? coverage.total.lines.pct : 0; |
| 23 | + |
| 24 | + let totalUncovered = 0; |
| 25 | + let totalLines = 0; |
| 26 | + const files = []; |
| 27 | + |
| 28 | + for (const [filePath, data] of Object.entries(coverage)) { |
| 29 | + if (filePath === 'total') continue; |
| 30 | + |
| 31 | + const uncovered = data.lines.total - data.lines.covered; |
| 32 | + totalUncovered += uncovered; |
| 33 | + totalLines += data.lines.total; |
| 34 | + |
| 35 | + // Extract relative path for cleaner display |
| 36 | + let cleanPath = filePath.replace(process.cwd(), '').replace(/^[/\\]/, ''); |
| 37 | + cleanPath = cleanPath.replace(/\\/g, '/'); |
| 38 | + |
| 39 | + files.push({ |
| 40 | + path: cleanPath, |
| 41 | + coverage: data.lines.pct, |
| 42 | + uncovered, |
| 43 | + total: data.lines.total |
| 44 | + }); |
| 45 | + } |
| 46 | + |
| 47 | + // Calculate impact on overall coverage |
| 48 | + files.forEach(f => { |
| 49 | + f.impact = totalLines > 0 ? (f.uncovered / totalLines) * 100 : 0; |
| 50 | + }); |
| 51 | + |
| 52 | + // Sort by uncovered lines (descending) |
| 53 | + files.sort((a, b) => b.uncovered - a.uncovered); |
| 54 | + |
| 55 | + console.log('\nMCP-DEBUGGER DETAILED COVERAGE ANALYSIS'); |
| 56 | + console.log('═'.repeat(80)); |
| 57 | + console.log(`Overall Coverage: ${overall.toFixed(1)}%`); |
| 58 | + console.log(`Total Lines: ${totalLines.toLocaleString()}`); |
| 59 | + console.log(`Uncovered Lines: ${totalUncovered.toLocaleString()}`); |
| 60 | + console.log('═'.repeat(80)); |
| 61 | + console.log('\nFiles sorted by number of uncovered lines (highest impact first):\n'); |
| 62 | + console.log('Uncovered Coverage Impact File'); |
| 63 | + console.log('─'.repeat(80)); |
| 64 | + |
| 65 | + // Show all files with uncovered lines |
| 66 | + files.forEach(f => { |
| 67 | + if (f.uncovered > 0) { |
| 68 | + console.log( |
| 69 | + f.uncovered.toString().padStart(9) + |
| 70 | + f.coverage.toFixed(1).padStart(9) + '%' + |
| 71 | + ('+' + f.impact.toFixed(1) + '%').padStart(8) + |
| 72 | + ' ' + f.path |
| 73 | + ); |
| 74 | + } |
| 75 | + }); |
| 76 | + |
| 77 | + console.log('\n' + '─'.repeat(80)); |
| 78 | + console.log('Impact: Percentage points overall coverage would increase if file reaches 100%'); |
| 79 | + |
| 80 | + // Summary insights |
| 81 | + console.log('\n' + '═'.repeat(80)); |
| 82 | + console.log('INSIGHTS:'); |
| 83 | + console.log('─'.repeat(80)); |
| 84 | + |
| 85 | + const top5 = files.slice(0, 5); |
| 86 | + const top5Impact = top5.reduce((sum, f) => sum + f.impact, 0); |
| 87 | + console.log(`• Top 5 files contain ${top5.reduce((sum, f) => sum + f.uncovered, 0)} uncovered lines`); |
| 88 | + console.log(`• Fixing top 5 files would improve coverage by ${top5Impact.toFixed(1)} percentage points`); |
| 89 | + console.log(`• This would bring overall coverage from ${overall.toFixed(1)}% to ${(overall + top5Impact).toFixed(1)}%`); |
| 90 | + |
| 91 | + if (files[0] && files[0].uncovered > 50) { |
| 92 | + console.log(`\n• Priority: Focus on ${files[0].path.split('/').pop()}`); |
| 93 | + console.log(` ${files[0].uncovered} uncovered lines, currently ${files[0].coverage.toFixed(1)}% covered`); |
| 94 | + } |
| 95 | + |
| 96 | + console.log('═'.repeat(80) + '\n'); |
| 97 | + |
| 98 | +} catch (error) { |
| 99 | + console.error('Error:', error.message); |
| 100 | +} |
0 commit comments