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>
433 lines
13 KiB
TypeScript
433 lines
13 KiB
TypeScript
/**
|
|
* AgentDB Integration Tests
|
|
*
|
|
* Tests for AgentDB with QUIC sync, vector search, and learning plugins.
|
|
* Includes performance benchmarks to verify 150x speedup claim.
|
|
*
|
|
* @version 1.0.0
|
|
* @author Agentic QE Team
|
|
*/
|
|
|
|
import { describe, it, expect, beforeAll, afterAll, beforeEach } from '@jest/globals';
|
|
import { EnhancedAgentDBService, LearningExperience, RLAlgorithm } from '@core/memory/EnhancedAgentDBService';
|
|
import { QEPattern } from '@core/memory/AgentDBService';
|
|
import * as fs from 'fs';
|
|
import * as path from 'path';
|
|
import { createSeededRandom } from '../../src/utils/SeededRandom';
|
|
|
|
describe('AgentDB Integration', () => {
|
|
const testDbPath = path.join(__dirname, '../fixtures/test-agentdb.db');
|
|
let agentDB: EnhancedAgentDBService;
|
|
|
|
beforeAll(async () => {
|
|
// Ensure test directory exists
|
|
const dir = path.dirname(testDbPath);
|
|
if (!fs.existsSync(dir)) {
|
|
fs.mkdirSync(dir, { recursive: true });
|
|
}
|
|
});
|
|
|
|
beforeEach(async () => {
|
|
// Clean up existing database
|
|
if (fs.existsSync(testDbPath)) {
|
|
fs.unlinkSync(testDbPath);
|
|
}
|
|
|
|
// Initialize AgentDB with all features
|
|
agentDB = new EnhancedAgentDBService({
|
|
dbPath: testDbPath,
|
|
embeddingDim: 384,
|
|
enableHNSW: true,
|
|
enableCache: true,
|
|
enableQuantization: true,
|
|
quantizationBits: 8,
|
|
enableQuic: true,
|
|
enableLearning: true,
|
|
learningPlugins: [
|
|
{ algorithm: 'q-learning', learningRate: 0.1, discountFactor: 0.99 },
|
|
{ algorithm: 'sarsa', learningRate: 0.1, discountFactor: 0.99 }
|
|
]
|
|
});
|
|
|
|
await agentDB.initialize();
|
|
});
|
|
|
|
afterAll(async () => {
|
|
// Clean up test database
|
|
if (fs.existsSync(testDbPath)) {
|
|
fs.unlinkSync(testDbPath);
|
|
}
|
|
});
|
|
|
|
describe('Initialization', () => {
|
|
it('should initialize with all features enabled', async () => {
|
|
expect(agentDB).toBeDefined();
|
|
});
|
|
|
|
it('should support QUIC synchronization', async () => {
|
|
const pattern = createTestPattern('test-1', 'test-gen', 'test-planning');
|
|
const embedding = generateRandomEmbedding(384);
|
|
|
|
const startTime = Date.now();
|
|
const id = await agentDB.storePatternWithSync(pattern, embedding);
|
|
const latency = Date.now() - startTime;
|
|
|
|
expect(id).toBeDefined();
|
|
// QUIC should keep latency low (target: <1ms, but allow up to 100ms for testing)
|
|
expect(latency).toBeLessThan(100);
|
|
});
|
|
|
|
it('should support learning plugins', async () => {
|
|
const experience: LearningExperience = {
|
|
state: { coverage: 75, complexity: 5 },
|
|
action: 'generate-edge-cases',
|
|
reward: 10,
|
|
nextState: { coverage: 85, complexity: 5 },
|
|
done: false
|
|
};
|
|
|
|
await expect(
|
|
agentDB.trainLearningPlugin('agent-1', experience, 'q-learning')
|
|
).resolves.not.toThrow();
|
|
});
|
|
});
|
|
|
|
describe('Vector Search Performance', () => {
|
|
it('should achieve fast vector search with HNSW', async () => {
|
|
// Store 1000 patterns
|
|
const patterns: QEPattern[] = [];
|
|
const embeddings: number[][] = [];
|
|
|
|
for (let i = 0; i < 1000; i++) {
|
|
patterns.push(createTestPattern(`pattern-${i}`, 'test-gen', 'test-planning'));
|
|
embeddings.push(generateRandomEmbedding(384));
|
|
}
|
|
|
|
await agentDB.storeBatch(patterns, embeddings);
|
|
|
|
// Benchmark HNSW search
|
|
const queryEmbedding = generateRandomEmbedding(384);
|
|
const startHNSW = Date.now();
|
|
const resultsHNSW = await agentDB.searchSimilar(queryEmbedding, { k: 10 });
|
|
const durationHNSW = Date.now() - startHNSW;
|
|
|
|
expect(resultsHNSW).toBeDefined();
|
|
expect(resultsHNSW.length).toBeGreaterThan(0);
|
|
|
|
// HNSW search should be fast (<100ms for 1000 vectors)
|
|
expect(durationHNSW).toBeLessThan(100);
|
|
|
|
console.log(`HNSW search completed in ${durationHNSW}ms for 1000 vectors`);
|
|
}, 30000);
|
|
|
|
it('should demonstrate speedup over linear search', async () => {
|
|
// Store patterns
|
|
const count = 500;
|
|
const patterns: QEPattern[] = [];
|
|
const embeddings: number[][] = [];
|
|
|
|
for (let i = 0; i < count; i++) {
|
|
patterns.push(createTestPattern(`pattern-${i}`, 'test-gen', 'test-planning'));
|
|
embeddings.push(generateRandomEmbedding(384));
|
|
}
|
|
|
|
await agentDB.storeBatch(patterns, embeddings);
|
|
|
|
// Benchmark HNSW search
|
|
const queryEmbedding = generateRandomEmbedding(384);
|
|
const startHNSW = Date.now();
|
|
await agentDB.searchSimilar(queryEmbedding, { k: 10 });
|
|
const durationHNSW = Date.now() - startHNSW;
|
|
|
|
// Benchmark linear search
|
|
const startLinear = Date.now();
|
|
const linearResults = await linearVectorSearch(queryEmbedding, embeddings, 10);
|
|
const durationLinear = Date.now() - startLinear;
|
|
|
|
const speedup = durationLinear / Math.max(durationHNSW, 1);
|
|
|
|
console.log(`HNSW: ${durationHNSW}ms vs Linear: ${durationLinear}ms = ${speedup.toFixed(1)}x faster`);
|
|
|
|
// Verify speedup (should be significantly faster, target 150x but allow lower for small dataset)
|
|
expect(speedup).toBeGreaterThan(1);
|
|
}, 30000);
|
|
});
|
|
|
|
describe('Learning Plugins', () => {
|
|
it('should train Q-Learning model', async () => {
|
|
const experiences: LearningExperience[] = [
|
|
{
|
|
state: { coverage: 70 },
|
|
action: 'generate-tests',
|
|
reward: 5,
|
|
nextState: { coverage: 75 },
|
|
done: false
|
|
},
|
|
{
|
|
state: { coverage: 75 },
|
|
action: 'generate-edge-cases',
|
|
reward: 10,
|
|
nextState: { coverage: 85 },
|
|
done: false
|
|
},
|
|
{
|
|
state: { coverage: 85 },
|
|
action: 'optimize-coverage',
|
|
reward: 15,
|
|
nextState: { coverage: 95 },
|
|
done: true
|
|
}
|
|
];
|
|
|
|
for (const exp of experiences) {
|
|
await agentDB.trainLearningPlugin('agent-1', exp, 'q-learning');
|
|
}
|
|
|
|
const stats = await agentDB.getLearningStats('agent-1');
|
|
expect(stats.totalExperiences).toBe(3);
|
|
expect(stats.avgReward).toBeGreaterThan(0);
|
|
});
|
|
|
|
it('should provide learning recommendations', async () => {
|
|
// Train with some experiences
|
|
const experiences: LearningExperience[] = [
|
|
{
|
|
state: { coverage: 70 },
|
|
action: 'generate-tests',
|
|
reward: 10,
|
|
nextState: { coverage: 80 },
|
|
done: false
|
|
},
|
|
{
|
|
state: { coverage: 70 },
|
|
action: 'skip',
|
|
reward: -5,
|
|
nextState: { coverage: 70 },
|
|
done: false
|
|
}
|
|
];
|
|
|
|
await agentDB.batchTrain('agent-1', experiences, 'q-learning');
|
|
|
|
// Get recommendation
|
|
const recommendation = await agentDB.getLearningRecommendations(
|
|
'agent-1',
|
|
{ coverage: 70 },
|
|
'q-learning'
|
|
);
|
|
|
|
expect(recommendation).toBeDefined();
|
|
expect(recommendation.action).toBeDefined();
|
|
expect(recommendation.confidence).toBeGreaterThanOrEqual(0);
|
|
expect(recommendation.confidence).toBeLessThanOrEqual(1);
|
|
});
|
|
|
|
it('should support multiple learning algorithms', async () => {
|
|
const algorithms: RLAlgorithm[] = ['q-learning', 'sarsa'];
|
|
|
|
for (const algorithm of algorithms) {
|
|
const experience: LearningExperience = {
|
|
state: { test: 'state' },
|
|
action: 'test-action',
|
|
reward: 5,
|
|
nextState: { test: 'next-state' },
|
|
done: false
|
|
};
|
|
|
|
await expect(
|
|
agentDB.trainLearningPlugin('agent-1', experience, algorithm)
|
|
).resolves.not.toThrow();
|
|
}
|
|
});
|
|
|
|
it('should maintain experience replay buffer', async () => {
|
|
const experiences: LearningExperience[] = [];
|
|
|
|
for (let i = 0; i < 10; i++) {
|
|
const exp: LearningExperience = {
|
|
state: { step: i },
|
|
action: `action-${i}`,
|
|
reward: i * 2,
|
|
nextState: { step: i + 1 },
|
|
done: false
|
|
};
|
|
|
|
experiences.push(exp);
|
|
await agentDB.trainLearningPlugin('agent-1', exp, 'q-learning');
|
|
}
|
|
|
|
const replay = agentDB.getExperienceReplay('agent-1', 5);
|
|
expect(replay.length).toBe(5);
|
|
expect(replay[0].state.step).toBe(5); // Last 5 experiences
|
|
});
|
|
|
|
it('should calculate learning statistics', async () => {
|
|
const experiences: LearningExperience[] = [
|
|
{ state: {}, action: 'a1', reward: 10, nextState: {}, done: false },
|
|
{ state: {}, action: 'a2', reward: -5, nextState: {}, done: false },
|
|
{ state: {}, action: 'a3', reward: 15, nextState: {}, done: true }
|
|
];
|
|
|
|
await agentDB.batchTrain('agent-1', experiences, 'q-learning');
|
|
|
|
const stats = await agentDB.getLearningStats('agent-1');
|
|
|
|
expect(stats.totalExperiences).toBe(3);
|
|
expect(stats.avgReward).toBeCloseTo((10 - 5 + 15) / 3, 1);
|
|
expect(stats.successRate).toBeCloseTo(2 / 3, 1); // 2 positive rewards
|
|
expect(stats.modelsActive).toBeGreaterThan(0);
|
|
});
|
|
});
|
|
|
|
describe('Pattern Storage and Retrieval', () => {
|
|
it('should store and retrieve patterns', async () => {
|
|
const pattern = createTestPattern('test-1', 'test-gen', 'test-planning');
|
|
const embedding = generateRandomEmbedding(384);
|
|
|
|
const id = await agentDB.storePattern(pattern, embedding);
|
|
const retrieved = await agentDB.retrievePattern(id);
|
|
|
|
expect(retrieved).toBeDefined();
|
|
expect(retrieved!.id).toBe(pattern.id);
|
|
expect(retrieved!.type).toBe(pattern.type);
|
|
expect(retrieved!.domain).toBe(pattern.domain);
|
|
});
|
|
|
|
it('should support batch storage', async () => {
|
|
const patterns: QEPattern[] = [];
|
|
const embeddings: number[][] = [];
|
|
|
|
for (let i = 0; i < 100; i++) {
|
|
patterns.push(createTestPattern(`batch-${i}`, 'test-gen', 'test-planning'));
|
|
embeddings.push(generateRandomEmbedding(384));
|
|
}
|
|
|
|
const startTime = Date.now();
|
|
const result = await agentDB.storeBatch(patterns, embeddings);
|
|
const duration = Date.now() - startTime;
|
|
|
|
expect(result.success).toBe(true);
|
|
expect(result.insertedIds.length).toBe(100);
|
|
expect(result.errors.length).toBe(0);
|
|
|
|
// Batch insert should be fast (target: 2ms for 100 patterns, allow 100ms for testing)
|
|
expect(duration).toBeLessThan(100);
|
|
|
|
console.log(`Batch stored 100 patterns in ${duration}ms`);
|
|
});
|
|
|
|
it('should filter search results by domain and type', async () => {
|
|
// Store patterns with different domains and types
|
|
const patterns: QEPattern[] = [
|
|
createTestPattern('p1', 'test-gen', 'unit-testing'),
|
|
createTestPattern('p2', 'test-gen', 'integration-testing'),
|
|
createTestPattern('p3', 'coverage-analyzer', 'unit-testing'),
|
|
createTestPattern('p4', 'coverage-analyzer', 'integration-testing')
|
|
];
|
|
|
|
const embeddings = patterns.map(() => generateRandomEmbedding(384));
|
|
await agentDB.storeBatch(patterns, embeddings);
|
|
|
|
// Search with domain filter
|
|
const queryEmbedding = generateRandomEmbedding(384);
|
|
const domainResults = await agentDB.searchSimilar(queryEmbedding, {
|
|
k: 10,
|
|
domain: 'unit-testing'
|
|
});
|
|
|
|
expect(domainResults.every(r => r.pattern.domain === 'unit-testing')).toBe(true);
|
|
|
|
// Search with type filter
|
|
const typeResults = await agentDB.searchSimilar(queryEmbedding, {
|
|
k: 10,
|
|
type: 'test-gen'
|
|
});
|
|
|
|
expect(typeResults.every(r => r.pattern.type === 'test-gen')).toBe(true);
|
|
});
|
|
});
|
|
});
|
|
|
|
// Helper Functions
|
|
|
|
// Seeded RNG for deterministic test results
|
|
const rng = createSeededRandom(17001);
|
|
|
|
function createTestPattern(id: string, type: string, domain: string): QEPattern {
|
|
return {
|
|
id,
|
|
type,
|
|
domain,
|
|
data: { test: 'data', value: rng.random() },
|
|
confidence: 0.8,
|
|
usageCount: 0,
|
|
successCount: 0,
|
|
createdAt: Date.now(),
|
|
lastUsed: Date.now(),
|
|
metadata: {
|
|
version: '1.0.0',
|
|
author: 'test'
|
|
}
|
|
};
|
|
}
|
|
|
|
function generateRandomEmbedding(dimensions: number): number[] {
|
|
const embedding: number[] = [];
|
|
for (let i = 0; i < dimensions; i++) {
|
|
embedding.push(rng.random() * 2 - 1); // Random values between -1 and 1
|
|
}
|
|
|
|
// Normalize to unit vector
|
|
const magnitude = Math.sqrt(embedding.reduce((sum, val) => sum + val * val, 0));
|
|
return embedding.map(val => val / magnitude);
|
|
}
|
|
|
|
/**
|
|
* Linear vector search for baseline comparison
|
|
*/
|
|
async function linearVectorSearch(
|
|
query: number[],
|
|
embeddings: number[][],
|
|
k: number
|
|
): Promise<Array<{ index: number; similarity: number }>> {
|
|
const results: Array<{ index: number; similarity: number }> = [];
|
|
|
|
for (let i = 0; i < embeddings.length; i++) {
|
|
const similarity = cosineSimilarity(query, embeddings[i]);
|
|
results.push({ index: i, similarity });
|
|
}
|
|
|
|
// Sort by similarity (descending)
|
|
results.sort((a, b) => b.similarity - a.similarity);
|
|
|
|
return results.slice(0, k);
|
|
}
|
|
|
|
/**
|
|
* Calculate cosine similarity between two vectors
|
|
*/
|
|
function cosineSimilarity(a: number[], b: number[]): number {
|
|
if (a.length !== b.length) {
|
|
throw new Error('Vectors must have same length');
|
|
}
|
|
|
|
let dotProduct = 0;
|
|
let magnitudeA = 0;
|
|
let magnitudeB = 0;
|
|
|
|
for (let i = 0; i < a.length; i++) {
|
|
dotProduct += a[i] * b[i];
|
|
magnitudeA += a[i] * a[i];
|
|
magnitudeB += b[i] * b[i];
|
|
}
|
|
|
|
magnitudeA = Math.sqrt(magnitudeA);
|
|
magnitudeB = Math.sqrt(magnitudeB);
|
|
|
|
if (magnitudeA === 0 || magnitudeB === 0) {
|
|
return 0;
|
|
}
|
|
|
|
return dotProduct / (magnitudeA * magnitudeB);
|
|
}
|