test-git-analyzer.jsโข6.5 kB
#!/usr/bin/env node
/**
* Test script for the enhanced GitAnalyzer
* CONTEXT: Testing comprehensive Git analysis functionality
* REASON: Validate GitAnalyzer implementation and demonstrate capabilities
* CHANGE: Added test script for GitAnalyzer validation
* PREVENTION: Unvalidated GitAnalyzer implementation
*/
import { GitAnalyzer } from './src/analyzers/gitAnalyzer.js';
import { KuzuClient } from './src/database/kuzuClient.js';
import { logger } from './src/utils/logger.js';
import fs from 'fs/promises';
import path from 'path';
async function testGitAnalyzer() {
try {
console.log('๐ Testing Enhanced GitAnalyzer...\n');
// Use current directory as test repository
const repoPath = process.cwd();
// Check if it's a Git repository
try {
await fs.access(path.join(repoPath, '.git'));
console.log(`โ
Found Git repository at: ${repoPath}`);
} catch (error) {
console.log('โ Current directory is not a Git repository');
console.log('Please run this test from within a Git repository');
return;
}
// Initialize Kuzu client (optional for testing)
let kuzuClient = null;
try {
kuzuClient = new KuzuClient({
databasePath: './.kg-context/test-git-analysis.kuzu'
});
await kuzuClient.connect();
console.log('โ
Connected to Kuzu database for knowledge graph storage');
} catch (error) {
console.log('โ ๏ธ Could not connect to Kuzu database, proceeding without KG storage');
console.log(` Error: ${error.message}`);
}
// Create GitAnalyzer instance
const gitAnalyzer = new GitAnalyzer(repoPath, kuzuClient);
// Set up event listeners
gitAnalyzer.on('analysisComplete', (analysis) => {
console.log('\n๐ Analysis completed successfully!');
console.log(` Total analysis time: ${analysis.metrics.totalAnalysisTime}ms`);
});
gitAnalyzer.on('analysisError', (error) => {
console.error('\nโ Analysis failed:', error.message);
});
console.log('\n๐ Starting comprehensive Git analysis...');
// Run comprehensive analysis
const startTime = Date.now();
const analysis = await gitAnalyzer.analyzeRepository({
storeInKG: !!kuzuClient // Only store if Kuzu is available
});
const analysisTime = Date.now() - startTime;
// Display results summary
console.log(`\n๐ Analysis Results Summary:`);
console.log(` Repository: ${analysis.metadata?.rootDirectory || repoPath}`);
console.log(` Analysis Duration: ${analysisTime}ms`);
console.log(` Total Commits Analyzed: ${analysis.summary?.totalCommits || 0}`);
console.log(` Contributors: ${analysis.contributors?.total || 0}`);
console.log(` Hotspots Identified: ${analysis.hotspots?.length || 0}`);
console.log(` Technical Debt Level: ${analysis.technicalDebt?.debtLevel || 'unknown'}`);
console.log(` Change Couplings: ${analysis.changeCoupling?.strongCouplings?.length || 0}`);
console.log(` Insights Generated: ${analysis.insights?.length || 0}`);
// Display key insights
if (analysis.insights && analysis.insights.length > 0) {
console.log(`\n๐ก Key Insights:`);
analysis.insights.slice(0, 5).forEach((insight, index) => {
console.log(` ${index + 1}. [${insight.severity.toUpperCase()}] ${insight.message}`);
});
}
// Display top hotspots
if (analysis.hotspots && analysis.hotspots.length > 0) {
console.log(`\n๐ฅ Top Hotspots:`);
analysis.hotspots.slice(0, 5).forEach((hotspot, index) => {
console.log(` ${index + 1}. ${hotspot.file} (risk: ${hotspot.riskScore.toFixed(2)})`);
});
}
// Display technical debt files
if (analysis.technicalDebt?.highDebtFiles && analysis.technicalDebt.highDebtFiles.length > 0) {
console.log(`\nโ ๏ธ High Technical Debt Files:`);
analysis.technicalDebt.highDebtFiles.slice(0, 5).forEach((debt, index) => {
console.log(` ${index + 1}. ${debt.file} (debt: ${debt.debtScore.toFixed(2)}, priority: ${debt.priority})`);
});
}
// Display contributor insights
if (analysis.contributors?.busFactor) {
console.log(`\n๐ฅ Team Health:`);
console.log(` Bus Factor: ${analysis.contributors.busFactor.factor} (${analysis.contributors.busFactor.risk} risk)`);
console.log(` Active Contributors: ${analysis.contributors.diversityMetrics?.activeContributors || 0}`);
}
// Save detailed results to file
const resultsFile = './git-analysis-results.json';
await fs.writeFile(resultsFile, JSON.stringify(analysis, null, 2));
console.log(`\n๐พ Detailed results saved to: ${resultsFile}`);
// Test specific methods
console.log(`\n๐งช Testing specific analysis methods...`);
// Test code churn analysis
const churnAnalysis = await gitAnalyzer.analyzeCodeChurn();
console.log(` Code Churn: ${churnAnalysis.commits?.length || 0} commits analyzed`);
// Test contributor analysis
const contributorAnalysis = await gitAnalyzer.analyzeContributors();
console.log(` Contributors: ${contributorAnalysis.total} total, ${contributorAnalysis.topContributors?.length || 0} top contributors`);
// Test change coupling
const couplingAnalysis = await gitAnalyzer.analyzeChangeCoupling();
console.log(` Change Coupling: ${couplingAnalysis.strongCouplings?.length || 0} strong couplings detected`);
// Clean up
if (kuzuClient) {
await kuzuClient.close();
console.log(`\nโ
Closed Kuzu database connection`);
}
console.log(`\n๐ฏ GitAnalyzer test completed successfully!`);
console.log(`\nKey capabilities demonstrated:`);
console.log(` โ
Comprehensive repository analysis`);
console.log(` โ
Technical debt identification`);
console.log(` โ
Change coupling detection`);
console.log(` โ
Hotspot identification`);
console.log(` โ
Team collaboration analysis`);
console.log(` โ
Knowledge graph integration`);
console.log(` โ
Actionable insights generation`);
} catch (error) {
console.error('โ Test failed:', error);
logger.error('GitAnalyzer test failed:', error);
process.exit(1);
}
}
// Run the test
testGitAnalyzer().catch(console.error);