Files
proffesor-for-testing__agen…/tests/integration/learning-persistence.test.ts
Profa 86ccaa6ee4 fix(critical): Learning system persistence + comprehensive test coverage expansion
CRITICAL LEARNING SYSTEM FIXES (4 issues):
- Fixed Q-learning persistence: Database auto-initialization when not provided
- Fixed Database.initialize() call for auto-created instances
- Removed FK constraint on learning_experiences.task_id (architectural fix)
- Fixed SQL syntax: datetime('now', '-7 days') quotes

NEW FEATURES:
- LearningPersistenceAdapter pattern for flexible storage backends
- Auto-initialize Database in LearningEngine when learning enabled

COMPREHENSIVE TEST EXPANSION (44 MCP handler test files):
- Analysis handlers: +1,637 lines across 5 files
- Chaos handlers: +2,094 lines across 3 files
- Coordination handlers: +5,225 lines across 7 files
- Memory handlers: +6,415 lines across 10 files
- Prediction handlers: +3,440 lines across 5 files
- Test handlers: +1,628 lines across 4 files

NEW TEST SUITES (9 total):
- tests/integration/learning-persistence.test.ts (468 lines, 7 tests)
- tests/integration/learning-backward-compat.test.ts
- tests/unit/learning/LearningEngine.database.test.ts
- tests/unit/learning/LearningPersistenceAdapter.test.ts
- tests/core/memory/SwarmMemoryManager.test.ts
- tests/performance/learning-engine-refactor.perf.test.ts
- 3 test helper scripts for Q-learning verification

DOCUMENTATION:
- Updated CHANGELOG.md with learning fixes + test stats
- Added 16 comprehensive reports in docs/ (learning analysis, Q-learning evidence, fixes)
- Cleaned up 6 obsolete release files from root directory

FILES CHANGED:
- Modified: 48 source/test files
- Added: 25 new files (tests + docs)
- Deleted: 6 obsolete release files
- Total: ~22,000 lines added

IMPACT:
✅ Learning system now persists Q-values correctly
✅ Comprehensive MCP handler test coverage
✅ Zero breaking changes (backward compatible)
✅ Documentation verified: 18 QE agents, 34 QE skills, 8 commands

🤖 Generated with Claude Code
https://claude.com/claude-code

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-03 14:34:53 +00:00

469 lines
14 KiB
TypeScript

