Files
Profa 2e8642030b release: v1.4.3 - Test Suite Stabilization & TypeScript Fixes
🎯 Test Suite Stabilization - 94.2% Pass Rate Achieved!

This release represents a major quality milestone with systematic test
stabilization that increased the unit test pass rate from 71.1% (619/870)
to 94.2% (903/959), exceeding the 90% goal set in GitHub issue #33.

Major Changes:
- Fixed all TypeScript compilation errors blocking release
- Created batched integration test script to prevent OOM crashes
- Documented integration test memory leak root cause (issue #35)
- Enhanced mock infrastructure for better test reliability
- Updated release verification policies in CLAUDE.md

TypeScript Fixes:
- Fixed TestGeneratorAgent AgentCapability structure (4 errors)
- Rewrote Database mock with proper type casting (36 methods)
- Rewrote LearningEngine mock with proper type casting
- Excluded __mocks__ from TypeScript compilation

Test Infrastructure:
- Created scripts/test-integration-batched.sh for safe execution
- Batches 46 integration tests (5 files at a time)
- Prevents workspace crashes in constrained environments
- Updated package.json test:integration to use batched script

Memory Leak Investigation:
- Root cause: MemoryManager setInterval leak (138 timers)
- Missing cleanup in 31 of 46 integration tests
- Database connection pool exhaustion (23 instances)
- Documented in GitHub issue #35 for v1.4.4 fix

Release Verification:
- aqe init: 19 agents, 34 skills, 8 commands ✅
- Databases: memory.db (221KB), patterns.db (155KB) ✅
- MCP interface: 54 tools functional ✅
- Fleet startup: 5 agents running successfully ✅

Files Changed:
- Modified: 40 files (tests, mocks, core agents)
- New: 2 files (batched script, LearningEngine mock)
- Version: 1.4.2 → 1.4.3

Closes #33

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-06 08:10:28 +00:00

604 lines
16 KiB
TypeScript

/**
* Integration Test: Learning Engine Backward Compatibility
*
* Verifies that the refactored LearningEngine maintains backward compatibility:
* 1. Old recordExperience() API still works (with deprecation warning)
* 2. BaseAgent integration remains unchanged
* 3. Existing test suites pass without modification
* 4. Migration path is smooth
*/
// CRITICAL: Unmock Database to use real implementation
jest.unmock('../../src/utils/Database');
import { describe, it, expect, beforeEach, afterEach } from '@jest/globals';
import { LearningEngine } from '../../src/learning/LearningEngine';
import { SwarmMemoryManager } from '../../src/core/memory/SwarmMemoryManager';
import { Database } from '../../src/utils/Database';
import { TaskResult } from '../../src/learning/RewardCalculator';
import { LearningFeedback } from '../../src/learning/types';
import fs from 'fs';
import path from 'path';
describe('LearningEngine Backward Compatibility', () => {
const testDbPath = path.join(process.cwd(), '.test-learning-compat.db');
const memoryDbPath = path.join(process.cwd(), '.test-memory-compat.db');
let database: Database;
let memoryManager: SwarmMemoryManager;
let learningEngine: LearningEngine | null = null;
beforeEach(async () => {
// Clean up test databases
[testDbPath, memoryDbPath].forEach(dbPath => {
if (fs.existsSync(dbPath)) {
fs.unlinkSync(dbPath);
}
});
// Create fresh database and memory manager
database = new Database(testDbPath);
await database.initialize();
memoryManager = new SwarmMemoryManager(memoryDbPath);
await memoryManager.initialize();
});
afterEach(async () => {
// Dispose learning engine to prevent open handles
if (learningEngine) {
learningEngine.dispose();
learningEngine = null;
}
// Close database connections
try {
if (database) await database.close();
} catch (error) {
// Ignore close errors
}
try {
if (memoryManager && (memoryManager as any).db) {
(memoryManager as any).db.close();
}
} catch (error) {
// Ignore close errors
}
// Clean up test databases
[testDbPath, memoryDbPath].forEach(dbPath => {
if (fs.existsSync(dbPath)) {
fs.unlinkSync(dbPath);
}
});
});
describe('recordExperience() Compatibility', () => {
it('should work with old recordExperience() API', async () => {
const agentId = 'compat-agent-old-api';
learningEngine = new LearningEngine(
agentId,
memoryManager,
{ enabled: true },
database
);
await learningEngine.initialize();
// Use OLD API (recordExperience instead of learnFromExecution)
const task = {
type: 'unit-test-generation',
context: {},
requirements: {
capabilities: ['code-analysis', 'test-generation']
}
};
const result: TaskResult = {
success: true,
executionTime: 1500,
errors: [],
coverage: 0.85,
metadata: {
strategy: 'template-based',
toolsUsed: ['ast-parser', 'jest-generator'],
parallelization: 0.5,
retryPolicy: 'exponential',
resourceAllocation: 0.6
}
};
// OLD API - should still work
await learningEngine.recordExperience(task, result);
// Verify data was persisted
const stats = await database.getLearningStatistics(agentId);
expect(stats.totalExperiences).toBeGreaterThan(0);
expect(stats.qTableSize).toBeGreaterThan(0);
});
it('should work with old recordExperience() API with feedback', async () => {
const agentId = 'compat-agent-feedback';
learningEngine = new LearningEngine(
agentId,
memoryManager,
{ enabled: true },
database
);
await learningEngine.initialize();
const task = {
type: 'security-test-generation',
context: {},
requirements: {
capabilities: ['security-testing', 'owasp']
}
};
const result: TaskResult = {
success: true,
executionTime: 3000,
errors: [],
coverage: 0.95,
metadata: {
strategy: 'threat-modeling',
toolsUsed: ['semgrep', 'bandit'],
parallelization: 0.8,
retryPolicy: 'exponential',
resourceAllocation: 0.9
}
};
const feedback: LearningFeedback = {
rating: 0.9,
issues: [],
suggestions: ['Excellent coverage of OWASP Top 10']
};
// OLD API with feedback
await learningEngine.recordExperience(task, result, feedback);
// Verify feedback influenced learning
const stats = await database.getLearningStatistics(agentId);
expect(stats.avgReward).toBeGreaterThan(0);
});
it('should handle multiple calls to old API', async () => {
const agentId = 'compat-agent-multiple';
learningEngine = new LearningEngine(
agentId,
memoryManager,
{ enabled: true },
database
);
await learningEngine.initialize();
// Multiple calls using OLD API
for (let i = 0; i < 10; i++) {
const task = {
type: 'test-generation',
context: {},
requirements: { capabilities: ['testing'] }
};
const result: TaskResult = {
success: i % 2 === 0,
executionTime: 1000 + i * 100,
errors: [],
coverage: 0.7 + i * 0.02,
metadata: {
strategy: i % 2 === 0 ? 'template' : 'property',
toolsUsed: ['jest'],
parallelization: 0.5,
retryPolicy: 'exponential',
resourceAllocation: 0.5
}
};
await learningEngine.recordExperience(task, result);
}
// Verify all experiences were recorded
const stats = await database.getLearningStatistics(agentId);
expect(stats.totalExperiences).toBe(10);
});
});
describe('learnFromExecution() New API', () => {
it('should work with new learnFromExecution() API', async () => {
const agentId = 'compat-agent-new-api';
learningEngine = new LearningEngine(
agentId,
memoryManager,
{ enabled: true },
database
);
await learningEngine.initialize();
// Use NEW API
const task = {
type: 'integration-test-generation',
context: {},
requirements: {
capabilities: ['api-testing']
}
};
const result = {
success: true,
executionTime: 2000,
errors: [],
coverage: 0.90
};
// NEW API - returns LearningOutcome
const outcome = await learningEngine.learnFromExecution(task, result);
expect(outcome).toBeDefined();
expect(outcome.newPerformance).toBeDefined();
expect(outcome.confidence).toBeGreaterThanOrEqual(0);
expect(outcome.patterns).toBeDefined();
// Verify learning occurred (but no database persistence in learnFromExecution)
expect(learningEngine.getTotalExperiences()).toBe(1);
});
it('should support both APIs in same session', async () => {
const agentId = 'compat-agent-mixed';
learningEngine = new LearningEngine(
agentId,
memoryManager,
{ enabled: true },
database
);
await learningEngine.initialize();
const task1 = {
type: 'test-generation',
context: {},
requirements: { capabilities: ['testing'] }
};
const result1: TaskResult = {
success: true,
executionTime: 1000,
errors: [],
coverage: 0.85,
metadata: {
strategy: 'template',
toolsUsed: ['jest'],
parallelization: 0.5,
retryPolicy: 'exponential',
resourceAllocation: 0.5
}
};
// OLD API (with database persistence)
await learningEngine.recordExperience(task1, result1);
const task2 = {
type: 'test-generation',
context: {},
requirements: { capabilities: ['testing'] }
};
const result2 = {
success: true,
executionTime: 1200,
errors: [],
coverage: 0.88
};
// NEW API (in-memory only)
await learningEngine.learnFromExecution(task2, result2);
// Verify both APIs worked
expect(learningEngine.getTotalExperiences()).toBe(2);
// OLD API should have persisted to database
const stats = await database.getLearningStatistics(agentId);
expect(stats.totalExperiences).toBeGreaterThan(0);
});
});
describe('Migration Scenarios', () => {
it('should migrate from old to new API smoothly', async () => {
const agentId = 'compat-agent-migration';
learningEngine = new LearningEngine(
agentId,
memoryManager,
{ enabled: true },
database
);
await learningEngine.initialize();
// Phase 1: Using OLD API (existing codebase)
for (let i = 0; i < 5; i++) {
const task = {
type: 'test-generation',
context: {},
requirements: { capabilities: ['testing'] }
};
const result: TaskResult = {
success: true,
executionTime: 1000,
errors: [],
coverage: 0.85,
metadata: {
strategy: 'template',
toolsUsed: ['jest'],
parallelization: 0.5,
retryPolicy: 'exponential',
resourceAllocation: 0.5
}
};
await learningEngine.recordExperience(task, result);
}
// Verify OLD API worked
let stats = await database.getLearningStatistics(agentId);
expect(stats.totalExperiences).toBe(5);
// Phase 2: Gradual migration to NEW API
for (let i = 0; i < 5; i++) {
const task = {
type: 'test-generation',
context: {},
requirements: { capabilities: ['testing'] }
};
const result = {
success: true,
executionTime: 1000,
errors: [],
coverage: 0.85
};
// NEW API
await learningEngine.learnFromExecution(task, result);
}
// Verify total experiences (5 old + 5 new)
expect(learningEngine.getTotalExperiences()).toBe(10);
// OLD API calls should still be in database
stats = await database.getLearningStatistics(agentId);
expect(stats.totalExperiences).toBe(5); // Only OLD API persists
});
it('should handle disabled learning gracefully', async () => {
const agentId = 'compat-agent-disabled';
learningEngine = new LearningEngine(
agentId,
memoryManager,
{ enabled: false }, // Learning disabled
database
);
await learningEngine.initialize();
const task = {
type: 'test-generation',
context: {},
requirements: { capabilities: ['testing'] }
};
const result: TaskResult = {
success: true,
executionTime: 1000,
errors: [],
coverage: 0.85,
metadata: {
strategy: 'template',
toolsUsed: ['jest'],
parallelization: 0.5,
retryPolicy: 'exponential',
resourceAllocation: 0.5
}
};
// Should not throw even when disabled
await expect(learningEngine.recordExperience(task, result)).resolves.not.toThrow();
const result2 = {
success: true,
executionTime: 1000,
errors: [],
coverage: 0.85
};
await expect(learningEngine.learnFromExecution(task, result2)).resolves.not.toThrow();
// No learning should have occurred
expect(learningEngine.getTotalExperiences()).toBe(0);
});
});
describe('Error Recovery', () => {
it('should handle persistence errors gracefully in old API', async () => {
const agentId = 'compat-agent-error';
learningEngine = new LearningEngine(
agentId,
memoryManager,
{ enabled: true },
database
);
await learningEngine.initialize();
const task = {
type: 'test-generation',
context: {},
requirements: { capabilities: ['testing'] }
};
const result: TaskResult = {
success: true,
executionTime: 1000,
errors: [],
coverage: 0.85,
metadata: {
strategy: 'template',
toolsUsed: ['jest'],
parallelization: 0.5,
retryPolicy: 'exponential',
resourceAllocation: 0.5
}
};
// Record first experience
await learningEngine.recordExperience(task, result);
// Close database to trigger error
await database.close();
// Should not throw - error should be caught internally
await expect(learningEngine.recordExperience(task, result)).resolves.not.toThrow();
// Learning should continue (in-memory)
expect(learningEngine.getTotalExperiences()).toBe(2);
});
it('should recover from database errors and continue learning', async () => {
const agentId = 'compat-agent-recovery';
// Temporarily set invalid database path
const badDbPath = '/invalid/path/db.sqlite';
const badDatabase = new Database(badDbPath);
// This should fail to initialize, but engine should handle it
try {
await badDatabase.initialize();
} catch (error) {
// Expected - database initialization failed
}
// Create engine with bad database
learningEngine = new LearningEngine(
agentId,
memoryManager,
{ enabled: true },
badDatabase
);
// Initialize should not throw
await expect(learningEngine.initialize()).resolves.not.toThrow();
const task = {
type: 'test-generation',
context: {},
requirements: { capabilities: ['testing'] }
};
const result: TaskResult = {
success: true,
executionTime: 1000,
errors: [],
coverage: 0.85,
metadata: {
strategy: 'template',
toolsUsed: ['jest'],
parallelization: 0.5,
retryPolicy: 'exponential',
resourceAllocation: 0.5
}
};
// Should not throw - error should be caught internally
await expect(learningEngine.recordExperience(task, result)).resolves.not.toThrow();
// Learning should continue (in-memory)
expect(learningEngine.getTotalExperiences()).toBeGreaterThan(0);
});
});
describe('Feature Parity', () => {
let engine1: LearningEngine | null = null;
let engine2: LearningEngine | null = null;
afterEach(() => {
if (engine1) {
engine1.dispose();
engine1 = null;
}
if (engine2) {
engine2.dispose();
engine2 = null;
}
});
it('should provide same learning capabilities in both APIs', async () => {
const agentId1 = 'compat-agent-old-features';
const agentId2 = 'compat-agent-new-features';
engine1 = new LearningEngine(agentId1, memoryManager, { enabled: true }, database);
await engine1.initialize();
engine2 = new LearningEngine(agentId2, memoryManager, { enabled: true }, database);
await engine2.initialize();
const task = {
type: 'test-generation',
context: {},
requirements: { capabilities: ['testing'] }
};
const result1: TaskResult = {
success: true,
executionTime: 1000,
errors: [],
coverage: 0.85,
metadata: {
strategy: 'template',
toolsUsed: ['jest'],
parallelization: 0.5,
retryPolicy: 'exponential',
resourceAllocation: 0.5
}
};
const result2 = {
success: true,
executionTime: 1000,
errors: [],
coverage: 0.85
};
// OLD API
await engine1.recordExperience(task, result1);
// NEW API
await engine2.learnFromExecution(task, result2);
// Both should have learned
expect(engine1.getTotalExperiences()).toBe(1);
expect(engine2.getTotalExperiences()).toBe(1);
// Both should support strategy recommendations
const state = {
taskComplexity: 0.5,
requiredCapabilities: ['testing'],
contextFeatures: {},
previousAttempts: 0,
availableResources: 0.8,
timeConstraint: 30000
};
const rec1 = await engine1.recommendStrategy(state);
const rec2 = await engine2.recommendStrategy(state);
expect(rec1).toBeDefined();
expect(rec2).toBeDefined();
expect(rec1.strategy).toBeDefined();
expect(rec2.strategy).toBeDefined();
});
});
});