UNPKG

ai-debug-local-mcp

Version:

🎯 ENHANCED AI GUIDANCE v4.1.2: Dramatically improved tool descriptions help AI users choose the right tools instead of 'close enough' options. Ultra-fast keyboard automation (10x speed), universal recording, multi-ecosystem debugging support, and compreh

289 lines • 11.3 kB
/** * TDD Workflow Handler - Test-Driven Development Monitoring * * Implements user-requested TDD cycle monitoring from Cycle 28 feedback * Provides tools for tracking red-green-refactor phases with intelligent guidance */ import * as fs from 'fs'; /** * TDD Workflow Handler - Implements TDD-specific monitoring and guidance tools * Based on real-world user feedback from Cycle 28 (8.5/10 satisfaction score) */ export class TddWorkflowHandler { tools; cycleHistory = new Map(); constructor() { this.tools = this.getTools(); } /** * Get available TDD workflow tools */ getTools() { return [ { name: 'tdd_cycle_monitor', description: 'Track TDD cycle phases (red-green-refactor) with file monitoring and intelligent guidance', inputSchema: { type: 'object', properties: { phase: { type: 'string', enum: ['red', 'green', 'refactor'], description: 'Current TDD phase' }, testFile: { type: 'string', description: 'Path to test file' }, implementationFile: { type: 'string', description: 'Path to implementation file' } }, required: ['phase', 'testFile', 'implementationFile'] } }, { name: 'red_green_refactor_tracker', description: 'Automatically detect and track TDD phase transitions', inputSchema: { type: 'object', properties: { testFile: { type: 'string', description: 'Path to test file' }, implementationFile: { type: 'string', description: 'Path to implementation file' }, autoDetect: { type: 'boolean', description: 'Enable automatic phase detection', default: true } }, required: ['testFile', 'implementationFile'] } }, { name: 'test_coverage_delta', description: 'Monitor test coverage changes during TDD cycles', inputSchema: { type: 'object', properties: { testFile: { type: 'string', description: 'Path to test file' }, implementationFile: { type: 'string', description: 'Path to implementation file' }, baseline: { type: 'number', description: 'Baseline coverage percentage', minimum: 0, maximum: 1 } }, required: ['testFile', 'implementationFile', 'baseline'] } } ]; } /** * Handle tool requests by routing to appropriate methods */ async handle(toolName, args, sessions) { switch (toolName) { case 'tdd_cycle_monitor': return this.tddCycleMonitor(args, sessions); case 'red_green_refactor_tracker': return this.redGreenRefactorTracker(args, sessions); case 'test_coverage_delta': return this.testCoverageDelta(args, sessions); default: throw new Error(`Unknown tool: ${toolName}`); } } /** * Monitor TDD cycle phases with intelligent guidance * Implements user's exact specification from Cycle 28 feedback */ async tddCycleMonitor(args, sessions) { // Validate inputs if (!['red', 'green', 'refactor'].includes(args.phase)) { throw new Error('Invalid TDD phase. Must be red, green, or refactor'); } if (!args.testFile || !args.implementationFile) { throw new Error('Test file and implementation file are required'); } const timestamp = Date.now(); const warnings = []; // Validate file existence if (!fs.existsSync(args.testFile)) { warnings.push('Test file not found'); } if (!fs.existsSync(args.implementationFile)) { warnings.push('Implementation file not found'); } // Get or create cycle history for this file pair const historyKey = `${args.testFile}:${args.implementationFile}`; let cycleHistory = this.cycleHistory.get(historyKey) || []; // Add current entry to history const currentEntry = { phase: args.phase, timestamp, testFile: args.testFile, implementationFile: args.implementationFile }; cycleHistory.push(currentEntry); this.cycleHistory.set(historyKey, cycleHistory); // Generate phase-specific suggestions const suggestions = this.generatePhaseSuggestions(args.phase); // Generate intelligent suggestions based on history const intelligentSuggestions = this.generateIntelligentSuggestions(cycleHistory); const result = { phase: args.phase, status: 'monitoring', testFile: args.testFile, implementationFile: args.implementationFile, timestamp, suggestions, cycleHistory, intelligentSuggestions }; if (warnings.length > 0) { result.warnings = warnings; } return result; } /** * Automatically track red-green-refactor phase transitions */ async redGreenRefactorTracker(args, sessions) { const historyKey = `${args.testFile}:${args.implementationFile}`; const phaseTransitions = this.cycleHistory.get(historyKey) || []; // Analyze current phase based on file states and history const currentPhase = this.detectCurrentPhase(args, phaseTransitions); const recommendations = this.generatePhaseRecommendations(currentPhase, phaseTransitions); return { currentPhase, phaseTransitions, recommendations }; } /** * Calculate test coverage changes during TDD cycle */ async testCoverageDelta(args, sessions) { // Mock coverage calculation for now - in real implementation would use coverage tools const previousCoverage = args.baseline; const currentCoverage = this.calculateCurrentCoverage(args.testFile, args.implementationFile); const delta = currentCoverage - previousCoverage; let status; if (delta > 0.01) { status = 'improved'; } else if (delta < -0.01) { status = 'decreased'; } else { status = 'maintained'; } return { previousCoverage, currentCoverage, delta, status }; } /** * Generate phase-specific suggestions based on TDD best practices */ generatePhaseSuggestions(phase) { switch (phase) { case 'red': return [ 'Write minimal implementation to make test pass', 'Focus on making the test fail for the right reason', 'Ensure test clearly expresses the requirement', 'Avoid over-engineering at this stage' ]; case 'green': return [ 'Run tests to verify implementation works', 'Write minimal code to pass the test', 'Avoid premature optimization', 'Focus on making tests pass quickly' ]; case 'refactor': return [ 'Improve code quality without changing behavior', 'Extract common patterns and remove duplication', 'Ensure all tests still pass after refactoring', 'Consider design patterns and clean code principles' ]; } } /** * Generate intelligent suggestions based on cycle history patterns */ generateIntelligentSuggestions(cycleHistory) { if (cycleHistory.length < 2) { return ['Pattern detected: Starting TDD cycle']; } const suggestions = []; // Analyze patterns in cycle history const recentPhases = cycleHistory.slice(-3).map(entry => entry.phase); if (recentPhases.includes('red') && recentPhases.includes('green')) { suggestions.push('Pattern detected: Successful red-green transition'); } if (cycleHistory.length > 5) { suggestions.push('Pattern detected: Extended TDD cycle - consider breaking down tasks'); } return suggestions; } /** * Detect current TDD phase based on file states and history */ detectCurrentPhase(args, history) { // Simple heuristic - in real implementation would analyze test results and file changes if (history.length === 0) { return 'red'; // Start with red phase } const lastPhase = history[history.length - 1].phase; // Cycle through phases switch (lastPhase) { case 'red': return 'green'; case 'green': return 'refactor'; case 'refactor': return 'red'; } } /** * Generate recommendations based on current phase and history */ generatePhaseRecommendations(currentPhase, history) { const baseRecommendations = this.generatePhaseSuggestions(currentPhase); // Add history-based recommendations if (history.length > 3) { baseRecommendations.push('Consider splitting into smaller test cases'); } return baseRecommendations; } /** * Calculate current test coverage (mock implementation) */ calculateCurrentCoverage(testFile, implementationFile) { // Mock calculation - real implementation would use coverage tools like nyc, jest, etc. if (fs.existsSync(testFile) && fs.existsSync(implementationFile)) { const testSize = fs.statSync(testFile).size; const implSize = fs.statSync(implementationFile).size; // Simple heuristic: larger test files generally mean better coverage return Math.min(0.95, 0.3 + (testSize / implSize) * 0.4); } return 0.0; } } //# sourceMappingURL=tdd-workflow-handler.js.map