/**
* Integration Test: Learning System Database Persistence
*
* Verifies that Q-learning experiences and Q-values are correctly
* persisted to the database and restored across agent restarts.
*
* This test was created to prevent regression of the issue where
* LearningEngine was not receiving a Database instance, causing
* all persistence operations to be silently skipped.
*/
// CRITICAL: Unmock Database to use real implementation for persistence tests
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('Learning System Database Persistence', () => {
// Use SEPARATE database files to avoid schema conflicts
const testDbPath = path.join(process.cwd(), '.test-learning.db');
const memoryDbPath = path.join(process.cwd(), '.test-memory.db');
let database: Database;
let memoryManager: SwarmMemoryManager;
beforeEach(async () => {
// Clean up test databases if they exist
if (fs.existsSync(testDbPath)) {
fs.unlinkSync(testDbPath);
}
if (fs.existsSync(memoryDbPath)) {
fs.unlinkSync(memoryDbPath);
}
// Create fresh database (for Q-learning) and memory manager (for coordination)
database = new Database(testDbPath);
await database.initialize();
memoryManager = new SwarmMemoryManager(memoryDbPath);
await memoryManager.initialize();
});
afterEach(async () => {
// CRITICAL: Close database connections before cleanup to prevent crashes
try {
if (database && typeof database.close === 'function') {
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 database files
if (fs.existsSync(testDbPath)) {
fs.unlinkSync(testDbPath);
}
if (fs.existsSync(memoryDbPath)) {
fs.unlinkSync(memoryDbPath);
}
});
describe('Q-Value Persistence', () => {
it('should persist Q-values to database when explicitly provided', async () => {
const agentId = 'test-agent-explicit-db';
// Create LearningEngine WITH database parameter (old way)
const engine = new LearningEngine(
agentId,
memoryManager,
{ enabled: true },
database // ← Explicitly provided
);
await engine.initialize();
// Create test task and result (NO task_id to avoid FK constraint complexity)
const task = {
// id: undefined, // Don't provide ID to avoid FK constraint to tasks table
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
}
};
// Record experience (should persist to database)
await engine.recordExperience(task, result);
// Verify Q-values were persisted
const qValues = await database.getAllQValues(agentId);
expect(qValues.length).toBeGreaterThan(0);
expect(qValues[0].q_value).toBeDefined();
expect(qValues[0].state_key).toBeDefined();
expect(qValues[0].action_key).toBeDefined();
});
it('should auto-initialize database if not provided (NEW FIX)', async () => {
const agentId = 'test-agent-auto-init';
// Temporarily set AQE_DB_PATH to test database
const originalPath = process.env.AQE_DB_PATH;
process.env.AQE_DB_PATH = testDbPath;
try {
// Create LearningEngine WITHOUT database parameter (new way - should auto-init)
const engine = new LearningEngine(
agentId,
memoryManager,
{ enabled: true }
// ← No 4th parameter - should auto-initialize
);
await engine.initialize();
// Create test task and result
const task = {
id: 'task-002',
type: 'integration-test-generation',
context: {},
requirements: {
capabilities: ['api-testing']
}
};
const result: TaskResult = {
success: true,
executionTime: 2000,
errors: [],
coverage: 0.90,
metadata: {
strategy: 'property-based',
toolsUsed: ['fast-check'],
parallelization: 0.7,
retryPolicy: 'linear',
resourceAllocation: 0.8
}
};
// Record experience (should persist to auto-initialized database)
await engine.recordExperience(task, result);
// Verify Q-values were persisted to auto-initialized database
const qValues = await database.getAllQValues(agentId);
expect(qValues.length).toBeGreaterThan(0);
expect(qValues[0].q_value).toBeGreaterThan(-2); // Valid Q-value range [-2, 2]
expect(qValues[0].q_value).toBeLessThan(2);
} finally {
// Restore environment
if (originalPath) {
process.env.AQE_DB_PATH = originalPath;
} else {
delete process.env.AQE_DB_PATH;
}
}
});
it('should restore Q-values across agent restarts', async () => {
const agentId = 'test-agent-restart';
// Agent 1: Learn from multiple tasks
const engine1 = new LearningEngine(
agentId,
memoryManager,
{ enabled: true },
database
);
await engine1.initialize();
// Execute 5 tasks to build Q-table
for (let i = 0; i < 5; i++) {
const task = {
id: `task-00${i}`,
type: 'unit-test-generation',
context: {},
requirements: { capabilities: ['testing'] }
};
const result: TaskResult = {
success: i % 2 === 0, // Alternate success/failure
executionTime: 1000 + i * 200,
errors: [],
coverage: 0.7 + i * 0.05,
metadata: {
strategy: i % 2 === 0 ? 'template-based' : 'property-based',
toolsUsed: ['jest'],
parallelization: 0.5,
retryPolicy: 'exponential',
resourceAllocation: 0.5
}
};
await engine1.recordExperience(task, result);
}
// Get Q-table size from first agent
const qValues1 = await database.getAllQValues(agentId);
const firstAgentQTableSize = qValues1.length;
expect(firstAgentQTableSize).toBeGreaterThan(0);
// Agent 2: Same ID, should restore previous learning
const engine2 = new LearningEngine(
agentId,
memoryManager,
{ enabled: true },
database
);
await engine2.initialize();
// Verify Q-table was restored
const qValues2 = await database.getAllQValues(agentId);
expect(qValues2.length).toBe(firstAgentQTableSize);
// Get strategy recommendation (should use learned Q-values)
const recommendation = await engine2.recommendStrategy({
taskComplexity: 0.5,
requiredCapabilities: ['testing'],
contextFeatures: {},
previousAttempts: 0,
availableResources: 0.8,
timeConstraint: 30000
});
expect(recommendation.confidence).toBeGreaterThan(0);
expect(recommendation.strategy).toBeDefined();
});
});
describe('Experience Persistence', () => {
it('should store learning experiences in database', async () => {
const agentId = 'test-agent-experiences';
const engine = new LearningEngine(
agentId,
memoryManager,
{ enabled: true },
database
);
await engine.initialize();
// Create task with feedback
const task = {
id: 'task-feedback-001',
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']
};
// Record experience with feedback
await engine.recordExperience(task, result, feedback);
// Verify experience was stored
const stats = await database.getLearningStatistics(agentId);
expect(stats.totalExperiences).toBeGreaterThan(0);
expect(stats.avgReward).toBeGreaterThan(0);
});
it('should handle high volume of experiences efficiently', async () => {
const agentId = 'test-agent-volume';
const engine = new LearningEngine(
agentId,
memoryManager,
{
enabled: true,
batchSize: 10,
updateFrequency: 5
},
database
);
await engine.initialize();
const startTime = Date.now();
// REDUCED: Record only 10 experiences to prevent memory overload in constrained environments
for (let i = 0; i < 10; i++) {
const task = {
id: `volume-task-${i}`,
type: 'test-generation',
context: {},
requirements: { capabilities: ['testing'] }
};
const result: TaskResult = {
success: Math.random() > 0.3,
executionTime: 1000 + Math.random() * 2000,
errors: [],
coverage: 0.6 + Math.random() * 0.4,
metadata: {
strategy: i % 3 === 0 ? 'template' : i % 3 === 1 ? 'property' : 'mutation',
toolsUsed: ['jest'],
parallelization: 0.5,
retryPolicy: 'exponential',
resourceAllocation: 0.5
}
};
await engine.recordExperience(task, result);
}
const duration = Date.now() - startTime;
// Verify all experiences were stored
const stats = await database.getLearningStatistics(agentId);
expect(stats.totalExperiences).toBe(10);
// Performance check: Should complete in under 3 seconds
expect(duration).toBeLessThan(3000);
// Verify Q-table was built
expect(stats.qTableSize).toBeGreaterThan(0);
});
});
describe('Pattern Discovery Persistence', () => {
it('should discover and persist patterns across tasks', async () => {
const agentId = 'test-agent-patterns';
const engine = new LearningEngine(
agentId,
memoryManager,
{ enabled: true },
database
);
await engine.initialize();
// Execute similar tasks to discover pattern
for (let i = 0; i < 10; i++) {
const task = {
id: `pattern-task-${i}`,
type: 'api-test-generation', // Same type
context: {},
requirements: { capabilities: ['api-testing'] }
};
const result: TaskResult = {
success: true, // Always successful
executionTime: 1500,
errors: [],
coverage: 0.88,
metadata: {
strategy: 'contract-based', // Same strategy
toolsUsed: ['pact'],
parallelization: 0.6,
retryPolicy: 'exponential',
resourceAllocation: 0.7
}
};
await engine.recordExperience(task, result);
}
// Get discovered patterns
const patterns = engine.getPatterns();
expect(patterns.length).toBeGreaterThan(0);
// Verify pattern has increasing confidence and high success rate
const topPattern = patterns[0];
// Confidence starts at 0.5 and increases by 0.01 per use: 0.5 + (10 * 0.01) = 0.6
expect(topPattern.confidence).toBeGreaterThanOrEqual(0.59); // Allow floating point margin
expect(topPattern.confidence).toBeLessThanOrEqual(0.61);
expect(topPattern.successRate).toBe(1.0); // All 10 tasks succeeded
expect(topPattern.usageCount).toBe(10);
});
});
describe('Learning Statistics', () => {
it('should provide accurate statistics from database', async () => {
const agentId = 'test-agent-stats';
const engine = new LearningEngine(
agentId,
memoryManager,
{ enabled: true },
database
);
await engine.initialize();
// Record mix of successful and failed experiences
const experiences = [
{ success: true, coverage: 0.85 },
{ success: true, coverage: 0.90 },
{ success: false, coverage: 0.50 },
{ success: true, coverage: 0.88 },
{ success: false, coverage: 0.45 }
];
for (let i = 0; i < experiences.length; i++) {
const exp = experiences[i];
const task = {
id: `stats-task-${i}`,
type: 'test-generation',
context: {},
requirements: { capabilities: ['testing'] }
};
const result: TaskResult = {
success: exp.success,
executionTime: 1500,
errors: exp.success ? [] : [{ message: 'Test failed' }],
coverage: exp.coverage,
metadata: {
strategy: 'template-based',
toolsUsed: ['jest'],
parallelization: 0.5,
retryPolicy: 'exponential',
resourceAllocation: 0.5
}
};
await engine.recordExperience(task, result);
}
// Get statistics from database
const stats = await database.getLearningStatistics(agentId);
expect(stats.totalExperiences).toBe(5);
// Verify average reward is positive (more successes than failures: 3 vs 2)
expect(stats.avgReward).toBeGreaterThan(0);
expect(stats.avgReward).toBeLessThan(2); // Within valid reward range
expect(stats.qTableSize).toBeGreaterThan(0);
});
});
});