Files
proffesor-for-testing__agen…/tests/unit/reasoning/PatternExtractor.test.ts
Profa c3b342a03a fix(init): Complete Phase 2 integration with comprehensive bug fixes (v1.1.0)
## Summary
Successfully fixed all initialization issues and completed Phase 2 integration:
- Fixed agent template discovery and copying (18 agents now copy correctly)
- Added comprehensive logging throughout init process
- Fixed undefined config property serialization bug
- All databases initialize correctly (memory.db, patterns.db)
- Phase 2 features (learning, patterns, improvement) fully functional

## Bug Fixes
1. **Agent Copy Bug**: Changed from directory copy to individual file copy
   - Fixed: "Copied 0 agent definitions" → Now copies all 18 agents
   - Issue: fs.copy with filter wasn't working, changed to per-file copy

2. **Missing Agent**: Created qe-quality-analyzer.md
   - Was the only missing agent out of 17 required QE agents
   - Added complete agent definition with AQE hooks integration

3. **Incomplete Fallback**: Updated fallback from 6 to all 17 agents
   - Ensures all agents are created even if templates not found
   - Maintains feature parity in all scenarios

4. **Serialization Bug**: Added sanitizeConfig() helper
   - Fixed: "Cannot read properties of undefined (reading 'replace')"
   - Issue: jsonfile couldn't serialize config with undefined properties
   - Solution: Recursive sanitization removes undefined values before write

5. **Config Path Bug**: Fixed target vs source file list confusion
   - Was passing source files to createMissingAgents instead of target
   - Now correctly identifies which agents are missing in target

## Improvements
- Added detailed path discovery logging with ✓/✗ checkmarks
- Show agent template counts and copy progress
- Log each config file write with file paths
- Added stack traces to all error handlers (removed verbose-only check)
- Created sanitizeConfig for robust JSON serialization

## Testing
Verified in test project `/tmp/aqe-test-final`:
✅ All 17 QE agents created
✅ memory.db created (221KB, 12 tables)
✅ patterns.db created (155KB, 4 tables + FTS)
✅ All Phase 2 configs written successfully
✅ Complete initialization with no errors

## Phase 2 Features Confirmed Working
- Learning System: Q-learning (lr=0.1, γ=0.95, 20% target)
- Pattern Bank: 85% confidence, extraction enabled
- Improvement Loop: 1hr cycles, A/B testing, manual approval

## Files Changed
- .claude/agents/qe-quality-analyzer.md (NEW)
- src/cli/commands/init.ts (MAJOR FIXES)
  - copyAgentTemplates(): Individual file copy, better logging
  - createBasicAgents(): All 17 agents
  - createMissingAgents(): Fixed file list bug
  - writeFleetConfig(): Added sanitization
  - sanitizeConfig(): NEW helper for undefined removal
