mirror of
https://github.com/proffesor-for-testing/agentic-qe.git
synced 2026-09-19 08:45:47 +08:00
b91627f282
Phase 1: Test Determinism - Create SeededRandom utility with Mulberry32 PRNG (src/utils/SeededRandom.ts) - Migrate 258+ Math.random() calls to SeededRandom across 65+ test files - Add timer test utilities (tests/helpers/timerTestUtils.ts) - Add jest.useFakeTimers() pattern to 13 test files Phase 2: Type Safety (63.5% reduction) - Reduce `any` types from ~1,800 to 656 - Add 50+ new interfaces across agents, core, mcp, cli modules - Fix all TypeScript errors (0 errors, build passes) - Replace Record<string, any> with Record<string, unknown> - Add proper type guards for hook methods (PostTaskData, TaskErrorData) Key improvements: - agents/: 105 → 0 any types (100% reduction) - mcp/: 226 → 102 any types (55% reduction) - cli/: 277 → 168 any types (39% reduction) - core/: 199 → 175 any types (12% reduction) Closes #149 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
576 lines
18 KiB
TypeScript
576 lines
18 KiB
TypeScript
/**
|
|
* AgentDB End-to-End Agent Execution Integration Tests
|
|
*
|
|
* Tests complete agent workflow with AgentDB integration:
|
|
* - Agent executes real task
|
|
* - Patterns stored in actual database
|
|
* - Embeddings generated with real neural network
|
|
* - QUIC sync occurs across network
|
|
* - Neural training completed with RL algorithm
|
|
*
|
|
* This verifies the ENTIRE stack works, not just individual components.
|
|
*/
|
|
|
|
import { describe, it, expect, beforeAll, afterAll } from '@jest/globals';
|
|
import { TestGeneratorAgent } from '@agents/TestGeneratorAgent';
|
|
import { CoverageAnalyzerAgent } from '@agents/CoverageAnalyzerAgent';
|
|
import { AgentDBManager, AgentDBConfig } from '@core/memory/AgentDBManager';
|
|
import { EventBus } from '@core/EventBus';
|
|
import { MemoryManager } from '@core/MemoryManager';
|
|
import { AgentId, TaskAssignment } from '@typessrc/types';
|
|
import Database from 'better-sqlite3';
|
|
import * as fs from 'fs';
|
|
import * as path from 'path';
|
|
import { createSeededRandom } from '../../../src/utils/SeededRandom';
|
|
|
|
describe('AgentDB End-to-End Agent Execution', () => {
|
|
let agentDB1: AgentDBManager;
|
|
let agentDB2: AgentDBManager;
|
|
let testAgent: TestGeneratorAgent;
|
|
let coverageAgent: CoverageAnalyzerAgent;
|
|
let eventBus: EventBus;
|
|
let memoryManager: MemoryManager;
|
|
|
|
const TEST_DATA_DIR = path.join(__dirname, '../../fixtures/agentdb');
|
|
const DB_PATH_1 = path.join(TEST_DATA_DIR, 'e2e-agent-1.db');
|
|
const DB_PATH_2 = path.join(TEST_DATA_DIR, 'e2e-agent-2.db');
|
|
const BASE_PORT = 24433;
|
|
|
|
beforeAll(async () => {
|
|
if (!fs.existsSync(TEST_DATA_DIR)) {
|
|
fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
|
|
}
|
|
|
|
// Clean up old databases
|
|
[DB_PATH_1, DB_PATH_2].forEach(dbPath => {
|
|
if (fs.existsSync(dbPath)) {
|
|
fs.unlinkSync(dbPath);
|
|
}
|
|
});
|
|
|
|
// Initialize shared services
|
|
eventBus = new EventBus();
|
|
memoryManager = new MemoryManager();
|
|
|
|
// Initialize two AgentDB instances for QUIC sync testing
|
|
const config1: AgentDBConfig = {
|
|
dbPath: DB_PATH_1,
|
|
enableQUICSync: true,
|
|
syncPort: BASE_PORT,
|
|
syncPeers: [`localhost:${BASE_PORT + 1}`],
|
|
enableLearning: true,
|
|
enableReasoning: true,
|
|
cacheSize: 500,
|
|
quantizationType: 'scalar',
|
|
syncInterval: 200
|
|
};
|
|
|
|
const config2: AgentDBConfig = {
|
|
dbPath: DB_PATH_2,
|
|
enableQUICSync: true,
|
|
syncPort: BASE_PORT + 1,
|
|
syncPeers: [`localhost:${BASE_PORT}`],
|
|
enableLearning: true,
|
|
enableReasoning: true,
|
|
cacheSize: 500,
|
|
quantizationType: 'scalar',
|
|
syncInterval: 200
|
|
};
|
|
|
|
agentDB1 = new AgentDBManager(config1);
|
|
agentDB2 = new AgentDBManager(config2);
|
|
|
|
await agentDB1.initialize();
|
|
await agentDB2.initialize();
|
|
|
|
// Wait for QUIC connection
|
|
await new Promise(resolve => setTimeout(resolve, 500));
|
|
|
|
// Initialize agents
|
|
const testAgentId: AgentId = {
|
|
type: 'qe-test-generator',
|
|
instanceId: 'e2e-test-001'
|
|
};
|
|
|
|
const coverageAgentId: AgentId = {
|
|
type: 'qe-coverage-analyzer',
|
|
instanceId: 'e2e-coverage-001'
|
|
};
|
|
|
|
testAgent = new TestGeneratorAgent(testAgentId, eventBus, memoryManager, {
|
|
agentDBManager: agentDB1
|
|
});
|
|
|
|
coverageAgent = new CoverageAnalyzerAgent(coverageAgentId, eventBus, memoryManager, {
|
|
agentDBManager: agentDB2
|
|
});
|
|
|
|
await testAgent.initialize();
|
|
await coverageAgent.initialize();
|
|
}, 30000);
|
|
|
|
afterAll(async () => {
|
|
if (testAgent) await testAgent.shutdown();
|
|
if (coverageAgent) await coverageAgent.shutdown();
|
|
if (agentDB1) await agentDB1.shutdown();
|
|
if (agentDB2) await agentDB2.shutdown();
|
|
|
|
// Clean up databases
|
|
[DB_PATH_1, DB_PATH_2].forEach(dbPath => {
|
|
if (fs.existsSync(dbPath)) {
|
|
fs.unlinkSync(dbPath);
|
|
}
|
|
});
|
|
});
|
|
|
|
describe('Complete Agent Execution Workflow', () => {
|
|
it('should execute task and write actual data to database', async () => {
|
|
const task: TaskAssignment = {
|
|
id: 'e2e-task-001',
|
|
task: {
|
|
id: 'task-001',
|
|
description: 'Generate comprehensive test suite for UserService',
|
|
priority: 'high',
|
|
metadata: {
|
|
sourceFile: 'src/services/UserService.ts',
|
|
framework: 'jest',
|
|
testTypes: ['unit', 'integration']
|
|
}
|
|
},
|
|
assignedAt: new Date(),
|
|
status: 'assigned'
|
|
};
|
|
|
|
// Execute task
|
|
const result = await testAgent.execute(task);
|
|
|
|
expect(result.success).toBe(true);
|
|
expect(result.testsGenerated).toBeGreaterThan(0);
|
|
|
|
// VERIFY actual database writes (not just JSON flags)
|
|
const db = new Database(DB_PATH_1, { readonly: true });
|
|
|
|
// Check patterns table
|
|
const patternCount = db.prepare('SELECT COUNT(*) as c FROM patterns').get() as any;
|
|
expect(patternCount.c).toBeGreaterThan(0);
|
|
|
|
// Check pattern has embedding
|
|
const pattern = db.prepare('SELECT * FROM patterns LIMIT 1').get() as any;
|
|
expect(pattern).toBeDefined();
|
|
expect(pattern.id).toBeDefined();
|
|
expect(pattern.pattern_data).toBeDefined();
|
|
|
|
const patternData = JSON.parse(pattern.pattern_data);
|
|
expect(patternData.embedding).toBeDefined();
|
|
expect(Array.isArray(patternData.embedding)).toBe(true);
|
|
expect(patternData.embedding.length).toBe(384); // MiniLM embedding dimension
|
|
|
|
db.close();
|
|
}, 60000);
|
|
|
|
it('should generate embeddings using real neural network', async () => {
|
|
const task: TaskAssignment = {
|
|
id: 'e2e-task-002',
|
|
task: {
|
|
id: 'task-002',
|
|
description: 'Generate edge case tests',
|
|
priority: 'medium',
|
|
metadata: {
|
|
sourceFile: 'src/utils/validation.ts',
|
|
framework: 'jest'
|
|
}
|
|
},
|
|
assignedAt: new Date(),
|
|
status: 'assigned'
|
|
};
|
|
|
|
await testAgent.execute(task);
|
|
|
|
// Verify embeddings in database
|
|
const db = new Database(DB_PATH_1, { readonly: true });
|
|
|
|
const patterns = db.prepare('SELECT * FROM patterns ORDER BY created_at DESC LIMIT 5').all() as any[];
|
|
|
|
expect(patterns.length).toBeGreaterThan(0);
|
|
|
|
// Check each pattern has valid embedding
|
|
patterns.forEach(pattern => {
|
|
const data = JSON.parse(pattern.pattern_data);
|
|
|
|
expect(data.embedding).toBeDefined();
|
|
expect(data.embedding).toHaveLength(384);
|
|
|
|
// Embeddings should be normalized (values between -1 and 1)
|
|
const allValid = data.embedding.every((v: number) => v >= -1 && v <= 1);
|
|
expect(allValid).toBe(true);
|
|
|
|
// Embeddings should not be all zeros
|
|
const hasNonZero = data.embedding.some((v: number) => Math.abs(v) > 0.01);
|
|
expect(hasNonZero).toBe(true);
|
|
});
|
|
|
|
db.close();
|
|
}, 60000);
|
|
|
|
it('should sync patterns to peer via QUIC', async () => {
|
|
const task: TaskAssignment = {
|
|
id: 'e2e-task-003',
|
|
task: {
|
|
id: 'task-003',
|
|
description: 'Generate performance tests',
|
|
priority: 'high',
|
|
metadata: {
|
|
sourceFile: 'src/api/endpoints.ts',
|
|
framework: 'jest',
|
|
testTypes: ['performance']
|
|
}
|
|
},
|
|
assignedAt: new Date(),
|
|
status: 'assigned'
|
|
};
|
|
|
|
// Execute on agent with DB1
|
|
await testAgent.execute(task);
|
|
|
|
// Get pattern count in DB1
|
|
const db1 = new Database(DB_PATH_1, { readonly: true });
|
|
const count1Before = db1.prepare('SELECT COUNT(*) as c FROM patterns').get() as any;
|
|
db1.close();
|
|
|
|
// Wait for QUIC sync
|
|
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
|
|
// Check pattern synced to DB2
|
|
const db2 = new Database(DB_PATH_2, { readonly: true });
|
|
const count2After = db2.prepare('SELECT COUNT(*) as c FROM patterns').get() as any;
|
|
|
|
expect(count2After.c).toBeGreaterThan(0);
|
|
|
|
// Verify at least one pattern synced
|
|
const syncedPattern = db2.prepare('SELECT * FROM patterns LIMIT 1').get() as any;
|
|
expect(syncedPattern).toBeDefined();
|
|
expect(syncedPattern.type).toBe('test-generation');
|
|
|
|
db2.close();
|
|
}, 90000);
|
|
|
|
it('should complete neural training with RL algorithm', async () => {
|
|
const task: TaskAssignment = {
|
|
id: 'e2e-task-004',
|
|
task: {
|
|
id: 'task-004',
|
|
description: 'Generate security tests',
|
|
priority: 'critical',
|
|
metadata: {
|
|
sourceFile: 'src/auth/authenticate.ts',
|
|
framework: 'jest',
|
|
testTypes: ['security', 'unit']
|
|
}
|
|
},
|
|
assignedAt: new Date(),
|
|
status: 'assigned'
|
|
};
|
|
|
|
// Execute task
|
|
const result = await testAgent.execute(task);
|
|
|
|
// Get training metrics from AgentDB
|
|
const trainingMetrics = await agentDB1.getTrainingMetrics();
|
|
|
|
expect(trainingMetrics).toBeDefined();
|
|
expect(trainingMetrics.algorithm).toBeDefined();
|
|
expect(trainingMetrics.episodeCount).toBeGreaterThan(0);
|
|
expect(trainingMetrics.totalReward).toBeDefined();
|
|
|
|
// Verify model was updated
|
|
const modelInfo = await agentDB1.getModelInfo();
|
|
expect(modelInfo.lastTrainingTime).toBeDefined();
|
|
expect(modelInfo.totalTrainingSteps).toBeGreaterThan(0);
|
|
}, 60000);
|
|
|
|
it('should store training experiences in database', async () => {
|
|
const db = new Database(DB_PATH_1, { readonly: true });
|
|
|
|
// Check for training experiences table
|
|
const tables = db.prepare("SELECT name FROM sqlite_master WHERE type='table'").all();
|
|
const tableNames = tables.map((t: any) => t.name);
|
|
|
|
expect(tableNames).toContain('training_experiences');
|
|
|
|
// Check experiences were stored
|
|
const expCount = db.prepare('SELECT COUNT(*) as c FROM training_experiences').get() as any;
|
|
expect(expCount.c).toBeGreaterThan(0);
|
|
|
|
db.close();
|
|
});
|
|
});
|
|
|
|
describe('Cross-Agent Pattern Sharing', () => {
|
|
it('should share learned patterns between agents', async () => {
|
|
// Test agent learns pattern
|
|
const testTask: TaskAssignment = {
|
|
id: 'share-task-001',
|
|
task: {
|
|
id: 'task-share-001',
|
|
description: 'Generate API tests',
|
|
priority: 'medium',
|
|
metadata: {
|
|
sourceFile: 'src/api/users.ts',
|
|
framework: 'jest'
|
|
}
|
|
},
|
|
assignedAt: new Date(),
|
|
status: 'assigned'
|
|
};
|
|
|
|
await testAgent.execute(testTask);
|
|
|
|
// Wait for sync
|
|
await new Promise(resolve => setTimeout(resolve, 1500));
|
|
|
|
// Coverage agent should have access to shared patterns
|
|
const patterns = await agentDB2.retrievePatterns('API test generation', { k: 5 });
|
|
|
|
expect(patterns.memories.length).toBeGreaterThan(0);
|
|
expect(patterns.memories[0].type).toBe('test-generation');
|
|
expect(patterns.memories[0].confidence).toBeGreaterThan(0);
|
|
}, 90000);
|
|
|
|
it('should use shared patterns to improve quality', async () => {
|
|
const coverageTask: TaskAssignment = {
|
|
id: 'coverage-task-001',
|
|
task: {
|
|
id: 'task-coverage-001',
|
|
description: 'Analyze coverage gaps',
|
|
priority: 'high',
|
|
metadata: {
|
|
testSuite: 'src/tests/api.test.ts',
|
|
framework: 'jest'
|
|
}
|
|
},
|
|
assignedAt: new Date(),
|
|
status: 'assigned'
|
|
};
|
|
|
|
const result = await coverageAgent.execute(coverageTask);
|
|
|
|
expect(result.success).toBe(true);
|
|
expect(result.gapsIdentified).toBeDefined();
|
|
|
|
// Verify it used patterns from shared database
|
|
const usedPatterns = await agentDB2.retrievePatterns('coverage analysis', { k: 3 });
|
|
expect(usedPatterns.memories.length).toBeGreaterThan(0);
|
|
}, 60000);
|
|
});
|
|
|
|
describe('Performance Verification', () => {
|
|
it('should demonstrate "150x faster" vector search', async () => {
|
|
// Seeded RNG for deterministic test results
|
|
const rng = createSeededRandom(17005);
|
|
|
|
// Store many patterns for benchmark
|
|
const patterns = Array.from({ length: 100 }, (_, i) => ({
|
|
id: `perf-pattern-${i}`,
|
|
type: 'test-pattern',
|
|
domain: 'performance',
|
|
pattern_data: JSON.stringify({
|
|
text: `Performance test pattern ${i}`,
|
|
embedding: Array.from({ length: 384 }, () => rng.random() * 2 - 1)
|
|
}),
|
|
confidence: 0.9,
|
|
usage_count: 0,
|
|
success_count: 0,
|
|
created_at: Date.now(),
|
|
last_used: Date.now()
|
|
}));
|
|
|
|
await agentDB1.storeBatch(patterns);
|
|
|
|
// Measure search time
|
|
const measurements: number[] = [];
|
|
|
|
for (let i = 0; i < 50; i++) {
|
|
const start = performance.now();
|
|
await agentDB1.retrievePatterns('performance test', { k: 10 });
|
|
const duration = performance.now() - start;
|
|
measurements.push(duration);
|
|
}
|
|
|
|
const avgTime = measurements.reduce((a, b) => a + b, 0) / measurements.length;
|
|
|
|
console.log(`Vector search performance (100 vectors):
|
|
Average: ${avgTime.toFixed(3)}ms
|
|
Target: <0.1ms (100µs)`);
|
|
|
|
expect(avgTime).toBeLessThan(1); // Should be very fast
|
|
}, 60000);
|
|
|
|
it('should demonstrate "84% faster" QUIC sync latency', async () => {
|
|
// Seeded RNG for deterministic test results
|
|
const rng = createSeededRandom(17006);
|
|
|
|
const pattern = {
|
|
id: 'latency-test',
|
|
type: 'test',
|
|
domain: 'latency',
|
|
pattern_data: JSON.stringify({
|
|
text: 'Latency test pattern',
|
|
embedding: Array.from({ length: 384 }, () => rng.random())
|
|
}),
|
|
confidence: 0.95,
|
|
usage_count: 0,
|
|
success_count: 0,
|
|
created_at: Date.now(),
|
|
last_used: Date.now()
|
|
};
|
|
|
|
// Measure sync time
|
|
const start = performance.now();
|
|
await agentDB1.storePattern(pattern);
|
|
|
|
// Poll DB2 until pattern appears
|
|
let synced = false;
|
|
let attempts = 0;
|
|
while (!synced && attempts < 100) {
|
|
const result = await agentDB2.retrievePatterns('latency-test', { k: 1 });
|
|
if (result.memories.length > 0 && result.memories[0].id === 'latency-test') {
|
|
synced = true;
|
|
} else {
|
|
await new Promise(resolve => setTimeout(resolve, 10));
|
|
attempts++;
|
|
}
|
|
}
|
|
|
|
const syncLatency = performance.now() - start;
|
|
|
|
console.log(`QUIC sync latency: ${syncLatency.toFixed(2)}ms
|
|
Target: <1ms
|
|
Synced: ${synced}`);
|
|
|
|
expect(synced).toBe(true);
|
|
expect(syncLatency).toBeLessThan(2000); // Allow for network variability
|
|
}, 60000);
|
|
|
|
it('should demonstrate memory reduction with quantization', async () => {
|
|
// Seeded RNG for deterministic test results
|
|
const rng = createSeededRandom(17007);
|
|
|
|
const statsBeforeQuant = await agentDB1.getMemoryUsage();
|
|
|
|
// Store large dataset
|
|
const largeDataset = Array.from({ length: 200 }, (_, i) => ({
|
|
id: `quant-pattern-${i}`,
|
|
type: 'test',
|
|
domain: 'quantization',
|
|
pattern_data: JSON.stringify({
|
|
text: `Quantization test ${i}`,
|
|
embedding: Array.from({ length: 384 }, () => rng.random())
|
|
}),
|
|
confidence: 0.9,
|
|
usage_count: 0,
|
|
success_count: 0,
|
|
created_at: Date.now(),
|
|
last_used: Date.now()
|
|
}));
|
|
|
|
await agentDB1.storeBatch(largeDataset);
|
|
|
|
const statsAfterQuant = await agentDB1.getMemoryUsage();
|
|
|
|
console.log(`Memory usage:
|
|
Before: ${statsBeforeQuant.totalBytes} bytes
|
|
After: ${statsAfterQuant.totalBytes} bytes
|
|
Patterns: 200
|
|
Bytes per pattern: ${(statsAfterQuant.totalBytes - statsBeforeQuant.totalBytes) / 200}`);
|
|
|
|
// With scalar quantization, should use less memory than unquantized
|
|
const bytesPerPattern = (statsAfterQuant.totalBytes - statsBeforeQuant.totalBytes) / 200;
|
|
expect(bytesPerPattern).toBeLessThan(2000); // Each pattern should be reasonably sized
|
|
}, 60000);
|
|
});
|
|
|
|
describe('Error Handling and Recovery', () => {
|
|
it('should handle database errors gracefully', async () => {
|
|
// Simulate database error by shutting down
|
|
await agentDB1.shutdown();
|
|
|
|
const task: TaskAssignment = {
|
|
id: 'error-task-001',
|
|
task: {
|
|
id: 'task-error-001',
|
|
description: 'Test with DB offline',
|
|
priority: 'low',
|
|
metadata: {}
|
|
},
|
|
assignedAt: new Date(),
|
|
status: 'assigned'
|
|
};
|
|
|
|
// Should handle gracefully
|
|
const result = await testAgent.execute(task);
|
|
|
|
// Agent should still complete task even if AgentDB is down
|
|
expect(result).toBeDefined();
|
|
|
|
// Restart DB
|
|
agentDB1 = new AgentDBManager({
|
|
dbPath: DB_PATH_1,
|
|
enableQUICSync: true,
|
|
syncPort: BASE_PORT,
|
|
syncPeers: [`localhost:${BASE_PORT + 1}`],
|
|
enableLearning: true,
|
|
enableReasoning: true,
|
|
cacheSize: 500,
|
|
quantizationType: 'scalar'
|
|
});
|
|
|
|
await agentDB1.initialize();
|
|
}, 60000);
|
|
|
|
it('should recover from QUIC connection failures', async () => {
|
|
// Disconnect peer
|
|
await agentDB2.shutdown();
|
|
|
|
const task: TaskAssignment = {
|
|
id: 'quic-error-task',
|
|
task: {
|
|
id: 'task-quic-error',
|
|
description: 'Test with peer offline',
|
|
priority: 'medium',
|
|
metadata: {
|
|
sourceFile: 'test.ts'
|
|
}
|
|
},
|
|
assignedAt: new Date(),
|
|
status: 'assigned'
|
|
};
|
|
|
|
// Execute task (sync will fail but queued)
|
|
await testAgent.execute(task);
|
|
|
|
const syncStats = await agentDB1.getSyncStats();
|
|
expect(syncStats.queuedPatterns).toBeGreaterThan(0);
|
|
|
|
// Restart peer
|
|
agentDB2 = new AgentDBManager({
|
|
dbPath: DB_PATH_2,
|
|
enableQUICSync: true,
|
|
syncPort: BASE_PORT + 1,
|
|
syncPeers: [`localhost:${BASE_PORT}`],
|
|
enableLearning: true,
|
|
enableReasoning: true,
|
|
cacheSize: 500,
|
|
quantizationType: 'scalar'
|
|
});
|
|
|
|
await agentDB2.initialize();
|
|
|
|
// Wait for sync recovery
|
|
await new Promise(resolve => setTimeout(resolve, 2000));
|
|
|
|
const updatedStats = await agentDB1.getSyncStats();
|
|
expect(updatedStats.queuedPatterns).toBeLessThan(syncStats.queuedPatterns);
|
|
}, 90000);
|
|
});
|
|
});
|