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
557 lines (525 loc) โข 25 kB
JavaScript
/**
* Sub-Agent Test Generation Integration
*
* Automatically generates tests based on sub-agent debugging findings.
* Each specialized agent generates tests that prevent the specific issues they discover.
* Revolutionary "Debug Once, Test Forever" approach integrated into delegation workflow.
*
* ENHANCED: Now includes automatic test quality review using TestReviewerAI
* to ensure all generated tests meet production quality standards.
*/
import { IntelligentTestReviewOrchestrator } from './intelligent-test-review-orchestrator.js';
export class SubAgentTestGenerator {
static reviewOrchestrator = new IntelligentTestReviewOrchestrator({
preferSubAgent: true,
fallbackEnabled: true,
maxSubAgentRetries: 2,
subAgentTimeout: 10000,
batchSizeThreshold: 10,
legacyQualityThreshold: 0.85,
subAgentQualityThreshold: 0.90,
alwaysUseSubAgentFor: ['performance', 'accessibility', 'error-prevention'],
neverUseSubAgentFor: ['syntax-only']
});
static AGENT_TEST_STRATEGIES = {
'debug-discovery-agent': {
testTypes: ['integration', 'e2e'],
focus: 'Framework detection and initial setup validation',
testPatterns: ['framework-boot', 'environment-setup', 'initial-health-check']
},
'performance-analysis-agent': {
testTypes: ['performance', 'e2e'],
focus: 'Performance regression prevention and Core Web Vitals monitoring',
testPatterns: ['core-web-vitals', 'performance-regression', 'bundle-size', 'load-time']
},
'accessibility-audit-agent': {
testTypes: ['accessibility', 'e2e'],
focus: 'WCAG compliance and accessibility barrier prevention',
testPatterns: ['wcag-compliance', 'keyboard-navigation', 'screen-reader', 'color-contrast']
},
'error-investigation-agent': {
testTypes: ['unit', 'integration', 'e2e'],
focus: 'Error reproduction and prevention',
testPatterns: ['error-scenarios', 'exception-handling', 'edge-cases', 'error-boundaries']
},
'validation-testing-agent': {
testTypes: ['e2e', 'integration'],
focus: 'User flow validation and regression testing',
testPatterns: ['user-flows', 'regression-scenarios', 'cross-browser', 'mobile-responsive']
},
'framework-specialist-agent': {
testTypes: ['unit', 'integration', 'e2e'],
focus: 'Framework-specific functionality and optimization validation',
testPatterns: ['framework-features', 'component-behavior', 'framework-optimization', 'ssr-hydration']
},
'data-extraction-agent': {
testTypes: ['integration', 'unit'],
focus: 'Data flow and API validation',
testPatterns: ['api-responses', 'data-validation', 'network-resilience', 'data-integrity']
},
'testing-infrastructure-agent': {
testTypes: ['unit', 'integration', 'e2e'],
focus: 'Test infrastructure and TDD workflow validation',
testPatterns: ['test-infrastructure', 'tdd-workflows', 'mock-behavior', 'test-reliability']
}
};
/**
* Generate tests based on sub-agent debugging results
* ENHANCED: Now includes automatic test quality review
*/
static async generateTestsFromDebugging(context) {
const strategy = this.AGENT_TEST_STRATEGIES[context.agentType];
if (!strategy) {
console.warn(`โ ๏ธ No test generation strategy for agent: ${context.agentType}`);
return [];
}
const generatedTests = [];
// Generate tests for each issue found
for (const issue of context.issuesFound) {
const preventionTests = await this.generateIssuePreventionTests(issue, context, strategy);
generatedTests.push(...preventionTests);
}
// Generate tests for optimizations applied
for (const optimization of context.optimizationsApplied) {
const optimizationTests = await this.generateOptimizationValidationTests(optimization, context, strategy);
generatedTests.push(...optimizationTests);
}
// Generate proactive tests based on agent specialization
const proactiveTests = await this.generateProactiveTests(context, strategy);
generatedTests.push(...proactiveTests);
console.log(`๐งช Generated ${generatedTests.length} tests from ${context.agentType} debugging session`);
// ENHANCED: Review all generated tests with intelligent orchestration
const reviewedTests = await this.reviewGeneratedTestsIntelligently(generatedTests, context);
const approvedCount = reviewedTests.filter(t => t.approved).length;
const contextAwareCount = reviewedTests.filter(t => t.contextAware).length;
const issueValidatedCount = reviewedTests.filter(t => t.issueValidated).length;
console.log(`โ
Intelligent Test Review: ${approvedCount}/${reviewedTests.length} approved`);
console.log(`๐ง Context-aware reviews: ${contextAwareCount}/${reviewedTests.length}`);
console.log(`๐ฏ Issue validation: ${issueValidatedCount}/${reviewedTests.length}`);
return reviewedTests;
}
/**
* Generate tests that prevent specific issues found during debugging
*/
static async generateIssuePreventionTests(issue, context, strategy) {
const tests = [];
switch (context.agentType) {
case 'performance-analysis-agent':
if (issue.type.includes('slow-loading') || issue.type.includes('performance')) {
tests.push({
testType: 'performance',
testName: `performance_regression_${issue.type}`,
testDescription: `Prevent performance regression: ${issue.description}`,
testCode: this.generatePerformanceTest(issue, context),
framework: context.frameworkDetected || 'generic',
priority: issue.severity === 'critical' ? 'high' : 'medium',
preventionTarget: issue.description,
agentSource: context.agentType
});
}
break;
case 'accessibility-audit-agent':
if (issue.type.includes('accessibility') || issue.type.includes('wcag')) {
tests.push({
testType: 'accessibility',
testName: `accessibility_compliance_${issue.type}`,
testDescription: `Ensure WCAG compliance: ${issue.description}`,
testCode: this.generateAccessibilityTest(issue, context),
framework: context.frameworkDetected || 'generic',
priority: 'high', // Accessibility is always high priority
preventionTarget: issue.description,
agentSource: context.agentType
});
}
break;
case 'error-investigation-agent':
tests.push({
testType: issue.type.includes('javascript') ? 'unit' : 'integration',
testName: `error_prevention_${issue.type}`,
testDescription: `Prevent error recurrence: ${issue.description}`,
testCode: this.generateErrorPreventionTest(issue, context),
framework: context.frameworkDetected || 'generic',
priority: issue.severity === 'critical' ? 'high' : 'medium',
preventionTarget: issue.description,
agentSource: context.agentType
});
break;
case 'validation-testing-agent':
if (issue.type.includes('user-flow') || issue.type.includes('regression')) {
tests.push({
testType: 'e2e',
testName: `user_flow_validation_${issue.type}`,
testDescription: `Validate user flow: ${issue.description}`,
testCode: this.generateUserFlowTest(issue, context),
framework: context.frameworkDetected || 'generic',
priority: 'high',
preventionTarget: issue.description,
agentSource: context.agentType
});
}
break;
case 'framework-specialist-agent':
tests.push({
testType: 'integration',
testName: `framework_specific_${issue.type}`,
testDescription: `Validate framework behavior: ${issue.description}`,
testCode: this.generateFrameworkTest(issue, context),
framework: context.frameworkDetected || 'generic',
priority: 'medium',
preventionTarget: issue.description,
agentSource: context.agentType
});
break;
}
return tests;
}
/**
* Generate tests that validate optimizations continue to work
*/
static async generateOptimizationValidationTests(optimization, context, strategy) {
const tests = [];
// Performance optimizations need performance tests
if (context.agentType === 'performance-analysis-agent') {
tests.push({
testType: 'performance',
testName: `optimization_validation_${optimization.type}`,
testDescription: `Validate optimization continues to work: ${optimization.description}`,
testCode: this.generateOptimizationValidationTest(optimization, context),
framework: context.frameworkDetected || 'generic',
priority: 'medium',
preventionTarget: `Regression of ${optimization.description}`,
agentSource: context.agentType
});
}
// Framework optimizations need framework-specific tests
if (context.agentType === 'framework-specialist-agent') {
tests.push({
testType: 'integration',
testName: `framework_optimization_${optimization.type}`,
testDescription: `Validate framework optimization: ${optimization.description}`,
testCode: this.generateFrameworkOptimizationTest(optimization, context),
framework: context.frameworkDetected || 'generic',
priority: 'medium',
preventionTarget: `Regression of ${optimization.description}`,
agentSource: context.agentType
});
}
return tests;
}
/**
* Generate proactive tests based on agent specialization
*/
static async generateProactiveTests(context, strategy) {
const tests = [];
// Each agent generates proactive tests for their domain
for (const pattern of strategy.testPatterns) {
tests.push({
testType: strategy.testTypes[0],
testName: `proactive_${context.agentType}_${pattern}`,
testDescription: `Proactive ${strategy.focus} validation for ${pattern}`,
testCode: this.generateProactiveTest(pattern, context, strategy),
framework: context.frameworkDetected || 'generic',
priority: 'low',
preventionTarget: `Proactive monitoring for ${pattern} issues`,
agentSource: context.agentType
});
}
return tests;
}
/**
* Review all generated tests using Intelligent Test Review Orchestrator
* REVOLUTIONARY: Context-aware validation with intelligent fallback to legacy patterns
*/
static async reviewGeneratedTestsIntelligently(tests, context) {
const reviewedTests = [];
console.log(`๐ Reviewing ${tests.length} generated tests for quality...`);
// Use intelligent batch review for optimal performance
try {
const intelligentResults = await this.reviewOrchestrator.reviewTestBatch(tests, context);
for (let i = 0; i < tests.length; i++) {
const test = tests[i];
const reviewResult = intelligentResults[i];
// Enhance test with intelligent review information
const reviewedTest = {
...test,
reviewResult,
qualityScore: reviewResult.overallScore,
approved: reviewResult.approved,
reviewFeedback: reviewResult.feedback,
contextAware: reviewResult.contextAware,
issueValidated: reviewResult.issueValidated
};
reviewedTests.push(reviewedTest);
// Log intelligent review results
const status = reviewedTest.approved ? 'โ
APPROVED' : 'โ ๏ธ NEEDS IMPROVEMENT';
const score = `${(reviewedTest.qualityScore * 100).toFixed(1)}%`;
const strategy = reviewResult.reviewStrategy === 'sub-agent' ? '๐ง INTELLIGENT' : 'โก LEGACY';
const contextStatus = reviewResult.contextAware ? '๐ฏ CONTEXT-AWARE' : '๐ SYNTAX-ONLY';
console.log(`๐ ${test.testName}: ${status} (${score}) ${strategy} ${contextStatus}`);
if (reviewResult.escalateToHuman) {
console.log(`๐จ ${test.testName} flagged for human review - complex test detected`);
}
if (reviewResult.issueValidated) {
console.log(`โ
${test.testName} validated to prevent actual discovered issue`);
}
}
}
catch (error) {
console.warn(`โ ๏ธ Intelligent batch review failed: ${error}. Using individual reviews.`);
// Fallback to individual review
for (const test of tests) {
try {
const reviewResult = await this.reviewOrchestrator.reviewTest(test, context);
reviewedTests.push({
...test,
reviewResult,
qualityScore: reviewResult.overallScore,
approved: reviewResult.approved,
reviewFeedback: reviewResult.feedback,
contextAware: reviewResult.contextAware,
issueValidated: reviewResult.issueValidated
});
}
catch (individualError) {
console.warn(`โ ๏ธ Failed to review test ${test.testName}:`, individualError);
reviewedTests.push({
...test,
approved: false,
reviewFeedback: `Review failed: ${individualError instanceof Error ? individualError.message : 'Unknown error'}`,
contextAware: false,
issueValidated: false
});
}
}
}
return reviewedTests;
}
/**
* Configure the intelligent test review orchestrator
*/
static configureReviewOrchestrator(config) {
if (config.subAgentAvailable !== undefined) {
this.reviewOrchestrator.setSubAgentAvailability(config.subAgentAvailable);
}
if (config.environment) {
const envConfigs = {
production: { preferSubAgent: true, fallbackEnabled: true },
development: { preferSubAgent: true, batchSizeThreshold: 5 },
ci: { preferSubAgent: false, batchSizeThreshold: 20 },
offline: { preferSubAgent: false, fallbackEnabled: true }
};
this.reviewOrchestrator.updateConfig(envConfigs[config.environment]);
}
if (config.preferSubAgent !== undefined) {
this.reviewOrchestrator.updateConfig({ preferSubAgent: config.preferSubAgent });
}
console.log(`๐ง Test review orchestrator configured for ${config.environment || 'custom'} environment`);
}
/**
* Get orchestration statistics
*/
static getReviewStats() {
return this.reviewOrchestrator.getOrchestrationStats();
}
/**
* Apply automatic fixes to test code based on review suggestions
*/
static applyAutoFixes(testCode, suggestions) {
let fixedCode = testCode;
for (const suggestion of suggestions) {
// Apply common auto-fixes based on suggestion patterns
if (suggestion.includes('data-testid')) {
// Replace brittle selectors with data-testid
fixedCode = fixedCode.replace(/page\.locator\('[#.][\w-]+'\)/g, 'page.locator(\'[data-testid="test-element"]\')');
}
if (suggestion.includes('await')) {
// Add missing await keywords
fixedCode = fixedCode.replace(/page\.(click|fill|goto|type|select)\(/g, 'await page.$1(');
}
if (suggestion.includes('expect')) {
// Add await to expect statements that might need it
fixedCode = fixedCode.replace(/expect\(page\.locator/g, 'await expect(page.locator');
}
}
return fixedCode;
}
// Test code generation methods
static generatePerformanceTest(issue, context) {
return `
// Performance test generated by ${context.agentType}
// Prevents: ${issue.description}
describe('Performance Regression Prevention', () => {
test('should maintain performance standards for ${issue.type}', async () => {
const startTime = performance.now();
// Simulate the scenario that caused the performance issue
await simulateUserScenario('${context.sessionMetadata.url}');
const endTime = performance.now();
const duration = endTime - startTime;
// Assert performance meets standards
expect(duration).toBeLessThan(2000); // 2 second threshold
// Validate Core Web Vitals if applicable
const vitals = await getCoreWebVitals();
expect(vitals.LCP).toBeLessThan(2500);
expect(vitals.FCP).toBeLessThan(1800);
});
});`;
}
static generateAccessibilityTest(issue, context) {
return `
// Accessibility test generated by ${context.agentType}
// Prevents: ${issue.description}
describe('Accessibility Compliance', () => {
test('should maintain WCAG AA compliance for ${issue.type}', async () => {
const { page } = await setupAccessibilityTest('${context.sessionMetadata.url}');
// Run accessibility audit
const results = await runAxeAudit(page);
// Assert no critical accessibility violations
const violations = results.violations.filter(v => v.impact === 'critical' || v.impact === 'serious');
expect(violations).toHaveLength(0);
// Specific check for the issue that was found
const specificCheck = await checkSpecificA11y(page, '${issue.type}');
expect(specificCheck.passed).toBe(true);
});
});`;
}
static generateErrorPreventionTest(issue, context) {
const testCode = issue.type.includes('javascript') ?
this.generateJavaScriptErrorTest(issue, context) :
this.generateNetworkErrorTest(issue, context);
return `
// Error prevention test generated by ${context.agentType}
// Prevents: ${issue.description}
${testCode}`;
}
static generateJavaScriptErrorTest(issue, context) {
return `
describe('JavaScript Error Prevention', () => {
test('should handle ${issue.type} without throwing errors', () => {
// Reproduce the scenario that caused the error
const errorSpy = jest.spyOn(console, 'error').mockImplementation();
try {
// Simulate the problematic scenario
${issue.reproductionSteps?.join(';\n ') || '// Reproduce error scenario'}
// Assert no errors were logged
expect(errorSpy).not.toHaveBeenCalled();
} finally {
errorSpy.mockRestore();
}
});
});`;
}
static generateNetworkErrorTest(issue, context) {
return `
describe('Network Error Prevention', () => {
test('should handle ${issue.type} gracefully', async () => {
// Mock network conditions that caused the error
const mockFetch = jest.fn();
global.fetch = mockFetch;
// Simulate the error condition
mockFetch.mockRejectedValueOnce(new Error('${issue.description}'));
// Test error handling
const result = await networkOperation();
// Assert graceful error handling
expect(result.error).toBeDefined();
expect(result.fallbackUsed).toBe(true);
});
});`;
}
static generateUserFlowTest(issue, context) {
return `
// User flow test generated by ${context.agentType}
// Prevents: ${issue.description}
describe('User Flow Validation', () => {
test('should complete ${issue.type} user flow successfully', async () => {
const { page } = await setupE2ETest('${context.sessionMetadata.url}');
// Execute the user flow that had issues
${context.sessionMetadata.userFlow?.map(step => `await page.${step};`).join('\n ') || '// User flow steps'}
// Validate successful completion
await expect(page.locator('[data-testid="success-indicator"]')).toBeVisible();
// Check for any console errors during the flow
const errors = await page.evaluate(() => window.errors || []);
expect(errors).toHaveLength(0);
});
});`;
}
static generateFrameworkTest(issue, context) {
const frameworkSpecific = this.getFrameworkSpecificTestCode(context.frameworkDetected || 'generic', issue);
return `
// Framework-specific test generated by ${context.agentType}
// Prevents: ${issue.description}
${frameworkSpecific}`;
}
static getFrameworkSpecificTestCode(framework, issue) {
switch (framework.toLowerCase()) {
case 'react':
return `
describe('React Component Behavior', () => {
test('should handle ${issue.type} in React components', () => {
const { render, screen } = testingLibrary;
const TestComponent = () => <div>{/* Component logic */}</div>;
render(<TestComponent />);
// Validate React-specific behavior
expect(screen.getByRole('main')).toBeInTheDocument();
});
});`;
case 'nextjs':
return `
describe('Next.js Behavior', () => {
test('should handle ${issue.type} in Next.js environment', async () => {
const { req, res } = createMocks();
await handler(req, res);
expect(res._getStatusCode()).toBe(200);
});
});`;
default:
return `
describe('Framework Behavior', () => {
test('should handle ${issue.type} correctly', () => {
// Generic framework test
expect(true).toBe(true);
});
});`;
}
}
static generateOptimizationValidationTest(optimization, context) {
return `
// Optimization validation test generated by ${context.agentType}
// Validates: ${optimization.description}
describe('Optimization Validation', () => {
test('should maintain ${optimization.type} optimization benefits', async () => {
const beforeMetrics = await measureBaseline();
// Execute the optimized flow
await executeOptimizedFlow('${context.sessionMetadata.url}');
const afterMetrics = await measureOptimized();
// Validate optimization is still effective
expect(afterMetrics.improvement).toBeGreaterThan(beforeMetrics.baseline * 0.9);
});
});`;
}
static generateFrameworkOptimizationTest(optimization, context) {
return `
// Framework optimization test generated by ${context.agentType}
// Validates: ${optimization.description}
describe('Framework Optimization', () => {
test('should maintain ${optimization.type} framework optimization', () => {
// Test framework-specific optimization
const optimizedResult = runFrameworkOptimization();
expect(optimizedResult.performanceGain).toBeGreaterThan(0);
expect(optimizedResult.errors).toHaveLength(0);
});
});`;
}
static generateProactiveTest(pattern, context, strategy) {
return `
// Proactive test generated by ${context.agentType}
// Pattern: ${pattern}
describe('Proactive ${strategy.focus}', () => {
test('should monitor for ${pattern} issues', async () => {
const monitoringResult = await monitorFor('${pattern}', '${context.sessionMetadata.url}');
expect(monitoringResult.issuesDetected).toHaveLength(0);
expect(monitoringResult.healthScore).toBeGreaterThan(0.8);
});
});`;
}
}
//# sourceMappingURL=sub-agent-test-generation-integration.js.map