- src/learning/* (NEW - 9 files)
- src/reasoning/* (NEW - 8 files)
- tests/* (NEW - 30+ test files)
- docs/* (NEW - 60+ documentation files)

## Breaking Changes
None - all changes are internal improvements

## Migration
No migration needed - initialization now works correctly

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-16 16:25:46 +00:00

370 lines
11 KiB
TypeScript

/**
* PatternExtractor Tests
* Phase 2 (v1.1.0) - Pattern Extraction Specialist
*/
import { PatternExtractor } from '../../../src/reasoning/PatternExtractor';
import { TestFramework, PatternType } from '../../../src/types/pattern.types';
import * as fs from 'fs-extra';
import * as path from 'path';
describe('PatternExtractor', () => {
let extractor: PatternExtractor;
let tempDir: string;
beforeEach(() => {
extractor = new PatternExtractor({
frameworks: [TestFramework.JEST],
minConfidence: 0.7,
minFrequency: 1,
maxPatternsPerFile: 10
});
tempDir = path.join(__dirname, '.temp-test-files');
});
afterEach(async () => {
if (await fs.pathExists(tempDir)) {
await fs.remove(tempDir);
}
});
describe('extractFromFile', () => {
it('should extract edge case patterns', async () => {
const testCode = `
describe('Calculator', () => {
it('should handle null input', () => {
const result = calculate(null);
expect(result).toBeNull();
});
it('should handle undefined input', () => {
const result = calculate(undefined);
expect(result).toBeUndefined();
});
it('should handle empty string', () => {
const result = calculate('');
expect(result).toBe(0);
});
});
`;
await fs.ensureDir(tempDir);
const testFile = path.join(tempDir, 'calculator.test.ts');
await fs.writeFile(testFile, testCode);
const patterns = await extractor.extractFromFile(testFile);
expect(patterns.length).toBeGreaterThan(0);
const edgeCasePatterns = patterns.filter(p => p.type === PatternType.EDGE_CASE);
expect(edgeCasePatterns.length).toBeGreaterThanOrEqual(1);
expect(edgeCasePatterns[0].confidence).toBeGreaterThanOrEqual(0.7);
});
it('should extract boundary condition patterns', async () => {
const testCode = `
describe('Range Validator', () => {
it('should validate values within range', () => {
expect(isInRange(5, 0, 10)).toBe(true);
});
it('should reject values below minimum', () => {
expect(isInRange(-1, 0, 10)).toBe(false);
});
it('should reject values above maximum', () => {
expect(isInRange(11, 0, 10)).toBe(false);
});
});
`;
await fs.ensureDir(tempDir);
const testFile = path.join(tempDir, 'range.test.ts');
await fs.writeFile(testFile, testCode);
const patterns = await extractor.extractFromFile(testFile);
const boundaryPatterns = patterns.filter(p => p.type === PatternType.BOUNDARY_CONDITION);
expect(boundaryPatterns.length).toBeGreaterThan(0);
});
it('should extract error handling patterns', async () => {
const testCode = `
describe('UserService', () => {
it('should throw error for invalid user', () => {
expect(() => createUser({ name: '' })).toThrow(ValidationError);
});
it('should handle async errors', async () => {
await expect(fetchUser('invalid-id')).rejects.toThrow();
});
it('should catch and handle errors', async () => {
try {
await riskyOperation();
} catch (error) {
expect(error).toBeDefined();
}
});
});
`;
await fs.ensureDir(tempDir);
const testFile = path.join(tempDir, 'user.test.ts');
await fs.writeFile(testFile, testCode);
const patterns = await extractor.extractFromFile(testFile);
const errorPatterns = patterns.filter(p => p.type === PatternType.ERROR_HANDLING);
expect(errorPatterns.length).toBeGreaterThan(0);
});
it('should extract mock patterns', async () => {
const testCode = `
describe('EmailService', () => {
it('should send email with mock transport', () => {
const mockTransport = jest.fn().mockReturnValue({ success: true });
const service = new EmailService(mockTransport);
service.send('test@example.com', 'Hello');
expect(mockTransport).toHaveBeenCalled();
});
it('should use spy for verification', () => {
const spy = jest.spyOn(emailClient, 'send');
sendWelcomeEmail('user@example.com');
expect(spy).toHaveBeenCalledWith('user@example.com', expect.any(String));
});
});
`;
await fs.ensureDir(tempDir);
const testFile = path.join(tempDir, 'email.test.ts');
await fs.writeFile(testFile, testCode);
const patterns = await extractor.extractFromFile(testFile);
const mockPatterns = patterns.filter(p => p.type === PatternType.MOCK_PATTERN);
expect(mockPatterns.length).toBeGreaterThan(0);
});
it('should extract async patterns', async () => {
const testCode = `
describe('AsyncAPI', () => {
it('should handle async operations', async () => {
const result = await fetchData();
expect(result).toBeDefined();
});
it('should handle promise chains', () => {
return getData()
.then(data => processData(data))
.then(processed => {
expect(processed).toBeDefined();
});
});
});
`;
await fs.ensureDir(tempDir);
const testFile = path.join(tempDir, 'async.test.ts');
await fs.writeFile(testFile, testCode);
const patterns = await extractor.extractFromFile(testFile);
const asyncPatterns = patterns.filter(p => p.type === PatternType.ASYNC_PATTERN);
expect(asyncPatterns.length).toBeGreaterThan(0);
});
});
describe('extractFromFiles', () => {
it('should extract patterns from multiple files', async () => {
await fs.ensureDir(tempDir);
const file1 = path.join(tempDir, 'test1.test.ts');
await fs.writeFile(file1, `
describe('Test 1', () => {
it('should handle null', () => {
expect(func(null)).toBeNull();
});
});
`);
const file2 = path.join(tempDir, 'test2.test.ts');
await fs.writeFile(file2, `
describe('Test 2', () => {
it('should throw error', () => {
expect(() => func()).toThrow();
});
});
`);
const result = await extractor.extractFromFiles([file1, file2]);
expect(result.patterns.length).toBeGreaterThan(0);
expect(result.statistics.filesProcessed).toBe(2);
expect(result.errors.length).toBe(0);
});
it('should handle parsing errors gracefully', async () => {
await fs.ensureDir(tempDir);
const validFile = path.join(tempDir, 'valid.test.ts');
await fs.writeFile(validFile, `
describe('Valid', () => {
it('works', () => {
expect(true).toBe(true);
});
});
`);
const invalidFile = path.join(tempDir, 'invalid.test.ts');
await fs.writeFile(invalidFile, 'this is not valid JavaScript {{{');
const result = await extractor.extractFromFiles([validFile, invalidFile]);
expect(result.errors.length).toBeGreaterThan(0);
expect(result.errors[0].type).toBe('analysis-error');
});
it('should calculate statistics correctly', async () => {
await fs.ensureDir(tempDir);
const testFile = path.join(tempDir, 'stats.test.ts');
await fs.writeFile(testFile, `
describe('Statistics Test', () => {
it('pattern 1', () => expect(func(null)).toBeNull());
it('pattern 2', () => expect(() => func()).toThrow());
it('pattern 3', async () => expect(await func()).toBeDefined());
});
`);
const result = await extractor.extractFromFiles([testFile]);
expect(result.statistics.filesProcessed).toBe(1);
expect(result.statistics.processingTime).toBeGreaterThan(0);
expect(result.statistics.patternsExtracted).toBeGreaterThan(0);
expect(result.statistics.patternTypeDistribution).toBeDefined();
});
});
describe('pattern deduplication', () => {
it('should deduplicate similar patterns', async () => {
await fs.ensureDir(tempDir);
const testFile = path.join(tempDir, 'dedup.test.ts');
await fs.writeFile(testFile, `
describe('Deduplication Test', () => {
it('should handle null input 1', () => {
expect(func1(null)).toBeNull();
});
it('should handle null input 2', () => {
expect(func2(null)).toBeNull();
});
it('should handle null input 3', () => {
expect(func3(null)).toBeNull();
});
});
`);
const result = await extractor.extractFromFiles([testFile]);
// Patterns should be deduplicated based on type and name similarity
const edgeCasePatterns = result.patterns.filter(p => p.type === PatternType.EDGE_CASE);
expect(edgeCasePatterns.length).toBeLessThan(3); // Should be deduplicated
});
});
describe('framework detection', () => {
it('should detect Jest framework', async () => {
await fs.ensureDir(tempDir);
const testFile = path.join(tempDir, 'jest.test.ts');
await fs.writeFile(testFile, `
describe('Jest Test', () => {
it('uses jest matchers', () => {
expect(value).toBe(expected);
});
});
`);
const patterns = await extractor.extractFromFile(testFile);
expect(patterns.length).toBeGreaterThan(0);
expect(patterns[0].framework).toBe(TestFramework.JEST);
});
it('should detect Cypress framework', async () => {
await fs.ensureDir(tempDir);
const testFile = path.join(tempDir, 'cypress.test.ts');
await fs.writeFile(testFile, `
describe('Cypress Test', () => {
it('uses cypress commands', () => {
cy.visit('/page');
cy.get('.button').click();
});
});
`);
const patterns = await extractor.extractFromFile(testFile);
expect(patterns.length).toBeGreaterThan(0);
expect(patterns[0].framework).toBe(TestFramework.CYPRESS);
});
});
describe('performance', () => {
it('should process 100+ files in < 5 seconds', async () => {
// This test would require creating 100+ test files
// Simplified version:
await fs.ensureDir(tempDir);
const files: string[] = [];
for (let i = 0; i < 10; i++) {
const file = path.join(tempDir, `perf-test-${i}.test.ts`);
await fs.writeFile(file, `
describe('Performance Test ${i}', () => {
it('test ${i}', () => {
expect(func(null)).toBeNull();
});
});
`);
files.push(file);
}
const startTime = Date.now();
const result = await extractor.extractFromFiles(files);
const processingTime = Date.now() - startTime;
// Should be very fast for 10 files
expect(processingTime).toBeLessThan(2000);
expect(result.patterns.length).toBeGreaterThan(0);
}, 10000);
});
describe('pattern confidence', () => {
it('should assign high confidence to clear patterns', async () => {
await fs.ensureDir(tempDir);
const testFile = path.join(tempDir, 'confidence.test.ts');
await fs.writeFile(testFile, `
describe('High Confidence Pattern', () => {
it('should handle null edge case explicitly', () => {
expect(func(null)).toBeNull();
});
});
`);
const patterns = await extractor.extractFromFile(testFile);
const edgeCasePattern = patterns.find(p => p.type === PatternType.EDGE_CASE);
expect(edgeCasePattern).toBeDefined();
expect(edgeCasePattern!.confidence).toBeGreaterThanOrEqual(0.7);
});
});
});